comparison vim/autoload/ctrlp/autoignore.vim @ 240:3a6b11d16a2a

Simpler registration for CtrlP extensions.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 12 Dec 2014 16:53:19 -0800
parents vim/autoload/ctrlpext/autoignore.vim@4e276e0cdd98
children
comparison
equal deleted inserted replaced
239:ec74791ee042 240:3a6b11d16a2a
1 " =============================================================================
2 " File: autoload/ctrlp/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 " Initialization {{{
27
28 if !exists('g:ctrlp_custom_ignore')
29 let g:ctrlp_custom_ignore = {}
30 endif
31 let g:ctrlp_custom_ignore['func'] = 'ctrlp#autoignore#ignore'
32 let g:ctrlp_custom_ignore['func-init'] = 'ctrlp#autoignore#ignore_init'
33
34 " }}}
35
36 " Internals {{{
37
38 function! s:trace(message) abort
39 if g:ctrlp_autoignore_trace
40 echom "ctrlp_autoignore: " . a:message
41 endif
42 endfunction
43
44 let s:proj_cache = {}
45 let s:active_patterns = []
46
47 function! s:load_project_patterns(root_dir) abort
48 let l:ign_path = a:root_dir . '/.ctrlpignore'
49 if !filereadable(l:ign_path)
50 call s:trace("No pattern file at: " . l:ign_path)
51 return []
52 endif
53 let l:patterns = []
54 let l:lines = readfile(l:ign_path)
55 for line in l:lines
56 if match(line, '\v^\s*$') >= 0 || match(line, '\v^\s*#') >= 0
57 continue
58 endif
59 let l:matches = matchlist(line, '\v^((dir|file|link)\:)?(.*)')
60 let l:mtype = l:matches[2]
61 let l:mpat = l:matches[3]
62 call add(l:patterns, {'type': l:mtype, 'pat': l:mpat})
63 endfor
64 call s:trace("Loaded " . len(l:patterns) . " patterns from: " . l:ign_path)
65 return l:patterns
66 endfunction
67
68 function! s:get_project_patterns(root_dir) abort
69 let l:patterns = get(s:proj_cache, a:root_dir)
70 if type(l:patterns) == type([])
71 return l:patterns
72 endif
73
74 call s:trace("Loading patterns for project: " . a:root_dir)
75 let l:loaded = s:load_project_patterns(a:root_dir)
76 let s:proj_cache[a:root_dir] = l:loaded
77 return l:loaded
78 endfunction
79
80 " The custom ignore function that CtrlP will be using in addition to
81 " normal pattern-based matching.
82 function! ctrlp#autoignore#ignore(item, type) abort
83 for pat in s:active_patterns
84 if pat['type'] == '' || pat['type'] == a:type
85 if match(a:item, pat['pat']) >= 0
86 return 1
87 endif
88 endif
89 endfor
90 return 0
91 endfunction
92
93 function! ctrlp#autoignore#ignore_init() abort
94 let l:root = getcwd()
95 let s:active_patterns = s:get_project_patterns(l:root)
96 endfunction
97
98 " List patterns for a given project's root.
99 function! ctrlp#autoignore#get_patterns(root_dir) abort
100 let l:patterns = s:get_project_patterns(a:root_dir)
101 for pat in l:patterns
102 let l:prefix = pat['type'] == '' ? '(all)' : pat['type']
103 echom l:prefix . ':' . pat['pat']
104 endfor
105 endfunction
106
107 " }}}
108