WindowCoord: rename to DisplayCoord and move with DisplayBuffer

This commit is contained in:
Maxime Coste 2011-10-14 14:29:53 +00:00
parent c5be69a9a9
commit 26459abf8a
4 changed files with 53 additions and 46 deletions

View File

@ -4,11 +4,19 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "line_and_column.hh"
#include "buffer.hh" #include "buffer.hh"
namespace Kakoune namespace Kakoune
{ {
struct DisplayCoord : LineAndColumn<DisplayCoord>
{
DisplayCoord(int line = 0, int column = 0)
: LineAndColumn(line, column) {}
};
typedef int Attribute; typedef int Attribute;
enum Attributes enum Attributes

View File

@ -79,10 +79,10 @@ void draw_window(Window& window)
getmaxyx(stdscr, max_y, max_x); getmaxyx(stdscr, max_y, max_x);
max_y -= 1; max_y -= 1;
window.set_dimensions(WindowCoord(max_y, max_x)); window.set_dimensions(DisplayCoord(max_y, max_x));
window.update_display_buffer(); window.update_display_buffer();
WindowCoord position; DisplayCoord position;
for (const DisplayAtom& atom : window.display_buffer()) for (const DisplayAtom& atom : window.display_buffer())
{ {
const std::string content = atom.replacement_text.empty() ? const std::string content = atom.replacement_text.empty() ?
@ -142,7 +142,7 @@ void draw_window(Window& window)
clrtoeol(); clrtoeol();
addstr(status_line.c_str()); 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); move(cursor_position.line, cursor_position.column);
} }
@ -285,7 +285,7 @@ void do_insert(Window& window, IncrementalInserter::Mode mode)
draw_window(window); draw_window(window);
while(true) while(true)
{ {
const WindowCoord& pos = window.cursor_position(); const DisplayCoord& pos = window.cursor_position();
move(pos.line, pos.column); move(pos.line, pos.column);
char c = getch(); char c = getch();
@ -493,15 +493,15 @@ void do_paste(Window& window, int count)
std::unordered_map<char, std::function<void (Window& window, int count)>> keymap = std::unordered_map<char, std::function<void (Window& window, int count)>> keymap =
{ {
{ 'h', [](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(WindowCoord( std::max(count,1), 0)); } }, { 'j', [](Window& window, int count) { window.move_cursor(DisplayCoord( std::max(count,1), 0)); } },
{ 'k', [](Window& window, int count) { window.move_cursor(WindowCoord(-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(WindowCoord(0, std::max(count,1))); } }, { '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); } }, { 'H', [](Window& window, int count) { window.move_cursor(DisplayCoord(0, -std::max(count,1)), true); } },
{ 'J', [](Window& window, int count) { window.move_cursor(WindowCoord( std::max(count,1), 0), true); } }, { 'J', [](Window& window, int count) { window.move_cursor(DisplayCoord( std::max(count,1), 0), true); } },
{ 'K', [](Window& window, int count) { window.move_cursor(WindowCoord(-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(WindowCoord(0, std::max(count,1)), 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)); } }, { '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)); } }, { 'f', [](Window& window, int count) { window.select(std::bind(select_to, _1, getch(), count, true)); } },

View File

@ -121,7 +121,7 @@ void Window::check_invariant() const
assert(not m_selections.empty()); assert(not m_selections.empty());
} }
WindowCoord Window::cursor_position() const DisplayCoord Window::cursor_position() const
{ {
check_invariant(); check_invariant();
return line_and_column_at(m_selections.back().last()); return line_and_column_at(m_selections.back().last());
@ -144,22 +144,29 @@ void Window::erase_noundo()
scroll_to_keep_cursor_visible_ifn(); scroll_to_keep_cursor_visible_ifn();
} }
static WindowCoord measure_string(const Window::String& string) template<typename Iterator>
static DisplayCoord measure_string(Iterator begin, Iterator end)
{ {
WindowCoord result(0, 0); DisplayCoord result(0, 0);
for (size_t i = 0; i < string.length(); ++i) while (begin != end)
{ {
if (string[i] == '\n') if (*begin == '\n')
{ {
++result.line; ++result.line;
result.column = 0; result.column = 0;
} }
else else
++result.column; ++result.column;
++begin;
} }
return result; return result;
} }
static DisplayCoord measure_string(const Window::String& string)
{
return measure_string(string.begin(), string.end());
}
void Window::insert(const String& string) void Window::insert(const String& string)
{ {
scoped_undo_group undo_group(m_buffer); scoped_undo_group undo_group(m_buffer);
@ -199,24 +206,24 @@ bool Window::redo()
return m_buffer.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, return BufferCoord(m_position.line + window_pos.line,
m_position.column + window_pos.column); 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); 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)); 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)); return buffer_to_window(m_buffer.line_and_column_at(iterator));
} }
@ -258,7 +265,7 @@ BufferString Window::selection_content() const
m_selections.back().end()); 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) if (not append)
move_cursor_to(iterator_at(cursor_position() + offset)); 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) 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)); sel = Selection(sel.first(), iterator_at(pos + offset));
} }
scroll_to_keep_cursor_visible_ifn(); 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; m_dimensions = dimensions;
} }
@ -309,7 +316,7 @@ void Window::scroll_to_keep_cursor_visible_ifn()
{ {
check_invariant(); 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) if (cursor.line < 0)
{ {
m_position.line = std::max(m_position.line + 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() void IncrementalInserter::erase()
{ {
move_cursor(WindowCoord(0, -1)); move_cursor(DisplayCoord(0, -1));
m_window.erase_noundo(); 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) 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); BufferIterator it = m_window.iterator_at(pos + offset);
sel = Selection(it, it); sel = Selection(it, it);
} }

View File

@ -3,20 +3,12 @@
#include <functional> #include <functional>
#include "line_and_column.hh"
#include "buffer.hh" #include "buffer.hh"
#include "display_buffer.hh" #include "display_buffer.hh"
namespace Kakoune namespace Kakoune
{ {
struct WindowCoord : LineAndColumn<WindowCoord>
{
WindowCoord(int line = 0, int column = 0)
: LineAndColumn(line, column) {}
};
struct Selection struct Selection
{ {
Selection(const BufferIterator& first, const BufferIterator& last) Selection(const BufferIterator& first, const BufferIterator& last)
@ -50,21 +42,21 @@ public:
void append(const String& string); void append(const String& string);
const BufferCoord& position() const { return m_position; } const BufferCoord& position() const { return m_position; }
WindowCoord cursor_position() const; DisplayCoord cursor_position() const;
Buffer& buffer() const { return m_buffer; } Buffer& buffer() const { return m_buffer; }
BufferIterator iterator_at(const WindowCoord& window_pos) const; BufferIterator iterator_at(const DisplayCoord& window_pos) const;
WindowCoord line_and_column_at(const BufferIterator& iterator) 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 move_cursor_to(const BufferIterator& iterator);
void clear_selections(); void clear_selections();
void select(const Selector& selector, bool append = false); void select(const Selector& selector, bool append = false);
BufferString selection_content() const; 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; } const DisplayBuffer& display_buffer() const { return m_display_buffer; }
@ -88,8 +80,8 @@ private:
void insert_noundo(const String& string); void insert_noundo(const String& string);
void append_noundo(const String& string); void append_noundo(const String& string);
BufferCoord window_to_buffer(const WindowCoord& window_pos) const; BufferCoord window_to_buffer(const DisplayCoord& window_pos) const;
WindowCoord buffer_to_window(const BufferCoord& buffer_pos) const; DisplayCoord buffer_to_window(const BufferCoord& buffer_pos) const;
friend class IncrementalInserter; friend class IncrementalInserter;
IncrementalInserter* m_current_inserter; IncrementalInserter* m_current_inserter;
@ -98,7 +90,7 @@ private:
Buffer& m_buffer; Buffer& m_buffer;
BufferCoord m_position; BufferCoord m_position;
WindowCoord m_dimensions; DisplayCoord m_dimensions;
SelectionList m_selections; SelectionList m_selections;
DisplayBuffer m_display_buffer; DisplayBuffer m_display_buffer;
@ -123,7 +115,7 @@ public:
void insert(const Window::String& string); void insert(const Window::String& string);
void erase(); void erase();
void move_cursor(const WindowCoord& offset); void move_cursor(const DisplayCoord& offset);
private: private:
Window& m_window; Window& m_window;