Add scrolloff option support

This commit is contained in:
Maxime Coste 2013-05-16 19:22:44 +02:00
parent 7054dcea8e
commit 400e46719d
2 changed files with 20 additions and 9 deletions

View File

@ -110,6 +110,7 @@ GlobalOptions::GlobalOptions()
{
declare_option<int>("tabstop", 8);
declare_option<int>("indentwidth", 4);
declare_option<int>("scrolloff", 0);
declare_option<String>("eolformat", "lf");
declare_option<String>("BOM", "no");
declare_option<String>("shell", "bash");

View File

@ -98,21 +98,31 @@ void Window::set_dimensions(const DisplayCoord& dimensions)
m_dimensions = dimensions;
}
static LineCount adapt_view_pos(LineCount line, LineCount offset,
LineCount view_pos, LineCount view_size,
LineCount buffer_size)
{
if (line - offset < view_pos)
return std::max(0_line, line - offset);
else if (line + offset >= view_pos + view_size)
return std::min(buffer_size - view_size,
line + offset - (view_size - 1));
return view_pos;
}
void Window::scroll_to_keep_cursor_visible_ifn()
{
const BufferIterator first = main_selection().first();
const BufferIterator last = main_selection().last();
// scroll lines if needed
if (first.line() < m_position.line)
m_position.line = first.line();
else if (first.line() >= m_position.line + m_dimensions.line)
m_position.line = first.line() - (m_dimensions.line - 1);
const LineCount offset = std::min<LineCount>(options()["scrolloff"].get<int>(),
(m_dimensions.line - 1) / 2);
if (last.line() < m_position.line)
m_position.line = last.line();
else if (last.line() >= m_position.line + m_dimensions.line)
m_position.line = last.line() - (m_dimensions.line - 1);
// scroll lines if needed, try to get as much of the selection visible as possible
m_position.line = adapt_view_pos(first.line(), offset, m_position.line,
m_dimensions.line, buffer().line_count());
m_position.line = adapt_view_pos(last.line(), offset, m_position.line,
m_dimensions.line, buffer().line_count());
// highlight only the line containing the cursor
DisplayBuffer display_buffer;