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