Skip to content

Commit

Permalink
less command: fix next/prev page operation, fixes #434
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 5, 2019
1 parent f3bea60 commit f1e3820
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2018, the original author or authors.
* Copyright (c) 2002-2019, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -66,7 +66,7 @@ public class Less {
public boolean ignoreCaseAlways;
public boolean noKeypad;
public boolean noInit;

protected List<Integer> tabs = Arrays.asList(4);
protected final Terminal terminal;
protected final Display display;
Expand Down Expand Up @@ -363,6 +363,7 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) ==
offsetInLine = 0;
chopLongLines = !chopLongLines;
message = chopLongLines ? "Chop long lines" : "Fold long lines";
display.clear();
break;
case OPT_IGNORE_CASE_COND:
ignoreCaseCond = !ignoreCaseCond;
Expand Down Expand Up @@ -410,7 +411,7 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) ==
case GOTO_FILE:
int tofile = getStrictPositiveNumberInBuffer(1);
if (tofile < sources.size()) {
SavedSourcePositions ssp = new SavedSourcePositions(tofile < sourceIdx ? -1 : 0);
SavedSourcePositions ssp = new SavedSourcePositions(tofile < sourceIdx ? -1 : 0);
sourceIdx = tofile;
String newSource = sources.get(sourceIdx).getName();
try {
Expand Down Expand Up @@ -572,14 +573,14 @@ public int editBuffer(Operation op, int curPos) {
return curPos;
}
}

private class SavedSourcePositions {
int saveSourceIdx;
int saveFirstLineToDisplay;
int saveFirstColumnToDisplay;
int saveOffsetInLine;
boolean savePrintLineNumbers;

public SavedSourcePositions (){
this(0);
}
Expand All @@ -590,7 +591,7 @@ public SavedSourcePositions (int dec){
saveOffsetInLine = offsetInLine;
savePrintLineNumbers = printLineNumbers;
}

public void restore(String failingSource) throws IOException {
sourceIdx = saveSourceIdx;
openSource();
Expand All @@ -601,7 +602,7 @@ public void restore(String failingSource) throws IOException {
if (failingSource != null) {
message = failingSource + " not found!";
}
}
}
}

private void addSource(String file) throws IOException {
Expand Down Expand Up @@ -677,11 +678,11 @@ private boolean search() throws IOException, InterruptedException {
searchKeyMap.bind(Operation.HOME, key(terminal, Capability.key_home), alt('0'));
searchKeyMap.bind(Operation.END, key(terminal, Capability.key_end), alt('$'));
searchKeyMap.bind(Operation.BACKSPACE, del());
searchKeyMap.bind(Operation.DELETE, alt('x'));
searchKeyMap.bind(Operation.DELETE, alt('x'));
searchKeyMap.bind(Operation.DELETE_WORD, alt('X'));
searchKeyMap.bind(Operation.DELETE_LINE, ctrl('U'));
searchKeyMap.bind(Operation.UP, key(terminal, Capability.key_up), alt('k'));
searchKeyMap.bind(Operation.DOWN, key(terminal, Capability.key_down), alt('j'));
searchKeyMap.bind(Operation.DOWN, key(terminal, Capability.key_down), alt('j'));
searchKeyMap.bind(Operation.ACCEPT, "\r");

boolean forward = true;
Expand Down Expand Up @@ -778,7 +779,7 @@ private boolean search() throws IOException, InterruptedException {
}
}
}

private void help() throws IOException {
SavedSourcePositions ssp = new SavedSourcePositions();
printLineNumbers = false;
Expand Down Expand Up @@ -862,13 +863,14 @@ protected void openSource() throws IOException {
}
} while (!open && sourceIdx > 0);
if (!open) {
throw new FileNotFoundException();
throw new FileNotFoundException();
}
}

void moveTo(int lineNum) throws IOException {
AttributedString line = getLine(lineNum);
if (line != null){
display.clear();
if (firstLineInMemory > lineNum) {
openSource();
}
Expand All @@ -878,7 +880,7 @@ void moveTo(int lineNum) throws IOException {
message = "Cannot seek to line number " + (lineNum + 1);
}
}

private void moveToNextMatch() throws IOException {
Pattern compiled = getPattern();
Pattern dpCompiled = getPattern(true);
Expand All @@ -890,6 +892,7 @@ private void moveToNextMatch() throws IOException {
} else if (!toBeDisplayed(line, dpCompiled)) {
continue;
} else if (compiled.matcher(line).find()) {
display.clear();
firstLineToDisplay = lineNumber;
offsetInLine = 0;
return;
Expand All @@ -910,6 +913,7 @@ private void moveToPreviousMatch() throws IOException {
} else if (!toBeDisplayed(line, dpCompiled)) {
continue;
} else if (compiled.matcher(line).find()) {
display.clear();
firstLineToDisplay = lineNumber;
offsetInLine = 0;
return;
Expand Down Expand Up @@ -941,6 +945,9 @@ void moveForward(int lines) throws IOException {
int width = size.getColumns() - (printLineNumbers ? 8 : 0);
int height = size.getRows();
boolean doOffsets = firstColumnToDisplay == 0 && !chopLongLines;
if (lines >= size.getRows() - 1) {
display.clear();
}
if (lines == Integer.MAX_VALUE) {
Long allLines = sources.get(sourceIdx).lines();
if (allLines != null) {
Expand All @@ -949,7 +956,7 @@ void moveForward(int lines) throws IOException {
firstLineToDisplay = prevLine2display(firstLineToDisplay, dpCompiled).getU();
}
}
}
}
while (--lines >= 0) {
int lastLineToDisplay = firstLineToDisplay;
if (!doOffsets) {
Expand Down Expand Up @@ -991,6 +998,9 @@ void moveForward(int lines) throws IOException {
void moveBackward(int lines) throws IOException {
Pattern dpCompiled = getPattern(true);
int width = size.getColumns() - (printLineNumbers ? 8 : 0);
if (lines >= size.getRows() - 1) {
display.clear();
}
while (--lines >= 0) {
if (offsetInLine > 0) {
offsetInLine = Math.max(0, offsetInLine - width);
Expand Down Expand Up @@ -1039,15 +1049,15 @@ int getStrictPositiveNumberInBuffer(int def) {
buffer.setLength(0);
}
}

private Pair<Integer, AttributedString> nextLine2display(int line, Pattern dpCompiled) throws IOException {
AttributedString curLine = null;
do {
curLine = getLine(line++);
} while (!toBeDisplayed(curLine, dpCompiled));
return new Pair<>(line, curLine);
}

private Pair<Integer, AttributedString> prevLine2display(int line, Pattern dpCompiled) throws IOException {
AttributedString curLine = null;
do {
Expand All @@ -1062,11 +1072,11 @@ private Pair<Integer, AttributedString> prevLine2display(int line, Pattern dpCom
private boolean toBeDisplayed(AttributedString curLine, Pattern dpCompiled) {
return curLine == null || dpCompiled == null || sourceIdx == 0 || dpCompiled.matcher(curLine).find();
}

synchronized boolean display(boolean oneScreen) throws IOException {
return display(oneScreen, null);
}

synchronized boolean display(boolean oneScreen, Integer curPos) throws IOException {
List<AttributedString> newLines = new ArrayList<>();
int width = size.getColumns() - (printLineNumbers ? 8 : 0);
Expand Down Expand Up @@ -1156,7 +1166,7 @@ synchronized boolean display(boolean oneScreen, Integer curPos) throws IOExcepti
if (curPos == null) {
display.update(newLines, -1);
} else {
display.update(newLines, size.cursorPos(size.getRows() - 1, curPos + 1));
display.update(newLines, size.cursorPos(size.getRows() - 1, curPos + 1));
}
return false;
}
Expand Down Expand Up @@ -1218,7 +1228,7 @@ private void bindKeys(KeyMap<Operation> map) {
map.bind(Operation.LEFT_ONE_HALF_SCREEN, alt('('), key(terminal, Capability.key_left));
map.bind(Operation.FORWARD_FOREVER, "F");
map.bind(Operation.REPAINT, "r", ctrl('R'), ctrl('L'));
map.bind(Operation.REPAINT_AND_DISCARD, "R");
map.bind(Operation.REPAINT_AND_DISCARD, "R");
map.bind(Operation.REPEAT_SEARCH_FORWARD, "n");
map.bind(Operation.REPEAT_SEARCH_BACKWARD, "N");
map.bind(Operation.REPEAT_SEARCH_FORWARD_SPAN_FILES, alt('n'));
Expand Down Expand Up @@ -1293,8 +1303,8 @@ protected enum Operation {
GOTO_FILE,
INFO_FILE,
DELETE_FILE,
//

//
CHAR,

// Edit pattern
Expand Down

0 comments on commit f1e3820

Please sign in to comment.