aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-06-30 20:49:25 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-06-30 20:49:25 +1200
commitd2fa881601e47b724dec9af5ef482f99e2d9f904 (patch)
tree6e5440272bd0ec0bac0fe6a20f12359af2869cd3
parentMerge branch 'hotfix/v0.4.2' (diff)
downloadvim-auto-cache-dirs-d2fa881601e47b724dec9af5ef482f99e2d9f904.tar.gz
vim-auto-cache-dirs-d2fa881601e47b724dec9af5ef482f99e2d9f904.zip
Revert "Remove accidentally committed backup"
This reverts commit 912b4da69a48710fddbca6498c304dde0c2d5c91. Removed the wrong file! Today is not going well.
-rw-r--r--plugin/auto_cache_dirs.vim139
1 files changed, 139 insertions, 0 deletions
diff --git a/plugin/auto_cache_dirs.vim b/plugin/auto_cache_dirs.vim
new file mode 100644
index 0000000..809ee31
--- /dev/null
+++ b/plugin/auto_cache_dirs.vim
@@ -0,0 +1,139 @@
+"
+" auto_cache_dirs.vim: Configure 'backupdir', 'directory', and 'undodir'
+" automatically, including trying hard to create them, and leaving the options
+" untouched if we can't.
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if exists('g:loaded_auto_cache_dirs') || &compatible
+ finish
+endif
+if v:version < 600
+ finish
+endif
+let g:loaded_auto_cache_dirs = 1
+
+" 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:dir)
+ return 1
+ endif
+
+ " Try Vim's native mkdir() first
+ if exists('*mkdir')
+ 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)
+ 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 . s:ShellEscape(a:dir)
+ endif
+
+ " Return whether the directory exists now
+ return isdirectory(a:dir)
+
+endfunction
+
+" 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
+ if (has('win32') || has('win64')) && !&shellslash
+ return '"' . substitute(a:string, '"', '""', 'g') . '"'
+ else
+ return "'" . substitute(a:string, "'", "'\\\\''", 'g') . "'"
+ endif
+ endif
+
+endfunction
+
+" 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 = s:root . '/' . a:subdir
+
+ " Verify or create the dir
+ if !s:EstablishDir(l:dir)
+ echoerr 'Could not create ' . l:dir
+ return
+ endif
+
+ " 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
+
+" If the root directory hasn't been specified with a global variable, we'll
+" try to figure out a sensible value for it
+if exists('g:auto_cache_dirs_root')
+ let s:root = g:auto_cache_dirs_root
+else
+
+ " 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 s: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 s:root = $HOME . '/.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 s:root = expand(strpart(&runtimepath, 0, stridx(&runtimepath, ',')))
+ endif
+
+endif
+
+" Don't proceed if we have a blank root dir
+if !strlen(s:root)
+ echoerr 'Blank or unexpanded root dir'
+ finish
+endif
+
+" Decide whether to append double-slash for supported options
+let s:name_path = exists('g:auto_cache_dirs_name_path')
+ \ ? g:auto_cache_dirs_name_path
+ \ : 1
+
+" Set backup, swap, and undo data directories as configured
+if !exists('g:auto_cache_dirs_backup') || g:auto_cache_dirs_backup
+ call s:SetDir('backup', 'backupdir', 'backup',
+ \ 0)
+endif
+if !exists('g:auto_cache_dirs_swap') || g:auto_cache_dirs_swap
+ call s:SetDir('swapfile', 'directory', 'swap',
+ \ s:name_path)
+endif
+if (!exists('g:auto_cache_dirs_undo') || g:auto_cache_dirs_undo)
+ \ && has('persistent_undo')
+ call s:SetDir('undofile', 'undodir', 'undo',
+ \ s:name_path)
+endif