From 087a17eb24059da1a98596bb5c8e9064a72776f4 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 20 Jul 2016 20:29:45 +0100 Subject: [PATCH] Support for going backward/forward in buffer history with / --- src/buffer.cc | 5 +++++ src/buffer.hh | 1 + src/normal.cc | 22 +++++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/buffer.cc b/src/buffer.cc index 028f9516..ec970676 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -425,6 +425,11 @@ bool Buffer::move_to(size_t history_id) noexcept return true; } +size_t Buffer::current_history_id() const noexcept +{ + return m_history_cursor->id; +} + void Buffer::check_invariant() const { #ifdef KAK_DEBUG diff --git a/src/buffer.hh b/src/buffer.hh index 4cd99c8d..845d04b1 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -135,6 +135,7 @@ public: bool undo(size_t count = 1) noexcept; bool redo(size_t count = 1) noexcept; bool move_to(size_t history_id) noexcept; + size_t current_history_id() const noexcept; String string(ByteCoord begin, ByteCoord end) const; diff --git a/src/normal.cc b/src/normal.cc index 316fc5e2..544fa08f 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1431,7 +1431,6 @@ void undo(Context& context, NormalParams params) void redo(Context& context, NormalParams params) { - using namespace std::placeholders; Buffer& buffer = context.buffer(); size_t timestamp = buffer.timestamp(); if (buffer.redo(std::max(1, params.count))) @@ -1445,6 +1444,25 @@ void redo(Context& context, NormalParams params) context.print_status({ "nothing left to redo", get_face("Information") }); } +template +void move_in_history(Context& context, NormalParams params) +{ + Buffer& buffer = context.buffer(); + size_t timestamp = buffer.timestamp(); + const size_t count = (size_t)std::max(1, params.count); + const size_t history_id = buffer.current_history_id() + + (direction == Direction::Forward ? count : -count); + if (buffer.move_to(history_id)) + { + auto ranges = compute_modified_ranges(buffer, timestamp); + if (not ranges.empty()) + context.selections_write_only() = std::move(ranges); + context.selections().avoid_eol(); + } + else + context.print_status({ "nothing left to redo", get_face("Information") }); +} + void exec_user_mappings(Context& context, NormalParams params) { on_next_key_with_autoinfo(context, KeymapMode::None, @@ -1679,6 +1697,8 @@ static NormalCmdDesc cmds[] = { 'u', "undo", undo }, { 'U', "redo", redo }, + { alt('u'), "move backward in history", move_in_history }, + { alt('U'), "move forward in history", move_in_history }, { alt('i'), "select inner object", select_object }, { alt('a'), "select whole object", select_object },