From f3a5803f56914f829f40b30014adb5b5599e4e8d Mon Sep 17 00:00:00 2001 From: mopp Date: Thu, 4 Jan 2018 15:30:31 +0900 Subject: [PATCH] Add (vaffle-open-current-window) --- autoload/vaffle/buffer.vim | 5 ++- autoload/vaffle/file.vim | 80 +++++++++++++++++++++++++++++++++----- plugin/vaffle.vim | 1 + 3 files changed, 75 insertions(+), 11 deletions(-) diff --git a/autoload/vaffle/buffer.vim b/autoload/vaffle/buffer.vim index 1ecd366..6dc592e 100644 --- a/autoload/vaffle/buffer.vim +++ b/autoload/vaffle/buffer.vim @@ -22,8 +22,9 @@ function! s:set_up_default_mappings() abort call s:map_default('n', '', 'open-selected', ' ') call s:map_default('n', 'r', 'rename-selected', ' ') " Operations for a item on cursor - call s:map_default('n', 'l', 'open-current', ' ') - call s:map_default('n', 't', 'open-current-tab', ' ') + call s:map_default('n', 'l', 'open-current', ' ') + call s:map_default('n', 't', 'open-current-tab', ' ') + call s:map_default('n', 'w', 'open-current-window', ' ') " Misc call s:map_default('n', 'o', 'mkdir', ' ') call s:map_default('n', 'i', 'new-file', ' ') diff --git a/autoload/vaffle/file.vim b/autoload/vaffle/file.vim index bcc57d9..2eef333 100644 --- a/autoload/vaffle/file.vim +++ b/autoload/vaffle/file.vim @@ -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 @@ -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()) + + " `` 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 diff --git a/plugin/vaffle.vim b/plugin/vaffle.vim index 10cdc5f..090360b 100644 --- a/plugin/vaffle.vim +++ b/plugin/vaffle.vim @@ -53,6 +53,7 @@ nnoremap (vaffle-rename-selected) :call vaffle#rename_s " Operations for a item on cursor nnoremap (vaffle-open-current) :call vaffle#open_current('') nnoremap (vaffle-open-current-tab) :call vaffle#open_current('tab') +nnoremap (vaffle-open-current-window) :call vaffle#open_current('window') " Misc nnoremap (vaffle-chdir-here) :call vaffle#chdir_here() nnoremap (vaffle-mkdir) :call vaffle#mkdir()