diff --git a/src/keys.hh b/src/keys.hh index 9f13e04a..cb3acc67 100644 --- a/src/keys.hh +++ b/src/keys.hh @@ -16,7 +16,7 @@ struct Key Alt = 2, ControlAlt = 3 }; - enum NamedKeys : Character + enum NamedKey : Character { Backspace = 256, Escape, @@ -24,6 +24,8 @@ struct Key Down, Left, Right, + PageUp, + PageDown, }; Modifiers modifiers; diff --git a/src/main.cc b/src/main.cc index 90114c91..5574327e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -236,6 +236,31 @@ void do_select_object(Context& context) }); } +template +void do_scroll(Context& context) +{ + static_assert(key == Key::PageUp or key == Key::PageDown, + "do_scrool only implements PageUp and PageDown"); + Window& window = context.window(); + Buffer& buffer = context.buffer(); + BufferCoord position = window.position(); + BufferIterator cursor_pos; + + if (key == Key::PageUp) + { + position.line -= (window.dimensions().line - 2); + cursor_pos = buffer.iterator_at(position); + } + else if (key == Key::PageDown) + { + position.line += (window.dimensions().line - 2); + cursor_pos = buffer.iterator_at(position + BufferCoord{window.dimensions().line - 1, 0}); + } + + window.select(cursor_pos); + window.set_position(position); +} + template class Repeated { @@ -370,6 +395,9 @@ std::unordered_map> keymap = { { Key::Modifiers::Alt, 'x' }, [](Context& context) { context.editor().select(select_whole_lines); } }, { { Key::Modifiers::Alt, 'c' }, [](Context& context) { if (context.has_window()) context.window().center_selection(); } }, + + { { Key::Modifiers::None, Key::PageUp }, do_scroll }, + { { Key::Modifiers::None, Key::PageDown }, do_scroll }, }; } diff --git a/src/ncurses.cc b/src/ncurses.cc index 51c17655..ec435811 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -187,6 +187,8 @@ Key NCursesClient::get_key() case KEY_DOWN: return Key::Down; case KEY_LEFT: return Key::Left; case KEY_RIGHT: return Key::Right; + case KEY_PPAGE: return Key::PageUp; + case KEY_NPAGE: return Key::PageDown; } return c; } diff --git a/src/window.hh b/src/window.hh index fd0598e4..f8e2ef3c 100644 --- a/src/window.hh +++ b/src/window.hh @@ -25,7 +25,9 @@ public: ~Window(); const BufferCoord& position() const { return m_position; } + void set_position(const BufferCoord& position) { m_position = buffer().clamp(position); } + const DisplayCoord& dimensions() const { return m_dimensions; } void set_dimensions(const DisplayCoord& dimensions); const DisplayBuffer& display_buffer() const { return m_display_buffer; }