summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston2021-03-28 13:23:12 +0100
committerDenys Vlasenko2021-03-29 12:16:21 +0200
commitd56da680577ae5ad43833040a310706e8331c684 (patch)
treef49cd7e3e3361fc98e488a30c7839db76101d9c4
parent8b571bd7b507c5dbdf8bb2bd804047ed66ea9809 (diff)
downloadbusybox-d56da680577ae5ad43833040a310706e8331c684.zip
busybox-d56da680577ae5ad43833040a310706e8331c684.tar.gz
vi: improve operations involving paragraph movement
Movement by paragraph doesn't always involve whole lines. If the cursor is positioned in the middle of a line deleting to either end of the paragraph will result in one partial line and zero or more full lines. Adjust the end of ranges delimited by paragraph movement to more closely match what vi does. function old new delta find_range 467 518 +51 at_eof - 49 +49 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/0 up/down: 100/0) Total: 100 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/vi.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/editors/vi.c b/editors/vi.c
index 5d4b0f2..323ca44 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -3051,6 +3051,12 @@ static void int_handler(int sig)
static void do_cmd(int c);
+static int at_eof(const char *s)
+{
+ // does 's' point to end of file, even with no terminating newline?
+ return ((s == end - 2 && s[1] == '\n') || s == end - 1);
+}
+
static int find_range(char **start, char **stop, char c)
{
char *save_dot, *p, *q, *t;
@@ -3064,7 +3070,7 @@ static int find_range(char **start, char **stop, char c)
buftype = WHOLE;
if (--cmdcnt > 0)
do_cmd('j');
- } else if (strchr("^%$0bBeEfFtTh|\b\177", c)) {
+ } else if (strchr("^%$0bBeEfFtTh|{}\b\177", c)) {
// These cmds operate on char positions
buftype = PARTIAL;
do_cmd(c); // execute movement cmd
@@ -3074,9 +3080,9 @@ static int find_range(char **start, char **stop, char c)
buftype = MULTI;
do_cmd(c); // execute movement cmd
// step back one char, but not if we're at end of file
- if (dot > p && !((dot == end - 2 && end[-1] == '\n') || dot == end - 1))
+ if (dot > p && !at_eof(dot))
dot--;
- } else if (strchr("GHL+-jk{}\r\n", c)) {
+ } else if (strchr("GHL+-jk\r\n", c)) {
// these operate on whole lines
buftype = WHOLE;
do_cmd(c); // execute movement cmd
@@ -3101,14 +3107,26 @@ static int find_range(char **start, char **stop, char c)
p = t;
}
+ // movements which don't include end of range
+ if (q > p) {
+ if (strchr("^0bBFTh|\b\177", c)) {
+ q--;
+ } else if (strchr("{}", c)) {
+ buftype = (p == begin_line(p) && (*q == '\n' || at_eof(q))) ?
+ WHOLE : MULTI;
+ if (!at_eof(q)) {
+ q--;
+ if (q > p && p != begin_line(p))
+ q--;
+ }
+ }
+ }
+
if (buftype == WHOLE) {
p = begin_line(p);
q = end_line(q);
}
- // movements which don't include end of range
- if (q > p && strchr("^0bBFTh|\b\177", c)) q--;
-
*start = p;
*stop = q;
dot = save_dot;