diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:39:28 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:39:28 -0600 |
| commit | 95bf87d4ed037e6d6786cc395d94dbe44ee88e32 (patch) | |
| tree | 0d0bbbf26871a00a58c980af01ac13bdec886bda | |
| parent | d538cc8ce9c32c932e12134b7bd59a88e88d748c (diff) | |
| download | zash-95bf87d4ed037e6d6786cc395d94dbe44ee88e32.tar.gz | |
fix(edit): vi search reads its pattern into a buffer of its own
`/` wrote the new pattern over the old one in ed.search and only wrote a
terminator when something had been typed. Type `ban` over `apple`, delete
the three letters, press Enter: the buffer still held "banle", so the
"empty pattern repeats the previous search" case searched a mix of the two
and found nothing (the terminal just beeped).
vi_read_pattern() collects the pattern in a local array and copies it into
ed.search only when it is non-empty; an empty pattern now really does leave
the previous one alone.
Test: tests/linux/i-vi.sh searches for apple, then types `ban` + three
backspaces + Enter, and expects `echo apple` back. Without the change the
line stays empty.
Verified on Linux with make check.
| -rw-r--r-- | src/edit_builtin.c | 13 | ||||
| -rw-r--r-- | tests/linux/i-vi.exp | 8 | ||||
| -rw-r--r-- | tests/linux/i-vi.sh | 10 |
3 files changed, 27 insertions, 4 deletions
diff --git a/src/edit_builtin.c b/src/edit_builtin.c index b0558f4..bc66b5a 100644 --- a/src/edit_builtin.c +++ b/src/edit_builtin.c @@ -1161,11 +1161,14 @@ static bool vi_search(bool backwards) { * @return false if the user gave up with ESC or an interrupt. */ static bool vi_read_pattern(char lead) { + /* Read into a buffer of its own: typing over the old pattern and then + * backspacing past it would otherwise leave a mix of two patterns. */ + char pat[sizeof ed.search]; size_t n = 0; for (;;) { ob_putc('\r'); ob_putc(lead); - ob_put(ed.search, n); + ob_put(pat, n); ob_csi(0, 'K'); ob_flush(); int k = read_key(); @@ -1179,12 +1182,14 @@ static bool vi_read_pattern(char lead) { if (k == CH_DEL || k == CH_CTRL_H) { if (n) n--; - } else if (k < 0x100 && !iscntrl(k) && n + 1 < sizeof ed.search) { - ed.search[n++] = (char)k; + } else if (k < 0x100 && !iscntrl(k) && n + 1 < sizeof pat) { + pat[n++] = (char)k; } } - if (n) /* an empty pattern repeats the previous search */ + if (n) { /* an empty pattern repeats the previous search */ + memcpy(ed.search, pat, n); ed.search[n] = '\0'; + } return ed.search[0] != '\0'; } diff --git a/tests/linux/i-vi.exp b/tests/linux/i-vi.exp index 91ef7c6..891cde4 100644 --- a/tests/linux/i-vi.exp +++ b/tests/linux/i-vi.exp @@ -35,5 +35,13 @@ $ abcdef SHELL: abcdef: not found $ echo bdef bdef +$ echo apple +apple +$ echo banana +banana +$ echo apple +apple +$ echo apple +apple $ exit [status 0] diff --git a/tests/linux/i-vi.sh b/tests/linux/i-vi.sh index 348d14e..d5fb71f 100644 --- a/tests/linux/i-vi.sh +++ b/tests/linux/i-vi.sh @@ -44,5 +44,15 @@ send echo abcdef\x1b0wx send l. send \r expect echo bdef +# `/` typed over the previous pattern and backspaced to empty repeats the +# previous search, not a mix of the two patterns. +send echo apple\r +expect apple +send echo banana\r +expect banana +send \x1b/apple\r\r +expect echo apple +send \x1b/ban\x7f\x7f\x7f\r\r +expect echo apple send exit\r END |
