diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-18 13:05:38 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-18 13:05:38 -0600 |
| commit | bc0cab203252597d29cbce7fcba0ca4fadfbc846 (patch) | |
| tree | d3a7f65bca4fcd2960c6a0cb6b2a2fa1670850a4 | |
| parent | e46eeeea900703956783d835edd800567989ceb0 (diff) | |
| download | zash-bc0cab203252597d29cbce7fcba0ca4fadfbc846.tar.gz | |
edit_builtin: the emacs keys bash users reach for
meta-u, meta-l and meta-c change the case of a word, meta-t transposes
the words either side of the cursor, meta-. inserts the last word of the
previous line and meta-\\ collapses the blanks around the cursor. The
rest of the map was already bash's: a BSD sh or a bash prompt is the
target, not ksh's.
| -rw-r--r-- | src/edit_builtin.c | 74 | ||||
| -rw-r--r-- | tests/linux/i-emacs.exp | 14 | ||||
| -rw-r--r-- | tests/linux/i-emacs.sh | 16 |
3 files changed, 102 insertions, 2 deletions
diff --git a/src/edit_builtin.c b/src/edit_builtin.c index d996fc2..49078e5 100644 --- a/src/edit_builtin.c +++ b/src/edit_builtin.c @@ -2,8 +2,8 @@ * @file edit_builtin.c * @brief The built-in line editor. * - * A small editor in the style of the emacs and vi modes of the Korn - * shell. It needs nothing but a terminal that understands a handful of + * A small editor with the emacs and vi modes of bash and the BSD shells, + * and with bash's default key bindings in emacs mode. It needs nothing but a terminal that understands a handful of * ANSI control sequences (carriage return, "erase to end of line", * "cursor right N", "clear screen"), so it has no terminfo or curses * dependency, which matters on z/OS. @@ -1189,6 +1189,76 @@ static bool edit_key(int k) { case K_META + 'd': remove_range(ed.pos, word_right(ed.pos), true); break; + case K_META + 'u': + case K_META + 'l': + case K_META + 'c': { + /* Change the case of the word after the cursor, as bash does. + * `\c` capitalises its first character and lowercases the rest. */ + size_t to = word_right(ed.pos); + bool first = true; + for (size_t i = ed.pos; i < to; i++) { + unsigned char c = (unsigned char)ed.buf[i]; + if (k == K_META + 'u') + ed.buf[i] = (char)toupper(c); + else if (k == K_META + 'l' || !first) + ed.buf[i] = (char)tolower(c); + else + ed.buf[i] = (char)toupper(c); + if (is_word(ed.buf[i])) + first = false; + } + ed.pos = to; + break; + } + case K_META + 't': { + /* Transpose the words on either side of the cursor, as bash does: + * word1, space, word2 becomes word2, space, word1 in place. */ + size_t a1 = word_left(ed.pos), b1 = ed.pos; + while (b1 > a1 && !is_word(ed.buf[b1 - 1])) + b1--; + size_t a2 = ed.pos; + while (a2 < ed.len && !is_word(ed.buf[a2])) + a2++; + size_t b2 = word_right(a2); + if (a1 == b1 || a2 == b2) + break; /* there is no word on one side */ + size_t n1 = b1 - a1, gap = a2 - b1, n2 = b2 - a2; + char *tmp = xmalloc(n1 + gap + n2); + memcpy(tmp, ed.buf + a2, n2); + memcpy(tmp + n2, ed.buf + b1, gap); + memcpy(tmp + n2 + gap, ed.buf + a1, n1); + memcpy(ed.buf + a1, tmp, n1 + gap + n2); + free(tmp); + ed.pos = b2; + break; + } + case K_META + '.': { + /* Insert the last word of the previous line, as bash does. */ + int idx = ed.hidx < hist_count() ? ed.hidx - 1 : hist_count() - 1; + if (idx < 0) + break; + const char *h = hist_entry(idx); + size_t n = strlen(h); + while (n && (h[n - 1] == ' ' || h[n - 1] == '\t')) + n--; + size_t from = n; + while (from && h[from - 1] != ' ' && h[from - 1] != '\t') + from--; + if (from < n) + insert(h + from, n - from); + break; + } + case K_META + '\\': { + /* Delete the blanks around the cursor, as bash does. */ + size_t from = ed.pos, to = ed.pos; + while (from && (ed.buf[from - 1] == ' ' || ed.buf[from - 1] == '\t')) + from--; + while (to < ed.len && (ed.buf[to] == ' ' || ed.buf[to] == '\t')) + to++; + if (from < to) + remove_range(from, to, false); + break; + } case CH_CTRL_Y: if (ed.kill_len) insert(ed.kill, ed.kill_len); diff --git a/tests/linux/i-emacs.exp b/tests/linux/i-emacs.exp index a1a4754..a7fe58a 100644 --- a/tests/linux/i-emacs.exp +++ b/tests/linux/i-emacs.exp @@ -10,6 +10,20 @@ $ echo aa XXbb aa XXbb $ echo ba ba +$ echo HELLO +HELLO +$ echo hello +hello +$ echo Hello +Hello +$ echo bb aa +bb aa +$ echo alpha beta +alpha beta +$ echo beta +beta +$ echo xy +xy $ echo 'a^Ib' | tr '\t' T aTb $ 23456789012345678901234567890123456789012345678901234567890123456789 | wc -c diff --git a/tests/linux/i-emacs.sh b/tests/linux/i-emacs.sh index cb9bc6e..465b583 100644 --- a/tests/linux/i-emacs.sh +++ b/tests/linux/i-emacs.sh @@ -18,6 +18,22 @@ send echo aa bb cc\x1bb\x1bbXX\x1bf\x1bd\r expect aa XXbb send echo ab\x14\r expect ba +# bash's word keys: meta-u, meta-l, meta-c, meta-t +send echo hello\x01\x06\x06\x06\x06\x06\x1bu\r +expect HELLO +send echo HELLO\x01\x06\x06\x06\x06\x06\x1bl\r +expect hello +send echo HELLO\x01\x06\x06\x06\x06\x06\x1bc\r +expect Hello +send echo aa bb\x01\x06\x06\x06\x06\x06\x06\x06\x1bt\r +expect bb aa +# meta-. takes the last word of the previous line; meta-\\ closes blanks +send echo alpha beta\r +expect alpha beta +send echo \x1b.\r +expect echo beta +send echo x y\x1b[4D\x1b\\\r +expect xy # ^V inserts the next key literally send echo 'a\x16\tb' | tr '\\t' T\r expect aTb |
