Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <Plug>(vaffle-open-current-window) #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions autoload/vaffle/buffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ function! s:set_up_default_mappings() abort
call s:map_default('n', '<CR>', 'open-selected', '<buffer> <silent>')
call s:map_default('n', 'r', 'rename-selected', '<buffer> <silent>')
" Operations for a item on cursor
call s:map_default('n', 'l', 'open-current', '<buffer> <silent>')
call s:map_default('n', 't', 'open-current-tab', '<buffer> <nowait> <silent>')
call s:map_default('n', 'l', 'open-current', '<buffer> <silent>')
call s:map_default('n', 't', 'open-current-tab', '<buffer> <nowait> <silent>')
call s:map_default('n', 'w', 'open-current-window', '<buffer> <nowait> <silent>')
" Misc
call s:map_default('n', 'o', 'mkdir', '<buffer> <silent>')
call s:map_default('n', 'i', 'new-file', '<buffer> <silent>')
Expand Down
80 changes: 71 additions & 9 deletions autoload/vaffle/file.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ function! s:open_single(item, open_mode) abort
return
endif

let open_cmd = get(s:open_mode_to_cmd_single_map,
\ a:open_mode,
\ 'edit')
execute printf('%s %s',
\ open_cmd,
\ fnameescape(a:item.path))
if a:open_mode ==# 'window'
call s:open_at_specified_window(a:item)
else
let open_cmd = get(s:open_mode_to_cmd_single_map,
\ a:open_mode,
\ 'edit')
execute printf('%s %s',
\ open_cmd,
\ fnameescape(a:item.path))
endif
endfunction


Expand All @@ -43,10 +47,68 @@ function! s:open_multiple(items, open_mode) abort
\ 'split')

for item in a:items
execute printf('%s %s',
\ open_cmd,
\ fnameescape(item.path))
if a:open_mode ==# 'window'
call s:open_at_specified_window(item)
else
execute printf('%s %s',
\ open_cmd,
\ fnameescape(item.path))
endif
endfor
endfunction


function! s:open_at_specified_window(item) abort
let candidate_winnrs = range(1, winnr('$'))

" Remove current winnr.
call remove(candidate_winnrs, winnr() - 1)
if empty(candidate_winnrs)
echo 'No windows to open the file.'
return
endif

let len = len(candidate_winnrs)
let keys = ['a', 's', 'd', 'f', 'h', 'j', 'k', 'l', '1', '2', '3', '4']

" Keep the statuslines of the all windows in the current tab.
let stored_statuslines = map(copy(candidate_winnrs),'getwinvar(v:val, "&statusline")')

" Show the window keys at each center of statusline.
for i in range(0, len - 1)
let winnr = candidate_winnrs[i]
let sline = printf('%' . winwidth(winnr) / 2 . 's', keys[i])
call setwinvar(winnr, '&statusline', sline)
endfor
redrawstatus

" Select a window to open the buffer.
echomsg 'Select window: '
let selected_key = nr2char(getchar())

" `<ESC>` is cancel key.
if selected_key ==? ''
echomsg 'Canceled.'
return
endif

let key_index = index(keys, selected_key)
if key_index == -1
echoerr 'Your input is invalid: ' . string(selected_key)
return
endif

" Open file at the window referred by `target_winnr`.
let target_winnr = candidate_winnrs[key_index]
execute target_winnr . 'wincmd w'
execute printf('edit %s', fnameescape(a:item.path))

" Restore statuslines.
for i in range(0, len - 1)
let winnr = candidate_winnrs[i]
call setwinvar(winnr, '&statusline', stored_statuslines[i])
endfor
redrawstatus
endfunction


Expand Down
1 change: 1 addition & 0 deletions plugin/vaffle.vim
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ nnoremap <silent> <Plug>(vaffle-rename-selected) :<C-u>call vaffle#rename_s
" Operations for a item on cursor
nnoremap <silent> <Plug>(vaffle-open-current) :<C-u>call vaffle#open_current('')<CR>
nnoremap <silent> <Plug>(vaffle-open-current-tab) :<C-u>call vaffle#open_current('tab')<CR>
nnoremap <silent> <Plug>(vaffle-open-current-window) :<C-u>call vaffle#open_current('window')<CR>
" Misc
nnoremap <silent> <Plug>(vaffle-chdir-here) :<C-u>call vaffle#chdir_here()<CR>
nnoremap <silent> <Plug>(vaffle-mkdir) :<C-u>call vaffle#mkdir()<CR>
Expand Down