Skip to content

Commit

Permalink
Respect the value of g:ctrlp_match_current_file
Browse files Browse the repository at this point in the history
Note that in imitation of CtrlP's default semantics, cpsm now defaults
to not matching the current file.

Fixes #14.
  • Loading branch information
nixprime committed Jan 26, 2016
1 parent 8af2807 commit e97b7c4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ to your .vimrc.
currently very limited, and consists mostly of parsing input strings as UTF-8
and handling the case of non-ASCII letters correctly.

In addition, cpsm respects the value set for `g:ctrlp_match_current_file`.

Performance
-----------

Expand Down
1 change: 1 addition & 0 deletions autoload/cpsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def ctrlp_match():
limit=int(vim.eval("a:limit")), mmode=vim.eval("a:mmode"),
ispath=int(vim.eval("a:ispath")), crfile=vim.eval("a:crfile"),
highlight_mode=vim.eval("g:cpsm_highlight_mode"),
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"),
unicode=int(vim.eval("g:cpsm_unicode")))
Expand Down
1 change: 1 addition & 0 deletions autoload/cpsm.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ let s:script_dir = escape(expand('<sfile>:p:h'), '\')
execute 'pyfile ' . s:script_dir . '/cpsm.py'

function cpsm#CtrlPMatch(items, str, limit, mmode, ispath, crfile, regex)
let s:match_crfile = exists('g:ctrlp_match_current_file') ? g:ctrlp_match_current_file : 0
py ctrlp_match()
call clearmatches()
" Apply highlight regexes.
Expand Down
9 changes: 9 additions & 0 deletions src/matcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ bool Matcher::match(boost::string_ref const item, MatchBase& m,

if (query_.empty()) {
match_path(item_parts, scorer);
if (!opts_.match_crfile && scorer.path_distance == 0) {
return false;
}
m.reverse_score = scorer.reverse_score();
return true;
}
Expand Down Expand Up @@ -193,6 +196,12 @@ bool Matcher::match(boost::string_ref const item, MatchBase& m,
// Fill path match data.
match_path(item_parts, scorer);

// Now that match_path has filled scorer.path_distance, apply
// match_crfile.
if (!opts_.match_crfile && scorer.path_distance == 0) {
return false;
}

// Now do more refined matching on the key (the rightmost path component of
// the item for a path match, and just the full item otherwise).
if (match_positions) {
Expand Down
3 changes: 3 additions & 0 deletions src/matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ struct MatcherOpts {
AUTO,
};
QueryPathMode query_path_mode = QueryPathMode::AUTO;

// If true, consider the currently open file as a match.
bool match_crfile = true;
};

class Matcher {
Expand Down
17 changes: 10 additions & 7 deletions src/python_extension_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ extern "C" {

static PyObject* cpsm_ctrlp_match(PyObject* self, PyObject* args,
PyObject* kwargs) {
static char const* kwlist[] = {
"items", "query", "limit", "mmode", "ispath", "crfile", "highlight_mode",
"max_threads", "query_inverting_delimiter", "unicode", nullptr};
static char const* kwlist[] = {"items", "query", "limit", "mmode", "ispath",
"crfile", "highlight_mode", "match_crfile",
"max_threads", "query_inverting_delimiter",
"unicode", nullptr};
// Required parameters.
PyObject* items_obj;
char const* query_data;
Expand All @@ -114,17 +115,18 @@ static PyObject* cpsm_ctrlp_match(PyObject* self, PyObject* args,
// cpsm-specific options.
char const* highlight_mode_data = nullptr;
Py_ssize_t highlight_mode_size = 0;
int match_crfile = 0;
int max_threads_int = 0;
char const* query_inverting_delimiter_data = nullptr;
Py_ssize_t query_inverting_delimiter_size = 0;
int unicode = 0;
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "Os#|is#is#s#is#i", const_cast<char**>(kwlist),
args, kwargs, "Os#|is#is#s#iis#i", const_cast<char**>(kwlist),
&items_obj, &query_data, &query_size, &limit_int, &mmode_data,
&mmode_size, &is_path, &cur_file_data, &cur_file_size,
&highlight_mode_data, &highlight_mode_size, &max_threads_int,
&query_inverting_delimiter_data, &query_inverting_delimiter_size,
&unicode)) {
&highlight_mode_data, &highlight_mode_size, &match_crfile,
&max_threads_int, &query_inverting_delimiter_data,
&query_inverting_delimiter_size, &unicode)) {
return nullptr;
}

Expand All @@ -150,6 +152,7 @@ static PyObject* cpsm_ctrlp_match(PyObject* self, PyObject* args,
MatcherOpts mopts;
mopts.cur_file = std::string(cur_file_data, cur_file_size);
mopts.is_path = is_path;
mopts.match_crfile = match_crfile;
StringHandlerOpts sopts;
sopts.unicode = unicode;
Matcher const matcher(std::move(query), std::move(mopts),
Expand Down

0 comments on commit e97b7c4

Please sign in to comment.