diff options
| author | Maxime Coste <mawww@kakoune.org> | 2018-10-01 11:25:53 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2018-10-01 11:25:53 +1000 |
| commit | 551021d6e5360c48c0d08da6464a2ee4081bb3d8 (patch) | |
| tree | 2cd6a3751dafa2c7ad34d6d84d12e0556a4b6f93 /src | |
| parent | 150ee050776f058cbd2ba451d8cb8e12bca73eaa (diff) | |
| parent | 2d44712766081a0b3b4f71ac77f73e973e724b02 (diff) | |
Merge remote-tracking branch 'lenormf/backward-match'
Diffstat (limited to 'src')
| -rw-r--r-- | src/normal.cc | 8 | ||||
| -rw-r--r-- | src/selectors.cc | 17 | ||||
| -rw-r--r-- | src/selectors.hh | 1 |
3 files changed, 22 insertions, 4 deletions
diff --git a/src/normal.cc b/src/normal.cc index eb71ce4a..7a1533ee 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -2185,7 +2185,7 @@ static const HashMap<Key, NormalCmd, MemoryDomain::Undefined, KeymapBackend> key { {';'}, {"reduce selections to their cursor", clear_selections} }, { {alt(';')}, {"swap selections cursor and anchor", flip_selections} }, { {alt(':')}, {"ensure selection cursor is after anchor", ensure_forward} }, - { {alt('m')}, {"merge consecutive selections", merge_consecutive} }, + { {alt('_')}, {"merge consecutive selections", merge_consecutive} }, { {'w'}, {"select to next word start", repeated<&select<SelectMode::Replace, select_to_next_word<Word>>>} }, { {'e'}, {"select to next word end", repeated<select<SelectMode::Replace, select_to_next_word_end<Word>>>} }, @@ -2215,8 +2215,10 @@ static const HashMap<Key, NormalCmd, MemoryDomain::Undefined, KeymapBackend> key { {alt('x')}, {"extend selections to whole lines", select<SelectMode::Replace, select_lines>} }, { {alt('X')}, {"crop selections to whole lines", select<SelectMode::Replace, trim_partial_lines>} }, - { {'m'}, {"select to matching character", select<SelectMode::Replace, select_matching>} }, - { {'M'}, {"extend to matching character", select<SelectMode::Extend, select_matching>} }, + { {'m'}, {"select to matching character", select<SelectMode::Replace, select_matching<true>>} }, + { {alt('m')}, {"backward select to matching character", select<SelectMode::Replace, select_matching<false>>} }, + { {'M'}, {"extend to matching character", select<SelectMode::Extend, select_matching<true>>} }, + { {alt('M')}, {"backward extend to matching character", select<SelectMode::Extend, select_matching<false>>} }, { {'/'}, {"select next given regex match", search<SelectMode::Replace, MatchDirection::Forward>} }, { {'?'}, {"extend with next given regex match", search<SelectMode::Extend, MatchDirection::Forward>} }, diff --git a/src/selectors.cc b/src/selectors.cc index a11a200d..cbbc3f03 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -220,6 +220,7 @@ select_to_first_non_blank(const Context& context, const Selection& selection) return {it.coord()}; } +template<bool forward> Optional<Selection> select_matching(const Context& context, const Selection& selection) { @@ -227,13 +228,23 @@ select_matching(const Context& context, const Selection& selection) auto& matching_pairs = context.options()["matching_pairs"].get<Vector<Codepoint, MemoryDomain::Options>>(); Utf8Iterator it{buffer.iterator_at(selection.cursor()), buffer}; auto match = matching_pairs.end(); - while (it != buffer.end()) + + if (forward) while (it != buffer.end()) { match = find(matching_pairs, *it); if (match != matching_pairs.end()) break; ++it; } + else while (true) + { + match = find(matching_pairs, *it); + if (match != matching_pairs.end() + or it == buffer.begin()) + break; + --it; + } + if (match == matching_pairs.end()) return {}; @@ -271,6 +282,10 @@ select_matching(const Context& context, const Selection& selection) } return {}; } +template Optional<Selection> +select_matching<true>(const Context& context, const Selection& selection); +template Optional<Selection> +select_matching<false>(const Context& context, const Selection& selection); template<typename Iterator, typename Container> Optional<std::pair<Iterator, Iterator>> diff --git a/src/selectors.hh b/src/selectors.hh index 00bc83dd..2e56c214 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -31,6 +31,7 @@ select_to_previous_word(const Context& context, const Selection& selection); Optional<Selection> select_line(const Context& context, const Selection& selection); +template<bool forward> Optional<Selection> select_matching(const Context& context, const Selection& selection); |
