]> git.rmz.io Git - dotfiles.git/blob - vim/plugin/close_another_window.vim
qutebrowser: update config/bindings
[dotfiles.git] / vim / plugin / close_another_window.vim
1 "=============================================================================
2 " File: close_another_window.vim
3 " Author: Samir Benmendil <me [at] rmz [dot] io>
4 " Contributor: Dmitry Frank (dimon.frank@gmail.com)
5 " Version: 1.04
6 "=============================================================================
7 " See documentation in accompanying help file
8 " You may use this code in whatever way you see fit.
9
10 function! s:getchar()
11 let c = getchar()
12 if c =~ '^\d\+$'
13 let c = nr2char(c)
14 endif
15 return c
16 endfunction
17
18 function! s:inputtarget()
19 let a = s:getchar()
20 let c = ''
21 while a != ' '
22 if a =~ '^[hjkl]$'
23 let c = c . a
24 endif
25 let a = s:getchar()
26 endwhile
27 return c
28 endfunction
29
30 function! <SID>CloseWindow(boolBang)
31 if (!a:boolBang)
32 :close
33 else
34 :close!
35 endif
36 endfunction
37
38 "" function to close other window
39 " @param sWincmd - window movement command(s).
40 " for example, "h" used to close left window,
41 " "j" used to close lower window,
42 " etc.
43 "
44 " You can combine them:
45 " "jl" used to go to lower window,
46 " then go to right window,
47 " then close it.
48 " etc.
49 function! <SID>CloseAnotherWindow(sWincmd, boolBang)
50
51 if empty(a:sWincmd)
52 call <SID>CloseWindow(a:boolBang)
53 return
54 endif
55
56 " remember current winnr, e.g. winnr of starting window
57 " (we need to go to the same winnr after close other window)
58 let l:iCurWinnr = winnr()
59
60 let l:iIndex = 0
61
62 while (l:iIndex < strlen(a:sWincmd))
63 " move to window that should be closed and get its winnr
64 exec 'wincmd '.a:sWincmd[l:iIndex]
65 let l:iClosedWinnr = winnr()
66
67 let l:iIndex += 1
68 endwhile
69
70 " if its winnr is the same as starting winnr, then do nothing.
71 " (you should use just :q instead)
72 if (l:iCurWinnr == l:iClosedWinnr)
73 return
74 endif
75
76 " calculate target winnr
77 if (l:iCurWinnr > l:iClosedWinnr)
78 let l:iCurWinnr -= 1
79 endif
80
81 " close needed window
82 call <SID>CloseWindow(a:boolBang)
83
84 " go to starting window
85 while (winnr() != l:iCurWinnr)
86 :wincmd w
87 endwhile
88 endfunction
89
90 " define plugin's commands
91 command! -nargs=? -bang -complete=file CloseLeftWindow call <SID>CloseAnotherWindow('h', <bang>0)
92 command! -nargs=? -bang -complete=file CloseBelowWindow call <SID>CloseAnotherWindow('j', <bang>0)
93 command! -nargs=? -bang -complete=file CloseAboveWindow call <SID>CloseAnotherWindow('k', <bang>0)
94 command! -nargs=? -bang -complete=file CloseRightWindow call <SID>CloseAnotherWindow('l', <bang>0)
95
96 command! -nargs=? -bang -complete=file CloseAnotherWindow call <SID>CloseAnotherWindow("<args>", <bang>0)
97
98 nnoremap <silent> <Plug>CloseAnotherWindow :<C-U>call <SID>CloseAnotherWindow(<SID>inputtarget(), 0)<CR>