diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2018-06-01 23:41:00 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2018-06-01 23:41:00 +1200 |
commit | b51aebde5bec9d58dba96377e0348fa82274bb83 (patch) | |
tree | d1074d8f0cfcde1bee676e0aacd09ba571ddec60 | |
parent | Initial commit (diff) | |
parent | Add VERSION (diff) | |
download | vim-auto-cache-dirs-b51aebde5bec9d58dba96377e0348fa82274bb83.tar.gz vim-auto-cache-dirs-b51aebde5bec9d58dba96377e0348fa82274bb83.zip |
Merge branch 'release/v0.2.0'v0.2.0
* release/v0.2.0:
Add VERSION
Correct variable evaluation
Adjust variable name and comments
Escape item properly before adding to path
Report the expanded name of the failed creation
Use function! consistently
Remove now-unneeded s:Run() function
Short-circuit if dir cannot be established
Add some comments and spacing
Completely refactor SetDir() calling
Add backported shellslashes() fallback function
Use consistent line breaks in preamble
Use consistent approach to expansion
Rename local variable
Rename g:..._complete_path option in Vim plugin
-rw-r--r-- | VERSION | 1 | ||||
-rw-r--r-- | doc/auto_cache_dirs.txt | 10 | ||||
-rw-r--r-- | plugin/auto_cache_dirs.vim | 105 |
3 files changed, 72 insertions, 44 deletions
@@ -0,0 +1 @@ +0.2.0 diff --git a/doc/auto_cache_dirs.txt b/doc/auto_cache_dirs.txt index 814db7d..5d3928f 100644 --- a/doc/auto_cache_dirs.txt +++ b/doc/auto_cache_dirs.txt @@ -48,11 +48,11 @@ files. Defaults to 1 (true). This requires the |+persistent_undo| feature. Set `g:auto_cache_dirs_root` to the directory that should contain the files. See DESCRIPTION above for how the default value for this is determined. - *g:auto_cache_dirs_complete_path* -Set `g:auto_cache_dirs_complete_path` to specify whether 'directory' and -'undodir' (but not 'backupdir') should have a double-slash appended to the end -of the added path, to tell Vim to save the cache files with the complete path -names of the files to which they refer. Defaults to 1 (true). + *g:auto_cache_dirs_name_path* +Set `g:auto_cache_dirs_name_path` to specify whether 'directory' and 'undodir' +(but not 'backupdir') should have a double-slash appended to the end of the +added path, to tell Vim to save the cache files with the complete path names +of the files to which they refer. Defaults to 1 (true). AUTHOR *auto_cache_dirs-author* diff --git a/plugin/auto_cache_dirs.vim b/plugin/auto_cache_dirs.vim index 3937679..7224456 100644 --- a/plugin/auto_cache_dirs.vim +++ b/plugin/auto_cache_dirs.vim @@ -23,8 +23,8 @@ endif if !exists('g:auto_cache_dirs_undo') let g:auto_cache_dirs_undo = 1 endif -if !exists('g:auto_cache_dirs_complete_path') - let g:auto_cache_dirs_complete_path = 1 +if !exists('g:auto_cache_dirs_name_path') + let g:auto_cache_dirs_name_path = 1 endif " If g:auto_cache_dirs_root isn't already set, we'll try to figure out a @@ -34,82 +34,109 @@ if !exists('g:auto_cache_dirs_root') " If $XDG_CACHE_HOME is set, it's likely that using that is what the user " will be least surprised by if exists('$XDG_CACHE_HOME') - let g:auto_cache_dirs_root = $XDG_CACHE_HOME.'/vim' + let g:auto_cache_dirs_root + \ = '$XDG_CACHE_HOME/vim' " If there's no explicit cache dir, but we are running some sort of Unix, " try to put it in ~/.cache elseif has('unix') - let g:auto_cache_dirs_root = expand('~/.cache/vim') + let g:auto_cache_dirs_root + \ = '~/.cache/vim' " If not, we'll try putting the directory into the first element of the " 'runtimepath' value, so they'll at least be findable else let g:auto_cache_dirs_root - \ = strpart(&runtimepath, 0, stridx(&runtimepath, ',')) + \ = strpart(&runtimepath, 0, stridx(&runtimepath, ',')) endif endif -" Try hard to create a directory -function! s:EstablishDir(dirname) +" Try hard to create a directory with the given expanded name +function! s:EstablishDir(dir) " If it's already a directory, we have nothing to do - if isdirectory(a:dirname) + if isdirectory(a:dir) return 1 endif " Try Vim's native mkdir() first if exists('*mkdir') - silent! call mkdir(a:dirname, 'p', 0700) + silent! call mkdir(a:dir, 'p', 0700) " Failing that, try to use an OS-dependent command " (Fortunately, Unix and Windows are the only OS types in the world) - elseif has('*shellescape') + else if has('unix') let l:mkdir = '!mkdir -m 0700 -p' elseif has('win32') || has('win64') let l:mkdir = '!mkdir' endif - silent! execute l:mkdir . ' ' . shellescape(expand(a:dirname)) + silent! execute l:mkdir . ' ' . s:ShellEscape(a:dir) endif " Return whether the directory exists now - return isdirectory(a:dirname) + return isdirectory(a:dir) endfunction -" If we can find or create a directory for it, set an option and its related -" directory option -function s:SetDir(subdir, option, optiondir) - let l:dir = g:auto_cache_dirs_root . '/' . a:subdir - if s:EstablishDir(l:dir) - execute 'set ' . a:option - execute 'set ' . a:optiondir . '^=' . l:dir +" Wrapper around shellescape(), with fallback backport if not available +function! s:ShellEscape(string) + + " Use native function if we can + if has('*shellescape') + return shellescape(a:string) + + " If not, manually write out what the :help for shellescape() says it does else - echoerr 'Could not create ' . l:dir + if (has('win32') || has('win64')) && !&shellslash + return '"' . substitute(a:string, '"', '""', 'g') . '"' + else + return "'" . substitute(a:string, "'", "'\\\\''", 'g') . "'" + endif endif + endfunction -" Set backup, swap, and undo data directories as configured -function s:Run() - if g:auto_cache_dirs_backup - let l:subdir = 'backup' - call s:SetDir(l:subdir, 'backup', 'backupdir') - endif - if g:auto_cache_dirs_swap - let l:subdir = 'swap' - if g:auto_cache_dirs_complete_path - let l:subdir .= '//' - endif - call s:SetDir(l:subdir, 'swapfile', 'directory') +" If we can find or create a directory for it, set an option and its related +" path option +function! s:SetDir(option_bool, option_path, subdir, name_path) + + " Build full path + let l:dir = g:auto_cache_dirs_root . '/' . a:subdir + + " Verify or create the dir + let l:exp = expand(l:dir) + if !s:EstablishDir(expand(l:exp)) + echoerr 'Could not create ' . l:exp + return endif - if g:auto_cache_dirs_undo && has('persistent_undo') - let l:subdir = 'undo' - if g:auto_cache_dirs_complete_path - let l:subdir .= '//' - endif - call s:SetDir(l:subdir, 'undofile', 'undodir') + + " Set bool option (e.g. 'backup') + execute 'set ' . a:option_bool + + " Determine element to add to accompanying path option; escape spaces and + " backslashes, and append double-slash if requested + let l:item = substitute(l:dir, '[ ,\\]', '\\&', 'g') + if a:name_path + let l:item = l:item . '//' endif + + " Set path option (e.g. 'backupdir') + execute 'set ' . a:option_path . '^=' . l:item + endfunction -call s:Run() +" Set backup, swap, and undo data directories as configured +if g:auto_cache_dirs_backup + call s:SetDir('backup', 'backupdir', 'backup', + \ 0) +endif +if g:auto_cache_dirs_swap + call s:SetDir('swapfile', 'directory', 'swap', + \ g:auto_cache_dirs_name_path) +endif +if g:auto_cache_dirs_undo && has('persistent_undo') + call s:SetDir('undofile', 'undodir', 'undo', + \ g:auto_cache_dirs_name_path) +endif |