2011-09-02 18:51:20 +02:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-19 23:56:29 +02:00
|
|
|
#include "assert.hh"
|
2016-07-20 09:49:04 +02:00
|
|
|
#include "clock.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"
|
2015-06-21 20:56:23 +02:00
|
|
|
#include "input_handler.hh"
|
2016-02-27 18:23:13 +01:00
|
|
|
#include "client.hh"
|
2016-05-06 01:24:54 +02:00
|
|
|
#include "buffer_utils.hh"
|
2017-03-16 10:57:39 +01:00
|
|
|
#include "option.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
|
2017-05-08 12:29:23 +02:00
|
|
|
void setup_builtin_highlighters(HighlighterGroup& group);
|
2013-03-01 19:15:05 +01:00
|
|
|
|
2011-09-08 02:13:19 +02:00
|
|
|
Window::Window(Buffer& buffer)
|
2014-10-30 15:00:42 +01:00
|
|
|
: Scope(buffer),
|
2017-05-03 20:41:37 +02:00
|
|
|
m_buffer(&buffer),
|
2017-10-28 05:00:51 +02:00
|
|
|
m_builtin_highlighters{highlighters()}
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2014-12-19 00:12:58 +01:00
|
|
|
run_hook_in_own_context("WinCreate", buffer.name());
|
|
|
|
|
2014-10-30 15:00:42 +01:00
|
|
|
options().register_watcher(*this);
|
2011-11-26 19:39:59 +01:00
|
|
|
|
2017-10-31 06:53:08 +01:00
|
|
|
setup_builtin_highlighters(m_builtin_highlighters.group());
|
2012-06-14 15:19:38 +02:00
|
|
|
|
2014-10-30 15:00:42 +01:00
|
|
|
for (auto& option : 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()
|
|
|
|
{
|
2014-12-19 00:12:58 +01:00
|
|
|
run_hook_in_own_context("WinClose", buffer().name());
|
2014-10-30 15:00:42 +01:00
|
|
|
options().unregister_watcher(*this);
|
2011-09-27 20:45:22 +02:00
|
|
|
}
|
|
|
|
|
2015-11-02 20:49:58 +01:00
|
|
|
void Window::scroll(LineCount offset)
|
|
|
|
{
|
|
|
|
m_position.line = std::max(0_line, m_position.line + offset);
|
|
|
|
}
|
|
|
|
|
2013-12-16 15:01:40 +01:00
|
|
|
void Window::display_line_at(LineCount buffer_line, LineCount display_line)
|
2013-04-11 21:01:00 +02:00
|
|
|
{
|
2013-12-16 15:01:40 +01:00
|
|
|
if (display_line >= 0 or display_line < m_dimensions.line)
|
|
|
|
m_position.line = std::max(0_line, buffer_line - display_line);
|
2013-04-11 21:01:00 +02:00
|
|
|
}
|
|
|
|
|
2013-12-16 15:01:40 +01:00
|
|
|
void Window::center_line(LineCount buffer_line)
|
2012-08-21 20:05:56 +02:00
|
|
|
{
|
2013-12-16 15:01:40 +01:00
|
|
|
display_line_at(buffer_line, m_dimensions.line/2_line);
|
2012-08-21 20:05:56 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
void Window::scroll(ColumnCount offset)
|
2013-04-12 01:31:21 +02:00
|
|
|
{
|
2016-09-22 21:36:26 +02:00
|
|
|
m_position.column = std::max(0_col, m_position.column + offset);
|
2013-04-12 01:31:21 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
void Window::display_column_at(ColumnCount buffer_column, ColumnCount display_column)
|
2013-07-24 01:38:30 +02:00
|
|
|
{
|
2015-11-02 20:49:58 +01:00
|
|
|
if (display_column >= 0 or display_column < m_dimensions.column)
|
2016-09-22 21:36:26 +02:00
|
|
|
m_position.column = std::max(0_col, buffer_column - display_column);
|
2015-11-02 20:49:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
void Window::center_column(ColumnCount buffer_column)
|
2015-11-02 20:49:58 +01:00
|
|
|
{
|
2016-09-22 21:36:26 +02:00
|
|
|
display_column_at(buffer_column, m_dimensions.column/2_col);
|
2013-07-24 01:38:30 +02:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:51:56 +01:00
|
|
|
Window::Setup Window::build_setup(const Context& context) const
|
2011-09-22 16:00:04 +02:00
|
|
|
{
|
2016-11-28 14:59:55 +01:00
|
|
|
Vector<BufferRange, MemoryDomain::Display> selections;
|
2016-02-03 10:51:56 +01:00
|
|
|
for (auto& sel : context.selections())
|
|
|
|
selections.push_back({sel.cursor(), sel.anchor()});
|
2015-06-21 20:56:23 +02:00
|
|
|
|
2016-05-13 21:32:53 +02:00
|
|
|
return { m_position, m_dimensions,
|
2016-02-03 10:51:56 +01:00
|
|
|
context.buffer().timestamp(),
|
|
|
|
context.selections().main_index(),
|
|
|
|
std::move(selections) };
|
2015-06-21 20:56:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::needs_redraw(const Context& context) const
|
|
|
|
{
|
2016-02-03 10:51:56 +01:00
|
|
|
auto& selections = context.selections();
|
|
|
|
|
|
|
|
if (m_position != m_last_setup.position or
|
2016-05-13 21:32:53 +02:00
|
|
|
m_dimensions != m_last_setup.dimensions or
|
2016-02-03 10:51:56 +01:00
|
|
|
context.buffer().timestamp() != m_last_setup.timestamp or
|
|
|
|
selections.main_index() != m_last_setup.main_selection or
|
|
|
|
selections.size() != m_last_setup.selections.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (int i = 0; i < selections.size(); ++i)
|
|
|
|
{
|
|
|
|
if (selections[i].cursor() != m_last_setup.selections[i].begin or
|
|
|
|
selections[i].anchor() != m_last_setup.selections[i].end)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-06-21 20:56:23 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 14:34:22 +02:00
|
|
|
const DisplayBuffer& Window::update_display_buffer(const Context& context)
|
2015-06-21 20:56:23 +02:00
|
|
|
{
|
2016-05-06 01:24:54 +02:00
|
|
|
const bool profile = context.options()["debug"].get<DebugFlags>() &
|
|
|
|
DebugFlags::Profile;
|
|
|
|
|
2016-07-20 09:49:04 +02:00
|
|
|
auto start_time = profile ? Clock::now() : Clock::time_point{};
|
2016-05-06 01:24:54 +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
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
if (m_dimensions == DisplayCoord{0,0})
|
2015-06-22 14:34:22 +02:00
|
|
|
return m_display_buffer;
|
2015-06-21 20:56:23 +02:00
|
|
|
|
|
|
|
kak_assert(&buffer() == &context.buffer());
|
2017-06-09 17:00:22 +02:00
|
|
|
const DisplaySetup setup = compute_display_setup(context);
|
|
|
|
|
|
|
|
m_position = setup.window_pos;
|
|
|
|
m_range = setup.window_range;
|
2015-06-21 20:56:23 +02:00
|
|
|
|
2017-06-09 14:22:32 +02:00
|
|
|
const int tabstop = context.options()["tabstop"].get<int>();
|
2017-05-07 14:43:43 +02:00
|
|
|
for (LineCount line = 0; line < m_range.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;
|
2017-06-09 14:22:32 +02:00
|
|
|
auto beg_byte = get_byte_to_column(buffer(), tabstop, {buffer_line, m_position.column});
|
2017-06-15 18:25:37 +02:00
|
|
|
auto end_byte = setup.full_lines ?
|
|
|
|
buffer()[buffer_line].length()
|
|
|
|
: get_byte_to_column(buffer(), tabstop, {buffer_line, m_position.column + m_range.column});
|
2017-06-09 17:00:22 +02:00
|
|
|
|
2017-10-12 08:38:19 +02:00
|
|
|
// The display buffer always has at least one buffer atom, which might be empty if
|
|
|
|
// beg_byte == end_byte
|
2017-06-15 18:25:37 +02:00
|
|
|
lines.emplace_back(AtomList{ {buffer(), {buffer_line, beg_byte}, {buffer_line, end_byte}} });
|
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();
|
2014-12-02 20:56:17 +01:00
|
|
|
BufferRange range{{0,0}, buffer().end_coord()};
|
2017-04-27 20:27:28 +02:00
|
|
|
for (auto pass : { HighlightPass::Wrap, HighlightPass::Move, HighlightPass::Colorize })
|
|
|
|
m_builtin_highlighters.highlight(context, pass, m_display_buffer, range);
|
2013-06-28 00:03:11 +02:00
|
|
|
|
2012-10-22 13:20:02 +02:00
|
|
|
m_display_buffer.optimize();
|
2012-11-05 19:54:09 +01:00
|
|
|
|
2016-02-03 10:51:56 +01:00
|
|
|
m_last_setup = build_setup(context);
|
2015-06-22 14:34:22 +02:00
|
|
|
|
2016-05-06 01:24:54 +02:00
|
|
|
if (profile and not (buffer().flags() & Buffer::Flags::Debug))
|
|
|
|
{
|
2016-07-20 09:49:04 +02:00
|
|
|
using namespace std::chrono;
|
2017-06-07 21:06:47 +02:00
|
|
|
auto duration = duration_cast<microseconds>(Clock::now() - start_time);
|
|
|
|
write_to_debug_buffer(format("window display update for '{}' took {} us",
|
2016-05-06 01:24:54 +02:00
|
|
|
buffer().display_name(), (size_t)duration.count()));
|
|
|
|
}
|
|
|
|
|
2015-06-22 14:34:22 +02:00
|
|
|
return m_display_buffer;
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
void Window::set_position(DisplayCoord position)
|
2012-10-31 14:28:47 +01:00
|
|
|
{
|
|
|
|
m_position.line = std::max(0_line, position.line);
|
2016-09-22 21:36:26 +02:00
|
|
|
m_position.column = std::max(0_col, position.column);
|
2012-10-31 14:28:47 +01:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
void Window::set_dimensions(DisplayCoord dimensions)
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
2016-05-05 00:26:15 +02:00
|
|
|
if (m_dimensions != dimensions)
|
|
|
|
{
|
|
|
|
m_dimensions = dimensions;
|
|
|
|
run_hook_in_own_context("WinResize", format("{}.{}", dimensions.line,
|
|
|
|
dimensions.column));
|
|
|
|
}
|
2011-09-05 20:54:17 +02:00
|
|
|
}
|
|
|
|
|
2017-06-09 17:00:22 +02:00
|
|
|
DisplaySetup Window::compute_display_setup(const Context& context)
|
2013-07-24 01:34:00 +02:00
|
|
|
{
|
2017-04-27 20:27:28 +02:00
|
|
|
DisplayCoord offset = options()["scrolloff"].get<DisplayCoord>();
|
|
|
|
offset.line = std::min(offset.line, (m_dimensions.line + 1) / 2);
|
|
|
|
offset.column = std::min(offset.column, (m_dimensions.column + 1) / 2);
|
2013-07-24 01:34:00 +02:00
|
|
|
|
2017-05-07 14:43:43 +02:00
|
|
|
const int tabstop = context.options()["tabstop"].get<int>();
|
2017-04-27 20:27:28 +02:00
|
|
|
const auto& cursor = context.selections().main().cursor();
|
|
|
|
|
2017-05-01 21:55:18 +02:00
|
|
|
// Ensure cursor line is visible
|
|
|
|
if (cursor.line - offset.line < m_position.line)
|
2017-05-06 20:43:12 +02:00
|
|
|
m_position.line = std::max(0_line, cursor.line - offset.line);
|
2017-05-01 21:55:18 +02:00
|
|
|
if (cursor.line + offset.line >= m_position.line + m_dimensions.line)
|
2017-05-06 20:43:12 +02:00
|
|
|
m_position.line = std::min(buffer().line_count()-1, cursor.line + offset.line - m_dimensions.line + 1);
|
2017-04-27 20:27:28 +02:00
|
|
|
|
2017-05-07 14:43:43 +02:00
|
|
|
DisplaySetup setup{
|
2017-04-27 20:27:28 +02:00
|
|
|
m_position,
|
|
|
|
m_dimensions,
|
2017-05-07 14:43:43 +02:00
|
|
|
{cursor.line - m_position.line,
|
|
|
|
get_column(buffer(), tabstop, cursor) - m_position.column},
|
2017-06-09 17:00:22 +02:00
|
|
|
offset,
|
|
|
|
false
|
2017-04-27 20:27:28 +02:00
|
|
|
};
|
2017-04-30 22:15:56 +02:00
|
|
|
for (auto pass : { HighlightPass::Move, HighlightPass::Wrap })
|
2017-05-07 14:43:43 +02:00
|
|
|
m_builtin_highlighters.compute_display_setup(context, pass, setup);
|
2017-04-27 20:27:28 +02:00
|
|
|
|
2017-05-01 21:55:18 +02:00
|
|
|
// now ensure the cursor column is visible
|
|
|
|
{
|
2017-05-07 14:43:43 +02:00
|
|
|
auto underflow = setup.cursor_pos.column - setup.scroll_offset.column;
|
2017-05-01 21:55:18 +02:00
|
|
|
if (underflow < 0)
|
|
|
|
{
|
2017-05-07 14:43:43 +02:00
|
|
|
setup.window_pos.column += underflow;
|
|
|
|
setup.cursor_pos.column -= underflow;
|
2017-05-01 21:55:18 +02:00
|
|
|
}
|
2017-05-07 14:43:43 +02:00
|
|
|
auto overflow = setup.cursor_pos.column + setup.scroll_offset.column - setup.window_range.column + 1;
|
2017-05-01 21:55:18 +02:00
|
|
|
if (overflow > 0)
|
|
|
|
{
|
2017-05-07 14:43:43 +02:00
|
|
|
setup.window_pos.column += overflow;
|
|
|
|
setup.cursor_pos.column -= overflow;
|
2017-05-01 21:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-07 14:43:43 +02:00
|
|
|
|
2017-06-09 17:00:22 +02:00
|
|
|
return setup;
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-17 21:17:32 +02:00
|
|
|
namespace
|
|
|
|
{
|
2016-09-22 21:36:26 +02:00
|
|
|
ColumnCount find_display_column(const DisplayLine& line, const Buffer& buffer,
|
2016-12-07 00:08:18 +01:00
|
|
|
BufferCoord coord)
|
2013-07-17 21:17:32 +02:00
|
|
|
{
|
2016-09-22 21:36:26 +02:00
|
|
|
ColumnCount column = 0;
|
2013-07-17 21:17:32 +02:00
|
|
|
for (auto& atom : line)
|
|
|
|
{
|
2013-07-24 14:55:57 +02:00
|
|
|
if (atom.has_buffer_range() and
|
|
|
|
coord >= atom.begin() and coord < atom.end())
|
2013-07-17 21:17:32 +02:00
|
|
|
{
|
2016-10-13 20:55:15 +02:00
|
|
|
if (atom.type() == DisplayAtom::Range)
|
2017-06-16 09:06:24 +02:00
|
|
|
column += utf8::column_distance(get_iterator(buffer, atom.begin()),
|
|
|
|
get_iterator(buffer, coord));
|
2013-07-17 21:17:32 +02:00
|
|
|
return column;
|
|
|
|
}
|
2013-07-24 14:55:57 +02:00
|
|
|
column += atom.length();
|
2013-07-17 21:17:32 +02:00
|
|
|
}
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord find_buffer_coord(const DisplayLine& line, const Buffer& buffer,
|
|
|
|
ColumnCount column)
|
2013-07-17 21:17:32 +02:00
|
|
|
{
|
2013-07-23 20:11:26 +02:00
|
|
|
auto& range = line.range();
|
2013-07-17 21:17:32 +02:00
|
|
|
for (auto& atom : line)
|
|
|
|
{
|
2016-09-22 21:36:26 +02:00
|
|
|
ColumnCount len = atom.length();
|
2013-07-24 14:55:57 +02:00
|
|
|
if (atom.has_buffer_range() and column < len)
|
2013-07-17 21:17:32 +02:00
|
|
|
{
|
2016-10-13 20:55:15 +02:00
|
|
|
if (atom.type() == DisplayAtom::Range)
|
2015-08-10 00:18:26 +02:00
|
|
|
return buffer.clamp(
|
2017-06-16 09:06:24 +02:00
|
|
|
utf8::advance(get_iterator(buffer, atom.begin()),
|
|
|
|
get_iterator(buffer, range.end),
|
2016-09-22 21:36:26 +02:00
|
|
|
std::max(0_col, column)).coord());
|
2015-08-10 00:18:26 +02:00
|
|
|
return buffer.clamp(atom.begin());
|
2014-10-27 14:22:42 +01:00
|
|
|
}
|
2013-07-17 21:17:32 +02:00
|
|
|
column -= len;
|
|
|
|
}
|
2015-04-23 22:38:45 +02:00
|
|
|
return buffer.clamp(buffer.prev(range.end));
|
2013-07-17 21:17:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 11:19:08 +02:00
|
|
|
Optional<DisplayCoord> Window::display_position(BufferCoord coord) const
|
2012-09-30 16:22:03 +02:00
|
|
|
{
|
2013-07-17 21:17:32 +02:00
|
|
|
LineCount l = 0;
|
2012-09-30 16:22:03 +02:00
|
|
|
for (auto& line : m_display_buffer.lines())
|
|
|
|
{
|
2013-07-23 20:11:26 +02:00
|
|
|
auto& range = line.range();
|
2015-04-23 22:38:45 +02:00
|
|
|
if (range.begin <= coord and coord < range.end)
|
2017-06-16 11:19:08 +02:00
|
|
|
return DisplayCoord{l, find_display_column(line, buffer(), coord)};
|
2013-07-17 21:17:32 +02:00
|
|
|
++l;
|
2012-09-30 16:22:03 +02:00
|
|
|
}
|
2017-06-16 11:19:08 +02:00
|
|
|
return {};
|
2012-09-30 16:22:03 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord Window::buffer_coord(DisplayCoord coord) const
|
2015-03-19 00:40:26 +01:00
|
|
|
{
|
2015-07-22 19:38:44 +02:00
|
|
|
if (m_display_buffer.lines().empty())
|
|
|
|
return {0,0};
|
2015-03-22 11:07:26 +01:00
|
|
|
if (coord <= 0_line)
|
|
|
|
coord = {0,0};
|
2017-04-25 19:44:14 +02:00
|
|
|
if ((size_t)coord.line >= m_display_buffer.lines().size())
|
2016-09-22 21:36:26 +02:00
|
|
|
coord = DisplayCoord{(int)m_display_buffer.lines().size()-1, INT_MAX};
|
2015-03-22 11:07:26 +01:00
|
|
|
|
|
|
|
return find_buffer_coord(m_display_buffer.lines()[(int)coord.line],
|
|
|
|
buffer(), coord.column);
|
2015-03-19 00:40:26 +01:00
|
|
|
}
|
|
|
|
|
2015-06-04 14:49:28 +02:00
|
|
|
void Window::clear_display_buffer()
|
|
|
|
{
|
|
|
|
m_display_buffer = DisplayBuffer{};
|
|
|
|
}
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
void Window::on_option_changed(const Option& option)
|
2012-06-14 15:19:38 +02:00
|
|
|
{
|
2016-05-13 21:32:53 +02:00
|
|
|
run_hook_in_own_context("WinSetOption", format("{}={}", option.name(),
|
|
|
|
option.get_as_string()));
|
2013-11-18 23:43:37 +01:00
|
|
|
// an highlighter might depend on the option, so we need to redraw
|
2015-06-21 20:56:23 +02:00
|
|
|
force_redraw();
|
2012-06-14 15:19:38 +02:00
|
|
|
}
|
|
|
|
|
2014-12-19 00:12:58 +01:00
|
|
|
|
2016-11-24 14:35:42 +01:00
|
|
|
void Window::run_hook_in_own_context(StringView hook_name, StringView param,
|
|
|
|
String client_name)
|
2014-12-19 00:12:58 +01:00
|
|
|
{
|
2016-11-14 14:59:33 +01:00
|
|
|
if (m_buffer->flags() & Buffer::Flags::NoHooks)
|
|
|
|
return;
|
|
|
|
|
2016-11-24 14:35:42 +01:00
|
|
|
InputHandler hook_handler{{ *m_buffer, Selection{} },
|
|
|
|
Context::Flags::Transient,
|
|
|
|
std::move(client_name)};
|
2014-12-19 00:12:58 +01:00
|
|
|
hook_handler.context().set_window(*this);
|
2016-05-09 14:48:48 +02:00
|
|
|
if (m_client)
|
|
|
|
hook_handler.context().set_client(*m_client);
|
|
|
|
|
2014-12-19 00:12:58 +01:00
|
|
|
hooks().run_hook(hook_name, param, hook_handler.context());
|
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|