changeset 168:e59321cbaff7

Use scope-local functions This avoids overriding local cwd settings, as vim allows `lcd` for window-local working directory and neovim supports additionally `tcd` for tab-local working directory.
author Henry Kupty <hkupty@gmail.com>
date Mon, 10 Oct 2016 19:04:05 -0300
parents 95092f4fbc4b
children 95afd985a4c3
files autoload/gutentags.vim autoload/gutentags/cscope.vim autoload/gutentags/ctags.vim
diffstat 3 files changed, 25 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/autoload/gutentags.vim	Thu Sep 22 21:06:58 2016 -0700
+++ b/autoload/gutentags.vim	Mon Oct 10 19:04:05 2016 -0300
@@ -2,6 +2,23 @@
 
 " Utilities {{{
 
+function! gutentags#pwd()
+  if has('nvim')
+    return haslocaldir() ? getcwd(0, 0) : haslocaldir(-1, 0) ? getcwd(-1, 0) : getcwd()
+  else
+    return haslocaldir() ? getcwd(0, 0) : getcwd()
+  endif
+endfunction
+
+function! gutentags#chdir(path)
+  if has('nvim')
+    let chdir = haslocaldir() ? 'lcd' : haslocaldir(-1, 0) ? 'tcd' : 'cd'
+  else
+    let chdir = haslocaldir() ? 'lcd' : 'cd'
+  endif
+  execute chdir a:path
+endfunction
+
 " Throw an exception message.
 function! gutentags#throw(message)
     let v:errmsg = "gutentags: " . a:message
@@ -362,8 +379,8 @@
     " Switch to the project root to make the command line smaller, and make
     " it possible to get the relative path of the filename to parse if we're
     " doing an incremental update.
-    let l:prev_cwd = getcwd()
-    execute "chdir " . fnameescape(l:proj_dir)
+    let l:prev_cwd = gutentags#pwd()
+    call gutentags#chdir(fnameescape(l:proj_dir))
     try
         call call("gutentags#".a:module."#generate",
                     \[l:proj_dir, l:tags_file, a:write_mode])
@@ -372,7 +389,7 @@
         echom v:exception
     finally
         " Restore the current directory...
-        execute "chdir " . fnameescape(l:prev_cwd)
+        call gutentags#chdir(fnameescape(l:prev_cwd))
     endtry
 endfunction
 
--- a/autoload/gutentags/cscope.vim	Thu Sep 22 21:06:58 2016 -0700
+++ b/autoload/gutentags/cscope.vim	Mon Oct 10 19:04:05 2016 -0300
@@ -64,7 +64,7 @@
     let l:cmd .= gutentags#get_execute_cmd_suffix()
 
     call gutentags#trace("Running: " . l:cmd)
-    call gutentags#trace("In:      " . getcwd())
+    call gutentags#trace("In:      " . gutentags#pwd())
     if !g:gutentags_fake
         if !(has('nvim') && exists('*jobwait'))
             if !g:gutentags_trace
--- a/autoload/gutentags/ctags.vim	Thu Sep 22 21:06:58 2016 -0700
+++ b/autoload/gutentags/ctags.vim	Mon Oct 10 19:04:05 2016 -0300
@@ -37,7 +37,7 @@
 function! gutentags#ctags#generate(proj_dir, tags_file, write_mode) abort
     " Get to the tags file directory because ctags is finicky about
     " these things.
-    let l:prev_cwd = getcwd()
+    let l:prev_cwd = gutentags#pwd()
     let l:tags_file_exists = filereadable(a:tags_file)
 
     if l:tags_file_exists && g:gutentags_ctags_check_tagfile
@@ -59,7 +59,7 @@
         " root directory.
         let l:actual_proj_dir = '.'
         let l:actual_tags_file = g:gutentags_tagfile
-        execute "chdir " . fnameescape(a:proj_dir)
+        call gutentags#chdir(fnameescape(a:proj_dir))
     else
         " else: the tags file goes in a cache directory, so we need to specify
         " all the paths absolutely for `ctags` to do its job correctly.
@@ -121,7 +121,7 @@
         let l:cmd .= gutentags#get_execute_cmd_suffix()
 
         call gutentags#trace("Running: " . l:cmd)
-        call gutentags#trace("In:      " . getcwd())
+        call gutentags#trace("In:      " . gutentags#pwd())
         if !g:gutentags_fake
             " Run the background process.
             if !g:gutentags_trace
@@ -139,7 +139,7 @@
         call gutentags#trace("")
     finally
         " Restore the previous working directory.
-        execute "chdir " . fnameescape(l:prev_cwd)
+        call gutentags#chdir(fnameescape(l:prev_cwd))
     endtry
 endfunction