c11d54a61d87f1558ba0f80201640bb8a5ed39cb
[~madcoder/dotfiles.git] / vim / plugin / ZoomWin.vim
1 " ZoomWin: Brief-like ability to zoom into/out-of a window
2 "  Author: Ron Aaron
3 "          modified by Charles Campbell
4 " Version: 19
5 " History: see :he zoomwin-history
6
7 " ---------------------------------------------------------------------
8 if &cp || exists("s:loaded_zoomwin")
9  finish
10 endif
11 let s:loaded_zoomwin= 1
12
13 " ---------------------------------------------------------------------
14 "  Public Interface:
15 if !hasmapto("<Plug>ZoomWin")
16  nmap <unique> <c-w>o  <Plug>ZoomWin
17 endif
18 nnoremap <silent> <script> <Plug>ZoomWin :set lz<CR>:call ZoomWin()<CR>:set nolz<CR>
19 com! ZoomWin :set lz|silent call ZoomWin()|set nolz
20
21 au VimLeave * call <SID>CleanupSessionFile()
22
23 " ---------------------------------------------------------------------
24
25 " ZoomWin: toggles between a single-window and a multi-window layout
26 "          The original was by Ron Aaron and has been extensively
27 "          modified by Charles E. Campbell.
28 fun! ZoomWin()  
29 "  let g:decho_hide= 1          "Decho
30 "  call Dfunc("ZoomWin() winbufnr(2)=".winbufnr(2))
31
32   let keep_hidden = &hidden
33   let keep_write  = &write
34   if &wmh == 0 || &wmw == 0
35    let keep_wmh = &wmh
36    let keep_wmw = &wmw
37    set wmh=1 wmw=1
38   endif
39   set hidden write
40
41   if winbufnr(2) == -1
42     " there's only one window - restore to multiple-windows mode
43     
44     if exists("s:sessionfile") && filereadable(s:sessionfile)
45       let sponly= s:SavePosn(0)
46
47       " read session file to restore window layout
48           let ei_keep= &ei
49           set ei=all
50       exe 'silent! so '.s:sessionfile
51       let v:this_session= s:sesskeep
52
53       if exists("s:savedposn1")
54         " restore windows' positioning and buffers
55         windo call s:RestorePosn(s:savedposn{winnr()})|unlet s:savedposn{winnr()}
56         call s:GotoWinNum(s:winkeep)
57         unlet s:winkeep
58       endif
59
60           if line(".") != s:origline || virtcol(".") != s:origcol
61            " If the cursor hasn't moved from the original position,
62            " then let the position remain what it was in the original
63            " multi-window layout.
64        call s:RestorePosn(sponly)
65           endif
66
67           " delete session file and variable holding its name
68       call delete(s:sessionfile)
69       unlet s:sessionfile
70           let &ei=ei_keep
71     endif
72
73   else " there's more than one window - go to only-one-window mode
74
75     let s:winkeep    = winnr()
76     let s:sesskeep   = v:this_session
77         let s:origline   = line(".")
78         let s:origcol    = virtcol(".")
79
80         " doesn't work with the command line window (normal mode q:)
81         if &bt == "nofile" && expand("%") == "command-line"
82          echoerr "***error*** ZoomWin doesn't work with the command line window"
83 "     call Dret("ZoomWin")
84          return
85         endif
86
87     " save window positioning commands
88     let ei_keep   = &ei
89         set ei=all
90     windo let s:savedposn{winnr()}= s:SavePosn(1)
91     call s:GotoWinNum(s:winkeep)
92
93     " set up name of session file
94     let s:sessionfile= tempname()
95
96     " save session
97     let ssop_keep = &ssop
98     let &ssop     = 'blank,help,winsize'
99     exe 'mksession! '.s:sessionfile
100     set lz ei=all bh=
101      exe "new! ".s:sessionfile
102      v/wincmd\|split\|resize/d
103      w!
104          bw!
105     set nolz
106     let &ssop = ssop_keep
107     only!
108     let &ei   = ei_keep
109     echomsg expand("%")
110   endif
111
112   " restore user option settings
113   let &hidden= keep_hidden
114   let &write = keep_write
115   if exists("keep_wmw")
116    let &wmh= keep_wmh
117    let &wmw= keep_wmw
118   endif
119 "  call Dret("ZoomWin")
120 endfun
121
122 " ---------------------------------------------------------------------
123
124 " SavePosn: this function sets up a savedposn variable that
125 "          has the commands necessary to restore the view
126 "          of the current window.
127 fun! s:SavePosn(savewinhoriz)
128 "  call Dfunc("SavePosn(savewinhoriz=".a:savewinhoriz.") file<".expand("%").">")
129   let swline    = line(".")
130   let swcol     = col(".")
131   let swwline   = winline()-1
132   let swwcol    = virtcol(".") - wincol()
133   let savedposn = "silent b ".winbufnr(0)."|".swline."|silent norm! z\<cr>"
134   if swwline > 0
135    let savedposn= savedposn.":silent norm! ".swwline."\<c-y>\<cr>:silent norm! zs\<cr>"
136   endif
137   let savedposn= savedposn.":silent call cursor(".swline.",".swcol.")\<cr>"
138
139   if a:savewinhoriz
140    if swwcol > 0
141     let savedposn= savedposn.":silent norm! ".swwcol."zl\<cr>"
142    endif
143
144    " handle certain special settings for the multi-window savedposn call
145    "   bufhidden buftype buflisted
146    let settings= ""
147    if &bh != ""
148         let settings="bh=".&bh
149         setlocal bh=hide
150    endif
151    if !&bl
152         let settings= settings." nobl"
153         setlocal bl
154    endif
155    if &bt != ""
156         let settings= settings." bt=".&bt
157         setlocal bt=
158    endif
159    if settings != ""
160         let savedposn= savedposn.":setlocal ".settings."\<cr>"
161    endif
162
163   endif
164 "  call Dret("SavePosn savedposn<".savedposn.">")
165   return savedposn
166 endfun
167
168 " ---------------------------------------------------------------------
169
170 " s:RestorePosn: this function restores noname and scratch windows
171 fun! s:RestorePosn(savedposn)
172 "  call Dfunc("RestorePosn(savedposn<".a:savedposn.">) file<".expand("%").">")
173   exe a:savedposn
174 "  call Dret("RestorePosn")
175 endfun
176
177 " ---------------------------------------------------------------------
178
179 " CleanupSessionFile: if you exit Vim before cleaning up the
180 "                     supposed-to-be temporary session file
181 fun! s:CleanupSessionFile()
182 "  call Dfunc("CleanupSessionFile()")
183   if exists("s:sessionfile") && filereadable(s:sessionfile)
184 "   call Decho("sessionfile exists and is readable; deleting it")
185    silent! call delete(s:sessionfile)
186    unlet s:sessionfile
187   endif
188 "  call Dret("CleanupSessionFile")
189 endfun
190
191 " ---------------------------------------------------------------------
192
193 " GotoWinNum: this function puts cursor into specified window
194 fun! s:GotoWinNum(winnum)
195 "  call Dfunc("GotoWinNum(winnum=".a:winnum.") winnr=".winnr())
196   if a:winnum != winnr()
197    exe a:winnum."wincmd w"
198   endif
199 "  call Dret("GotoWinNum")
200 endfun
201
202 " ---------------------------------------------------------------------
203 " vim: ts=4