Refactoring: handle all file opening in #open_buffers()

This commit is contained in:
Marco Hinz 2015-05-28 17:14:25 +02:00
parent d2af054a59
commit 8fd4247218

View file

@ -99,6 +99,10 @@ function! startify#insane_in_the_membrane() abort
\ 'bookmarks', \ 'bookmarks',
\ ]) \ ])
let s:tick = 0
let s:entries = {}
let s:markers = {}
for item in s:lists for item in s:lists
if type(item) == 1 if type(item) == 1
call s:show_{item}() call s:show_{item}()
@ -333,50 +337,37 @@ function! startify#session_list_as_string(lead, ...) abort
endfunction endfunction
" Function: #open_buffers {{{1 " Function: #open_buffers {{{1
function! startify#open_buffers() function! startify#open_buffers(...) abort
" markers found; open one or more buffers if exists('a:1') " used in mappings
if exists('s:marked') && !empty(s:marked) execute 'edit' s:entries[a:1]
elseif empty(s:markers) " open the current entry
call s:set_mark('B')
return startify#open_buffers()
else " open all marked entries in the order they were given
enew enew
setlocal nobuflisted setlocal nobuflisted
for val in values(s:marked) for markers in sort(values(s:markers), 's:sort_by_tick')
let [path, type] = val[1:2] let path = s:entries[markers.original_index]
let path = fnameescape(path) let type = markers.type
if has('win32')
let path = substitute(path, '\[', '\[[]', 'g')
endif
if line2byte('$') == -1 if line2byte('$') == -1
" open in current window
execute 'edit' path execute 'edit' path
elseif type == 'S' elseif type == 'S'
" open in split
execute 'split' path execute 'split' path
elseif type == 'V' elseif type == 'V'
" open in vsplit
execute 'vsplit' path execute 'vsplit' path
elseif type == 'T' elseif type == 'T'
" open in tab
execute 'tabnew' path execute 'tabnew' path
else else
" open in current window
execute 'edit' path execute 'edit' path
endif endif
call s:check_user_options() call s:check_user_options()
endfor endfor
" remove markers for next instance of :Startify
if exists('s:marked')
unlet s:marked
endif
else " no markers found; open a single buffer
call s:set_mark('B')
return startify#open_buffers()
endif endif
endfunction endfunction
" Function: s:display_by_path {{{1 " Function: s:display_by_path {{{1
function! s:display_by_path(path_prefix, path_format) abort function! s:display_by_path(path_prefix, path_format) abort
let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles', let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
@ -393,7 +384,7 @@ function! s:display_by_path(path_prefix, path_format) abort
if has('win32') if has('win32')
let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g') let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
endif endif
execute 'nnoremap <buffer><silent>' index ':edit' absolute_path '<bar> call <sid>check_user_options()<cr>' let s:entries[index] = absolute_path
let s:entry_number += 1 let s:entry_number += 1
endfor endfor
@ -560,6 +551,10 @@ endfunction
" Function: s:set_mappings {{{1 " Function: s:set_mappings {{{1
function! s:set_mappings() abort function! s:set_mappings() abort
for index in keys(s:entries)
execute 'nnoremap <buffer><silent>' index ':call startify#open_buffers('. string(index) .')<cr>'
endfor
nnoremap <buffer><silent> e :enew<cr> nnoremap <buffer><silent> e :enew<cr>
nnoremap <buffer><silent> i :enew <bar> startinsert<cr> nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
nnoremap <buffer><silent> <insert> :enew <bar> startinsert<cr> nnoremap <buffer><silent> <insert> :enew <bar> startinsert<cr>
@ -573,54 +568,54 @@ function! s:set_mappings() abort
" Prevent 'nnoremap j gj' mappings, since they would break navigation. " Prevent 'nnoremap j gj' mappings, since they would break navigation.
" (One can't leave the [x].) " (One can't leave the [x].)
if !empty(mapcheck('h', 'n')) if !empty(maparg('j', 'n'))
nnoremap <buffer> h h
endif
if !empty(mapcheck('j', 'n'))
nnoremap <buffer> j j nnoremap <buffer> j j
endif endif
if !empty(mapcheck('k', 'n')) if !empty(maparg('k', 'n'))
nnoremap <buffer> k k nnoremap <buffer> k k
endif endif
if !empty(mapcheck('l', 'n'))
nnoremap <buffer> l l
endif
endfunction endfunction
" Function: s:set_mark {{{1 " Function: s:set_mark {{{1
"
" Markers are saved in the s:marked dict using the follow format:
" - s:marked[0]: ID
" - s:marked[1]: path
" - s:marked[2]: type (buffer, split, vsplit)
"
function! s:set_mark(type) abort function! s:set_mark(type) abort
if !exists('s:marked') let index = expand('<cword>')
let s:marked = {} let line = line('.')
endif
let [id, path] = matchlist(getline('.'), '\v\[(.{-})\]\s+(.*)')[1:2] if index =~# '[eq]'
let path = fnamemodify(path, ':p')
if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
return return
endif endif
setlocal modifiable setlocal modifiable
" set markers if has_key(s:markers, line)
if id =~# '[BSTV]' if s:markers[line].type == a:type " remove type
" replace marker by old ID execute 'normal! ci]'. s:markers[line].original_index
execute 'normal! ci]'. remove(s:marked, line('.'))[0] call remove(s:markers, line)
else else " update type
" save ID and replace it by the marker of the given type execute 'normal! ci]'. repeat(a:type, len(index))
let s:marked[line('.')] = [id, path, a:type] let s:markers[line].type = a:type
execute 'normal! ci]'. repeat(a:type, len(id)) let s:markers[line].tick = s:tick
let s:tick += 1
endif
else " set type
let s:markers[line] = {
\ 'line': line,
\ 'type': a:type,
\ 'original_index': index,
\ 'tick': s:tick,
\ }
let s:tick += 1
execute 'normal! ci]'. repeat(a:type, len(index))
endif endif
setlocal nomodifiable nomodified setlocal nomodifiable nomodified
endfunction endfunction
" Function: s:sort_by_tick {{{1
function! s:sort_by_tick(one, two)
return a:one.tick - a:two.tick
endfunction
" Function: s:check_user_options {{{1 " Function: s:check_user_options {{{1
function! s:check_user_options() abort function! s:check_user_options() abort
let path = expand('%') let path = expand('%')