From 69cac9e123d74fa9fecf96177cb79d8370a833f1 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 10 Nov 2017 21:13:21 +1300 Subject: Move backup, swap, and undo dir logic into plugins --- vim/config/backup.vim | 33 +++++++------------------ vim/config/swapfile.vim | 44 ++++++++++----------------------- vim/config/undo.vim | 22 +++++------------ vim/doc/auto_backupdir.txt | 12 +++++++++ vim/doc/auto_swapdir.txt | 12 +++++++++ vim/doc/auto_undodir.txt | 14 +++++++++++ vim/plugin/auto_backupdir.vim | 56 ++++++++++++++++++++++++++++++++++++++++++ vim/plugin/auto_swapdir.vim | 56 ++++++++++++++++++++++++++++++++++++++++++ vim/plugin/auto_undodir.vim | 57 +++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 235 insertions(+), 71 deletions(-) create mode 100644 vim/doc/auto_backupdir.txt create mode 100644 vim/doc/auto_swapdir.txt create mode 100644 vim/doc/auto_undodir.txt create mode 100644 vim/plugin/auto_backupdir.vim create mode 100644 vim/plugin/auto_swapdir.vim create mode 100644 vim/plugin/auto_undodir.vim diff --git a/vim/config/backup.vim b/vim/config/backup.vim index 718647fc..8735a094 100644 --- a/vim/config/backup.vim +++ b/vim/config/backup.vim @@ -1,26 +1,11 @@ -" Use backup features if on a UNIX-like system and not using sudo(8) -if !strlen($SUDO_USER) && has('unix') +" Default to no backup files at all, in a way that even ancient/tiny Vims will +" understand; the auto_backupdir.vim plugin will take care of re-enabling this +set nobackup +set nowritebackup - " Keep backups with a .bak extension in ~/.vim/backup; the double-slash at - " the end of the directory is supposed to prod Vim into keeping the full - " path to the file in its backup filename to avoid collisions, but I don't - " think it actually works for backups, just undo and swap files - set backup - set backupext=.bak - set backupdir^=~/.vim/backup// +" If backps are enabled, use a more explicit and familiar backup suffix +set backupext=.bak - " This option already includes various temporary directories, but we - " append to it so that we don't back up anything in a shared memory - " filesystem either - set backupskip+=*/shm/* - - " Create the backup directory if necessary and possible - if !isdirectory($HOME . '/.vim/backup') && exists('*mkdir') - call mkdir($HOME . '/.vim/backup', 'p', 0700) - endif - -" Don't use backups at all otherwise -else - set nobackup - set nowritebackup -endif +" Don't back up files in anything named */shm/; they might be password +" files +set backupskip+=*/shm/* diff --git a/vim/config/swapfile.vim b/vim/config/swapfile.vim index 778ae2f0..bf91aa6b 100644 --- a/vim/config/swapfile.vim +++ b/vim/config/swapfile.vim @@ -1,32 +1,14 @@ -" Swap files are used if using Unix and not using sudo(8); I very seldom need -" them, but they are occasionally useful after a crash, and they don't really -" get in the way if kept in their own directory -if !strlen($SUDO_USER) && has('unix') - - " Use swap files but keep them in ~/.vim/swap; the double-slash at the end - " of the directory prods Vim into keeping the full path to the file in its - " undo filename to avoid collisions; the same thing works for undo files - set swapfile - set directory^=~/.vim/swap// - - " Create the ~/.vim/swap directory if necessary and possible - if !isdirectory($HOME . '/.vim/swap') && exists('*mkdir') - call mkdir($HOME . '/.vim/swap', 'p', 0700) - endif - - " Don't keep swap files for files in temporary directories or shared memory - " filesystems; this is because they're used as scratch spaces for tools - " like sudoedit(8) and pass(1) and hence could present a security problem - if has('autocmd') - augroup dotfiles_swap_skip - autocmd! - autocmd BufNewFile,BufReadPre - \ /tmp/*,$TMPDIR/*,$TMP/*,$TEMP/*,*/shm/* - \ setlocal noswapfile - augroup END - endif - -" Otherwise, don't use swap files at all -else - set noswapfile +" Default to no swapfile files at all, in a way that even ancient/tiny Vims +" will understand; the auto_swapdir.vim plugin will take care of this +set noswapfile + +" Don't keep swap files from temporary directories or shared memory in case +" they're secrets +if has('autocmd') + augroup dotfiles_swap_skip + autocmd! + autocmd BufNewFile,BufReadPre + \ /tmp/*,$TMPDIR/*,$TMP/*,$TEMP/*,*/shm/* + \ setlocal noswapfile + augroup END endif diff --git a/vim/config/undo.vim b/vim/config/undo.vim index c9539665..c31780e7 100644 --- a/vim/config/undo.vim +++ b/vim/config/undo.vim @@ -4,23 +4,13 @@ inoremap u " Keep screeds of undo history set undolevels=2000 -" Keep undo history in a separate file if the feature is available, we're on -" Unix, and not using sudo(8); this goes really well with undo visualization -" plugins like Gundo or Undotree. -if !strlen($SUDO_USER) && has('unix') && has('persistent_undo') +" 'undodir' and 'undofile' settings will be taken care of by the +" auto_undodir.vim plugin if applicable/possible +if has('persistent_undo') + set noundofile - " Keep per-file undo history in ~/.vim/undo; the double-slash at the end - " of the directory prods Vim into keeping the full path to the file in its - " undo filename to avoid collisions; the same thing works for swap files - set undofile - set undodir^=~/.vim/undo// - - " Create the ~/.vim/undo directory if necessary and possible - if !isdirectory($HOME . '/.vim/undo') && exists('*mkdir') - call mkdir($HOME . '/.vim/undo', 'p', 0700) - endif - - " Don't track changes to sensitive files + " Don't keep undo files from temporary directories or shared memory in case + " they're secrets if has('autocmd') augroup dotfiles_undo_skip autocmd! diff --git a/vim/doc/auto_backupdir.txt b/vim/doc/auto_backupdir.txt new file mode 100644 index 00000000..c5b6ebad --- /dev/null +++ b/vim/doc/auto_backupdir.txt @@ -0,0 +1,12 @@ +*auto_backupdir.txt* Automatically create 'backupdir' in 'realtimepath' + +Author: Tom Ryder +License: Same terms as Vim itself (see |license|) + +This plugin attempts to create a directory "backup" in the directory named by +the first element of 'realtimepath', and enables 'backup' with that as the +'backupdir' if it succeeds or if the directory already exists. + +This plugin lives in Tom Ryder's "dotfiles" suite, and will eventually be spun +off into a separate distribution as it solidifies and this documentation +improves. diff --git a/vim/doc/auto_swapdir.txt b/vim/doc/auto_swapdir.txt new file mode 100644 index 00000000..f88cfcf3 --- /dev/null +++ b/vim/doc/auto_swapdir.txt @@ -0,0 +1,12 @@ +*auto_swapdir.txt* Automatically create 'swapdir' in 'realtimepath' + +Author: Tom Ryder +License: Same terms as Vim itself (see |license|) + +This plugin attempts to create a directory "swap" in the directory named by the +first element of 'realtimepath', and enables 'swapfile' with that as the +'directory' if it succeeds or if the directory already exists. + +This plugin lives in Tom Ryder's "dotfiles" suite, and will eventually be spun +off into a separate distribution as it solidifies and this documentation +improves. diff --git a/vim/doc/auto_undodir.txt b/vim/doc/auto_undodir.txt new file mode 100644 index 00000000..c782a70e --- /dev/null +++ b/vim/doc/auto_undodir.txt @@ -0,0 +1,14 @@ +*auto_undodir.txt* Automatically create 'undodir' in 'realtimepath' + +Author: Tom Ryder +License: Same terms as Vim itself (see |license|) + +This plugin attempts to create a directory "undo" in the directory named by the +first element of 'realtimepath', and enables 'undofile' with that as the +'undodir' if it succeeds or if the directory already exists. + +It requires the +persistent_undo feature. + +This plugin lives in Tom Ryder's "dotfiles" suite, and will eventually be spun +off into a separate distribution as it solidifies and this documentation +improves. diff --git a/vim/plugin/auto_backupdir.vim b/vim/plugin/auto_backupdir.vim new file mode 100644 index 00000000..f15e7ce6 --- /dev/null +++ b/vim/plugin/auto_backupdir.vim @@ -0,0 +1,56 @@ +" +" auto_backupdir.vim: Configure 'backupdir' automatically, including trying +" hard to create it. +" +" Author: Tom Ryder +" License: Same as Vim itself +" +if exists('g:loaded_auto_backupdir') + \ || &compatible + finish +endif +let g:loaded_auto_backupdir = 1 + +" Define the backup path we want +if exists('$VIM_BACKUPDIR') + let s:backupdir = $VIM_BACKUPDIR +else + + " This is imperfect in that it will break if you have a backslashed comma in + " the first component of your &runtimepath, but if you're doing that, you + " probably already have way bigger problems + let s:backupdir + \ = strpart(&runtimepath, 0, stridx(&runtimepath, ',')) + \ . '/backup' +endif + +" If the prospective backup directory does not exist, try hard to create it +if !isdirectory(expand(s:backupdir)) + + " Try Vim's native mkdir() first + if exists('*mkdir') + silent! call mkdir(expand(s:backupdir), 'p', 0700) + + " Failing that, use an OS-dependent command + " (Fortunately, Unix and Windows are the only OS types in the world) + elseif has('unix') + silent! execute '!mkdir -m 0700 -p ' + \ . shellescape(expand(s:backupdir)) + elseif has('win32') || has('win64') + silent! execute '!mkdir ' + \ . shellescape(expand(s:backupdir)) + endif + +endif + +" If the directory exists after that... +if isdirectory(expand(s:backupdir)) + + " Set the backup directory and turn backups on + execute 'set backupdir^=' . s:backupdir . '//' + set backup + +" If not, give up and raise an error +else + echoerr 'Could not create backupdir ' . s:backupdir +endif diff --git a/vim/plugin/auto_swapdir.vim b/vim/plugin/auto_swapdir.vim new file mode 100644 index 00000000..ea41a0f0 --- /dev/null +++ b/vim/plugin/auto_swapdir.vim @@ -0,0 +1,56 @@ +" +" auto_swapdir.vim: Configure 'directory' automatically, including trying hard +" to create it. +" +" Author: Tom Ryder +" License: Same as Vim itself +" +if exists('g:loaded_auto_swapdir') + \ || &compatible + finish +endif +let g:loaded_auto_swapdir = 1 + +" Define the swap path we want +if exists('$VIM_SWAPDIR') + let s:swapdir = $VIM_SWAPDIR +else + + " This is imperfect in that it will break if you have a backslashed comma in + " the first component of your &runtimepath, but if you're doing that, you + " probably already have way bigger problems + let s:swapdir + \ = strpart(&runtimepath, 0, stridx(&runtimepath, ',')) + \ . '/swap' +endif + +" If the prospective swapfile directory does not exist, try hard to create it +if !isdirectory(expand(s:swapdir)) + + " Try Vim's native mkdir() first + if exists('*mkdir') + silent! call mkdir(expand(s:swapdir), 'p', 0700) + + " Failing that, use an OS-dependent command + " (Fortunately, Unix and Windows are the only OS types in the world) + elseif has('unix') + silent! execute '!mkdir -m 0700 -p ' + \ . shellescape(expand(s:swapdir)) + elseif has('win32') || has('win64') + silent! execute '!mkdir ' + \ . shellescape(expand(s:swapdir)) + endif + +endif + +" If the directory exists after that... +if isdirectory(expand(s:swapdir)) + + " Set the swapfile directory and turn swapfiles on + execute 'set directory^=' . s:swapdir . '//' + set swapfile + +" If not, give up and raise an error +else + echoerr 'Could not create swapdir ' . s:swapdir +endif diff --git a/vim/plugin/auto_undodir.vim b/vim/plugin/auto_undodir.vim new file mode 100644 index 00000000..1d20ba95 --- /dev/null +++ b/vim/plugin/auto_undodir.vim @@ -0,0 +1,57 @@ +" +" auto_undodir.vim: Configure 'undodir' automatically, including trying hard +" to create it. +" +" Author: Tom Ryder +" License: Same as Vim itself +" +if exists('g:loaded_auto_undodir') + \ || !has('persistent_undo') + \ || &compatible + finish +endif +let g:loaded_auto_undodir = 1 + +" Define the undo path we want +if exists('$VIM_UNDODIR') + let s:undodir = $VIM_UNDODIR +else + + " This is imperfect in that it will break if you have a backslashed comma in + " the first component of your &runtimepath, but if you're doing that, you + " probably already have way bigger problems + let s:undodir + \ = strpart(&runtimepath, 0, stridx(&runtimepath, ',')) + \ . '/undo' +endif + +" If the prospective undo directory does not exist, try hard to create it +if !isdirectory(expand(s:undodir)) + + " Try Vim's native mkdir() first + if exists('*mkdir') + silent! call mkdir(expand(s:undodir), 'p', 0700) + + " Failing that, use an OS-dependent command + " (Fortunately, Unix and Windows are the only OS types in the world) + elseif has('unix') + silent! execute '!mkdir -m 0700 -p ' + \ . shellescape(expand(s:undodir)) + elseif has('win32') || has('win64') + silent! execute '!mkdir ' + \ . shellescape(expand(s:undodir)) + endif + +endif + +" If the directory exists after that... +if isdirectory(expand(s:undodir)) + + " Set the undo directory and turn persistent undo files on + execute 'set undodir^=' . s:undodir . '//' + set undofile + +" If not, give up and raise an error +else + echoerr 'Could not create undodir ' . s:undodir +endif -- cgit v1.2.3