1 " ZoomWin: Brief-like ability to zoom into/out-of a window
2 " Author: Charles Campbell
3 " original version by Ron Aaron
6 " History: see :help zoomwin-history {{{1
7 " GetLatestVimScripts: 508 1 :AutoInstall: ZoomWin.vim
9 " ---------------------------------------------------------------------
11 if &cp || exists("g:loaded_ZoomWin")
16 echo "***warning*** this version of ZoomWin needs vim 7.2"
21 let g:loaded_ZoomWin = "v23"
25 " =====================================================================
28 " ---------------------------------------------------------------------
29 " ZoomWin#ZoomWin: toggles between a single-window and a multi-window layout {{{2
30 " The original version was by Ron Aaron.
31 fun! ZoomWin#ZoomWin()
32 " let g:decho_hide= 1 "Decho
33 " call Dfunc("ZoomWin#ZoomWin() winbufnr(2)=".winbufnr(2))
35 " if the vim doesn't have +mksession, only a partial zoom is available {{{3
37 if !exists("s:partialzoom")
38 echomsg "missing the +mksession feature; only a partial zoom is available"
42 echoerr "***sorry*** you need an updated vim, preferably with +mksession"
50 let s:winrestore = winrestcmd()
53 " call Dret("ZoomWin#ZoomWin : partialzoom=".s:partialzoom)
57 " Close certain windows {{{3
58 call s:ZoomWinPreserve(0)
60 " save options. Force window minimum height/width to be >= 1 {{{3
61 let keep_hidden = &hidden
62 let keep_write = &write
65 if &wmh == 0 || &wmw == 0
68 silent! set wmh=1 wmw=1
74 " there's only one window - restore to multiple-windows mode {{{3
75 " call Decho("there's only one window - restore to multiple windows")
77 if exists("s:sessionfile") && filereadable(s:sessionfile)
78 " save position in current one-window-only
79 " call Decho("save position in current one-window-only in sponly")
80 let sponly = s:SavePosn(0)
81 let s:origline = line(".")
82 let s:origcol = virtcol(".")
84 " source session file to restore window layout
87 exe 'silent! so '.fnameescape(s:sessionfile)
89 let v:this_session= s:sesskeep
91 if exists("s:savedposn1")
92 " restore windows' positioning and buffers
93 " call Decho("restore windows, positions, buffers")
94 windo call s:RestorePosn(s:savedposn{winnr()})|unlet s:savedposn{winnr()}
95 call s:GotoWinNum(s:winkeep)
99 if line(".") != s:origline || virtcol(".") != s:origcol
100 " If the cursor hasn't moved from the original position,
101 " then let the position remain what it was in the original
102 " multi-window layout.
103 " call Decho("restore position using sponly")
104 call s:RestorePosn(sponly)
107 " delete session file and variable holding its name
108 " call Decho("delete session file")
109 call delete(s:sessionfile)
114 else " there's more than one window - go to only-one-window mode {{{3
115 " call Decho("there's multiple windows - goto one-window-only")
117 let s:winkeep = winnr()
118 let s:sesskeep = v:this_session
120 " doesn't work with the command line window (normal mode q:)
121 if &bt == "nofile" && expand("%") == (v:version < 702 ? 'command-line' : '[Command Line]')
122 echoerr "***error*** ZoomWin#ZoomWin doesn't work with the ".expand("%")." window"
123 " call Dret("ZoomWin#ZoomWin : ".expand('%')." window error")
126 " call Decho("1: @@<".@@.">")
128 " disable all events (autocmds)
129 " call Decho("disable events")
132 " call Decho("2: @@<".@@.">")
134 " save window positioning commands
135 " call Decho("save window positioning commands")
136 windo let s:savedposn{winnr()}= s:SavePosn(1)
137 call s:GotoWinNum(s:winkeep)
139 " set up name of session file
140 " call Decho("3: @@<".@@.">")
141 let s:sessionfile= tempname()
142 " call Decho("4: @@<".@@.">")
145 " call Decho("save session")
146 let ssop_keep = &ssop
147 let &ssop = 'blank,help,winsize,folds,globals,localoptions,options'
148 " call Decho("5: @@<".@@.">")
149 exe 'mksession! '.fnameescape(s:sessionfile)
150 " call Decho("6: @@<".@@.">")
165 exe "keepalt keepmarks new! ".fnameescape(s:sessionfile)
166 catch /^Vim\%((\a\+)\)\=:E/
167 echoerr "Too many windows"
168 silent! call delete(s:sessionfile)
170 " call Dret("ZoomWin#ZoomWin : too many windows")
173 silent! keepjumps keepmarks v/wincmd\|split\|resize/d
177 exe "new! ".fnameescape(s:sessionfile)
178 v/wincmd\|split\|resize/d
193 call histdel('search', -1)
194 let @/ = histget('search', -1)
195 " call Decho("7: @@<".@@.">")
197 " restore user's session options and restore event handling
198 " call Decho("restore user session options and event handling")
200 let &ssop = ssop_keep
202 " call Decho("8: @@<".@@.">")
205 " call Decho("9: @@<".@@.">")
208 " restore user option settings {{{3
209 " call Decho("restore user option settings")
210 let &hidden= keep_hidden
211 let &write = keep_write
213 if exists("keep_wmw")
219 " Re-open certain windows {{{3
220 call s:ZoomWinPreserve(1)
222 " call Dret("ZoomWin#ZoomWin")
225 " ---------------------------------------------------------------------
226 " SavePosn: this function sets up a savedposn variable that {{{2
227 " has the commands necessary to restore the view
228 " of the current window.
229 fun! s:SavePosn(savewinhoriz)
230 " call Dfunc("SavePosn(savewinhoriz=".a:savewinhoriz.") file<".expand("%").">")
231 let swline = line(".")
232 if swline == 1 && getline(1) == ""
234 let savedposn= "silent b ".winbufnr(0)
235 " call Dret("SavePosn savedposn<".savedposn.">")
239 let swwline = winline()-1
240 let swwcol = virtcol(".") - wincol()
241 let savedposn = "silent b ".winbufnr(0)."|".swline."|silent norm! z\<cr>"
243 let savedposn= savedposn.":silent norm! ".swwline."\<c-y>\<cr>:silent norm! zs\<cr>"
245 let savedposn= savedposn.":silent call cursor(".swline.",".swcol.")\<cr>"
249 let savedposn= savedposn.":silent norm! ".swwcol."zl\<cr>"
252 " handle certain special settings for the multi-window savedposn call
253 " bufhidden buftype buflisted
256 let settings="bh=".&bh
260 let settings= settings." nobl"
264 let settings= settings." bt=".&bt
268 let savedposn= savedposn.":setlocal ".settings."\<cr>"
272 " call Dret("SavePosn savedposn<".savedposn.">")
276 " ---------------------------------------------------------------------
277 " s:RestorePosn: this function restores noname and scratch windows {{{2
278 fun! s:RestorePosn(savedposn)
279 " call Dfunc("RestorePosn(savedposn<".a:savedposn.">) file<".expand("%").">")
287 " call Dret("RestorePosn")
290 " ---------------------------------------------------------------------
291 " CleanupSessionFile: if you exit Vim before cleaning up the {{{2
292 " supposed-to-be temporary session file
293 fun! ZoomWin#CleanupSessionFile()
294 " call Dfunc("ZoomWin#CleanupSessionFile()")
295 if exists("s:sessionfile") && filereadable(s:sessionfile)
296 " call Decho("sessionfile exists and is readable; deleting it")
297 silent! call delete(s:sessionfile)
300 " call Dret("ZoomWin#CleanupSessionFile")
303 " ---------------------------------------------------------------------
304 " GotoWinNum: this function puts cursor into specified window {{{2
305 fun! s:GotoWinNum(winnum)
306 " call Dfunc("GotoWinNum(winnum=".a:winnum.") winnr=".winnr())
307 if a:winnum != winnr()
308 exe a:winnum."wincmd w"
310 " call Dret("GotoWinNum")
314 " ---------------------------------------------------------------------
315 " ZoomWinPreserve: This function, largely written by David Fishburn, {{{2
316 " allows ZoomWin to "preserve" certain windows:
318 " TagList, by Yegappan Lakshmanan
319 " http://vim.sourceforge.net/scripts/script.php?script_id=273
321 " WinManager, by Srinath Avadhanula
322 " http://vim.sourceforge.net/scripts/script.php?script_id=95
324 " It does so by closing the associated window upon entry to ZoomWin
325 " and re-opening it upon exit by using commands provided by the
326 " utilities themselves.
327 fun! s:ZoomWinPreserve(open)
328 " call Dfunc("ZoomWinPreserve(open=".a:open.")")
333 if exists('g:zoomwin_preserve_taglist') && exists('g:loaded_taglist')
334 " If taglist window is open then close it.
335 let s:taglist_winnum = bufwinnr(g:TagList_title)
336 if s:taglist_winnum != -1
343 if exists('g:zoomwin_preserve_winmanager') && exists('g:loaded_winmanager')
344 " If the winmanager window is open then close it.
345 let s:is_winmgr_vis = IsWinManagerVisible()
346 if s:is_winmgr_vis == 1
354 if exists('g:zoomwin_preserve_taglist') && exists('g:loaded_taglist')
355 " If taglist window was open, open it again
356 if s:taglist_winnum != -1
362 if exists('g:zoomwin_preserve_winmanager') && exists('g:loaded_winmanager')
363 " If the winmanager window is open then close it.
364 if s:is_winmgr_vis == 1
370 " call Dret("ZoomWinPreserve")
373 " =====================================================================
378 " ---------------------------------------------------------------------
380 " vim: ts=4 fdm=marker