Easytags -> bundle (Careful: Notes is now depending on it!)
[my-vim-dotfolder.git] / PACKAGES / tabbar.vim
blobb914734bca42f530f3a198dae5f63256b987112b
1 "   Copyright: Copyright (C) 2005 Marius Groleo
2 "              Permission is hereby granted to use and distribute this code,
3 "              with or without modifications, provided that  this  copyright
4 "              notice is copied with it. Like anything else that's free,
5 "              tabbar.vim is provided *AS IS* and comes with no warranty of
6 "              any kind, either expressed or implied. In no event will the
7 "              copyright holder be liable for any damamges resulting from the
8 "              use of this software.
10 "              Derived from  Bindu Wavell miniBufExplorer.vim version 6.3.2
13 "   Version:      0.7
14 "   Maintainer:   Marius Groleo < groleo@gmail.com >
15 "   Description:  TabBar buffer explorer Vim Plugin
16 "   Name Of File: tabbar.vim
18 "   DOCUMENTATION: is at line :1445
20 " press zR , in normal mode to OPEN  all folds
21 " press zM , in normal mode to CLOSE all folds
23 " Already been loaded? ~~
24 if exists('Tb_loaded')
25     finish
26 else
27       let Tb_loaded= 1
28 endif "%%
31 " Debug Level ~~
32 "   0 = no logging
33 " 1-5 = errors ; 1 is the most important
34 " 5-9 = info ; 5 is the most important
35 "  10 = Entry/Exit
36 if !exists('g:Tb_DBG_LVL')
37       let g:Tb_DBG_LVL = 0
38 endif" %%
41 " Logging method ~~
42 " 0 = log to a window
43 " 1 = log with vim's echo facility
44 " 2 = log to a file named TabBar.DBG
45 "     in the directory where vim was started
46 "     THIS IS VERY SLOW
47 " 3 = log into g:Tb_DbgOutput
48 "     global variable [This is the default]
49 if !exists('g:Tb_DebugMode')
50       let g:Tb_DebugMode = 0
51 endif" %%
54 " Mappings and Commands
55 " TabBar Keyboard Mappings ~~
56 if ! hasmapto('\e1') || !hasmapto('<M-1>')
57       "gui bindings containing META key, are different from terminal bindings
58       if has('gui_running')
59             "NORMAL mode bindings for gvim
60             noremap <unique> <script> <M-1> :call <SID>Bf_SwitchTo( 1)<CR>:<BS>
61             noremap <unique> <script> <M-2> :call <SID>Bf_SwitchTo( 2)<CR>:<BS>
62             noremap <unique> <script> <M-3> :call <SID>Bf_SwitchTo( 3)<CR>:<BS>
63             noremap <unique> <script> <M-4> :call <SID>Bf_SwitchTo( 4)<CR>:<BS>
64             noremap <unique> <script> <M-5> :call <SID>Bf_SwitchTo( 5)<CR>:<BS>
65             noremap <unique> <script> <M-6> :call <SID>Bf_SwitchTo( 6)<CR>:<BS>
66             noremap <unique> <script> <M-7> :call <SID>Bf_SwitchTo( 7)<CR>:<BS>
67             noremap <unique> <script> <M-8> :call <SID>Bf_SwitchTo( 8)<CR>:<BS>
68             noremap <unique> <script> <M-9> :call <SID>Bf_SwitchTo( 9)<CR>:<BS>
69             noremap <unique> <script> <M-0> :call <SID>Bf_SwitchTo( 10)<CR>:<BS>
70             "INSERT mode bindings for gvim
71             inoremap <unique> <script> <M-1> <esc>:call <SID>Bf_SwitchTo( 1)<CR>:<BS>a
72             inoremap <unique> <script> <M-2> <esc>:call <SID>Bf_SwitchTo( 2)<CR>:<BS>a
73             inoremap <unique> <script> <M-3> <esc>:call <SID>Bf_SwitchTo( 3)<CR>:<BS>a
74             inoremap <unique> <script> <M-4> <esc>:call <SID>Bf_SwitchTo( 4)<CR>:<BS>a
75             inoremap <unique> <script> <M-5> <esc>:call <SID>Bf_SwitchTo( 5)<CR>:<BS>a
76             inoremap <unique> <script> <M-6> <esc>:call <SID>Bf_SwitchTo( 6)<CR>:<BS>a
77             inoremap <unique> <script> <M-7> <esc>:call <SID>Bf_SwitchTo( 7)<CR>:<BS>a
78             inoremap <unique> <script> <M-8> <esc>:call <SID>Bf_SwitchTo( 8)<CR>:<BS>a
79             inoremap <unique> <script> <M-9> <esc>:call <SID>Bf_SwitchTo( 9)<CR>:<BS>a
80             inoremap <unique> <script> <M-0> <esc>:call <SID>Bf_SwitchTo( 10)<CR>:<BS>a
81       else
82             "NORMAL mode bindings for vim( dos32 )
83             noremap <unique> <script> ± :call <SID>Bf_SwitchTo( 1)<CR>:<BS>
84             noremap <unique> <script> ² :call <SID>Bf_SwitchTo( 2)<CR>:<BS>
85             noremap <unique> <script> ³ :call <SID>Bf_SwitchTo( 3)<CR>:<BS>
86             noremap <unique> <script> ´ :call <SID>Bf_SwitchTo( 4)<CR>:<BS>
87             noremap <unique> <script> µ :call <SID>Bf_SwitchTo( 5)<CR>:<BS>
88             noremap <unique> <script> ¶ :call <SID>Bf_SwitchTo( 6)<CR>:<BS>
89             noremap <unique> <script> · :call <SID>Bf_SwitchTo( 7)<CR>:<BS>
90             noremap <unique> <script> ¸ :call <SID>Bf_SwitchTo( 8)<CR>:<BS>
91       "else
92             "NORMAL mode bindings for vim( terminal)
93             noremap <unique> <script> \e1 :call <SID>Bf_SwitchTo( 1)<CR>:<BS>
94             noremap <unique> <script> \e2 :call <SID>Bf_SwitchTo( 2)<CR>:<BS>
95             noremap <unique> <script> \e3 :call <SID>Bf_SwitchTo( 3)<CR>:<BS>
96             noremap <unique> <script> \e4 :call <SID>Bf_SwitchTo( 4)<CR>:<BS>
97             noremap <unique> <script> \e5 :call <SID>Bf_SwitchTo( 5)<CR>:<BS>
98             noremap <unique> <script> \e6 :call <SID>Bf_SwitchTo( 6)<CR>:<BS>
99             noremap <unique> <script> \e7 :call <SID>Bf_SwitchTo( 7)<CR>:<BS>
100             noremap <unique> <script> \e8 :call <SID>Bf_SwitchTo( 8)<CR>:<BS>
101             noremap <unique> <script> \e9 :call <SID>Bf_SwitchTo( 9)<CR>:<BS>
102             noremap <unique> <script> \e0 :call <SID>Bf_SwitchTo( 10)<CR>:<BS>
103             "INSERT mode bindings for vim( terminal)
104             inoremap <unique> <script> \e1 <esc>:call <SID>Bf_SwitchTo( 1)<CR>:<BS>a
105             inoremap <unique> <script> \e2 <esc>:call <SID>Bf_SwitchTo( 2)<CR>:<BS>a
106             inoremap <unique> <script> \e3 <esc>:call <SID>Bf_SwitchTo( 3)<CR>:<BS>a
107             inoremap <unique> <script> \e4 <esc>:call <SID>Bf_SwitchTo( 4)<CR>:<BS>a
108             inoremap <unique> <script> \e5 <esc>:call <SID>Bf_SwitchTo( 5)<CR>:<BS>a
109             inoremap <unique> <script> \e6 <esc>:call <SID>Bf_SwitchTo( 6)<CR>:<BS>a
110             inoremap <unique> <script> \e7 <esc>:call <SID>Bf_SwitchTo( 7)<CR>:<BS>a
111             inoremap <unique> <script> \e8 <esc>:call <SID>Bf_SwitchTo( 8)<CR>:<BS>a
112             inoremap <unique> <script> \e9 <esc>:call <SID>Bf_SwitchTo( 9)<CR>:<BS>a
113             inoremap <unique> <script> \e0 <esc>:call <SID>Bf_SwitchTo( 10)<CR>:<BS>a
114       endif
115 endif " %%
118 " TabBar <Script> internal map ~~
119 noremap <unique> <script> <Plug>tbstart  :call <SID>Tb_Start(1, -1)<CR>:<BS>
120 noremap <unique> <script> <Plug>tbstop   :call <SID>Tb_Stop(1)<CR>:<BS>
121 noremap <unique> <script> <Plug>tbaut    :call <SID>Tb_Aup(-1)<CR>:<BS>
122 noremap <unique> <script> <Plug>tbtoggle :call <SID>Tb_Toggle()<CR>:<BS>
123 " %%
126 " TabBar commands ~~
127 if !exists(':TbStart')
128       command! TbStart  call <SID>Tb_Start(1, -1)
129 endif
131 if !exists(':TbStop')
132       command! TbStop  call <SID>Tb_Stop(1)
133 endif
135 if !exists(':TbAup')
136       command! TbAup  call <SID>Tb_AutoUpdt(-1)
137 endif
139 if !exists(':TbToggle')
140       command! TbToggle  call <SID>Tb_Toggle()
141 endif
143 if !exists(':Tbbn')
144       command! Tbbn call <SID>Bf_Cycle(1)
145 endif
146 if !exists(':Tbp')
147       command! Tbbp call <SID>Bf_Cycle(0)
148 endif " %%
152 " Global Configuration Variables
153 " Allow auto update? ~~
154 " We start out with this off for startup, but once vim is running we
155 " turn this on.
156 if !exists('g:Tb_AutoUpdt')
157       let g:Tb_AutoUpdt = 0
158 endif " %%
161 " MoreThanOne? ~~
162 " Display Mini Buf Explorer when there are 'More Than One' eligible buffers
163 if !exists('g:Tb_MoreThanOne')
164       let g:Tb_MoreThanOne = 2
165 endif" %%
168 " Split below/above/left/right? ~~
169 " When opening a new -TabBar- window, split the new windows below or
170 " above the current window?  1 = below, 0 = above.
171 if !exists('g:Tb_SplitBelow')
172       let g:Tb_SplitBelow = &splitbelow
173 endif" %%
176 " Horizontal or Vertical explorer? ~~
177 " For folks that like vertical explorers, I'm caving in and providing for
178 " veritcal splits. If this is set to 0 then the current horizontal
179 " splitting logic will be run. If however you want a vertical split,
180 " assign the width (in characters) you wish to assign to the -TabBar- window.
181 if !exists('g:Tb_VSplit')
182       let g:Tb_VSplit = 0
183 endif" %%
186 " TabWrap? ~~
187 " By default line wrap is used (possibly breaking a tab name between two
188 " lines.) Turning this option on (setting it to 1) can take more screen
189 " space, but will make sure that each tab is on one and only one line.
190 if !exists('g:Tb_TabWrap')
191     let g:Tb_TabWrap = 0
192 endif" %%
195 " Switch buffers using Ctrl-Tab ?~~
196 if !exists('g:Tb_cTabSwitchBufs')
197     let g:Tb_cTabSwitchBufs = 1
198 endif" %%
201 " if cTabSwitchBufs is turned on then we turn off cTabSwitchWindows.~~
202 if g:Tb_cTabSwitchBufs == 1 || !exists('g:Tb_cTabSwitchWindows')
203       let g:Tb_cTabSwitchWindows = 0
204 endif" %%
207 " If we have enabled <C-TAB> and <C-S-TAB> to switch buffers~~
208 " in the current window then perform the remapping
209 if g:Tb_cTabSwitchBufs
210     noremap <C-TAB>   :call <SID>Bf_Cycle(1)<CR>:<BS>
211     noremap <C-S-TAB> :call <SID>Bf_Cycle(0)<CR>:<BS>
212 endif "%%
215 " If we have enabled <C-TAB> and <C-S-TAB> to switch windows ~~
216 " then perform the remapping
217 if g:Tb_cTabSwitchWindows
218     noremap <C-TAB>   <C-W>w
219     noremap <C-S-TAB> <C-W>W
220 endif "%%
223 " Modifiable Select Target ~~
224 if !exists('g:Tb_ModSelTarget')
225       let g:Tb_ModSelTarget = 0
226 endif "%%
229 " Force Syntax Enable ~~
230 if !exists('g:Tb_ForceSyntaxEnable')
231       let g:Tb_ForceSyntaxEnable = 0
232 endif "%%
235 " Single/Double Click? ~~
236 " flag that can be set to 1 in a users .vimrc to allow
237 " single click switching of tabs. By default we use
238 " double click for tab selection.
239 if !exists('g:Tb_UseSingleClick')
240       let g:Tb_UseSingleClick = 0
241 endif
244 " attempt to perform single click mapping, it would be much
245 " nicer if we could nnoremap <buffer> ... however vim does
246 " not fire the <buffer> <leftmouse> when you use the mouse
247 " to enter a buffer.
249 if g:Tb_UseSingleClick == 1
250     let s:clickmap = ':if bufname("%") == "-TabBar-" <bar> call <SID>Tb_Click() <bar> endif <CR>'
251     if maparg('<LEFTMOUSE>', 'n') == ''
252         " no mapping for leftmouse
253         exec ':nnoremap <silent> <LEFTMOUSE> <LEFTMOUSE>' . s:clickmap
254     else
255         " we have a mapping
256         let  g:Tb_DoneClickSave = 1
257         let  s:m = ':nnoremap <silent> <LEFTMOUSE> <LEFTMOUSE>'
258         let  s:m = s:m . substitute(substitute(maparg('<LEFTMOUSE>', 'n'), '|', '<bar>', 'g'), '\c^<LEFTMOUSE>', '', '')
259         let  s:m = s:m . s:clickmap
260         exec s:m
261     endif
262 endif " %%
266 "------------"
267 " Variables  "
268 "------------"
269 " used to pass maxTabWidth info between functions
270 let s:maxTabWidth = 0
273 " Debug line counter
274 let s:DBG_LN_CNT = 0
277 " Global used to store the buffer list so we don't update the ~~
278 " UI unless the list has changed.
279 if !exists('g:Tb_VimBufList')
280     let g:Tb_VimBufList = ''
281     let g:Tb_BufferMap=''
282 endif "%%
285 " g:Tb_UpdtMutex: Variable used as a mutex so that we don't do lots~~
286 " of Tb_AutoUpdts at the same time.
287 if !exists('g:Tb_UpdtMutex')
288     let g:Tb_UpdtMutex = 0
289 endif "%%
292 " g:Tb_MaxHeight maxSize ~~
293 if !exists('g:Tb_MaxSize')
294       let g:Tb_MaxSize = 1
295       "TODO implement Tb_MaxHeight
296 endif "%%
299 " g:Tb_MaxHeight MinSize ~~
300 if !exists('g:Tb_MinSize')
301       let g:Tb_MinSize = 1
302       "TODO implement Tb_MaxHeight
303 endif "%%
306 " g:Tb_DbgOutput: In debug mode 3 this variable will hold the debug output~~
307 if !exists('g:Tb_DbgOutput')
308       let g:Tb_DbgOutput = ''
309 endif "%%
312 " g:Tb_ForceDisplay ~~
313 if !exists('g:Tb_ForceDisplay')
314       let g:Tb_ForceDisplay = 0
315 endif "%%
318 " Setup an autocommand group and some autocommands ~~
319 " that keep our explorer updated automatically.
320 augroup TabBar
321 autocmd TabBar BufDelete   * call <SID>DEBUG('-=> BufDelete AutoCmd', 10) |call <SID>Tb_AutoUpdt(expand('<abuf>'))
322 autocmd TabBar BufEnter    * call <SID>DEBUG('-=> BufEnter  AutoCmd', 10) |call <SID>Tb_AutoUpdt(-1)
323 autocmd TabBar VimEnter    * call <SID>DEBUG('-=> VimEnter  AutoCmd', 10) |let g:Tb_AutoUpdt = 1 |call <SID>Tb_AutoUpdt(-1)
324 " %%
327 "------------"
328 " Functions  "
329 "------------"
330 " Tb_Start - Sets up our explorer and causes it to be displayed ~~
331 function! <SID>Tb_Start(sticky, delBufNum)
332     if g:Tb_DBG_LVL > 0
333         call <SID>DEBUG('ENTER: Tb_Start()'   ,10)
334     endif
336     if a:sticky == 1
337         let g:Tb_AutoUpdt = 1
338     endif
340     " Store the current buffer
341     let l:curBuf = bufnr('%')
343     " Prevent a report of our actions from showing up.
344     let l:save_rep = &report
345     let l:save_sc  = &showcmd
346     let &report    = 10000
347     set noshowcmd
349     call <SID>Win_FindOrCreate('-TabBar-', -1, 1)
351     " Make sure we are in our window
352     if bufname('%') != '-TabBar-'
353         call <SID>DEBUG('Tb_Start: called in invalid window',1)
354         let &report  = l:save_rep
355         let &showcmd = l:save_sc
356         return
357     endif
359     " !!! We may want to make the following optional -- Bindu
360     " New windows don't cause all windows to be resized to equal sizes
361     set noequalalways
362     " !!! We may want to make the following optional -- Bindu
363     " We don't want the mouse to change focus without a click
364     set nomousefocus
366     " If folks turn numbering and columns on by default we will turn
367     " them off for the -TabBar- window
368     setlocal foldcolumn=0
369     setlocal nonumber
371     if has("syntax")
372         syn clear
373         syn match Tb_Normal             '\[[^\]]*\]'
374         syn match Tb_Changed            '\[[^\]]*\]+'
375         syn match Tb_VisibleNormal      '\[[^\]]*\]\*+\='
376         syn match Tb_VisibleChanged     '\[[^\]]*\]\*+'
378         if !exists("g:did_tabbar_syntax_inits")
379             let g:did_tabbar_syntax_inits = 1
380             highlight def link Tb_Normal         Comment
381             highlight def link Tb_Changed        String
382             highlight def link Tb_VisibleNormal  StatusLineNC
383             highlight def link Tb_VisibleChanged Special
384         endif
385     endif
388     " If you press return in the -TabBar- then try
389     " to open the selected buffer in the previous window.
390     " Bf_CrSel = Buffer CR Select
391     nnoremap <buffer> <CR> :call <SID>Bf_CrSel()<CR>:<BS>
394     " If you Bf_DblClkSel in the -TabBar- then try
395     " to open the selected buffer in the previous window.
396     nnoremap <buffer> <2-LEFTMOUSE> :call <SID>Bf_DblClkSel()<CR>:<BS>
399     " delete the selected buffer.
400     nnoremap <buffer> d :call <SID>Bf_DelWithD()<CR>:<BS>
403     " If you press w in the -TabBar- then switch back
404     " to the previous window.
405     nnoremap <buffer> p :wincmd p<CR>:<BS>
407     " The following allows for quicker moving between buffer
408     " names in the -TabBar- window it also saves the last-pattern
409     " and restores it.
410     nnoremap <buffer> <TAB>   :call search('\[[0-9]*:[^\]]*\]')<CR>:<BS>
411     nnoremap <buffer> <S-TAB> :call search('\[[0-9]*:[^\]]*\]','b')<CR>:<BS>
413     call <SID>Bf_SafePrint(a:delBufNum)
415     if (l:curBuf != -1)
416         call search('\['.l:curBuf.':'.expand('#'.l:curBuf.':t').'\]')
417     else
418         call <SID>DEBUG('Tb_Start: No current buffer to search for',9)
419     endif
421     let &report  = l:save_rep
422     let &showcmd = l:save_sc
424     if g:Tb_DBG_LVL > 0
425         call <SID>DEBUG('EXIT : Tb_Start()'  ,10)
426     endif
427 endfunction " %%
430 " Tb_Stop - Looks for our explorer and closes the window if it is opened ~~
431 function! <SID>Tb_Stop( sticky)
432     if g:Tb_DBG_LVL > 0
433         call <SID>DEBUG('ENTER: Tb_Stop()'    ,10)
434     endif
436     if a:sticky == 1
437         let g:Tb_AutoUpdt = 0
438     endif
440     let l:winNum = <SID>Win_Find('-TabBar-')
442     if l:winNum != -1
443         exec l:winNum.' wincmd w'
444         silent! close
445         wincmd p
446     endif
448     if g:Tb_DBG_LVL > 0
449         call <SID>DEBUG('EXIT : Tb_Stop()'   ,10)
450     endif
451 endfunction " %%
454 " Tb_Toggle - Looks for our explorer and opens/closes the window ~~
455 function! <SID>Tb_Toggle()
456     if g:Tb_DBG_LVL > 0
457         call <SID>DEBUG('ENTER: Tb_Toggle()'  ,10)
458     endif
460     let g:Tb_AutoUpdt = 0
462     let l:winNum = <SID>Win_Find('-TabBar-')
464     if l:winNum != -1
465         call <SID>Tb_Stop(1)
466     else
467         call <SID>Tb_Start(1, -1)
468         wincmd p
469     endif
471     if g:Tb_DBG_LVL > 0
472         call <SID>DEBUG('EXIT : Tb_Toggle()' ,10)
473     endif
474 endfunction " %%
477 " Tb_Max - Returns the max of two numbers ~~
478 function! <SID>Tb_Max(argOne, argTwo)
479     if a:argOne > a:argTwo
480         return a:argOne
481     else
482         return a:argTwo
483     endif
484 endfunction " %%
487 " Tb_AutoUpdt - Function called by auto commands for auto updating -TabBar- ~~
488 "     IF auto update is turned on     AND
489 "     we are in a real buffer         AND
490 "     we have enough eligible buffers THEN
491 "     Update our explorer and get back to the current window
492 " If we get a buffer number for a buffer that
493 " is being deleted, we need to make sure and
494 " remove the buffer from the list of eligible
495 " buffers in case we are down to one eligible
496 " buffer, in which case we will want to close
497 " the -TabBar- window.
498 function! <SID>Tb_AutoUpdt(delBufNum)
499     if g:Tb_DBG_LVL > 0
500         call <SID>DEBUG('ENTER: Tb_AutoUpdt( delBufNum='.a:delBufNum.') : bufnr(%)='.bufnr('%').' : bufname(%)=['.bufname('%') . ']',10)
501     endif
503     if (g:Tb_UpdtMutex == 1)
504         if g:Tb_DBG_LVL > 0
505               call <SID>DEBUG('Tb_AutoUpdt: recursion stopped',9)
506               call <SID>DEBUG('EXIT : Tb_AutoUpdt()'    ,10)
507         endif
508         return
509     else
510         let g:Tb_UpdtMutex = 1
511     endif
513     " Don't update the TabBar window
514     if (bufname('%') == '-TabBar-')
515         " If this is the only buffer left then toggle the buffer
516         if (winbufnr(2) == -1)
517             call <SID>Bf_Cycle(1)
518             if g:Tb_DBG_LVL > 0
519                 call <SID>DEBUG('Tb_AutoUpdt: does not run for cycled windows', 9)
520             endif
521         else
522             if g:Tb_DBG_LVL > 0
523                 call <SID>DEBUG('Tb_AutoUpdt: does not run for the -TabBar- window', 9)
524             endif
525         endif
527         if g:Tb_DBG_LVL > 0
528             call <SID>DEBUG('EXIT : Tb_AutoUpdt()'    ,10)
529         endif
531         let g:Tb_UpdtMutex = 0
532         return
533     endif
535     if (a:delBufNum != -1)
536         if g:Tb_DBG_LVL > 0
537             call <SID>DEBUG('Tb_AutoUpdt: will make sure that buffer '.a:delBufNum.' is not included in the buffer list.', 5)
538         endif
539     endif
541   " Only allow updates when the Tb_AutoUpdt flag is set
542   " this allows us to stop updates on startup.
543     if g:Tb_AutoUpdt == 1
544         " Only show TabBar if we have a real buffer
545         if ((g:Tb_MoreThanOne == 0) || (bufnr('%') != -1 && bufname('%') != ""))
546             if <SID>Bf_Eligible(a:delBufNum) == 1
547                 " if we don't have a window then create one
548                 let l:bufnr = <SID>Win_Find('-TabBar-')
549                 if (l:bufnr == -1)
550                         if g:Tb_DBG_LVL > 0
551                             call <SID>DEBUG('Tb_AutoUpdt: About to call Tb_Start (Create -TabBar-)', 9)
552                         endif
553                         call <SID>Tb_Start(0, a:delBufNum)
554                 else
555                     " otherwise only update the window if the contents have changed
556                     let l:ListChanged = <SID>Bf_BuildList(a:delBufNum, 0)
557                     if (l:ListChanged)
558                         if g:Tb_DBG_LVL > 0
559                             call <SID>DEBUG('Tb_AutoUpdt: About to call Tb_Start (Update -TabBar-)', 9)
560                         endif
561                         call <SID>Tb_Start(0, a:delBufNum)
562                     endif
563                 endif
565                 " go back to the working buffer
566                 if (bufname('%') == '-TabBar-')
567                     wincmd p
568                 endif
569             else
570                 if g:Tb_DBG_LVL > 0
571                     call <SID>DEBUG('Tb_AutoUpdt: Failed in eligible check', 9)
572                 endif
573                 call <SID>Tb_Stop(0)
574             endif
576             " VIM sometimes turns syntax highlighting off,
577             " we can force it on, but this may cause weird
578             " behavior so this is an optional hack to force
579             " syntax back on when we enter a buffer
580             if g:Tb_ForceSyntaxEnable
581                 if g:Tb_DBG_LVL > 0
582                     call <SID>DEBUG('Tb_AutoUpdt: Enable Syntax', 9)
583                 endif
584                 exec 'syntax enable'
585             endif
586         else
587             if g:Tb_DBG_LVL > 0
588                 call <SID>DEBUG('Tb_AutoUpdt: No buffers loaded...',9)
589             endif
590         endif
591     else
592         if g:Tb_DBG_LVL > 0
593             call <SID>DEBUG('EXIT : Tb_AutoUpdt are turned off',9)
594         endif
595     endif
597     if g:Tb_DBG_LVL > 0
598         call <SID>DEBUG('EXIT : Tb_AutoUpdt()'     ,10)
599     endif
601 let g:Tb_UpdtMutex = 0
602 endfunction " %%
605 " Tb_Click - Handle mouse double click ~~
606 function! <SID>Tb_Click()
607     if g:Tb_DBG_LVL > 0
608         call <SID>DEBUG('ENTER: Tb_Click()',10)
609     endif
610     call <SID>Bf_CrSel()
611     if g:Tb_DBG_LVL > 0
612         call <SID>DEBUG('EXIT : Tb_Click()',10)
613     endif
614 endfunction " %%
618 "-------------------"
619 " Window operations "
620 "-------------------"
621 " Win_Find - Return the window number of a named buffer ~~
622 " If none is found then returns -1.
623 function! <SID>Win_Find(bufName)
624     "if g:Tb_DBG_LVL > 0
625     "    call <SID>DEBUG('ENTER: Win_Find()',10)
626     "endif
628     " Try to find an existing window that contains
629     " our buffer.
630     let l:bufNr = bufnr(a:bufName)
631     if l:bufNr != -1
632         "if g:Tb_DBG_LVL > 0
633         "    call <SID>DEBUG('Found buffer ('.a:bufName.'): '.l:bufNr,9)
634         "endif
635         let l:bufWinNr = bufwinnr(l:bufNr)
636     else
637         let l:bufWinNr = -1
638     endif
639     return l:bufWinNr
640 endfunction " %%
643 " Win_FindOrCreate - Attempts to find a window for a named buffer. ~~
645 " If it is found then moves there. Otherwise creates a new window and
646 " configures it and moves there.
648 " forceEdge, -1 use defaults, 0 below, 1 above
649 " isExplorer, 0 no, 1 yes
650 " 0 no, 1 yes
651 function! <SID>Win_FindOrCreate(bufName, forceEdge, isExplorer)
652   "if g:Tb_DBG_LVL > 0
653   "  call <SID>DEBUG('ENTER: Win_FindOrCreate('.a:bufName.')',10)
654   "endif
656   " Save the user's split setting.
657   let l:saveSplitBelow = &splitbelow
659   " Set to our new values.
660   let &splitbelow = g:Tb_SplitBelow
662   " Try to find an existing explorer window
663   let l:winNum = <SID>Win_Find(a:bufName)
665   " If found goto the existing window, otherwise
666   " split open a new window.
667   if l:winNum != -1
668 "    if g:Tb_DBG_LVL > 0
669 "      call <SID>DEBUG('Found window ('.a:bufName.'): '.l:winNum,9)
670 "    endif
671     exec l:winNum.' wincmd w'
672     let l:winFound = 1
673   else
675         "if g:Tb_SplitToEdge == 1 || a:forceEdge >= 0
676         if a:forceEdge >= 0
678             let l:edge = &splitbelow
679             if a:forceEdge >= 0
680                 let l:edge = a:forceEdge
681             endif
683             if l:edge
684                 if g:Tb_VSplit == 0
685                 exec 'bo sp '.a:bufName
686                 else
687                 exec 'bo vsp '.a:bufName
688                 endif
689             else
690                 if g:Tb_VSplit == 0
691                 exec 'to sp '.a:bufName
692                 else
693                 exec 'to vsp '.a:bufName
694                 endif
695             endif
696         else
697             if g:Tb_VSplit == 0
698             exec 'sp '.a:bufName
699             else
700             " &splitbelow doesn't affect vertical splits
701             " so we have to do this explicitly.. ugh.
702             if &splitbelow
703                 exec 'rightb vsp '.a:bufName
704             else
705                 exec 'vsp '.a:bufName
706             endif
707             endif
708         endif
710     let g:Tb_ForceDisplay = 1
712     " Try to find an existing explorer window
713     let l:winNum = <SID>Win_Find(a:bufName)
714     if l:winNum != -1
715       "if g:Tb_DBG_LVL > 0
716       "  call <SID>DEBUG('Created and then found window ('.a:bufName.'): '.l:winNum,9)
717       "endif
718       exec l:winNum.' wincmd w'
719     else
720       "if g:Tb_DBG_LVL > 0
721       "  call <SID>DEBUG('Win_FindOrCreate failed to create window ('.a:bufName.').',1)
722       "endif
723       return
724     endif
726     if a:isExplorer
727       " Turn off the swapfile, set the buffer type so that it won't get written,
728       " and so that it will get deleted when it gets hidden and turn on word wrap.
729       setlocal noswapfile
730       setlocal buftype=nofile
731       setlocal bufhidden=delete
732       if g:Tb_VSplit == 0
733         setlocal wrap
734       else
735         setlocal nowrap
736         exec('setlocal winwidth='.g:Tb_MinSize)
737       endif
738     endif
740     "if g:Tb_DBG_LVL > 0
741     "  call <SID>DEBUG('Window ('.a:bufName.') created: '.winnr(),9)
742     "endif
744   endif
746   " Restore the user's split setting.
747   let &splitbelow = l:saveSplitBelow
749 endfunction " %%
752 " Win_Resize - Set width/height of -TabBar- window ~~
754 " Makes sure we are in our explorer, then sets the height/width for our explorer
755 " window so that we can fit all of our information without taking extra lines.
756 function! <SID>Win_Resize()
757     if g:Tb_DBG_LVL > 0
758         call <SID>DEBUG('ENTER: Win_Resize()',10)
759     endif
761   " Make sure we are in our window
762   if bufname('%') != '-TabBar-'
763     call <SID>DEBUG('EXIT : Win_Resize called in invalid window',1)
764     return
765   endif
767   let l:width  = winwidth('.')
769   " Horizontal Resize
770   if g:Tb_VSplit == 0
772     if g:Tb_TabWrap == 0
773       let l:length = strlen(getline('.'))
774       let l:height = 0
775       if (l:width == 0)
776         let l:height = winheight('.')
777       else
778         let l:height = (l:length / l:width)
779         " handle truncation from div
780         if (l:length % l:width) != 0
781           let l:height = l:height + 1
782         endif
783       endif
784     else
785       exec("setlocal textwidth=".l:width)
786       normal gg
787       normal gq}
788       normal G
789       let l:height = line('.')
790       normal gg
791     endif
793     " enforce max window height
794     if g:Tb_MaxSize != 0
795       if g:Tb_MaxSize < l:height
796         let l:height = g:Tb_MaxSize
797       endif
798     endif
800     " enfore min window height
801     if l:height < g:Tb_MinSize || l:height == 0
802       let l:height = g:Tb_MinSize
803     endif
805     if g:Tb_DBG_LVL > 0
806         call <SID>DEBUG('EXIT : Win_Resize to '.l:height.' lines',9)
807     endif
808     exec('resize '.l:height)
810   " Vertical Resize
811   else
813     if g:Tb_MaxSize != 0
814       let l:newWidth = s:maxTabWidth
815       if l:newWidth > g:Tb_MaxSize
816           let l:newWidth = g:Tb_MaxSize
817       endif
818       if l:newWidth < g:Tb_MinSize
819           let l:newWidth = g:Tb_MinSize
820       endif
821     else
822       let l:newWidth = g:Tb_VSplit
823     endif
825     if l:width != l:newWidth
826         if g:Tb_DBG_LVL > 0
827             call <SID>DEBUG('EXIT : Win_Resize to '.l:newWidth.' columns',9)
828         endif
829       exec('vertical resize '.l:newWidth)
830     endif
832   endif
834 endfunction " %%
838 "-------------------"
839 " Buffer operations "
840 "-------------------"
841 " Bf_Choosed - From the -TabBar- window, return the bufnum for buf under cursor ~~
842 " If we are in our explorer window then return the buffer number
843 " for the buffer under the cursor.
844 function! <SID>Bf_Choosed()
845     if g:Tb_DBG_LVL > 0
846         call <SID>DEBUG('ENTER: Bf_Choosed()',10)
847     endif
849     " Make sure we are in our window
850     if bufname('%') != '-TabBar-'
851         if g:Tb_DBG_LVL > 0
852             call <SID>DEBUG('EXIT : Bf_Choosed: called in invalid window',1)
853         endif
854         return -1
855     endif
857     let l:save_reg = @"
858     let @" = ""
859     normal ""yi[
860     if @" != ""
861         let l:retv = substitute(@",'\([0-9]*\):.*', '\1', '') + 0
862         let @" = l:save_reg
863         if g:Tb_DBG_LVL > 0
864             call <SID>DEBUG('EXIT : Bf_Choosed: l:retv='.l:retv,5)
865         endif
866         return l:retv
867     else
868         let @" = l:save_reg
869         if g:Tb_DBG_LVL > 0
870             call <SID>DEBUG('EXIT : Bf_Choosed: return -1',1)
871         endif
872         return -1
873     endif
874 endfunction " %%
877 " Bf_DelWithD - From the -TabBar- window, delete selected buffer from list ~~
878 " After making sure that we are in our explorer, This will delete the buffer
879 " under the cursor. If the buffer under the cursor is being displayed in a
880 " window, this routine will attempt to get different buffers into the
881 " windows that will be affected so that windows don't get removed.
882 function! <SID>Bf_DelWithD()
883     if g:Tb_DBG_LVL > 0
884         call <SID>DEBUG('ENTER: Bf_DelWithD() g:Tb_VimBufList =['. g:Tb_VimBufList.'] g:Tb_BufferMap=['.g:Tb_BufferMap.']',10)
885     endif
887     " Make sure we are in our window
888     if bufname('%') != '-TabBar-'
889         if g:Tb_DBG_LVL > 0
890             call <SID>DEBUG('EXIT : Bf_DelWithD not called in -TabBar-',1)
891         endif
892         return
893     endif
895     let l:selected_buf  =  <SID>Bf_Choosed()
896     let l:selected_buf  =  <SID>Map_Get_key( l:selected_buf )
897     if g:Tb_DBG_LVL > 0
898         call <SID>DEBUG('Bf_DelWithD: l:selected_buf=['.l:selected_buf.']',5)
899     endif
900     let l:selected_name = bufname(l:selected_buf +0 )
901     if g:Tb_DBG_LVL > 0
902         call <SID>DEBUG('Bf_DelWithD: l:selBufName=['. l:selected_name.']',5)
903     endif
905     let l:curLine    = line('.')
906     let l:curCol     = virtcol('.')
907     "make the filename an option
908     if l:selected_name == 'TabBar.DBG' && g:Tb_DBG_LVL > 0
909         if g:Tb_DBG_LVL > 0
910             call <SID>DEBUG('EXIT : Bf_DelWithD will not delete the debug window.',1)
911         endif
912         return
913     endif
915     let l:save_rep = &report
916     let l:save_sc  = &showcmd
917     let &report    = 10000
918     set noshowcmd
921     if l:selected_buf != -1 && l:selected_name != ""
923         " Don't want auto updates while we are processing a delete
924         " request.
925         let l:saveTb_AutoUpdt = g:Tb_AutoUpdt
926         let g:Tb_AutoUpdt = 0
928         " Save previous window so that if we show a buffer after
929         " deleting. The show will come up in the correct window.
930         wincmd p
931         let l:prevWin    = winnr()
932         let l:prevWinBuf = winbufnr(winnr())
934         if g:Tb_DBG_LVL > 0
935             call <SID>DEBUG('Bf_DelWithD: l:prevWin='.l:prevWin.' buffer in window: '.l:prevWinBuf,5)
936             call <SID>DEBUG('Bf_DelWithD: l:selected_name=<'.l:selected_name.'>: l:selected_buf=['.l:selected_buf.']',5)
937         endif
939         " If buffer is being displayed in a window then
940         " move window to a different buffer before
941         " deleting this one.
942         let l:winNum = (bufwinnr(l:selected_name) + 0)
943         " while we have windows that contain our buffer
944         while l:winNum != -1
945             if g:Tb_DBG_LVL > 0
946                 call <SID>DEBUG('Bf_DelWithD: l:selected_buf='.l:selected_buf.' is being displayed in window: l:winNum='.l:winNum,5)
947             endif
949             " move to window that contains our selected buffer
950             exec l:winNum.' wincmd w'
952             if g:Tb_DBG_LVL > 0
953                 call <SID>DEBUG('Bf_DelWithD: We are now in window: '.winnr().' which contains buffer: '.bufnr('%').' and should contain buffer: '.l:selected_buf,5)
954             endif
956             let l:origBuf = bufnr('%')
957             call <SID>Bf_Cycle(1)
958             let l:curBuf  = bufnr('%')
960             if g:Tb_DBG_LVL > 0
961                 call <SID>DEBUG('Bf_DelWithD: Window now contains buffer: '.bufnr('%').' which should not be: '.l:selected_buf,5)
962             endif
964             if l:origBuf == l:curBuf
965                 " we wrapped so we are going to have to delete a buffer
966                 " that is in an open window.
967                 let l:winNum = -1
968             else
969                 " see if we have anymore windows with our selected buffer
970                 let l:winNum = (bufwinnr(l:selected_name) + 0)
971             endif
972         endwhile
974         " Attempt to restore previous window
975         if g:Tb_DBG_LVL > 0
976             call <SID>DEBUG('Bf_DelWithD: Restoring previous window to: '.l:prevWin,5)
977         endif
978         exec l:prevWin.' wincmd w'
980         " Try to get back to the -TabBar- window
981         let l:winNum = bufwinnr(bufnr('-TabBar-'))
982         if l:winNum != -1
983             exec l:winNum.' wincmd w'
984             if g:Tb_DBG_LVL > 0
985                 call <SID>DEBUG('Bf_DelWithD: Got to -TabBar- window: '.winnr(),5)
986             endif
987         else
988             if g:Tb_DBG_LVL > 0
989                 call <SID>DEBUG('Bf_DelWithD: Unable to get to -TabBar- window',1)
990             endif
991         endif
993         " Delete the buffer selected.
994         if g:Tb_DBG_LVL > 0
995             call <SID>DEBUG('Bf_DelWithD: About to delete buffer: '.l:selected_buf,5)
996         endif
997         " [displayed_buffer]-- [real_buffer] list
998         "let l:vimBuf = <SID>Map_Get_key( l:selected_buf )
999         exec('silent! bd '.l:selected_buf )
1001         let g:Tb_AutoUpdt = l:saveTb_AutoUpdt
1002         if g:Tb_DBG_LVL > 0
1003             call <SID>DEBUG('Bf_DelWithD: current window=: '.bufname('%'),5)
1004         endif
1005         call <SID>Bf_SafePrint(1)
1006         call cursor(l:curLine, l:curCol)
1007     endif
1009     let &report  = l:save_rep
1010     let &showcmd = l:save_sc
1012     if g:Tb_DBG_LVL > 0
1013         call <SID>DEBUG('EXIT : Bf_DelWithD() g:Tb_VimBufList =['. g:Tb_VimBufList.'] g:Tb_BufferMap=['.g:Tb_BufferMap.']',10)
1014     endif
1015 endfunction " %%
1018 " Bf_SafePrint - Wrapper for getting -TabBar- window shown ~~
1020 " Makes sure we are in our explorer, then erases the current buffer and turns
1021 " it into a mini buffer explorer window.
1022 function! <SID>Bf_SafePrint(delBufNum)
1023     if g:Tb_DBG_LVL > 0
1024         call <SID>DEBUG('ENTER: Bf_SafePrint()',10)
1025     endif
1027     " Make sure we are in our window
1028     if bufname('%') != '-TabBar-'
1029         call <SID>DEBUG('EXIT : Bf_SafePrint not called in -TabBar-',1)
1030         return
1031     endif
1033     " We need to be able to modify the buffer
1034     setlocal modifiable
1036     call <SID>Bf_PrintList(a:delBufNum)
1037     call <SID>Win_Resize()
1039     normal! zz
1041     " Prevent the buffer from being modified.
1042     setlocal nomodifiable
1043     set nobuflisted
1044     if g:Tb_DBG_LVL > 0
1045         call <SID>DEBUG('EXIT : Bf_SafePrint()',10)
1046     endif
1047 endfunction " %%
1050 " Bf_PrintList - Clear current buffer and put the -TabBar- text into it ~~
1051 " Makes sure we are in our explorer, then adds a list of all modifiable
1052 " buffers to the current buffer. Special marks are added for buffers that
1053 " are in one or more windows (*) and buffers that have been modified (+)
1054 function! <SID>Bf_PrintList(delBufNum)
1055     if g:Tb_DBG_LVL > 0
1056         call <SID>DEBUG('ENTER: Bf_PrintList()',10)
1057     endif
1059     let l:ListChanged = <SID>Bf_BuildList(a:delBufNum, 1)
1061     if (l:ListChanged == 1 || g:Tb_ForceDisplay)
1062         let l:save_rep = &report
1063         let l:save_sc = &showcmd
1064         let &report = 10000
1065         set noshowcmd
1067         " Delete all lines in buffer.
1068         1,$d _
1070         " Goto the end of the buffer put the buffer list
1071         " and then delete the extra trailing blank line
1072         $
1073         put! =g:Tb_VimBufList
1074         $ d _
1075         let g:Tb_ForceDisplay = 0
1077         let &report  = l:save_rep
1078         let &showcmd = l:save_sc
1079     else
1080         if g:Tb_DBG_LVL > 0
1081             call <SID>DEBUG('Buffer list not update since there was no change',9)
1082         endif
1083     endif
1084     if g:Tb_DBG_LVL > 0
1085         call <SID>DEBUG('EXIT : Bf_PrintList()',10)
1086     endif
1087 endfunction " %%
1090 " Bf_BuildList - Build the text for the -TabBar- window ~~
1091 " Creates the buffer list string and returns 1 if it is different than
1092 " last time this was called and 0 otherwise.
1093 function! <SID>Bf_BuildList(delBufNum, updateBufList)
1094     if g:Tb_DBG_LVL > 0
1095         call <SID>DEBUG('ENTER: Bf_BuildList()',10)
1096     endif
1098     let l:NBuffers = bufnr('$')     " Get the number of the last buffer.
1099     let l:i = 0                     " Set the buffer index to zero.
1100     let l:y = 0                     " Displayed buffers: more sugestive
1101     let l:fileNames = ''
1102     let l:maxTabWidth = 0
1103     call <SID>Map_Clear()
1105     " Loop through every buffer less than the total number of buffers.
1106     while( l:i <= l:NBuffers)
1107             let l:i = l:i + 1
1109         " If we have a delBufNum and it is the current
1110         " buffer then ignore the current buffer.
1111         " Otherwise, continue.
1112         if (a:delBufNum == -1 || l:i != a:delBufNum)
1113             " Make sure the buffer in question is listed.
1114             if(getbufvar(l:i, '&buflisted') == 1)
1115                 " Get the name of the buffer.
1116                 let l:BufName = bufname(l:i)
1117                 " Check to see if the buffer is a blank or not. If the buffer does have
1118                 " a name, process it.
1119                 if(strlen(l:BufName))
1120                     " Only show modifiable buffers (The idea is that we don't
1121                     " want to show Explorers)
1122                     if (getbufvar(l:i, '&modifiable') == 1 && BufName != '-TabBar-')
1123                         " Get filename & Remove []'s & ()'s
1124                         let l:shortBufName = fnamemodify(l:BufName, ":t")
1125                         let l:shortBufName = substitute(l:shortBufName, '[][()]', '', 'g')
1126                         let l:y =l:y +1
1127                         let g:Tb_BufferMap=g:Tb_BufferMap . l:y . "-" . l:i . "\r"
1128                         let l:tab = '['.l:y.':'.l:shortBufName." ]"
1130                         " If the buffer is open in a window mark it
1131                         if bufwinnr(l:i) != -1
1132                             let l:tab = "[".l:y.':'.l:shortBufName."]*"
1133                         endif
1135                         " If the buffer is modified then mark it
1136                         if(getbufvar(l:i, '&modified') == 1)
1137                             let l:tab = l:tab . '+'
1138                         endif
1139                         let l:maxTabWidth = <SID>Tb_Max(strlen(l:tab), l:maxTabWidth)
1140                         let l:fileNames = l:fileNames.l:tab
1141                         " If horizontal and tab wrap is turned on we need to add spaces
1142                         if g:Tb_VSplit == 0
1143                             if g:Tb_TabWrap != 0
1144                                 let l:fileNames = l:fileNames.' '
1145                             endif
1146                             " If not horizontal we need a newline
1147                         else
1148                             let l:fileNames = l:fileNames . "\n"
1149                         endif
1150                     endif
1151                 endif
1152             endif
1153         endif
1154     endwhile
1156     if (g:Tb_VimBufList != l:fileNames)
1157         if (a:updateBufList)
1158             let g:Tb_VimBufList = l:fileNames
1159             let s:maxTabWidth = l:maxTabWidth
1160         endif
1161         if g:Tb_DBG_LVL > 0
1162             call <SID>DEBUG('EXIT : Bf_BuildList: Bf_List has changed',10)
1163         endif
1164         return 1
1165     else
1166         if g:Tb_DBG_LVL > 0
1167             call <SID>DEBUG('EXIT : Bf_BuildList: no changes',10)
1168         endif
1169         return 0
1170     endif
1171 endfunction " %%
1174 " Bf_Eligible - Are there enough -TabBar- eligible buffers to open the -TabBar- window? ~~
1175 " Returns 1 if there are any buffers that can be displayed in a
1176 " mini buffer explorer. Otherwise returns 0. If delBufNum is
1177 " any non -1 value then don't include that buffer in the list
1178 " of eligible buffers.
1179 function! <SID>Bf_Eligible(delBufNum)
1180     if g:Tb_DBG_LVL > 0
1181         call <SID>DEBUG('ENTER: Bf_Eligible()',10)
1182     endif
1184   let l:save_rep = &report
1185   let l:save_sc = &showcmd
1186   let &report = 10000
1187   set noshowcmd
1189   let l:NBuffers = bufnr('$')     " Get the number of the last buffer.
1190   let l:i        = 0              " Set the buffer index to zero.
1191   let l:found    = 0              " No buffer found
1193   if (g:Tb_MoreThanOne > 1)
1194     if g:Tb_DBG_LVL > 0
1195         call <SID>DEBUG('Bf_Eligible : More Than One mode turned on',6)
1196     endif
1197   endif
1198   let l:needed = g:Tb_MoreThanOne
1200   " Loop through every buffer less than the total number of buffers.
1201   while(l:i <= l:NBuffers && l:found < l:needed)
1202     let l:i = l:i + 1
1204     " If we have a delBufNum and it is the current
1205     " buffer then ignore the current buffer.
1206     " Otherwise, continue.
1207     if (a:delBufNum == -1 || l:i != a:delBufNum)
1208       " Make sure the buffer in question is listed.
1209       if (getbufvar(l:i, '&buflisted') == 1)
1210         " Get the name of the buffer.
1211         let l:BufName = bufname(l:i)
1212         " Check to see if the buffer is a blank or not. If the buffer does have
1213         " a name, process it.
1214         if (strlen(l:BufName))
1215           " Only show modifiable buffers (The idea is that we don't
1216           " want to show Explorers)
1217           if ((getbufvar(l:i, '&modifiable') == 1) && (BufName != '-TabBar-'))
1219               let l:found = l:found + 1
1221           endif
1222         endif
1223       endif
1224     endif
1225   endwhile
1227   let &report  = l:save_rep
1228   let &showcmd = l:save_sc
1230     if g:Tb_DBG_LVL > 0
1231         call <SID>DEBUG('EXIT : Bf_Eligible '.l:found.' eligible buffers of '.l:needed.' needed',6)
1232     endif
1233     return (l:found >= l:needed)
1234 endfunction " %%
1237 " Bf_SwitchTo      Switch to bufNum( parameter) buffer~~
1238 function! <SID>Bf_SwitchTo( bufNum)
1240     let l:vimbuf = <SID>Map_Get_key( a:bufNum )
1241     exec "b!" . l:vimbuf
1242 endfunction " %%
1245 " Bf_CrSel - From the -TabBar- window, open buffer under the cursor ~~
1246 " If we are in our explorer, then we attempt to open the buffer under the
1247 " cursor in the previous window.
1248 function! <SID>Bf_CrSel()
1249     if g:Tb_DBG_LVL > 0
1250         call <SID>DEBUG('ENTER: Bf_CrSel()' ,10)
1251     endif
1253   " Make sure we are in our window
1254   if bufname('%') != '-TabBar-'
1255     if g:Tb_DBG_LVL > 0
1256         call <SID>DEBUG('EXIT : Bf_CrSel : called in invalid window',1)
1257     endif
1258     return
1259   endif
1261   let l:save_rep = &report
1262   let l:save_sc  = &showcmd
1263   let &report    = 10000
1264   set noshowcmd
1266   let l:bufnr  = <SID>Bf_Choosed()
1267   let l:resize = 0
1269   if(l:bufnr != -1)             " If the buffer exists.
1271     let l:saveTb_AutoUpdt = g:Tb_AutoUpdt
1272     let g:Tb_AutoUpdt = 0
1273     " Switch to the previous window
1274     wincmd p
1276     " If we are in the buffer explorer or in a nonmodifiable buffer with
1277     " g:Tb_ModSelTarget set then try another window (a few times)
1278     if bufname('%') == '-TabBar-' || (g:Tb_ModSelTarget == 1 && getbufvar(bufnr('%'), '&modifiable') == 0)
1279       wincmd w
1280       if bufname('%') == '-TabBar-' || (g:Tb_ModSelTarget == 1 && getbufvar(bufnr('%'), '&modifiable') == 0)
1281         wincmd w
1282         if bufname('%') == '-TabBar-' || (g:Tb_ModSelTarget == 1 && getbufvar(bufnr('%'), '&modifiable') == 0)
1283           wincmd w
1284           " The following handles the case where -TabBar-
1285           " is the only window left. We need to resize so we don't
1286           " end up with a 1 or two line buffer.
1287           if bufname('%') == '-TabBar-'
1288             let l:resize = 1
1289           endif
1290         endif
1291       endif
1292     endif
1294     "exec('b! '.l:bufnr)
1295     call <SID>Bf_SwitchTo( l:bufnr)
1296     if (l:resize)
1297       resize
1298     endif
1299     let g:Tb_AutoUpdt = l:saveTb_AutoUpdt
1300     call <SID>Tb_AutoUpdt(-1)
1302   endif
1304   let &report  = l:save_rep
1305   let &showcmd = l:save_sc
1307     if g:Tb_DBG_LVL > 0
1308         call <SID>DEBUG('EXIT : Bf_CrSel()',10)
1309     endif
1310 endfunction " %%
1313 " Bf_DblClkSel - Double click with the mouse.~~
1314 function! <SID>Bf_DblClkSel()
1315     if g:Tb_DBG_LVL > 0
1316         call <SID>DEBUG('ENTER: Bf_DblClkSel()',10)
1317     endif
1318   call <SID>Bf_CrSel()
1319     if g:Tb_DBG_LVL > 0
1320         call <SID>DEBUG('EXIT : Bf_DblClkSel()',10)
1321     endif
1322 endfunction " %%
1325 " Bf_Cycle - Cycle Through Buffers ~~
1326 " Move to next or previous buffer in the current window. If there
1327 " are no more modifiable buffers then stay on the current buffer.
1328 " can be called with no parameters in which case the buffers are
1329 " cycled forward. Otherwise a single argument is accepted, if
1330 " it's 0 then the buffers are cycled backwards, otherwise they
1331 " are cycled forward.
1332 function! <SID>Bf_Cycle(forward)
1334     " The following hack handles the case where we only have one
1335     " window open and it is too small
1336     let l:saveTb_AutoUpdt = g:Tb_AutoUpdt
1337     if (winbufnr(2) == -1)
1338         resize
1339         let g:Tb_AutoUpdt = 0
1340     endif
1342     " Change buffer (keeping track of before and after buffers)
1343     let l:origBuf = bufnr('%')
1344     if (a:forward == 1)
1345         bn!
1346     else
1347         bp!
1348     endif
1349     let l:curBuf  = bufnr('%')
1351     " Skip any non-modifiable buffers, but don't cycle forever
1352     " This should stop us from stopping in any of the [Explorers]
1353     " FIXME: infite loop
1354     while getbufvar(l:curBuf, '&modifiable') == 0 && l:origBuf != l:curBuf
1355         if (a:forward == 1)
1356             bn!
1357         else
1358             bp!
1359         endif
1360         let l:curBuf = bufnr('%')
1361     endwhile
1363     let g:Tb_AutoUpdt = l:saveTb_AutoUpdt
1364     if (l:saveTb_AutoUpdt == 1)
1365         call <SID>Tb_AutoUpdt(-1)
1366     endif
1367 endfunction " %%
1369 " the format for a map is something like:
1370 " idx-key\r
1371 " idx: the number displayed in tabbar [idx:fooBar.txt]
1372 " key: the buffer number in which idx is hold
1373 function! <SID>Map_Get_key( idx )
1374     let l:i=matchstr( g:Tb_BufferMap, a:idx . "-[0-9]*\r" )
1375     let l:x=substitute (l:i , a:idx ."-", "","")
1376     let l:x=substitute (l:x , "\r", "","")
1377     return l:x
1378 endfunction
1380 function! <SID>Map_Get_idx( key )
1381     let l:i=matchstr( g:Tb_BufferMap, "[0-9]*-" , a:key."\r" )
1382     let l:x=substitute (l:i , "-".a:key, "","")
1383     let l:x=substitute (l:x , "\r", "","")
1384     return l:x
1385 endfunction
1388 " I know this is short, But it keeps the code clean
1389 function! <SID>Map_Clear()
1390     let g:Tb_BufferMap=''
1391 endfunction
1394 " DEBUG - Display debug output when debugging is turned on ~~
1395 " Thanks to Charles E. Campbell, Jr. PhD <cec@NgrOyphSon.gPsfAc.nMasa.gov>
1396 " for Decho.vim which was the inspiration for this enhanced debugging
1397 " capability.
1398 function! <SID>DEBUG(msg, level)
1400     if g:Tb_DBG_LVL >= a:level
1402         " Prevent a report of our actions from showing up.
1403         let l:save_rep    = &report
1404         let l:save_sc     = &showcmd
1405         let &report       = 10000
1406         set noshowcmd
1408         " Debug output to a buffer
1409         if g:Tb_DebugMode == 0
1410             " Save the current window number so we can come back here
1411             let l:prevWin     = winnr()
1412             wincmd p
1413             let l:prevPrevWin = winnr()
1414             wincmd p
1416             " Get into the debug window or create it if needed
1417             call <SID>Win_FindOrCreate('TabBar.DBG', 1, 0)
1419             " Make sure we really got to our window, if not we
1420             " will display a confirm dialog and turn debugging
1421             " off so that we won't break things even more.
1422             if bufname('%') != 'TabBar.DBG'
1423                 call confirm('ERROR in window debugging code. Dissabling TabBar debugging.', 'OK')
1424                 let g:Tb_DBG_LVL = 0
1425             endif
1426             " Write Message to DBG buffer
1427             let res=append("$",s:DBG_LN_CNT.':'.a:level.":\t".a:msg)
1428             norm G
1429             "set nomodified
1431             " Return to original window
1432             exec l:prevPrevWin.' wincmd w'
1433             exec l:prevWin.' wincmd w'
1434         " Debug output using VIM's echo facility
1435         elseif g:Tb_DebugMode == 1
1436         echo s:DBG_LN_CNT.':'.a:level.':'.a:msg
1437         " Debug output to a file -- VERY SLOW!!!
1438         " should be OK on UNIX and Win32 (not the 95/98 variants)
1439         elseif g:Tb_DebugMode == 2
1440             if has('system') || has('fork')
1441                 if has('win32') && !has('win95')
1442                     let l:result = system("cmd /c 'echo ".s:DBG_LN_CNT.':'.a:level.':'.a:msg." >> TabBar.DBG'")
1443                 endif
1444                 if has('unix')
1445                     let l:result = system("echo '".s:DBG_LN_CNT.':'.a:level.':'.a:msg." >> TabBar.DBG'")
1446                 endif
1447             else
1448                 call confirm('Error in file writing version of the debugging code, vim not compiled with system or fork. Dissabling TabBar debugging.', 'OK')
1449                 let g:Tb_DBG_LVL = 0
1450             endif
1451         elseif g:Tb_DebugMode == 3
1452             let g:Tb_DbgOutput = g:Tb_DbgOutput."\n".s:DBG_LN_CNT.':'.a:level.':'.a:msg
1453         endif
1454         let s:DBG_LN_CNT = s:DBG_LN_CNT + 1
1456         let &report  = l:save_rep
1457         let &showcmd = l:save_sc
1459     endif
1460 endfunc " %%
1463 "     Documentation~~
1465 "     :TbStart .......... Open and/or goto Explorer
1466 "     :TbStop  .......... Close the Explorer if it's open
1467 "     :TbAup   .......... Update Explorer without navigating
1468 "     :TbToggle ......... Toggle Tabbar
1469 "``````````````````````````````````````````````````````````````````
1472 "     let g:Tb_SplitBelow=0     " Put new window above current or on the  left
1473                                 " for vertical split
1474 "     let g:Tb_SplitBelow=1     " Put new window below current or on the  right
1475                                 " for vertical split
1476 "     By default we are now forcing the Tabbar window to open up at the edge of
1477 "     the screen.
1478 "     You can turn this off by setting the following variable in .vimrc:
1480 "           let g:Tb_SplitToEdge = 0
1482 "  o  IN HORIZONTAL MODE
1483 "     You can set the max height by setting this in .vimrc:
1485 "           let g:Tb_MaxSize = <max lines: default 0>
1487 "     Setting this to 0 will mean the window gets as big as
1488 "     needed to fit all your buffers.
1490 "   o  NOTE
1491 "   You can set the min height by
1492 "   letting the following variable in your .vimrc:
1494 "       let g:Tb_MinSize = <min height: default 1>
1496 "   o  IN VERTICAL MODE
1497 "   By default the vertical explorer has a fixed width. If you put:
1499 "       let g:Tb_MaxSize = <max width: default 0>
1501 "   into your .vimrc then tabbar will attempt to set the width of the
1502 "   tabbar window to be as wide as your widest tab. The width will not
1503 "   exceed Tb_MaxSize even if you have wider tabs.
1505 "   Accepting the default value of 0 for this will give you a fixed
1506 "   width tabbar window.
1508 "   --    You can specify a MinSize for the vertical explorer window by
1509 "   putting the following in your .vimrc:
1511 "         let g:Tb_MinSize = <min width: default 1>
1513 "   This will have no effect unless you also specivy Tb_MaxSize.
1514 "````````````````````````````````````````````````````````````````
1517 "   --  This stops the -TabBar- from opening
1518 "   automatically until more than one eligible buffer is available.
1519 "   You can turn this feature off by setting the following variable
1520 "   in your .vimrc:
1521 "      Setting this to 0 will cause the TabBar window to be loaded even
1522 "   if no buffers are available. Setting it to 1 causes the TabBar
1523 "   window to be loaded as soon as an eligible buffer is read. You
1524 "   can also set it to larger numbers. So if you set it to 4 for
1525 "   example the TabBar window wouldn't auto-open until 4 eligibles
1526 "   buffers had been loaded. This is nice for folks that don't
1527 "   want an TabBar window unless they are editing more than two or
1528 "   three buffers.
1530 "       let g:Tb_MoreThanOne=1
1531 "``````````````````````````````````````````````````````````````````
1534 "   --  To enable the optional mapping of Control + Vim Direction Keys
1535 "   [hjkl] to window movement commands, you can put the following into
1536 "   your .vimrc:
1538 "       let g:Tb_MapWindowNavVim = 1
1540 "   To enable the optional mapping of Control + Arrow Keys to window
1541 "   movement commands, you can put the following into your .vimrc:
1543 "       let g:Tb_MapWindowNavArrows = 1
1544 "``````````````````````````````````````````````````````````````````
1547 "   --  To enable the optional mapping of <C-TAB> and <C-S-TAB> to a
1548 "   function that will bring up the next or previous buffer in the
1549 "   current window, you can put the following into your .vimrc:
1551 "       let g:Tb_MapCTabSwitchBufs = 1
1552 "````````````````````````````````````````````````````````````````
1555 "   --  To enable the optional mapping of <C-TAB> and <C-S-TAB> to mappings
1556 "   that will move to the next and previous (respectively) window, you
1557 "   can put the following into your .vimrc:
1559 "       let g:Tb_MapCTabSwitchWindows = 1
1562 "   o  NOTE
1563 "   If you set TabSwitchBufs AND ...TabSwitchWindows,
1564 "           TabSwitchBufs will *BE* enabled and
1565 "           TabSwitchWindows will be *NOT*.
1566 "``````````````````````````````````````````````````````````````````
1569 "   --  If you would like to single click on tabs rather than double
1570 "   clicking on them to goto the selected buffer.
1572 "       let g:Tb_UseSingleClick = 1
1573 "````````````````````````````````````````````````````````````````
1576 "   --  It is possible to customize the the highlighting for the tabs in
1577 "   the TabBar by configuring the following highlighting groups:
1579 "      Tb_Normal .............  for buffers that have NOT CHANGED and
1580 "                               are NOT VISIBLE.
1581 "      Tb_Changed ............. for buffers that HAVE CHANGED and are
1582 "                               NOT VISIBLE
1583 "      Tb_VisibleNormal ....... buffers that have NOT CHANGED and are
1584 "                               VISIBLE
1585 "      Tb_VisibleChanged ...... buffers that have CHANGED and are VISIBLE
1587 "   You can either link to an existing highlighting group by
1588 "   adding a command like:
1590 "       hi link Tb_VisibleChanged Error
1592 "   to your .vimrc or you can specify exact foreground and background
1593 "   colors using the following syntax:
1595 "       hi Tb_Changed guibg=darkblue ctermbg=darkblue termbg=white
1597 "   o  NOTE
1598 "   If you set a colorscheme in your .vimrc you should do it
1599 "       BEFORE updating the TabBar highlighting groups.
1601 "   If you use other explorers like TagList you can put:
1603 "       let g:Tb_ModSelTarget = 1
1605 "   into your .vimrc in order to force TabBar to try to place selected
1606 "   buffers into a window that does not have a nonmodifiable buffer.
1607 "   The upshot of this should be that if you go into TabBar and select
1608 "   a buffer, the buffer should not show up in a window that is
1609 "   hosting an explorer.
1611 "       let g:Tb_ForceSyntaxEnable = 1
1612 "````````````````````````````````````````````````````````````````
1615 "   --  TabBar has a basic debugging capability.
1616 "       let g:Tb_DebugLevel = 0     " TabBar serious errors output
1617 "       let g:Tb_DebugLevel = 4     " TabBar all errors output
1618 "       let g:Tb_DebugLevel = 10    " TabBar reports everything
1620 "   You can also set a DebugMode to cause output to be target as
1621 "   follows (default is mode 3):
1623 "       let g:Tb_DebugMode  = 0     " Errors will show up in a vim window
1624 "       let g:Tb_DebugMode  = 1     " Uses VIM's echo function to display on
1625 "                                   " the screen
1626 "       let g:Tb_DebugMode  = 2     " Writes to a file TabBarDBG.vim
1627 "       let g:Tb_DebugMode  = 3     " Store output in variable g:Tb_DbgOutput
1629 "   Or if you are able to start VIM, you might just perform these
1630 "   at a command prompt right before you do the operation that is
1631 "   failing.
1632 "````````````````````````````````````````````````````````````````
1633 " %%
1635 " BUGFIX:
1636 " 0.7. Removed mapping to <tt>, to avoid delayed response to <<
1637 " 0.6. Fixed the "delete with d" bug.
1639 " TODO: 0.  When the tabbar is full, try to scroll it
1640 "       1.  See what is goind on with the error "Not enough space"
1641 "       2.  Indent code
1642 "       3.  Optimize code
1643 "       4.  Better Document
1644 "       5.  noremap in vimrc to <C-TAB> to update the buffer list
1645 "       6.  noremap in vimrc to <?> to switch to -TabBar-
1646 "vim:foldmethod=marker vim:foldmarker=~~,%%