dictionary.vim 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "=============================================================================
  2. " FILE: dictionary.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. " Global options definition. "{{{
  28. let g:neocomplete#sources#dictionary#dictionaries =
  29. \ get(g:, 'neocomplete#sources#dictionary#dictionaries', {})
  30. "}}}
  31. " Important variables.
  32. if !exists('s:dictionary_cache')
  33. let s:dictionary_cache = {}
  34. let s:async_dictionary_list = {}
  35. endif
  36. function! neocomplete#sources#dictionary#define() abort "{{{
  37. return s:source
  38. endfunction"}}}
  39. let s:source = {
  40. \ 'name' : 'dictionary',
  41. \ 'kind' : 'keyword',
  42. \ 'mark' : '[D]',
  43. \ 'rank' : 4,
  44. \ 'hooks' : {},
  45. \}
  46. function! s:source.hooks.on_init(context) abort "{{{
  47. augroup neocomplete "{{{
  48. autocmd FileType * call s:make_cache(&l:filetype)
  49. augroup END"}}}
  50. " Create cache directory.
  51. call neocomplete#cache#make_directory('dictionary_cache')
  52. " Initialize check.
  53. call s:make_cache(&l:filetype)
  54. endfunction"}}}
  55. function! s:source.hooks.on_final(context) abort "{{{
  56. silent! delcommand NeoCompleteDictionaryMakeCache
  57. endfunction"}}}
  58. function! s:source.gather_candidates(context) abort "{{{
  59. let list = []
  60. for ft in a:context.filetypes
  61. if !has_key(s:dictionary_cache, ft)
  62. call s:make_cache(ft)
  63. endif
  64. call neocomplete#cache#check_cache(
  65. \ 'dictionary_cache', ft,
  66. \ s:async_dictionary_list, s:dictionary_cache, 1)
  67. let list += get(s:dictionary_cache, ft, [])
  68. endfor
  69. return list
  70. endfunction"}}}
  71. function! s:make_cache(filetype) abort "{{{
  72. if !has_key(s:dictionary_cache, a:filetype)
  73. \ && !has_key(s:async_dictionary_list, a:filetype)
  74. call neocomplete#sources#dictionary#remake_cache(a:filetype)
  75. endif
  76. endfunction"}}}
  77. function! neocomplete#sources#dictionary#remake_cache(filetype) abort "{{{
  78. if !neocomplete#is_enabled()
  79. call neocomplete#initialize()
  80. endif
  81. let filetype = a:filetype
  82. if filetype == ''
  83. let filetype = neocomplete#get_context_filetype(1)
  84. endif
  85. if !has_key(s:async_dictionary_list, filetype)
  86. let s:async_dictionary_list[filetype] = []
  87. endif
  88. let pattern = neocomplete#get_keyword_pattern(filetype, s:source.name)
  89. for dictionary in neocomplete#sources#dictionary#get_dictionaries(filetype)
  90. let dictionary = neocomplete#util#substitute_path_separator(
  91. \ fnamemodify(dictionary, ':p'))
  92. if filereadable(dictionary)
  93. call neocomplete#print_debug('Make cache dictionary: ' . dictionary)
  94. call add(s:async_dictionary_list[filetype], {
  95. \ 'filename' : dictionary,
  96. \ 'cachename' : neocomplete#cache#async_load_from_file(
  97. \ 'dictionary_cache', dictionary, pattern, 'D')
  98. \ })
  99. endif
  100. endfor
  101. endfunction"}}}
  102. function! neocomplete#sources#dictionary#get_dictionaries(filetype) abort "{{{
  103. let filetype = a:filetype
  104. if filetype == ''
  105. let filetype = neocomplete#get_context_filetype(1)
  106. endif
  107. " Make cache.
  108. let dictionaries = get(
  109. \ g:neocomplete#sources#dictionary#dictionaries, filetype, '')
  110. if has_key(g:neocomplete#sources#dictionary#dictionaries, '_')
  111. " Load global dictionaries.
  112. let dictionaries .= ',' .
  113. \ g:neocomplete#sources#dictionary#dictionaries['_']
  114. endif
  115. if dictionaries == '' && &l:dictionary != ''
  116. if ((filetype ==# 'nothing' && &filetype == '')
  117. \ || filetype ==# &filetype)
  118. \ && &l:dictionary !=# &g:dictionary
  119. let dictionaries = &l:dictionary
  120. endif
  121. endif
  122. return split(dictionaries, ',')
  123. endfunction"}}}
  124. let &cpo = s:save_cpo
  125. unlet s:save_cpo
  126. " vim: foldmethod=marker