summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-18 13:05:38 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-18 13:05:38 -0600
commitbc0cab203252597d29cbce7fcba0ca4fadfbc846 (patch)
treed3a7f65bca4fcd2960c6a0cb6b2a2fa1670850a4 /src
parente46eeeea900703956783d835edd800567989ceb0 (diff)
downloadzash-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.
Diffstat (limited to 'src')
-rw-r--r--src/edit_builtin.c74
1 files changed, 72 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);