1 " vim: set noet nosta sw=4 ts=4 fdm=marker : |
|
2 " |
|
3 " A basic Glimpse interface for Vim |
|
4 " |
|
5 " Based on a Vim Tip from Jean-Rene David. |
|
6 " http://vim.wikia.com/wiki/Use_glimpse_from_within_Vim |
|
7 " |
|
8 " Mahlon E. Smith <mahlon@martini.nu> |
|
9 " $Id$ |
|
10 " |
|
11 " Here's a bash function that's nice too! |
|
12 " function g() { command glimpse -winO -F `pwd` $1 | sed 's|^`pwd`/||'; } |
|
13 " |
|
14 " And a tcsh alias, while we're at it! |
|
15 " alias g 'glimpse -winO -F `pwd` \!:1 | sed -e "s|^`pwd`/||"' |
|
16 " |
|
17 |
|
18 " Loaded check {{{ |
|
19 if exists( 'g:glimpse_loaded' ) |
|
20 finish |
|
21 endif |
|
22 let g:glimpse_loaded = '$Rev$' |
|
23 |
|
24 " }}} |
|
25 " Hook up the functions to the user supplied key bindings. {{{ |
|
26 " |
|
27 if exists( 'g:glimpseKey' ) |
|
28 execute 'map ' . g:glimpseKey . ' :call <SID>Glimpse()<CR>' |
|
29 endif |
|
30 |
|
31 if exists( 'g:glimpseindexKey' ) |
|
32 execute 'map ' . g:glimpseindexKey . ' :call <SID>GlimpseIndex()<CR>' |
|
33 endif |
|
34 |
|
35 " }}} |
|
36 " Defaults for misc settings {{{ |
|
37 " |
|
38 if !exists( 'g:glimpseWindowResultSize' ) |
|
39 let g:glimpseWindowResultSize = 15 |
|
40 endif |
|
41 |
|
42 if !exists( 'g:glimpseFlags' ) |
|
43 let g:glimpseFlags = "-iny" |
|
44 endif |
|
45 |
|
46 if !exists( 'g:glimpseindexFlags' ) |
|
47 let g:glimpseindexFlags = '-M 10 -B -o' |
|
48 endif |
|
49 |
|
50 if !exists( 'g:glimpseErrorFormat' ) |
|
51 let g:glimpseErrorFormat = '%f: %l: %m' |
|
52 endif |
|
53 |
|
54 |
|
55 "}}} |
|
56 " Commands {{{ |
|
57 " |
|
58 command! -nargs=* Glimpse :call <SID>Glimpse( <f-args> ) |
|
59 command! GlimpseIndex :call <SID>GlimpseIndex() |
|
60 |
|
61 |
|
62 "}}} |
|
63 " Glimpse( search_pattern, file_pattern ) {{{ |
|
64 " |
|
65 " Run a glimpse search using a pattern from +search_pattern+ and an optional |
|
66 " +file_pattern+ to limit the breadth of the search. Places results into the |
|
67 " quickfix window. |
|
68 " |
|
69 function! <SID>Glimpse( ... ) |
|
70 |
|
71 " parse command line opts |
|
72 " |
|
73 let l:search_pattern = expand("<cword>") |
|
74 let l:file_pattern = '' |
|
75 if exists( 'a:1' ) |
|
76 let l:search_pattern = a:1 |
|
77 endif |
|
78 if exists( 'a:2' ) |
|
79 let l:file_pattern = a:2 |
|
80 endif |
|
81 |
|
82 " everything is based on the cwd so results are relevant to the |
|
83 " current project. |
|
84 " |
|
85 let l:cwd = getcwd() |
|
86 |
|
87 " save the original error format, we want to play nice with others. |
|
88 " |
|
89 let l:orig_errorformat = &errorformat |
|
90 execute 'set errorformat=' . escape( g:glimpseErrorFormat, ' ' ) |
|
91 |
|
92 " execute the search. |
|
93 " |
|
94 let l:cmd = "glimpse " . g:glimpseFlags . " -F '" . l:cwd |
|
95 if ( l:file_pattern != '' ) |
|
96 let l:cmd = l:cmd . ";" . l:file_pattern |
|
97 endif |
|
98 let l:cmd = l:cmd . "' '" . l:search_pattern . "'" |
|
99 |
|
100 let l:result_list = split( system( l:cmd ), "\n" ) |
|
101 |
|
102 " no results, escape now |
|
103 " |
|
104 if ( empty(l:result_list) ) |
|
105 let l:no_match_msg = "No matches for '" . l:search_pattern . "'" |
|
106 if ( l:file_pattern != '' ) |
|
107 let l:no_match_msg = l:no_match_msg . " in files matching '" . l:file_pattern . "'" |
|
108 endif |
|
109 call s:err( l:no_match_msg ) |
|
110 execute 'set errorformat=' . escape( l:orig_errorformat, ' ' ) |
|
111 return |
|
112 endif |
|
113 |
|
114 " populate the quickfix window. |
|
115 " |
|
116 let l:results = '' |
|
117 for l:result in l:result_list |
|
118 " 'pretty up' the glimpse output. |
|
119 let l:result = substitute( l:result, ('^' . l:cwd . '/'), '', '' ) |
|
120 let l:result = substitute( l:result, '\(: \d\+:\)\s\+', '\=submatch(1) . " "', '' ) |
|
121 |
|
122 let l:results = l:results . l:result . "\n" |
|
123 endfor |
|
124 cgetexpr( l:results ) |
|
125 execute ':copen' . g:glimpseWindowResultSize |
|
126 |
|
127 " quick close |
|
128 nnoremap <silent> <buffer> q :ccl<CR> |
|
129 |
|
130 " reset error format to the original |
|
131 execute 'set errorformat=' . escape( l:orig_errorformat, ' ' ) |
|
132 endfunction |
|
133 |
|
134 |
|
135 " }}} |
|
136 " GlimpseReindex() {{{ |
|
137 " |
|
138 " Update local indexes for the current working directory. |
|
139 " Results of the index are placed into the location list. (:lopen to view.) |
|
140 " |
|
141 function! <SID>GlimpseIndex() |
|
142 let l:cmd = 'glimpseindex ' . g:glimpseindexFlags . ' -f .' |
|
143 let l:cwd = getcwd() |
|
144 let $LC_ALL = 'C' |
|
145 let l:index_output = system(l:cmd) |
|
146 let l:index_exit = v:shell_error |
|
147 call s:err( "Updated indexes for '" . l:cwd . "'" ) |
|
148 if l:index_exit != 0 |
|
149 call s:err( "Uh oh, " . l:cmd . " exited with " . l:index_exit. "! Output follows:" ) |
|
150 call s:err( l:index_output ) |
|
151 endif |
|
152 endfunction |
|
153 |
|
154 |
|
155 " }}} |
|
156 " s:err( msg ) {{{ |
|
157 " |
|
158 " Notify user in a consistent fashion. |
|
159 " |
|
160 function! s:err( msg ) |
|
161 echohl WarningMsg|echomsg 'glimpse: ' . a:msg|echohl None |
|
162 endfunction " }}} |
|
163 |
|