aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/vimrc.vim
blob: 94922e9358f7654f9dc84b2b6da4e92b31c4afc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
" Escape a text value for :execute-based :set inclusion in an option
function! vimrc#EscapeSet(string) abort
  return escape(a:string, '\ |"')
endfunction

" Escape a text value for inclusion as an element in a comma-separated list
" option.  Yes, the comma being the sole inner escaped character here is
" correct.  No, we shouldn't escape backslash itself.  Yes, that means it's
" impossible to have the literal string '\,' in a part.
function! vimrc#EscapeSetPart(string) abort
  return vimrc#EscapeSet(escape(a:string, ','))
endfunction

" Check that we have a plugin available, and will be loading it
function! vimrc#PluginReady(filename) abort
  return globpath(&runtimepath, 'plugin/'.a:filename.'.vim') !=# ''
        \ && &loadplugins
endfunction

" Split a comma-separated option string into its constituent parts, imitating
" copy_option_part() in the Vim sources.  This isn't perfect, but it should be
" more than good enough.  A separator can be defined as: a comma that is not
" preceded by a backslash, and which is followed by any number of spaces
" and/or further commas.
function! vimrc#SplitOption(string) abort
  return split(a:string, '\\\@<!,[, ]*')
endfunction

" Convenience version function check that should work with 7.0 or newer;
" takes strings like 7.3.251
function! vimrc#Version(string) abort

  " Test the version string and get submatches for each part
  let match = matchlist(a:string, '^\(\d\+\)\.\(\d\+\)\.\(\d\+\)$')

  " Throw toys if the string didn't match the expected format
  if !len(match)
    echoerr 'Invalid version string: '.a:string
    return
  endif

  " Get the major, minor, and patch numbers from the submatches
  let [major, minor, patch] = match[1:3]

  " 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
  return v:version > ver
        \ || v:version == ver && has('patch'.patch)

endfunction