From 8ca01a93dc1e9139f7dc74036b3b40586b06ae15 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 3 Jun 2019 22:24:37 +1200 Subject: Factor out Vim version checks into autoload func --- vim/autoload/vimrc.vim | 27 +++++++++++++++++++++++++++ vim/vimrc | 11 ++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/vim/autoload/vimrc.vim b/vim/autoload/vimrc.vim index 71b29d61..bfbae263 100644 --- a/vim/autoload/vimrc.vim +++ b/vim/autoload/vimrc.vim @@ -45,3 +45,30 @@ function! vimrc#SplitEscaped(str, ...) abort return list endfunction + +" Convenience version function check that should work with 7.0 or newer; +" takes strings like 7.3.251 +function! vimrc#Version(verstr) abort + + " Throw toys if the string doesn't match the expected format + if a:verstr !~# '^\d\+\.\d\+.\d\+$' + echoerr 'Invalid version string: '.a:verstr + endif + + " Split version string into major, minor, and patch level integers + let [major, minor, patch] = split(a:verstr, '\.') + + " Create a string like 801 from a version number 8.1 to compare it to + " the v:version integer + let ver = major * 100 + minor + + " Compare versions + if v:version > ver + return 1 " Current Vim is newer than the wanted one + elseif ver < v:version + return 0 " Current Vim is older than the wanted one + else + return has('patch'.patch) " Versions equal, return patch presence + endif + +endfunction diff --git a/vim/vimrc b/vim/vimrc index ad582717..302f1388 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -14,9 +14,7 @@ set shiftwidth=4 " Indent with four spaces " Make insert mode tab key add the same number of spaces as 'shiftwidth', use " negative value to do this if Vim new enough to support it -let &softtabstop = v:version > 703 - \ || v:version == 703 && has('patch693') - \ ? -1 : &shiftwidth +let &softtabstop = vimrc#Version('7.3.693') ? -1 : &shiftwidth " Allow me to backspace 'autoindent' spaces in insert mode, and back over the " end of lines I've inserted in this session @@ -26,7 +24,7 @@ set backspace=indent,eol " full path in name, if Vim is new enough to support that set backup execute 'set backupdir^='.escape($MYVIM, '\ ').'/cache/backup' - \ . (has('patch-8.1.251') ? '//' : '') + \ . (vimrc#Version('8.1.251') ? '//' : '') " Add some *nix paths not to back up if has('unix') @@ -74,13 +72,12 @@ set foldlevelstart=99 set foldmethod=indent " Delete comment leaders when joining lines, if supported -if v:version > 703 - \ || v:version == 703 && has('patch541') +if vimrc#Version('7.3.541') set formatoptions+=j endif " Don't break a single space after a period, if supported -if has('patch-8.1.728') +if vimrc#Version('8.1.728') set formatoptions+=p endif -- cgit v1.2.3