From 26459abf8aef509ee45831bcfd1a541bbf831355 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 14 Oct 2011 14:29:53 +0000 Subject: [PATCH] WindowCoord: rename to DisplayCoord and move with DisplayBuffer --- src/display_buffer.hh | 8 ++++++++ src/main.cc | 24 ++++++++++++------------ src/window.cc | 41 ++++++++++++++++++++++++----------------- src/window.hh | 26 +++++++++----------------- 4 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/display_buffer.hh b/src/display_buffer.hh index 7f022b8c..a3451c35 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -4,11 +4,19 @@ #include #include +#include "line_and_column.hh" + #include "buffer.hh" namespace Kakoune { +struct DisplayCoord : LineAndColumn +{ + DisplayCoord(int line = 0, int column = 0) + : LineAndColumn(line, column) {} +}; + typedef int Attribute; enum Attributes diff --git a/src/main.cc b/src/main.cc index a2b4aeb8..7f6902df 100644 --- a/src/main.cc +++ b/src/main.cc @@ -79,10 +79,10 @@ void draw_window(Window& window) getmaxyx(stdscr, max_y, max_x); max_y -= 1; - window.set_dimensions(WindowCoord(max_y, max_x)); + window.set_dimensions(DisplayCoord(max_y, max_x)); window.update_display_buffer(); - WindowCoord position; + DisplayCoord position; for (const DisplayAtom& atom : window.display_buffer()) { const std::string content = atom.replacement_text.empty() ? @@ -142,7 +142,7 @@ void draw_window(Window& window) clrtoeol(); addstr(status_line.c_str()); - const WindowCoord& cursor_position = window.cursor_position(); + const DisplayCoord& cursor_position = window.cursor_position(); move(cursor_position.line, cursor_position.column); } @@ -285,7 +285,7 @@ void do_insert(Window& window, IncrementalInserter::Mode mode) draw_window(window); while(true) { - const WindowCoord& pos = window.cursor_position(); + const DisplayCoord& pos = window.cursor_position(); move(pos.line, pos.column); char c = getch(); @@ -493,15 +493,15 @@ void do_paste(Window& window, int count) std::unordered_map> keymap = { - { 'h', [](Window& window, int count) { window.move_cursor(WindowCoord(0, -std::max(count,1))); } }, - { 'j', [](Window& window, int count) { window.move_cursor(WindowCoord( std::max(count,1), 0)); } }, - { 'k', [](Window& window, int count) { window.move_cursor(WindowCoord(-std::max(count,1), 0)); } }, - { 'l', [](Window& window, int count) { window.move_cursor(WindowCoord(0, std::max(count,1))); } }, + { 'h', [](Window& window, int count) { window.move_cursor(DisplayCoord(0, -std::max(count,1))); } }, + { 'j', [](Window& window, int count) { window.move_cursor(DisplayCoord( std::max(count,1), 0)); } }, + { 'k', [](Window& window, int count) { window.move_cursor(DisplayCoord(-std::max(count,1), 0)); } }, + { 'l', [](Window& window, int count) { window.move_cursor(DisplayCoord(0, std::max(count,1))); } }, - { 'H', [](Window& window, int count) { window.move_cursor(WindowCoord(0, -std::max(count,1)), true); } }, - { 'J', [](Window& window, int count) { window.move_cursor(WindowCoord( std::max(count,1), 0), true); } }, - { 'K', [](Window& window, int count) { window.move_cursor(WindowCoord(-std::max(count,1), 0), true); } }, - { 'L', [](Window& window, int count) { window.move_cursor(WindowCoord(0, std::max(count,1)), true); } }, + { 'H', [](Window& window, int count) { window.move_cursor(DisplayCoord(0, -std::max(count,1)), true); } }, + { 'J', [](Window& window, int count) { window.move_cursor(DisplayCoord( std::max(count,1), 0), true); } }, + { 'K', [](Window& window, int count) { window.move_cursor(DisplayCoord(-std::max(count,1), 0), true); } }, + { 'L', [](Window& window, int count) { window.move_cursor(DisplayCoord(0, std::max(count,1)), true); } }, { 't', [](Window& window, int count) { window.select(std::bind(select_to, _1, getch(), count, false)); } }, { 'f', [](Window& window, int count) { window.select(std::bind(select_to, _1, getch(), count, true)); } }, diff --git a/src/window.cc b/src/window.cc index 3cd7dc33..f03bf5d5 100644 --- a/src/window.cc +++ b/src/window.cc @@ -121,7 +121,7 @@ void Window::check_invariant() const assert(not m_selections.empty()); } -WindowCoord Window::cursor_position() const +DisplayCoord Window::cursor_position() const { check_invariant(); return line_and_column_at(m_selections.back().last()); @@ -144,22 +144,29 @@ void Window::erase_noundo() scroll_to_keep_cursor_visible_ifn(); } -static WindowCoord measure_string(const Window::String& string) +template +static DisplayCoord measure_string(Iterator begin, Iterator end) { - WindowCoord result(0, 0); - for (size_t i = 0; i < string.length(); ++i) + DisplayCoord result(0, 0); + while (begin != end) { - if (string[i] == '\n') + if (*begin == '\n') { ++result.line; result.column = 0; } else ++result.column; + ++begin; } return result; } +static DisplayCoord measure_string(const Window::String& string) +{ + return measure_string(string.begin(), string.end()); +} + void Window::insert(const String& string) { scoped_undo_group undo_group(m_buffer); @@ -199,24 +206,24 @@ bool Window::redo() return m_buffer.redo(); } -BufferCoord Window::window_to_buffer(const WindowCoord& window_pos) const +BufferCoord Window::window_to_buffer(const DisplayCoord& window_pos) const { return BufferCoord(m_position.line + window_pos.line, m_position.column + window_pos.column); } -WindowCoord Window::buffer_to_window(const BufferCoord& buffer_pos) const +DisplayCoord Window::buffer_to_window(const BufferCoord& buffer_pos) const { - return WindowCoord(buffer_pos.line - m_position.line, + return DisplayCoord(buffer_pos.line - m_position.line, buffer_pos.column - m_position.column); } -BufferIterator Window::iterator_at(const WindowCoord& window_pos) const +BufferIterator Window::iterator_at(const DisplayCoord& window_pos) const { return m_buffer.iterator_at(window_to_buffer(window_pos)); } -WindowCoord Window::line_and_column_at(const BufferIterator& iterator) const +DisplayCoord Window::line_and_column_at(const BufferIterator& iterator) const { return buffer_to_window(m_buffer.line_and_column_at(iterator)); } @@ -258,7 +265,7 @@ BufferString Window::selection_content() const m_selections.back().end()); } -void Window::move_cursor(const WindowCoord& offset, bool append) +void Window::move_cursor(const DisplayCoord& offset, bool append) { if (not append) move_cursor_to(iterator_at(cursor_position() + offset)); @@ -266,7 +273,7 @@ void Window::move_cursor(const WindowCoord& offset, bool append) { for (auto& sel : m_selections) { - WindowCoord pos = line_and_column_at(sel.last()); + DisplayCoord pos = line_and_column_at(sel.last()); sel = Selection(sel.first(), iterator_at(pos + offset)); } scroll_to_keep_cursor_visible_ifn(); @@ -300,7 +307,7 @@ void Window::update_display_buffer() } } -void Window::set_dimensions(const WindowCoord& dimensions) +void Window::set_dimensions(const DisplayCoord& dimensions) { m_dimensions = dimensions; } @@ -309,7 +316,7 @@ void Window::scroll_to_keep_cursor_visible_ifn() { check_invariant(); - WindowCoord cursor = line_and_column_at(m_selections.back().last()); + DisplayCoord cursor = line_and_column_at(m_selections.back().last()); if (cursor.line < 0) { m_position.line = std::max(m_position.line + cursor.line, 0); @@ -399,15 +406,15 @@ void IncrementalInserter::insert(const Window::String& string) void IncrementalInserter::erase() { - move_cursor(WindowCoord(0, -1)); + move_cursor(DisplayCoord(0, -1)); m_window.erase_noundo(); } -void IncrementalInserter::move_cursor(const WindowCoord& offset) +void IncrementalInserter::move_cursor(const DisplayCoord& offset) { for (auto& sel : m_window.m_selections) { - WindowCoord pos = m_window.line_and_column_at(sel.last()); + DisplayCoord pos = m_window.line_and_column_at(sel.last()); BufferIterator it = m_window.iterator_at(pos + offset); sel = Selection(it, it); } diff --git a/src/window.hh b/src/window.hh index 7a45ed8a..05141fe2 100644 --- a/src/window.hh +++ b/src/window.hh @@ -3,20 +3,12 @@ #include -#include "line_and_column.hh" - #include "buffer.hh" #include "display_buffer.hh" namespace Kakoune { -struct WindowCoord : LineAndColumn -{ - WindowCoord(int line = 0, int column = 0) - : LineAndColumn(line, column) {} -}; - struct Selection { Selection(const BufferIterator& first, const BufferIterator& last) @@ -50,21 +42,21 @@ public: void append(const String& string); const BufferCoord& position() const { return m_position; } - WindowCoord cursor_position() const; + DisplayCoord cursor_position() const; Buffer& buffer() const { return m_buffer; } - BufferIterator iterator_at(const WindowCoord& window_pos) const; - WindowCoord line_and_column_at(const BufferIterator& iterator) const; + BufferIterator iterator_at(const DisplayCoord& window_pos) const; + DisplayCoord line_and_column_at(const BufferIterator& iterator) const; - void move_cursor(const WindowCoord& offset, bool append = false); + void move_cursor(const DisplayCoord& offset, bool append = false); void move_cursor_to(const BufferIterator& iterator); void clear_selections(); void select(const Selector& selector, bool append = false); BufferString selection_content() const; - void set_dimensions(const WindowCoord& dimensions); + void set_dimensions(const DisplayCoord& dimensions); const DisplayBuffer& display_buffer() const { return m_display_buffer; } @@ -88,8 +80,8 @@ private: void insert_noundo(const String& string); void append_noundo(const String& string); - BufferCoord window_to_buffer(const WindowCoord& window_pos) const; - WindowCoord buffer_to_window(const BufferCoord& buffer_pos) const; + BufferCoord window_to_buffer(const DisplayCoord& window_pos) const; + DisplayCoord buffer_to_window(const BufferCoord& buffer_pos) const; friend class IncrementalInserter; IncrementalInserter* m_current_inserter; @@ -98,7 +90,7 @@ private: Buffer& m_buffer; BufferCoord m_position; - WindowCoord m_dimensions; + DisplayCoord m_dimensions; SelectionList m_selections; DisplayBuffer m_display_buffer; @@ -123,7 +115,7 @@ public: void insert(const Window::String& string); void erase(); - void move_cursor(const WindowCoord& offset); + void move_cursor(const DisplayCoord& offset); private: Window& m_window;