From 17d591b61cc89d9343ad024fdbe76da96f7675c6 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 6 Oct 2014 19:21:32 +0100 Subject: [PATCH] scrolloff is now a line,column pair Fixes #152 --- src/option_manager.cc | 4 ++-- src/option_types.hh | 17 +++++++++++++++++ src/window.cc | 24 +++++++++++++----------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/option_manager.cc b/src/option_manager.cc index ce806b27..8b7c2f76 100644 --- a/src/option_manager.cc +++ b/src/option_manager.cc @@ -134,8 +134,8 @@ GlobalOptions::GlobalOptions() declare_option("tabstop", "size of a tab character", 8); declare_option("indentwidth", "indentation width", 4); declare_option("scrolloff", - "number of lines to keep visible main cursor when scrolling", - 0); + "number of lines and columns to keep visible main cursor when scrolling", + CharCoord{0,0}); declare_option("eolformat", "end of line format: 'crlf' or 'lf'", "lf"_str); declare_option("BOM", "insert a byte order mark when writing buffer", "no"_str); diff --git a/src/option_types.hh b/src/option_types.hh index 3d8e7476..e68ddc30 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -4,6 +4,7 @@ #include "exception.hh" #include "string.hh" #include "units.hh" +#include "coord.hh" #include #include @@ -171,6 +172,22 @@ bool option_add(T&, const T&) throw runtime_error("no add operation supported for this option type"); } +template +inline void option_from_string(const String& str, LineAndColumn& opt) +{ + auto vals = split(str, '|'); + if (vals.size() != 2) + throw runtime_error("expected |"); + opt.line = str_to_int(vals[0]); + opt.column = str_to_int(vals[1]); +} + +template +inline String option_to_string(const LineAndColumn& opt) +{ + return to_string(opt.line) + '|' + to_string(opt.column); +} + enum YesNoAsk { Yes, diff --git a/src/window.cc b/src/window.cc index da2b5f06..e76dcc8d 100644 --- a/src/window.cc +++ b/src/window.cc @@ -116,7 +116,7 @@ static LineCount adapt_view_pos(LineCount line, LineCount offset, return view_pos; } -static CharCount adapt_view_pos(const DisplayBuffer& display_buffer, +static CharCount adapt_view_pos(const DisplayBuffer& display_buffer, CharCount offset, ByteCoord pos, CharCount view_pos, CharCount view_size) { CharCount buffer_column = 0; @@ -144,11 +144,11 @@ static CharCount adapt_view_pos(const DisplayBuffer& display_buffer, pos_end = pos_beg + atom.length(); } - if (pos_beg < view_pos) - return pos_beg; + if (pos_beg - offset < view_pos) + return std::max(0_char, pos_beg - offset); - if (pos_end >= view_pos + view_size - non_buffer_column) - return pos_end - view_size + non_buffer_column; + if (pos_end + offset >= view_pos + view_size - non_buffer_column) + return pos_end + offset - view_size + non_buffer_column; } buffer_column += atom.length(); } @@ -165,13 +165,15 @@ void Window::scroll_to_keep_selection_visible_ifn(const Context& context) const auto& anchor = selection.anchor(); const auto& cursor = selection.cursor(); - const LineCount offset = std::min(options()["scrolloff"].get(), - (m_dimensions.line - 1) / 2); + const CharCoord max_offset{(m_dimensions.line - 1)/2, + (m_dimensions.column - 1)/2}; + const CharCoord offset = std::min(options()["scrolloff"].get(), + max_offset); // scroll lines if needed, try to get as much of the selection visible as possible - m_position.line = adapt_view_pos(anchor.line, offset, m_position.line, + m_position.line = adapt_view_pos(anchor.line, offset.line, m_position.line, m_dimensions.line, buffer().line_count()); - m_position.line = adapt_view_pos(cursor.line, offset, m_position.line, + m_position.line = adapt_view_pos(cursor.line, offset.line, m_position.line, m_dimensions.line, buffer().line_count()); // highlight only the line containing the cursor @@ -187,10 +189,10 @@ void Window::scroll_to_keep_selection_visible_ifn(const Context& context) // (this is only valid if highlighting one line and multiple lines put // the cursor in the same position, however I do not find any sane example // of highlighters not doing that) - m_position.column = adapt_view_pos(display_buffer, + m_position.column = adapt_view_pos(display_buffer, offset.column, anchor.line == cursor.line ? anchor : cursor.line, m_position.column, m_dimensions.column); - m_position.column = adapt_view_pos(display_buffer, cursor, + m_position.column = adapt_view_pos(display_buffer, offset.column, cursor, m_position.column, m_dimensions.column); }