Skip to content

Commit

Permalink
Use matcher plugin for CtrlP
Browse files Browse the repository at this point in the history
This speeds up the indexing and has a different algorithm that is more 
like Command-T.

$ make
$ make install
  • Loading branch information
bittersweet committed Jul 13, 2012
1 parent 2922c8d commit 3833f5b
Show file tree
Hide file tree
Showing 7 changed files with 455 additions and 6 deletions.
22 changes: 22 additions & 0 deletions vim/bundle/matcher/LICENSE
@@ -0,0 +1,22 @@
opyright (c) 2012, Burke Libbey
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 changes: 14 additions & 0 deletions vim/bundle/matcher/Makefile
@@ -0,0 +1,14 @@
EXENAME=matcher
PREFIX=/usr/local
BINDIR=$(PREFIX)/bin

.PHONY: all
all: $(EXENAME)

$(EXENAME): main.c matcher.c
$(CC) $(CFLAGS) -O3 -Wall $^ -o $@

.PHONY: install
install: $(EXENAME)
install -d $(DESTDIR)$(PREFIX)
install -m 0755 $< $(DESTDIR)$(PREFIX)/bin
98 changes: 98 additions & 0 deletions vim/bundle/matcher/README.md
@@ -0,0 +1,98 @@
# Matcher

This is a standalone library that does the same fuzzy-find matching as Command-T.vim.

# Installation

```shell
$ make
# move `matcher` somewhere useful
$ make install
# make install will install it to /usr/local/bin.
```

# Usage

Matcher searches for a string in a list of filenames, and returns the
ones it thinks you are most likely referring to. It works exactly like
fuzzy-finder, Command-T, and so on.

### Usage:

```shell
$ matcher [options] <search>
```

#### Options:

* `--limit`: The number of matches to return (default 10)
* `--no-dotfiles`: Dotfiles will never be returned (by default, they may
be)
* `--manifest`: Specify a file containing the list of files to scan. If
none given, matcher will read the list from stdin.

### Examples

```shell
$ matcher --limit 20 --no-dotfiles --manifest filelist.txt customer.rb
$ find . | matcher order
```

# Using with CtrlP.vim

```viml
let g:path_to_matcher = "/path/to/matcher"
let g:ctrlp_user_command = ['.git/', 'cd %s && git ls-files . -co --exclude-standard']
let g:ctrlp_match_func = { 'match': 'GoodMatch' }
function! GoodMatch(items, str, limit, mmode, ispath, crfile, regex)
" Create a cache file if not yet exists
let cachefile = ctrlp#utils#cachedir().'/matcher.cache'
if !( filereadable(cachefile) && a:items == readfile(cachefile) )
call writefile(a:items, cachefile)
endif
if !filereadable(cachefile)
return []
endif
" a:mmode is currently ignored. In the future, we should probably do
" something about that. the matcher behaves like "full-line".
let cmd = g:path_to_matcher.' --limit '.a:limit.' --manifest '.cachefile.' '
if !( exists('g:ctrlp_dotfiles') && g:ctrlp_dotfiles )
let cmd = cmd.'--no-dotfiles '
endif
let cmd = cmd.a:str
return split(system(cmd), "\n")
endfunction
```

# Using with zsh

```shell
_matcher_complete() {
git ls-files | /Users/burke/bin/matcher -l20 ${words[CURRENT]} | while read line; do
compadd -U "$line"
done
compstate[insert]=menu # no expand
}

zle -C matcher-complete 'menu-select' _matcher_complete

bindkey '^X^T' matcher-complete # C-x C-t to find matches for the search under the cursor
# bindkey '^T' matcher-complete # C-t to find matches for the search under the cursor
```


# Bugs

* Probably

# Contributing

* Fork branch commit push pullrequest
* I'm bad at github notifications. Send me an email too at burke@burkelibbey.org
101 changes: 101 additions & 0 deletions vim/bundle/matcher/main.c
@@ -0,0 +1,101 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>

#include "matcher.h"

struct globalArgs_t {
int dotfiles; // -d
int limit; // -l
char *manifest; // -m
char *search;
} globalArgs;

static const char *optString = "dl:m:h?";

static const struct option longOpts[] = {
{ "no-dotfiles", no_argument, NULL, 'd' },
{ "limit", required_argument, NULL, 'l' },
{ "manifest", required_argument, NULL, 'm' },
{ "help", no_argument, NULL, 'h' },
{ NULL, no_argument, NULL, 0 }
};

/* Display program usage, and exit. */
void display_usage(void)
{
puts("Usage: matcher [--no-dotfiles] [--limit num] [--manifest filename] <query>\n");
exit( EXIT_FAILURE );
}

void parse_arguments(int argc, char *argv[])
{
int opt = 0;
int longIndex = 0;

globalArgs.dotfiles = 1;
globalArgs.limit = 10;
globalArgs.manifest = NULL;
globalArgs.search = NULL;

opt = getopt_long(argc, argv, optString, longOpts, &longIndex);

while (opt != -1) {
switch (opt) {
case 'l':
globalArgs.limit = atoi(optarg);
break;
case 'd':
globalArgs.dotfiles = 0;
break;
case 'm':
globalArgs.manifest = optarg;
break;
case 'h':
case '?':
display_usage();
break;
default:
break;
}
opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
}


globalArgs.search = argv[argc - 1];
int i = 0;
while(globalArgs.search[i] != '\0'){
globalArgs.search[i] = tolower(globalArgs.search[i]);
i++;
}
}

int main(int argc, char *argv[])
{
parse_arguments(argc, argv);

char *strings[20000];
int num_strings = 0;

FILE *fp = stdin;
if (globalArgs.manifest) {
fp = fopen(globalArgs.manifest, "r");
}

for (num_strings = 0; num_strings < 20000; num_strings++) {
strings[num_strings] = malloc(1024 * sizeof(char));
fgets(strings[num_strings], 1023, fp);
if (feof(fp)) break;
}

fclose(fp);

score_list(globalArgs.search,
strings,
num_strings,
globalArgs.dotfiles,
globalArgs.limit);
return 0;
}

0 comments on commit 3833f5b

Please sign in to comment.