2011-09-02 20:51:20 +04:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-20 01:56:29 +04:00
|
|
|
#include "assert.hh"
|
2011-11-30 02:37:20 +04:00
|
|
|
#include "highlighter_registry.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),
|
2011-09-02 20:51:20 +04:00
|
|
|
m_position(0, 0),
|
2012-04-03 17:39:20 +04:00
|
|
|
m_dimensions(0, 0),
|
2012-06-07 17:29:44 +04:00
|
|
|
m_hook_manager(buffer.hook_manager()),
|
2012-04-03 17:39:20 +04:00
|
|
|
m_option_manager(buffer.option_manager())
|
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
|
|
|
|
2012-06-07 17:29:44 +04:00
|
|
|
m_hook_manager.run_hook("WinCreate", buffer.name(), Context(*this));
|
2012-06-14 17:19:38 +04:00
|
|
|
m_option_manager.register_watcher(*this);
|
2011-11-26 22:39:59 +04:00
|
|
|
|
2012-06-12 22:24:29 +04:00
|
|
|
registry.add_highlighter_to_group(*this, m_highlighters, "expand_tabs", HighlighterParameters());
|
|
|
|
registry.add_highlighter_to_group(*this, m_highlighters, "highlight_selections", HighlighterParameters());
|
2012-06-14 17:19:38 +04:00
|
|
|
|
|
|
|
for (auto& option : m_option_manager.flatten_options())
|
|
|
|
on_option_changed(option.first, option.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
|
|
|
m_option_manager.unregister_watcher(*this);
|
2011-09-27 22:45:22 +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-07-04 01:23:07 +04:00
|
|
|
for (auto line = 0; line < m_dimensions.line; ++line)
|
|
|
|
{
|
|
|
|
auto buffer_line = m_position.line + line;
|
|
|
|
if (buffer_line >= buffer().line_count())
|
|
|
|
break;
|
|
|
|
BufferIterator pos = buffer().iterator_at({ buffer_line, m_position.column });
|
|
|
|
BufferIterator line_begin = buffer().iterator_at_line_begin(pos);
|
|
|
|
BufferIterator line_end = buffer().iterator_at_line_end(pos);
|
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
BufferIterator end;
|
|
|
|
if (line_end - pos > m_dimensions.column)
|
|
|
|
end = pos + m_dimensions.column;
|
|
|
|
else
|
|
|
|
end = line_end;
|
|
|
|
|
|
|
|
lines.push_back(DisplayLine(buffer_line));
|
|
|
|
lines.back().push_back(DisplayAtom(AtomContent(pos,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);
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2012-07-15 03:48:50 +04:00
|
|
|
BufferIterator cursor = selections().back().last();
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
int column = 0;
|
|
|
|
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)
|
|
|
|
column += cursor - atom.content.begin();
|
|
|
|
else
|
|
|
|
column += atom.content.content().length();
|
|
|
|
|
|
|
|
// we could early out on this, but having scrolling left
|
|
|
|
// faster than not scrolling at all is not really useful.
|
|
|
|
if (cursor.column() < m_position.column)
|
|
|
|
m_position.column = cursor.column();
|
|
|
|
else if (column >= m_position.column + m_dimensions.column)
|
|
|
|
m_position.column = column - (m_dimensions.column - 1);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
column += atom.content.content().length();
|
|
|
|
}
|
2012-08-02 09:04:42 +04:00
|
|
|
if (cursor != buffer().end())
|
|
|
|
{
|
|
|
|
// the cursor should always be visible.
|
|
|
|
assert(false);
|
|
|
|
}
|
2011-09-05 22:55:31 +04:00
|
|
|
}
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
String Window::status_line() const
|
2011-10-04 22:49:41 +04:00
|
|
|
{
|
2012-01-31 23:12:06 +04:00
|
|
|
BufferCoord cursor = buffer().line_and_column_at(selections().back().last());
|
2011-10-04 22:49:41 +04:00
|
|
|
std::ostringstream oss;
|
2012-01-31 23:12:06 +04:00
|
|
|
oss << buffer().name();
|
|
|
|
if (buffer().is_modified())
|
2011-10-05 18:21:24 +04:00
|
|
|
oss << " [+]";
|
2011-10-27 18:27:39 +04:00
|
|
|
oss << " -- " << cursor.line+1 << "," << cursor.column+1
|
2011-12-21 23:06:26 +04:00
|
|
|
<< " -- " << selections().size() << " sel -- ";
|
2012-02-08 03:41:10 +04:00
|
|
|
if (is_editing())
|
2011-10-05 18:21:24 +04:00
|
|
|
oss << "[Insert]";
|
2011-10-04 22:49:41 +04:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2012-02-08 03:41:10 +04:00
|
|
|
void Window::on_incremental_insertion_end()
|
2011-09-20 01:56:29 +04:00
|
|
|
{
|
2012-01-31 23:12:06 +04:00
|
|
|
push_selections();
|
2012-04-03 16:01:01 +04:00
|
|
|
hook_manager().run_hook("InsertEnd", "", Context(*this));
|
2012-01-31 23:12:06 +04:00
|
|
|
pop_selections();
|
2011-09-20 01:56:29 +04:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
m_hook_manager.run_hook("WinSetOption", desc, Context(*this));
|
|
|
|
}
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|