Mercurial > dotfiles
comparison vim/autoload/ctrlpext/autoignore.vim @ 193:ae53d68033d9
Enable fugitive and some CtrlP extensions, include a new one of my own.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 23 Jul 2014 16:57:59 -0700 |
parents | |
children | 4e276e0cdd98 |
comparison
equal
deleted
inserted
replaced
192:89e9c0c0b839 | 193:ae53d68033d9 |
---|---|
1 " ============================================================================= | |
2 " File: autoload/ctrlpext/autoignore.vim | |
3 " Description: Auto-ignore Extension | |
4 " Author: Ludovic Chabant <github.com/ludovicchabant> | |
5 " ============================================================================= | |
6 | |
7 | |
8 " Global Settings {{{ | |
9 | |
10 if !exists('g:ctrlp_autoignore_debug') | |
11 let g:ctrlp_autoignore_debug = 0 | |
12 endif | |
13 | |
14 if !exists('g:ctrlp_autoignore_trace') | |
15 let g:ctrlp_autoignore_trace = 0 | |
16 endif | |
17 | |
18 if exists('g:ctrlp_autoignore_loaded') && g:ctrlp_autoignore_loaded | |
19 \ && !g:ctrlp_autoignore_debug | |
20 finish | |
21 endif | |
22 let g:ctrlp_autoignore_loaded = 1 | |
23 | |
24 " }}} | |
25 | |
26 " Autoload functions {{{ | |
27 | |
28 " Call this in your `vimrc` to enable auto-ignore. | |
29 function! ctrlpext#autoignore#init() abort | |
30 if !exists('g:ctrlp_custom_ignore') | |
31 let g:ctrlp_custom_ignore = {} | |
32 endif | |
33 let g:ctrlp_custom_ignore['func'] = 'ctrlpext#autoignore#ignore' | |
34 endfunction | |
35 | |
36 " List patterns for a given project's root. | |
37 function! ctrlpext#autoignore#get_patterns(root_dir) abort | |
38 let l:patterns = s:get_project_patterns(a:root_dir) | |
39 for pat in l:patterns | |
40 let l:prefix = pat['type'] == '' ? '(all)' : pat['type'] | |
41 echom l:prefix . ':' . pat['pat'] | |
42 endfor | |
43 endfunction | |
44 | |
45 " }}} | |
46 | |
47 " Internals {{{ | |
48 | |
49 function! s:trace(message) abort | |
50 if g:ctrlp_autoignore_trace | |
51 echom "ctrlp_autoignore: " . a:message | |
52 endif | |
53 endfunction | |
54 | |
55 let s:proj_cache = {} | |
56 | |
57 function! s:load_project_patterns(root_dir) abort | |
58 let l:ign_path = a:root_dir . '/.ctrlpignore' | |
59 if !filereadable(l:ign_path) | |
60 call s:trace("No pattern file at: " . l:ign_path) | |
61 return [] | |
62 endif | |
63 let l:patterns = [] | |
64 let l:lines = readfile(l:ign_path) | |
65 for line in l:lines | |
66 if match(line, '\v^\s*$') >= 0 || match(line, '\v^\s*#') >= 0 | |
67 continue | |
68 endif | |
69 let l:matches = matchlist(line, '\v^((dir|file|link)\:)?(.*)') | |
70 let l:mtype = l:matches[2] | |
71 let l:mpat = l:matches[3] | |
72 call add(l:patterns, {'type': l:mtype, 'pat': l:mpat}) | |
73 endfor | |
74 call s:trace("Loaded " . len(l:patterns) . " patterns from: " . l:ign_path) | |
75 return l:patterns | |
76 endfunction | |
77 | |
78 function! s:get_project_patterns(root_dir) abort | |
79 let l:patterns = get(s:proj_cache, a:root_dir) | |
80 if type(l:patterns) == type([]) | |
81 return l:patterns | |
82 endif | |
83 | |
84 call s:trace("Loading patterns for project: " . a:root_dir) | |
85 let l:loaded = s:load_project_patterns(a:root_dir) | |
86 let s:proj_cache[a:root_dir] = l:loaded | |
87 return l:loaded | |
88 endfunction | |
89 | |
90 " The custom ignore function that CtrlP will be using in addition to | |
91 " normal pattern-based matching. | |
92 function! ctrlpext#autoignore#ignore(item, type) abort | |
93 let l:root = getcwd() | |
94 let l:patterns = s:get_project_patterns(l:root) | |
95 for pat in l:patterns | |
96 if pat['type'] == '' || pat['type'] == a:type | |
97 if match(a:item, pat['pat']) >= 0 | |
98 return 1 | |
99 endif | |
100 endif | |
101 endfor | |
102 return 0 | |
103 endfunction | |
104 | |
105 " }}} | |
106 |