scrolloff is now a line,column pair

Fixes #152
This commit is contained in:
Maxime Coste 2014-10-06 19:21:32 +01:00
parent 844c8f1ec4
commit 17d591b61c
3 changed files with 32 additions and 13 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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);
}