Skip to content

Commit

Permalink
Make emacs(Backward|Forward)Word aliases to forwardWord/forwardWord, …
Browse files Browse the repository at this point in the history
…add tests, fixes #644
  • Loading branch information
gnodet committed Jan 26, 2021
1 parent 859bc82 commit bcdd8ef
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 33 deletions.
35 changes: 2 additions & 33 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1407,21 +1407,7 @@ && isWhitespace(buf.currChar())) {
}

protected boolean emacsForwardWord() {
if (count < 0) {
return callNeg(this::emacsBackwardWord);
}
while (count-- > 0) {
while (buf.cursor() < buf.length() && !isWord(buf.currChar())) {
buf.move(1);
}
if (isInViChangeOperation() && count == 0) {
return true;
}
while (buf.cursor() < buf.length() && isWord(buf.currChar())) {
buf.move(1);
}
}
return true;
return forwardWord();
}

protected boolean viForwardBlankWordEnd() {
Expand Down Expand Up @@ -1596,24 +1582,7 @@ protected boolean viBackwardBlankWordEnd() {
}

protected boolean emacsBackwardWord() {
if (count < 0) {
return callNeg(this::emacsForwardWord);
}
while (count-- > 0) {
while (buf.cursor() > 0) {
buf.move(-1);
if (isWord(buf.currChar())) {
break;
}
}
while (buf.cursor() > 0) {
buf.move(-1);
if (!isWord(buf.currChar())) {
break;
}
}
}
return true;
return backwardWord();
}

protected boolean backwardDeleteWord() {
Expand Down
111 changes: 111 additions & 0 deletions reader/src/test/java/org/jline/reader/impl/EditLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.jline.reader.impl;

import org.jline.keymap.KeyMap;
import org.jline.reader.LineReader;
import org.jline.reader.Reference;
import org.junit.Test;
Expand All @@ -16,6 +17,8 @@
import static org.jline.reader.LineReader.BACKWARD_KILL_LINE;
import static org.jline.reader.LineReader.BACKWARD_KILL_WORD;
import static org.jline.reader.LineReader.BACKWARD_WORD;
import static org.jline.reader.LineReader.EMACS_BACKWARD_WORD;
import static org.jline.reader.LineReader.EMACS_FORWARD_WORD;
import static org.jline.reader.LineReader.END_OF_LINE;
import static org.jline.reader.LineReader.FORWARD_WORD;
import static org.jline.reader.LineReader.KILL_WORD;
Expand Down Expand Up @@ -123,6 +126,114 @@ public void testPreviousWord() throws Exception {
.append('X'));
}

@Test
public void testBackwardWord() throws Exception {
assertBuffer("This is a Xtest",
new TestBuffer("This is a test").op(BACKWARD_WORD)
.append('X'));

assertBuffer("This is Xa test",
new TestBuffer("This is a test").op(BACKWARD_WORD).op(BACKWARD_WORD)
.append('X'));
}

@Test
public void testForwardWord() throws Exception {
assertBuffer("This Xis a test",
new TestBuffer("This is a test").ctrlA().op(FORWARD_WORD)
.append('X'));

assertBuffer("This is Xa test",
new TestBuffer("This is a test").ctrlA().op(FORWARD_WORD).op(FORWARD_WORD)
.append('X'));
}

@Test
public void testBackwardWordWithSeparator() throws Exception {
// Use an empty string for WORDCHARS so that / is not treated as part of the word
reader.setVariable(LineReader.WORDCHARS, "");

assertBuffer("/tmp/foo/Xmoo",
new TestBuffer("/tmp/foo/moo").op(BACKWARD_WORD)
.append('X'));

assertBuffer("/tmp/Xfoo/moo",
new TestBuffer("/tmp/foo/moo").op(BACKWARD_WORD).op(BACKWARD_WORD)
.append('X'));
}

@Test
public void testForwardWordWithSeparator() throws Exception {
// Use an empty string for WORDCHARS so that / is not treated as part of the word
reader.setVariable(LineReader.WORDCHARS, "");

assertBuffer("/Xtmp/foo/moo",
new TestBuffer("/tmp/foo/moo").ctrlA().op(FORWARD_WORD)
.append('X'));

assertBuffer("/tmp/Xfoo/moo",
new TestBuffer("/tmp/foo/moo").ctrlA().op(FORWARD_WORD).op(FORWARD_WORD)
.append('X'));
}

@Test
public void testEmacsBackwardWord() throws Exception {
reader.getKeys().bind(new Reference(LineReader.EMACS_BACKWARD_WORD), KeyMap.alt('b'));

assertBuffer("This is a Xtest",
new TestBuffer("This is a test").op(BACKWARD_WORD)
.append('X'));

assertBuffer("This is Xa test",
new TestBuffer("This is a test").op(BACKWARD_WORD).op(BACKWARD_WORD)
.append('X'));
}

@Test
public void testEmacsForwardWord() throws Exception {
reader.getKeys().bind(new Reference(LineReader.EMACS_FORWARD_WORD), KeyMap.alt('f'));

assertBuffer("This Xis a test",
new TestBuffer("This is a test").ctrlA().op(FORWARD_WORD)
.append('X'));

assertBuffer("This is Xa test",
new TestBuffer("This is a test").ctrlA().op(FORWARD_WORD).op(FORWARD_WORD)
.append('X'));
}

@Test
public void testEmacsBackwardWordWithSeparator() throws Exception {
reader.getKeys().bind(new Reference(LineReader.EMACS_BACKWARD_WORD), KeyMap.alt('b'));

// Use an empty string for WORDCHARS so that / is not treated as part of the word
reader.setVariable(LineReader.WORDCHARS, "");

assertBuffer("/tmp/foo/Xmoo",
new TestBuffer("/tmp/foo/moo").op(BACKWARD_WORD)
.append('X'));

assertBuffer("/tmp/Xfoo/moo",
new TestBuffer("/tmp/foo/moo").op(BACKWARD_WORD).op(BACKWARD_WORD)
.append('X'));
}

@Test
public void testEmacsForwardWordWithSeparator() throws Exception {
reader.getKeys().bind(new Reference(LineReader.EMACS_FORWARD_WORD), KeyMap.alt('f'));

// Use an empty string for WORDCHARS so that / is not treated as part of the word
reader.setVariable(LineReader.WORDCHARS, "");

assertBuffer("/Xtmp/foo/moo",
new TestBuffer("/tmp/foo/moo").ctrlA().op(FORWARD_WORD)
.append('X'));

assertBuffer("/tmp/Xfoo/moo",
new TestBuffer("/tmp/foo/moo").ctrlA().op(FORWARD_WORD).op(FORWARD_WORD)
.append('X'));
}

@Test
public void testLineStart() throws Exception {
assertBuffer("XThis is a test",
Expand Down

0 comments on commit bcdd8ef

Please sign in to comment.