summaryrefslogtreecommitdiff
path: root/src/normal.cc
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2017-12-03 17:06:11 +0800
committerMaxime Coste <mawww@kakoune.org>2017-12-03 17:15:24 +0800
commit73a239d3be33fd7429ee21677649a206f5d683a7 (patch)
tree310987b7f5241f6918a69b369d50345269eb067f /src/normal.cc
parentc8f5935c48bcafc9f44998b4f6389edc62b4913e (diff)
Text-Objects: Use regex to select surroundings
Fixes #925
Diffstat (limited to 'src/normal.cc')
-rw-r--r--src/normal.cc37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/normal.cc b/src/normal.cc
index 224319b7..a92eb257 100644
--- a/src/normal.cc
+++ b/src/normal.cc
@@ -1171,7 +1171,9 @@ void select_object(Context& context, NormalParams params)
if (cp == 'c')
{
const bool info = show_auto_info_ifn(
- "Enter object desc", "format: <open text>,<close text>",
+ "Enter object desc",
+ "format: <open regex>,<close regex>\n"
+ " escape commas with '\\'",
AutoInfo::Command, context);
context.input_handler().prompt(
@@ -1189,7 +1191,8 @@ void select_object(Context& context, NormalParams params)
select_and_set_last<mode>(
context, std::bind(select_surrounding, _1, _2,
- params[0], params[1],
+ Regex{params[0], RegexCompileFlags::Backward},
+ Regex{params[1], RegexCompileFlags::Backward},
count, flags));
});
return;
@@ -1197,36 +1200,36 @@ void select_object(Context& context, NormalParams params)
static constexpr struct SurroundingPair
{
- StringView opening;
- StringView closing;
- Codepoint name;
+ char opening;
+ char closing;
+ char name;
} surrounding_pairs[] = {
- { "(", ")", 'b' },
- { "{", "}", 'B' },
- { "[", "]", 'r' },
- { "<", ">", 'a' },
- { "\"", "\"", 'Q' },
- { "'", "'", 'q' },
- { "`", "`", 'g' },
+ { '(', ')', 'b' },
+ { '{', '}', 'B' },
+ { '[', ']', 'r' },
+ { '<', '>', 'a' },
+ { '"', '"', 'Q' },
+ { '\'', '\'', 'q' },
+ { '`', '`', 'g' },
};
auto pair_it = find_if(surrounding_pairs,
[cp](const SurroundingPair& s) {
- return s.opening[0_char] == cp or
- s.closing[0_char] == cp or
+ return s.opening == cp or s.closing == cp or
(s.name != 0 and s.name == cp);
});
if (pair_it != std::end(surrounding_pairs))
return select_and_set_last<mode>(
context, std::bind(select_surrounding, _1, _2,
- pair_it->opening, pair_it->closing,
+ Regex{format("\\Q{}", pair_it->opening), RegexCompileFlags::Backward},
+ Regex{format("\\Q{}", pair_it->closing), RegexCompileFlags::Backward},
count, flags));
if (is_punctuation(cp) or cp == '_')
{
- auto utf8cp = to_string(cp);
+ auto re = Regex{"\\Q" + to_string(cp), RegexCompileFlags::Backward};
return select_and_set_last<mode>(
context, std::bind(select_surrounding, _1, _2,
- utf8cp, utf8cp, count, flags));
+ re, re, count, flags));
}
}, get_title(),
build_autoinfo_for_mapping(context, KeymapMode::Object,