diff --git a/src/input_handler.cc b/src/input_handler.cc index 772be7d9..5e246b5a 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -95,12 +95,12 @@ struct MouseHandler case Key::Modifiers::MouseWheelDown: m_dragging = false; - wheel(context, 3); + scroll_window(context, 3); return true; case Key::Modifiers::MouseWheelUp: m_dragging = false; - wheel(context, -3); + scroll_window(context, -3); return true; default: return false; @@ -108,32 +108,6 @@ struct MouseHandler } private: - void wheel(Context& context, LineCount offset) - { - Window& window = context.window(); - Buffer& buffer = context.buffer(); - - CharCoord win_pos = window.position(); - CharCoord win_dim = window.dimensions(); - - const CharCoord max_offset{(win_dim.line - 1)/2, (win_dim.column - 1)/2}; - const CharCoord scrolloff = - std::min(context.options()["scrolloff"].get(), max_offset); - - const LineCount line_count = buffer.line_count(); - win_pos.line = clamp(win_pos.line + offset, 0_line, line_count-1); - - SelectionList& selections = context.selections(); - const ByteCoord cursor = selections.main().cursor(); - - auto clamp_line = [&](LineCount line) { return clamp(line, 0_line, line_count-1); }; - auto min_coord = buffer.offset_coord(clamp_line(win_pos.line + scrolloff.line), win_pos.column); - auto max_coord = buffer.offset_coord(clamp_line(win_pos.line + win_dim.line - 1 - scrolloff.line), win_pos.column); - - selections = SelectionList{buffer, clamp(cursor, min_coord, max_coord)}; - - window.set_position(win_pos); - } bool m_dragging = false; ByteCoord m_anchor; @@ -1459,4 +1433,31 @@ void hide_auto_info_ifn(const Context& context, bool hide) context.client().info_hide(); } +void scroll_window(Context& context, LineCount offset) +{ + Window& window = context.window(); + Buffer& buffer = context.buffer(); + + CharCoord win_pos = window.position(); + CharCoord win_dim = window.dimensions(); + + const CharCoord max_offset{(win_dim.line - 1)/2, (win_dim.column - 1)/2}; + const CharCoord scrolloff = + std::min(context.options()["scrolloff"].get(), max_offset); + + const LineCount line_count = buffer.line_count(); + win_pos.line = clamp(win_pos.line + offset, 0_line, line_count-1); + + SelectionList& selections = context.selections(); + const ByteCoord cursor = selections.main().cursor(); + + auto clamp_line = [&](LineCount line) { return clamp(line, 0_line, line_count-1); }; + auto min_coord = buffer.offset_coord(clamp_line(win_pos.line + scrolloff.line), win_pos.column); + auto max_coord = buffer.offset_coord(clamp_line(win_pos.line + win_dim.line - 1 - scrolloff.line), win_pos.column); + + selections = SelectionList{buffer, clamp(cursor, min_coord, max_coord)}; + + window.set_position(win_pos); +} + } diff --git a/src/input_handler.hh b/src/input_handler.hh index 35603053..c4fcb1a3 100644 --- a/src/input_handler.hh +++ b/src/input_handler.hh @@ -149,6 +149,8 @@ void on_next_key_with_autoinfo(const Context& context, KeymapMode keymap_mode, C }); } +void scroll_window(Context& context, LineCount offset); + } #endif // input_handler_hh_INCLUDED diff --git a/src/normal.cc b/src/normal.cc index 6927c291..c22b47fc 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1027,25 +1027,9 @@ template void scroll(Context& context, NormalParams) { Window& window = context.window(); - Buffer& buffer = context.buffer(); - CharCoord position = window.position(); - LineCount cursor_line = 0; + const LineCount offset = (window.dimensions().line - 2) / (half ? 2 : 1); - if (direction == Backward) - { - position.line -= (window.dimensions().line - 2) / (half ? 2 : 1); - cursor_line = position.line; - } - else if (direction == Forward) - { - position.line += (window.dimensions().line - 2) / (half ? 2 : 1); - cursor_line = position.line + window.dimensions().line - 1; - } - auto cursor_pos = utf8::advance(buffer.iterator_at(position.line), - buffer.iterator_at(position.line+1), - position.column); - select_coord(buffer, cursor_pos.coord(), context.selections()); - window.set_position(position); + scroll_window(context, direction == Direction::Forward ? offset : -offset); } template