2011-09-02 18:51:20 +02:00
|
|
|
#include "window.hh"
|
|
|
|
|
2011-09-19 23:56:29 +02:00
|
|
|
#include "assert.hh"
|
2011-11-29 23:37:20 +01:00
|
|
|
#include "highlighter_registry.hh"
|
2011-11-26 19:39:59 +01:00
|
|
|
#include "hooks_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),
|
2011-09-02 18:51:20 +02:00
|
|
|
m_position(0, 0),
|
2012-01-31 20:12:06 +01:00
|
|
|
m_dimensions(0, 0)
|
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-01-23 14:40:42 +01:00
|
|
|
GlobalHooksManager::instance().run_hook("WinCreate", buffer.name(),
|
|
|
|
Context(*this));
|
2011-11-26 19:39:59 +01:00
|
|
|
|
2011-11-29 23:37:20 +01:00
|
|
|
registry.add_highlighter_to_window(*this, "expand_tabs", HighlighterParameters());
|
|
|
|
registry.add_highlighter_to_window(*this, "highlight_selections", HighlighterParameters());
|
2011-09-27 20:45:22 +02:00
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
DisplayCoord Window::cursor_position() const
|
2011-09-27 20:45:22 +02:00
|
|
|
{
|
2011-10-15 06:45:49 +02:00
|
|
|
return line_and_column_at(cursor_iterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferIterator Window::cursor_iterator() const
|
|
|
|
{
|
2011-12-21 20:06:26 +01:00
|
|
|
return selections().back().last();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
template<typename Iterator>
|
|
|
|
static DisplayCoord measure_string(Iterator begin, Iterator end)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-10-14 16:29:53 +02:00
|
|
|
DisplayCoord result(0, 0);
|
|
|
|
while (begin != end)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-10-14 16:29:53 +02:00
|
|
|
if (*begin == '\n')
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
|
|
|
++result.line;
|
|
|
|
result.column = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++result.column;
|
2011-10-14 16:29:53 +02:00
|
|
|
++begin;
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
static DisplayCoord measure_string(const Editor::String& string)
|
2011-10-14 16:29:53 +02:00
|
|
|
{
|
|
|
|
return measure_string(string.begin(), string.end());
|
|
|
|
}
|
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
BufferIterator Window::iterator_at(const DisplayCoord& window_pos) const
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
2011-10-15 06:45:49 +02:00
|
|
|
if (m_display_buffer.begin() == m_display_buffer.end())
|
2012-01-31 20:12:06 +01:00
|
|
|
return buffer().begin();
|
2011-09-05 20:54:17 +02:00
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
if (DisplayCoord(0,0) <= window_pos)
|
|
|
|
{
|
|
|
|
for (auto atom_it = m_display_buffer.begin();
|
|
|
|
atom_it != m_display_buffer.end(); ++atom_it)
|
|
|
|
{
|
|
|
|
if (window_pos < atom_it->coord())
|
|
|
|
{
|
2011-10-18 15:59:32 +02:00
|
|
|
return (--atom_it)->iterator_at(window_pos);
|
2011-10-15 06:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-05 20:54:17 +02:00
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
return buffer().iterator_at(m_position + BufferCoord(window_pos));
|
2011-09-05 20:54:17 +02:00
|
|
|
}
|
|
|
|
|
2011-10-14 16:29:53 +02:00
|
|
|
DisplayCoord Window::line_and_column_at(const BufferIterator& iterator) const
|
2011-09-05 20:54:17 +02:00
|
|
|
{
|
2011-10-15 06:45:49 +02:00
|
|
|
if (m_display_buffer.begin() == m_display_buffer.end())
|
|
|
|
return DisplayCoord(0, 0);
|
|
|
|
|
|
|
|
if (iterator >= m_display_buffer.front().begin() and
|
|
|
|
iterator < m_display_buffer.back().end())
|
|
|
|
{
|
|
|
|
for (auto& atom : m_display_buffer)
|
|
|
|
{
|
|
|
|
if (atom.end() > iterator)
|
|
|
|
{
|
|
|
|
assert(atom.begin() <= iterator);
|
|
|
|
return atom.line_and_column_at(iterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-31 20:12:06 +01:00
|
|
|
BufferCoord coord = buffer().line_and_column_at(iterator);
|
2011-10-15 06:45:49 +02:00
|
|
|
return DisplayCoord(coord.line - m_position.line,
|
|
|
|
coord.column - m_position.column);
|
2011-09-05 20:54:17 +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
|
|
|
|
|
|
|
m_display_buffer.clear();
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
BufferIterator begin = buffer().iterator_at(m_position);
|
|
|
|
BufferIterator end = buffer().iterator_at(m_position +
|
2011-11-24 15:23:41 +01:00
|
|
|
BufferCoord(m_dimensions.line, m_dimensions.column))+1;
|
2011-10-07 16:19:58 +02:00
|
|
|
if (begin == end)
|
|
|
|
return;
|
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
m_display_buffer.append(DisplayAtom(DisplayCoord(0,0), begin, end));
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2012-01-19 21:37:29 +01:00
|
|
|
m_highlighters(m_display_buffer);
|
|
|
|
m_display_buffer.check_invariant();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2011-12-21 20:06:26 +01:00
|
|
|
DisplayCoord cursor = line_and_column_at(selections().back().last());
|
2011-09-27 20:45:22 +02:00
|
|
|
if (cursor.line < 0)
|
2011-09-05 20:55:31 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_position.line = std::max(m_position.line + cursor.line, 0);
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
2011-09-27 20:45:22 +02:00
|
|
|
else if (cursor.line >= m_dimensions.line)
|
2011-09-05 20:55:31 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_position.line += cursor.line - (m_dimensions.line - 1);
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
|
|
|
|
2011-09-27 20:45:22 +02:00
|
|
|
if (cursor.column < 0)
|
2011-09-05 20:55:31 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_position.column = std::max(m_position.column + cursor.column, 0);
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
2011-09-27 20:45:22 +02:00
|
|
|
else if (cursor.column >= m_dimensions.column)
|
2011-09-05 20:55:31 +02:00
|
|
|
{
|
2011-09-27 20:45:22 +02:00
|
|
|
m_position.column += cursor.column - (m_dimensions.column - 1);
|
2011-09-05 20:55:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 20:49:41 +02:00
|
|
|
std::string Window::status_line() const
|
|
|
|
{
|
2012-01-31 20:12:06 +01:00
|
|
|
BufferCoord cursor = buffer().line_and_column_at(selections().back().last());
|
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 << " [+]";
|
2011-10-27 16:27:39 +02:00
|
|
|
oss << " -- " << cursor.line+1 << "," << cursor.column+1
|
2011-12-21 20:06:26 +01:00
|
|
|
<< " -- " << selections().size() << " sel -- ";
|
2012-01-31 20:12:06 +01:00
|
|
|
if (is_inserting())
|
2011-10-05 16:21:24 +02:00
|
|
|
oss << "[Insert]";
|
2011-10-04 20:49:41 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
void Window::on_end_incremental_insert()
|
2011-09-19 23:56:29 +02:00
|
|
|
{
|
2012-01-31 20:12:06 +01:00
|
|
|
push_selections();
|
|
|
|
hooks_manager().run_hook("InsertEnd", "", Context(*this));
|
|
|
|
pop_selections();
|
2011-09-19 23:56:29 +02:00
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|