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