Skip to content

Instantly share code, notes, and snippets.

@Houl
Created August 11, 2019 16:45
Show Gist options
  • Save Houl/151594d0089fee91e33ba6d676f5092c to your computer and use it in GitHub Desktop.
Save Houl/151594d0089fee91e33ba6d676f5092c to your computer and use it in GitHub Desktop.
HasPatchExpr()
" File: D2190.vim
" Created: 2017 Jun 30
" Last Change: 2018 May 15
" Version: 0.5
" Author: Andy Wokula <[email protected]>
" License: Vim License, see :h license
" Dependencies: (by :PutDepend)
" CallOverloaded()
" HasPatchExpr({vimver}, {patchnr})
" HasPatchExpr({major}, {minor}, {patchnr})
" HasPatchExpr({patchlevel})
"
" return an expression (string) that can be used (literally in a script)
" to check the running Vim against a certain patch level.
"
" Depending on the represented patch value, this for example returns
" has("patch-7.4.237")
" or
" v:version > 704 || v:version == 704 && has("patch236")
" (the patch numbers are different by intention).
"
" The first variant is easier to read, but only understood since
" Vim 7.4.237 (older Vims always return 0). The second variant works
" everywhere and will be used for versions up to Vim 7.4.236.
"
" Arguments can be given in several ways:
" {vimver} (number) v:version, eg 704 for Vim 7.4
" {patchnr} (number) 0 or greater
" or
" {major} (number) major version, eg 7
" {minor} (number) minor version, eg 4
" {patchnr} (number) 0 or greater
" or
" {patchlevel} (string) '7.4.237' or 'v7-4-237' or 'patch-7.4.237'
"
" A general check can use the newer notation for a newer patch, but must
" use the older notation for an older patch.
"
" History:
" 2019 Jun 14 Note: v:versionlong (added with 8.1.1526) cannot be used for
" version checking
" 2018 Jul 28 BF: HasPatchExpr('701', '040') => '... has("patch36")'
" 2018 May 15 not sure why Empty() was used
" 2017 Nov 05 BF: HasPatchExpr('7.1.040') => '... has("patch36")'
" 2017 Nov 03 use CallOverloaded()
" 2017 Jul 17 BF: checked argument against version of running Vim (stupid)
func! HasPatchExpr(arg, ...) "{{{
return CallOverloaded(s:f, [a:arg] + a:000, {})
endfunc "}}}
let s:f = {}
func! s:f.XX(vimver, patchnr) "{{{
return s:HasPatchExpr(str2nr(a:vimver), str2nr(a:patchnr))
endfunc "}}}
func! s:f.XXX(major, minor, patchnr) "{{{
return s:HasPatchExpr(str2nr(a:major) * 100 + str2nr(a:minor), str2nr(a:patchnr))
endfunc "}}}
func! s:f.S(str) "{{{
" from HasPatch()
let ml = matchlist(a:str, '\(\d\+\)\D\(\d\+\)\D\(\d\+\)')
if !empty(ml)
let [major, minor, patchnr] = ml[1:3]
return s:HasPatchExpr(str2nr(major) * 100 + str2nr(minor), str2nr(patchnr))
else
echoerr printf('HasPatchExpr(): invalid argument "%s"', a:str)
return ''
endif
endfunc "}}}
func! s:f._(...) "{{{
echoerr 'HasPatchExpr(): unrecognized arguments'
return ''
endfunc "}}}
func! s:HasPatchExpr(vimver, patchnr) "{{{
if a:patchnr >= 1
if a:vimver > 704 || a:vimver == 704 && a:patchnr >= 237
return printf('has("patch-%d.%d.%d")', a:vimver/100, a:vimver%100, a:patchnr)
else
return printf('v:version > %d || v:version == %d && has("patch%d")', a:vimver, a:vimver, a:patchnr)
endif
else
return printf('v:version >= %d', a:vimver)
endif
endfunc "}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment