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