Load matchit plugin where available.
[profile.git] / .vim / plugin / ZoomWin.vim
1 " ZoomWin:      Brief-like ability to zoom into/out-of a window
2 " Author:       Charles Campbell
3 "                       original version by Ron Aaron
4 " Date:         Apr 24, 2008
5 " Version:      23a     ASTRO-ONLY
6 " History: see :help zoomwin-history {{{1
7 " GetLatestVimScripts: 508 1 :AutoInstall: ZoomWin.vim
8
9 " ---------------------------------------------------------------------
10 " Load Once: {{{1
11 if &cp || exists("g:loaded_ZoomWin")
12  finish
13 endif
14 let s:keepcpo        = &cpo
15 let g:loaded_ZoomWin = "v23a"
16 set cpo&vim
17 "DechoTabOn
18
19 " ---------------------------------------------------------------------
20 "  Public Interface: {{{1
21 if !hasmapto("<Plug>ZoomWin")
22  nmap <unique> <c-w>o  <Plug>ZoomWin
23 endif
24 nnoremap <silent> <script> <Plug>ZoomWin :set lz<CR>:silent call ZoomWin()<CR>:set nolz<CR>
25 com! ZoomWin :set lz|silent call ZoomWin()|set nolz
26
27 au VimLeave * call <SID>CleanupSessionFile()
28
29 " ---------------------------------------------------------------------
30 " ZoomWin: toggles between a single-window and a multi-window layout {{{1
31 "          The original version was by Ron Aaron.
32 fun! ZoomWin()
33 "  let g:decho_hide= 1          "Decho
34 "  call Dfunc("ZoomWin() winbufnr(2)=".winbufnr(2))
35
36   " if the vim doesn't have +mksession, only a partial zoom is available {{{2
37   if !has("mksession")
38    if !exists("s:partialzoom")
39     echomsg "missing the +mksession feature; only a partial zoom is available"
40         let s:partialzoom= 0
41    endif
42    if v:version < 630
43         echoerr "***sorry*** you need an updated vim, preferably with +mksession"
44    elseif s:partialzoom
45         " partial zoom out
46         let s:partialzoom= 0
47         exe s:winrestore
48    else
49         " partial zoom in
50         let s:partialzoom= 1
51         let s:winrestore = winrestcmd()
52         res
53    endif
54 "  call Dret("ZoomWin : partialzoom=".s:partialzoom)
55    return
56   endif
57
58   " Close certain windows {{{2
59   call s:ZoomWinPreserve(0)
60
61   " save options.  Force window minimum height/width to be >= 1 {{{2
62   let keep_hidden = &hidden
63   let keep_write  = &write
64
65   if v:version < 603
66    if &wmh == 0 || &wmw == 0
67     let keep_wmh = &wmh
68     let keep_wmw = &wmw
69     silent! set wmh=1 wmw=1
70    endif
71   endif
72   set hidden write
73
74   if winbufnr(2) == -1
75     " there's only one window - restore to multiple-windows mode {{{2
76 "       call Decho("there's only one window - restore to multiple windows")
77
78     if exists("s:sessionfile") && filereadable(s:sessionfile)
79           " save position in current one-window-only
80 "         call Decho("save position in current one-window-only in sponly")
81       let sponly     = s:SavePosn(0)
82       let s:origline = line(".")
83       let s:origcol  = virtcol(".")
84
85       " source session file to restore window layout
86           let ei_keep= &ei
87           set ei=all
88       exe 'silent! so '.s:sessionfile
89 "         Decho("@@<".@@.">")
90       let v:this_session= s:sesskeep
91
92       if exists("s:savedposn1")
93         " restore windows' positioning and buffers
94 "               call Decho("restore windows, positions, buffers")
95         windo call s:RestorePosn(s:savedposn{winnr()})|unlet s:savedposn{winnr()}
96         call s:GotoWinNum(s:winkeep)
97         unlet s:winkeep
98       endif
99
100           if line(".") != s:origline || virtcol(".") != s:origcol
101            " If the cursor hasn't moved from the original position,
102            " then let the position remain what it was in the original
103            " multi-window layout.
104 "          call Decho("restore position using sponly")
105        call s:RestorePosn(sponly)
106           endif
107
108           " delete session file and variable holding its name
109 "         call Decho("delete session file")
110       call delete(s:sessionfile)
111       unlet s:sessionfile
112           let &ei=ei_keep
113     endif
114
115   else " there's more than one window - go to only-one-window mode {{{2
116 "       call Decho("there's multiple windows - goto one-window-only")
117
118     let s:winkeep    = winnr()
119     let s:sesskeep   = v:this_session
120
121         " doesn't work with the command line window (normal mode q:)
122         if &bt == "nofile" && expand("%") == "command-line"
123          echoerr "***error*** ZoomWin doesn't work with the command line window"
124 "     call Dret("ZoomWin : commandline window error")
125          return
126         endif
127 "       call Decho("1: @@<".@@.">")
128
129         " disable all events (autocmds)
130 "       call Decho("disable events")
131     let ei_keep= &ei
132         set ei=all
133 "       call Decho("2: @@<".@@.">")
134
135     " save window positioning commands
136 "       call Decho("save window positioning commands")
137     windo let s:savedposn{winnr()}= s:SavePosn(1)
138     call s:GotoWinNum(s:winkeep)
139
140     " set up name of session file
141 "       call Decho("3: @@<".@@.">")
142     let s:sessionfile= tempname()
143 "       call Decho("4: @@<".@@.">")
144
145     " save session
146 "       call Decho("save session")
147     let ssop_keep = &ssop
148     let &ssop     = 'blank,help,winsize'
149 "       call Decho("5: @@<".@@.">")
150     exe 'mksession! '.s:sessionfile
151 "       call Decho("6: @@<".@@.">")
152         let keepyy= @@
153         let keepy0= @0
154         let keepy1= @1
155         let keepy2= @2
156         let keepy3= @3
157         let keepy4= @4
158         let keepy5= @5
159         let keepy6= @6
160         let keepy7= @7
161         let keepy8= @8
162         let keepy9= @9
163     set lz ei=all bh=
164         if v:version >= 700
165      exe "keepalt keepmarks new! ".s:sessionfile
166      silent! keepjumps keepmarks v/wincmd\|split\|resize/d
167      keepalt w!
168      keepalt bw!
169         else
170      exe "new! ".s:sessionfile
171      v/wincmd\|split\|resize/d
172      w!
173      bw!
174     endif
175         let @@= keepyy
176         let @0= keepy0
177         let @1= keepy1
178         let @2= keepy2
179         let @3= keepy3
180         let @4= keepy4
181         let @5= keepy5
182         let @6= keepy6
183         let @7= keepy7
184         let @8= keepy8
185         let @9= keepy9
186 "       call Decho("7: @@<".@@.">")
187
188     " restore user's session options and restore event handling
189 "       call Decho("restore user session options and event handling")
190     set nolz
191     let &ssop = ssop_keep
192     silent! only!
193 "       call Decho("8: @@<".@@.">")
194     let &ei   = ei_keep
195     echomsg expand("%")
196 "       call Decho("9: @@<".@@.">")
197   endif
198
199   " restore user option settings {{{2
200 "  call Decho("restore user option settings")
201   let &hidden= keep_hidden
202   let &write = keep_write
203   if v:version < 603
204    if exists("keep_wmw")
205     let &wmh= keep_wmh
206     let &wmw= keep_wmw
207    endif
208   endif
209
210   " Re-open certain windows {{{2
211   call s:ZoomWinPreserve(1)
212
213 "  call Dret("ZoomWin")
214 endfun
215
216 " ---------------------------------------------------------------------
217 " SavePosn: this function sets up a savedposn variable that {{{1
218 "          has the commands necessary to restore the view
219 "          of the current window.
220 fun! s:SavePosn(savewinhoriz)
221 "  call Dfunc("SavePosn(savewinhoriz=".a:savewinhoriz.") file<".expand("%").">")
222   let swline    = line(".")
223   if swline == 1 && getline(1) == ""
224    " empty buffer
225    let savedposn= "silent b ".winbufnr(0)
226 "   call Dret("SavePosn savedposn<".savedposn.">")
227    return savedposn
228   endif
229   let swcol     = col(".")
230   let swwline   = winline()-1
231   let swwcol    = virtcol(".") - wincol()
232   let savedposn = "silent b ".winbufnr(0)."|".swline."|silent norm! z\<cr>"
233   if swwline > 0
234    let savedposn= savedposn.":silent norm! ".swwline."\<c-y>\<cr>:silent norm! zs\<cr>"
235   endif
236   let savedposn= savedposn.":silent call cursor(".swline.",".swcol.")\<cr>"
237
238   if a:savewinhoriz
239    if swwcol > 0
240     let savedposn= savedposn.":silent norm! ".swwcol."zl\<cr>"
241    endif
242
243    " handle certain special settings for the multi-window savedposn call
244    "   bufhidden buftype buflisted
245    let settings= ""
246    if &bh != ""
247         let settings="bh=".&bh
248         setlocal bh=hide
249    endif
250    if !&bl
251         let settings= settings." nobl"
252         setlocal bl
253    endif
254    if &bt != ""
255         let settings= settings." bt=".&bt
256         setlocal bt=
257    endif
258    if settings != ""
259         let savedposn= savedposn.":setlocal ".settings."\<cr>"
260    endif
261
262   endif
263 "  call Dret("SavePosn savedposn<".savedposn.">")
264   return savedposn
265 endfun
266
267 " ---------------------------------------------------------------------
268 " s:RestorePosn: this function restores noname and scratch windows {{{1
269 fun! s:RestorePosn(savedposn)
270 "  call Dfunc("RestorePosn(savedposn<".a:savedposn.">) file<".expand("%").">")
271   if &scb
272    setlocal noscb
273    exe a:savedposn
274    setlocal scb
275   else
276    exe a:savedposn
277   endif
278 "  call Dret("RestorePosn")
279 endfun
280
281 " ---------------------------------------------------------------------
282 " CleanupSessionFile: if you exit Vim before cleaning up the {{{1
283 "                     supposed-to-be temporary session file
284 fun! s:CleanupSessionFile()
285 "  call Dfunc("CleanupSessionFile()")
286   if exists("s:sessionfile") && filereadable(s:sessionfile)
287 "   call Decho("sessionfile exists and is readable; deleting it")
288    silent! call delete(s:sessionfile)
289    unlet s:sessionfile
290   endif
291 "  call Dret("CleanupSessionFile")
292 endfun
293
294 " ---------------------------------------------------------------------
295 " GotoWinNum: this function puts cursor into specified window {{{1
296 fun! s:GotoWinNum(winnum)
297 "  call Dfunc("GotoWinNum(winnum=".a:winnum.") winnr=".winnr())
298   if a:winnum != winnr()
299    exe a:winnum."wincmd w"
300   endif
301 "  call Dret("GotoWinNum")
302 endfun
303
304
305 " ---------------------------------------------------------------------
306 " ZoomWinPreserve:  This function, largely written by David Fishburn, {{{1
307 "   allows ZoomWin to "preserve" certain windows:
308 "
309 "       TagList, by Yegappan Lakshmanan
310 "         http://vim.sourceforge.net/scripts/script.php?script_id=273
311 "
312 "       WinManager, by Srinath Avadhanula
313 "         http://vim.sourceforge.net/scripts/script.php?script_id=95
314 "
315 "  It does so by closing the associated window upon entry to ZoomWin
316 "  and re-opening it upon exit by using commands provided by the
317 "  utilities themselves.
318 fun! s:ZoomWinPreserve(open)
319 "  call Dfunc("ZoomWinPreserve(open=".a:open.")")
320
321   if a:open == 0
322
323    " Close Taglist
324    if exists('g:zoomwin_preserve_taglist') && exists('g:loaded_taglist')
325        " If taglist window is open then close it.
326        let s:taglist_winnum = bufwinnr(g:TagList_title)
327        if s:taglist_winnum != -1
328            " Close the window
329            exec "silent! Tlist"
330        endif
331    endif
332
333    " Close Winmanager
334    if exists('g:zoomwin_preserve_winmanager') && exists('g:loaded_winmanager')
335        " If the winmanager window is open then close it.
336        let s:is_winmgr_vis = IsWinManagerVisible()
337        if s:is_winmgr_vis == 1
338            exec "WMClose"
339        endif
340    endif
341
342   else
343
344    " Re-open Taglist
345    if exists('g:zoomwin_preserve_taglist') && exists('g:loaded_taglist')
346        " If taglist window was open, open it again
347        if s:taglist_winnum != -1
348            exec "silent! Tlist"
349        endif
350    endif
351
352    " Re-Open Winmanager
353    if exists('g:zoomwin_preserve_winmanager') && exists('g:loaded_winmanager')
354        " If the winmanager window is open then close it.
355        if s:is_winmgr_vis == 1
356            exec "WManager"
357        endif
358    endif
359   endif
360
361 "  call Dret("ZoomWinPreserve")
362 endfun
363
364 let &cpo= s:keepcpo
365 unlet s:keepcpo
366 " ---------------------------------------------------------------------
367 "  Modelines: {{{1
368 " vim: ts=4 fdm=marker