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