Merge remote-tracking branch 'lenormf/backward-match'

This commit is contained in:
Maxime Coste 2018-10-01 11:25:53 +10:00
commit 551021d6e5
4 changed files with 35 additions and 5 deletions

View File

@ -139,6 +139,18 @@ the Shift modifier and moving will extend each selection instead.
select to the next sequence enclosed by matching character, see the select to the next sequence enclosed by matching character, see the
`matching_pairs` option in <<options#,`:doc options`>> `matching_pairs` option in <<options#,`:doc options`>>
*M*::
extend the current selection to the next sequence enclosed by matching
character, see the `matching_pairs` option in <<options#,`:doc options`>>
*<a-m>*::
select to the previous sequence enclosed by matching character, see the
`matching_pairs` option in <<options#,`:doc options`>>
*<a-M>*::
extend the current selection to the previous sequence enclosed by matching
character, see the `matching_pairs` option in <<options#,`:doc options`>>
*x*:: *x*::
select line on which the end of each selection lies (or next line when end lies select line on which the end of each selection lies (or next line when end lies
on an end-of-line) on an end-of-line)
@ -258,7 +270,7 @@ Yanking (copying) and pasting use the *"* register by default (See <<registers#,
*<a-J>*:: *<a-J>*::
join selected lines and select spaces inserted in place of line breaks join selected lines and select spaces inserted in place of line breaks
*<a-m>*:: *<a-_>*::
merge contiguous selections together (works across lines as well) merge contiguous selections together (works across lines as well)
*>*:: *>*::

View File

@ -2185,7 +2185,7 @@ static const HashMap<Key, NormalCmd, MemoryDomain::Undefined, KeymapBackend> key
{ {';'}, {"reduce selections to their cursor", clear_selections} }, { {';'}, {"reduce selections to their cursor", clear_selections} },
{ {alt(';')}, {"swap selections cursor and anchor", flip_selections} }, { {alt(';')}, {"swap selections cursor and anchor", flip_selections} },
{ {alt(':')}, {"ensure selection cursor is after anchor", ensure_forward} }, { {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>>>} }, { {'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>>>} }, { {'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')}, {"extend selections to whole lines", select<SelectMode::Replace, select_lines>} },
{ {alt('X')}, {"crop selections to whole lines", select<SelectMode::Replace, trim_partial_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'}, {"select to matching character", select<SelectMode::Replace, select_matching<true>>} },
{ {'M'}, {"extend to matching character", select<SelectMode::Extend, select_matching>} }, { {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>} }, { {'/'}, {"select next given regex match", search<SelectMode::Replace, MatchDirection::Forward>} },
{ {'?'}, {"extend with next given regex match", search<SelectMode::Extend, MatchDirection::Forward>} }, { {'?'}, {"extend with next given regex match", search<SelectMode::Extend, MatchDirection::Forward>} },

View File

@ -220,6 +220,7 @@ select_to_first_non_blank(const Context& context, const Selection& selection)
return {it.coord()}; return {it.coord()};
} }
template<bool forward>
Optional<Selection> Optional<Selection>
select_matching(const Context& context, const Selection& 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>>(); auto& matching_pairs = context.options()["matching_pairs"].get<Vector<Codepoint, MemoryDomain::Options>>();
Utf8Iterator it{buffer.iterator_at(selection.cursor()), buffer}; Utf8Iterator it{buffer.iterator_at(selection.cursor()), buffer};
auto match = matching_pairs.end(); auto match = matching_pairs.end();
while (it != buffer.end())
if (forward) while (it != buffer.end())
{ {
match = find(matching_pairs, *it); match = find(matching_pairs, *it);
if (match != matching_pairs.end()) if (match != matching_pairs.end())
break; break;
++it; ++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()) if (match == matching_pairs.end())
return {}; return {};
@ -271,6 +282,10 @@ select_matching(const Context& context, const Selection& selection)
} }
return {}; 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> template<typename Iterator, typename Container>
Optional<std::pair<Iterator, Iterator>> Optional<std::pair<Iterator, Iterator>>

View File

@ -31,6 +31,7 @@ select_to_previous_word(const Context& context, const Selection& selection);
Optional<Selection> Optional<Selection>
select_line(const Context& context, const Selection& selection); select_line(const Context& context, const Selection& selection);
template<bool forward>
Optional<Selection> Optional<Selection>
select_matching(const Context& context, const Selection& selection); select_matching(const Context& context, const Selection& selection);