1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-21 18:41:29 +03:00
kakoune/src/window.cc

130 lines
3.7 KiB
C++
Raw Normal View History

2011-09-02 20:51:20 +04:00
#include "window.hh"
#include "assert.hh"
#include "highlighter_registry.hh"
2012-04-03 16:01:01 +04:00
#include "hook_manager.hh"
#include "context.hh"
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
{
Window::Window(Buffer& buffer)
: Editor(buffer),
2011-09-02 20:51:20 +04:00
m_position(0, 0),
m_dimensions(0, 0),
m_option_manager(buffer.option_manager())
2011-09-02 20:51:20 +04:00
{
HighlighterRegistry& registry = HighlighterRegistry::instance();
2012-04-03 16:01:01 +04:00
GlobalHookManager::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 18:00:04 +04:00
{
scroll_to_keep_cursor_visible_ifn();
2011-09-02 20:51:20 +04: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 20:51:20 +04:00
m_highlighters(m_display_buffer);
m_display_buffer.check_invariant();
2011-09-02 20:51:20 +04:00
}
void Window::set_dimensions(const DisplayCoord& dimensions)
{
m_dimensions = dimensions;
}
2011-09-05 22:55:31 +04:00
void Window::scroll_to_keep_cursor_visible_ifn()
{
BufferCoord cursor = buffer().line_and_column_at(selections().back().last());
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);
if (cursor.column < m_position.column)
m_position.column = cursor.column;
else if (cursor.column >= m_position.column + m_dimensions.column)
m_position.column = cursor.column - (m_dimensions.column - 1);
2011-09-05 22:55:31 +04:00
}
String Window::status_line() const
2011-10-04 22:49:41 +04:00
{
BufferCoord cursor = buffer().line_and_column_at(selections().back().last());
2011-10-04 22:49:41 +04:00
std::ostringstream oss;
oss << buffer().name();
if (buffer().is_modified())
2011-10-05 18:21:24 +04:00
oss << " [+]";
oss << " -- " << cursor.line+1 << "," << cursor.column+1
2011-12-21 23:06:26 +04:00
<< " -- " << selections().size() << " sel -- ";
if (is_editing())
2011-10-05 18:21:24 +04:00
oss << "[Insert]";
2011-10-04 22:49:41 +04:00
return oss.str();
}
void Window::on_incremental_insertion_end()
{
push_selections();
2012-04-03 16:01:01 +04:00
hook_manager().run_hook("InsertEnd", "", Context(*this));
pop_selections();
}
2011-09-02 20:51:20 +04:00
}