My dotfiles. Period.

option.vim 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "=============================================================================
  2. " $Id: option.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  3. " File: autoload/lh/option.vim {{{1
  4. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5. " <URL:http://code.google.com/p/lh-vim/>
  6. " License: GPLv3 with exceptions
  7. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  8. " Version: 3.0.0
  9. " Created: 24th Jul 2004
  10. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (07th Oct 2006)
  11. "------------------------------------------------------------------------
  12. " Description:
  13. " Defines the global function lh#option#get().
  14. " Aimed at (ft)plugin writers.
  15. "
  16. "------------------------------------------------------------------------
  17. " Installation:
  18. " Drop this file into {rtp}/autoload/lh/
  19. " Requires Vim 7+
  20. " History:
  21. " v3.0.0
  22. " (*) GPLv3
  23. " v2.0.6
  24. " (*) lh#option#add() add values to a vim list |option|
  25. " v2.0.5
  26. " (*) lh#option#get_non_empty() manages Lists and Dictionaries
  27. " (*) lh#option#get() doesn't test emptyness anymore
  28. " v2.0.0
  29. " Code moved from {rtp}/macros/
  30. " }}}1
  31. "=============================================================================
  32. "=============================================================================
  33. let s:cpo_save=&cpo
  34. set cpo&vim
  35. "------------------------------------------------------------------------
  36. " ## Functions {{{1
  37. " # Debug {{{2
  38. function! lh#option#verbose(level)
  39. let s:verbose = a:level
  40. endfunction
  41. function! s:Verbose(expr)
  42. if exists('s:verbose') && s:verbose
  43. echomsg a:expr
  44. endif
  45. endfunction
  46. function! lh#option#debug(expr)
  47. return eval(a:expr)
  48. endfunction
  49. " # Public {{{2
  50. " Function: lh#option#get(name, default [, scope]) {{{3
  51. " @return b:{name} if it exists, or g:{name} if it exists, or {default}
  52. " otherwise
  53. " The order of the variables checked can be specified through the optional
  54. " argument {scope}
  55. function! lh#option#get(name,default,...)
  56. let scope = (a:0 == 1) ? a:1 : 'bg'
  57. let name = a:name
  58. let i = 0
  59. while i != strlen(scope)
  60. if exists(scope[i].':'.name)
  61. " \ && (0 != strlen({scope[i]}:{name}))
  62. return {scope[i]}:{name}
  63. endif
  64. let i += 1
  65. endwhile
  66. return a:default
  67. endfunction
  68. function! lh#option#Get(name,default,...)
  69. let scope = (a:0 == 1) ? a:1 : 'bg'
  70. return lh#option#get(a:name, a:default, scope)
  71. endfunction
  72. function! s:IsEmpty(variable)
  73. if type(a:variable) == type('string') | return 0 == strlen(a:variable)
  74. elseif type(a:variable) == type(42) | return 0 == a:variable
  75. elseif type(a:variable) == type([]) | return 0 == len(a:variable)
  76. elseif type(a:variable) == type({}) | return 0 == len(a:variable)
  77. else | return false
  78. endif
  79. endfunction
  80. " Function: lh#option#get_non_empty(name, default [, scope]) {{{3
  81. " @return of b:{name}, g:{name}, or {default} the first which exists and is not empty
  82. " The order of the variables checked can be specified through the optional
  83. " argument {scope}
  84. function! lh#option#get_non_empty(name,default,...)
  85. let scope = (a:0 == 1) ? a:1 : 'bg'
  86. let name = a:name
  87. let i = 0
  88. while i != strlen(scope)
  89. if exists(scope[i].':'.name) && !s:IsEmpty({scope[i]}:{name})
  90. return {scope[i]}:{name}
  91. endif
  92. let i += 1
  93. endwhile
  94. return a:default
  95. endfunction
  96. function! lh#option#GetNonEmpty(name,default,...)
  97. let scope = (a:0 == 1) ? a:1 : 'bg'
  98. return lh#option#get_non_empty(a:name, a:default, scope)
  99. endfunction
  100. " Function: lh#option#add(name, values) {{{3
  101. " Add fields to a vim option.
  102. " @param values list of values to add
  103. " @example let lh#option#add('l:tags', ['.tags'])
  104. function! lh#option#add(name,values)
  105. let values = type(a:values) == type([])
  106. \ ? copy(a:values)
  107. \ : [a:values]
  108. let old = split(eval('&'.a:name), ',')
  109. let new = filter(values, 'match(old, v:val) < 0')
  110. let val = join(old+new, ',')
  111. exe 'let &'.a:name.' = val'
  112. endfunction
  113. " Functions }}}1
  114. "------------------------------------------------------------------------
  115. let &cpo=s:cpo_save
  116. "=============================================================================
  117. " vim600: set fdm=marker: