Skip to content

Commit

Permalink
less command: repeat search backward operation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jul 14, 2019
1 parent a1f2c93 commit e2dd9bf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
92 changes: 60 additions & 32 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public void run(List<Source> sources) throws IOException, InterruptedException {
options.put("--IGNORE-CASE", Operation.OPT_IGNORE_CASE_ALWAYS);

Operation op;
boolean forward = true;
do {
checkInterrupted();

Expand Down Expand Up @@ -228,33 +229,7 @@ public void run(List<Source> sources) throws IOException, InterruptedException {
// Pattern edition
//
else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) == '?')) {
int c = terminal.reader().read();
message = null;
if (c == '\r') {
try {
pattern = buffer.toString().substring(1);
getPattern();
if (buffer.charAt(0) == '/') {
moveToNextMatch();
} else {
moveToPreviousMatch();
}
buffer.setLength(0);
} catch (PatternSyntaxException e) {
String str = e.getMessage();
if (str.indexOf('\n') > 0) {
str = str.substring(0, str.indexOf('\n'));
}
pattern = null;
buffer.setLength(0);
message = "Invalid pattern: " + str + " (Press a key)";
display(false);
terminal.reader().read();
message = null;
}
} else {
buffer.append((char) c);
}
forward = search();
}
//
// Command reading
Expand Down Expand Up @@ -326,11 +301,19 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) ==
break;
case REPEAT_SEARCH_BACKWARD:
case REPEAT_SEARCH_BACKWARD_SPAN_FILES:
moveToPreviousMatch();
if (forward) {
moveToPreviousMatch();
} else {
moveToNextMatch();
}
break;
case REPEAT_SEARCH_FORWARD:
case REPEAT_SEARCH_FORWARD_SPAN_FILES:
moveToNextMatch();
if (forward) {
moveToNextMatch();
} else {
moveToPreviousMatch();
}
break;
case UNDO_SEARCH:
pattern = null;
Expand Down Expand Up @@ -426,6 +409,49 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) ==
}
}

private boolean search() throws IOException, InterruptedException {
// TODO add edit line key bindings
boolean forward = true;
while (true) {
checkInterrupted();
int c = terminal.reader().read();
message = null;
if (c == '\r') {
try {
pattern = buffer.toString().substring(1);
getPattern();
if (buffer.charAt(0) == '/') {
moveToNextMatch();
} else {
if (lines.size() - firstLineToDisplay <= size.getRows() ) {
firstLineToDisplay = lines.size();
} else {
moveForward(size.getRows() - 1);
}
moveToPreviousMatch();
forward = false;
}
buffer.setLength(0);
} catch (PatternSyntaxException e) {
String str = e.getMessage();
if (str.indexOf('\n') > 0) {
str = str.substring(0, str.indexOf('\n'));
}
pattern = null;
buffer.setLength(0);
message = "Invalid pattern: " + str + " (Press a key)";
display(false);
terminal.reader().read();
message = null;
}
return forward;
} else {
buffer.append((char) c);
}
display(false);
}
}

private void help() throws IOException {
int saveSourceIdx = sourceIdx;
int saveFirstLineToDisplay = firstLineToDisplay;
Expand All @@ -450,7 +476,7 @@ private void help() throws IOException {
break;
case BACKWARD_ONE_WINDOW_OR_LINES:
moveBackward(getStrictPositiveNumberInBuffer(window));
break;
break;
}
}
display(false);
Expand Down Expand Up @@ -764,8 +790,10 @@ private void bindKeys(KeyMap<Operation> map) {
map.bind(Operation.RIGHT_ONE_HALF_SCREEN, alt(')'), key(terminal, Capability.key_right));
map.bind(Operation.LEFT_ONE_HALF_SCREEN, alt('('), key(terminal, Capability.key_left));
map.bind(Operation.FORWARD_FOREVER, "F");
map.bind(Operation.REPEAT_SEARCH_FORWARD, "n", "N");
map.bind(Operation.REPEAT_SEARCH_FORWARD_SPAN_FILES, alt('n'), alt('N'));
map.bind(Operation.REPEAT_SEARCH_FORWARD, "n");
map.bind(Operation.REPEAT_SEARCH_BACKWARD, "N");
map.bind(Operation.REPEAT_SEARCH_FORWARD_SPAN_FILES, alt('n'));
map.bind(Operation.REPEAT_SEARCH_BACKWARD_SPAN_FILES, alt('N'));
map.bind(Operation.UNDO_SEARCH, alt('u'));
map.bind(Operation.GO_TO_FIRST_LINE_OR_N, "g", "<", alt('<'));
map.bind(Operation.GO_TO_LAST_LINE_OR_N, "G", ">", alt('>'));
Expand Down
6 changes: 4 additions & 2 deletions builtins/src/main/resources/org/jline/builtins/less-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@

/pattern * Search forward for (N-th) matching line.
?pattern * Search backward for (N-th) matching line.
n N * Repeat previous search (for N-th occurrence).
ESC-n ESC-N * Repeat previous search, spanning files.
n * Repeat previous search (for N-th occurrence).
N * Repeat previous search in reverse direction.
ESC-n * Repeat previous search, spanning files.
ESC-N * Repeat previous search, reverse dir. & spanning files.
ESC-u Undo (toggle) search highlighting.
---------------------------------------------------------------------------

Expand Down

1 comment on commit e2dd9bf

@mattirn
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.