"============================================================================= " FILE: helper.vim " AUTHOR: Shougo Matsushita " License: MIT license {{{ " Permission is hereby granted, free of charge, to any person obtaining " a copy of this software and associated documentation files (the " "Software"), to deal in the Software without restriction, including " without limitation the rights to use, copy, modify, merge, publish, " distribute, sublicense, and/or sell copies of the Software, and to " permit persons to whom the Software is furnished to do so, subject to " the following conditions: " " The above copyright notice and this permission notice shall be included " in all copies or substantial portions of the Software. " " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS " OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. " IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY " CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, " TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE " SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. " }}} "============================================================================= let s:save_cpo = &cpo set cpo&vim function! vimfiler#helper#_get_directory_files(directory, ...) abort "{{{ " Save current files. let is_manualed = get(a:000, 0, 0) let context = { \ 'vimfiler__is_dummy' : 0, \ 'is_redraw' : is_manualed, \ } let path = a:directory if path !~ '^\a\w*:' let path = b:vimfiler.source . ':' . path endif let args = vimfiler#parse_path(path) let current_files = vimfiler#init#_candidates( \ unite#get_vimfiler_candidates([args], context), \ b:vimfiler.source) for file in current_files " Initialize. let file.vimfiler__is_marked = 0 let file.vimfiler__is_opened = 0 let file.vimfiler__nest_level = 0 endfor return vimfiler#helper#_sort_files(current_files) endfunction"}}} function! vimfiler#helper#_sort_files(files) abort "{{{ let files = a:files let dirs = filter(copy(a:files), 'v:val.vimfiler__is_directory') let files = filter(copy(a:files), '!v:val.vimfiler__is_directory') if g:vimfiler_directory_display_top let files = s:sort(dirs, b:vimfiler.local_sort_type) \+ s:sort(files, b:vimfiler.local_sort_type) else let files = s:sort(files + dirs, b:vimfiler.local_sort_type) endif return files endfunction"}}} function! vimfiler#helper#_parse_path(path) abort "{{{ let path = a:path let source_name = matchstr(path, '^\h[^:]*\ze:') if (vimfiler#util#is_windows() && len(source_name) == 1) \ || source_name == '' " Default source. let source_name = 'file' let source_arg = path if vimfiler#util#is_win_path(source_arg) let source_arg = vimfiler#util#substitute_path_separator( \ fnamemodify(expand(source_arg), ':p')) endif else let source_arg = path[len(source_name)+1 :] endif let source_args = source_arg == '' ? [] : \ map(split(source_arg, '\\\@ winheight(0) ? 'zz' : line('$').'zb') call setpos('.', pos) endfunction"}}} function! vimfiler#helper#_call_filters(files, context) abort "{{{ let files = a:files for filter in b:vimfiler.filters let files = filter.filter(files, a:context) endfor return files endfunction"}}} function! s:sort(files, type) abort "{{{ let ignorecase_save = &ignorecase try let &ignorecase = vimfiler#util#is_windows() if a:type =~? '^n\%[one]$' " Ignore. let files = a:files elseif a:type =~? '^s\%[ize]$' let files = vimfiler#util#sort_by( \ a:files, 'v:val.vimfiler__filesize') elseif a:type =~? '^e\%[xtension]$' let files = vimfiler#util#sort_by( \ a:files, 'v:val.vimfiler__extension') elseif a:type =~? '^f\%[ilename]$' let files = vimfiler#helper#_sort_human( \ a:files, vimfiler#util#has_lua()) elseif a:type =~? '^t\%[ime]$' let files = vimfiler#util#sort_by( \ a:files, 'v:val.vimfiler__filetime') elseif a:type =~? '^m\%[anual]$' " Not implemented. let files = a:files else throw 'Invalid sort type.' endif finally let &ignorecase = ignorecase_save endtry if a:type =~ '^\u' " Reverse order. let files = reverse(files) endif return files endfunction"}}} function! vimfiler#helper#_sort_human(candidates, has_lua) abort "{{{ if !a:has_lua || len(filter(copy(a:candidates), \ "v:val.vimfiler__filename =~ '\\d'")) >= 2 return sort(a:candidates, 's:compare_filename') endif " Use lua interface. lua << EOF do local ignorecase = vim.eval('&ignorecase') local candidates = vim.eval('a:candidates') local t = {} for i = 1, #candidates do t[i] = candidates[i-1] if ignorecase ~= 0 then t[i].vimfiler__filename = string.lower(t[i].vimfiler__filename) end end table.sort(t, function(a, b) return a.vimfiler__filename < b.vimfiler__filename end) for i = 0, #candidates-1 do candidates[i] = t[i+1] end end EOF return a:candidates endfunction"}}} " Compare filename by human order. "{{{ function! s:compare_filename(i1, i2) abort let words_1 = s:get_words(a:i1.vimfiler__filename) let words_2 = s:get_words(a:i2.vimfiler__filename) let words_1_len = len(words_1) let words_2_len = len(words_2) for i in range(0, min([words_1_len, words_2_len])-1) if words_1[i] >? words_2[i] return 1 elseif words_1[i]