2011-09-02 20:51:20 +04:00
|
|
|
#include "window.hh"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
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-05 22:54:17 +04:00
|
|
|
m_cursor(0, 0),
|
|
|
|
m_dimensions(0, 0)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::erase()
|
|
|
|
{
|
2011-09-08 04:13:19 +04:00
|
|
|
m_buffer.begin_undo_group();
|
2011-09-02 20:51:20 +04:00
|
|
|
if (m_selections.empty())
|
|
|
|
{
|
2011-09-05 22:54:17 +04:00
|
|
|
BufferIterator cursor = iterator_at(m_cursor);
|
2011-09-08 04:13:19 +04:00
|
|
|
m_buffer.erase(cursor, cursor+1);
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2011-09-17 18:57:35 +04:00
|
|
|
for (auto& sel : m_selections)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-09-19 00:09:25 +04:00
|
|
|
sel.canonicalize();
|
2011-09-17 18:57:35 +04:00
|
|
|
m_buffer.erase(sel.begin(), sel.end());
|
|
|
|
sel = Selection(sel.begin(), sel.begin());
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
2011-09-19 00:09:25 +04:00
|
|
|
if (not m_selections.empty())
|
|
|
|
m_cursor = line_and_column_at(m_selections.back().end());
|
2011-09-08 04:13:19 +04:00
|
|
|
m_buffer.end_undo_group();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
static WindowCoord measure_string(const Window::String& string)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-09-05 23:06:31 +04:00
|
|
|
WindowCoord result(0, 0);
|
2011-09-02 20:51:20 +04: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-08 04:13:19 +04:00
|
|
|
m_buffer.begin_undo_group();
|
2011-09-02 20:51:20 +04:00
|
|
|
if (m_selections.empty())
|
|
|
|
{
|
2011-09-08 04:13:19 +04:00
|
|
|
m_buffer.insert(iterator_at(m_cursor), string);
|
2011-09-02 20:51:20 +04:00
|
|
|
move_cursor(measure_string(string));
|
|
|
|
}
|
|
|
|
|
2011-09-17 18:57:35 +04:00
|
|
|
for (auto& sel : m_selections)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-09-17 18:57:35 +04:00
|
|
|
m_buffer.insert(sel.begin(), string);
|
|
|
|
sel.offset(string.length());
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
2011-09-08 04:13:19 +04:00
|
|
|
m_buffer.end_undo_group();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::append(const String& string)
|
|
|
|
{
|
2011-09-08 04:13:19 +04:00
|
|
|
m_buffer.begin_undo_group();
|
2011-09-02 20:51:20 +04:00
|
|
|
if (m_selections.empty())
|
|
|
|
{
|
2011-09-05 23:06:31 +04:00
|
|
|
move_cursor(WindowCoord(0 , 1));
|
2011-09-02 20:51:20 +04:00
|
|
|
insert(string);
|
|
|
|
}
|
|
|
|
|
2011-09-17 18:57:35 +04:00
|
|
|
for (auto& sel : m_selections)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-09-17 18:57:35 +04:00
|
|
|
m_buffer.insert(sel.end(), string);
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
2011-09-08 04:13:19 +04:00
|
|
|
m_buffer.end_undo_group();
|
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-09-05 23:06:31 +04:00
|
|
|
BufferCoord Window::window_to_buffer(const WindowCoord& window_pos) const
|
2011-09-05 22:54:17 +04:00
|
|
|
{
|
2011-09-05 23:06:31 +04:00
|
|
|
return BufferCoord(m_position.line + window_pos.line,
|
|
|
|
m_position.column + window_pos.column);
|
2011-09-05 22:54:17 +04:00
|
|
|
}
|
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
WindowCoord Window::buffer_to_window(const BufferCoord& buffer_pos) const
|
2011-09-05 22:54:17 +04:00
|
|
|
{
|
2011-09-05 23:06:31 +04:00
|
|
|
return WindowCoord(buffer_pos.line - m_position.line,
|
|
|
|
buffer_pos.column - m_position.column);
|
2011-09-05 22:54:17 +04:00
|
|
|
}
|
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
BufferIterator Window::iterator_at(const WindowCoord& window_pos) const
|
2011-09-05 22:54:17 +04:00
|
|
|
{
|
2011-09-08 04:13:19 +04:00
|
|
|
return m_buffer.iterator_at(window_to_buffer(window_pos));
|
2011-09-05 22:54:17 +04:00
|
|
|
}
|
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
WindowCoord Window::line_and_column_at(const BufferIterator& iterator) const
|
2011-09-05 22:54:17 +04:00
|
|
|
{
|
2011-09-08 04:13:19 +04:00
|
|
|
return buffer_to_window(m_buffer.line_and_column_at(iterator));
|
2011-09-05 22:54:17 +04:00
|
|
|
}
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
void Window::empty_selections()
|
|
|
|
{
|
|
|
|
m_selections.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::select(bool append, const Selector& selector)
|
|
|
|
{
|
|
|
|
if (not append or m_selections.empty())
|
|
|
|
{
|
|
|
|
empty_selections();
|
2011-09-05 22:54:17 +04:00
|
|
|
m_selections.push_back(selector(iterator_at(m_cursor)));
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-17 18:57:35 +04:00
|
|
|
for (auto& sel : m_selections)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-09-17 18:57:35 +04:00
|
|
|
sel = Selection(sel.begin(), selector(sel.end()).end());
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
}
|
2011-09-17 18:57:35 +04:00
|
|
|
m_cursor = line_and_column_at(m_selections.back().end());
|
2011-09-05 22:54:17 +04:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
void Window::move_cursor(const WindowCoord& offset)
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-09-17 18:28:23 +04:00
|
|
|
BufferCoord target_position = window_to_buffer(m_cursor + offset);
|
2011-09-05 22:54:17 +04:00
|
|
|
|
2011-09-08 04:13:19 +04:00
|
|
|
m_cursor = buffer_to_window(m_buffer.clamp(target_position));
|
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();
|
|
|
|
|
|
|
|
SelectionList sorted_selections = m_selections;
|
2011-09-17 18:57:35 +04:00
|
|
|
|
|
|
|
for (auto& sel : sorted_selections)
|
|
|
|
sel.canonicalize();
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
std::sort(sorted_selections.begin(), sorted_selections.end(),
|
2011-09-17 18:57:35 +04:00
|
|
|
[](const Selection& lhs, const Selection& rhs) { return lhs.begin() < rhs.begin(); });
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2011-09-08 04:13:19 +04:00
|
|
|
BufferIterator current_position = m_buffer.iterator_at(m_position);
|
2011-09-02 20:51:20 +04:00
|
|
|
|
|
|
|
for (Selection& sel : sorted_selections)
|
|
|
|
{
|
2011-09-17 18:57:35 +04:00
|
|
|
if (current_position != sel.begin())
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
|
|
|
DisplayAtom atom;
|
2011-09-17 18:57:35 +04:00
|
|
|
atom.content = m_buffer.string(current_position, sel.begin());
|
2011-09-02 20:51:20 +04:00
|
|
|
m_display_buffer.append(atom);
|
|
|
|
}
|
2011-09-17 18:57:35 +04:00
|
|
|
if (sel.begin() != sel.end())
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
|
|
|
DisplayAtom atom;
|
2011-09-17 18:57:35 +04:00
|
|
|
atom.content = m_buffer.string(sel.begin(), sel.end());
|
2011-09-02 20:51:20 +04:00
|
|
|
atom.attribute = UNDERLINE;
|
|
|
|
m_display_buffer.append(atom);
|
|
|
|
}
|
2011-09-17 18:57:35 +04:00
|
|
|
current_position = sel.end();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
2011-09-08 04:13:19 +04:00
|
|
|
if (current_position != m_buffer.end())
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
|
|
|
DisplayAtom atom;
|
2011-09-08 04:13:19 +04:00
|
|
|
atom.content = m_buffer.string(current_position, m_buffer.end());
|
2011-09-02 20:51:20 +04:00
|
|
|
m_display_buffer.append(atom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
void Window::set_dimensions(const WindowCoord& 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()
|
|
|
|
{
|
|
|
|
if (m_cursor.line < 0)
|
|
|
|
{
|
|
|
|
m_position.line = std::max(m_position.line + m_cursor.line, 0);
|
|
|
|
m_cursor.line = 0;
|
|
|
|
}
|
|
|
|
else if (m_cursor.line >= m_dimensions.line)
|
|
|
|
{
|
|
|
|
m_position.line += m_cursor.line - (m_dimensions.line - 1);
|
|
|
|
m_cursor.line = m_dimensions.line - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_cursor.column < 0)
|
|
|
|
{
|
|
|
|
m_position.column = std::max(m_position.column + m_cursor.column, 0);
|
|
|
|
m_cursor.column = 0;
|
|
|
|
}
|
|
|
|
else if (m_cursor.column >= m_dimensions.column)
|
|
|
|
{
|
|
|
|
m_position.column += m_cursor.column - (m_dimensions.column - 1);
|
|
|
|
m_cursor.column = m_dimensions.column - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|