vi: improve motion by paragraph

When moving by paragraph ('{' and '}'):

- Treat multiple empty lines as a single paragraph separator.

- When no paragraph separator is found move to the start or end of
  the file depending on the direction of motion.

function                                             old     new   delta
do_cmd                                              4821    4900     +79
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 79/0)               Total: 79 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Ron Yorston 2021-03-28 13:22:43 +01:00 committed by Denys Vlasenko
parent d3b74826c5
commit 8b571bd7b5

View File

@ -3515,12 +3515,20 @@ static void do_cmd(int c)
case '{': // {- move backward paragraph
case '}': // }- move forward paragraph
do {
q = char_search(dot, "\n\n", c == '{' ?
((unsigned)BACK << 1) | FULL :
(FORWARD << 1) | FULL);
dir = c == '}' ? FORWARD : BACK;
// skip over consecutive empty lines
while ((dir == FORWARD ? dot < end - 1 : dot > text) &&
*dot == '\n' && dot[dir] == '\n') {
dot += dir;
}
q = char_search(dot, "\n\n", ((unsigned)dir << 1) | FULL);
if (q != NULL) { // found blank line
dot = next_line(q); // move to next blank line
}
else { // blank line not found, move to end of file
dot = dir == FORWARD ? end - 1 : text;
break;
}
} while (--cmdcnt > 0);
break;
#endif /* FEATURE_VI_SEARCH */