Refactoring: show_dir and show_files
This de-duplicates the shared code, and adds some (first) improvements to not stat all of v:oldfiles (twice). If accepted, it allows for easily adding a cache for abs_path handling etc. - Add _show_filtered_oldfiles and _get_filtered_oldfiles - Fixes g:startify_files_number<1
This commit is contained in:
parent
f86d604915
commit
1b6b53f2f9
|
@ -369,72 +369,20 @@ function! startify#open_buffers() abort
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" Function: s:show_dir {{{1
|
|
||||||
function! s:show_dir(cnt) abort
|
|
||||||
if empty(v:oldfiles)
|
|
||||||
return a:cnt
|
|
||||||
endif
|
|
||||||
|
|
||||||
let cnt = a:cnt
|
" Function: s:_get_filtered_oldfiles {{{1
|
||||||
let num = s:numfiles
|
function! s:_get_filtered_oldfiles(num, prefix)
|
||||||
|
let num = a:num
|
||||||
|
let filter_prefix = len(a:prefix) > 0
|
||||||
let entries = {}
|
let entries = {}
|
||||||
let cwd = escape(getcwd(), '\')
|
let files = []
|
||||||
let files = filter(map(copy(v:oldfiles),
|
|
||||||
\ 'glob(fnameescape(fnamemodify(resolve(v:val), ":p")))'), 'match(v:val, cwd) == 0')
|
|
||||||
|
|
||||||
if !empty(files)
|
for fname in copy(v:oldfiles)
|
||||||
if exists('s:last_message')
|
if num <= 0
|
||||||
call s:print_section_header()
|
break
|
||||||
endif
|
endif
|
||||||
|
|
||||||
for abs_path in files
|
let abs_path = glob(fnameescape(fnamemodify(resolve(fname), ":p")))
|
||||||
let abs_path = glob(abs_path)
|
|
||||||
|
|
||||||
" filter duplicates, bookmarks and entries from the skiplist
|
|
||||||
if has_key(entries, abs_path)
|
|
||||||
\ || !filereadable(abs_path)
|
|
||||||
\ || s:is_in_skiplist(abs_path)
|
|
||||||
\ || (exists('g:startify_bookmarks') && s:is_bookmark(abs_path))
|
|
||||||
continue
|
|
||||||
endif
|
|
||||||
|
|
||||||
let entries[abs_path] = 1
|
|
||||||
let index = s:get_index_as_string(cnt)
|
|
||||||
let display_path = fnamemodify(abs_path, s:relative_path ? ':.' : ':p:~')
|
|
||||||
|
|
||||||
call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . display_path)
|
|
||||||
execute 'nnoremap <buffer><silent>' index ':edit' escape(abs_path, ' ') '<bar> call <sid>check_user_options()<cr>'
|
|
||||||
|
|
||||||
let cnt += 1
|
|
||||||
let num -= 1
|
|
||||||
|
|
||||||
if !num
|
|
||||||
break
|
|
||||||
endif
|
|
||||||
endfor
|
|
||||||
|
|
||||||
call append('$', '')
|
|
||||||
endif
|
|
||||||
|
|
||||||
return cnt
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
" Function: s:show_files {{{1
|
|
||||||
function! s:show_files(cnt) abort
|
|
||||||
if empty(v:oldfiles)
|
|
||||||
return a:cnt
|
|
||||||
endif
|
|
||||||
|
|
||||||
if exists('s:last_message')
|
|
||||||
call s:print_section_header()
|
|
||||||
endif
|
|
||||||
|
|
||||||
let cnt = a:cnt
|
|
||||||
let num = s:numfiles
|
|
||||||
let entries = {}
|
|
||||||
|
|
||||||
for fname in v:oldfiles
|
|
||||||
let abs_path = glob(fnameescape(fnamemodify(resolve(fname), ':p')))
|
|
||||||
|
|
||||||
" filter duplicates, bookmarks and entries from the skiplist
|
" filter duplicates, bookmarks and entries from the skiplist
|
||||||
if has_key(entries, abs_path)
|
if has_key(entries, abs_path)
|
||||||
|
@ -444,26 +392,54 @@ function! s:show_files(cnt) abort
|
||||||
continue
|
continue
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let entries[abs_path] = 1
|
if filter_prefix && match(abs_path, a:prefix) != 0
|
||||||
let index = s:get_index_as_string(cnt)
|
continue
|
||||||
let display_path = fnamemodify(abs_path, s:relative_path ? ':.' : ':p:~')
|
|
||||||
|
|
||||||
call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . display_path)
|
|
||||||
execute 'nnoremap <buffer><silent>' index ':edit' escape(abs_path, ' ') '<bar> call <sid>check_user_options()<cr>'
|
|
||||||
|
|
||||||
let cnt += 1
|
|
||||||
let num -= 1
|
|
||||||
|
|
||||||
if !num
|
|
||||||
break
|
|
||||||
endif
|
endif
|
||||||
endfor
|
|
||||||
|
|
||||||
call append('$', '')
|
let display_path = fnamemodify(abs_path, s:relative_path ? ':.' : ':p:~')
|
||||||
|
let entries[abs_path] = 1
|
||||||
|
|
||||||
|
let files += [[abs_path, display_path]]
|
||||||
|
|
||||||
|
let num -= 1
|
||||||
|
endfor
|
||||||
|
return files
|
||||||
|
endfun
|
||||||
|
|
||||||
|
" Function: s:_show_filtered_oldfiles {{{1
|
||||||
|
function! s:_show_filtered_oldfiles(cnt, prefix) abort
|
||||||
|
let cnt = a:cnt
|
||||||
|
let files = s:_get_filtered_oldfiles(s:numfiles, a:prefix)
|
||||||
|
|
||||||
|
if !empty(files)
|
||||||
|
if exists('s:last_message')
|
||||||
|
call s:print_section_header()
|
||||||
|
endif
|
||||||
|
|
||||||
|
for [abs_path, display_path] in files
|
||||||
|
let index = s:get_index_as_string(cnt)
|
||||||
|
call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . display_path)
|
||||||
|
execute 'nnoremap <buffer><silent>' index ':edit' escape(abs_path, ' ') '<bar> call <sid>check_user_options()<cr>'
|
||||||
|
let cnt += 1
|
||||||
|
endfor
|
||||||
|
|
||||||
|
call append('$', '')
|
||||||
|
endif
|
||||||
|
|
||||||
return cnt
|
return cnt
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
" Function: s:show_dir {{{1
|
||||||
|
function! s:show_dir(cnt) abort
|
||||||
|
let cwd = escape(getcwd(), '\')
|
||||||
|
return s:_show_filtered_oldfiles(a:cnt, cwd)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" Function: s:show_files {{{1
|
||||||
|
function! s:show_files(cnt) abort
|
||||||
|
return s:_show_filtered_oldfiles(a:cnt, '')
|
||||||
|
endfunction
|
||||||
|
|
||||||
" Function: s:show_sessions {{{1
|
" Function: s:show_sessions {{{1
|
||||||
function! s:show_sessions(cnt) abort
|
function! s:show_sessions(cnt) abort
|
||||||
let sfiles = split(globpath(s:session_dir, '*'), '\n')
|
let sfiles = split(globpath(s:session_dir, '*'), '\n')
|
||||||
|
|
Loading…
Reference in a new issue