summaryrefslogtreecommitdiff
path: root/src/normal.cc
diff options
context:
space:
mode:
authorJohannes Altmanninger <aclopte@gmail.com>2022-08-15 22:21:53 +0200
committerJohannes Altmanninger <aclopte@gmail.com>2022-09-02 02:59:47 +0200
commitc2ab5d46940bb01ab4ab2b4ef82b6829ddbadb5c (patch)
treee39c2eb2a16e58b7df486890a0a8c82fa9ac8c6f /src/normal.cc
parentdd4ba2ee88747616f7125024b192cec75cd7e58c (diff)
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 <c-h> and <c-k>. Here are some other vacant normal mode keys I considered X Y <backspace> <minus> # ^ = <plus> ' 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 (<c-o>/<c-i>) 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
Diffstat (limited to 'src/normal.cc')
-rw-r--r--src/normal.cc17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/normal.cc b/src/normal.cc
index 4873703a..531b6a08 100644
--- a/src/normal.cc
+++ b/src/normal.cc
@@ -2042,6 +2042,20 @@ void move_in_history(Context& context, NormalParams params)
history_id, max_history_id));
}
+void undo_selection_change(Context& context, NormalParams params)
+{
+ int count = std::max(1, params.count);
+ while (count--)
+ context.undo_selection_change();
+}
+
+void redo_selection_change(Context& context, NormalParams params)
+{
+ int count = std::max(1, params.count);
+ while (count--)
+ context.redo_selection_change();
+}
+
void exec_user_mappings(Context& context, NormalParams params)
{
on_next_key_with_autoinfo(context, "user-mapping", KeymapMode::None,
@@ -2367,6 +2381,9 @@ static constexpr HashMap<Key, NormalCmd, MemoryDomain::Undefined, KeymapBackend>
{ {alt('u')}, {"move backward in history", move_in_history<Direction::Backward>} },
{ {alt('U')}, {"move forward in history", move_in_history<Direction::Forward>} },
+ { {ctrl('h')}, {"undo selection change", undo_selection_change} },
+ { {ctrl('k')}, {"redo selection change", redo_selection_change} },
+
{ {alt('i')}, {"select inner object", select_object<ObjectFlags::ToBegin | ObjectFlags::ToEnd | ObjectFlags::Inner>} },
{ {alt('a')}, {"select whole object", select_object<ObjectFlags::ToBegin | ObjectFlags::ToEnd>} },
{ {'['}, {"select to object start", select_object<ObjectFlags::ToBegin>} },