2011-09-02 18:51:20 +02:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-19 23:56:29 +02:00
|
|
|
#include "assert.hh"
|
2011-11-09 15:06:05 +01:00
|
|
|
#include "filter_registry.hh"
|
2011-09-19 23:56:29 +02:00
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
#include <algorithm>
|
2011-10-04 20:49:41 +02:00
|
|
|
#include <sstream>
|
2011-09-02 18:51:20 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2011-09-23 11:17:19 +02: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 16:22:17 +02:00
|
|
|
void Selection::merge_with(const Selection& selection)
|
2011-09-23 11:17:19 +02:00
|
|
|
{
|
2011-10-27 16:22:17 +02: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 11:17:19 +02:00
|
|
|
}
|
|
|
|
|
2011-11-16 20:23:09 +01:00
|
|
|
BufferString Selection::capture(size_t index) const
|
|
|
|
{
|
|
|
|
if (index < m_captures.size())
|
|
|
|
return m_captures[index];
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2011-09-24 14:39:40 +02: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 02:13:19 +02:00
|
|
|
Window::Window(Buffer& buffer)
|
2011-09-02 18:51:20 +02:00
|
|
|
: m_buffer(buffer),
|
|
|
|
m_position(0, 0),
|
2011-09-19 23:56:29 +02:00
|
|
|
m_dimensions(0, 0),
|
|
|
|
m_current_inserter(nullptr)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_selections.push_back(Selection(buffer.begin(), buffer.begin()));
|
2011-11-09 15:06:05 +01:00
|
|
|
|
|
|
|
FilterRegistry& registry = FilterRegistry::instance();
|
2011-11-10 00:56:22 +01:00
|
|
|
registry.add_filter_to_window(*this, "expand_tabs", FilterParameters());
|
|
|
|
registry.add_filter_to_window(*this, "highlight_selections", FilterParameters());
|
|
|
|
registry.add_filter_to_window(*this, "hlcpp", FilterParameters());
|
2011-09-27 20:45:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::check_invariant() const
|
|
|
|
{
|
|
|
|
assert(not m_selections.empty());
|
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
DisplayCoord Window::cursor_position() const
|
2011-09-27 20:45:22 +02:00
|
|
|
{
|
|
|
|
check_invariant();
|
2011-10-15 06:45:49 +02:00
|
|
|
return line_and_column_at(cursor_iterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferIterator Window::cursor_iterator() const
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
return m_selections.back().last();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::erase()
|
|
|
|
{
|
2011-09-24 14:39:40 +02:00
|
|
|
scoped_undo_group undo_group(m_buffer);
|
2011-09-27 20:45:22 +02:00
|
|
|
erase_noundo();
|
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2011-09-27 20:45:22 +02:00
|
|
|
void Window::erase_noundo()
|
|
|
|
{
|
|
|
|
check_invariant();
|
2011-09-17 16:57:35 +02:00
|
|
|
for (auto& sel : m_selections)
|
|
|
|
m_buffer.erase(sel.begin(), sel.end());
|
2011-10-10 16:28:39 +02:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
template<typename Iterator>
|
|
|
|
static DisplayCoord measure_string(Iterator begin, Iterator end)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-10-14 16:29:53 +02:00
|
|
|
DisplayCoord result(0, 0);
|
|
|
|
while (begin != end)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-10-14 16:29:53 +02:00
|
|
|
if (*begin == '\n')
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
|
|
|
++result.line;
|
|
|
|
result.column = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++result.column;
|
2011-10-14 16:29:53 +02:00
|
|
|
++begin;
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
static DisplayCoord measure_string(const Window::String& string)
|
|
|
|
{
|
|
|
|
return measure_string(string.begin(), string.end());
|
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
void Window::insert(const String& string)
|
|
|
|
{
|
2011-09-24 14:39:40 +02:00
|
|
|
scoped_undo_group undo_group(m_buffer);
|
2011-09-27 20:45:22 +02:00
|
|
|
insert_noundo(string);
|
|
|
|
}
|
2011-09-24 14:39:40 +02:00
|
|
|
|
2011-09-27 20:45:22 +02:00
|
|
|
void Window::insert_noundo(const String& string)
|
|
|
|
{
|
2011-09-17 16:57:35 +02:00
|
|
|
for (auto& sel : m_selections)
|
|
|
|
m_buffer.insert(sel.begin(), string);
|
2011-10-10 16:28:39 +02:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::append(const String& string)
|
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
scoped_undo_group undo_group(m_buffer);
|
|
|
|
append_noundo(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::append_noundo(const String& string)
|
|
|
|
{
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
m_buffer.insert(sel.end(), string);
|
2011-10-10 16:28:39 +02:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-06 20:52:52 +02:00
|
|
|
}
|
|
|
|
|
2011-11-22 15:23:46 +01:00
|
|
|
void Window::replace(const std::string& string)
|
|
|
|
{
|
|
|
|
scoped_undo_group undo_group(m_buffer);
|
|
|
|
erase_noundo();
|
|
|
|
insert_noundo(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-06 20:52:52 +02:00
|
|
|
bool Window::undo()
|
|
|
|
{
|
2011-09-08 02:13:19 +02:00
|
|
|
return m_buffer.undo();
|
2011-09-06 20:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::redo()
|
|
|
|
{
|
2011-09-08 02:13:19 +02:00
|
|
|
return m_buffer.redo();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
BufferIterator Window::iterator_at(const DisplayCoord& window_pos) const
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
2011-10-15 06:45:49 +02:00
|
|
|
if (m_display_buffer.begin() == m_display_buffer.end())
|
|
|
|
return m_buffer.begin();
|
2011-09-05 20:54:17 +02:00
|
|
|
|
2011-10-15 06:45:49 +02: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 15:59:32 +02:00
|
|
|
return (--atom_it)->iterator_at(window_pos);
|
2011-10-15 06:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-05 20:54:17 +02:00
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
return m_buffer.iterator_at(m_position + BufferCoord(window_pos));
|
2011-09-05 20:54:17 +02:00
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
DisplayCoord Window::line_and_column_at(const BufferIterator& iterator) const
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
2011-10-15 06:45:49 +02: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 20:54:17 +02:00
|
|
|
}
|
|
|
|
|
2011-10-05 16:24:52 +02:00
|
|
|
void Window::clear_selections()
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
check_invariant();
|
|
|
|
Selection sel = Selection(m_selections.back().last(),
|
|
|
|
m_selections.back().last());
|
2011-09-02 18:51:20 +02:00
|
|
|
m_selections.clear();
|
2011-09-27 20:45:22 +02:00
|
|
|
m_selections.push_back(std::move(sel));
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2011-10-07 16:03:25 +02:00
|
|
|
void Window::select(const Selector& selector, bool append)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
check_invariant();
|
|
|
|
|
2011-10-07 16:03:25 +02:00
|
|
|
if (not append)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
Selection sel = selector(m_selections.back().last());
|
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(std::move(sel));
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-17 16:57:35 +02:00
|
|
|
for (auto& sel : m_selections)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-10-27 16:22:17 +02:00
|
|
|
sel.merge_with(selector(sel.last()));
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-05 20:54:17 +02:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2011-11-16 15:04:45 +01:00
|
|
|
void Window::multi_select(const MultiSelector& selector)
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
|
|
|
|
SelectionList new_selections;
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
{
|
|
|
|
SelectionList selections = selector(sel);
|
|
|
|
std::copy(selections.begin(), selections.end(),
|
|
|
|
std::back_inserter(new_selections));
|
|
|
|
}
|
|
|
|
m_selections = std::move(new_selections);
|
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
|
|
|
}
|
|
|
|
|
2011-09-23 16:31:15 +02:00
|
|
|
BufferString Window::selection_content() const
|
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
check_invariant();
|
|
|
|
|
|
|
|
return m_buffer.string(m_selections.back().begin(),
|
|
|
|
m_selections.back().end());
|
2011-09-23 16:31:15 +02:00
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
void Window::move_cursor(const DisplayCoord& offset, bool append)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-10-07 16:03:25 +02:00
|
|
|
if (not append)
|
2011-10-15 06:45:49 +02:00
|
|
|
{
|
|
|
|
BufferCoord pos = m_buffer.line_and_column_at(cursor_iterator());
|
|
|
|
move_cursor_to(m_buffer.iterator_at(pos + BufferCoord(offset)));
|
|
|
|
}
|
2011-09-28 16:23:43 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
{
|
2011-10-15 06:45:49 +02: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 16:23:43 +02:00
|
|
|
}
|
2011-09-28 21:21:49 +02:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-28 16:23:43 +02:00
|
|
|
}
|
2011-09-22 16:00:04 +02:00
|
|
|
}
|
|
|
|
|
2011-10-12 20:53:38 +02:00
|
|
|
void Window::move_cursor_to(const BufferIterator& iterator)
|
2011-09-22 16:00:04 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_selections.clear();
|
2011-10-12 20:53:38 +02:00
|
|
|
m_selections.push_back(Selection(iterator, iterator));
|
2011-09-05 20:54:17 +02:00
|
|
|
|
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::update_display_buffer()
|
|
|
|
{
|
|
|
|
m_display_buffer.clear();
|
|
|
|
|
2011-09-28 22:54:11 +02:00
|
|
|
BufferIterator begin = m_buffer.iterator_at(m_position);
|
|
|
|
BufferIterator end = m_buffer.iterator_at(m_position +
|
2011-11-24 15:23:41 +01:00
|
|
|
BufferCoord(m_dimensions.line, m_dimensions.column))+1;
|
2011-10-07 16:19:58 +02:00
|
|
|
if (begin == end)
|
|
|
|
return;
|
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
m_display_buffer.append(DisplayAtom(DisplayCoord(0,0), begin, end));
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2011-09-28 22:54:11 +02:00
|
|
|
for (auto& filter : m_filters)
|
2011-09-29 11:10:27 +02:00
|
|
|
{
|
2011-11-08 15:29:52 +01:00
|
|
|
filter.second(m_display_buffer);
|
2011-09-29 11:10:27 +02:00
|
|
|
m_display_buffer.check_invariant();
|
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
void Window::set_dimensions(const DisplayCoord& dimensions)
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
|
|
|
m_dimensions = dimensions;
|
|
|
|
}
|
|
|
|
|
2011-09-05 20:55:31 +02:00
|
|
|
void Window::scroll_to_keep_cursor_visible_ifn()
|
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
check_invariant();
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
DisplayCoord cursor = line_and_column_at(m_selections.back().last());
|
2011-09-27 20:45:22 +02:00
|
|
|
if (cursor.line < 0)
|
2011-09-05 20:55:31 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_position.line = std::max(m_position.line + cursor.line, 0);
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
2011-09-27 20:45:22 +02:00
|
|
|
else if (cursor.line >= m_dimensions.line)
|
2011-09-05 20:55:31 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_position.line += cursor.line - (m_dimensions.line - 1);
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
|
|
|
|
2011-09-27 20:45:22 +02:00
|
|
|
if (cursor.column < 0)
|
2011-09-05 20:55:31 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_position.column = std::max(m_position.column + cursor.column, 0);
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
2011-09-27 20:45:22 +02:00
|
|
|
else if (cursor.column >= m_dimensions.column)
|
2011-09-05 20:55:31 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_position.column += cursor.column - (m_dimensions.column - 1);
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 20:49:41 +02:00
|
|
|
std::string Window::status_line() const
|
|
|
|
{
|
2011-10-18 15:49:10 +02:00
|
|
|
BufferCoord cursor = m_buffer.line_and_column_at(m_selections.back().last());
|
2011-10-04 20:49:41 +02:00
|
|
|
std::ostringstream oss;
|
2011-10-05 16:21:24 +02:00
|
|
|
oss << m_buffer.name();
|
|
|
|
if (m_buffer.is_modified())
|
|
|
|
oss << " [+]";
|
2011-10-27 16:27:39 +02:00
|
|
|
oss << " -- " << cursor.line+1 << "," << cursor.column+1
|
2011-10-04 20:49:41 +02:00
|
|
|
<< " -- " << m_selections.size() << " sel -- ";
|
2011-10-05 16:21:24 +02:00
|
|
|
if (m_current_inserter)
|
|
|
|
oss << "[Insert]";
|
2011-10-04 20:49:41 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2011-11-08 15:29:52 +01:00
|
|
|
void Window::add_filter(FilterAndId&& filter)
|
|
|
|
{
|
|
|
|
for (auto it = m_filters.begin(); it != m_filters.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->first == filter.first)
|
|
|
|
throw filter_id_not_unique(filter.first);
|
|
|
|
}
|
|
|
|
m_filters.push_back(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::remove_filter(const std::string& id)
|
|
|
|
{
|
|
|
|
for (auto it = m_filters.begin(); it != m_filters.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->first == id)
|
|
|
|
{
|
|
|
|
m_filters.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-12 15:15:35 +01:00
|
|
|
CandidateList Window::complete_filterid(const std::string& prefix,
|
|
|
|
size_t cursor_pos)
|
|
|
|
{
|
|
|
|
std::string real_prefix = prefix.substr(0, cursor_pos);
|
|
|
|
CandidateList result;
|
|
|
|
for (auto& filter : m_filters)
|
|
|
|
{
|
|
|
|
if (filter.first.substr(0, real_prefix.length()) == real_prefix)
|
|
|
|
result.push_back(filter.first);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 21:14:39 +02:00
|
|
|
IncrementalInserter::IncrementalInserter(Window& window, Mode mode)
|
2011-09-19 23:56:29 +02:00
|
|
|
: m_window(window)
|
|
|
|
{
|
|
|
|
assert(not m_window.m_current_inserter);
|
|
|
|
m_window.m_current_inserter = this;
|
2011-09-27 20:45:22 +02:00
|
|
|
m_window.check_invariant();
|
2011-09-19 23:56:29 +02:00
|
|
|
|
2011-09-28 21:14:39 +02:00
|
|
|
m_window.m_buffer.begin_undo_group();
|
|
|
|
|
|
|
|
if (mode == Mode::Change)
|
|
|
|
window.erase_noundo();
|
|
|
|
|
2011-09-27 20:45:22 +02:00
|
|
|
for (auto& sel : m_window.m_selections)
|
2011-09-19 23:56:29 +02:00
|
|
|
{
|
2011-10-06 23:12:55 +02:00
|
|
|
BufferIterator pos;
|
|
|
|
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-02 21:03:41 +01:00
|
|
|
case Mode::AppendAtLineEnd:
|
|
|
|
pos = sel.end() - 1;
|
2011-10-06 23:12:55 +02:00
|
|
|
while (not pos.is_end() and *pos != '\n')
|
|
|
|
++pos;
|
2011-11-02 21:03:41 +01:00
|
|
|
if (mode == Mode::OpenLineBelow)
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
window.m_buffer.insert(pos, "\n");
|
|
|
|
}
|
2011-10-06 23:12:55 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Mode::OpenLineAbove:
|
2011-11-02 21:03:41 +01:00
|
|
|
case Mode::InsertAtLineBegin:
|
2011-10-06 23:12:55 +02:00
|
|
|
pos = sel.begin();
|
|
|
|
while (not pos.is_begin() and *pos != '\n')
|
|
|
|
--pos;
|
2011-11-02 21:03:41 +01:00
|
|
|
if (mode == Mode::OpenLineAbove)
|
|
|
|
window.m_buffer.insert(pos, "\n");
|
2011-10-06 23:12:55 +02:00
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-16 20:24:37 +01:00
|
|
|
sel = Selection(pos, pos, sel.captures());
|
2011-09-19 23:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IncrementalInserter::~IncrementalInserter()
|
|
|
|
{
|
2011-11-04 15:15:35 +01:00
|
|
|
move_cursor(DisplayCoord(0, -1));
|
2011-09-19 23:56:29 +02:00
|
|
|
assert(m_window.m_current_inserter == this);
|
|
|
|
m_window.m_current_inserter = nullptr;
|
|
|
|
|
|
|
|
m_window.m_buffer.end_undo_group();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementalInserter::insert(const Window::String& string)
|
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_window.insert_noundo(string);
|
2011-09-19 23:56:29 +02:00
|
|
|
}
|
|
|
|
|
2011-11-16 20:24:37 +01:00
|
|
|
void IncrementalInserter::insert_capture(size_t index)
|
|
|
|
{
|
|
|
|
for (auto& sel : m_window.m_selections)
|
|
|
|
m_window.m_buffer.insert(sel.begin(), sel.capture(index));
|
|
|
|
m_window.scroll_to_keep_cursor_visible_ifn();
|
|
|
|
}
|
|
|
|
|
2011-09-19 23:56:29 +02:00
|
|
|
void IncrementalInserter::erase()
|
|
|
|
{
|
2011-11-21 23:18:00 +01:00
|
|
|
for (auto& sel : m_window.m_selections)
|
|
|
|
sel = Selection(sel.first() - 1, sel.last() - 1);
|
2011-09-27 20:45:22 +02:00
|
|
|
m_window.erase_noundo();
|
2011-11-21 23:18:00 +01:00
|
|
|
m_window.scroll_to_keep_cursor_visible_ifn();
|
2011-09-19 23:56:29 +02:00
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
void IncrementalInserter::move_cursor(const DisplayCoord& offset)
|
2011-09-19 23:56:29 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
for (auto& sel : m_window.m_selections)
|
2011-09-19 23:56:29 +02:00
|
|
|
{
|
2011-10-14 16:29:53 +02:00
|
|
|
DisplayCoord pos = m_window.line_and_column_at(sel.last());
|
2011-09-27 20:45:22 +02:00
|
|
|
BufferIterator it = m_window.iterator_at(pos + offset);
|
|
|
|
sel = Selection(it, it);
|
2011-09-19 23:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|