2011-09-02 20:51:20 +04:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-20 01:56:29 +04:00
|
|
|
#include "assert.hh"
|
2012-11-23 16:40:20 +04:00
|
|
|
#include "highlighter.hh"
|
2012-04-03 16:01:01 +04:00
|
|
|
#include "hook_manager.hh"
|
2012-01-23 17:56:43 +04:00
|
|
|
#include "context.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-08 04:13:19 +04:00
|
|
|
Window::Window(Buffer& buffer)
|
2012-01-31 23:12:06 +04:00
|
|
|
: Editor(buffer),
|
2012-11-22 16:50:29 +04:00
|
|
|
m_hooks(buffer.hooks()),
|
|
|
|
m_options(buffer.options())
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2011-11-30 02:37:20 +04:00
|
|
|
HighlighterRegistry& registry = HighlighterRegistry::instance();
|
2011-11-26 22:39:59 +04:00
|
|
|
|
2013-01-17 16:44:14 +04:00
|
|
|
Context hook_context{*this};
|
|
|
|
m_hooks.run_hook("WinCreate", buffer.name(), hook_context);
|
2012-11-22 16:50:29 +04:00
|
|
|
m_options.register_watcher(*this);
|
2011-11-26 22:39:59 +04:00
|
|
|
|
2013-03-01 17:29:59 +04:00
|
|
|
m_builtin_highlighters.append(registry["expand_tabs"](*this, {}));
|
|
|
|
m_builtin_highlighters.append(registry["expand_unprintable"](*this, {}));
|
|
|
|
m_builtin_highlighters.append(registry["highlight_selections"](*this, {}));
|
2012-06-14 17:19:38 +04:00
|
|
|
|
2012-11-22 16:50:29 +04:00
|
|
|
for (auto& option : m_options.flatten_options())
|
2012-06-14 17:19:38 +04:00
|
|
|
on_option_changed(option.first, option.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
2012-11-22 16:50:29 +04:00
|
|
|
m_options.unregister_watcher(*this);
|
2011-09-27 22:45:22 +04:00
|
|
|
}
|
|
|
|
|
2012-08-21 22:05:56 +04:00
|
|
|
void Window::center_selection()
|
|
|
|
{
|
|
|
|
BufferIterator cursor = selections().back().last();
|
2012-08-23 01:33:52 +04:00
|
|
|
m_position.line = std::max(0_line, cursor.line() - m_dimensions.line/2_line);
|
2012-08-21 22:05:56 +04:00
|
|
|
}
|
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
void Window::update_display_buffer()
|
2011-09-22 18:00:04 +04:00
|
|
|
{
|
2011-09-05 22:54:17 +04:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
DisplayBuffer::LineList& lines = m_display_buffer.lines();
|
|
|
|
lines.clear();
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2012-08-23 01:33:52 +04:00
|
|
|
for (LineCount line = 0; line < m_dimensions.line; ++line)
|
2012-07-04 01:23:07 +04:00
|
|
|
{
|
2012-08-23 01:33:52 +04:00
|
|
|
LineCount buffer_line = m_position.line + line;
|
2012-07-04 01:23:07 +04:00
|
|
|
if (buffer_line >= buffer().line_count())
|
|
|
|
break;
|
2012-10-11 02:41:48 +04:00
|
|
|
BufferIterator line_begin = buffer().iterator_at_line_begin(buffer_line);
|
|
|
|
BufferIterator line_end = buffer().iterator_at_line_end(buffer_line);
|
2012-07-04 01:23:07 +04:00
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
BufferIterator begin = utf8::advance(line_begin, line_end, (int)m_position.column);
|
|
|
|
BufferIterator end = utf8::advance(begin, line_end, (int)m_dimensions.column);
|
2012-07-13 01:19:10 +04:00
|
|
|
|
|
|
|
lines.push_back(DisplayLine(buffer_line));
|
2012-10-11 02:41:48 +04:00
|
|
|
lines.back().push_back(DisplayAtom(AtomContent(begin, end)));
|
2012-07-04 01:23:07 +04:00
|
|
|
}
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2012-07-13 01:51:13 +04:00
|
|
|
m_display_buffer.compute_range();
|
2012-01-20 00:37:29 +04:00
|
|
|
m_highlighters(m_display_buffer);
|
2013-03-01 17:29:59 +04:00
|
|
|
m_builtin_highlighters(m_display_buffer);
|
2012-10-22 15:20:02 +04:00
|
|
|
m_display_buffer.optimize();
|
2012-11-05 22:54:09 +04:00
|
|
|
|
|
|
|
m_timestamp = buffer().timestamp();
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2012-10-31 17:28:47 +04:00
|
|
|
void Window::set_position(const DisplayCoord& position)
|
|
|
|
{
|
|
|
|
m_position.line = std::max(0_line, position.line);
|
|
|
|
m_position.column = std::max(0_char, position.column);
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2013-02-12 22:01:25 +04:00
|
|
|
const BufferIterator first = selections().back().first();
|
|
|
|
const BufferIterator last = selections().back().last();
|
2012-07-15 03:48:50 +04:00
|
|
|
|
|
|
|
// scroll lines if needed
|
2013-02-12 22:01:25 +04:00
|
|
|
if (first.line() < m_position.line)
|
|
|
|
m_position.line = first.line();
|
|
|
|
else if (first.line() >= m_position.line + m_dimensions.line)
|
|
|
|
m_position.line = first.line() - (m_dimensions.line - 1);
|
|
|
|
|
|
|
|
if (last.line() < m_position.line)
|
|
|
|
m_position.line = last.line();
|
|
|
|
else if (last.line() >= m_position.line + m_dimensions.line)
|
|
|
|
m_position.line = last.line() - (m_dimensions.line - 1);
|
2012-07-15 03:48:50 +04:00
|
|
|
|
|
|
|
// highlight only the line containing the cursor
|
|
|
|
DisplayBuffer display_buffer;
|
|
|
|
DisplayBuffer::LineList& lines = display_buffer.lines();
|
2013-02-12 22:01:25 +04:00
|
|
|
lines.push_back(DisplayLine(last.line()));
|
2012-07-15 03:48:50 +04:00
|
|
|
|
2013-02-12 22:01:25 +04:00
|
|
|
BufferIterator line_begin = buffer().iterator_at_line_begin(last);
|
|
|
|
BufferIterator line_end = buffer().iterator_at_line_end(last);
|
2012-07-15 03:48:50 +04:00
|
|
|
lines.back().push_back(DisplayAtom(AtomContent(line_begin, line_end)));
|
|
|
|
|
|
|
|
display_buffer.compute_range();
|
|
|
|
m_highlighters(display_buffer);
|
2013-03-01 17:29:59 +04:00
|
|
|
m_builtin_highlighters(display_buffer);
|
2012-07-15 03:48:50 +04:00
|
|
|
|
|
|
|
// now we can compute where the cursor is in display columns
|
|
|
|
// (this is only valid if highlighting one line and multiple lines put
|
|
|
|
// the cursor in the same position, however I do not find any sane example
|
|
|
|
// of highlighters not doing that)
|
2012-08-24 01:56:35 +04:00
|
|
|
CharCount column = 0;
|
2012-07-15 03:48:50 +04:00
|
|
|
for (auto& atom : lines.back())
|
|
|
|
{
|
|
|
|
if (atom.content.has_buffer_range() and
|
2013-02-12 22:01:25 +04:00
|
|
|
atom.content.begin() <= last and atom.content.end() > last)
|
2012-07-15 03:48:50 +04:00
|
|
|
{
|
|
|
|
if (atom.content.type() == AtomContent::BufferRange)
|
2013-02-12 22:01:25 +04:00
|
|
|
column += utf8::distance(atom.content.begin(), last);
|
2012-07-15 03:48:50 +04:00
|
|
|
else
|
2012-10-11 02:41:48 +04:00
|
|
|
column += atom.content.content().char_length();
|
2012-07-15 03:48:50 +04:00
|
|
|
|
2013-02-12 22:01:25 +04:00
|
|
|
CharCount first_col = first.line() == last.line() ?
|
|
|
|
utf8::distance(line_begin, first) : 0_char;
|
|
|
|
if (first_col < m_position.column)
|
|
|
|
m_position.column = first_col;
|
|
|
|
else if (column >= m_position.column + m_dimensions.column)
|
|
|
|
m_position.column = column - (m_dimensions.column - 1);
|
|
|
|
|
|
|
|
CharCount last_col = utf8::distance(line_begin, last);
|
|
|
|
if (last_col < m_position.column)
|
|
|
|
m_position.column = last_col;
|
2012-07-15 03:48:50 +04:00
|
|
|
else if (column >= m_position.column + m_dimensions.column)
|
|
|
|
m_position.column = column - (m_dimensions.column - 1);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2012-10-11 02:41:48 +04:00
|
|
|
column += atom.content.content().char_length();
|
2012-07-15 03:48:50 +04:00
|
|
|
}
|
2013-02-12 22:01:25 +04:00
|
|
|
if (last != buffer().end())
|
2012-08-02 09:04:42 +04:00
|
|
|
{
|
|
|
|
// the cursor should always be visible.
|
|
|
|
assert(false);
|
|
|
|
}
|
2011-09-05 22:55:31 +04:00
|
|
|
}
|
|
|
|
|
2012-09-30 18:22:03 +04:00
|
|
|
DisplayCoord Window::display_position(const BufferIterator& iterator)
|
|
|
|
{
|
|
|
|
DisplayCoord res{0,0};
|
|
|
|
for (auto& line : m_display_buffer.lines())
|
|
|
|
{
|
|
|
|
if (line.buffer_line() == iterator.line())
|
|
|
|
{
|
|
|
|
for (auto& atom : line)
|
|
|
|
{
|
|
|
|
auto& content = atom.content;
|
|
|
|
if (content.has_buffer_range() and
|
|
|
|
iterator >= content.begin() and iterator < content.end())
|
|
|
|
{
|
2013-01-17 21:50:04 +04:00
|
|
|
res.column += utf8::distance(content.begin(), iterator);
|
2012-09-30 18:22:03 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
res.column += content.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++res.line;
|
|
|
|
}
|
|
|
|
return { 0, 0 };
|
|
|
|
}
|
|
|
|
|
2012-06-14 17:19:38 +04:00
|
|
|
void Window::on_option_changed(const String& name, const Option& option)
|
|
|
|
{
|
|
|
|
String desc = name + "=" + option.as_string();
|
2013-01-17 16:44:14 +04:00
|
|
|
Context hook_context{*this};
|
|
|
|
m_hooks.run_hook("WinSetOption", desc, hook_context);
|
2012-06-14 17:19:38 +04:00
|
|
|
}
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|