kakoune/src/window.cc

137 lines
3.7 KiB
C++
Raw Normal View History

2011-09-02 18:51:20 +02:00
#include "window.hh"
#include "assert.hh"
#include "highlighter_registry.hh"
#include "hooks_manager.hh"
#include "context.hh"
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
{
Window::Window(Buffer& buffer)
: Editor(buffer),
2011-09-02 18:51:20 +02:00
m_position(0, 0),
m_dimensions(0, 0)
2011-09-02 18:51:20 +02:00
{
HighlighterRegistry& registry = HighlighterRegistry::instance();
GlobalHooksManager::instance().run_hook("WinCreate", buffer.name(),
Context(*this));
registry.add_highlighter_to_window(*this, "expand_tabs", HighlighterParameters());
registry.add_highlighter_to_window(*this, "highlight_selections", HighlighterParameters());
}
BufferIterator Window::iterator_at(const DisplayCoord& window_pos) const
{
if (m_display_buffer.begin() == m_display_buffer.end())
return buffer().begin();
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())
{
return (--atom_it)->iterator_at(window_pos);
}
}
}
return buffer().iterator_at(m_position + BufferCoord(window_pos));
}
DisplayCoord Window::line_and_column_at(const BufferIterator& iterator) const
{
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);
}
}
}
BufferCoord coord = buffer().line_and_column_at(iterator);
return DisplayCoord(coord.line - m_position.line,
coord.column - m_position.column);
}
void Window::update_display_buffer()
2011-09-22 16:00:04 +02:00
{
scroll_to_keep_cursor_visible_ifn();
2011-09-02 18:51:20 +02:00
m_display_buffer.clear();
BufferIterator begin = buffer().iterator_at(m_position);
BufferIterator end = buffer().iterator_at(m_position +
BufferCoord(m_dimensions.line, m_dimensions.column))+2;
if (begin == end)
return;
m_display_buffer.append(DisplayAtom(DisplayCoord(0,0), begin, end));
2011-09-02 18:51:20 +02:00
m_highlighters(m_display_buffer);
m_display_buffer.check_invariant();
2011-09-02 18:51:20 +02:00
}
void Window::set_dimensions(const DisplayCoord& dimensions)
{
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());
if (cursor.line < 0)
2011-09-05 20:55:31 +02:00
{
m_position.line = std::max(m_position.line + cursor.line, 0);
2011-09-05 20:55:31 +02:00
}
else if (cursor.line >= m_dimensions.line)
2011-09-05 20:55:31 +02:00
{
m_position.line += cursor.line - (m_dimensions.line - 1);
2011-09-05 20:55:31 +02:00
}
if (cursor.column < 0)
2011-09-05 20:55:31 +02:00
{
m_position.column = std::max(m_position.column + cursor.column, 0);
2011-09-05 20:55:31 +02:00
}
else if (cursor.column >= m_dimensions.column)
2011-09-05 20:55:31 +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
{
BufferCoord cursor = buffer().line_and_column_at(selections().back().last());
2011-10-04 20:49:41 +02:00
std::ostringstream oss;
oss << buffer().name();
if (buffer().is_modified())
2011-10-05 16:21:24 +02:00
oss << " [+]";
oss << " -- " << cursor.line+1 << "," << cursor.column+1
2011-12-21 20:06:26 +01:00
<< " -- " << selections().size() << " sel -- ";
if (is_editing())
2011-10-05 16:21:24 +02:00
oss << "[Insert]";
2011-10-04 20:49:41 +02:00
return oss.str();
}
void Window::on_incremental_insertion_end()
{
push_selections();
hooks_manager().run_hook("InsertEnd", "", Context(*this));
pop_selections();
}
2011-09-02 18:51:20 +02:00
}