gl and gh now go to first or last character of the line

append mode is supported through G key
This commit is contained in:
Maxime Coste 2011-10-10 14:24:17 +00:00
parent 003c5d4e3d
commit 7e84ca9ae9
3 changed files with 45 additions and 22 deletions

View File

@ -322,11 +322,16 @@ void do_insert(Window& window, IncrementalInserter::Mode mode)
window.clear_selections(); window.clear_selections();
} }
template<bool append>
void do_go(Window& window, int count) void do_go(Window& window, int count)
{ {
BufferIterator target;
if (count != 0) if (count != 0)
target = window.buffer().iterator_at({count, 0}); {
BufferIterator target =
window.buffer().iterator_at(BufferCoord(count, 0));
window.move_cursor_to(window.line_and_column_at(target));
}
else else
{ {
char c = getch(); char c = getch();
@ -334,28 +339,29 @@ void do_go(Window& window, int count)
{ {
case 'g': case 'g':
case 't': case 't':
target = window.buffer().iterator_at({0,0}); {
BufferIterator target =
window.buffer().iterator_at(BufferCoord(0,0));
window.move_cursor_to(window.line_and_column_at(target));
break; break;
}
case 'l': case 'l':
target = window.iterator_at(window.cursor_position()); case 'L':
while (not target.is_end() and *target != '\n') window.select(select_to_eol, append);
++target;
--target;
break; break;
case 'h': case 'h':
target = window.iterator_at(window.cursor_position()); case 'H':
while (not target.is_begin() and *target != '\n') window.select(select_to_eol_reverse, append);
--target;
++target;
break; break;
case 'b': case 'b':
target = window.buffer().iterator_at( {
{window.buffer().line_count() - 1, 0}); BufferIterator target = window.buffer().iterator_at(
break; BufferCoord(window.buffer().line_count() - 1, 0));
break;
}
}
window.move_cursor_to(window.line_and_column_at(target)); window.move_cursor_to(window.line_and_column_at(target));
break;
}
}
}
} }
Window* current_window; Window* current_window;
@ -516,7 +522,8 @@ std::unordered_map<char, std::function<void (Window& window, int count)>> keymap
{ 'o', [](Window& window, int count) { do_insert(window, IncrementalInserter::Mode::OpenLineBelow); } }, { 'o', [](Window& window, int count) { do_insert(window, IncrementalInserter::Mode::OpenLineBelow); } },
{ 'O', [](Window& window, int count) { do_insert(window, IncrementalInserter::Mode::OpenLineAbove); } }, { 'O', [](Window& window, int count) { do_insert(window, IncrementalInserter::Mode::OpenLineAbove); } },
{ 'g', do_go }, { 'g', do_go<false> },
{ 'G', do_go<true> },
{ 'y', do_yank }, { 'y', do_yank },
{ 'p', do_paste<true> }, { 'p', do_paste<true> },

View File

@ -223,4 +223,18 @@ Selection select_to_reverse(const BufferIterator& cursor, char c, int count, boo
return Selection(cursor, inclusive ? end : end+1); return Selection(cursor, inclusive ? end : end+1);
} }
Selection select_to_eol(const BufferIterator& cursor)
{
BufferIterator end = cursor + 1;
skip_while(end, [](char cur) { return not is_eol(cur); });
return Selection(cursor, end-1);
}
Selection select_to_eol_reverse(const BufferIterator& cursor)
{
BufferIterator end = cursor - 1;
skip_while_reverse(end, [](char cur) { return not is_eol(cur); });
return Selection(cursor, end.is_begin() ? end : end+1);
}
} }

View File

@ -16,6 +16,8 @@ Selection select_matching(const BufferIterator& cursor);
Selection select_to(const BufferIterator& cursor, char c, int count, bool inclusive); Selection select_to(const BufferIterator& cursor, char c, int count, bool inclusive);
Selection select_to_reverse(const BufferIterator& cursor, char c, int count, bool inclusive); Selection select_to_reverse(const BufferIterator& cursor, char c, int count, bool inclusive);
Selection select_to_eol(const BufferIterator& cursor);
Selection select_to_eol_reverse(const BufferIterator& cursor);
} }
#endif // selectors_hh_INCLUDED #endif // selectors_hh_INCLUDED