From ff1f07a1affe6858dfddb27edc8655be91b6400d Mon Sep 17 00:00:00 2001 From: mattirn Date: Fri, 20 Sep 2019 18:57:35 +0200 Subject: [PATCH] less command: implemented search operations with spanning files --- .../main/java/org/jline/builtins/Less.java | 74 +++++++++++++++---- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/builtins/src/main/java/org/jline/builtins/Less.java b/builtins/src/main/java/org/jline/builtins/Less.java index b76f8f5d0..b50a78164 100644 --- a/builtins/src/main/java/org/jline/builtins/Less.java +++ b/builtins/src/main/java/org/jline/builtins/Less.java @@ -114,7 +114,6 @@ public class Less { private List syntaxFiles = new ArrayList<>(); private boolean highlight = true; - public Less(Terminal terminal, Path currentDir) { this(terminal, currentDir, null); } @@ -477,21 +476,17 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) == case RIGHT_ONE_HALF_SCREEN: firstColumnToDisplay += size.getColumns() / 2; break; - case REPEAT_SEARCH_BACKWARD: case REPEAT_SEARCH_BACKWARD_SPAN_FILES: - if (forward) { - moveToPreviousMatch(); - } else { - moveToNextMatch(); - } + moveToMatch(!forward, true); + break; + case REPEAT_SEARCH_BACKWARD: + moveToMatch(!forward, false); break; - case REPEAT_SEARCH_FORWARD: case REPEAT_SEARCH_FORWARD_SPAN_FILES: - if (forward) { - moveToNextMatch(); - } else { - moveToPreviousMatch(); - } + moveToMatch(forward, true); + break; + case REPEAT_SEARCH_FORWARD: + moveToMatch(forward, false); break; case UNDO_SEARCH: pattern = null; @@ -645,6 +640,14 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) == } } + private void moveToMatch(boolean forward, boolean spanFiles) throws IOException { + if (forward) { + moveToNextMatch(spanFiles); + } else { + moveToPreviousMatch(spanFiles); + } + } + private class LineEditor { private int begPos; @@ -976,7 +979,7 @@ protected void openSource() throws IOException { wasOpen = true; } boolean open = false; - boolean displayMessage = false; + boolean displayMessage = false; do { Source source = sources.get(sourceIdx); try { @@ -1046,6 +1049,10 @@ void moveTo(int lineNum) throws IOException { } private void moveToNextMatch() throws IOException { + moveToNextMatch(false); + } + + private void moveToNextMatch(boolean spanFiles) throws IOException { Pattern compiled = getPattern(); Pattern dpCompiled = getPattern(true); if (compiled != null) { @@ -1063,10 +1070,29 @@ private void moveToNextMatch() throws IOException { } } } - message = "Pattern not found"; + if (spanFiles) { + if (sourceIdx < sources.size() - 1) { + SavedSourcePositions ssp = new SavedSourcePositions(); + String newSource = sources.get(++sourceIdx).getName(); + try { + openSource(); + moveToNextMatch(true); + } catch (FileNotFoundException exp) { + ssp.restore(newSource); + } + } else { + message = "Pattern not found"; + } + } else { + message = "Pattern not found"; + } } private void moveToPreviousMatch() throws IOException { + moveToPreviousMatch(false); + } + + private void moveToPreviousMatch(boolean spanFiles) throws IOException { Pattern compiled = getPattern(); Pattern dpCompiled = getPattern(true); if (compiled != null) { @@ -1084,7 +1110,23 @@ private void moveToPreviousMatch() throws IOException { } } } - message = "Pattern not found"; + if (spanFiles) { + if (sourceIdx > 1) { + SavedSourcePositions ssp = new SavedSourcePositions(-1); + String newSource = sources.get(--sourceIdx).getName(); + try { + openSource(); + firstLineToDisplay = (int)(long)sources.get(sourceIdx).lines(); + moveToPreviousMatch(true); + } catch (FileNotFoundException exp) { + ssp.restore(newSource); + } + } else { + message = "Pattern not found"; + } + } else { + message = "Pattern not found"; + } } private String printable(String s) {