Skip to content

Commit

Permalink
LibLine: Implement internal functions to jump cursor over non-space word
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
ronak69 authored and alimpfard committed Jan 6, 2024
1 parent a6ce86a commit 97bcdba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Userland/Libraries/LibLine/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibLine/Editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
35 changes: 35 additions & 0 deletions Userland/Libraries/LibLine/InternalFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 97bcdba

Please sign in to comment.