util.vim 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. "=============================================================================
  2. " FILE: util.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
  4. " License: MIT license {{{
  5. " Permission is hereby granted, free of charge, to any person obtaining
  6. " a copy of this software and associated documentation files (the
  7. " "Software"), to deal in the Software without restriction, including
  8. " without limitation the rights to use, copy, modify, merge, publish,
  9. " distribute, sublicense, and/or sell copies of the Software, and to
  10. " permit persons to whom the Software is furnished to do so, subject to
  11. " the following conditions:
  12. "
  13. " The above copyright notice and this permission notice shall be included
  14. " in all copies or substantial portions of the Software.
  15. "
  16. " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. " OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. " MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. " IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. " CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. " TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. " SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. " }}}
  24. "=============================================================================
  25. let s:save_cpo = &cpo
  26. set cpo&vim
  27. function! neocomplete#util#get_vital() abort "{{{
  28. if !exists('s:V')
  29. let s:V = vital#neocomplete#new()
  30. endif
  31. return s:V
  32. endfunction"}}}
  33. function! s:get_prelude() abort "{{{
  34. if !exists('s:Prelude')
  35. let s:Prelude = neocomplete#util#get_vital().import('Prelude')
  36. endif
  37. return s:Prelude
  38. endfunction"}}}
  39. function! s:get_list() abort "{{{
  40. if !exists('s:List')
  41. let s:List = neocomplete#util#get_vital().import('Data.List')
  42. endif
  43. return s:List
  44. endfunction"}}}
  45. function! s:get_string() abort "{{{
  46. if !exists('s:String')
  47. let s:String = neocomplete#util#get_vital().import('Data.String')
  48. endif
  49. return s:String
  50. endfunction"}}}
  51. function! s:get_process() abort "{{{
  52. if !exists('s:Process')
  53. let s:Process = neocomplete#util#get_vital().import('Process')
  54. endif
  55. return s:Process
  56. endfunction"}}}
  57. function! neocomplete#util#truncate_smart(...) abort "{{{
  58. return call(s:get_string().truncate_skipping, a:000)
  59. endfunction"}}}
  60. function! neocomplete#util#truncate(...) abort "{{{
  61. return call(s:get_string().truncate, a:000)
  62. endfunction"}}}
  63. function! neocomplete#util#strchars(...) abort "{{{
  64. return call(s:get_string().strchars, a:000)
  65. endfunction"}}}
  66. function! neocomplete#util#wcswidth(string) abort "{{{
  67. return strwidth(a:string)
  68. endfunction"}}}
  69. function! neocomplete#util#strwidthpart(...) abort "{{{
  70. return call(s:get_string().strwidthpart, a:000)
  71. endfunction"}}}
  72. function! neocomplete#util#strwidthpart_reverse(...) abort "{{{
  73. return call(s:get_string().strwidthpart_reverse, a:000)
  74. endfunction"}}}
  75. function! neocomplete#util#substitute_path_separator(...) abort "{{{
  76. return call(s:get_prelude().substitute_path_separator, a:000)
  77. endfunction"}}}
  78. function! neocomplete#util#mb_strlen(...) abort "{{{
  79. return call(s:get_string().strchars, a:000)
  80. endfunction"}}}
  81. function! neocomplete#util#uniq(list) abort "{{{
  82. let dict = {}
  83. for item in a:list
  84. if !has_key(dict, item)
  85. let dict[item] = item
  86. endif
  87. endfor
  88. return values(dict)
  89. endfunction"}}}
  90. function! neocomplete#util#system(...) abort "{{{
  91. return call(s:get_process().system, a:000)
  92. endfunction"}}}
  93. function! neocomplete#util#is_windows(...) abort "{{{
  94. return call(s:get_prelude().is_windows, a:000)
  95. endfunction"}}}
  96. function! neocomplete#util#is_mac(...) abort "{{{
  97. return call(s:get_prelude().is_mac, a:000)
  98. endfunction"}}}
  99. function! neocomplete#util#is_complete_select() abort "{{{
  100. return has('patch-7.4.775')
  101. endfunction"}}}
  102. function! neocomplete#util#get_last_status(...) abort "{{{
  103. return call(s:get_process().get_last_status, a:000)
  104. endfunction"}}}
  105. function! neocomplete#util#escape_pattern(...) abort "{{{
  106. return call(s:get_string().escape_pattern, a:000)
  107. endfunction"}}}
  108. function! neocomplete#util#iconv(...) abort "{{{
  109. return call(s:get_process().iconv, a:000)
  110. endfunction"}}}
  111. function! neocomplete#util#uniq(...) abort "{{{
  112. return call(s:get_list().uniq, a:000)
  113. endfunction"}}}
  114. function! neocomplete#util#sort_by(...) abort "{{{
  115. return call(s:get_list().sort_by, a:000)
  116. endfunction"}}}
  117. " Sudo check.
  118. function! neocomplete#util#is_sudo() abort "{{{
  119. return $SUDO_USER != '' && $USER !=# $SUDO_USER
  120. \ && $HOME !=# expand('~'.$USER)
  121. \ && $HOME ==# expand('~'.$SUDO_USER)
  122. endfunction"}}}
  123. function! neocomplete#util#glob(pattern, ...) abort "{{{
  124. if a:pattern =~ "'"
  125. " Use glob('*').
  126. let cwd = getcwd()
  127. let base = neocomplete#util#substitute_path_separator(
  128. \ fnamemodify(a:pattern, ':h'))
  129. execute 'lcd' fnameescape(base)
  130. let files = map(split(neocomplete#util#substitute_path_separator(
  131. \ glob('*')), '\n'), "base . '/' . v:val")
  132. execute 'lcd' fnameescape(cwd)
  133. return files
  134. endif
  135. " let is_force_glob = get(a:000, 0, 0)
  136. let is_force_glob = get(a:000, 0, 1)
  137. if !is_force_glob && a:pattern =~ '^[^\\*]\+/\*'
  138. \ && neocomplete#util#has_vimproc() && exists('*vimproc#readdir')
  139. return filter(vimproc#readdir(a:pattern[: -2]), 'v:val !~ "/\\.\\.\\?$"')
  140. else
  141. " Escape [.
  142. if neocomplete#util#is_windows()
  143. let glob = substitute(a:pattern, '\[', '\\[[]', 'g')
  144. else
  145. let glob = escape(a:pattern, '[')
  146. endif
  147. return split(neocomplete#util#substitute_path_separator(glob(glob)), '\n')
  148. endif
  149. endfunction"}}}
  150. function! neocomplete#util#expand(path) abort "{{{
  151. return expand(escape(a:path, '*?[]"={}'), 1)
  152. endfunction"}}}
  153. function! neocomplete#util#set_default(var, val, ...) abort "{{{
  154. if !exists(a:var) || type({a:var}) != type(a:val)
  155. let alternate_var = get(a:000, 0, '')
  156. let {a:var} = exists(alternate_var) ?
  157. \ {alternate_var} : a:val
  158. endif
  159. endfunction"}}}
  160. function! neocomplete#util#set_dictionary_helper(variable, keys, pattern) abort "{{{
  161. for key in split(a:keys, '\s*,\s*')
  162. if !has_key(a:variable, key)
  163. let a:variable[key] = a:pattern
  164. endif
  165. endfor
  166. endfunction"}}}
  167. function! neocomplete#util#set_default_dictionary(variable, keys, value) abort "{{{
  168. if !exists('s:disable_dictionaries')
  169. let s:disable_dictionaries = {}
  170. endif
  171. if has_key(s:disable_dictionaries, a:variable)
  172. return
  173. endif
  174. call neocomplete#util#set_dictionary_helper({a:variable}, a:keys, a:value)
  175. endfunction"}}}
  176. function! neocomplete#util#disable_default_dictionary(variable) abort "{{{
  177. if !exists('s:disable_dictionaries')
  178. let s:disable_dictionaries = {}
  179. endif
  180. let s:disable_dictionaries[a:variable] = 1
  181. endfunction"}}}
  182. function! neocomplete#util#split_rtp(...) abort "{{{
  183. let rtp = a:0 ? a:1 : &runtimepath
  184. if type(rtp) == type([])
  185. return rtp
  186. endif
  187. if rtp !~ '\\'
  188. return split(rtp, ',')
  189. endif
  190. let split = split(rtp, '\\\@<!\%(\\\\\)*\zs,')
  191. return map(split,'substitute(v:val, ''\\\([\\,]\)'', "\\1", "g")')
  192. endfunction"}}}
  193. function! neocomplete#util#join_rtp(list) abort "{{{
  194. return join(map(copy(a:list), 's:escape(v:val)'), ',')
  195. endfunction"}}}
  196. " Escape a path for runtimepath.
  197. function! s:escape(path) abort"{{{
  198. return substitute(a:path, ',\|\\,\@=', '\\\0', 'g')
  199. endfunction"}}}
  200. function! neocomplete#util#has_vimproc() abort "{{{
  201. " Initialize.
  202. if !exists('g:neocomplete#use_vimproc')
  203. " Check vimproc.
  204. try
  205. call vimproc#version()
  206. let exists_vimproc = 1
  207. catch
  208. let exists_vimproc = 0
  209. endtry
  210. let g:neocomplete#use_vimproc = exists_vimproc
  211. endif
  212. return g:neocomplete#use_vimproc
  213. endfunction"}}}
  214. function! neocomplete#util#dup_filter(list) abort "{{{
  215. let dict = {}
  216. for keyword in a:list
  217. if !has_key(dict, keyword.word)
  218. let dict[keyword.word] = keyword
  219. endif
  220. endfor
  221. return values(dict)
  222. endfunction"}}}
  223. function! neocomplete#util#convert2list(expr) abort "{{{
  224. return type(a:expr) ==# type([]) ? a:expr : [a:expr]
  225. endfunction"}}}
  226. function! neocomplete#util#is_text_changed() abort "{{{
  227. " Note: Vim 7.4.143 fixed TextChangedI bug.
  228. return v:version > 704 || v:version == 704 && has('patch143')
  229. endfunction"}}}
  230. let &cpo = s:save_cpo
  231. unlet s:save_cpo
  232. " vim: foldmethod=marker