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.
This commit is contained in:
Ali Mohammad Pur 2022-03-06 12:58:45 +03:30 committed by Andreas Kling
parent 0ea775f257
commit d7d847c8c6
2 changed files with 14 additions and 1 deletions

View file

@ -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;

View file

@ -163,6 +163,7 @@ public:
static StringMetrics actual_rendered_string_metrics(Utf32View const&);
Function<Vector<CompletionSuggestion>(Editor const&)> on_tab_complete;
Function<void(Utf32View, Editor&)> on_paste;
Function<void()> on_interrupt_handled;
Function<void(Editor&)> 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<Core::Notifier> m_notifier;
Vector<u32> m_paste_buffer;
bool m_initialized { false };
bool m_refresh_needed { false };
Vector<int, 2> m_signal_handlers;