From ae75594d252651c4557b621573c2481bee40a137 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 16 Dec 2013 14:01:40 +0000 Subject: [PATCH] Window: refactor center_selection/display_selection_at Take the line we are intersted in as a parameter rather than getting the main selection last char line. --- src/commands.cc | 2 +- src/normal.cc | 9 +++++---- src/window.cc | 13 +++++-------- src/window.hh | 4 ++-- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 7c692745..dc798f86 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -129,7 +129,7 @@ void edit(CommandParameters params, Context& context) context.selections() = context.buffer().clamp({ line, column }); if (context.has_window()) - context.window().center_selection(); + context.window().center_line(context.selections().main().last().line); } } diff --git a/src/normal.cc b/src/normal.cc index 16690f39..175173c0 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -224,7 +224,7 @@ void goto_commands(Context& context, int line) context.push_jump(); select_coord(context.buffer(), LineCount{line - 1}, context.selections()); if (context.has_window()) - context.window().center_selection(); + context.window().center_line(LineCount{line-1}); } else { @@ -331,18 +331,19 @@ void view_commands(Context& context, int param) if (key.modifiers != Key::Modifiers::None or not context.has_window()) return; + LineCount cursor_line = context.selections().main().last().line; Window& window = context.window(); switch (tolower(key.key)) { case 'v': case 'c': - context.window().center_selection(); + context.window().center_line(cursor_line); break; case 't': - context.window().display_selection_at(0); + context.window().display_line_at(cursor_line, 0); break; case 'b': - context.window().display_selection_at(window.dimensions().line-1); + context.window().display_line_at(cursor_line, window.dimensions().line-1); break; case 'h': context.window().scroll(-std::max(1, param)); diff --git a/src/window.cc b/src/window.cc index bd47aea6..ef3e303d 100644 --- a/src/window.cc +++ b/src/window.cc @@ -42,18 +42,15 @@ Window::~Window() m_options.unregister_watcher(*this); } -void Window::display_selection_at(LineCount line) +void Window::display_line_at(LineCount buffer_line, LineCount display_line) { - if (line >= 0 or line < m_dimensions.line) - { - auto cursor_line = selections().main().last().line; - m_position.line = std::max(0_line, cursor_line - line); - } + if (display_line >= 0 or display_line < m_dimensions.line) + m_position.line = std::max(0_line, buffer_line - display_line); } -void Window::center_selection() +void Window::center_line(LineCount buffer_line) { - display_selection_at(m_dimensions.line/2_line); + display_line_at(buffer_line, m_dimensions.line/2_line); } void Window::scroll(LineCount offset) diff --git a/src/window.hh b/src/window.hh index 533f9b98..d95105e6 100644 --- a/src/window.hh +++ b/src/window.hh @@ -33,8 +33,8 @@ public: const DisplayBuffer& display_buffer() const { return m_display_buffer; } - void center_selection(); - void display_selection_at(LineCount line); + void center_line(LineCount buffer_line); + void display_line_at(LineCount buffer_line, LineCount display_line); void scroll(LineCount offset); void scroll(CharCount offset); void update_display_buffer();