2011-09-02 18:51:20 +02:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-19 23:56:29 +02:00
|
|
|
#include "assert.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "context.hh"
|
2012-11-23 13:40:20 +01:00
|
|
|
#include "highlighter.hh"
|
2012-04-03 14:01:01 +02:00
|
|
|
#include "hook_manager.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
|
|
|
|
{
|
|
|
|
|
2013-03-01 19:15:05 +01:00
|
|
|
// Implementation in highlighters.cc
|
2013-03-19 14:04:24 +01:00
|
|
|
void highlight_selections(const Window& window, DisplayBuffer& display_buffer);
|
2013-05-23 13:39:00 +02:00
|
|
|
void expand_tabulations(const Window& window, DisplayBuffer& display_buffer);
|
|
|
|
void expand_unprintable(const Window& window, DisplayBuffer& display_buffer);
|
2013-03-01 19:15:05 +01:00
|
|
|
|
2011-09-08 02:13:19 +02:00
|
|
|
Window::Window(Buffer& buffer)
|
2012-01-31 20:12:06 +01:00
|
|
|
: Editor(buffer),
|
2012-11-22 13:50:29 +01:00
|
|
|
m_hooks(buffer.hooks()),
|
|
|
|
m_options(buffer.options())
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2013-01-17 13:44:14 +01:00
|
|
|
Context hook_context{*this};
|
|
|
|
m_hooks.run_hook("WinCreate", buffer.name(), hook_context);
|
2012-11-22 13:50:29 +01:00
|
|
|
m_options.register_watcher(*this);
|
2011-11-26 19:39:59 +01:00
|
|
|
|
2013-05-23 13:39:00 +02:00
|
|
|
m_builtin_highlighters.append({"tabulations", expand_tabulations});
|
2013-03-01 19:15:05 +01:00
|
|
|
m_builtin_highlighters.append({"unprintable", expand_unprintable});
|
2013-05-23 13:39:00 +02:00
|
|
|
m_builtin_highlighters.append({"selections", highlight_selections});
|
2012-06-14 15:19:38 +02:00
|
|
|
|
2012-11-22 13:50:29 +01:00
|
|
|
for (auto& option : m_options.flatten_options())
|
2013-03-03 17:25:40 +01:00
|
|
|
on_option_changed(*option);
|
2012-06-14 15:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
2012-11-22 13:50:29 +01:00
|
|
|
m_options.unregister_watcher(*this);
|
2011-09-27 20:45:22 +02:00
|
|
|
}
|
|
|
|
|
2013-04-11 21:01:00 +02:00
|
|
|
void Window::display_selection_at(LineCount line)
|
|
|
|
{
|
|
|
|
if (line >= 0 or line < m_dimensions.line)
|
|
|
|
{
|
2013-06-03 18:58:09 +02:00
|
|
|
auto cursor_line = main_selection().last().line;
|
2013-04-11 21:01:00 +02:00
|
|
|
m_position.line = std::max(0_line, cursor_line - line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-21 20:05:56 +02:00
|
|
|
void Window::center_selection()
|
|
|
|
{
|
2013-04-11 21:01:00 +02:00
|
|
|
display_selection_at(m_dimensions.line/2_line);
|
2012-08-21 20:05:56 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 01:31:21 +02:00
|
|
|
void Window::scroll(LineCount offset)
|
|
|
|
{
|
|
|
|
m_position.line = std::max(0_line, m_position.line + offset);
|
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
void Window::update_display_buffer()
|
2011-09-22 16:00:04 +02:00
|
|
|
{
|
2011-09-05 20:54:17 +02:00
|
|
|
scroll_to_keep_cursor_visible_ifn();
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
DisplayBuffer::LineList& lines = m_display_buffer.lines();
|
|
|
|
lines.clear();
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2012-08-22 23:33:52 +02:00
|
|
|
for (LineCount line = 0; line < m_dimensions.line; ++line)
|
2012-07-03 23:23:07 +02:00
|
|
|
{
|
2012-08-22 23:33:52 +02:00
|
|
|
LineCount buffer_line = m_position.line + line;
|
2012-07-03 23:23:07 +02:00
|
|
|
if (buffer_line >= buffer().line_count())
|
|
|
|
break;
|
2013-06-05 18:47:39 +02:00
|
|
|
const String& content = buffer()[buffer_line];
|
|
|
|
BufferCoord begin{buffer_line, content.byte_count_to(m_position.column)};
|
2013-06-21 19:50:45 +02:00
|
|
|
BufferCoord end = buffer().advance(buffer_line, content.byte_count_to(m_position.column + m_dimensions.column));
|
2012-07-12 23:19:10 +02:00
|
|
|
|
|
|
|
lines.push_back(DisplayLine(buffer_line));
|
2013-06-03 18:58:09 +02:00
|
|
|
lines.back().push_back(DisplayAtom(AtomContent(buffer(), begin, end)));
|
2012-07-03 23:23:07 +02:00
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2012-07-12 23:51:13 +02:00
|
|
|
m_display_buffer.compute_range();
|
2013-05-23 13:39:00 +02:00
|
|
|
m_highlighters(*this, m_display_buffer);
|
|
|
|
m_builtin_highlighters(*this, m_display_buffer);
|
2012-10-22 13:20:02 +02:00
|
|
|
m_display_buffer.optimize();
|
2012-11-05 19:54:09 +01:00
|
|
|
|
|
|
|
m_timestamp = buffer().timestamp();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2012-10-31 14:28:47 +01: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 16:29:53 +02:00
|
|
|
void Window::set_dimensions(const DisplayCoord& dimensions)
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
|
|
|
m_dimensions = dimensions;
|
|
|
|
}
|
|
|
|
|
2013-05-16 19:22:44 +02:00
|
|
|
static LineCount adapt_view_pos(LineCount line, LineCount offset,
|
|
|
|
LineCount view_pos, LineCount view_size,
|
|
|
|
LineCount buffer_size)
|
|
|
|
{
|
|
|
|
if (line - offset < view_pos)
|
|
|
|
return std::max(0_line, line - offset);
|
|
|
|
else if (line + offset >= view_pos + view_size)
|
|
|
|
return std::min(buffer_size - view_size,
|
|
|
|
line + offset - (view_size - 1));
|
|
|
|
return view_pos;
|
|
|
|
}
|
|
|
|
|
2011-09-05 20:55:31 +02:00
|
|
|
void Window::scroll_to_keep_cursor_visible_ifn()
|
|
|
|
{
|
2013-06-03 18:58:09 +02:00
|
|
|
const auto& first = main_selection().first();
|
|
|
|
const auto& last = main_selection().last();
|
2013-06-05 18:47:39 +02:00
|
|
|
const String& content = buffer()[last.line];
|
2012-07-15 01:48:50 +02:00
|
|
|
|
2013-05-16 19:22:44 +02:00
|
|
|
const LineCount offset = std::min<LineCount>(options()["scrolloff"].get<int>(),
|
|
|
|
(m_dimensions.line - 1) / 2);
|
2013-02-12 19:01:25 +01:00
|
|
|
|
2013-05-16 19:22:44 +02:00
|
|
|
// scroll lines if needed, try to get as much of the selection visible as possible
|
2013-06-03 18:58:09 +02:00
|
|
|
m_position.line = adapt_view_pos(first.line, offset, m_position.line,
|
2013-05-16 19:22:44 +02:00
|
|
|
m_dimensions.line, buffer().line_count());
|
2013-06-03 18:58:09 +02:00
|
|
|
m_position.line = adapt_view_pos(last.line, offset, m_position.line,
|
2013-05-16 19:22:44 +02:00
|
|
|
m_dimensions.line, buffer().line_count());
|
2012-07-15 01:48:50 +02:00
|
|
|
|
|
|
|
// highlight only the line containing the cursor
|
|
|
|
DisplayBuffer display_buffer;
|
|
|
|
DisplayBuffer::LineList& lines = display_buffer.lines();
|
2013-06-03 18:58:09 +02:00
|
|
|
lines.push_back(DisplayLine(last.line));
|
2012-07-15 01:48:50 +02:00
|
|
|
|
2013-06-03 18:58:09 +02:00
|
|
|
lines.back().push_back(DisplayAtom(AtomContent(buffer(), last.line, last.line+1)));
|
2012-07-15 01:48:50 +02:00
|
|
|
|
|
|
|
display_buffer.compute_range();
|
2013-05-23 13:39:00 +02:00
|
|
|
m_highlighters(*this, display_buffer);
|
|
|
|
m_builtin_highlighters(*this, display_buffer);
|
2012-07-15 01:48:50 +02: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-23 23:56:35 +02:00
|
|
|
CharCount column = 0;
|
2012-07-15 01:48:50 +02:00
|
|
|
for (auto& atom : lines.back())
|
|
|
|
{
|
|
|
|
if (atom.content.has_buffer_range() and
|
2013-06-03 18:58:09 +02:00
|
|
|
atom.content.begin() <= last and atom.content.end() > last)
|
2012-07-15 01:48:50 +02:00
|
|
|
{
|
2013-06-04 13:42:45 +02:00
|
|
|
column += atom.content.length();
|
2012-07-15 01:48:50 +02:00
|
|
|
|
2013-06-05 18:47:39 +02:00
|
|
|
auto first_col = (first.line == last.line) ? content.char_count_to(first.column) : 0_char;
|
|
|
|
auto last_col = content.char_count_to(last.column);
|
|
|
|
|
2013-02-12 19:01:25 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
if (last_col < m_position.column)
|
|
|
|
m_position.column = last_col;
|
2012-07-15 01:48:50 +02:00
|
|
|
else if (column >= m_position.column + m_dimensions.column)
|
|
|
|
m_position.column = column - (m_dimensions.column - 1);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2012-10-11 00:41:48 +02:00
|
|
|
column += atom.content.content().char_length();
|
2012-07-15 01:48:50 +02:00
|
|
|
}
|
2013-06-03 18:58:09 +02:00
|
|
|
if (not buffer().is_end(last))
|
2012-08-02 07:04:42 +02:00
|
|
|
{
|
|
|
|
// the cursor should always be visible.
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(false);
|
2012-08-02 07:04:42 +02:00
|
|
|
}
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
|
|
|
|
2013-05-23 19:21:34 +02:00
|
|
|
DisplayCoord Window::display_position(const BufferCoord& coord)
|
2012-09-30 16:22:03 +02:00
|
|
|
{
|
|
|
|
DisplayCoord res{0,0};
|
|
|
|
for (auto& line : m_display_buffer.lines())
|
|
|
|
{
|
2013-05-23 19:21:34 +02:00
|
|
|
if (line.buffer_line() == coord.line)
|
2012-09-30 16:22:03 +02:00
|
|
|
{
|
|
|
|
for (auto& atom : line)
|
|
|
|
{
|
|
|
|
auto& content = atom.content;
|
|
|
|
if (content.has_buffer_range() and
|
2013-05-23 19:21:34 +02:00
|
|
|
coord >= content.begin() and coord < content.end())
|
2012-09-30 16:22:03 +02:00
|
|
|
{
|
2013-05-23 19:21:34 +02:00
|
|
|
res.column += utf8::distance(buffer().iterator_at(content.begin()),
|
|
|
|
buffer().iterator_at(coord));
|
2012-09-30 16:22:03 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
res.column += content.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++res.line;
|
|
|
|
}
|
|
|
|
return { 0, 0 };
|
|
|
|
}
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
void Window::on_option_changed(const Option& option)
|
2012-06-14 15:19:38 +02:00
|
|
|
{
|
2013-03-03 17:25:40 +01:00
|
|
|
String desc = option.name() + "=" + option.get_as_string();
|
2013-01-17 13:44:14 +01:00
|
|
|
Context hook_context{*this};
|
|
|
|
m_hooks.run_hook("WinSetOption", desc, hook_context);
|
2012-06-14 15:19:38 +02:00
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|