comparison autoload/gutentags/ctags.vim @ 98:d645125192aa

Fix more problems with paths and spaces in them.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 25 Feb 2016 21:04:46 -0800
parents e03a661dc983
children 05fc1e2172cc 7fd3958a1c0d
comparison
equal deleted inserted replaced
97:794ee111bce6 98:d645125192aa
20 20
21 if !exists('g:gutentags_ctags_check_tagfile') 21 if !exists('g:gutentags_ctags_check_tagfile')
22 let g:gutentags_ctags_check_tagfile = 0 22 let g:gutentags_ctags_check_tagfile = 0
23 endif 23 endif
24 24
25 if !exists('g:gutentags_ctags_no_space_in_paths')
26 let g:gutentags_ctags_no_space_in_paths = 1
27 endif
28
29 " }}} 25 " }}}
30 26
31 " Gutentags Module Interface {{{ 27 " Gutentags Module Interface {{{
32 28
33 let s:runner_exe = gutentags#get_plat_file('update_tags') 29 let s:runner_exe = gutentags#get_plat_file('update_tags')
34 30
35 function! gutentags#ctags#init(project_root) abort 31 function! gutentags#ctags#init(project_root) abort
36 " Figure out the path to the tags file. 32 " Figure out the path to the tags file.
37 if g:gutentags_ctags_no_space_in_paths
38 " We need to replace spaces in the project root because `ctags` seems
39 " incapable of reading/writing to tags files with spaces.
40 let l:fixed_project_root = tr(a:project_root, ' ', '_')
41 else
42 let l:fixed_project_root = a:project_root
43 endif
44 let b:gutentags_files['ctags'] = gutentags#get_cachefile( 33 let b:gutentags_files['ctags'] = gutentags#get_cachefile(
45 \l:fixed_project_root, g:gutentags_tagfile) 34 \a:project_root, g:gutentags_tagfile)
46 35
47 " Set the tags file for Vim to use. 36 " Set the tags file for Vim to use.
48 if g:gutentags_auto_set_tags 37 if g:gutentags_auto_set_tags
49 execute 'setlocal tags^=' . fnameescape(b:gutentags_files['ctags']) 38 execute 'setlocal tags^=' . fnameescape(b:gutentags_files['ctags'])
50 endif 39 endif
60 49
61 function! gutentags#ctags#generate(proj_dir, tags_file, write_mode) abort 50 function! gutentags#ctags#generate(proj_dir, tags_file, write_mode) abort
62 " Get to the tags file directory because ctags is finicky about 51 " Get to the tags file directory because ctags is finicky about
63 " these things. 52 " these things.
64 let l:prev_cwd = getcwd() 53 let l:prev_cwd = getcwd()
65 let l:work_dir = fnamemodify(a:tags_file, ':h')
66 let l:tags_file_exists = filereadable(a:tags_file) 54 let l:tags_file_exists = filereadable(a:tags_file)
67 55
68 if l:tags_file_exists && g:gutentags_ctags_check_tagfile 56 if l:tags_file_exists && g:gutentags_ctags_check_tagfile
69 let l:first_lines = readfile(a:tags_file, '', 1) 57 let l:first_lines = readfile(a:tags_file, '', 1)
70 if len(l:first_lines) == 0 || stridx(l:first_lines[0], '!_TAG_') != 0 58 if len(l:first_lines) == 0 || stridx(l:first_lines[0], '!_TAG_') != 0
73 \":GutentagsUpdate!.") 61 \":GutentagsUpdate!.")
74 return 62 return
75 endif 63 endif
76 endif 64 endif
77 65
78 execute "chdir " . fnameescape(l:work_dir) 66 if g:gutentags_cache_dir == ""
67 " If we don't use the cache directory, let's just use the tag filename
68 " as specified by the user, and change the working directory to the
69 " project root.
70 " Note that if we don't do this and pass a full path, `ctags` gets
71 " confused if the paths have spaces -- but not if you're *in* the
72 " root directory.
73 let l:actual_proj_dir = '.'
74 let l:actual_tags_file = g:gutentags_tagfile
75 execute "chdir " . fnameescape(a:proj_dir)
76 else
77 " else: the tags file goes in a cache directory, so we need to specify
78 " all the paths absolutely for `ctags` to do its job correctly.
79 let l:actual_proj_dir = a:proj_dir
80 let l:actual_tags_file = a:tags_file
81 endif
79 82
80 try 83 try
81 " Build the command line. 84 " Build the command line.
82 let l:cmd = gutentags#get_execute_cmd() . s:runner_exe 85 let l:cmd = gutentags#get_execute_cmd() . s:runner_exe
83 let l:cmd .= ' -e "' . s:get_ctags_executable(a:proj_dir) . '"' 86 let l:cmd .= ' -e "' . s:get_ctags_executable(a:proj_dir) . '"'
84 let l:cmd .= ' -t "' . a:tags_file . '"' 87 let l:cmd .= ' -t "' . l:actual_tags_file . '"'
85 let l:cmd .= ' -p "' . a:proj_dir . '"' 88 let l:cmd .= ' -p "' . l:actual_proj_dir . '"'
86 if a:write_mode == 0 && l:tags_file_exists 89 if a:write_mode == 0 && l:tags_file_exists
87 let l:full_path = expand('%:p') 90 let l:full_path = expand('%:p')
88 let l:cmd .= ' -s "' . l:full_path . '"' 91 let l:cmd .= ' -s "' . l:full_path . '"'
89 endif 92 endif
90 " Pass the Gutentags options file first, and then the project specific 93 " Pass the Gutentags options file first, and then the project specific
106 if g:gutentags_pause_after_update 109 if g:gutentags_pause_after_update
107 let l:cmd .= ' -c' 110 let l:cmd .= ' -c'
108 endif 111 endif
109 if g:gutentags_trace 112 if g:gutentags_trace
110 if has('win32') 113 if has('win32')
111 let l:cmd .= ' -l "' . a:tags_file . '.log"' 114 let l:cmd .= ' -l "' . l:actual_tags_file . '.log"'
112 else 115 else
113 let l:cmd .= ' > "' . a:tags_file . '.log" 2>&1' 116 let l:cmd .= ' > "' . l:actual_tags_file . '.log" 2>&1'
114 endif 117 endif
115 else 118 else
116 if !has('win32') 119 if !has('win32')
117 let l:cmd .= ' > /dev/null 2>&1' 120 let l:cmd .= ' > /dev/null 2>&1'
118 endif 121 endif