From d7d847c8c6057aa2a4ab33b894692e74bb46a621 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sun, 6 Mar 2022 12:58:45 +0330 Subject: [PATCH] LibLine: Allow the embedder to optionally handle pasted data itself If the 'on_paste' callback is set, LibLine will buffer the pasted data and pass it over to the embedder to use as it pleases; in practice, this means that the users of LibLine can now escape or otherwise handle pasted data without the incremental codepoint-by-codepoint buildup. --- Userland/Libraries/LibLine/Editor.cpp | 11 ++++++++++- Userland/Libraries/LibLine/Editor.h | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibLine/Editor.cpp b/Userland/Libraries/LibLine/Editor.cpp index 6e33d22941..847d4f5dc2 100644 --- a/Userland/Libraries/LibLine/Editor.cpp +++ b/Userland/Libraries/LibLine/Editor.cpp @@ -974,6 +974,12 @@ void Editor::handle_read_event() } if (is_in_paste && param1 == 201) { m_state = InputState::Free; + if (on_paste) { + on_paste(Utf32View { m_paste_buffer.data(), m_paste_buffer.size() }, *this); + m_paste_buffer.clear_with_capacity(); + } + if (!m_paste_buffer.is_empty()) + insert(Utf32View { m_paste_buffer.data(), m_paste_buffer.size() }); continue; } } @@ -998,7 +1004,10 @@ void Editor::handle_read_event() m_state = InputState::GotEscape; continue; } - insert(code_point); + if (on_paste) + m_paste_buffer.append(code_point); + else + insert(code_point); continue; case InputState::Free: m_previous_free_state = InputState::Free; diff --git a/Userland/Libraries/LibLine/Editor.h b/Userland/Libraries/LibLine/Editor.h index 446585fe3d..5215505a4e 100644 --- a/Userland/Libraries/LibLine/Editor.h +++ b/Userland/Libraries/LibLine/Editor.h @@ -163,6 +163,7 @@ public: static StringMetrics actual_rendered_string_metrics(Utf32View const&); Function(Editor const&)> on_tab_complete; + Function on_paste; Function on_interrupt_handled; Function on_display_refresh; @@ -316,6 +317,7 @@ private: m_chars_touched_in_the_middle = 0; m_drawn_end_of_line_offset = 0; m_drawn_spans = {}; + m_paste_buffer.clear_with_capacity(); } void refresh_display(); @@ -491,6 +493,8 @@ private: RefPtr m_notifier; + Vector m_paste_buffer; + bool m_initialized { false }; bool m_refresh_needed { false }; Vector m_signal_handlers;