You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

148 lines
4.7 KiB

  1. "=============================================================================
  2. " FILE: util.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
  4. " License: MIT license
  5. "=============================================================================
  6. function! neosnippet#util#get_vital() abort "{{{
  7. if !exists('s:V')
  8. let s:V = vital#neosnippet#new()
  9. endif
  10. return s:V
  11. endfunction"}}}
  12. function! s:get_prelude() abort "{{{
  13. if !exists('s:Prelude')
  14. let s:Prelude = neosnippet#util#get_vital().import('Prelude')
  15. endif
  16. return s:Prelude
  17. endfunction"}}}
  18. function! s:get_list() abort "{{{
  19. if !exists('s:List')
  20. let s:List = neosnippet#util#get_vital().import('Data.List')
  21. endif
  22. return s:List
  23. endfunction"}}}
  24. function! s:get_string() abort "{{{
  25. if !exists('s:String')
  26. let s:String = neosnippet#util#get_vital().import('Data.String')
  27. endif
  28. return s:String
  29. endfunction"}}}
  30. function! s:get_process() abort "{{{
  31. if !exists('s:Process')
  32. let s:Process = neosnippet#util#get_vital().import('Process')
  33. endif
  34. return s:Process
  35. endfunction"}}}
  36. function! neosnippet#util#substitute_path_separator(...) abort "{{{
  37. return call(s:get_prelude().substitute_path_separator, a:000)
  38. endfunction"}}}
  39. function! neosnippet#util#system(...) abort "{{{
  40. return call(s:get_process().system, a:000)
  41. endfunction"}}}
  42. function! neosnippet#util#has_vimproc(...) abort "{{{
  43. return call(s:get_process().has_vimproc, a:000)
  44. endfunction"}}}
  45. function! neosnippet#util#is_windows(...) abort "{{{
  46. return call(s:get_prelude().is_windows, a:000)
  47. endfunction"}}}
  48. function! neosnippet#util#is_mac(...) abort "{{{
  49. return call(s:get_prelude().is_mac, a:000)
  50. endfunction"}}}
  51. function! neosnippet#util#get_last_status(...) abort "{{{
  52. return call(s:get_process().get_last_status, a:000)
  53. endfunction"}}}
  54. function! neosnippet#util#escape_pattern(...) abort "{{{
  55. return call(s:get_string().escape_pattern, a:000)
  56. endfunction"}}}
  57. function! neosnippet#util#iconv(...) abort "{{{
  58. return call(s:get_process().iconv, a:000)
  59. endfunction"}}}
  60. function! neosnippet#util#truncate(...) abort "{{{
  61. return call(s:get_string().truncate, a:000)
  62. endfunction"}}}
  63. function! neosnippet#util#strwidthpart(...) abort "{{{
  64. return call(s:get_string().strwidthpart, a:000)
  65. endfunction"}}}
  66. function! neosnippet#util#expand(path) abort "{{{
  67. return neosnippet#util#substitute_path_separator(
  68. \ expand(escape(a:path, '*?[]"={}'), 1))
  69. endfunction"}}}
  70. function! neosnippet#util#set_default(var, val, ...) abort "{{{
  71. let old_var = get(a:000, 0, '')
  72. if exists(old_var)
  73. let {a:var} = {old_var}
  74. elseif !exists(a:var)
  75. let {a:var} = a:val
  76. endif
  77. endfunction"}}}
  78. function! neosnippet#util#set_dictionary_helper(...) abort "{{{
  79. return call(s:get_prelude().set_dictionary_helper, a:000)
  80. endfunction"}}}
  81. function! neosnippet#util#get_cur_text() abort "{{{
  82. return
  83. \ (mode() ==# 'i' ? (col('.')-1) : col('.')) >= len(getline('.')) ?
  84. \ getline('.') :
  85. \ matchstr(getline('.'),
  86. \ '^.*\%' . col('.') . 'c' . (mode() ==# 'i' ? '' : '.'))
  87. endfunction"}}}
  88. function! neosnippet#util#get_next_text() abort "{{{
  89. return getline('.')[len(neosnippet#util#get_cur_text()) :]
  90. endfunction"}}}
  91. function! neosnippet#util#print_error(string) abort "{{{
  92. echohl Error | echomsg '[neosnippet] ' . a:string | echohl None
  93. endfunction"}}}
  94. function! neosnippet#util#parse_options(args, options_list) abort "{{{
  95. let args = []
  96. let options = {}
  97. for arg in split(a:args, '\%(\\\@<!\s\)\+')
  98. let arg = substitute(arg, '\\\( \)', '\1', 'g')
  99. let matched_list = filter(copy(a:options_list),
  100. \ 'stridx(arg, v:val) == 0')
  101. for option in matched_list
  102. let key = substitute(substitute(option, '-', '_', 'g'), '=$', '', '')[1:]
  103. let options[key] = (option =~ '=$') ?
  104. \ arg[len(option) :] : 1
  105. break
  106. endfor
  107. if empty(matched_list)
  108. call add(args, arg)
  109. endif
  110. endfor
  111. return [args, options]
  112. endfunction"}}}
  113. function! neosnippet#util#get_buffer_config(
  114. \ filetype, buffer_var, user_var, default_var, ...) abort "{{{
  115. let default_val = get(a:000, 0, '')
  116. if exists(a:buffer_var)
  117. return {a:buffer_var}
  118. endif
  119. let filetype = !has_key({a:user_var}, a:filetype)
  120. \ && !has_key(eval(a:default_var), a:filetype) ? '_' : a:filetype
  121. return get({a:user_var}, filetype,
  122. \ get(eval(a:default_var), filetype, default_val))
  123. endfunction"}}}
  124. " Sudo check.
  125. function! neosnippet#util#is_sudo() abort "{{{
  126. return $SUDO_USER != '' && $USER !=# $SUDO_USER
  127. \ && $HOME !=# expand('~'.$USER)
  128. \ && $HOME ==# expand('~'.$SUDO_USER)
  129. endfunction"}}}
  130. function! neosnippet#util#option2list(str) abort "{{{
  131. return type(a:str) == type('') ? split(a:str, '\s*,\s*') : a:str
  132. endfunction"}}}
  133. " vim: foldmethod=marker