1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:57:36 +00:00

LibLine: Fix "word" handling for alt-d/u/l/c/f/b and ctrl-left/right

All these shortcuts treat consecutive alnums as a word, not consecutive
non-spaces.

For example, `alias KILL='kill -9'` can now be written by typing it
out lowercase, then hitting ctrl-a alt-f alt-u.

Ctrl-W still treats a word as a sequence of non-spaces. Alt-backspace
in a future patch will add the ability to backward-delete a word
that's a sequence of alnums.
This commit is contained in:
Nico Weber 2020-08-06 14:43:23 -04:00 committed by Andreas Kling
parent d9de0a5007
commit 2051d690d5

View file

@ -464,7 +464,7 @@ void Editor::handle_read_event()
for (;;) { for (;;) {
if (m_cursor == 0) if (m_cursor == 0)
break; break;
if (skipped_at_least_one_character && isspace(m_buffer[m_cursor - 1])) // stop *after* a space, but only if it changes the position if (skipped_at_least_one_character && !isalnum(m_buffer[m_cursor - 1])) // stop *after* a non-alnum, but only if it changes the position
break; break;
skipped_at_least_one_character = true; skipped_at_least_one_character = true;
--m_cursor; --m_cursor;
@ -484,7 +484,7 @@ void Editor::handle_read_event()
for (;;) { for (;;) {
if (m_cursor >= m_buffer.size()) if (m_cursor >= m_buffer.size())
break; break;
if (isspace(m_buffer[++m_cursor])) if (!isalnum(m_buffer[++m_cursor]))
break; break;
} }
m_buffer.take_last(); m_buffer.take_last();
@ -589,13 +589,14 @@ void Editor::handle_read_event()
continue; continue;
case 'd': // ^[d: alt-d: forward delete word case 'd': // ^[d: alt-d: forward delete word
{ {
bool has_seen_nonspace = false; // A word here is contiguous alnums. `foo=bar baz` is three words.
bool has_seen_alnum = false;
while (m_cursor < m_buffer.size()) { while (m_cursor < m_buffer.size()) {
if (isspace(m_buffer[m_cursor])) { if (!isalnum(m_buffer[m_cursor])) {
if (has_seen_nonspace) if (has_seen_alnum)
break; break;
} else { } else {
has_seen_nonspace = true; has_seen_alnum = true;
} }
do_delete(); do_delete();
} }
@ -610,10 +611,11 @@ void Editor::handle_read_event()
case 'l': // ^[l: alt-l: lowercase word case 'l': // ^[l: alt-l: lowercase word
case 'u': // ^[u: alt-u: uppercase word case 'u': // ^[u: alt-u: uppercase word
{ {
while (m_cursor < m_buffer.size() && isspace(m_buffer[m_cursor])) // A word here is contiguous alnums. `foo=bar baz` is three words.
while (m_cursor < m_buffer.size() && !isalnum(m_buffer[m_cursor]))
++m_cursor; ++m_cursor;
size_t start = m_cursor; size_t start = m_cursor;
while (m_cursor < m_buffer.size() && !isspace(m_buffer[m_cursor])) { while (m_cursor < m_buffer.size() && isalnum(m_buffer[m_cursor])) {
if (code_point == 'u' || (code_point == 'c' && m_cursor == start)) { if (code_point == 'u' || (code_point == 'c' && m_cursor == start)) {
m_buffer[m_cursor] = toupper(m_buffer[m_cursor]); m_buffer[m_cursor] = toupper(m_buffer[m_cursor]);
} else { } else {
@ -835,6 +837,7 @@ void Editor::handle_read_event()
// Normally ^W. `stty werase \^n` can change it to ^N (or something else), but Serenity doesn't have `stty` yet. // Normally ^W. `stty werase \^n` can change it to ^N (or something else), but Serenity doesn't have `stty` yet.
if (code_point == m_termios.c_cc[VWERASE]) { if (code_point == m_termios.c_cc[VWERASE]) {
// A word here is space-separated. `foo=bar baz` is two words.
bool has_seen_nonspace = false; bool has_seen_nonspace = false;
while (m_cursor > 0) { while (m_cursor > 0) {
if (isspace(m_buffer[m_cursor - 1])) { if (isspace(m_buffer[m_cursor - 1])) {