2011-09-02 18:51:20 +02:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-19 23:56:29 +02:00
|
|
|
#include "assert.hh"
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::offset(int offset)
|
|
|
|
{
|
|
|
|
m_first += offset;
|
|
|
|
m_last += offset;
|
|
|
|
}
|
|
|
|
|
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),
|
2011-09-28 16:23:43 +02:00
|
|
|
m_select_mode(SelectMode::Normal),
|
2011-09-19 23:56:29 +02:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::check_invariant() const
|
|
|
|
{
|
|
|
|
assert(not m_selections.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowCoord Window::cursor_position() const
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
return line_and_column_at(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)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-17 16:57:35 +02:00
|
|
|
m_buffer.erase(sel.begin(), sel.end());
|
|
|
|
sel = Selection(sel.begin(), sel.begin());
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
static WindowCoord measure_string(const Window::String& string)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-05 21:06:31 +02:00
|
|
|
WindowCoord result(0, 0);
|
2011-09-02 18:51:20 +02:00
|
|
|
for (size_t i = 0; i < string.length(); ++i)
|
|
|
|
{
|
|
|
|
if (string[i] == '\n')
|
|
|
|
{
|
|
|
|
++result.line;
|
|
|
|
result.column = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++result.column;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-17 16:57:35 +02:00
|
|
|
m_buffer.insert(sel.begin(), string);
|
|
|
|
sel.offset(string.length());
|
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-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-09-05 21:06:31 +02:00
|
|
|
BufferCoord Window::window_to_buffer(const WindowCoord& window_pos) const
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
2011-09-05 21:06:31 +02:00
|
|
|
return BufferCoord(m_position.line + window_pos.line,
|
|
|
|
m_position.column + window_pos.column);
|
2011-09-05 20:54:17 +02:00
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
WindowCoord Window::buffer_to_window(const BufferCoord& buffer_pos) const
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
2011-09-05 21:06:31 +02:00
|
|
|
return WindowCoord(buffer_pos.line - m_position.line,
|
|
|
|
buffer_pos.column - m_position.column);
|
2011-09-05 20:54:17 +02:00
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
BufferIterator Window::iterator_at(const WindowCoord& window_pos) const
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
2011-09-08 02:13:19 +02:00
|
|
|
return m_buffer.iterator_at(window_to_buffer(window_pos));
|
2011-09-05 20:54:17 +02:00
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
WindowCoord Window::line_and_column_at(const BufferIterator& iterator) const
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
2011-09-08 02:13:19 +02:00
|
|
|
return buffer_to_window(m_buffer.line_and_column_at(iterator));
|
2011-09-05 20:54:17 +02:00
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
void Window::empty_selections()
|
|
|
|
{
|
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-09-28 16:23:43 +02:00
|
|
|
void Window::select(const Selector& selector)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
check_invariant();
|
|
|
|
|
2011-09-28 16:23:43 +02:00
|
|
|
if (m_select_mode == SelectMode::Normal)
|
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-09-23 11:17:19 +02:00
|
|
|
sel = Selection(sel.first(), selector(sel.last()).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-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-09-05 21:06:31 +02:00
|
|
|
void Window::move_cursor(const WindowCoord& offset)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-28 16:23:43 +02:00
|
|
|
if (m_select_mode == SelectMode::Normal)
|
|
|
|
move_cursor_to(cursor_position() + offset);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
{
|
|
|
|
WindowCoord pos = line_and_column_at(sel.last());
|
|
|
|
sel = Selection(sel.first(), iterator_at(pos + offset));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Window::move_cursor_to(const WindowCoord& new_pos)
|
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
BufferIterator target = iterator_at(new_pos);
|
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(Selection(target, target));
|
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();
|
|
|
|
|
|
|
|
SelectionList sorted_selections = m_selections;
|
2011-09-17 16:57:35 +02:00
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
std::sort(sorted_selections.begin(), sorted_selections.end(),
|
2011-09-17 16:57:35 +02:00
|
|
|
[](const Selection& lhs, const Selection& rhs) { return lhs.begin() < rhs.begin(); });
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2011-09-08 02:13:19 +02:00
|
|
|
BufferIterator current_position = m_buffer.iterator_at(m_position);
|
2011-09-02 18:51:20 +02:00
|
|
|
|
|
|
|
for (Selection& sel : sorted_selections)
|
|
|
|
{
|
2011-09-17 16:57:35 +02:00
|
|
|
if (current_position != sel.begin())
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
|
|
|
DisplayAtom atom;
|
2011-09-17 16:57:35 +02:00
|
|
|
atom.content = m_buffer.string(current_position, sel.begin());
|
2011-09-02 18:51:20 +02:00
|
|
|
m_display_buffer.append(atom);
|
|
|
|
}
|
2011-09-17 16:57:35 +02:00
|
|
|
if (sel.begin() != sel.end())
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
|
|
|
DisplayAtom atom;
|
2011-09-17 16:57:35 +02:00
|
|
|
atom.content = m_buffer.string(sel.begin(), sel.end());
|
2011-09-26 01:51:12 +02:00
|
|
|
atom.attribute = Underline;
|
2011-09-02 18:51:20 +02:00
|
|
|
m_display_buffer.append(atom);
|
|
|
|
}
|
2011-09-17 16:57:35 +02:00
|
|
|
current_position = sel.end();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
2011-09-08 02:13:19 +02:00
|
|
|
if (current_position != m_buffer.end())
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
|
|
|
DisplayAtom atom;
|
2011-09-08 02:13:19 +02:00
|
|
|
atom.content = m_buffer.string(current_position, m_buffer.end());
|
2011-09-02 18:51:20 +02:00
|
|
|
m_display_buffer.append(atom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
void Window::set_dimensions(const WindowCoord& 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();
|
|
|
|
|
|
|
|
WindowCoord cursor = line_and_column_at(m_selections.back().last());
|
|
|
|
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-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-09-28 21:14:39 +02:00
|
|
|
const BufferIterator& pos = mode == Mode::Append ? sel.end() : sel.begin();
|
2011-09-27 20:45:22 +02:00
|
|
|
sel = Selection(pos, pos);
|
2011-09-19 23:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IncrementalInserter::~IncrementalInserter()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementalInserter::erase()
|
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
move_cursor(WindowCoord(0, -1));
|
|
|
|
m_window.erase_noundo();
|
2011-09-19 23:56:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementalInserter::move_cursor(const WindowCoord& offset)
|
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
for (auto& sel : m_window.m_selections)
|
2011-09-19 23:56:29 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
WindowCoord pos = m_window.line_and_column_at(sel.last());
|
|
|
|
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
|
|
|
}
|