diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2019-06-09 15:52:35 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2019-06-09 16:17:33 +1200 |
commit | 00fd7bf7cefa38a0840e31f83761e46208b97600 (patch) | |
tree | fbedebe393e51cae419ab23aae25427f21750c16 | |
parent | Refactor validity checks for $MYVIM (diff) | |
download | dotfiles-00fd7bf7cefa38a0840e31f83761e46208b97600.tar.gz dotfiles-00fd7bf7cefa38a0840e31f83761e46208b97600.zip |
Move EnsureDir() declaration up, ensure $MYVIM
-rw-r--r-- | vim/vimrc | 59 |
1 files changed, 35 insertions, 24 deletions
@@ -86,6 +86,41 @@ elseif v:version < 702 && $MYVIM =~# '\\' let $MYVIM = '' endif +" We're going to be creating a few directories, and the code to do so in +" a compatible way is surprisingly verbose, because we need to check the +" mkdir() function is actually available, and also whether the directory +" concerned already exists, even if we specify the special 'p' value for its +" optional {path} argument. +" +" This is because the meaning of mkdir(..., 'p') is not the same as `mkdir -p` +" in shell script, or at least, it isn't in versions of Vim before v8.0.1708. +" Even with the magic 'p' sauce, these versions throw errors if the directory +" already exists, despite what someone familiar with `mkdir -p`'s behaviour in +" shell script might expect. +" +" So, let's wrap all that nonsense in a script-local function, and then +" abstract that away too with a user command, to keep the semantics of the +" :set operations nice and clean. We'll make all the directories we create +" have restrictive permissions, too, with a {prot} argument of 0700 for the +" final one, since every directory we want to create in this file should be +" locked down in this way. +" +function s:EnsureDir(path) abort + let path = expand(a:path) + return isdirectory(path) + \ || exists('*mkdir') && mkdir(path, 'p', 0700) +endfunction +command! -complete=dir -nargs=+ EnsureDir + \ call s:EnsureDir(<q-args>) + +" Now that we have a clean means to create directories if they don't already +" exist, let's apply it for the first time, in making sure that the MYVIM +" directory exists, if it's been set. +" +if $MYVIM !=# '' + EnsureDir $MYVIM +endif + " Create a 'vimrc' automatic command hook group, if it already exists, and " clear away any automatic command hooks already defined within it if it does, " so that we don't end up collecting multiple copies of the hooks configured @@ -134,30 +169,6 @@ if exists('##SourceCmd') \|endif endif -" We're going to be creating a few directories, and the code to do so in -" a compatible way is surprisingly verbose, because we need to check the -" mkdir() function is actually available, and also whether the directory -" concerned already exists, even if we specify the special 'p' value for its -" optional {path} argument. -" -" This is because the meaning of mkdir(..., 'p') is not the same as `mkdir -p` -" in shell script, or at least, it isn't in versions of Vim before v8.0.1708. -" Even with the magic 'p' sauce, these versions throw errors if the directory -" already exists, despite what someone familiar with `mkdir -p`'s behaviour in -" shell script might expect. -" -" So, let's wrap all that nonsense in a script-local function. We'll make all -" the directories we create have restrictive permissions, too, with a {prot} -" argument of 0700. -" -function s:EnsureDir(path) abort - let path = expand(a:path) - return isdirectory(path) - \ || exists('*mkdir') && mkdir(path, 'p', 0700) -endfunction -command! -complete=dir -nargs=+ EnsureDir - \ call s:EnsureDir(<q-args>) - " Keep the viminfo file in a cache subdirectory of $MYVIM, creating that " subdirectory if necessary. " |