2011-09-02 20:51:20 +04:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-20 01:56:29 +04:00
|
|
|
#include "assert.hh"
|
2011-11-30 02:37:20 +04:00
|
|
|
#include "highlighter_registry.hh"
|
2011-11-26 22:39:59 +04:00
|
|
|
#include "hooks_manager.hh"
|
2011-09-20 01:56:29 +04:00
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
#include <algorithm>
|
2011-10-04 22:49:41 +04:00
|
|
|
#include <sstream>
|
2011-09-02 20:51:20 +04:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2011-09-23 13:17:19 +04:00
|
|
|
BufferIterator Selection::begin() const
|
|
|
|
{
|
|
|
|
return std::min(m_first, m_last);
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferIterator Selection::end() const
|
|
|
|
{
|
|
|
|
return std::max(m_first, m_last) + 1;
|
|
|
|
}
|
|
|
|
|
2011-10-27 18:22:17 +04:00
|
|
|
void Selection::merge_with(const Selection& selection)
|
2011-09-23 13:17:19 +04:00
|
|
|
{
|
2011-10-27 18:22:17 +04:00
|
|
|
if (m_first <= m_last)
|
|
|
|
m_first = std::min(m_first, selection.m_first);
|
|
|
|
else
|
|
|
|
m_first = std::max(m_first, selection.m_first);
|
|
|
|
m_last = selection.m_last;
|
2011-09-23 13:17:19 +04:00
|
|
|
}
|
|
|
|
|
2011-11-16 23:23:09 +04:00
|
|
|
BufferString Selection::capture(size_t index) const
|
|
|
|
{
|
|
|
|
if (index < m_captures.size())
|
|
|
|
return m_captures[index];
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2011-09-24 16:39:40 +04:00
|
|
|
struct scoped_undo_group
|
|
|
|
{
|
|
|
|
scoped_undo_group(Buffer& buffer)
|
|
|
|
: m_buffer(buffer) { m_buffer.begin_undo_group(); }
|
|
|
|
|
|
|
|
~scoped_undo_group() { m_buffer.end_undo_group(); }
|
|
|
|
private:
|
|
|
|
Buffer& m_buffer;
|
|
|
|
};
|
|
|
|
|
2011-09-08 04:13:19 +04:00
|
|
|
Window::Window(Buffer& buffer)
|
2011-09-02 20:51:20 +04:00
|
|
|
: m_buffer(buffer),
|
|
|
|
m_position(0, 0),
|
2011-09-20 01:56:29 +04:00
|
|
|
m_dimensions(0, 0),
|
|
|
|
m_current_inserter(nullptr)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
m_selections.push_back(SelectionList());
|
|
|
|
selections().push_back(Selection(buffer.begin(), buffer.begin()));
|
2011-11-09 18:06:05 +04:00
|
|
|
|
2011-11-30 02:37:20 +04:00
|
|
|
HighlighterRegistry& registry = HighlighterRegistry::instance();
|
2011-11-26 22:39:59 +04:00
|
|
|
|
|
|
|
HooksManager::instance().run_hook("WinCreate", buffer.name(),
|
|
|
|
Context(*this));
|
|
|
|
|
2011-11-30 02:37:20 +04:00
|
|
|
registry.add_highlighter_to_window(*this, "expand_tabs", HighlighterParameters());
|
|
|
|
registry.add_highlighter_to_window(*this, "highlight_selections", HighlighterParameters());
|
2011-09-27 22:45:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::check_invariant() const
|
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
assert(not selections().empty());
|
2011-09-27 22:45:22 +04:00
|
|
|
}
|
|
|
|
|
2011-10-14 18:29:53 +04:00
|
|
|
DisplayCoord Window::cursor_position() const
|
2011-09-27 22:45:22 +04:00
|
|
|
{
|
|
|
|
check_invariant();
|
2011-10-15 08:45:49 +04:00
|
|
|
return line_and_column_at(cursor_iterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferIterator Window::cursor_iterator() const
|
|
|
|
{
|
|
|
|
check_invariant();
|
2011-12-21 23:06:26 +04:00
|
|
|
return selections().back().last();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::erase()
|
|
|
|
{
|
2011-12-21 22:56:32 +04:00
|
|
|
if (m_current_inserter == nullptr)
|
|
|
|
{
|
|
|
|
scoped_undo_group undo_group(m_buffer);
|
|
|
|
erase_noundo();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
erase_noundo();
|
2011-09-27 22:45:22 +04:00
|
|
|
}
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2011-09-27 22:45:22 +04:00
|
|
|
void Window::erase_noundo()
|
|
|
|
{
|
|
|
|
check_invariant();
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : selections())
|
2011-12-07 18:26:40 +04:00
|
|
|
m_buffer.modify(Modification::make_erase(sel.begin(), sel.end()));
|
2011-10-10 18:28:39 +04:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2011-10-14 18:29:53 +04:00
|
|
|
template<typename Iterator>
|
|
|
|
static DisplayCoord measure_string(Iterator begin, Iterator end)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-10-14 18:29:53 +04:00
|
|
|
DisplayCoord result(0, 0);
|
|
|
|
while (begin != end)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-10-14 18:29:53 +04:00
|
|
|
if (*begin == '\n')
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
|
|
|
++result.line;
|
|
|
|
result.column = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++result.column;
|
2011-10-14 18:29:53 +04:00
|
|
|
++begin;
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-10-14 18:29:53 +04:00
|
|
|
static DisplayCoord measure_string(const Window::String& string)
|
|
|
|
{
|
|
|
|
return measure_string(string.begin(), string.end());
|
|
|
|
}
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
void Window::insert(const String& string)
|
|
|
|
{
|
2011-12-21 22:56:32 +04:00
|
|
|
if (m_current_inserter == nullptr)
|
|
|
|
{
|
|
|
|
scoped_undo_group undo_group(m_buffer);
|
|
|
|
insert_noundo(string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
insert_noundo(string);
|
2011-09-27 22:45:22 +04:00
|
|
|
}
|
2011-09-24 16:39:40 +04:00
|
|
|
|
2011-09-27 22:45:22 +04:00
|
|
|
void Window::insert_noundo(const String& string)
|
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : selections())
|
2011-12-07 18:26:40 +04:00
|
|
|
m_buffer.modify(Modification::make_insert(sel.begin(), string));
|
2011-10-10 18:28:39 +04:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::append(const String& string)
|
|
|
|
{
|
2011-12-21 22:56:32 +04:00
|
|
|
if (m_current_inserter == nullptr)
|
|
|
|
{
|
|
|
|
scoped_undo_group undo_group(m_buffer);
|
|
|
|
append_noundo(string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
append_noundo(string);
|
2011-09-27 22:45:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::append_noundo(const String& string)
|
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : selections())
|
2011-12-07 18:26:40 +04:00
|
|
|
m_buffer.modify(Modification::make_insert(sel.end(), string));
|
2011-10-10 18:28:39 +04:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-06 22:52:52 +04:00
|
|
|
}
|
|
|
|
|
2011-11-22 18:23:46 +04:00
|
|
|
void Window::replace(const std::string& string)
|
|
|
|
{
|
2011-12-21 22:56:32 +04:00
|
|
|
if (m_current_inserter == nullptr)
|
|
|
|
{
|
|
|
|
scoped_undo_group undo_group(m_buffer);
|
|
|
|
erase_noundo();
|
|
|
|
insert_noundo(string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
erase_noundo();
|
|
|
|
insert_noundo(string);
|
|
|
|
}
|
2011-11-22 18:23:46 +04:00
|
|
|
}
|
|
|
|
|
2011-09-06 22:52:52 +04:00
|
|
|
bool Window::undo()
|
|
|
|
{
|
2011-09-08 04:13:19 +04:00
|
|
|
return m_buffer.undo();
|
2011-09-06 22:52:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::redo()
|
|
|
|
{
|
2011-09-08 04:13:19 +04:00
|
|
|
return m_buffer.redo();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2011-10-15 08:45:49 +04:00
|
|
|
BufferIterator Window::iterator_at(const DisplayCoord& window_pos) const
|
2011-09-05 22:54:17 +04:00
|
|
|
{
|
2011-10-15 08:45:49 +04:00
|
|
|
if (m_display_buffer.begin() == m_display_buffer.end())
|
|
|
|
return m_buffer.begin();
|
2011-09-05 22:54:17 +04:00
|
|
|
|
2011-10-15 08:45:49 +04:00
|
|
|
if (DisplayCoord(0,0) <= window_pos)
|
|
|
|
{
|
|
|
|
for (auto atom_it = m_display_buffer.begin();
|
|
|
|
atom_it != m_display_buffer.end(); ++atom_it)
|
|
|
|
{
|
|
|
|
if (window_pos < atom_it->coord())
|
|
|
|
{
|
2011-10-18 17:59:32 +04:00
|
|
|
return (--atom_it)->iterator_at(window_pos);
|
2011-10-15 08:45:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-05 22:54:17 +04:00
|
|
|
|
2011-10-15 08:45:49 +04:00
|
|
|
return m_buffer.iterator_at(m_position + BufferCoord(window_pos));
|
2011-09-05 22:54:17 +04:00
|
|
|
}
|
|
|
|
|
2011-10-14 18:29:53 +04:00
|
|
|
DisplayCoord Window::line_and_column_at(const BufferIterator& iterator) const
|
2011-09-05 22:54:17 +04:00
|
|
|
{
|
2011-10-15 08:45:49 +04:00
|
|
|
if (m_display_buffer.begin() == m_display_buffer.end())
|
|
|
|
return DisplayCoord(0, 0);
|
|
|
|
|
|
|
|
if (iterator >= m_display_buffer.front().begin() and
|
|
|
|
iterator < m_display_buffer.back().end())
|
|
|
|
{
|
|
|
|
for (auto& atom : m_display_buffer)
|
|
|
|
{
|
|
|
|
if (atom.end() > iterator)
|
|
|
|
{
|
|
|
|
assert(atom.begin() <= iterator);
|
|
|
|
return atom.line_and_column_at(iterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BufferCoord coord = m_buffer.line_and_column_at(iterator);
|
|
|
|
return DisplayCoord(coord.line - m_position.line,
|
|
|
|
coord.column - m_position.column);
|
2011-09-05 22:54:17 +04:00
|
|
|
}
|
|
|
|
|
2011-10-05 18:24:52 +04:00
|
|
|
void Window::clear_selections()
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-09-27 22:45:22 +04:00
|
|
|
check_invariant();
|
2011-12-21 23:06:26 +04:00
|
|
|
BufferIterator pos = selections().back().last();
|
2011-12-05 18:29:36 +04:00
|
|
|
|
2011-11-30 02:48:00 +04:00
|
|
|
if (*pos == '\n' and not pos.is_begin() and *(pos-1) != '\n')
|
|
|
|
--pos;
|
2011-12-05 18:29:36 +04:00
|
|
|
|
2011-11-30 02:48:00 +04:00
|
|
|
Selection sel = Selection(pos, pos);
|
2011-12-21 23:06:26 +04:00
|
|
|
selections().clear();
|
|
|
|
selections().push_back(std::move(sel));
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2011-10-07 18:03:25 +04:00
|
|
|
void Window::select(const Selector& selector, bool append)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-09-27 22:45:22 +04:00
|
|
|
check_invariant();
|
|
|
|
|
2011-10-07 18:03:25 +04:00
|
|
|
if (not append)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
Selection sel = selector(selections().back().last());
|
|
|
|
selections().clear();
|
|
|
|
selections().push_back(std::move(sel));
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : selections())
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-10-27 18:22:17 +04:00
|
|
|
sel.merge_with(selector(sel.last()));
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
}
|
2011-09-05 22:54:17 +04:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2011-12-20 23:27:13 +04:00
|
|
|
struct nothing_selected : public runtime_error
|
|
|
|
{
|
|
|
|
nothing_selected() : runtime_error("nothing was selected") {}
|
|
|
|
};
|
|
|
|
|
2011-11-16 18:04:45 +04:00
|
|
|
void Window::multi_select(const MultiSelector& selector)
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
|
|
|
|
SelectionList new_selections;
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : selections())
|
2011-11-16 18:04:45 +04:00
|
|
|
{
|
|
|
|
SelectionList selections = selector(sel);
|
|
|
|
std::copy(selections.begin(), selections.end(),
|
|
|
|
std::back_inserter(new_selections));
|
|
|
|
}
|
2011-12-20 23:27:13 +04:00
|
|
|
if (new_selections.empty())
|
|
|
|
throw nothing_selected();
|
|
|
|
|
2011-12-21 23:06:26 +04:00
|
|
|
selections() = std::move(new_selections);
|
2011-12-20 23:27:13 +04:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-11-16 18:04:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 18:31:15 +04:00
|
|
|
BufferString Window::selection_content() const
|
|
|
|
{
|
2011-09-27 22:45:22 +04:00
|
|
|
check_invariant();
|
|
|
|
|
2011-12-21 23:06:26 +04:00
|
|
|
return m_buffer.string(selections().back().begin(),
|
|
|
|
selections().back().end());
|
2011-09-23 18:31:15 +04:00
|
|
|
}
|
|
|
|
|
2011-10-14 18:29:53 +04:00
|
|
|
void Window::move_cursor(const DisplayCoord& offset, bool append)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-10-07 18:03:25 +04:00
|
|
|
if (not append)
|
2011-10-15 08:45:49 +04:00
|
|
|
{
|
|
|
|
BufferCoord pos = m_buffer.line_and_column_at(cursor_iterator());
|
|
|
|
move_cursor_to(m_buffer.iterator_at(pos + BufferCoord(offset)));
|
|
|
|
}
|
2011-09-28 18:23:43 +04:00
|
|
|
else
|
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : selections())
|
2011-09-28 18:23:43 +04:00
|
|
|
{
|
2011-10-15 08:45:49 +04:00
|
|
|
BufferCoord pos = m_buffer.line_and_column_at(sel.last());
|
|
|
|
sel = Selection(sel.first(), m_buffer.iterator_at(pos + BufferCoord(offset)));
|
2011-09-28 18:23:43 +04:00
|
|
|
}
|
2011-09-28 23:21:49 +04:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-28 18:23:43 +04:00
|
|
|
}
|
2011-09-22 18:00:04 +04:00
|
|
|
}
|
|
|
|
|
2011-10-12 22:53:38 +04:00
|
|
|
void Window::move_cursor_to(const BufferIterator& iterator)
|
2011-09-22 18:00:04 +04:00
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
selections().clear();
|
|
|
|
selections().push_back(Selection(iterator, iterator));
|
2011-09-05 22:54:17 +04:00
|
|
|
|
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::update_display_buffer()
|
|
|
|
{
|
|
|
|
m_display_buffer.clear();
|
|
|
|
|
2011-09-29 00:54:11 +04:00
|
|
|
BufferIterator begin = m_buffer.iterator_at(m_position);
|
|
|
|
BufferIterator end = m_buffer.iterator_at(m_position +
|
2011-11-24 18:23:41 +04:00
|
|
|
BufferCoord(m_dimensions.line, m_dimensions.column))+1;
|
2011-10-07 18:19:58 +04:00
|
|
|
if (begin == end)
|
|
|
|
return;
|
|
|
|
|
2011-10-15 08:45:49 +04:00
|
|
|
m_display_buffer.append(DisplayAtom(DisplayCoord(0,0), begin, end));
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2011-11-30 02:37:20 +04:00
|
|
|
for (auto& highlighter : m_highlighters)
|
2011-09-29 13:10:27 +04:00
|
|
|
{
|
2011-11-30 02:37:20 +04:00
|
|
|
highlighter.second(m_display_buffer);
|
2011-09-29 13:10:27 +04:00
|
|
|
m_display_buffer.check_invariant();
|
|
|
|
}
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2011-10-14 18:29:53 +04:00
|
|
|
void Window::set_dimensions(const DisplayCoord& dimensions)
|
2011-09-05 22:54:17 +04:00
|
|
|
{
|
|
|
|
m_dimensions = dimensions;
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:55:31 +04:00
|
|
|
void Window::scroll_to_keep_cursor_visible_ifn()
|
|
|
|
{
|
2011-09-27 22:45:22 +04:00
|
|
|
check_invariant();
|
|
|
|
|
2011-12-21 23:06:26 +04:00
|
|
|
DisplayCoord cursor = line_and_column_at(selections().back().last());
|
2011-09-27 22:45:22 +04:00
|
|
|
if (cursor.line < 0)
|
2011-09-05 22:55:31 +04:00
|
|
|
{
|
2011-09-27 22:45:22 +04:00
|
|
|
m_position.line = std::max(m_position.line + cursor.line, 0);
|
2011-09-05 22:55:31 +04:00
|
|
|
}
|
2011-09-27 22:45:22 +04:00
|
|
|
else if (cursor.line >= m_dimensions.line)
|
2011-09-05 22:55:31 +04:00
|
|
|
{
|
2011-09-27 22:45:22 +04:00
|
|
|
m_position.line += cursor.line - (m_dimensions.line - 1);
|
2011-09-05 22:55:31 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 22:45:22 +04:00
|
|
|
if (cursor.column < 0)
|
2011-09-05 22:55:31 +04:00
|
|
|
{
|
2011-09-27 22:45:22 +04:00
|
|
|
m_position.column = std::max(m_position.column + cursor.column, 0);
|
2011-09-05 22:55:31 +04:00
|
|
|
}
|
2011-09-27 22:45:22 +04:00
|
|
|
else if (cursor.column >= m_dimensions.column)
|
2011-09-05 22:55:31 +04:00
|
|
|
{
|
2011-09-27 22:45:22 +04:00
|
|
|
m_position.column += cursor.column - (m_dimensions.column - 1);
|
2011-09-05 22:55:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 22:49:41 +04:00
|
|
|
std::string Window::status_line() const
|
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
BufferCoord cursor = m_buffer.line_and_column_at(selections().back().last());
|
2011-10-04 22:49:41 +04:00
|
|
|
std::ostringstream oss;
|
2011-10-05 18:21:24 +04:00
|
|
|
oss << m_buffer.name();
|
|
|
|
if (m_buffer.is_modified())
|
|
|
|
oss << " [+]";
|
2011-10-27 18:27:39 +04:00
|
|
|
oss << " -- " << cursor.line+1 << "," << cursor.column+1
|
2011-12-21 23:06:26 +04:00
|
|
|
<< " -- " << selections().size() << " sel -- ";
|
2011-10-05 18:21:24 +04:00
|
|
|
if (m_current_inserter)
|
|
|
|
oss << "[Insert]";
|
2011-10-04 22:49:41 +04:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2011-11-30 02:37:20 +04:00
|
|
|
void Window::add_highlighter(HighlighterAndId&& highlighter)
|
2011-11-08 18:29:52 +04:00
|
|
|
{
|
2011-12-02 18:20:11 +04:00
|
|
|
if (m_highlighters.contains(highlighter.first))
|
2011-12-07 18:29:10 +04:00
|
|
|
throw id_not_unique(highlighter.first);
|
2011-12-07 22:57:07 +04:00
|
|
|
m_highlighters.append(std::forward<HighlighterAndId>(highlighter));
|
2011-11-08 18:29:52 +04:00
|
|
|
}
|
|
|
|
|
2011-11-30 02:37:20 +04:00
|
|
|
void Window::remove_highlighter(const std::string& id)
|
2011-11-08 18:29:52 +04:00
|
|
|
{
|
2011-12-02 18:20:11 +04:00
|
|
|
m_highlighters.remove(id);
|
2011-11-08 18:29:52 +04:00
|
|
|
}
|
|
|
|
|
2011-11-30 02:37:20 +04:00
|
|
|
CandidateList Window::complete_highlighterid(const std::string& prefix,
|
2011-12-02 18:20:11 +04:00
|
|
|
size_t cursor_pos)
|
2011-11-12 18:15:35 +04:00
|
|
|
{
|
2011-12-02 18:20:11 +04:00
|
|
|
return m_highlighters.complete_id<str_to_str>(prefix, cursor_pos);
|
2011-11-12 18:15:35 +04:00
|
|
|
}
|
|
|
|
|
2011-12-07 18:29:10 +04:00
|
|
|
void Window::add_filter(FilterAndId&& filter)
|
|
|
|
{
|
|
|
|
if (m_filters.contains(filter.first))
|
|
|
|
throw id_not_unique(filter.first);
|
2011-12-07 22:57:07 +04:00
|
|
|
m_filters.append(std::forward<FilterAndId>(filter));
|
2011-12-07 18:29:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::remove_filter(const std::string& id)
|
|
|
|
{
|
|
|
|
m_filters.remove(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
CandidateList Window::complete_filterid(const std::string& prefix,
|
|
|
|
size_t cursor_pos)
|
|
|
|
{
|
|
|
|
return m_filters.complete_id<str_to_str>(prefix, cursor_pos);
|
|
|
|
}
|
|
|
|
|
2011-12-21 23:06:26 +04:00
|
|
|
void Window::push_selections()
|
|
|
|
{
|
|
|
|
SelectionList current_selections = selections();
|
|
|
|
m_selections.push_back(std::move(current_selections));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::pop_selections()
|
|
|
|
{
|
|
|
|
if (m_selections.size() > 1)
|
|
|
|
m_selections.pop_back();
|
|
|
|
else
|
|
|
|
throw runtime_error("no more selections on stack");
|
|
|
|
}
|
2011-11-12 18:15:35 +04:00
|
|
|
|
2011-09-28 23:14:39 +04:00
|
|
|
IncrementalInserter::IncrementalInserter(Window& window, Mode mode)
|
2011-09-20 01:56:29 +04:00
|
|
|
: m_window(window)
|
|
|
|
{
|
|
|
|
assert(not m_window.m_current_inserter);
|
|
|
|
m_window.m_current_inserter = this;
|
2011-09-27 22:45:22 +04:00
|
|
|
m_window.check_invariant();
|
2011-09-20 01:56:29 +04:00
|
|
|
|
2011-09-28 23:14:39 +04:00
|
|
|
m_window.m_buffer.begin_undo_group();
|
|
|
|
|
|
|
|
if (mode == Mode::Change)
|
|
|
|
window.erase_noundo();
|
|
|
|
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : m_window.selections())
|
2011-09-20 01:56:29 +04:00
|
|
|
{
|
2011-11-30 01:35:50 +04:00
|
|
|
DynamicBufferIterator pos;
|
2011-10-07 01:12:55 +04:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case Mode::Insert: pos = sel.begin(); break;
|
|
|
|
case Mode::Append: pos = sel.end(); break;
|
|
|
|
case Mode::Change: pos = sel.begin(); break;
|
|
|
|
|
|
|
|
case Mode::OpenLineBelow:
|
2011-11-03 00:03:41 +04:00
|
|
|
case Mode::AppendAtLineEnd:
|
2011-11-30 01:35:50 +04:00
|
|
|
pos = m_window.m_buffer.iterator_at_line_end(sel.end() - 1) - 1;
|
2011-11-03 00:03:41 +04:00
|
|
|
if (mode == Mode::OpenLineBelow)
|
2011-12-07 18:29:10 +04:00
|
|
|
apply(Modification::make_insert(pos, "\n"));
|
2011-10-07 01:12:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Mode::OpenLineAbove:
|
2011-11-03 00:03:41 +04:00
|
|
|
case Mode::InsertAtLineBegin:
|
2011-11-30 01:35:50 +04:00
|
|
|
pos = m_window.m_buffer.iterator_at_line_begin(sel.begin());
|
2011-11-03 00:03:41 +04:00
|
|
|
if (mode == Mode::OpenLineAbove)
|
2011-12-07 18:29:10 +04:00
|
|
|
apply(Modification::make_insert(--pos, "\n"));
|
2011-10-07 01:12:55 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-11-16 23:24:37 +04:00
|
|
|
sel = Selection(pos, pos, sel.captures());
|
2011-09-20 01:56:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IncrementalInserter::~IncrementalInserter()
|
|
|
|
{
|
2011-11-04 18:15:35 +04:00
|
|
|
move_cursor(DisplayCoord(0, -1));
|
2011-12-21 22:57:07 +04:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
HooksManager::instance().run_hook("WinInsertEnd", "", Context(m_window));
|
|
|
|
}
|
|
|
|
catch (runtime_error& e) {}
|
|
|
|
|
2011-09-20 01:56:29 +04:00
|
|
|
assert(m_window.m_current_inserter == this);
|
|
|
|
m_window.m_current_inserter = nullptr;
|
|
|
|
m_window.m_buffer.end_undo_group();
|
|
|
|
}
|
|
|
|
|
2011-12-07 18:29:10 +04:00
|
|
|
void IncrementalInserter::apply(Modification&& modification) const
|
|
|
|
{
|
|
|
|
for (auto filter : m_window.m_filters)
|
|
|
|
filter.second(m_window.buffer(), modification);
|
|
|
|
m_window.buffer().modify(std::move(modification));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-20 01:56:29 +04:00
|
|
|
void IncrementalInserter::insert(const Window::String& string)
|
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : m_window.selections())
|
2011-12-07 18:29:10 +04:00
|
|
|
apply(Modification::make_insert(sel.begin(), string));
|
2011-09-20 01:56:29 +04:00
|
|
|
}
|
|
|
|
|
2011-11-16 23:24:37 +04:00
|
|
|
void IncrementalInserter::insert_capture(size_t index)
|
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : m_window.selections())
|
2011-12-07 18:26:40 +04:00
|
|
|
m_window.m_buffer.modify(Modification::make_insert(sel.begin(),
|
|
|
|
sel.capture(index)));
|
2011-11-16 23:24:37 +04:00
|
|
|
m_window.scroll_to_keep_cursor_visible_ifn();
|
|
|
|
}
|
|
|
|
|
2011-09-20 01:56:29 +04:00
|
|
|
void IncrementalInserter::erase()
|
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : m_window.selections())
|
2011-12-07 18:29:10 +04:00
|
|
|
{
|
2011-11-22 02:18:00 +04:00
|
|
|
sel = Selection(sel.first() - 1, sel.last() - 1);
|
2011-12-07 18:29:10 +04:00
|
|
|
apply(Modification::make_erase(sel.begin(), sel.end()));
|
|
|
|
}
|
|
|
|
|
2011-11-22 02:18:00 +04:00
|
|
|
m_window.scroll_to_keep_cursor_visible_ifn();
|
2011-09-20 01:56:29 +04:00
|
|
|
}
|
|
|
|
|
2011-10-14 18:29:53 +04:00
|
|
|
void IncrementalInserter::move_cursor(const DisplayCoord& offset)
|
2011-09-20 01:56:29 +04:00
|
|
|
{
|
2011-12-21 23:06:26 +04:00
|
|
|
for (auto& sel : m_window.selections())
|
2011-09-20 01:56:29 +04:00
|
|
|
{
|
2011-10-14 18:29:53 +04:00
|
|
|
DisplayCoord pos = m_window.line_and_column_at(sel.last());
|
2011-09-27 22:45:22 +04:00
|
|
|
BufferIterator it = m_window.iterator_at(pos + offset);
|
|
|
|
sel = Selection(it, it);
|
2011-09-20 01:56:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|