From 194c30603b7ea0a9ac68126afe4434b71fc75580 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 Apr 2020 21:26:23 +1200 Subject: Correct absolute test --- vim/autoload/xdg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vim/autoload/xdg.vim b/vim/autoload/xdg.vim index 9e6d6553..b1bc523a 100644 --- a/vim/autoload/xdg.vim +++ b/vim/autoload/xdg.vim @@ -20,7 +20,7 @@ function! s:Get(name) abort endfunction function! s:Absolute(path) abort - return a:path !=# '^[/~]' + return a:path =~# '^[/~]' endfunction function! xdg#CacheDir(name) abort -- cgit v1.2.3 From 6dcd250f4fa39e3ac414338adbf6f95fc66fc04f Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 Apr 2020 21:27:48 +1200 Subject: Handle invalid XDG cache home --- vim/autoload/xdg.vim | 2 +- vim/vimrc | 46 +++++++++++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/vim/autoload/xdg.vim b/vim/autoload/xdg.vim index b1bc523a..675cf460 100644 --- a/vim/autoload/xdg.vim +++ b/vim/autoload/xdg.vim @@ -27,7 +27,7 @@ function! xdg#CacheDir(name) abort let name = a:name let home = s:Get('XDG_CACHE_HOME') if !s:Absolute(home) - return + return '' endif return join([home, name], '/') endfunction diff --git a/vim/vimrc b/vim/vimrc index 27a79a6f..8afb52d0 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -110,8 +110,12 @@ for s:configdir in reverse(xdg#ConfigDirs('vim')) 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)) +if strlen(s:cachedir) + execute 'set runtimepath^=' + \.option#Escape(option#item#Escape(s:cachedir)) +else + unlet s:cachedir +endif "" Data let s:datadir = xdg#DataDirs('vim')[0] @@ -144,8 +148,10 @@ command! -bang -bar -complete=dir -nargs=1 CreatePath " " " -execute 'set viminfo+='.option#Escape('n'.s:cachedir.'/viminfo') -call path#Create(s:cachedir) +if exists('s:cachedir') + execute 'set viminfo+='.option#Escape('n'.s:cachedir.'/viminfo') + call path#Create(s:cachedir) +endif " 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 @@ -190,10 +196,12 @@ set history=10000 " 'backupfullname', 'swapfilefullname' would have been clearer. " set backup -execute 'set backupdir^='.option#Escape(option#item#Escape( - \ s:cachedir.'/backup'.(has#('patch-8.1.251') ? '//' : ''), - \)) -call path#Create(s:cachedir.'/backup') +if exists('s:cachedir') + execute 'set backupdir^='.option#Escape(option#item#Escape( + \ s:cachedir.'/backup'.(has#('patch-8.1.251') ? '//' : ''), + \)) + call path#Create(s:cachedir.'/backup') +endif " Files in certain directories on Unix-compatible filesystems should not be " backed up, for security reasons. This is particularly important if editing @@ -229,10 +237,12 @@ 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:cachedir.'/swap' -execute 'set directory^=' - \.option#Escape(option#item#Escape(s:swap.'//')) -call path#Create(s:swap) +if exists('s:cachedir') + let s:swap = s:cachedir.'/swap' + execute 'set directory^=' + \.option#Escape(option#item#Escape(s:swap.'//')) + call path#Create(s:swap) +endif " 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 @@ -249,10 +259,12 @@ call path#Create(s:swap) " if has#('persistent_undo') set undofile - let s:undodir = s:cachedir.'/undo' - execute 'set undodir^=' - \.option#Escape(option#item#Escape(s:undodir.'//')) - call path#Create(s:undodir) + if exists('s:cachedir') + let s:undodir = s:cachedir.'/undo' + execute 'set undodir^=' + \.option#Escape(option#item#Escape(s:undodir.'//')) + call path#Create(s:undodir) + endif endif " Set up a directory for files generated by :mkview. To date, I think I have @@ -260,7 +272,7 @@ endif " directories of this type. This isn't a comma-separated list like the others " ('backupdir', 'directory', 'spell', 'undodir') " -if has#('mksession') +if has#('mksession') && exists('s:cachedir') let s:viewdir = s:cachedir.'/view' execute 'set viewdir=' \.option#Escape(option#item#Escape(s:viewdir)) -- cgit v1.2.3 From f0ef30a4c5eed9f0b15bdcd9bdd1093aba72348e Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 Apr 2020 22:34:39 +1200 Subject: Improve compliance/sanity of XDG implementation --- vim/autoload/xdg.vim | 45 +++++++++++++---------- vim/vimrc | 100 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 89 insertions(+), 56 deletions(-) diff --git a/vim/autoload/xdg.vim b/vim/autoload/xdg.vim index 675cf460..62d515c1 100644 --- a/vim/autoload/xdg.vim +++ b/vim/autoload/xdg.vim @@ -7,6 +7,8 @@ let s:defaults = { \ 'XDG_DATA_DIRS': '/usr/local/share/:/usr/share/', \} +let s:subdir = 'vim' + function! s:Get(name) abort let name = a:name let env = environ() @@ -23,31 +25,38 @@ function! s:Absolute(path) abort return a:path =~# '^[/~]' endfunction -function! xdg#CacheDir(name) abort - let name = a:name - let home = s:Get('XDG_CACHE_HOME') +function! s:Home(name) abort + let home = s:Get(a:name) if !s:Absolute(home) return '' endif - return join([home, name], '/') + return join([home, s:subdir], '/') endfunction -function! xdg#ConfigDirs(name) abort - let name = a:name - let home = s:Get('XDG_CONFIG_HOME') - let dirs = split(s:Get('XDG_CONFIG_DIRS'), ':') - return map( - \ filter(insert(dirs, home), 's:Absolute(v:val)') - \,'join([v:val, name], "/")' - \) +function! xdg#CacheHome() abort + return s:Home('XDG_CACHE_HOME') endfunction -function! xdg#DataDirs(name) abort - let name = a:name - let home = s:Get('XDG_DATA_HOME') - let dirs = split(s:Get('XDG_DATA_DIRS'), ':') +function! xdg#ConfigHome() abort + return s:Home('XDG_CONFIG_HOME') +endfunction + +function! xdg#DataHome() abort + return s:Home('XDG_DATA_HOME') +endfunction + +function! s:Dirs(name) abort + let dirs = split(s:Get(a:name), ':') return map( - \ filter(insert(dirs, home), 's:Absolute(v:val)') - \,'join([v:val, name], "/")' + \ filter(dirs, 's:Absolute(v:val)') + \,'join([v:val, s:subdir], "/")' \) endfunction + +function! xdg#ConfigDirs() abort + return s:Dirs('XDG_CONFIG_DIRS') +endfunction + +function! xdg#DataDirs() abort + return s:Dirs('XDG_DATA_DIRS') +endfunction diff --git a/vim/vimrc b/vim/vimrc index 8afb52d0..b4347b4f 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -103,21 +103,39 @@ endif " We'll use the XDG directories as machine-local configuration and storage. " " +"" Cache +let s:cache_home = xdg#CacheHome() +if strlen(s:cache_home) == 0 + unlet s:cache_home +endif "" 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') -if strlen(s:cachedir) - execute 'set runtimepath^=' - \.option#Escape(option#item#Escape(s:cachedir)) -else - unlet s:cachedir +let s:config_home = xdg#ConfigHome() +if strlen(s:config_home) == 0 + unlet s:config_home endif +let s:config_dirs = xdg#ConfigDirs() "" Data -let s:datadir = xdg#DataDirs('vim')[0] +let s:data_home = xdg#DataHome() +if strlen(s:data_home) == 0 + unlet s:data_home +endif +let s:data_dirs = xdg#DataDirs() + +" Add all the configuration directories to 'runtimepath', and then put the +" cache home at the very front, so that e.g. 'spellfile' gets created in there +" rather than in the configuration directories. +" +let s:runtime_dirs = s:config_dirs +if exists('s:config_home') + call insert(s:runtime_dirs, s:config_home) +endif +if exists('s:cache_home') + call insert(s:runtime_dirs, s:config_home) +endif +for s:runtime_dir in reverse(s:runtime_dirs) + execute 'set runtimepath^=' + \.option#Escape(option#item#Escape(s:runtime_dir)) +endfor " 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 @@ -148,9 +166,9 @@ command! -bang -bar -complete=dir -nargs=1 CreatePath " " " -if exists('s:cachedir') - execute 'set viminfo+='.option#Escape('n'.s:cachedir.'/viminfo') - call path#Create(s:cachedir) +if exists('s:cache_home') + execute 'set viminfo+='.option#Escape('n'.s:cache_home.'/viminfo') + call path#Create(s:cache_home) endif " Speaking of recorded data in viminfo files, the default Vim limit of a mere @@ -196,11 +214,11 @@ set history=10000 " 'backupfullname', 'swapfilefullname' would have been clearer. " set backup -if exists('s:cachedir') +if exists('s:cache_home') execute 'set backupdir^='.option#Escape(option#item#Escape( - \ s:cachedir.'/backup'.(has#('patch-8.1.251') ? '//' : ''), + \ s:cache_home.'/backup'.(has#('patch-8.1.251') ? '//' : ''), \)) - call path#Create(s:cachedir.'/backup') + call path#Create(s:cache_home.'/backup') endif " Files in certain directories on Unix-compatible filesystems should not be @@ -237,11 +255,11 @@ endif " option has supported that hint for much longer than 'backupdir' has. We " apply path#Create() to attempt to create the path, if needed. " -if exists('s:cachedir') - let s:swap = s:cachedir.'/swap' +if exists('s:cache_home') + let s:directory = s:cache_home.'/swap' execute 'set directory^=' - \.option#Escape(option#item#Escape(s:swap.'//')) - call path#Create(s:swap) + \.option#Escape(option#item#Escape(s:directory.'//')) + call path#Create(s:directory) endif " Keep tracked undo history for files permanently, in a dedicated cache @@ -259,8 +277,8 @@ endif " if has#('persistent_undo') set undofile - if exists('s:cachedir') - let s:undodir = s:cachedir.'/undo' + if exists('s:cache_home') + let s:undodir = s:cache_home.'/undo' execute 'set undodir^=' \.option#Escape(option#item#Escape(s:undodir.'//')) call path#Create(s:undodir) @@ -272,8 +290,8 @@ endif " directories of this type. This isn't a comma-separated list like the others " ('backupdir', 'directory', 'spell', 'undodir') " -if has#('mksession') && exists('s:cachedir') - let s:viewdir = s:cachedir.'/view' +if has#('mksession') && exists('s:cache_home') + let s:viewdir = s:cache_home.'/view' execute 'set viewdir=' \.option#Escape(option#item#Escape(s:viewdir)) call path#Create(s:viewdir) @@ -381,21 +399,27 @@ set spellcapcheck=[.?!]\\%(\ \ \\\|[\\n\\r\\t]\\) " first two metadata lines from thesaurus.txt, as Vim appeared to interpret " them as part of the body data. " -" Extra checks for appending the 'dictionary' and 'thesaurus' paths in MYVIM -" need to be made, because the P_NDNAME property is assigned to them, which -" enforces a character blacklist in the option value. We check for the -" expected Vim error code here, and if the MYVIM path offends, we just skip -" the setting entirely, rather than throwing cryptic errors at the user. None -" of the blacklisted characters are particularly wise characters to have in -" paths, anyway, legal though they may be on Unix filesystems. We can’t work -" around this one with 'isfname'; the blacklist is hard-coded. +" Extra checks for appending the 'dictionary' and 'thesaurus' paths need to be +" made, because the P_NDNAME property is assigned to them, which enforces +" a character blacklist in the option value. We check for the expected Vim +" error code here, and if the path offends, we just skip the setting entirely, +" rather than throwing cryptic errors at the user. None of the blacklisted +" characters are particularly wise characters to have in paths, anyway, legal +" though they may be on Unix filesystems. We can’t work around this one with +" 'isfname'; the blacklist is hard-coded. " set dictionary^=/usr/share/dict/words try - execute 'set dictionary^=' - \.option#Escape(option#item#Escape(s:datadir.'/dictionary.txt')) - execute 'set thesaurus^=' - \.option#Escape(option#item#Escape(s:datadir.'/thesaurus.txt')) + let s:ref_dirs = s:data_dirs + if exists('s:data_home') + call insert(s:ref_dirs, s:data_home) + endif + for s:ref_dir in reverse(s:ref_dirs) + execute 'set dictionary^=' + \.option#Escape(option#item#Escape(s:ref_dir.'/dictionary.txt')) + execute 'set thesaurus^=' + \.option#Escape(option#item#Escape(s:ref_dir.'/thesaurus.txt')) + endfor catch /^Vim\%((\a\+)\)\=:E474:/ endtry -- cgit v1.2.3 From 5f3ebed25f6d997c8f7161b97476054c865c0807 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 Apr 2020 23:02:40 +1200 Subject: Refactor adjusted XDG handling --- vim/autoload/xdg.vim | 32 ++++++++++-------------- vim/vimrc | 69 ++++++++++++++++++++-------------------------------- 2 files changed, 39 insertions(+), 62 deletions(-) diff --git a/vim/autoload/xdg.vim b/vim/autoload/xdg.vim index 62d515c1..14aa3055 100644 --- a/vim/autoload/xdg.vim +++ b/vim/autoload/xdg.vim @@ -33,18 +33,6 @@ function! s:Home(name) abort return join([home, s:subdir], '/') endfunction -function! xdg#CacheHome() abort - return s:Home('XDG_CACHE_HOME') -endfunction - -function! xdg#ConfigHome() abort - return s:Home('XDG_CONFIG_HOME') -endfunction - -function! xdg#DataHome() abort - return s:Home('XDG_DATA_HOME') -endfunction - function! s:Dirs(name) abort let dirs = split(s:Get(a:name), ':') return map( @@ -53,10 +41,16 @@ function! s:Dirs(name) abort \) endfunction -function! xdg#ConfigDirs() abort - return s:Dirs('XDG_CONFIG_DIRS') -endfunction - -function! xdg#DataDirs() abort - return s:Dirs('XDG_DATA_DIRS') -endfunction +let xdg# = { + \ 'cache': { + \ 'home': s:Home('XDG_CACHE_HOME'), + \ }, + \ 'config': { + \ 'home': s:Home('XDG_CONFIG_HOME'), + \ 'dirs': s:Dirs('XDG_CONFIG_DIRS'), + \ }, + \ 'data': { + \ 'home': s:Home('XDG_DATA_HOME'), + \ 'dirs': s:Dirs('XDG_DATA_DIRS'), + \ }, + \} diff --git a/vim/vimrc b/vim/vimrc index b4347b4f..abbdda4b 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -103,39 +103,22 @@ endif " We'll use the XDG directories as machine-local configuration and storage. " " -"" Cache -let s:cache_home = xdg#CacheHome() -if strlen(s:cache_home) == 0 - unlet s:cache_home -endif -"" Config -let s:config_home = xdg#ConfigHome() -if strlen(s:config_home) == 0 - unlet s:config_home -endif -let s:config_dirs = xdg#ConfigDirs() -"" Data -let s:data_home = xdg#DataHome() -if strlen(s:data_home) == 0 - unlet s:data_home -endif -let s:data_dirs = xdg#DataDirs() - " Add all the configuration directories to 'runtimepath', and then put the " cache home at the very front, so that e.g. 'spellfile' gets created in there " rather than in the configuration directories. " -let s:runtime_dirs = s:config_dirs -if exists('s:config_home') - call insert(s:runtime_dirs, s:config_home) +let s:xdgruntimepaths = xdg#['config']['dirs'] +if xdg#['config']['home'] !=# '' + call insert(s:xdgruntimepaths, xdg#['config']['home']) endif -if exists('s:cache_home') - call insert(s:runtime_dirs, s:config_home) +if xdg#['cache']['home'] !=# '' + call insert(s:xdgruntimepaths, xdg#['config']['home']) endif -for s:runtime_dir in reverse(s:runtime_dirs) +for s:xdgruntimepath in reverse(s:xdgruntimepaths) execute 'set runtimepath^=' - \.option#Escape(option#item#Escape(s:runtime_dir)) + \.option#Escape(option#item#Escape(s:xdgruntimepath)) endfor +unlet s:xdgruntimepaths s:xdgruntimepath " 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 @@ -166,9 +149,9 @@ command! -bang -bar -complete=dir -nargs=1 CreatePath " " " -if exists('s:cache_home') - execute 'set viminfo+='.option#Escape('n'.s:cache_home.'/viminfo') - call path#Create(s:cache_home) +if xdg#['cache']['home'] !=# '' + execute 'set viminfo+='.option#Escape('n'.xdg#['cache']['home'].'/viminfo') + call path#Create(xdg#['cache']['home']) endif " Speaking of recorded data in viminfo files, the default Vim limit of a mere @@ -214,11 +197,11 @@ set history=10000 " 'backupfullname', 'swapfilefullname' would have been clearer. " set backup -if exists('s:cache_home') +if xdg#['cache']['home'] !=# '' execute 'set backupdir^='.option#Escape(option#item#Escape( - \ s:cache_home.'/backup'.(has#('patch-8.1.251') ? '//' : ''), + \ xdg#['cache']['home'].'/backup'.(has#('patch-8.1.251') ? '//' : ''), \)) - call path#Create(s:cache_home.'/backup') + call path#Create(xdg#['cache']['home'].'/backup') endif " Files in certain directories on Unix-compatible filesystems should not be @@ -255,8 +238,8 @@ endif " option has supported that hint for much longer than 'backupdir' has. We " apply path#Create() to attempt to create the path, if needed. " -if exists('s:cache_home') - let s:directory = s:cache_home.'/swap' +if xdg#['cache']['home'] !=# '' + let s:directory = xdg#['cache']['home'].'/swap' execute 'set directory^=' \.option#Escape(option#item#Escape(s:directory.'//')) call path#Create(s:directory) @@ -277,8 +260,8 @@ endif " if has#('persistent_undo') set undofile - if exists('s:cache_home') - let s:undodir = s:cache_home.'/undo' + if xdg#['cache']['home'] !=# '' + let s:undodir = xdg#['cache']['home'].'/undo' execute 'set undodir^=' \.option#Escape(option#item#Escape(s:undodir.'//')) call path#Create(s:undodir) @@ -290,8 +273,8 @@ endif " directories of this type. This isn't a comma-separated list like the others " ('backupdir', 'directory', 'spell', 'undodir') " -if has#('mksession') && exists('s:cache_home') - let s:viewdir = s:cache_home.'/view' +if has#('mksession') && xdg#['cache']['home'] + let s:viewdir = xdg#['cache']['home'].'/view' execute 'set viewdir=' \.option#Escape(option#item#Escape(s:viewdir)) call path#Create(s:viewdir) @@ -410,15 +393,15 @@ set spellcapcheck=[.?!]\\%(\ \ \\\|[\\n\\r\\t]\\) " set dictionary^=/usr/share/dict/words try - let s:ref_dirs = s:data_dirs - if exists('s:data_home') - call insert(s:ref_dirs, s:data_home) + let s:refdirs = xdg#['data']['dirs'] + if xdg#['data']['home'] !=# '' + call insert(s:refdirs, xdg#['data']['home']) endif - for s:ref_dir in reverse(s:ref_dirs) + for s:refdir in reverse(s:refdirs) execute 'set dictionary^=' - \.option#Escape(option#item#Escape(s:ref_dir.'/dictionary.txt')) + \.option#Escape(option#item#Escape(s:refdir.'/dictionary.txt')) execute 'set thesaurus^=' - \.option#Escape(option#item#Escape(s:ref_dir.'/thesaurus.txt')) + \.option#Escape(option#item#Escape(s:refdir.'/thesaurus.txt')) endfor catch /^Vim\%((\a\+)\)\=:E474:/ endtry -- cgit v1.2.3 From cb6f10f8cee9c65af2ce4f3e01b823712fe2c8a0 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 Apr 2020 23:03:06 +1200 Subject: Bump VERSION --- VERSION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index e57e820b..f68eb207 100644 --- a/VERSION +++ b/VERSION @@ -1,2 +1,2 @@ -tejr dotfiles v8.20.1 -Sat, 25 Apr 2020 08:46:20 +0000 +tejr dotfiles v8.20.2 +Sat, 25 Apr 2020 11:03:00 +0000 -- cgit v1.2.3