LibLine: Implement basic cut-and-paste functionality

Every cut operation (erase last word backward/forward, erase line till
start/end) stores the erased characters in `m_last_erased` u32 vector.

The last erased characters will get inserted into the buffer when
`Ctrl+Y` (`insert_last_erased()` internal function) is pressed.
This commit is contained in:
ronak69 2024-01-03 13:14:34 +00:00 committed by Ali Mohammad Pur
parent 97bcdba2a5
commit 9d3604215d
Notes: sideshowbarker 2024-07-17 03:00:02 +09:00
3 changed files with 49 additions and 2 deletions

View file

@ -164,6 +164,7 @@ void Editor::set_default_keybinds()
// ^[.: alt-.: insert last arg of previous command (similar to `!$`)
register_key_input_callback(Key { '.', Key::Alt }, EDITOR_INTERNAL_FUNCTION(insert_last_words));
register_key_input_callback(ctrl('Y'), EDITOR_INTERNAL_FUNCTION(insert_last_erased));
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));

View file

@ -124,6 +124,7 @@ struct Configuration {
M(transpose_characters) \
M(transpose_words) \
M(insert_last_words) \
M(insert_last_erased) \
M(erase_alnum_word_backwards) \
M(erase_alnum_word_forwards) \
M(capitalize_word) \
@ -505,6 +506,7 @@ private:
RefPtr<Core::Notifier> m_notifier;
Vector<u32> m_paste_buffer;
Vector<u32> m_last_erased;
bool m_initialized { false };
bool m_refresh_needed { false };

View file

@ -221,8 +221,15 @@ void Editor::finish_edit()
void Editor::kill_line()
{
for (size_t i = 0; i < m_cursor; ++i)
if (m_cursor == 0)
return;
m_last_erased.clear_with_capacity();
for (size_t i = 0; i < m_cursor; ++i) {
m_last_erased.append(m_buffer[0]);
remove_at_index(0);
}
m_cursor = 0;
m_inline_search_cursor = m_cursor;
m_refresh_needed = true;
@ -230,6 +237,11 @@ void Editor::kill_line()
void Editor::erase_word_backwards()
{
if (m_cursor == 0)
return;
m_last_erased.clear_with_capacity();
// A word here is space-separated. `foo=bar baz` is two words.
bool has_seen_nonspace = false;
while (m_cursor > 0) {
@ -239,20 +251,36 @@ void Editor::erase_word_backwards()
} else {
has_seen_nonspace = true;
}
m_last_erased.append(m_buffer[m_cursor - 1]);
erase_character_backwards();
}
m_last_erased.reverse();
}
void Editor::erase_to_end()
{
while (m_cursor < m_buffer.size())
if (m_cursor == m_buffer.size())
return;
m_last_erased.clear_with_capacity();
while (m_cursor < m_buffer.size()) {
m_last_erased.append(m_buffer[m_cursor]);
erase_character_forwards();
}
}
void Editor::erase_to_beginning()
{
}
void Editor::insert_last_erased()
{
insert(Utf32View { m_last_erased.data(), m_last_erased.size() });
}
void Editor::transpose_characters()
{
if (m_cursor > 0 && m_buffer.size() >= 2) {
@ -507,6 +535,11 @@ void Editor::insert_last_words()
void Editor::erase_alnum_word_backwards()
{
if (m_cursor == 0)
return;
m_last_erased.clear_with_capacity();
// A word here is contiguous alnums. `foo=bar baz` is three words.
bool has_seen_alnum = false;
while (m_cursor > 0) {
@ -516,12 +549,21 @@ void Editor::erase_alnum_word_backwards()
} else {
has_seen_alnum = true;
}
m_last_erased.append(m_buffer[m_cursor - 1]);
erase_character_backwards();
}
m_last_erased.reverse();
}
void Editor::erase_alnum_word_forwards()
{
if (m_cursor == m_buffer.size())
return;
m_last_erased.clear_with_capacity();
// A word here is contiguous alnums. `foo=bar baz` is three words.
bool has_seen_alnum = false;
while (m_cursor < m_buffer.size()) {
@ -531,6 +573,8 @@ void Editor::erase_alnum_word_forwards()
} else {
has_seen_alnum = true;
}
m_last_erased.append(m_buffer[m_cursor]);
erase_character_forwards();
}
}