Skip to content

Commit

Permalink
vis: add non-default actions for vi compatible n/N motions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
martanne committed Mar 31, 2017
1 parent 9c48a18 commit 6f44057
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
12 changes: 12 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand 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")
Expand Down
1 change: 1 addition & 0 deletions vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
23 changes: 21 additions & 2 deletions vis-motions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
};

Expand Down

0 comments on commit 6f44057

Please sign in to comment.