summaryrefslogtreecommitdiff
path: root/Libraries/LibLine
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-08-06 10:52:18 -0400
committerAndreas Kling <kling@serenityos.org>2020-08-06 17:45:31 +0200
commit4d9d054386d75c3f7bae91cb9224593fdc0bb5fe (patch)
treeb9ae8ed8dbaa719e1bd7b72a57ff415a3a3c86ce /Libraries/LibLine
parent471083ca3dd73bdcf176e9b9c8983c534cdeb154 (diff)
downloadserenity-4d9d054386d75c3f7bae91cb9224593fdc0bb5fe.zip
Shell: Start adding some alt shortcuts
This adds Alt-f to go forward by a word, and Alt-b to go backward by a word (like ctrl-arrow-left / ctrl-arrow-right already do). Behind the scenes, alt-key is implemented by sending <esc> followed by that key, and typing <esc> f/b for moving by a word hence works too (in all other shells too, not just in Serenity's). While here, rename some InputState enum values to make the slightly expanded use of <esc> clearer, and expand a few comments.
Diffstat (limited to 'Libraries/LibLine')
-rw-r--r--Libraries/LibLine/Editor.cpp39
-rw-r--r--Libraries/LibLine/Editor.h4
2 files changed, 26 insertions, 17 deletions
diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp
index a77a158216..cf072b06ce 100644
--- a/Libraries/LibLine/Editor.cpp
+++ b/Libraries/LibLine/Editor.cpp
@@ -562,59 +562,68 @@ void Editor::handle_read_event()
continue;
switch (m_state) {
- case InputState::ExpectBracket:
- if (code_point == '[') {
- m_state = InputState::ExpectFinal;
+ case InputState::GotEscape:
+ switch (code_point) {
+ case '[':
+ m_state = InputState::GotEscapeFollowedByLeftBracket;
continue;
- } else {
+ case 'b': // ^[b: alt-b
+ do_cursor_left(Word);
m_state = InputState::Free;
- break;
+ continue;
+ case 'f': // ^[f: alt-f
+ do_cursor_right(Word);
+ m_state = InputState::Free;
+ continue;
+ default:
+ m_state = InputState::Free;
+ continue;
}
- case InputState::ExpectFinal:
+ case InputState::GotEscapeFollowedByLeftBracket:
switch (code_point) {
case 'O': // mod_ctrl
ctrl_held = true;
continue;
- case 'A': // up
+ case 'A': // ^[[A: arrow up
do_search_backwards();
m_state = InputState::Free;
ctrl_held = false;
continue;
- case 'B': // down
+ case 'B': // ^[[B: arrow down
do_search_forwards();
m_state = InputState::Free;
ctrl_held = false;
continue;
- case 'D': // left
+ case 'D': // ^[[D: arrow left
do_cursor_left(ctrl_held ? Word : Character);
m_state = InputState::Free;
ctrl_held = false;
continue;
- case 'C': // right
+ case 'C': // ^[[C: arrow right
do_cursor_right(ctrl_held ? Word : Character);
m_state = InputState::Free;
ctrl_held = false;
continue;
- case 'H':
+ case 'H': // ^[[H: home
m_cursor = 0;
m_inline_search_cursor = m_cursor;
m_search_offset = 0;
m_state = InputState::Free;
ctrl_held = false;
continue;
- case 'F':
+ case 'F': // ^[[F: end
m_cursor = m_buffer.size();
m_state = InputState::Free;
m_inline_search_cursor = m_cursor;
m_search_offset = 0;
ctrl_held = false;
continue;
- case 'Z': // shift+tab
+ case 'Z': // ^[[Z: shift+tab
reverse_tab = true;
m_state = InputState::Free;
ctrl_held = false;
break;
- case '3':
+ case '3': // ^[[3~: delete
do_delete();
m_search_offset = 0;
m_state = InputState::ExpectTerminator;
@@ -632,7 +641,7 @@ void Editor::handle_read_event()
continue;
case InputState::Free:
if (code_point == 27) {
- m_state = InputState::ExpectBracket;
+ m_state = InputState::GotEscape;
continue;
}
break;
diff --git a/Libraries/LibLine/Editor.h b/Libraries/LibLine/Editor.h
index 5574184550..553e2ae0e2 100644
--- a/Libraries/LibLine/Editor.h
+++ b/Libraries/LibLine/Editor.h
@@ -377,8 +377,8 @@ private:
enum class InputState {
Free,
- ExpectBracket,
- ExpectFinal,
+ GotEscape,
+ GotEscapeFollowedByLeftBracket,
ExpectTerminator,
};
InputState m_state { InputState::Free };