From 287628ec19d42f2ff2323972c2e1e4975b3e146f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 7 Sep 2012 14:29:29 +0200 Subject: [PATCH] Editor::{select,move_cursor} takes a enum SelectMode parameter instead of a boolean --- src/editor.cc | 9 +++--- src/editor.hh | 14 ++++++-- src/main.cc | 90 +++++++++++++++++++++++++-------------------------- 3 files changed, 62 insertions(+), 51 deletions(-) diff --git a/src/editor.cc b/src/editor.cc index 8de99e02..8775a013 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -111,13 +111,14 @@ void Editor::pop_selections() throw runtime_error("no more selections on stack"); } -void Editor::move_selections(const BufferCoord& offset, bool append) +void Editor::move_selections(const BufferCoord& offset, SelectMode mode) { + assert(mode == SelectMode::Replace or mode == SelectMode::Extend); for (auto& sel : m_selections.back()) { BufferCoord pos = m_buffer.line_and_column_at(sel.last()); BufferIterator last = m_buffer.iterator_at(pos + offset, true); - sel = Selection(append ? sel.first() : last, last); + sel = Selection(mode == SelectMode::Extend ? sel.first() : last, last); } } @@ -160,7 +161,7 @@ void Editor::select(const BufferIterator& iterator) m_selections.back().push_back(Selection(iterator, iterator)); } -void Editor::select(const Selector& selector, bool append) +void Editor::select(const Selector& selector, SelectMode mode) { check_invariant(); @@ -170,7 +171,7 @@ void Editor::select(const Selector& selector, bool append) for (auto& sel : m_selections.back()) { SelectionAndCaptures res = selector(sel); - if (append) + if (mode == SelectMode::Extend) sel.merge_with(res.selection); else sel = std::move(res.selection); diff --git a/src/editor.hh b/src/editor.hh index 72e9ef1d..3549b57f 100644 --- a/src/editor.hh +++ b/src/editor.hh @@ -13,6 +13,13 @@ namespace Kakoune class Register; +enum class SelectMode +{ + Replace, + Extend, + Append, +}; + // An Editor is a buffer mutator // // The Editor class provides methods to manipulate a set of selections @@ -23,6 +30,7 @@ public: typedef std::function Selector; typedef std::function MultiSelector; + Editor(Buffer& buffer); virtual ~Editor() {} @@ -42,12 +50,14 @@ public: void push_selections(); void pop_selections(); - void move_selections(const BufferCoord& offset, bool append = false); + void move_selections(const BufferCoord& offset, + SelectMode mode = SelectMode::Replace); void clear_selections(); void keep_selection(int index); void remove_selection(int index); void select(const BufferIterator& iterator); - void select(const Selector& selector, bool append = false); + void select(const Selector& selector, + SelectMode mode = SelectMode::Replace); void multi_select(const MultiSelector& selector); const SelectionList& selections() const { return m_selections.back(); } diff --git a/src/main.cc b/src/main.cc index aa4a828f..90114c91 100644 --- a/src/main.cc +++ b/src/main.cc @@ -43,7 +43,7 @@ void do_repeat_insert(Context& context) context.client().repeat_last_insert(context.editor(), context); } -template +template void do_go(Context& context) { int count = context.numeric_param(); @@ -70,11 +70,11 @@ void do_go(Context& context) break; case 'l': case 'L': - editor.select(select_to_eol, append); + editor.select(select_to_eol, mode); break; case 'h': case 'H': - editor.select(select_to_eol_reverse, append); + editor.select(select_to_eol_reverse, mode); break; case 'b': { @@ -108,7 +108,7 @@ void do_pipe(Context& context) } -template +template void do_search(Context& context) { context.client().prompt("/", complete_nothing, @@ -119,16 +119,16 @@ void do_search(Context& context) else RegisterManager::instance()['/'] = ex; - context.editor().select(std::bind(select_next_match, _1, ex), append); + context.editor().select(std::bind(select_next_match, _1, ex), mode); }); } -template +template void do_search_next(Context& context) { const String& ex = RegisterManager::instance()['/'].values(context)[0]; if (not ex.empty()) - context.editor().select(std::bind(select_next_match, _1, ex), append); + context.editor().select(std::bind(select_next_match, _1, ex), mode); else context.print_status("no search pattern"); } @@ -201,7 +201,7 @@ void do_join(Context& context) { Editor& editor = context.editor(); editor.select(select_whole_lines); - editor.select(select_to_eol, true); + editor.select(select_to_eol, SelectMode::Extend); editor.multi_select(std::bind(select_all_matches, _1, "\n\\h*")); editor.replace(" "); editor.clear_selections(); @@ -254,14 +254,14 @@ private: template Repeated repeated(T func) { return Repeated(func); } -namespace SelectMode +namespace SelectFlags { - enum Flags + enum Type { None = 0, Reverse = 1, Inclusive = 2, - Append = 4 + Extend = 4 }; } @@ -271,9 +271,9 @@ void select_to_next_char(Context& context) int param = context.numeric_param(); context.client().on_next_key([param](const Key& key, Context& context) { context.editor().select( - std::bind(flags & SelectMode::Reverse ? select_to_reverse : select_to, - _1, key.key, param, flags & SelectMode::Inclusive), - flags & SelectMode::Append); + std::bind(flags & SelectFlags::Reverse ? select_to_reverse : select_to, + _1, key.key, param, flags & SelectFlags::Inclusive), + flags & SelectFlags::Extend ? SelectMode::Extend : SelectMode::Replace); }); } @@ -284,19 +284,19 @@ std::unordered_map> keymap = { { Key::Modifiers::None, 'k' }, [](Context& context) { context.editor().move_selections({ -std::max(context.numeric_param(),1), 0 }); } }, { { Key::Modifiers::None, 'l' }, [](Context& context) { context.editor().move_selections({ 0, std::max(context.numeric_param(),1) }); } }, - { { Key::Modifiers::None, 'H' }, [](Context& context) { context.editor().move_selections({ 0, -std::max(context.numeric_param(),1) }, true); } }, - { { Key::Modifiers::None, 'J' }, [](Context& context) { context.editor().move_selections({ std::max(context.numeric_param(),1), 0 }, true); } }, - { { Key::Modifiers::None, 'K' }, [](Context& context) { context.editor().move_selections({ -std::max(context.numeric_param(),1), 0 }, true); } }, - { { Key::Modifiers::None, 'L' }, [](Context& context) { context.editor().move_selections({ 0, std::max(context.numeric_param(),1) }, true); } }, + { { Key::Modifiers::None, 'H' }, [](Context& context) { context.editor().move_selections({ 0, -std::max(context.numeric_param(),1) }, SelectMode::Extend); } }, + { { Key::Modifiers::None, 'J' }, [](Context& context) { context.editor().move_selections({ std::max(context.numeric_param(),1), 0 }, SelectMode::Extend); } }, + { { Key::Modifiers::None, 'K' }, [](Context& context) { context.editor().move_selections({ -std::max(context.numeric_param(),1), 0 }, SelectMode::Extend); } }, + { { Key::Modifiers::None, 'L' }, [](Context& context) { context.editor().move_selections({ 0, std::max(context.numeric_param(),1) }, SelectMode::Extend); } }, - { { Key::Modifiers::None, 't' }, select_to_next_char }, - { { Key::Modifiers::None, 'f' }, select_to_next_char }, - { { Key::Modifiers::None, 'T' }, select_to_next_char }, - { { Key::Modifiers::None, 'F' }, select_to_next_char }, - { { Key::Modifiers::Alt, 't' }, select_to_next_char }, - { { Key::Modifiers::Alt, 'f' }, select_to_next_char }, - { { Key::Modifiers::Alt, 'T' }, select_to_next_char }, - { { Key::Modifiers::Alt, 'F' }, select_to_next_char }, + { { Key::Modifiers::None, 't' }, select_to_next_char }, + { { Key::Modifiers::None, 'f' }, select_to_next_char }, + { { Key::Modifiers::None, 'T' }, select_to_next_char }, + { { Key::Modifiers::None, 'F' }, select_to_next_char }, + { { Key::Modifiers::Alt, 't' }, select_to_next_char }, + { { Key::Modifiers::Alt, 'f' }, select_to_next_char }, + { { Key::Modifiers::Alt, 'T' }, select_to_next_char }, + { { Key::Modifiers::Alt, 'F' }, select_to_next_char }, { { Key::Modifiers::None, 'd' }, do_erase }, { { Key::Modifiers::None, 'c' }, do_change }, @@ -307,8 +307,8 @@ std::unordered_map> keymap = { { Key::Modifiers::None, 'o' }, do_insert }, { { Key::Modifiers::None, 'O' }, do_insert }, - { { Key::Modifiers::None, 'g' }, do_go }, - { { Key::Modifiers::None, 'G' }, do_go }, + { { Key::Modifiers::None, 'g' }, do_go }, + { { Key::Modifiers::None, 'G' }, do_go }, { { Key::Modifiers::None, 'y' }, do_yank }, { { Key::Modifiers::None, 'p' }, do_paste }, @@ -332,18 +332,18 @@ std::unordered_map> keymap = { { Key::Modifiers::None, 'w' }, repeated([](Context& context) { context.editor().select(select_to_next_word); }) }, { { Key::Modifiers::None, 'e' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end); }) }, { { Key::Modifiers::None, 'b' }, repeated([](Context& context) { context.editor().select(select_to_previous_word); }) }, - { { Key::Modifiers::None, 'W' }, repeated([](Context& context) { context.editor().select(select_to_next_word, true); }) }, - { { Key::Modifiers::None, 'E' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end, true); }) }, - { { Key::Modifiers::None, 'B' }, repeated([](Context& context) { context.editor().select(select_to_previous_word, true); }) }, - { { Key::Modifiers::None, 'x' }, repeated([](Context& context) { context.editor().select(select_line, false); }) }, - { { Key::Modifiers::None, 'X' }, repeated([](Context& context) { context.editor().select(select_line, true); }) }, + { { Key::Modifiers::None, 'W' }, repeated([](Context& context) { context.editor().select(select_to_next_word, SelectMode::Extend); }) }, + { { Key::Modifiers::None, 'E' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end, SelectMode::Extend); }) }, + { { Key::Modifiers::None, 'B' }, repeated([](Context& context) { context.editor().select(select_to_previous_word, SelectMode::Extend); }) }, + { { Key::Modifiers::None, 'x' }, repeated([](Context& context) { context.editor().select(select_line, SelectMode::Replace); }) }, + { { Key::Modifiers::None, 'X' }, repeated([](Context& context) { context.editor().select(select_line, SelectMode::Extend); }) }, { { Key::Modifiers::None, 'm' }, [](Context& context) { context.editor().select(select_matching); } }, - { { Key::Modifiers::None, 'M' }, [](Context& context) { context.editor().select(select_matching, true); } }, + { { Key::Modifiers::None, 'M' }, [](Context& context) { context.editor().select(select_matching, SelectMode::Extend); } }, - { { Key::Modifiers::None, '/' }, do_search }, - { { Key::Modifiers::None, '?' }, do_search }, - { { Key::Modifiers::None, 'n' }, do_search_next }, - { { Key::Modifiers::None, 'N' }, do_search_next }, + { { Key::Modifiers::None, '/' }, do_search }, + { { Key::Modifiers::None, '?' }, do_search }, + { { Key::Modifiers::None, 'n' }, do_search_next }, + { { Key::Modifiers::None, 'N' }, do_search_next }, { { Key::Modifiers::None, 'u' }, repeated([](Context& context) { if (not context.editor().undo()) { context.print_status("nothing left to undo"); } }) }, { { Key::Modifiers::None, 'U' }, repeated([](Context& context) { if (not context.editor().redo()) { context.print_status("nothing left to redo"); } }) }, @@ -354,14 +354,14 @@ std::unordered_map> keymap = { { Key::Modifiers::Alt, 'w' }, repeated([](Context& context) { context.editor().select(select_to_next_word); }) }, { { Key::Modifiers::Alt, 'e' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end); }) }, { { Key::Modifiers::Alt, 'b' }, repeated([](Context& context) { context.editor().select(select_to_previous_word); }) }, - { { Key::Modifiers::Alt, 'W' }, repeated([](Context& context) { context.editor().select(select_to_next_word, true); }) }, - { { Key::Modifiers::Alt, 'E' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end, true); }) }, - { { Key::Modifiers::Alt, 'B' }, repeated([](Context& context) { context.editor().select(select_to_previous_word, true); }) }, + { { Key::Modifiers::Alt, 'W' }, repeated([](Context& context) { context.editor().select(select_to_next_word, SelectMode::Extend); }) }, + { { Key::Modifiers::Alt, 'E' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end, SelectMode::Extend); }) }, + { { Key::Modifiers::Alt, 'B' }, repeated([](Context& context) { context.editor().select(select_to_previous_word, SelectMode::Extend); }) }, - { { Key::Modifiers::Alt, 'l' }, repeated([](Context& context) { context.editor().select(select_to_eol, false); }) }, - { { Key::Modifiers::Alt, 'L' }, repeated([](Context& context) { context.editor().select(select_to_eol, true); }) }, - { { Key::Modifiers::Alt, 'h' }, repeated([](Context& context) { context.editor().select(select_to_eol_reverse, false); }) }, - { { Key::Modifiers::Alt, 'H' }, repeated([](Context& context) { context.editor().select(select_to_eol_reverse, true); }) }, + { { Key::Modifiers::Alt, 'l' }, repeated([](Context& context) { context.editor().select(select_to_eol, SelectMode::Replace); }) }, + { { Key::Modifiers::Alt, 'L' }, repeated([](Context& context) { context.editor().select(select_to_eol, SelectMode::Extend); }) }, + { { Key::Modifiers::Alt, 'h' }, repeated([](Context& context) { context.editor().select(select_to_eol_reverse, SelectMode::Replace); }) }, + { { Key::Modifiers::Alt, 'H' }, repeated([](Context& context) { context.editor().select(select_to_eol_reverse, SelectMode::Extend); }) }, { { Key::Modifiers::Alt, 's' }, do_split_regex },