From dd4ba2ee88747616f7125024b192cec75cd7e58c Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 6 Aug 2022 21:55:39 +0200 Subject: Prepare to record selection changes as perceived by the user To be able to undo selection changes, we want to record selections from all commands that modify selections. Each such command will get its own private copy of the selections object. This copy will live until the command is finished executing. All child commands that are run while the command is executing, will also use the same copy, because to the user it's all just one selection change anyway. Add an RAII object in all places where we might modify selections. The next commit will use this to create the private selections copy in the constructor (if there is none) and remove redundant history items in the destructor. We could avoid the RAII object in some places but that seems worse. For lifetimes that don't correspond to a lexical scope, we use a std::unique_ptr. For lambdas that require conversion to std::function, we use std::shared_ptr because we need something that's copyable. --- src/input_handler.cc | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'src/input_handler.cc') diff --git a/src/input_handler.cc b/src/input_handler.cc index 956c32d7..73ad276d 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -93,59 +93,67 @@ struct MouseHandler Buffer& buffer = context.buffer(); BufferCoord cursor; - auto& selections = context.selections(); constexpr auto modifiers = Key::Modifiers::Control | Key::Modifiers::Alt | Key::Modifiers::Shift | Key::Modifiers::MouseButtonMask; switch ((key.modifiers & ~modifiers).value) { case Key::Modifiers::MousePress: switch (key.mouse_button()) { - case Key::MouseButton::Right: - m_dragging = false; + case Key::MouseButton::Right: { + m_dragging.reset(); cursor = context.window().buffer_coord(key.coord()); + ScopedSelectionEdition selection_edition{context}; + auto& selections = context.selections(); if (key.modifiers & Key::Modifiers::Control) selections = {{selections.begin()->anchor(), cursor}}; else selections.main() = {selections.main().anchor(), cursor}; selections.sort_and_merge_overlapping(); return true; + } - case Key::MouseButton::Left: - m_dragging = true; + case Key::MouseButton::Left: { + m_dragging.reset(new ScopedSelectionEdition{context}); m_anchor = context.window().buffer_coord(key.coord()); if (not (key.modifiers & Key::Modifiers::Control)) context.selections_write_only() = { buffer, m_anchor}; else { + auto& selections = context.selections(); size_t main = selections.size(); selections.push_back({m_anchor}); selections.set_main_index(main); selections.sort_and_merge_overlapping(); } return true; + } default: return true; } - case Key::Modifiers::MouseRelease: + case Key::Modifiers::MouseRelease: { if (not m_dragging) return true; - m_dragging = false; + auto& selections = context.selections(); cursor = context.window().buffer_coord(key.coord()); selections.main() = {buffer.clamp(m_anchor), cursor}; selections.sort_and_merge_overlapping(); + m_dragging.reset(); return true; + } - case Key::Modifiers::MousePos: + case Key::Modifiers::MousePos: { if (not m_dragging) return true; cursor = context.window().buffer_coord(key.coord()); + auto& selections = context.selections(); selections.main() = {buffer.clamp(m_anchor), cursor}; selections.sort_and_merge_overlapping(); return true; + } case Key::Modifiers::Scroll: - scroll_window(context, static_cast(key.key), m_dragging); + scroll_window(context, static_cast(key.key), (bool)m_dragging); return true; default: return false; @@ -153,7 +161,7 @@ struct MouseHandler } private: - bool m_dragging = false; + std::unique_ptr m_dragging; BufferCoord m_anchor; }; @@ -1199,6 +1207,7 @@ public: Insert(InputHandler& input_handler, InsertMode mode, int count) : InputMode(input_handler), m_edition(context()), + m_selection_edition(context()), m_completer(context()), m_restore_cursor(mode == InsertMode::Append), m_auto_complete{context().options()["autocomplete"].get() & AutoComplete::Insert}, @@ -1549,6 +1558,7 @@ private: } ScopedEdition m_edition; + ScopedSelectionEdition m_selection_edition; InsertCompleter m_completer; const bool m_restore_cursor; bool m_auto_complete; @@ -1806,6 +1816,7 @@ void scroll_window(Context& context, LineCount offset, bool mouse_dragging) win_pos.line = clamp(win_pos.line + offset, 0_line, line_count-1); + ScopedSelectionEdition selection_edition{context}; SelectionList& selections = context.selections(); Selection& main_selection = selections.main(); const BufferCoord anchor = main_selection.anchor(); -- cgit v1.2.3 From c2ab5d46940bb01ab4ab2b4ef82b6829ddbadb5c Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Mon, 15 Aug 2022 22:21:53 +0200 Subject: Allow to undo and redo selection changes From the issue: > It often happens to me that I carefully craft a selection with multiple > cursors, ready to make changes elegantly, only to completely mess it > up by pressing a wrong key (by merging the cursors for example). Being > able to undo the last selection change (even if only until the previous > buffer change) would make this much less painful. Fix this by recording selection changes and allowing simple linear undo/redo of selection changes. The preliminary key bindings are and . Here are some other vacant normal mode keys I considered X Y # ^ = ' unfortunately none of them is super convenient to type. Maybe we can kick out some other normal mode command? --- This feature has some overlap with the jump list (/) and with undo (u) but each of the three features have their moment. Currently there's no special integration with either peer feature; the three histories are completely independent. In future we might want to synchronize them so we can implement Sublime Text's "Soft undo" feature. Note that it is possible to restore selections that predate a buffer modification. Depending on the buffer modification, the selections might look different of course. (When trying to apply an old buffer's selection to the new buffer, Kakoune computes a diff of the buffers and updates the selection accordingly. This works quite well for many practical examples.) This makes us record the full history of all selections for each client. This seems wasteful, we could set a limit. I don't expect excessive memory usage in practice (we also keep the full history of buffer changes) but I could be wrong. Closes #898 --- src/input_handler.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/input_handler.cc') diff --git a/src/input_handler.cc b/src/input_handler.cc index 73ad276d..25c450c8 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -100,6 +100,7 @@ struct MouseHandler switch (key.mouse_button()) { case Key::MouseButton::Right: { + kak_assert(not context.is_editing_selection()); m_dragging.reset(); cursor = context.window().buffer_coord(key.coord()); ScopedSelectionEdition selection_edition{context}; @@ -113,6 +114,7 @@ struct MouseHandler } case Key::MouseButton::Left: { + kak_assert(not context.is_editing_selection()); m_dragging.reset(new ScopedSelectionEdition{context}); m_anchor = context.window().buffer_coord(key.coord()); if (not (key.modifiers & Key::Modifiers::Control)) -- cgit v1.2.3