From 37807ba19bbdf41098fceb56642f3fc24cc6fb46 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 2 Jul 2013 13:55:34 +0100 Subject: [PATCH] Add support for arrow keys in normal mode use a Direction enum instead of a bool forward parameter --- src/normal.cc | 67 ++++++++++++++++++++++++++++-------------------- src/selectors.cc | 14 +++++----- src/selectors.hh | 4 ++- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/normal.cc b/src/normal.cc index 45cc8638..1b2120f5 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -258,10 +258,10 @@ void pipe(Context& context) }); } -template +template void search(Context& context) { - const char* prompt = forward ? "search:" : "reverse search:"; + const char* prompt = direction == Forward ? "search:" : "reverse search:"; DynamicSelectionList selections{context.buffer(), context.editor().selections()}; context.input_handler().prompt(prompt, get_color("Prompt"), complete_nothing, [selections](const String& str, PromptEvent event, Context& context) { @@ -285,7 +285,7 @@ void search(Context& context) else if (str.empty() or not context.options()["incsearch"].get()) return; - context.editor().select(std::bind(select_next_match, _1, _2, ex), mode); + context.editor().select(std::bind(select_next_match, _1, _2, ex), mode); } catch (boost::regex_error& err) { @@ -305,7 +305,7 @@ void search(Context& context) }); } -template +template void search_next(Context& context) { const String& str = RegisterManager::instance()['/'].values(context)[0]; @@ -318,7 +318,7 @@ void search_next(Context& context) context.push_jump(); int count = context.numeric_param(); do { - context.editor().select(std::bind(select_next_match, _1, _2, ex), mode); + context.editor().select(std::bind(select_next_match, _1, _2, ex), mode); } while (--count > 0); } catch (boost::regex_error& err) @@ -684,11 +684,10 @@ void replay_macro(Context& context) }); } -enum class JumpDirection { Forward, Backward }; -template +template void jump(Context& context) { - auto jump = (direction == JumpDirection::Forward) ? + auto jump = (direction == Forward) ? context.jump_forward() : context.jump_backward(); Buffer& buffer = const_cast(jump.buffer()); @@ -755,17 +754,24 @@ private: template constexpr Select select(T func) { return Select(func); } +template +void move(Context& context) +{ + Type offset(std::max(context.numeric_param(),1)); + context.editor().move_selections(direction == Backward ? -offset : offset, mode); +} + KeyMap keymap = { - { { Key::Modifiers::None, 'h' }, [](Context& context) { context.editor().move_selections(-CharCount(std::max(context.numeric_param(),1))); } }, - { { Key::Modifiers::None, 'j' }, [](Context& context) { context.editor().move_selections( LineCount(std::max(context.numeric_param(),1))); } }, - { { Key::Modifiers::None, 'k' }, [](Context& context) { context.editor().move_selections(-LineCount(std::max(context.numeric_param(),1))); } }, - { { Key::Modifiers::None, 'l' }, [](Context& context) { context.editor().move_selections( CharCount(std::max(context.numeric_param(),1))); } }, + { { Key::Modifiers::None, 'h' }, move }, + { { Key::Modifiers::None, 'j' }, move }, + { { Key::Modifiers::None, 'k' }, move }, + { { Key::Modifiers::None, 'l' }, move }, - { { Key::Modifiers::None, 'H' }, [](Context& context) { context.editor().move_selections(-CharCount(std::max(context.numeric_param(),1)), SelectMode::Extend); } }, - { { Key::Modifiers::None, 'J' }, [](Context& context) { context.editor().move_selections( LineCount(std::max(context.numeric_param(),1)), SelectMode::Extend); } }, - { { Key::Modifiers::None, 'K' }, [](Context& context) { context.editor().move_selections(-LineCount(std::max(context.numeric_param(),1)), SelectMode::Extend); } }, - { { Key::Modifiers::None, 'L' }, [](Context& context) { context.editor().move_selections( CharCount(std::max(context.numeric_param(),1)), SelectMode::Extend); } }, + { { Key::Modifiers::None, 'H' }, move }, + { { Key::Modifiers::None, 'J' }, move }, + { { Key::Modifiers::None, 'K' }, move }, + { { Key::Modifiers::None, 'L' }, move }, { { Key::Modifiers::None, 't' }, select_to_next_char }, { { Key::Modifiers::None, 'f' }, select_to_next_char }, @@ -840,13 +846,13 @@ KeyMap keymap = { { Key::Modifiers::None, 'm' }, select(select_matching) }, { { Key::Modifiers::None, 'M' }, select(select_matching) }, - { { Key::Modifiers::None, '/' }, search }, - { { Key::Modifiers::None, '?' }, search }, - { { Key::Modifiers::Alt, '/' }, search }, - { { Key::Modifiers::Alt, '?' }, search }, - { { Key::Modifiers::None, 'n' }, search_next }, - { { Key::Modifiers::Alt, 'n' }, search_next }, - { { Key::Modifiers::None, 'N' }, search_next }, + { { Key::Modifiers::None, '/' }, search }, + { { Key::Modifiers::None, '?' }, search }, + { { Key::Modifiers::Alt, '/' }, search }, + { { Key::Modifiers::Alt, '?' }, search }, + { { Key::Modifiers::None, 'n' }, search_next }, + { { Key::Modifiers::Alt, 'n' }, search_next }, + { { Key::Modifiers::None, 'N' }, search_next }, { { Key::Modifiers::None, '*' }, use_selection_as_search_pattern }, { { Key::Modifiers::Alt, '*' }, use_selection_as_search_pattern }, @@ -867,11 +873,8 @@ KeyMap keymap = { { Key::Modifiers::None, '<' }, deindent }, { { Key::Modifiers::None, '>' }, indent }, - { { Key::Modifiers::None, Key::PageUp }, scroll }, - { { Key::Modifiers::None, Key::PageDown }, scroll }, - - { { Key::Modifiers::Control, 'i' }, jump }, - { { Key::Modifiers::Control, 'o' }, jump }, + { { Key::Modifiers::Control, 'i' }, jump }, + { { Key::Modifiers::Control, 'o' }, jump }, { { Key::Modifiers::Alt, 'r' }, rotate_selections }, @@ -880,6 +883,14 @@ KeyMap keymap = { { Key::Modifiers::None, '~' }, swap_case }, { { Key::Modifiers::None, '&' }, align }, + + { Key::Left, move }, + { Key::Down, move }, + { Key::Up, move }, + { Key::Right, move }, + + { Key::PageUp, scroll }, + { Key::PageDown, scroll }, }; } diff --git a/src/selectors.cc b/src/selectors.cc index 2d015b55..946c5761 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -551,11 +551,11 @@ static bool find_last_match(BufferIterator begin, const BufferIterator& end, return not res.empty(); } -template +template bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos, MatchResults& matches, const Regex& ex) { - if (forward) + if (direction == Forward) return (boost::regex_search(pos, buffer.end(), matches, ex) or boost::regex_search(buffer.begin(), pos, matches, ex)); else @@ -564,7 +564,7 @@ bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos, } -template +template Selection select_next_match(const Buffer& buffer, const Selection& selection, const Regex& regex) { // regex matching do not use Utf8Iterator as boost::regex handle utf8 @@ -575,7 +575,7 @@ Selection select_next_match(const Buffer& buffer, const Selection& selection, co MatchResults matches; - if (find_match_in_buffer(buffer, utf8::next(begin), matches, regex)) + if (find_match_in_buffer(buffer, utf8::next(begin), matches, regex)) { begin = matches[0].first; end = matches[0].second; @@ -589,12 +589,12 @@ Selection select_next_match(const Buffer& buffer, const Selection& selection, co ++end; end = utf8::previous(end); - if (not forward) + if (direction == Backward) std::swap(begin, end); return Selection{begin.coord(), end.coord(), std::move(captures)}; } -template Selection select_next_match(const Buffer&, const Selection&, const Regex&); -template Selection select_next_match(const Buffer&, const Selection&, const Regex&); +template Selection select_next_match(const Buffer&, const Selection&, const Regex&); +template Selection select_next_match(const Buffer&, const Selection&, const Regex&); SelectionList select_all_matches(const Buffer& buffer, const Selection& selection, const Regex& regex) { diff --git a/src/selectors.hh b/src/selectors.hh index 78d3bfac..33d1dee1 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -52,7 +52,9 @@ Selection select_whole_lines(const Buffer& buffer, const Selection& selection); Selection select_whole_buffer(const Buffer& buffer, const Selection& selection); Selection trim_partial_lines(const Buffer& buffer, const Selection& selection); -template +enum Direction { Forward, Backward }; + +template Selection select_next_match(const Buffer& buffer, const Selection& selection, const Regex& regex);