From 5b1ad0bd0c06deab4183d1466908d09af38be849 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 14 Jun 2023 19:49:06 +1000 Subject: [PATCH] Add a -previous switch to show-matching highlighter This switch makes show-matching fallback to the character preceeding the cursor if the character under the cursor is not a matching character, which should make show-matching more useful in insert mode. --- doc/pages/highlighters.asciidoc | 6 +++++- src/highlighters.cc | 15 ++++++++++++--- src/main.cc | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/doc/pages/highlighters.asciidoc b/doc/pages/highlighters.asciidoc index 9368b270..6196c5fd 100644 --- a/doc/pages/highlighters.asciidoc +++ b/doc/pages/highlighters.asciidoc @@ -30,7 +30,11 @@ highlighter is replaced with the new one. *show-matching*:: highlight matching char of the character under the selections' cursor - using `MatchingChar` face + using `MatchingChar` face, with the following *options*: + + *-previous*::: + fall back to the character before the cursor when the cursor is not + over a matching char *show-whitespaces* [options]:: display symbols on top of whitespaces to make them more explicit diff --git a/src/highlighters.cc b/src/highlighters.cc index 259181f0..d2daa085 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -197,7 +197,7 @@ public: const auto faces = m_faces | transform([&faces = context.context.faces()](auto&& spec) { return faces[spec.second]; - }) | gather>(); + }) | gather(); const auto& matches = get_matches(context.context.buffer(), display_buffer.range(), range); kak_assert(matches.size() % m_faces.size() == 0); @@ -1154,8 +1154,9 @@ constexpr StringView LineNumbersHighlighter::ms_id; const HighlighterDesc show_matching_desc = { "Apply the MatchingChar face to the char matching the one under the cursor", - {} + { { { "previous", {} } }, ParameterDesc::Flags::SwitchesOnlyAtStart, 0, 0 } }; +template void show_matching_char(HighlightContext context, DisplayBuffer& display_buffer, BufferRange) { const Face face = context.context.faces()["MatchingChar"]; @@ -1170,10 +1171,17 @@ void show_matching_char(HighlightContext context, DisplayBuffer& display_buffer, Utf8Iterator it{buffer.iterator_at(pos), buffer}; auto match = find(matching_pairs, *it); + bool matching_prev = match == matching_pairs.end() and match_prev; + if (matching_prev) + match = find(matching_pairs, *--it); if (match == matching_pairs.end()) continue; + if (matching_prev) + highlight_range(display_buffer, it.base().coord(), (it+1).base().coord(), + false, apply_face(face)); + int level = 0; if (((match - matching_pairs.begin()) % 2) == 0) { @@ -1216,7 +1224,8 @@ void show_matching_char(HighlightContext context, DisplayBuffer& display_buffer, std::unique_ptr create_matching_char_highlighter(HighlighterParameters params, Highlighter*) { - return make_highlighter(show_matching_char); + ParametersParser parser{params, show_matching_desc.params}; + return make_highlighter(parser.get_switch("previous") ? show_matching_char : show_matching_char); } void highlight_selections(HighlightContext context, DisplayBuffer& display_buffer, BufferRange) diff --git a/src/main.cc b/src/main.cc index e49a1d0d..aabe45fd 100644 --- a/src/main.cc +++ b/src/main.cc @@ -49,6 +49,7 @@ struct { "» {+b}{} and {+b}{} now undo selection history\n" "» {+u}%exp\\{...}{} expansions provide flexible quoting for expanded " "strings (as double quoted strings)\n" + "» {+u}show-matching -previous{} switch\n" }, { 20221031, "» {+b}{} does not end macro recording anymore, use {+b}Q{}\n"