From d1fade5c9e7ec622ce1c1ed5d6c4418753068e09 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 21 Nov 2012 13:43:10 +0100 Subject: [PATCH] Buffer: replace reset_undo_data with a NoUndo flag --- src/buffer.cc | 30 +++++++++++++++--------------- src/buffer.hh | 4 ++-- src/commands.cc | 3 +-- src/debug.cc | 3 +-- src/file.cc | 6 +++--- src/main.cc | 2 +- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 3e6eb6e7..a540772a 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -14,7 +14,7 @@ namespace Kakoune Buffer::Buffer(String name, Flags flags, String initial_content) - : m_name(std::move(name)), m_flags(flags), + : m_name(std::move(name)), m_flags(flags | Flags::NoUndo), m_history(), m_history_cursor(m_history.begin()), m_last_save_undo_index(0), m_timestamp(0), @@ -26,7 +26,6 @@ Buffer::Buffer(String name, Flags flags, initial_content += '\n'; do_insert(begin(), std::move(initial_content)); - Editor editor_for_hooks(*this); Context context(editor_for_hooks); if (flags & Flags::File and flags & Flags::New) @@ -36,7 +35,8 @@ Buffer::Buffer(String name, Flags flags, m_hook_manager.run_hook("BufCreate", m_name, context); - reset_undo_data(); + // now we may begin to record undo data + m_flags = flags; } Buffer::~Buffer() @@ -148,6 +148,9 @@ String Buffer::string(const BufferIterator& begin, const BufferIterator& end) co void Buffer::begin_undo_group() { + if (m_flags & Flags::NoUndo) + return; + assert(m_current_undo_group.empty()); m_history.erase(m_history_cursor, m_history.end()); @@ -159,6 +162,9 @@ void Buffer::begin_undo_group() void Buffer::end_undo_group() { + if (m_flags & Flags::NoUndo) + return; + if (m_current_undo_group.empty()) return; @@ -217,13 +223,6 @@ bool Buffer::redo() return true; } -void Buffer::reset_undo_data() -{ - m_history.clear(); - m_history_cursor = m_history.end(); - m_current_undo_group.clear(); -} - void Buffer::check_invariant() const { ByteCount start = 0; @@ -370,9 +369,9 @@ void Buffer::insert(BufferIterator pos, String content) if (pos.is_end() and content.back() != '\n') content += '\n'; - m_current_undo_group.emplace_back(Modification::Insert, pos, - std::move(content)); - do_insert(pos, m_current_undo_group.back().content); + if (not (m_flags & Flags::NoUndo)) + m_current_undo_group.emplace_back(Modification::Insert, pos, content); + do_insert(pos, content); } void Buffer::erase(BufferIterator begin, BufferIterator end) @@ -383,8 +382,9 @@ void Buffer::erase(BufferIterator begin, BufferIterator end) if (begin == end) return; - m_current_undo_group.emplace_back(Modification::Erase, begin, - string(begin, end)); + if (not (m_flags & Flags::NoUndo)) + m_current_undo_group.emplace_back(Modification::Erase, begin, + string(begin, end)); do_erase(begin, end); } diff --git a/src/buffer.hh b/src/buffer.hh index 0d1ffa92..27b5ae79 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -100,7 +100,8 @@ public: None = 0, File = 1, New = 2, - Fifo = 4 + Fifo = 4, + NoUndo = 8, }; Buffer(String name, Flags flags, String initial_content = "\n"); @@ -121,7 +122,6 @@ public: void end_undo_group(); bool undo(); bool redo(); - void reset_undo_data(); String string(const BufferIterator& begin, const BufferIterator& end) const; diff --git a/src/commands.cc b/src/commands.cc index ef36b275..5c1146fb 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -231,7 +231,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context) int fd = open(filename.c_str(), O_RDONLY | O_CLOEXEC); if (fd < 0) throw runtime_error("unable to open " + filename); - Buffer* buffer = new Buffer(name, Buffer::Flags::Fifo); + Buffer* buffer = new Buffer(name, Buffer::Flags::Fifo | Buffer::Flags::NoUndo); buffer->hook_manager().add_hook("BufClose", [fd, buffer](const String&, const Context&) { @@ -250,7 +250,6 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context) buffer->insert(buffer->end()-1, count > 0 ? String(data, data+count) : "*** kak: fifo closed ***\n"); - buffer->reset_undo_data(); ClientManager::instance().redraw_clients(); if (count <= 0) { diff --git a/src/debug.cc b/src/debug.cc index ec6cef47..5f0fecab 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -13,7 +13,7 @@ static Buffer& get_or_create_debug_buffer() Buffer* buffer = BufferManager::instance().get_buffer(debug_buffer_name); if (not buffer) - buffer = new Buffer(debug_buffer_name, Buffer::Flags::None, ""); + buffer = new Buffer(debug_buffer_name, Buffer::Flags::NoUndo); assert(buffer); return *buffer; @@ -25,7 +25,6 @@ void write_debug(const String& str) Editor editor(debug_buffer); editor.select(debug_buffer.end()-1); editor.insert(str + "\n"); - debug_buffer.reset_undo_data(); } } diff --git a/src/file.cc b/src/file.cc index a5accf96..cf8df91e 100644 --- a/src/file.cc +++ b/src/file.cc @@ -85,7 +85,7 @@ Buffer* create_buffer_from_file(const String& filename) if (Buffer* buffer = BufferManager::instance().get_buffer(filename)) delete buffer; - Buffer* buffer = new Buffer(filename, Buffer::Flags::File, ""); + Buffer* buffer = new Buffer(filename, Buffer::Flags::File | Buffer::Flags::NoUndo); String content; char buf[256]; @@ -131,8 +131,8 @@ Buffer* create_buffer_from_file(const String& filename) if (*(buffer->end() - 2) == '\n') buffer->erase(buffer->end() - 1, buffer->end()); - // it never happened, buffer always was like that - buffer->reset_undo_data(); + // enable undo data recording + buffer->flags() &= ~Buffer::Flags::NoUndo; return buffer; } diff --git a/src/main.cc b/src/main.cc index 0aa10289..2aedb510 100644 --- a/src/main.cc +++ b/src/main.cc @@ -612,7 +612,7 @@ void create_local_client(const String& file) } } else - buffer = new Buffer("*scratch*", Buffer::Flags::None); + buffer = new Buffer("*scratch*", Buffer::Flags::NoUndo); ClientManager::instance().create_client( std::unique_ptr{ui}, *buffer, 0);