From 51f45f6af445f736ebc2143ed923bd59899e93da Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 Apr 2020 17:41:53 +1200 Subject: Roll out the beginnings of XDG support for Vim The idea here isn't to remove ~/.vim, but to be able to use ~/.cache and ~/.config on a per-machine basis. --- Makefile | 9 +-------- vim/vimrc | 42 +++++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 514a4444..8e537c84 100644 --- a/Makefile +++ b/Makefile @@ -588,18 +588,11 @@ install-vim-bundle: install-vim-config 'cp -p -- "$$1" $(VIMDIR)/"$${1#*/}"' _ {} \; $(VIM) -e -u NONE -c 'helptags $(VIMDIR)/doc' -c quit -install-vim-cache: - mkdir -p -- \ - $(VIMDIR)/backup \ - $(VIMDIR)/swap \ - $(VIMDIR)/undo \ - $(VIMDIR)/view - install-vim-compiler: mkdir -p -- $(VIMDIR)/compiler cp -p -- vim/compiler/*.vim $(VIMDIR)/compiler -install-vim-config: install-vim-autoload install-vim-cache +install-vim-config: install-vim-autoload cp -p -- vim/vimrc.stub $(HOME)/.vimrc cp -p -- vim/vimrc $(VIMRC) diff --git a/vim/vimrc b/vim/vimrc index aed91793..17d1656e 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -104,6 +104,20 @@ elseif &runtimepath !=# '' let $MYVIM = option#Split(&runtimepath)[0] endif +" TODO: Handle XDG_*_DIRS correctly +let s:cache = exists('$XDG_CACHE_HOME') + \ ? $XDG_CACHE_HOME.'/vim' + \ : '~/.cache/vim' +let s:config = exists('$XDG_CONFIG_HOME') + \ ? $XDG_CONFIG_HOME.'/vim' + \ : '~/.config/vim' +let s:data = exists('$XDG_DATA_HOME') + \ ? $XDG_DATA_HOME.'/vim' + \ : '~/.local/share/vim' + +execute 'set runtimepath^='.option#Escape(option#item#Escape(s:config)) +execute 'set runtimepath^='.option#Escape(option#item#Escape(s:cache)) + " We need a command to reliably establish a full path, whether or not the " directories already exist. We create a wrapper for the autoloaded function " path#Create() with similar calling conventions to mkdir(), but with the ‘p’ @@ -133,8 +147,8 @@ command! -bang -bar -complete=dir -nargs=1 CreatePath " " " -execute 'set viminfo+='.option#Escape('n'.$MYVIM.'/viminfo') -call path#Create($MYVIM) +execute 'set viminfo+='.option#Escape('n'.s:cache.'/viminfo') +call path#Create(s:cache) " Speaking of recorded data in viminfo files, the default Vim limit of a mere " 50 entries for command and search history is pretty stingy. Because I don’t @@ -180,9 +194,9 @@ set history=10000 " set backup execute 'set backupdir^='.option#Escape(option#item#Escape( - \ $MYVIM.'/backup'.(has#('patch-8.1.251') ? '//' : ''), + \ s:cache.'/backup'.(has#('patch-8.1.251') ? '//' : ''), \)) -call path#Create($MYVIM.'/backup') +call path#Create(s:cache.'/backup') " Files in certain directories on Unix-compatible filesystems should not be " backed up, for security reasons. This is particularly important if editing @@ -218,9 +232,10 @@ endif " option has supported that hint for much longer than 'backupdir' has. We " apply path#Create() to attempt to create the path, if needed. " +let s:swap = s:cache.'/swap' execute 'set directory^=' - \.option#Escape(option#item#Escape($MYVIM.'/swap//')) -call path#Create($MYVIM.'/swap') + \.option#Escape(option#item#Escape(s:swap.'//')) +call path#Create(s:swap) " Keep tracked undo history for files permanently, in a dedicated cache " directory, so that the u/:undo and CTRL-R/:redo commands will work between @@ -237,9 +252,10 @@ call path#Create($MYVIM.'/swap') " if has#('persistent_undo') set undofile + let s:undodir = s:cache.'/undo' execute 'set undodir^=' - \.option#Escape(option#item#Escape($MYVIM.'/undo//')) - call path#Create($MYVIM.'/undo') + \.option#Escape(option#item#Escape(s:undodir.'//')) + call path#Create(s:undodir) endif " Set up a directory for files generated by :mkview. To date, I think I have @@ -248,9 +264,10 @@ endif " ('backupdir', 'directory', 'spell', 'undodir') " if has#('mksession') + let s:viewdir = s:cache.'/view' execute 'set viewdir=' - \.option#Escape(option#item#Escape($MYVIM.'/view')) - call path#Create($MYVIM.'/view') + \.option#Escape(option#item#Escape(s:viewdir)) + call path#Create(s:viewdir) endif " Now that we have a bit more confidence in our runtime environment, set up @@ -365,12 +382,11 @@ set spellcapcheck=[.?!]\\%(\ \ \\\|[\\n\\r\\t]\\) " around this one with 'isfname'; the blacklist is hard-coded. " set dictionary^=/usr/share/dict/words -let s:ref = $MYVIM.'/ref' try execute 'set dictionary^=' - \.option#Escape(option#item#Escape(s:ref.'/dictionary.txt')) + \.option#Escape(option#item#Escape(s:data.'/dictionary.txt')) execute 'set thesaurus^=' - \.option#Escape(option#item#Escape(s:ref.'/thesaurus.txt')) + \.option#Escape(option#item#Escape(s:data.'/thesaurus.txt')) catch /^Vim\%((\a\+)\)\=:E474:/ endtry -- cgit v1.2.3