file.vim 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "=============================================================================
  2. " FILE: file.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. let s:source = {
  28. \ 'name' : 'file',
  29. \ 'kind' : 'manual',
  30. \ 'mark' : '[F]',
  31. \ 'rank' : 10,
  32. \ 'sorters' : 'sorter_filename',
  33. \ 'converters' : ['converter_remove_overlap', 'converter_abbr'],
  34. \ 'is_volatile' : 1,
  35. \ 'input_pattern': '/',
  36. \}
  37. function! s:source.get_complete_position(context) abort "{{{
  38. let filetype = a:context.filetype
  39. if filetype ==# 'vimshell' || filetype ==# 'unite' || filetype ==# 'int-ssh'
  40. return -1
  41. endif
  42. " Filename pattern.
  43. let pattern = neocomplete#get_keyword_pattern_end('filename', self.name)
  44. let [complete_pos, complete_str] =
  45. \ neocomplete#helper#match_word(a:context.input, pattern)
  46. if complete_str =~ '//' || complete_str == '/' ||
  47. \ (neocomplete#is_auto_complete() &&
  48. \ complete_str !~ '/' ||
  49. \ complete_str =~#
  50. \ '\\[^ ;*?[]"={}'']\|\.\.\+$\|/c\%[ygdrive/]$\|\${')
  51. " Not filename pattern.
  52. return -1
  53. endif
  54. if complete_str =~ '/'
  55. let complete_pos += strridx(complete_str, '/') + 1
  56. endif
  57. return complete_pos
  58. endfunction"}}}
  59. function! s:source.gather_candidates(context) abort "{{{
  60. let pattern = neocomplete#get_keyword_pattern_end('filename', self.name)
  61. let complete_str =
  62. \ neocomplete#helper#match_word(a:context.input, pattern)[1]
  63. if neocomplete#is_windows() && complete_str =~ '^[\\/]'
  64. return []
  65. endif
  66. let cwd = getcwd()
  67. try
  68. let buffer_dir = fnamemodify(bufname('%'), ':h')
  69. if isdirectory(buffer_dir)
  70. " cd to buffer directory.
  71. execute 'lcd' fnameescape(buffer_dir)
  72. endif
  73. let files = s:get_glob_files(complete_str, '')
  74. finally
  75. execute 'lcd' fnameescape(cwd)
  76. endtry
  77. return files
  78. endfunction"}}}
  79. let s:cached_files = {}
  80. function! s:get_glob_files(complete_str, path) abort "{{{
  81. let path = ',,' . substitute(a:path, '\.\%(,\|$\)\|,,', '', 'g')
  82. let complete_str = neocomplete#util#substitute_path_separator(
  83. \ substitute(a:complete_str, '\\\(.\)', '\1', 'g'))
  84. let complete_str = substitute(complete_str, '[^/.]\+$', '', '')
  85. " Note: Support ${env}
  86. let complete_str = substitute(complete_str, '\${\(\w\+\)}', '$\1', 'g')
  87. let glob = (complete_str !~ '\*$')?
  88. \ complete_str . '*' : complete_str
  89. let ftype = getftype(glob)
  90. if ftype != '' && ftype !=# 'dir'
  91. " Note: If glob() device files, Vim may freeze!
  92. return []
  93. endif
  94. if a:path == ''
  95. let files = neocomplete#util#glob(glob)
  96. else
  97. try
  98. let globs = globpath(path, glob)
  99. catch
  100. return []
  101. endtry
  102. let files = split(substitute(globs, '\\', '/', 'g'), '\n')
  103. endif
  104. call filter(files, 'v:val !~ "/\\.\\.\\?$"')
  105. let files = map(
  106. \ files, "{
  107. \ 'word' : fnamemodify(v:val, ':t'),
  108. \ 'action__is_directory' : isdirectory(v:val),
  109. \ 'kind' : (isdirectory(v:val) ? 'dir' : 'file'),
  110. \ }")
  111. let candidates = []
  112. for dict in files
  113. let abbr = dict.word
  114. if dict.action__is_directory && dict.word !~ '/$'
  115. let abbr .= '/'
  116. if g:neocomplete#enable_auto_delimiter
  117. let dict.word .= '/'
  118. endif
  119. endif
  120. let dict.abbr = abbr
  121. " Escape word.
  122. let dict.word = escape(dict.word, ' ;*?[]"={}''')
  123. call add(candidates, dict)
  124. endfor
  125. return candidates
  126. endfunction"}}}
  127. function! neocomplete#sources#file#define() abort "{{{
  128. return s:source
  129. endfunction"}}}
  130. let &cpo = s:save_cpo
  131. unlet s:save_cpo
  132. " vim: foldmethod=marker