diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2017-03-31 12:20:17 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2017-03-31 12:31:23 +0200 |
| commit | 6f44057d09b865e9c7e443cd7d7adb8e541121db (patch) | |
| tree | 85306ea233bcf9acb62fbe95f7f050ab59344880 | |
| parent | 9c48a188a51a971d65a6ce764733745fd163f017 (diff) | |
| download | vis-6f44057d09b865e9c7e443cd7d7adb8e541121db.tar.gz vis-6f44057d09b865e9c7e443cd7d7adb8e541121db.tar.xz | |
vis: add non-default actions for vi compatible n/N motions
The following key mappings should result in the vi behavior:
:map! normal n <vis-motion-search-repeat>
:map! normal N <vis-motion-search-repeat-reverse>
The default remains unchanged, that is `n` (`N`) always searches towards
the end (start) of the file.
Fix #470
| -rw-r--r-- | main.c | 12 | ||||
| -rw-r--r-- | vis-core.h | 1 | ||||
| -rw-r--r-- | vis-motions.c | 23 | ||||
| -rw-r--r-- | vis.h | 2 |
4 files changed, 36 insertions, 2 deletions
@@ -173,6 +173,8 @@ enum { VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM, VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD, VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD, + VIS_ACTION_CURSOR_SEARCH_REPEAT, + VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE, VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD, VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD, VIS_ACTION_WINDOW_PAGE_UP, @@ -525,6 +527,16 @@ static const KeyAction vis_action[] = { VIS_HELP("Move cursor to previous match in backward direction") movement, { .i = VIS_MOVE_SEARCH_REPEAT_BACKWARD } }, + [VIS_ACTION_CURSOR_SEARCH_REPEAT] = { + "vis-motion-search-repeat", + VIS_HELP("Move cursor to next match") + movement, { .i = VIS_MOVE_SEARCH_REPEAT } + }, + [VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE] = { + "vis-motion-search-repeat-reverse", + VIS_HELP("Move cursor to next match in opposite direction") + movement, { .i = VIS_MOVE_SEARCH_REPEAT_REVERSE } + }, [VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD] = { "vis-motion-search-word-forward", VIS_HELP("Move cursor to next occurence of the word under cursor") @@ -169,6 +169,7 @@ struct Vis { Mode *mode_before_prompt; /* user mode which was active before entering prompt */ char search_char[8]; /* last used character to search for via 'f', 'F', 't', 'T' */ int last_totill; /* last to/till movement used for ';' and ',' */ + int search_direction; /* used for `n` and `N` */ int tabwidth; /* how many spaces should be used to display a tab */ bool expandtab; /* whether typed tabs should be converted to spaces */ bool autoindent; /* whether indentation should be copied from previous line on newline */ diff --git a/vis-motions.c b/vis-motions.c index 7007f47..1ceb958 100644 --- a/vis-motions.c +++ b/vis-motions.c @@ -26,16 +26,20 @@ static Regex *search_word(Vis *vis, Text *txt, size_t pos) { static size_t search_word_forward(Vis *vis, Text *txt, size_t pos) { Regex *regex = search_word(vis, txt, pos); - if (regex) + if (regex) { + vis->search_direction = VIS_MOVE_SEARCH_REPEAT_FORWARD; pos = text_search_forward(txt, pos, regex); + } text_regex_free(regex); return pos; } static size_t search_word_backward(Vis *vis, Text *txt, size_t pos) { Regex *regex = search_word(vis, txt, pos); - if (regex) + if (regex) { + vis->search_direction = VIS_MOVE_SEARCH_REPEAT_BACKWARD; pos = text_search_backward(txt, pos, regex); + } text_regex_free(regex); return pos; } @@ -331,6 +335,21 @@ bool vis_motion(Vis *vis, enum VisMotion motion, ...) { motion = VIS_MOVE_SEARCH_REPEAT_FORWARD; else motion = VIS_MOVE_SEARCH_REPEAT_BACKWARD; + vis->search_direction = motion; + break; + } + case VIS_MOVE_SEARCH_REPEAT: + case VIS_MOVE_SEARCH_REPEAT_REVERSE: + { + if (!vis->search_direction) + vis->search_direction = VIS_MOVE_SEARCH_REPEAT_FORWARD; + if (motion == VIS_MOVE_SEARCH_REPEAT) { + motion = vis->search_direction; + } else { + motion = vis->search_direction == VIS_MOVE_SEARCH_REPEAT_FORWARD ? + VIS_MOVE_SEARCH_REPEAT_BACKWARD : + VIS_MOVE_SEARCH_REPEAT_FORWARD; + } break; } case VIS_MOVE_RIGHT_TO: @@ -322,6 +322,8 @@ enum VisMotion { VIS_MOVE_TOTILL_REVERSE, VIS_MOVE_SEARCH_FORWARD, VIS_MOVE_SEARCH_BACKWARD, + VIS_MOVE_SEARCH_REPEAT, + VIS_MOVE_SEARCH_REPEAT_REVERSE, VIS_MOVE_LAST, /* denotes the end of all motions */ }; |
