Buffer: replace reset_undo_data with a NoUndo flag

This commit is contained in:
Maxime Coste 2012-11-21 13:43:10 +01:00
parent ac6171686d
commit d1fade5c9e
6 changed files with 23 additions and 25 deletions

View File

@ -14,7 +14,7 @@ namespace Kakoune
Buffer::Buffer(String name, Flags flags, Buffer::Buffer(String name, Flags flags,
String initial_content) 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_history(), m_history_cursor(m_history.begin()),
m_last_save_undo_index(0), m_last_save_undo_index(0),
m_timestamp(0), m_timestamp(0),
@ -26,7 +26,6 @@ Buffer::Buffer(String name, Flags flags,
initial_content += '\n'; initial_content += '\n';
do_insert(begin(), std::move(initial_content)); do_insert(begin(), std::move(initial_content));
Editor editor_for_hooks(*this); Editor editor_for_hooks(*this);
Context context(editor_for_hooks); Context context(editor_for_hooks);
if (flags & Flags::File and flags & Flags::New) 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); 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() Buffer::~Buffer()
@ -148,6 +148,9 @@ String Buffer::string(const BufferIterator& begin, const BufferIterator& end) co
void Buffer::begin_undo_group() void Buffer::begin_undo_group()
{ {
if (m_flags & Flags::NoUndo)
return;
assert(m_current_undo_group.empty()); assert(m_current_undo_group.empty());
m_history.erase(m_history_cursor, m_history.end()); m_history.erase(m_history_cursor, m_history.end());
@ -159,6 +162,9 @@ void Buffer::begin_undo_group()
void Buffer::end_undo_group() void Buffer::end_undo_group()
{ {
if (m_flags & Flags::NoUndo)
return;
if (m_current_undo_group.empty()) if (m_current_undo_group.empty())
return; return;
@ -217,13 +223,6 @@ bool Buffer::redo()
return true; 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 void Buffer::check_invariant() const
{ {
ByteCount start = 0; ByteCount start = 0;
@ -370,9 +369,9 @@ void Buffer::insert(BufferIterator pos, String content)
if (pos.is_end() and content.back() != '\n') if (pos.is_end() and content.back() != '\n')
content += '\n'; content += '\n';
m_current_undo_group.emplace_back(Modification::Insert, pos, if (not (m_flags & Flags::NoUndo))
std::move(content)); m_current_undo_group.emplace_back(Modification::Insert, pos, content);
do_insert(pos, m_current_undo_group.back().content); do_insert(pos, content);
} }
void Buffer::erase(BufferIterator begin, BufferIterator end) void Buffer::erase(BufferIterator begin, BufferIterator end)
@ -383,6 +382,7 @@ void Buffer::erase(BufferIterator begin, BufferIterator end)
if (begin == end) if (begin == end)
return; return;
if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(Modification::Erase, begin, m_current_undo_group.emplace_back(Modification::Erase, begin,
string(begin, end)); string(begin, end));
do_erase(begin, end); do_erase(begin, end);

View File

@ -100,7 +100,8 @@ public:
None = 0, None = 0,
File = 1, File = 1,
New = 2, New = 2,
Fifo = 4 Fifo = 4,
NoUndo = 8,
}; };
Buffer(String name, Flags flags, String initial_content = "\n"); Buffer(String name, Flags flags, String initial_content = "\n");
@ -121,7 +122,6 @@ public:
void end_undo_group(); void end_undo_group();
bool undo(); bool undo();
bool redo(); bool redo();
void reset_undo_data();
String string(const BufferIterator& begin, String string(const BufferIterator& begin,
const BufferIterator& end) const; const BufferIterator& end) const;

View File

@ -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); int fd = open(filename.c_str(), O_RDONLY | O_CLOEXEC);
if (fd < 0) if (fd < 0)
throw runtime_error("unable to open " + filename); 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", buffer->hook_manager().add_hook("BufClose",
[fd, buffer](const String&, const Context&) { [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, buffer->insert(buffer->end()-1,
count > 0 ? String(data, data+count) count > 0 ? String(data, data+count)
: "*** kak: fifo closed ***\n"); : "*** kak: fifo closed ***\n");
buffer->reset_undo_data();
ClientManager::instance().redraw_clients(); ClientManager::instance().redraw_clients();
if (count <= 0) if (count <= 0)
{ {

View File

@ -13,7 +13,7 @@ static Buffer& get_or_create_debug_buffer()
Buffer* buffer = BufferManager::instance().get_buffer(debug_buffer_name); Buffer* buffer = BufferManager::instance().get_buffer(debug_buffer_name);
if (not buffer) if (not buffer)
buffer = new Buffer(debug_buffer_name, Buffer::Flags::None, ""); buffer = new Buffer(debug_buffer_name, Buffer::Flags::NoUndo);
assert(buffer); assert(buffer);
return *buffer; return *buffer;
@ -25,7 +25,6 @@ void write_debug(const String& str)
Editor editor(debug_buffer); Editor editor(debug_buffer);
editor.select(debug_buffer.end()-1); editor.select(debug_buffer.end()-1);
editor.insert(str + "\n"); editor.insert(str + "\n");
debug_buffer.reset_undo_data();
} }
} }

View File

@ -85,7 +85,7 @@ Buffer* create_buffer_from_file(const String& filename)
if (Buffer* buffer = BufferManager::instance().get_buffer(filename)) if (Buffer* buffer = BufferManager::instance().get_buffer(filename))
delete buffer; delete buffer;
Buffer* buffer = new Buffer(filename, Buffer::Flags::File, ""); Buffer* buffer = new Buffer(filename, Buffer::Flags::File | Buffer::Flags::NoUndo);
String content; String content;
char buf[256]; char buf[256];
@ -131,8 +131,8 @@ Buffer* create_buffer_from_file(const String& filename)
if (*(buffer->end() - 2) == '\n') if (*(buffer->end() - 2) == '\n')
buffer->erase(buffer->end() - 1, buffer->end()); buffer->erase(buffer->end() - 1, buffer->end());
// it never happened, buffer always was like that // enable undo data recording
buffer->reset_undo_data(); buffer->flags() &= ~Buffer::Flags::NoUndo;
return buffer; return buffer;
} }

View File

@ -612,7 +612,7 @@ void create_local_client(const String& file)
} }
} }
else else
buffer = new Buffer("*scratch*", Buffer::Flags::None); buffer = new Buffer("*scratch*", Buffer::Flags::NoUndo);
ClientManager::instance().create_client( ClientManager::instance().create_client(
std::unique_ptr<UserInterface>{ui}, *buffer, 0); std::unique_ptr<UserInterface>{ui}, *buffer, 0);