diff --git a/src/main.cc b/src/main.cc index 9d9abda2f..e3a21e27a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -346,12 +346,30 @@ void do_search(Window& window) catch (prompt_aborted&) {} } +Selection move_select(Window& window, const BufferIterator& cursor, const WindowCoord& offset) +{ + WindowCoord cursor_pos = window.line_and_column_at(cursor); + WindowCoord new_pos = cursor_pos + offset; + + return Selection(cursor, window.iterator_at(new_pos)); +} + std::unordered_map> keymap = { - { 'h', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(WindowCoord(0, -count)); window.empty_selections(); } }, - { 'j', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(WindowCoord(count, 0)); window.empty_selections(); } }, - { 'k', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(WindowCoord(-count, 0)); window.empty_selections(); } }, - { 'l', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(WindowCoord(0, count)); window.empty_selections(); } }, + { 'h', [](Window& window, int count) { window.move_cursor(WindowCoord(0, -std::max(count,1))); window.empty_selections(); } }, + { 'j', [](Window& window, int count) { window.move_cursor(WindowCoord( std::max(count,1), 0)); window.empty_selections(); } }, + { 'k', [](Window& window, int count) { window.move_cursor(WindowCoord(-std::max(count,1), 0)); window.empty_selections(); } }, + { 'l', [](Window& window, int count) { window.move_cursor(WindowCoord(0, std::max(count,1))); window.empty_selections(); } }, + + { 'H', [](Window& window, int count) { window.select(true, std::bind(move_select, std::ref(window), std::placeholders::_1, + WindowCoord(0, -std::max(count,1)))); } }, + { 'J', [](Window& window, int count) { window.select(true, std::bind(move_select, std::ref(window), std::placeholders::_1, + WindowCoord( std::max(count,1), 0))); } }, + { 'K', [](Window& window, int count) { window.select(true, std::bind(move_select, std::ref(window), std::placeholders::_1, + WindowCoord(-std::max(count,1), 0))); } }, + { 'L', [](Window& window, int count) { window.select(true, std::bind(move_select, std::ref(window), std::placeholders::_1, + WindowCoord(0, std::max(count,1)))); } }, + { 'd', [](Window& window, int count) { window.erase(); window.empty_selections(); } }, { 'c', [](Window& window, int count) { window.erase(); do_insert(window); } }, { 'i', [](Window& window, int count) { do_insert(window); } }, diff --git a/src/window.cc b/src/window.cc index 926258b9e..feb379d83 100644 --- a/src/window.cc +++ b/src/window.cc @@ -137,9 +137,7 @@ void Window::select(bool append, const Selector& selector) void Window::move_cursor(const WindowCoord& offset) { - BufferCoord target_position = - window_to_buffer(WindowCoord(m_cursor.line + offset.line, - m_cursor.column + offset.column)); + BufferCoord target_position = window_to_buffer(m_cursor + offset); m_cursor = buffer_to_window(m_buffer.clamp(target_position));