2011-09-02 20:51:20 +04:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-20 01:56:29 +04:00
|
|
|
#include "assert.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "context.hh"
|
2012-11-23 16:40:20 +04:00
|
|
|
#include "highlighter.hh"
|
2012-04-03 16:01:01 +04:00
|
|
|
#include "hook_manager.hh"
|
2013-11-14 22:09:15 +04:00
|
|
|
#include "client.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
|
|
|
|
{
|
|
|
|
|
2013-03-01 22:15:05 +04:00
|
|
|
// Implementation in highlighters.cc
|
2013-03-19 17:04:24 +04:00
|
|
|
void highlight_selections(const Window& window, DisplayBuffer& display_buffer);
|
2013-05-23 15:39:00 +04:00
|
|
|
void expand_tabulations(const Window& window, DisplayBuffer& display_buffer);
|
|
|
|
void expand_unprintable(const Window& window, DisplayBuffer& display_buffer);
|
2013-03-01 22:15:05 +04:00
|
|
|
|
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()),
|
2013-10-25 03:01:17 +04:00
|
|
|
m_options(buffer.options()),
|
|
|
|
m_keymaps(buffer.keymaps())
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2013-11-14 22:09:15 +04:00
|
|
|
InputHandler hook_handler{*this};
|
|
|
|
m_hooks.run_hook("WinCreate", buffer.name(), hook_handler.context());
|
2012-11-22 16:50:29 +04:00
|
|
|
m_options.register_watcher(*this);
|
2011-11-26 22:39:59 +04:00
|
|
|
|
2013-05-23 15:39:00 +04:00
|
|
|
m_builtin_highlighters.append({"tabulations", expand_tabulations});
|
2013-03-01 22:15:05 +04:00
|
|
|
m_builtin_highlighters.append({"unprintable", expand_unprintable});
|
2013-05-23 15:39:00 +04:00
|
|
|
m_builtin_highlighters.append({"selections", highlight_selections});
|
2012-06-14 17:19:38 +04:00
|
|
|
|
2012-11-22 16:50:29 +04:00
|
|
|
for (auto& option : m_options.flatten_options())
|
2013-03-03 20:25:40 +04:00
|
|
|
on_option_changed(*option);
|
2012-06-14 17:19:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
2013-11-14 22:09:15 +04:00
|
|
|
InputHandler hook_handler{*this};
|
|
|
|
m_hooks.run_hook("WinClose", buffer().name(), hook_handler.context());
|
2012-11-22 16:50:29 +04:00
|
|
|
m_options.unregister_watcher(*this);
|
2011-09-27 22:45:22 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 23:01:00 +04:00
|
|
|
void Window::display_selection_at(LineCount line)
|
|
|
|
{
|
|
|
|
if (line >= 0 or line < m_dimensions.line)
|
|
|
|
{
|
2013-12-14 18:17:02 +04:00
|
|
|
auto cursor_line = selections().main().last().line;
|
2013-04-11 23:01:00 +04:00
|
|
|
m_position.line = std::max(0_line, cursor_line - line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-21 22:05:56 +04:00
|
|
|
void Window::center_selection()
|
|
|
|
{
|
2013-04-11 23:01:00 +04:00
|
|
|
display_selection_at(m_dimensions.line/2_line);
|
2012-08-21 22:05:56 +04:00
|
|
|
}
|
|
|
|
|
2013-04-12 03:31:21 +04:00
|
|
|
void Window::scroll(LineCount offset)
|
|
|
|
{
|
|
|
|
m_position.line = std::max(0_line, m_position.line + offset);
|
|
|
|
}
|
|
|
|
|
2013-07-24 03:38:30 +04:00
|
|
|
void Window::scroll(CharCount offset)
|
|
|
|
{
|
|
|
|
m_position.column = std::max(0_char, m_position.column + offset);
|
|
|
|
}
|
|
|
|
|
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;
|
2013-07-24 16:55:57 +04:00
|
|
|
lines.emplace_back(AtomList{ {buffer(), buffer_line, buffer_line+1} });
|
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();
|
2013-05-23 15:39:00 +04:00
|
|
|
m_highlighters(*this, m_display_buffer);
|
|
|
|
m_builtin_highlighters(*this, m_display_buffer);
|
2013-06-28 02:03:11 +04:00
|
|
|
|
|
|
|
// cut the start of the line before m_position.column
|
|
|
|
for (auto& line : lines)
|
|
|
|
line.trim(m_position.column, m_dimensions.column);
|
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
|
|
|
}
|
|
|
|
|
2013-07-26 02:44:00 +04:00
|
|
|
void Window::set_position(DisplayCoord position)
|
2012-10-31 17:28:47 +04:00
|
|
|
{
|
|
|
|
m_position.line = std::max(0_line, position.line);
|
|
|
|
m_position.column = std::max(0_char, position.column);
|
|
|
|
}
|
|
|
|
|
2013-07-26 02:44:00 +04:00
|
|
|
void Window::set_dimensions(DisplayCoord dimensions)
|
2011-09-05 22:54:17 +04:00
|
|
|
{
|
|
|
|
m_dimensions = dimensions;
|
|
|
|
}
|
|
|
|
|
2013-05-16 21:22:44 +04: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;
|
|
|
|
}
|
|
|
|
|
2013-09-27 22:14:39 +04:00
|
|
|
static CharCount adapt_view_pos(const DisplayBuffer& display_buffer,
|
|
|
|
BufferCoord pos, CharCount view_pos, CharCount view_size)
|
2013-07-24 03:34:00 +04:00
|
|
|
{
|
|
|
|
CharCount buffer_column = 0;
|
|
|
|
CharCount non_buffer_column = 0;
|
2013-09-27 22:14:39 +04:00
|
|
|
for (auto& line : display_buffer.lines())
|
2013-07-24 03:34:00 +04:00
|
|
|
{
|
2013-09-27 22:14:39 +04:00
|
|
|
for (auto& atom : line)
|
2013-07-24 03:34:00 +04:00
|
|
|
{
|
2013-09-27 22:14:39 +04:00
|
|
|
if (atom.has_buffer_range())
|
2013-07-24 03:34:00 +04:00
|
|
|
{
|
2013-09-27 22:14:39 +04:00
|
|
|
if (atom.begin() <= pos and atom.end() > pos)
|
|
|
|
{
|
|
|
|
if (buffer_column < view_pos)
|
|
|
|
return buffer_column;
|
|
|
|
|
|
|
|
auto last_column = buffer_column + atom.length();
|
|
|
|
if (last_column >= view_pos + view_size - non_buffer_column)
|
|
|
|
return last_column - view_size + non_buffer_column;
|
|
|
|
}
|
|
|
|
buffer_column += atom.length();
|
2013-07-24 03:34:00 +04:00
|
|
|
}
|
2013-09-27 22:14:39 +04:00
|
|
|
else
|
|
|
|
non_buffer_column += atom.length();
|
2013-07-24 03:34:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return view_pos;
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:55:31 +04:00
|
|
|
void Window::scroll_to_keep_cursor_visible_ifn()
|
|
|
|
{
|
2013-12-14 18:17:02 +04:00
|
|
|
const auto& first = selections().main().first();
|
|
|
|
const auto& last = selections().main().last();
|
2012-07-15 03:48:50 +04:00
|
|
|
|
2013-05-16 21:22:44 +04:00
|
|
|
const LineCount offset = std::min<LineCount>(options()["scrolloff"].get<int>(),
|
|
|
|
(m_dimensions.line - 1) / 2);
|
2013-02-12 22:01:25 +04:00
|
|
|
|
2013-05-16 21:22:44 +04:00
|
|
|
// scroll lines if needed, try to get as much of the selection visible as possible
|
2013-06-03 20:58:09 +04:00
|
|
|
m_position.line = adapt_view_pos(first.line, offset, m_position.line,
|
2013-05-16 21:22:44 +04:00
|
|
|
m_dimensions.line, buffer().line_count());
|
2013-06-03 20:58:09 +04:00
|
|
|
m_position.line = adapt_view_pos(last.line, offset, m_position.line,
|
2013-05-16 21:22:44 +04:00
|
|
|
m_dimensions.line, buffer().line_count());
|
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-07-24 16:55:57 +04:00
|
|
|
lines.emplace_back(AtomList{ {buffer(), last.line, last.line+1} });
|
2012-07-15 03:48:50 +04:00
|
|
|
|
|
|
|
display_buffer.compute_range();
|
2013-05-23 15:39:00 +04:00
|
|
|
m_highlighters(*this, display_buffer);
|
|
|
|
m_builtin_highlighters(*this, 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)
|
2013-09-27 22:14:39 +04:00
|
|
|
m_position.column = adapt_view_pos(display_buffer,
|
2013-07-24 03:34:00 +04:00
|
|
|
first.line == last.line ? first : last.line,
|
2013-09-27 22:14:39 +04:00
|
|
|
m_position.column, m_dimensions.column);
|
|
|
|
m_position.column = adapt_view_pos(display_buffer, last,
|
|
|
|
m_position.column, m_dimensions.column);
|
2011-09-05 22:55:31 +04:00
|
|
|
}
|
|
|
|
|
2013-07-17 23:17:32 +04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
CharCount find_display_column(const DisplayLine& line, const Buffer& buffer,
|
2013-07-26 02:44:00 +04:00
|
|
|
BufferCoord coord)
|
2013-07-17 23:17:32 +04:00
|
|
|
{
|
|
|
|
CharCount column = 0;
|
|
|
|
for (auto& atom : line)
|
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
if (atom.has_buffer_range() and
|
|
|
|
coord >= atom.begin() and coord < atom.end())
|
2013-07-17 23:17:32 +04:00
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
if (atom.type() == DisplayAtom::BufferRange)
|
|
|
|
column += utf8::distance(buffer.iterator_at(atom.begin()),
|
2013-07-17 23:17:32 +04:00
|
|
|
buffer.iterator_at(coord));
|
|
|
|
return column;
|
|
|
|
}
|
2013-07-24 16:55:57 +04:00
|
|
|
column += atom.length();
|
2013-07-17 23:17:32 +04:00
|
|
|
}
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferCoord find_buffer_coord(const DisplayLine& line, const Buffer& buffer,
|
|
|
|
CharCount column)
|
|
|
|
{
|
2013-07-23 22:11:26 +04:00
|
|
|
auto& range = line.range();
|
2013-07-17 23:17:32 +04:00
|
|
|
for (auto& atom : line)
|
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
CharCount len = atom.length();
|
|
|
|
if (atom.has_buffer_range() and column < len)
|
2013-07-17 23:17:32 +04:00
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
if (atom.type() == DisplayAtom::BufferRange)
|
|
|
|
return utf8::advance(buffer.iterator_at(atom.begin()), buffer.iterator_at(range.second),
|
2013-07-17 23:17:32 +04:00
|
|
|
std::max(0_char, column)).coord();
|
2013-07-24 16:55:57 +04:00
|
|
|
return atom.begin();
|
2013-07-17 23:17:32 +04:00
|
|
|
}
|
|
|
|
column -= len;
|
|
|
|
}
|
2013-07-23 22:11:26 +04:00
|
|
|
return buffer.clamp(buffer.prev(range.second));
|
2013-07-17 23:17:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 02:44:00 +04:00
|
|
|
DisplayCoord Window::display_position(BufferCoord coord)
|
2012-09-30 18:22:03 +04:00
|
|
|
{
|
2013-07-17 23:17:32 +04:00
|
|
|
LineCount l = 0;
|
2012-09-30 18:22:03 +04:00
|
|
|
for (auto& line : m_display_buffer.lines())
|
|
|
|
{
|
2013-07-23 22:11:26 +04:00
|
|
|
auto& range = line.range();
|
|
|
|
if (range.first <= coord and coord < range.second)
|
2013-07-17 23:17:32 +04:00
|
|
|
return {l, find_display_column(line, buffer(), coord)};
|
|
|
|
++l;
|
2012-09-30 18:22:03 +04:00
|
|
|
}
|
|
|
|
return { 0, 0 };
|
|
|
|
}
|
|
|
|
|
2013-12-15 18:14:52 +04:00
|
|
|
BufferCoord Window::offset_coord(BufferCoord coord, CharCount offset)
|
|
|
|
{
|
|
|
|
return buffer().offset_coord(coord, offset);
|
|
|
|
}
|
|
|
|
|
2013-07-26 02:44:00 +04:00
|
|
|
BufferCoord Window::offset_coord(BufferCoord coord, LineCount offset)
|
2013-07-17 23:17:32 +04:00
|
|
|
{
|
|
|
|
auto line = clamp(coord.line + offset, 0_line, buffer().line_count()-1);
|
|
|
|
DisplayBuffer display_buffer;
|
|
|
|
DisplayBuffer::LineList& lines = display_buffer.lines();
|
2013-07-24 16:55:57 +04:00
|
|
|
lines.emplace_back(AtomList{ {buffer(), coord.line, coord.line+1} });
|
|
|
|
lines.emplace_back(AtomList{ {buffer(), line, line+1} });
|
2013-07-17 23:17:32 +04:00
|
|
|
display_buffer.compute_range();
|
|
|
|
m_highlighters(*this, display_buffer);
|
|
|
|
m_builtin_highlighters(*this, display_buffer);
|
|
|
|
|
|
|
|
CharCount column = find_display_column(lines[0], buffer(), coord);
|
|
|
|
return find_buffer_coord(lines[1], buffer(), column);
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
void Window::on_option_changed(const Option& option)
|
2012-06-14 17:19:38 +04:00
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
String desc = option.name() + "=" + option.get_as_string();
|
2013-11-14 22:09:15 +04:00
|
|
|
InputHandler hook_handler{*this};
|
|
|
|
m_hooks.run_hook("WinSetOption", desc, hook_handler.context());
|
2013-11-19 02:43:37 +04:00
|
|
|
|
|
|
|
// an highlighter might depend on the option, so we need to redraw
|
|
|
|
forget_timestamp();
|
2012-06-14 17:19:38 +04:00
|
|
|
}
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|