summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/VimEditingEngine.cpp
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2021-04-26 20:44:31 +0000
committerLinus Groh <mail@linusgroh.de>2021-04-27 13:10:10 +0200
commitace2c337dc5c9b1b8de897a39d4e9cd79d212510 (patch)
tree2e9799f10ae3b4042df6824c8744839d10ad08b9 /Userland/Libraries/LibGUI/VimEditingEngine.cpp
parentb970dbf2afae63e4bfd33656cd6c2f038a561382 (diff)
downloadserenity-ace2c337dc5c9b1b8de897a39d4e9cd79d212510.zip
VimEditingEngine: Handle arrow, Home/End and Page keys
This patch adds handling of the arrow, Home/End, and PageUp/PageDown keys to the Vim emulation mode. Home acts as 0, End acts as $, arrow keys act as their HJKL variants, and PageUp/Down behaves as you would expect. This patch also moves the default handling of the aforementioned keys to insert mode, since regular EditingEngine semantics are more appropriate there.
Diffstat (limited to 'Userland/Libraries/LibGUI/VimEditingEngine.cpp')
-rw-r--r--Userland/Libraries/LibGUI/VimEditingEngine.cpp38
1 files changed, 30 insertions, 8 deletions
diff --git a/Userland/Libraries/LibGUI/VimEditingEngine.cpp b/Userland/Libraries/LibGUI/VimEditingEngine.cpp
index 8b123f3e0b..fd7eed398a 100644
--- a/Userland/Libraries/LibGUI/VimEditingEngine.cpp
+++ b/Userland/Libraries/LibGUI/VimEditingEngine.cpp
@@ -168,6 +168,13 @@ void VimMotion::add_key_code(KeyCode key, [[maybe_unused]] bool ctrl, bool shift
#undef DIGIT
+ // Home means to the beginning of the line.
+ case KeyCode::Key_Home:
+ m_unit = Unit::Character;
+ m_amount = START_OF_LINE;
+ m_is_complete = true;
+ break;
+
// If 0 appears while amount is 0, then it means beginning of line.
// Otherwise, it adds 0 to the amount.
case KeyCode::Key_0:
@@ -180,8 +187,9 @@ void VimMotion::add_key_code(KeyCode key, [[maybe_unused]] bool ctrl, bool shift
}
break;
- // $ means end of line.
+ // End or $ means end of line.
// TODO: d2$ in vim deletes to the end of the line and then the next line.
+ case KeyCode::Key_End:
case KeyCode::Key_Dollar:
m_unit = Unit::Character;
m_amount = END_OF_LINE;
@@ -196,9 +204,9 @@ void VimMotion::add_key_code(KeyCode key, [[maybe_unused]] bool ctrl, bool shift
m_is_complete = true;
break;
- // j, up or + operates on this line and amount line(s) after.
+ // j, down or + operates on this line and amount line(s) after.
case KeyCode::Key_J:
- case KeyCode::Key_Up:
+ case KeyCode::Key_Down:
case KeyCode::Key_Plus:
m_unit = Unit::Line;
@@ -208,9 +216,9 @@ void VimMotion::add_key_code(KeyCode key, [[maybe_unused]] bool ctrl, bool shift
m_is_complete = true;
break;
- // k, down or - operates on this line and amount line(s) before.
+ // k, up or - operates on this line and amount line(s) before.
case KeyCode::Key_K:
- case KeyCode::Key_Down:
+ case KeyCode::Key_Up:
case KeyCode::Key_Minus:
m_unit = Unit::Line;
@@ -732,9 +740,6 @@ CursorWidth VimEditingEngine::cursor_width() const
bool VimEditingEngine::on_key(const KeyEvent& event)
{
- if (EditingEngine::on_key(event))
- return true;
-
switch (m_vim_mode) {
case (VimMode::Insert):
return on_key_in_insert_mode(event);
@@ -751,6 +756,9 @@ bool VimEditingEngine::on_key(const KeyEvent& event)
bool VimEditingEngine::on_key_in_insert_mode(const KeyEvent& event)
{
+ if (EditingEngine::on_key(event))
+ return true;
+
if (event.key() == KeyCode::Key_Escape || (event.ctrl() && event.key() == KeyCode::Key_LeftBracket) || (event.ctrl() && event.key() == KeyCode::Key_C)) {
if (m_editor->cursor().column() > 0)
move_one_left();
@@ -968,6 +976,12 @@ bool VimEditingEngine::on_key_in_normal_mode(const KeyEvent& event)
case (KeyCode::Key_P):
put();
return true;
+ case (KeyCode::Key_PageUp):
+ move_page_up();
+ return true;
+ case (KeyCode::Key_PageDown):
+ move_page_down();
+ return true;
default:
break;
}
@@ -1083,6 +1097,14 @@ bool VimEditingEngine::on_key_in_visual_mode(const KeyEvent& event)
yank(Selection);
switch_to_normal_mode();
return true;
+ case (KeyCode::Key_PageUp):
+ move_page_up();
+ update_selection_on_cursor_move();
+ return true;
+ case (KeyCode::Key_PageDown):
+ move_page_down();
+ update_selection_on_cursor_move();
+ return true;
default:
break;
}