From c9bc019da06f443b69cac5507594bd8b0d8e2a99 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 Apr 2020 19:25:36 +1200 Subject: Include XDG_{CONFIG,DATA}_DIRS handling in Vim This obsoletes custom $MYVIM variables. --- vim/autoload/xdg.vim | 35 ++++++++++++++++++++++++++++++++ vim/vimrc | 57 +++++++++++++++++++++++++--------------------------- 2 files changed, 62 insertions(+), 30 deletions(-) create mode 100644 vim/autoload/xdg.vim diff --git a/vim/autoload/xdg.vim b/vim/autoload/xdg.vim new file mode 100644 index 00000000..ab756f73 --- /dev/null +++ b/vim/autoload/xdg.vim @@ -0,0 +1,35 @@ +function! xdg#CacheDir(name) abort + let name = a:name + let home = exists('$XDG_CACHE_HOME') + \ ? $XDG_CACHE_HOME + \ : '~/.cache' + return join([home, name], '/') +endfunction + +function! xdg#ConfigDirs(name) abort + let name = a:name + let home = exists('$XDG_CONFIG_HOME') + \ ? $XDG_CONFIG_HOME + \ : '~/.config' + let dirs = exists('$XDG_CONFIG_DIRS') + \ ? split($XDG_CONFIG_DIRS, ':') + \ : [] + return map( + \ insert(dirs, home) + \,'join([v:val, name], "/")' + \) +endfunction + +function! xdg#DataDirs(name) abort + let name = a:name + let home = exists('$XDG_DATA_HOME') + \ ? $XDG_DATA_HOME + \ : '~/.local/share' + let dirs = exists('$XDG_DATA_DIRS') + \ ? split($XDG_DATA_DIRS, ':') + \ : [] + return map( + \ insert(dirs, home) + \,'join([v:val, name], "/")' + \) +endfunction diff --git a/vim/vimrc b/vim/vimrc index 17d1656e..27a79a6f 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -93,30 +93,27 @@ scriptencoding utf-8 " " We do all this with an autoloaded function option#Split(). " -" If an environment variable MYVIM exists, and it isn’t blank, apply its value -" as the first value of 'runtimepath', after escaping it appropriately. -" Otherwise, do it the other way around: the first path in the 'runtimepath' -" list becomes MYVIM. -" -if exists('$MYVIM') && $MYVIM !=# '' - execute 'set runtimepath^='.option#Escape(option#item#Escape($MYVIM, 1)) -elseif &runtimepath !=# '' +" We define an environment variable for ~/.vim or ~/vimfiles, by retrieving +" the first value from the 'runtimepath', correctly split. +" +if &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'll use the XDG directories as machine-local configuration and storage. +" +" +"" Config +for s:configdir in reverse(xdg#ConfigDirs('vim')) + execute 'set runtimepath^=' + \.option#Escape(option#item#Escape(s:configdir)) +endfor +"" Cache; put this first so that e.g. spellfiles get created in it +let s:cachedir = xdg#CacheDir('vim') +execute 'set runtimepath^=' + \.option#Escape(option#item#Escape(s:cachedir)) +"" Data +let s:datadir = xdg#DataDirs('vim')[0] " 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 @@ -147,8 +144,8 @@ command! -bang -bar -complete=dir -nargs=1 CreatePath " " " -execute 'set viminfo+='.option#Escape('n'.s:cache.'/viminfo') -call path#Create(s:cache) +execute 'set viminfo+='.option#Escape('n'.s:cachedir.'/viminfo') +call path#Create(s:cachedir) " 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 @@ -194,9 +191,9 @@ set history=10000 " set backup execute 'set backupdir^='.option#Escape(option#item#Escape( - \ s:cache.'/backup'.(has#('patch-8.1.251') ? '//' : ''), + \ s:cachedir.'/backup'.(has#('patch-8.1.251') ? '//' : ''), \)) -call path#Create(s:cache.'/backup') +call path#Create(s:cachedir.'/backup') " Files in certain directories on Unix-compatible filesystems should not be " backed up, for security reasons. This is particularly important if editing @@ -232,7 +229,7 @@ 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' +let s:swap = s:cachedir.'/swap' execute 'set directory^=' \.option#Escape(option#item#Escape(s:swap.'//')) call path#Create(s:swap) @@ -252,7 +249,7 @@ call path#Create(s:swap) " if has#('persistent_undo') set undofile - let s:undodir = s:cache.'/undo' + let s:undodir = s:cachedir.'/undo' execute 'set undodir^=' \.option#Escape(option#item#Escape(s:undodir.'//')) call path#Create(s:undodir) @@ -264,7 +261,7 @@ endif " ('backupdir', 'directory', 'spell', 'undodir') " if has#('mksession') - let s:viewdir = s:cache.'/view' + let s:viewdir = s:cachedir.'/view' execute 'set viewdir=' \.option#Escape(option#item#Escape(s:viewdir)) call path#Create(s:viewdir) @@ -384,9 +381,9 @@ set spellcapcheck=[.?!]\\%(\ \ \\\|[\\n\\r\\t]\\) set dictionary^=/usr/share/dict/words try execute 'set dictionary^=' - \.option#Escape(option#item#Escape(s:data.'/dictionary.txt')) + \.option#Escape(option#item#Escape(s:datadir.'/dictionary.txt')) execute 'set thesaurus^=' - \.option#Escape(option#item#Escape(s:data.'/thesaurus.txt')) + \.option#Escape(option#item#Escape(s:datadir.'/thesaurus.txt')) catch /^Vim\%((\a\+)\)\=:E474:/ endtry -- cgit v1.2.3