Add support for BufInsert/BufErase hooks

This commit is contained in:
Maxime Coste 2014-12-17 13:56:45 +00:00
parent 3b9f40fd58
commit 5f3a477277
2 changed files with 21 additions and 2 deletions

View File

@ -811,6 +811,10 @@ existing hooks are:
* +BufOpen+: A buffer for an existing file has been created, filename is
used for filtering
* +BufCreate+: A buffer has been created, filename is used for filtering
* +BufInsert+: An insertion just took place in a buffer, filtering string
is 'line.column+len'
* +BufErase+: An erase just took place in a buffer, filtering string
is 'line.column+len'
* +BufWritePre+: Executed just before a buffer is written, filename is
used for filtering.
* +BufWritePost+: Executed just after a buffer is written, filename is

View File

@ -374,6 +374,11 @@ void Buffer::apply_modification(const Modification& modification)
}
}
static String change_spec(ByteCoord coord, ByteCount len)
{
return to_string(coord.line) + "." + to_string(coord.column) + "+" + to_string(len);
}
BufferIterator Buffer::insert(const BufferIterator& pos, StringView content)
{
kak_assert(is_valid(pos.coord()));
@ -391,7 +396,11 @@ BufferIterator Buffer::insert(const BufferIterator& pos, StringView content)
auto coord = pos == end() ? ByteCoord{line_count()} : pos.coord();
if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(Modification::Insert, coord, real_content);
return {*this, do_insert(pos.coord(), real_content)};
BufferIterator it{*this, do_insert(pos.coord(), real_content)};
run_hook_in_own_context("BufInsert", change_spec(pos.coord(), real_content.length()));
return it;
}
BufferIterator Buffer::erase(BufferIterator begin, BufferIterator end)
@ -406,7 +415,13 @@ BufferIterator Buffer::erase(BufferIterator begin, BufferIterator end)
if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(Modification::Erase, begin.coord(),
InternedString(string(begin.coord(), end.coord())));
return {*this, do_erase(begin.coord(), end.coord())};
ByteCount len = distance(begin.coord(), end.coord());
BufferIterator it{*this, do_erase(begin.coord(), end.coord())};
run_hook_in_own_context("BufErase", change_spec(begin.coord(), len));
return it;
}
bool Buffer::is_modified() const