Add CTRL+u,d,f,b,e,y keys to scroll window by different amounts like vim

This commit is contained in:
Josh Holtrop 2017-01-02 11:49:18 -05:00
parent 842557ee46
commit 647b3b85f4
4 changed files with 55 additions and 8 deletions

View File

@ -608,9 +608,29 @@ void BufferPane::enter_insert_mode(Window::EnterInsertModeMode which)
m_window->request_redraw(); m_window->request_redraw();
} }
void BufferPane::scroll_window_up() int BufferPane::calculate_lines_to_scroll(Window::ScrollMode scroll_mode)
{ {
const int lines_to_scroll = 3; switch (scroll_mode)
{
case Window::ScrollMode::ONE_LINE:
return 1;
break;
case Window::ScrollMode::WHEEL:
return 3;
break;
case Window::ScrollMode::HALF_SCREEN:
return m_rows / 2;
break;
case Window::ScrollMode::WHOLE_SCREEN:
return m_rows - 1;
break;
}
return 0;
}
void BufferPane::scroll_window_up(Window::ScrollMode scroll_mode)
{
int lines_to_scroll = calculate_lines_to_scroll(scroll_mode);
int so = effective_scroll_offset(); int so = effective_scroll_offset();
int lines_to_move_cursor = (so + lines_to_scroll) - (m_rows - m_cursor_screen_row - 1); int lines_to_move_cursor = (so + lines_to_scroll) - (m_rows - m_cursor_screen_row - 1);
while (lines_to_move_cursor-- > 0) while (lines_to_move_cursor-- > 0)
@ -621,9 +641,9 @@ void BufferPane::scroll_window_up()
m_window->request_redraw(); m_window->request_redraw();
} }
void BufferPane::scroll_window_down() void BufferPane::scroll_window_down(Window::ScrollMode scroll_mode)
{ {
const int lines_to_scroll = 3; int lines_to_scroll = calculate_lines_to_scroll(scroll_mode);
int so = effective_scroll_offset(); int so = effective_scroll_offset();
int lines_to_move_cursor = (so + lines_to_scroll) - m_cursor_screen_row; int lines_to_move_cursor = (so + lines_to_scroll) - m_cursor_screen_row;
while (lines_to_move_cursor-- > 0) while (lines_to_move_cursor-- > 0)

View File

@ -21,8 +21,8 @@ public:
void kill_character_at_cursor(); void kill_character_at_cursor();
void write_file(); void write_file();
bool insert_mode() const { return m_buffer->insert_mode(); } bool insert_mode() const { return m_buffer->insert_mode(); }
void scroll_window_up(); void scroll_window_up(Window::ScrollMode scroll_mode);
void scroll_window_down(); void scroll_window_down(Window::ScrollMode scroll_mode);
protected: protected:
int effective_scroll_offset() int effective_scroll_offset()
@ -56,6 +56,7 @@ protected:
size_t display_line() const { return m_iterator->line() + 1u; } size_t display_line() const { return m_iterator->line() + 1u; }
size_t display_column() const; size_t display_column() const;
void draw_status_bar(); void draw_status_bar();
int calculate_lines_to_scroll(Window::ScrollMode scroll_mode);
Window * m_window; Window * m_window;
std::shared_ptr<Buffer> m_buffer; std::shared_ptr<Buffer> m_buffer;

View File

@ -220,11 +220,11 @@ void Window::handle_event(SDL_Event & event)
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
if (event.wheel.y > 0) if (event.wheel.y > 0)
{ {
m_buffer_pane->scroll_window_up(); m_buffer_pane->scroll_window_up(ScrollMode::WHEEL);
} }
else else
{ {
m_buffer_pane->scroll_window_down(); m_buffer_pane->scroll_window_down(ScrollMode::WHEEL);
} }
break; break;
} }
@ -297,9 +297,27 @@ void Window::handle_keyval(uint32_t keyval)
case 'x': case 'x':
m_buffer_pane->kill_character_at_cursor(); m_buffer_pane->kill_character_at_cursor();
break; break;
case Keymod::CTRL + 'b':
m_buffer_pane->scroll_window_up(ScrollMode::WHOLE_SCREEN);
break;
case Keymod::CTRL + 'd':
m_buffer_pane->scroll_window_down(ScrollMode::HALF_SCREEN);
break;
case Keymod::CTRL + 'e':
m_buffer_pane->scroll_window_down(ScrollMode::ONE_LINE);
break;
case Keymod::CTRL + 'f':
m_buffer_pane->scroll_window_down(ScrollMode::WHOLE_SCREEN);
break;
case Keymod::CTRL + 'u':
m_buffer_pane->scroll_window_up(ScrollMode::HALF_SCREEN);
break;
case Keymod::CTRL + 'w': case Keymod::CTRL + 'w':
m_buffer_pane->write_file(); m_buffer_pane->write_file();
break; break;
case Keymod::CTRL + 'y':
m_buffer_pane->scroll_window_up(ScrollMode::ONE_LINE);
break;
case Keymod::CTRL + 'q': case Keymod::CTRL + 'q':
m_exit_requested = true; m_exit_requested = true;
break; break;

View File

@ -39,6 +39,14 @@ public:
SCREEN_ROW_DOWN, SCREEN_ROW_DOWN,
}; };
enum class ScrollMode : uint8_t
{
ONE_LINE,
WHEEL,
HALF_SCREEN,
WHOLE_SCREEN,
};
enum class EnterInsertModeMode : uint8_t enum class EnterInsertModeMode : uint8_t
{ {
START_OF_CHAR, START_OF_CHAR,