diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2014-10-06 19:21:32 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2014-10-06 19:21:32 +0100 |
| commit | 17d591b61cc89d9343ad024fdbe76da96f7675c6 (patch) | |
| tree | 13048f0e516aa83d42140407f0c4d6195f789c09 | |
| parent | 844c8f1ec4bfae6ee51fc70b9b6ebb0a4cd894ff (diff) | |
scrolloff is now a line,column pair
Fixes #152
| -rw-r--r-- | src/option_manager.cc | 4 | ||||
| -rw-r--r-- | src/option_types.hh | 17 | ||||
| -rw-r--r-- | 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 <tuple> #include <vector> @@ -171,6 +172,22 @@ bool option_add(T&, const T&) throw runtime_error("no add operation supported for this option type"); } +template<typename EffectiveType, typename LineType, typename ColumnType> +inline void option_from_string(const String& str, LineAndColumn<EffectiveType, LineType, ColumnType>& opt) +{ + auto vals = split(str, '|'); + if (vals.size() != 2) + throw runtime_error("expected <line>|<column>"); + opt.line = str_to_int(vals[0]); + opt.column = str_to_int(vals[1]); +} + +template<typename EffectiveType, typename LineType, typename ColumnType> +inline String option_to_string(const LineAndColumn<EffectiveType, LineType, ColumnType>& 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<LineCount>(options()["scrolloff"].get<int>(), - (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<CharCoord>(), + 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); } |
