Mercurial > vim-gutentags
annotate plugin/autotags.vim @ 11:478638833c3b
Fix Win32 update script, add option to pause it.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 23 Jul 2014 17:04:34 -0700 |
parents | b853ad0e7afd |
children | b8f23bf7b20f |
rev | line source |
---|---|
0 | 1 " autotags.vim - Automatic ctags management for Vim |
2 " Maintainer: Ludovic Chabant <http://ludovic.chabant.com> | |
3 " Version: 0.0.1 | |
4 | |
5 " Globals {{{ | |
6 | |
7 if !exists('g:autotags_debug') | |
8 let g:autotags_debug = 0 | |
9 endif | |
10 | |
11 if (exists('g:loaded_autotags') || &cp) && !g:autotags_debug | |
12 finish | |
13 endif | |
14 if (exists('g:loaded_autotags') && g:autotags_debug) | |
15 echom "Reloaded autotags." | |
16 endif | |
17 let g:loaded_autotags = 1 | |
18 | |
19 if !exists('g:autotags_trace') | |
3 | 20 let g:autotags_trace = 0 |
0 | 21 endif |
22 | |
23 if !exists('g:autotags_fake') | |
24 let g:autotags_fake = 0 | |
25 endif | |
26 | |
27 if !exists('g:autotags_background_update') | |
28 let g:autotags_background_update = 1 | |
29 endif | |
30 | |
11
478638833c3b
Fix Win32 update script, add option to pause it.
Ludovic Chabant <ludovic@chabant.com>
parents:
10
diff
changeset
|
31 if !exists('g:autotags_pause_after_update') |
478638833c3b
Fix Win32 update script, add option to pause it.
Ludovic Chabant <ludovic@chabant.com>
parents:
10
diff
changeset
|
32 let g:autotags_pause_after_update = 0 |
478638833c3b
Fix Win32 update script, add option to pause it.
Ludovic Chabant <ludovic@chabant.com>
parents:
10
diff
changeset
|
33 endif |
478638833c3b
Fix Win32 update script, add option to pause it.
Ludovic Chabant <ludovic@chabant.com>
parents:
10
diff
changeset
|
34 |
0 | 35 if !exists('g:autotags_enabled') |
36 let g:autotags_enabled = 1 | |
37 endif | |
38 | |
39 if !exists('g:autotags_executable') | |
40 let g:autotags_executable = 'ctags' | |
41 endif | |
42 | |
43 if !exists('g:autotags_tagfile') | |
44 let g:autotags_tagfile = 'tags' | |
45 endif | |
46 | |
47 if !exists('g:autotags_project_root') | |
48 let g:autotags_project_root = [] | |
49 endif | |
50 let g:autotags_project_root += ['.git', '.hg', '.bzr', '_darcs'] | |
51 | |
3 | 52 if !exists('g:autotags_exclude') |
53 let g:autotags_exclude = [] | |
54 endif | |
55 | |
56 if !exists('g:autotags_generate_on_missing') | |
57 let g:autotags_generate_on_missing = 1 | |
58 endif | |
59 | |
60 if !exists('g:autotags_generate_on_write') | |
61 let g:autotags_generate_on_write = 1 | |
62 endif | |
63 | |
64 if !exists('g:autotags_auto_set_tags') | |
65 let g:autotags_auto_set_tags = 1 | |
66 endif | |
67 | |
0 | 68 " }}} |
69 | |
70 " Utilities {{{ | |
71 | |
72 " Throw an exception message. | |
73 function! s:throw(message) | |
74 let v:errmsg = "autotags: " . a:message | |
75 throw v:errmsg | |
76 endfunction | |
77 | |
78 " Prints a message if debug tracing is enabled. | |
79 function! s:trace(message, ...) | |
80 if g:autotags_trace || (a:0 && a:1) | |
81 let l:message = "autotags: " . a:message | |
82 echom l:message | |
83 endif | |
84 endfunction | |
85 | |
86 " Strips the ending slash in a path. | |
87 function! s:stripslash(path) | |
88 return fnamemodify(a:path, ':s?[/\\]$??') | |
89 endfunction | |
90 | |
91 " Normalizes the slashes in a path. | |
92 function! s:normalizepath(path) | |
93 if exists('+shellslash') && &shellslash | |
94 return substitute(a:path, '\v/', '\\', 'g') | |
95 elseif has('win32') | |
96 return substitute(a:path, '\v/', '\\', 'g') | |
97 else | |
98 return a:path | |
99 endif | |
100 endfunction | |
101 | |
102 " Shell-slashes the path (opposite of `normalizepath`). | |
103 function! s:shellslash(path) | |
104 if exists('+shellslash') && !&shellslash | |
105 return substitute(a:path, '\v\\', '/', 'g') | |
106 else | |
107 return a:path | |
108 endif | |
109 endfunction | |
110 | |
111 " }}} | |
112 | |
113 " Autotags Setup {{{ | |
114 | |
115 " Finds the tag file path for the given current directory | |
116 " (typically the directory of the file being edited) | |
117 function! s:get_tagfile_for(path) abort | |
118 let l:path = s:stripslash(a:path) | |
119 let l:previous_path = "" | |
5
12f4f50f4d3a
Use CtrlP root markers if they've been defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
120 let l:markers = g:autotags_project_root[:] |
12f4f50f4d3a
Use CtrlP root markers if they've been defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
121 if exists('g:ctrlp_root_markers') |
12f4f50f4d3a
Use CtrlP root markers if they've been defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
122 let l:markers += g:ctrlp_root_markers |
12f4f50f4d3a
Use CtrlP root markers if they've been defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
123 endif |
0 | 124 while l:path != l:previous_path |
125 for root in g:autotags_project_root | |
126 if getftype(l:path . '/' . root) != "" | |
127 return simplify(fnamemodify(l:path, ':p') . g:autotags_tagfile) | |
128 endif | |
129 endfor | |
130 let l:previous_path = l:path | |
131 let l:path = fnamemodify(l:path, ':h') | |
132 endwhile | |
133 call s:throw("Can't figure out what tag file to use for: " . a:path) | |
134 endfunction | |
135 | |
136 " Setup autotags for the current buffer. | |
137 function! s:setup_autotags() abort | |
3 | 138 if exists('b:autotags_file') && !g:autotags_debug |
139 " This buffer already has autotags support. | |
0 | 140 return |
141 endif | |
3 | 142 |
143 " Try and file what tags file we should manage. | |
144 call s:trace("Scanning buffer '" . bufname('%') . "' for autotags setup...") | |
0 | 145 try |
146 let b:autotags_file = s:get_tagfile_for(expand('%:h')) | |
147 catch /^autotags\:/ | |
3 | 148 call s:trace("Can't figure out what tag file to use... no autotags support.") |
0 | 149 return |
150 endtry | |
151 | |
3 | 152 " We know what tags file to manage! Now set things up. |
0 | 153 call s:trace("Setting autotags for buffer '" . bufname('%') . "' with tagfile: " . b:autotags_file) |
154 | |
3 | 155 " Set the tags file for Vim to use. |
156 if g:autotags_auto_set_tags | |
157 execute 'setlocal tags^=' . b:autotags_file | |
158 endif | |
159 | |
160 " Autocommands for updating the tags on save. | |
0 | 161 let l:bn = bufnr('%') |
162 execute 'augroup autotags_buffer_' . l:bn | |
163 execute ' autocmd!' | |
3 | 164 execute ' autocmd BufWritePost <buffer=' . l:bn . '> call s:write_triggered_update_tags()' |
0 | 165 execute 'augroup end' |
166 | |
3 | 167 " Miscellaneous commands. |
0 | 168 command! -buffer -bang AutotagsUpdate :call s:manual_update_tags(<bang>0) |
3 | 169 |
170 " If the tags file doesn't exist, start generating it now. | |
171 if g:autotags_generate_on_missing && !filereadable(b:autotags_file) | |
172 call s:trace("Generating missing tags file: " . b:autotags_file) | |
173 call s:update_tags(1, 0) | |
174 endif | |
0 | 175 endfunction |
176 | |
177 augroup autotags_detect | |
178 autocmd! | |
179 autocmd BufNewFile,BufReadPost * call s:setup_autotags() | |
180 autocmd VimEnter * if expand('<amatch>')==''|call s:setup_autotags()|endif | |
181 augroup end | |
182 | |
183 " }}} | |
184 | |
185 " Tags File Management {{{ | |
186 | |
187 let s:runner_exe = expand('<sfile>:h:h') . '/plat/unix/update_tags.sh' | |
188 if has('win32') | |
189 let s:runner_exe = expand('<sfile>:h:h') . '\plat\win32\update_tags.cmd' | |
190 endif | |
191 | |
192 let s:update_queue = [] | |
3 | 193 let s:maybe_in_progress = [] |
0 | 194 |
195 " Get how to execute an external command depending on debug settings. | |
196 function! s:get_execute_cmd() abort | |
197 if has('win32') | |
198 let l:cmd = '!start ' | |
199 if g:autotags_background_update | |
200 let l:cmd .= '/b ' | |
201 endif | |
202 return l:cmd | |
203 else | |
204 return '!' | |
205 endif | |
206 endfunction | |
207 | |
3 | 208 " Get the suffix for how to execute an external command. |
209 function! s:get_execute_cmd_suffix() abort | |
210 if has('win32') | |
211 return '' | |
212 else | |
213 return ' &' | |
214 endif | |
215 endfunction | |
216 | |
0 | 217 " (Re)Generate the tags file for the current buffer's file. |
218 function! s:manual_update_tags(bang) abort | |
219 call s:update_tags(a:bang, 0) | |
220 endfunction | |
221 | |
3 | 222 " (Re)Generate the tags file for a buffer that just go saved. |
223 function! s:write_triggered_update_tags() abort | |
224 if g:autotags_enabled && g:autotags_generate_on_write | |
225 call s:update_tags(0, 1) | |
226 endif | |
227 endfunction | |
228 | |
0 | 229 " Update the tags file for the current buffer's file. |
230 " write_mode: | |
231 " 0: update the tags file if it exists, generate it otherwise. | |
232 " 1: always generate (overwrite) the tags file. | |
233 " | |
234 " queue_mode: | |
235 " 0: if an update is already in progress, report it and abort. | |
236 " 1: if an update is already in progress, queue another one. | |
237 " | |
238 " An additional argument specifies where to write the tags file. If nothing | |
239 " is specified, it will go to the autotags-defined file. | |
240 function! s:update_tags(write_mode, queue_mode, ...) abort | |
241 " Figure out where to save. | |
242 if a:0 == 1 | |
243 let l:tags_file = a:1 | |
244 else | |
245 let l:tags_file = b:autotags_file | |
246 endif | |
247 | |
248 " Check that there's not already an update in progress. | |
249 let l:lock_file = l:tags_file . '.lock' | |
250 if filereadable(l:lock_file) | |
251 if a:queue_mode == 1 | |
252 let l:idx = index(s:update_queue, l:tags_file) | |
253 if l:idx < 0 | |
254 call add(s:update_queue, l:tags_file) | |
255 endif | |
256 call s:trace("Tag file '" . l:tags_file . "' is already being updated. Queuing it up...") | |
257 call s:trace("") | |
258 else | |
259 echom "autotags: The tags file is already being updated, please try again later." | |
260 echom "" | |
261 endif | |
262 return | |
263 endif | |
264 | |
265 " Switch to the project root to make the command line smaller, and make | |
266 " it possible to get the relative path of the filename to parse if we're | |
267 " doing an incremental update. | |
268 let l:prev_cwd = getcwd() | |
269 let l:work_dir = fnamemodify(l:tags_file, ':h') | |
270 execute "chdir " . l:work_dir | |
271 | |
272 try | |
273 " Build the command line. | |
274 let l:cmd = s:get_execute_cmd() . s:runner_exe | |
3 | 275 let l:cmd .= ' -e "' . g:autotags_executable . '"' |
276 let l:cmd .= ' -t "' . fnamemodify(l:tags_file, ':t') . '"' | |
0 | 277 if a:write_mode == 0 && filereadable(l:tags_file) |
278 " CTags specifies paths relative to the tags file with a `./` | |
279 " prefix, so we need to specify the same prefix otherwise it will | |
280 " think those are different files and we'll end up with duplicate | |
281 " entries. | |
282 let l:rel_path = s:normalizepath('./' . expand('%:.')) | |
3 | 283 let l:cmd .= ' -s "' . l:rel_path . '"' |
0 | 284 endif |
3 | 285 for ign in split(&wildignore, ',') |
286 let l:cmd .= ' -x ' . ign | |
287 endfor | |
288 for exc in g:autotags_exclude | |
289 let l:cmd .= ' -x ' . exc | |
290 endfor | |
11
478638833c3b
Fix Win32 update script, add option to pause it.
Ludovic Chabant <ludovic@chabant.com>
parents:
10
diff
changeset
|
291 if g:autotags_pause_after_update |
478638833c3b
Fix Win32 update script, add option to pause it.
Ludovic Chabant <ludovic@chabant.com>
parents:
10
diff
changeset
|
292 let l:cmd .= ' -p' |
478638833c3b
Fix Win32 update script, add option to pause it.
Ludovic Chabant <ludovic@chabant.com>
parents:
10
diff
changeset
|
293 endif |
0 | 294 if g:autotags_trace |
3 | 295 if has('win32') |
296 let l:cmd .= ' -l "' . fnamemodify(l:tags_file, ':t') . '.log"' | |
297 else | |
9
f0f1d20d6f5c
On Unix, either log to file or don't log at all.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
298 let l:cmd .= ' > "' . fnamemodify(l:tags_file, ':t') . '.log" 2>&1' |
f0f1d20d6f5c
On Unix, either log to file or don't log at all.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
299 endif |
f0f1d20d6f5c
On Unix, either log to file or don't log at all.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
300 else |
f0f1d20d6f5c
On Unix, either log to file or don't log at all.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
301 if !has('win32') |
f0f1d20d6f5c
On Unix, either log to file or don't log at all.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
302 let l:cmd .= ' > /dev/null 2>&1' |
3 | 303 endif |
0 | 304 endif |
3 | 305 let l:cmd .= s:get_execute_cmd_suffix() |
306 | |
307 " Run the background process. | |
0 | 308 call s:trace("Running: " . l:cmd) |
309 call s:trace("In: " . l:work_dir) | |
310 if !g:autotags_fake | |
3 | 311 " Flag this tags file as being in progress |
312 call add(s:maybe_in_progress, fnamemodify(l:tags_file, ':p')) | |
313 | |
0 | 314 if !g:autotags_trace |
315 silent execute l:cmd | |
316 else | |
317 execute l:cmd | |
318 endif | |
319 else | |
320 call s:trace("(fake... not actually running)") | |
321 endif | |
322 call s:trace("") | |
323 finally | |
324 " Restore the current directory... | |
325 execute "chdir " . l:prev_cwd | |
326 endtry | |
327 endfunction | |
328 | |
329 " }}} | |
330 | |
331 " Manual Tagfile Generation {{{ | |
332 | |
333 function! s:generate_tags(bang, ...) abort | |
334 call s:update_tags(1, 0, a:1) | |
335 endfunction | |
336 | |
337 command! -bang -nargs=1 -complete=file AutotagsGenerate :call s:generate_tags(<bang>0, <f-args>) | |
338 | |
339 " }}} | |
340 | |
3 | 341 " Toggles and Miscellaneous Commands {{{ |
0 | 342 |
343 command! AutotagsToggleEnabled :let g:autotags_enabled=!g:autotags_enabled | |
344 command! AutotagsToggleTrace :call autotags#trace() | |
345 command! AutotagsUnlock :call delete(b:autotags_file . '.lock') | |
346 | |
10
b853ad0e7afd
Only define `:AutotagsToggleFake` if debug mode is on.
Ludovic Chabant <ludovic@chabant.com>
parents:
9
diff
changeset
|
347 if g:autotags_debug |
b853ad0e7afd
Only define `:AutotagsToggleFake` if debug mode is on.
Ludovic Chabant <ludovic@chabant.com>
parents:
9
diff
changeset
|
348 command! AutotagsToggleFake :call autotags#fake() |
b853ad0e7afd
Only define `:AutotagsToggleFake` if debug mode is on.
Ludovic Chabant <ludovic@chabant.com>
parents:
9
diff
changeset
|
349 endif |
b853ad0e7afd
Only define `:AutotagsToggleFake` if debug mode is on.
Ludovic Chabant <ludovic@chabant.com>
parents:
9
diff
changeset
|
350 |
0 | 351 " }}} |
352 | |
353 " Autoload Functions {{{ | |
354 | |
355 function! autotags#rescan(...) | |
356 if exists('b:autotags_file') | |
357 unlet b:autotags_file | |
358 endif | |
359 if a:0 && a:1 | |
360 let l:trace_backup = g:autotags_trace | |
361 let l:autotags_trace = 1 | |
362 endif | |
363 call s:setup_autotags() | |
364 if a:0 && a:1 | |
365 let g:autotags_trace = l:trace_backup | |
366 endif | |
367 endfunction | |
368 | |
369 function! autotags#trace(...) | |
370 let g:autotags_trace = !g:autotags_trace | |
371 if a:0 > 0 | |
372 let g:autotags_trace = a:1 | |
373 endif | |
374 if g:autotags_trace | |
375 echom "autotags: Tracing is enabled." | |
376 else | |
377 echom "autotags: Tracing is disabled." | |
378 endif | |
379 echom "" | |
380 endfunction | |
381 | |
382 function! autotags#fake(...) | |
383 let g:autotags_fake = !g:autotags_fake | |
384 if a:0 > 0 | |
385 let g:autotags_fake = a:1 | |
386 endif | |
387 if g:autotags_fake | |
388 echom "autotags: Now faking autotags." | |
389 else | |
390 echom "autotags: Now running autotags for real." | |
391 endif | |
392 echom "" | |
393 endfunction | |
394 | |
3 | 395 function! autotags#inprogress() |
396 echom "autotags: generations in progress:" | |
397 for mip in s:maybe_in_progress | |
398 echom mip | |
399 endfor | |
400 echom "" | |
401 endfunction | |
402 | |
0 | 403 " }}} |
404 | |
3 | 405 " Statusline Functions {{{ |
406 | |
407 " Prints whether a tag file is being generated right now for the current | |
408 " buffer in the status line. | |
409 " | |
410 " Arguments can be passed: | |
411 " - args 1 and 2 are the prefix and suffix, respectively, of whatever output, | |
412 " if any, is going to be produced. | |
413 " (defaults to empty strings) | |
414 " - arg 3 is the text to be shown if tags are currently being generated. | |
415 " (defaults to 'TAGS') | |
416 " | |
417 function! autotags#statusline(...) abort | |
418 if !exists('b:autotags_file') | |
419 " This buffer doesn't have autotags. | |
420 return '' | |
421 endif | |
422 | |
423 " Figure out what the user is customizing. | |
424 let l:gen_msg = 'TAGS' | |
425 if a:0 >= 0 | |
426 let l:gen_msg = a:1 | |
427 endif | |
428 | |
429 " To make this function as fast as possible, we first check whether the | |
430 " current buffer's tags file is 'maybe' being generated. This provides a | |
431 " nice and quick bail out for 99.9% of cases before we need to this the | |
432 " file-system to check the lock file. | |
433 let l:abs_tag_file = fnamemodify(b:autotags_file, ':p') | |
434 let l:found = index(s:maybe_in_progress, l:abs_tag_file) | |
435 if l:found < 0 | |
436 return '' | |
437 endif | |
438 " It's maybe generating! Check if the lock file is still there. | |
439 if !filereadable(l:abs_tag_file . '.lock') | |
440 call remove(s:maybe_in_progress, l:found) | |
441 return '' | |
442 endif | |
443 " It's still there! So probably `ctags` is still running... | |
444 " (although there's a chance it crashed, or the script had a problem, and | |
445 " the lock file has been left behind... we could try and run some | |
446 " additional checks here to see if it's legitimately running, and | |
447 " otherwise delete the lock file... maybe in the future...) | |
448 return l:gen_msg | |
449 endfunction | |
450 | |
451 " }}} | |
452 |