aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/vimrc.vim
blob: de73720b1e84c463fb049a42668f1cb47355194b (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
" Escape a text value for inclusion in a comma-separated option value
function! vimrc#EscapeSet(string) abort
  return 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 string with a split character that can be escaped with another,
" e.g. &runtimepath with commas and backslashes respectively
function! vimrc#SplitEscaped(str, ...) abort

  " Arguments to function
  let str = a:str                 " String to split
  let sep = a:0 >= 1 ? a:1 : ','  " Optional split char, default comma
  let esc = a:0 >= 2 ? a:2 : '\'  " Optional escape char, default backslash

  " Get length of string, return empty list if it's zero
  let len = strlen(str)
  if !len
    return []
  endif

  " Collect items into list by iterating characterwise
  let list = ['']  " List items
  let idx = 0      " Offset in string
  while idx < len

    if str[idx] ==# sep

      " This character is the item separator, and it wasn't escaped; start a
      " new list item
      call add(list, '')

    else

      " This character is the escape character, so we'll skip to the next
      " character, if any, and add that; testing suggests that a terminal
      " escape character on its own shouldn't be added
      if str[idx] ==# esc
        let idx += 1
      endif
      let list[-1] .= str[idx]

    endif

    " Bump index for next character
    let idx += 1

  endwhile

  " Return the completed list
  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