aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-05-31 21:45:30 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-05-31 21:45:30 +1200
commit1bfe208f58f055f605806d7f5f05a208ae643ce1 (patch)
tree478b2d33fc37049dff12cbba6cdc601381e18e60
downloadvim-auto-cache-dirs-1bfe208f58f055f605806d7f5f05a208ae643ce1.tar.gz
vim-auto-cache-dirs-1bfe208f58f055f605806d7f5f05a208ae643ce1.zip
Initial commitv0.1.0
New plugin heavily modified from the originally separate auto_backupdir.vim, auto_swapdir.vim, and auto_undodir.vim plugins from tejr's dotfiles suite, v0.37.1.
-rw-r--r--README.markdown32
-rw-r--r--doc/auto_cache_dirs.txt65
-rw-r--r--plugin/auto_cache_dirs.vim115
3 files changed, 212 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
new file mode 100644
index 0000000..faf9892
--- /dev/null
+++ b/README.markdown
@@ -0,0 +1,32 @@
+auto\_cache\_dirs.vim
+=====================
+
+This plugin attempts to intelligently set and create the directories for the
+`'backupdir'`, `'directory'`, and `'undodir'` options in a sensible root
+location. It chooses the root directory by trying each of these in succession:
+
+- If `g:auto_cache_dirs` is set when the plugin loads, it uses that.
+- If the environment variable `$XDG_CONFIG_HOME` is set, it uses that, with
+ "/vim" appended.
+- If the system is Unix, it uses `~/.cache/vim`.
+- Otherwise, it uses the first element of the value of `'runtimepath'`.
+
+The plugin tries hard to create the directories, trying Vim's native `mkdir()`
+function if available first, and falling back to shell commands otherwise. At
+the time of writing, the latter only works for Unix and Windows. If you use
+another system and would like to see a routine for it added, please send a
+patch through.
+
+If the directory can be created, it sets the relevant option feature flag and
+prepends the path to that option path list.
+
+If the directory cannot be created, a warning is printed, and the options as
+the plugin found them are left untouched.
+
+License
+-------
+
+Copyright (c) [Tom Ryder][1]. Distributed under the same terms as Vim itself.
+See `:help license`.
+
+[1]: https://sanctum.geek.nz/
diff --git a/doc/auto_cache_dirs.txt b/doc/auto_cache_dirs.txt
new file mode 100644
index 0000000..814db7d
--- /dev/null
+++ b/doc/auto_cache_dirs.txt
@@ -0,0 +1,65 @@
+*auto_cache_dirs.txt* For Vim version 7.0 Last change: 2018 May 31
+
+DESCRIPTION *auto_cache_dirs*
+
+This plugin attempts to intelligently set and create the directories for the
+'backupdir', 'directory', and 'undodir' options in a sensible root location. It
+chooses the root directory by trying each of these in succession:
+
+- If |g:auto_cache_dirs| is set when the plugin loads, it uses that.
+- If the environment variable `$XDG_CONFIG_HOME` is set, it uses that, with
+ "/vim" appended.
+- If the system is Unix, it uses `~/.cache/vim`.
+- Otherwise, it uses the first element of the value of 'runtimepath'.
+
+The plugin tries hard to create the directories, trying Vim's native |mkdir()|
+function if available first, and falling back to shell commands otherwise. At
+the time of writing, the latter only works for Unix and Windows. If you use
+another system and would like to see a routine for it added, please send a
+patch through.
+
+If the directory can be created, it sets the relevant option feature flag and
+prepends the path to that option path list.
+
+If the directory cannot be created, a warning is printed, and the options as
+the plugin found them are left untouched.
+
+REQUIREMENTS *auto_cache_dirs-requirements*
+
+This plugin is only available if 'compatible' is not set.
+
+OPTIONS *auto_cache_dirs-options*
+
+There are a few options you can set in your |vimrc| before loading the plugin:
+
+ *g:auto_cache_dirs_backup*
+Set `g:auto_cache_dirs_backup` to create 'backupdir' and set options for
+'backup' files. Defaults to 1 (true).
+
+ *g:auto_cache_dirs_swap*
+Set `g:auto_cache_dirs_swap` to create 'directory' and set options for
+'swapfile' files. Defaults to 1 (true).
+
+ *g:auto_cache_dirs_undo*
+Set `g:auto_cache_dirs_undo` to create 'undodir' and set options for 'undofile'
+files. Defaults to 1 (true). This requires the |+persistent_undo| feature.
+
+ *g:auto_cache_dirs_root*
+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).
+
+AUTHOR *auto_cache_dirs-author*
+
+Written and maintained by Tom Ryder <tom@sanctum.geek.nz>.
+
+LICENSE *auto_cache_dirs-license*
+
+Licensed for distribution under the same terms as Vim itself (see |license|).
+
+ vim:tw=78:ts=8:ft=help:norl:
diff --git a/plugin/auto_cache_dirs.vim b/plugin/auto_cache_dirs.vim
new file mode 100644
index 0000000..3937679
--- /dev/null
+++ b/plugin/auto_cache_dirs.vim
@@ -0,0 +1,115 @@
+"
+" 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
+let g:loaded_auto_cache_dirs = 1
+
+" Default options: do all three of backup, swap, and undo, and add
+" double-slash to name cache files with complete paths for the options that
+" support it.
+if !exists('g:auto_cache_dirs_backup')
+ let g:auto_cache_dirs_backup = 1
+endif
+if !exists('g:auto_cache_dirs_swap')
+ let g:auto_cache_dirs_swap = 1
+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
+endif
+
+" If g:auto_cache_dirs_root isn't already set, we'll try to figure out a
+" sensible value for it
+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'
+
+ " 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')
+
+ " 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, ','))
+ endif
+
+endif
+
+" Try hard to create a directory
+function! s:EstablishDir(dirname)
+
+ " If it's already a directory, we have nothing to do
+ if isdirectory(a:dirname)
+ return 1
+ endif
+
+ " Try Vim's native mkdir() first
+ if exists('*mkdir')
+ silent! call mkdir(a:dirname, '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')
+ 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))
+ endif
+
+ " Return whether the directory exists now
+ return isdirectory(a:dirname)
+
+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
+ else
+ echoerr 'Could not create ' . l:dir
+ 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')
+ 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')
+ endif
+endfunction
+
+call s:Run()