From 97bcdba2a5403fc47465962f1baa50e37458741b Mon Sep 17 00:00:00 2001 From: ronak69 Date: Wed, 3 Jan 2024 11:12:29 +0000 Subject: [PATCH] LibLine: Implement internal functions to jump cursor over non-space word Two new internal functions `cursor_left_nonspace_word()` (`Ctrl+Alt+B`) and `cursor_right_nonspace_word()` (`Ctrl+Alt+F`) that jump the cursor left or right until a non-space character. Same implementation as the alphanumeric word jump functions `cursor_left_word()` (`Alt+B`) and `cursor_right_word()` (`Alt+F`). --- Userland/Libraries/LibLine/Editor.cpp | 2 ++ Userland/Libraries/LibLine/Editor.h | 2 ++ .../Libraries/LibLine/InternalFunctions.cpp | 35 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/Userland/Libraries/LibLine/Editor.cpp b/Userland/Libraries/LibLine/Editor.cpp index af4ebc891c..de70ef91c2 100644 --- a/Userland/Libraries/LibLine/Editor.cpp +++ b/Userland/Libraries/LibLine/Editor.cpp @@ -166,6 +166,8 @@ void Editor::set_default_keybinds() register_key_input_callback(Key { '.', Key::Alt }, EDITOR_INTERNAL_FUNCTION(insert_last_words)); register_key_input_callback(Key { 'b', Key::Alt }, EDITOR_INTERNAL_FUNCTION(cursor_left_word)); register_key_input_callback(Key { 'f', Key::Alt }, EDITOR_INTERNAL_FUNCTION(cursor_right_word)); + register_key_input_callback(Key { ctrl('B'), Key::Alt }, EDITOR_INTERNAL_FUNCTION(cursor_left_nonspace_word)); + register_key_input_callback(Key { ctrl('F'), Key::Alt }, EDITOR_INTERNAL_FUNCTION(cursor_right_nonspace_word)); // ^[^H: alt-backspace: backward delete word register_key_input_callback(Key { '\b', Key::Alt }, EDITOR_INTERNAL_FUNCTION(erase_alnum_word_backwards)); register_key_input_callback(Key { 'd', Key::Alt }, EDITOR_INTERNAL_FUNCTION(erase_alnum_word_forwards)); diff --git a/Userland/Libraries/LibLine/Editor.h b/Userland/Libraries/LibLine/Editor.h index ef5c32f20a..e5fad28e3e 100644 --- a/Userland/Libraries/LibLine/Editor.h +++ b/Userland/Libraries/LibLine/Editor.h @@ -105,8 +105,10 @@ struct Configuration { M(clear_screen) \ M(cursor_left_character) \ M(cursor_left_word) \ + M(cursor_left_nonspace_word) \ M(cursor_right_character) \ M(cursor_right_word) \ + M(cursor_right_nonspace_word) \ M(enter_search) \ M(erase_character_backwards) \ M(erase_character_forwards) \ diff --git a/Userland/Libraries/LibLine/InternalFunctions.cpp b/Userland/Libraries/LibLine/InternalFunctions.cpp index 41546d6b85..e53699d0e3 100644 --- a/Userland/Libraries/LibLine/InternalFunctions.cpp +++ b/Userland/Libraries/LibLine/InternalFunctions.cpp @@ -93,6 +93,23 @@ void Editor::cursor_left_word() m_inline_search_cursor = m_cursor; } +void Editor::cursor_left_nonspace_word() +{ + auto has_seen_nonspace = false; + while (m_cursor) { + // after seeing at least one non-space, stop just before a space + if (is_ascii_space(m_buffer[m_cursor - 1])) { + if (has_seen_nonspace) + break; + } else { + has_seen_nonspace = true; + } + + --m_cursor; + } + m_inline_search_cursor = m_cursor; +} + void Editor::cursor_left_character() { if (m_cursor > 0) { @@ -121,6 +138,24 @@ void Editor::cursor_right_word() m_search_offset = 0; } +void Editor::cursor_right_nonspace_word() +{ + auto has_seen_nonspace = false; + while (m_cursor < m_buffer.size()) { + // after seeing at least one non-space, stop at the first space + if (is_ascii_space(m_buffer[m_cursor])) { + if (has_seen_nonspace) + break; + } else { + has_seen_nonspace = true; + } + + ++m_cursor; + } + m_inline_search_cursor = m_cursor; + m_search_offset = 0; +} + void Editor::cursor_right_character() { if (m_cursor < m_buffer.size()) {