2011-09-02 18:51:20 +02:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-19 23:56:29 +02:00
|
|
|
#include "assert.hh"
|
2012-11-23 13:40:20 +01:00
|
|
|
#include "highlighter.hh"
|
2012-04-03 14:01:01 +02:00
|
|
|
#include "hook_manager.hh"
|
2012-01-23 14:56:43 +01:00
|
|
|
#include "context.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
|
|
|
|
{
|
|
|
|
|
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
|
|
|
{
|
2011-11-29 23:37:20 +01:00
|
|
|
HighlighterRegistry& registry = HighlighterRegistry::instance();
|
2011-11-26 19:39:59 +01:00
|
|
|
|
2012-11-22 13:50:29 +01:00
|
|
|
m_hooks.run_hook("WinCreate", buffer.name(), Context(*this));
|
|
|
|
m_options.register_watcher(*this);
|
2011-11-26 19:39:59 +01:00
|
|
|
|
2012-11-23 13:40:20 +01:00
|
|
|
m_highlighters.append(registry["expand_tabs"](*this, {}));
|
|
|
|
m_highlighters.append(registry["highlight_selections"](*this, {}));
|
2012-06-14 15:19:38 +02:00
|
|
|
|
2012-11-22 13:50:29 +01:00
|
|
|
for (auto& option : m_options.flatten_options())
|
2012-06-14 15:19:38 +02:00
|
|
|
on_option_changed(option.first, option.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
2012-11-22 13:50:29 +01:00
|
|
|
m_options.unregister_watcher(*this);
|
2011-09-27 20:45:22 +02:00
|
|
|
}
|
|
|
|
|
2012-08-21 20:05:56 +02:00
|
|
|
void Window::center_selection()
|
|
|
|
{
|
|
|
|
BufferIterator cursor = selections().back().last();
|
2012-08-22 23:33:52 +02:00
|
|
|
m_position.line = std::max(0_line, cursor.line() - m_dimensions.line/2_line);
|
2012-08-21 20:05:56 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2012-10-11 00:41:48 +02:00
|
|
|
BufferIterator line_begin = buffer().iterator_at_line_begin(buffer_line);
|
|
|
|
BufferIterator line_end = buffer().iterator_at_line_end(buffer_line);
|
2012-07-03 23:23:07 +02:00
|
|
|
|
2012-10-11 00:41:48 +02: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-12 23:19:10 +02:00
|
|
|
|
|
|
|
lines.push_back(DisplayLine(buffer_line));
|
2012-10-11 00:41:48 +02:00
|
|
|
lines.back().push_back(DisplayAtom(AtomContent(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();
|
2012-01-19 21:37:29 +01:00
|
|
|
m_highlighters(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;
|
|
|
|
}
|
|
|
|
|
2011-09-05 20:55:31 +02:00
|
|
|
void Window::scroll_to_keep_cursor_visible_ifn()
|
|
|
|
{
|
2012-10-11 14:23:20 +02:00
|
|
|
const BufferIterator cursor = selections().back().last();
|
2012-07-15 01:48:50 +02:00
|
|
|
|
|
|
|
// scroll lines if needed
|
|
|
|
if (cursor.line() < m_position.line)
|
|
|
|
m_position.line = cursor.line();
|
|
|
|
else if (cursor.line() >= m_position.line + m_dimensions.line)
|
|
|
|
m_position.line = cursor.line() - (m_dimensions.line - 1);
|
|
|
|
|
|
|
|
// highlight only the line containing the cursor
|
|
|
|
DisplayBuffer display_buffer;
|
|
|
|
DisplayBuffer::LineList& lines = display_buffer.lines();
|
|
|
|
lines.push_back(DisplayLine(cursor.line()));
|
|
|
|
|
|
|
|
BufferIterator line_begin = buffer().iterator_at_line_begin(cursor);
|
|
|
|
BufferIterator line_end = buffer().iterator_at_line_end(cursor);
|
|
|
|
lines.back().push_back(DisplayAtom(AtomContent(line_begin, line_end)));
|
|
|
|
|
|
|
|
display_buffer.compute_range();
|
|
|
|
m_highlighters(display_buffer);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
atom.content.begin() <= cursor and atom.content.end() > cursor)
|
|
|
|
{
|
|
|
|
if (atom.content.type() == AtomContent::BufferRange)
|
2012-10-11 00:41:48 +02:00
|
|
|
column += utf8::distance(atom.content.begin(), cursor);
|
2012-07-15 01:48:50 +02:00
|
|
|
else
|
2012-10-11 00:41:48 +02:00
|
|
|
column += atom.content.content().char_length();
|
2012-07-15 01:48:50 +02:00
|
|
|
|
2012-10-11 14:23:20 +02:00
|
|
|
CharCount cursor_col = utf8::distance(line_begin, cursor);
|
2012-07-15 01:48:50 +02:00
|
|
|
// we could early out on this, but having scrolling left
|
|
|
|
// faster than not scrolling at all is not really useful.
|
2012-10-11 14:23:20 +02:00
|
|
|
if (cursor_col < m_position.column)
|
|
|
|
m_position.column = cursor_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
|
|
|
}
|
2012-08-02 07:04:42 +02:00
|
|
|
if (cursor != buffer().end())
|
|
|
|
{
|
|
|
|
// the cursor should always be visible.
|
|
|
|
assert(false);
|
|
|
|
}
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
|
|
|
|
2012-09-30 16:22:03 +02: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())
|
|
|
|
{
|
|
|
|
res.column += iterator - content.begin();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
res.column += content.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++res.line;
|
|
|
|
}
|
|
|
|
return { 0, 0 };
|
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
String Window::status_line() const
|
2011-10-04 20:49:41 +02:00
|
|
|
{
|
2012-11-22 18:54:37 +01:00
|
|
|
BufferCoord cursor = selections().back().last().coord();
|
2011-10-04 20:49:41 +02:00
|
|
|
std::ostringstream oss;
|
2012-01-31 20:12:06 +01:00
|
|
|
oss << buffer().name();
|
|
|
|
if (buffer().is_modified())
|
2011-10-05 16:21:24 +02:00
|
|
|
oss << " [+]";
|
2012-08-23 23:56:35 +02:00
|
|
|
oss << " -- " << (int)cursor.line+1 << "," << (int)cursor.column+1
|
2011-12-21 20:06:26 +01:00
|
|
|
<< " -- " << selections().size() << " sel -- ";
|
2012-02-08 00:41:10 +01:00
|
|
|
if (is_editing())
|
2011-10-05 16:21:24 +02:00
|
|
|
oss << "[Insert]";
|
2011-10-04 20:49:41 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2012-02-08 00:41:10 +01:00
|
|
|
void Window::on_incremental_insertion_end()
|
2011-09-19 23:56:29 +02:00
|
|
|
{
|
2012-11-30 18:32:49 +01:00
|
|
|
SelectionList backup(selections());
|
2012-11-22 13:50:29 +01:00
|
|
|
hooks().run_hook("InsertEnd", "", Context(*this));
|
2012-09-24 20:47:06 +02:00
|
|
|
select(backup);
|
2011-09-19 23:56:29 +02:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:19:38 +02:00
|
|
|
void Window::on_option_changed(const String& name, const Option& option)
|
|
|
|
{
|
|
|
|
String desc = name + "=" + option.as_string();
|
2012-11-22 13:50:29 +01:00
|
|
|
m_hooks.run_hook("WinSetOption", desc, Context(*this));
|
2012-06-14 15:19:38 +02:00
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|