Skip to content

Commit

Permalink
Make highlight regexes honor g:ctrlp_line_prefix
Browse files Browse the repository at this point in the history
Fixes #32.
  • Loading branch information
nixprime committed Feb 12, 2017
1 parent 565ab53 commit ccc7a51
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
1 change: 1 addition & 0 deletions autoload/cpsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def ctrlp_match():
match_crfile=int(vim.eval("s:match_crfile")),
max_threads=int(vim.eval("g:cpsm_max_threads")),
query_inverting_delimiter=vim.eval("g:cpsm_query_inverting_delimiter"),
regex_line_prefix=vim.eval("s:regex_line_prefix"),
unicode=int(vim.eval("g:cpsm_unicode")))
# Escape backslashes and ".
vim.command("let s:results = [%s]" % ",".join(
Expand Down
4 changes: 4 additions & 0 deletions autoload/cpsm.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ function cpsm#CtrlPMatch(items, str, limit, mmode, ispath, crfile, regex)
elseif s:status == 1
return ['ERROR: cpsm built with version of Python not supported by Vim']
endif
let s:regex_line_prefix = '> '
if exists('g:ctrlp_line_prefix')
let s:regex_line_prefix = g:ctrlp_line_prefix
endif
if empty(a:str) && g:cpsm_match_empty_query == 0
let s:results = a:items[0:(a:limit)]
let s:regexes = []
Expand Down
24 changes: 14 additions & 10 deletions src/ctrlp_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,38 +86,42 @@ CtrlPMatchMode parse_ctrlp_match_mode(boost::string_ref const mmode) {
void get_highlight_regexes(boost::string_ref const mode,
boost::string_ref const item,
std::vector<std::size_t> const& positions,
std::vector<std::string>& regexes) {
std::vector<std::string>& regexes,
boost::string_ref const line_prefix) {
for (auto const group : group_positions(mode, positions)) {
// Each match group's regex has the same structure:
// - "\V": very nomagic (only "\" needs to be escaped)
// - "\C": forces case sensitivity
// - "\^": beginning of string
// - "> ": appears at the start of each line
// - the line prefix
// - characters in the item before the match
// - "\zs": starts the match
// - characters in the match group
// - "\ze": ends the match
// - characters in the item after the match
// - "\$": end of string
std::size_t i = 0;
std::string regex = R"(\V\C\^> )";
auto const write_char = [&](std::size_t i) {
if (item[i] == '\\') {
std::string regex = R"(\V\C\^)";
auto const write_char = [&](char c) {
if (c == '\\') {
regex += R"(\\)";
} else {
regex += item[i];
regex += c;
}
};
for (char const c : line_prefix) {
write_char(c);
}
std::size_t i = 0;
for (; i < group.first; i++) {
write_char(i);
write_char(item[i]);
}
regex += R"(\zs)";
for (; i < group.second; i++) {
write_char(i);
write_char(item[i]);
}
regex += R"(\ze)";
for (; i < item.size(); i++) {
write_char(i);
write_char(item[i]);
}
regex += R"(\$)";
regexes.emplace_back(std::move(regex));
Expand Down
3 changes: 2 additions & 1 deletion src/ctrlp_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ struct CtrlPItem {
// for the given highlight mode. `positions` must be sorted.
void get_highlight_regexes(boost::string_ref mode, boost::string_ref item,
std::vector<std::size_t> const& positions,
std::vector<std::string>& regexes);
std::vector<std::string>& regexes,
boost::string_ref line_prefix);

} // namespace cpsm

Expand Down
16 changes: 11 additions & 5 deletions src/python_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,15 @@ constexpr char CTRLP_MATCH_DOC[] =
"match_crfile -- if false, never match `crfile`\n"
"max_threads -- if positive, limit on the number of matcher threads\n"
"query_inverting_delimiter -- see README\n"
"regex_line_prefix -- prefix for each regex in `regexes`\n"
"unicode -- if true, all items are UTF-8-encoded";

static PyObject* cpsm_ctrlp_match(PyObject* self, PyObject* args,
PyObject* kwargs) {
static char const* kwlist[] = {"items", "query", "limit", "mmode", "ispath",
"crfile", "highlight_mode", "match_crfile",
"max_threads", "query_inverting_delimiter",
"unicode", nullptr};
"regex_line_prefix", "unicode", nullptr};
// Required parameters.
PyObject* items_obj;
char const* query_data;
Expand All @@ -293,14 +294,17 @@ static PyObject* cpsm_ctrlp_match(PyObject* self, PyObject* args,
int max_threads_int = 0;
char const* query_inverting_delimiter_data = nullptr;
Py_ssize_t query_inverting_delimiter_size = 0;
char const* regex_line_prefix_data = nullptr;
Py_ssize_t regex_line_prefix_size = 0;
int unicode = 0;
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "Os#|iz#iz#z#iiz#i", const_cast<char**>(kwlist),
args, kwargs, "Os#|iz#iz#z#iiz#z#i", const_cast<char**>(kwlist),
&items_obj, &query_data, &query_size, &limit_int, &mmode_data,
&mmode_size, &is_path, &crfile_data, &crfile_size,
&highlight_mode_data, &highlight_mode_size, &match_crfile,
&max_threads_int, &query_inverting_delimiter_data,
&query_inverting_delimiter_size, &unicode)) {
&query_inverting_delimiter_size, &regex_line_prefix_data,
&regex_line_prefix_size, &unicode)) {
return nullptr;
}

Expand Down Expand Up @@ -357,8 +361,10 @@ static PyObject* cpsm_ctrlp_match(PyObject* self, PyObject* args,
for (auto& pos : match_positions) {
pos += delta;
}
cpsm::get_highlight_regexes(highlight_mode, item, match_positions,
highlight_regexes);
cpsm::get_highlight_regexes(
highlight_mode, item, match_positions, highlight_regexes,
boost::string_ref(regex_line_prefix_data,
regex_line_prefix_size));
});
if (PyTuple_SetItem(output_tuple.get(), 0, matches_list.release())) {
return nullptr;
Expand Down

0 comments on commit ccc7a51

Please sign in to comment.