Skip to content

Commit

Permalink
Support for 'Y' (yank-whole-line command), fixes #85
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jan 4, 2017
1 parent 691e876 commit 12dd8cc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
33 changes: 30 additions & 3 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,23 @@ protected boolean viYankTo() {
}
}

protected boolean viYankWholeLine() {
int s, e;
int p = buf.cursor();
while (buf.move(-1) == -1 && buf.prevChar() != '\n') ;
s = buf.cursor();
for (int i = 0; i < repeatCount; i++) {
while (buf.move(1) == 1 && buf.prevChar() != '\n') ;
}
e = buf.cursor();
yankBuffer = buf.substring(s, e);
if (!yankBuffer.endsWith("\n")) {
yankBuffer += "\n";
}
buf.cursor(p);
return true;
}

protected boolean viChange() {
int cursorStart = buf.cursor();
Binding o = readBinding(getKeys());
Expand Down Expand Up @@ -2948,7 +2965,12 @@ protected boolean viOpenLineBelow() {
* and moves the cursor to the end of the pasted region.
*/
protected boolean viPutAfter() {
if (yankBuffer.length () != 0) {
if (yankBuffer.indexOf('\n') >= 0) {
while (buf.move(1) == 1 && buf.currChar() != '\n');
buf.move(1);
putString(yankBuffer);
buf.move(- yankBuffer.length());
} else if (yankBuffer.length () != 0) {
if (buf.cursor() < buf.length()) {
buf.move(1);
}
Expand All @@ -2961,7 +2983,11 @@ protected boolean viPutAfter() {
}

protected boolean viPutBefore() {
if (yankBuffer.length () != 0) {
if (yankBuffer.indexOf('\n') >= 0) {
while (buf.move(-1) == -1 && buf.prevChar() != '\n');
putString(yankBuffer);
buf.move(- yankBuffer.length());
} else if (yankBuffer.length () != 0) {
if (buf.cursor() > 0) {
buf.move(-1);
}
Expand Down Expand Up @@ -3223,6 +3249,7 @@ protected Map<String, Widget> builtinWidgets() {
widgets.put(VI_SWAP_CASE, this::viSwapCase);
widgets.put(VI_UP_LINE_OR_HISTORY, this::viUpLineOrHistory);
widgets.put(VI_YANK, this::viYankTo);
widgets.put(VI_YANK_WHOLE_LINE, this::viYankWholeLine);
widgets.put(VISUAL_LINE_MODE, this::visualLineMode);
widgets.put(VISUAL_MODE, this::visualMode);
widgets.put(WHAT_CURSOR_POSITION, this::whatCursorPosition);
Expand Down Expand Up @@ -5066,7 +5093,7 @@ public KeyMap<Binding> viCmd() {
bind(vicmd, VISUAL_LINE_MODE, "V");
bind(vicmd, VI_FORWARD_BLANK_WORD, "W");
bind(vicmd, VI_BACKWARD_DELETE_CHAR, "X");
bind(vicmd, VI_YANK, "Y");
bind(vicmd, VI_YANK_WHOLE_LINE, "Y");
bind(vicmd, VI_FIRST_NON_BLANK, "^");
bind(vicmd, VI_ADD_NEXT, "a");
bind(vicmd, VI_BACKWARD_WORD, "b");
Expand Down
35 changes: 35 additions & 0 deletions reader/src/test/java/org/jline/reader/impl/ViMoveModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,41 @@ public void testEndOfLine() throws Exception {
assertLine("chicken sushimiicken sushimi", b, false);
}

@Test
public void testYankLines() {
/*
* Yank whole line and put after
*/
TestBuffer b = (new TestBuffer("chicken"))
.escape()
.append("o")
.append("sushi")
.escape()
.append("o")
.append("pork")
.escape()
.up()
.append("2Ypiz")
.enter();
assertLine("chicken\nsushi\nzsushi\npork\npork", b, false);

/*
* Yank whole line and put before
*/
b = (new TestBuffer("chicken"))
.escape()
.append("o")
.append("sushi")
.escape()
.append("o")
.append("pork")
.escape()
.up()
.append("2YPiz")
.enter();
assertLine("chicken\nzsushi\npork\nsushi\npork", b, false);
}

@Test
public void firstPrintable() throws Exception {
TestBuffer b = (new TestBuffer(" foo bar"))
Expand Down

0 comments on commit 12dd8cc

Please sign in to comment.