diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2012-09-07 21:09:23 +0200 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2012-09-07 21:09:23 +0200 |
| commit | 46565723b1f146af461f93dd45a9c25d43ac553d (patch) | |
| tree | 6181587136e6b6d4e7d4629a800b3a51064a35ae /src | |
| parent | 499bb77491858d34b7278e91fd4ac69f9aa3855d (diff) | |
Add support for page up and page down
Diffstat (limited to 'src')
| -rw-r--r-- | src/keys.hh | 4 | ||||
| -rw-r--r-- | src/main.cc | 28 | ||||
| -rw-r--r-- | src/ncurses.cc | 2 | ||||
| -rw-r--r-- | src/window.hh | 2 |
4 files changed, 35 insertions, 1 deletions
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<Key::NamedKey key> +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<typename T> class Repeated { @@ -370,6 +395,9 @@ std::unordered_map<Key, std::function<void (Context& context)>> 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::PageUp> }, + { { Key::Modifiers::None, Key::PageDown }, do_scroll<Key::PageDown> }, }; } 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; } |
