bufexplorer.vim 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. "============================================================================
  2. " Copyright: Copyright (c) 2001-2017, Jeff Lanzarotta
  3. " All rights reserved.
  4. "
  5. " Redistribution and use in source and binary forms, with or
  6. " without modification, are permitted provided that the
  7. " following conditions are met:
  8. "
  9. " * Redistributions of source code must retain the above
  10. " copyright notice, this list of conditions and the following
  11. " disclaimer.
  12. "
  13. " * Redistributions in binary form must reproduce the above
  14. " copyright notice, this list of conditions and the following
  15. " disclaimer in the documentation and/or other materials
  16. " provided with the distribution.
  17. "
  18. " * Neither the name of the {organization} nor the names of its
  19. " contributors may be used to endorse or promote products
  20. " derived from this software without specific prior written
  21. " permission.
  22. "
  23. " THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  24. " CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  25. " INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. " MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. " DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  28. " CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. " SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  30. " NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. " LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. " HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. " CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  34. " OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  35. " EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. " Name Of File: bufexplorer.vim
  37. " Description: Buffer Explorer Vim Plugin
  38. " Maintainer: Jeff Lanzarotta (delux256-vim at outlook dot com)
  39. " Last Changed: Monday, 18 September 2017
  40. " Version: See g:bufexplorer_version for version number.
  41. " Usage: This file should reside in the plugin directory and be
  42. " automatically sourced.
  43. "
  44. " You may use the default keymappings of
  45. "
  46. " <Leader>be - Opens BufExplorer
  47. " <Leader>bt - Toggles BufExplorer open or closed
  48. " <Leader>bs - Opens horizontally split window BufExplorer
  49. " <Leader>bv - Opens vertically split window BufExplorer
  50. "
  51. " Or you can override the defaults and define your own mapping
  52. " in your vimrc file, for example:
  53. "
  54. " nnoremap <silent> <F11> :BufExplorer<CR>
  55. " nnoremap <silent> <s-F11> :ToggleBufExplorer<CR>
  56. " nnoremap <silent> <m-F11> :BufExplorerHorizontalSplit<CR>
  57. " nnoremap <silent> <c-F11> :BufExplorerVerticalSplit<CR>
  58. "
  59. " Or you can use
  60. "
  61. " ":BufExplorer" - Opens BufExplorer
  62. " ":ToggleBufExplorer" - Opens/Closes BufExplorer
  63. " ":BufExplorerHorizontalSplit" - Opens horizontally window BufExplorer
  64. " ":BufExplorerVerticalSplit" - Opens vertically split window BufExplorer
  65. "
  66. " For more help see supplied documentation.
  67. " History: See supplied documentation.
  68. "=============================================================================
  69. " Exit quickly if already running or when 'compatible' is set. {{{1
  70. if exists("g:bufexplorer_version") || &cp
  71. finish
  72. endif
  73. "1}}}
  74. " Version number
  75. let g:bufexplorer_version = "7.4.19"
  76. " Plugin Code {{{1
  77. " Check for Vim version {{{2
  78. if v:version < 700
  79. echohl WarningMsg
  80. echo "Sorry, bufexplorer ".g:bufexplorer_version." required Vim 7.0 or greater."
  81. echohl None
  82. finish
  83. endif
  84. " Check to see if the version of Vim has the correct patch applied, if not, do
  85. " not used <nowait>.
  86. if v:version > 703 || v:version == 703 && has('patch1261') && has('patch1264')
  87. " We are good to go.
  88. else
  89. echohl WarningMsg
  90. echo "Sorry, bufexplorer ".g:bufexplorer_version." required Vim 7.3 or greater with patch1261 and patch1264."
  91. echohl None
  92. finish
  93. endif
  94. " Create commands {{{2
  95. command! BufExplorer :call BufExplorer()
  96. command! ToggleBufExplorer :call ToggleBufExplorer()
  97. command! BufExplorerHorizontalSplit :call BufExplorerHorizontalSplit()
  98. command! BufExplorerVerticalSplit :call BufExplorerVerticalSplit()
  99. " Set {{{2
  100. function! s:Set(var, default)
  101. if !exists(a:var)
  102. if type(a:default)
  103. execute "let" a:var "=" string(a:default)
  104. else
  105. execute "let" a:var "=" a:default
  106. endif
  107. return 1
  108. endif
  109. return 0
  110. endfunction
  111. " Script variables {{{2
  112. let s:MRU_Exclude_List = ["[BufExplorer]","__MRU_Files__","[Buf\ List]"]
  113. let s:MRUList = []
  114. let s:name = '[BufExplorer]'
  115. let s:originBuffer = 0
  116. let s:running = 0
  117. let s:sort_by = ["number", "name", "fullpath", "mru", "extension"]
  118. let s:splitMode = ""
  119. let s:types = {"fullname": ':p', "path": ':p:h', "relativename": ':~:.', "relativepath": ':~:.:h', "shortname": ':t'}
  120. " Setup the autocommands that handle the MRUList and other stuff. {{{2
  121. autocmd VimEnter * call s:Setup()
  122. " Setup {{{2
  123. function! s:Setup()
  124. call s:Reset()
  125. " Now that the MRUList is created, add the other autocmds.
  126. augroup BufExplorer
  127. autocmd!
  128. autocmd BufEnter,BufNew * call s:ActivateBuffer()
  129. autocmd BufWipeOut * call s:DeactivateBuffer(1)
  130. autocmd BufDelete * call s:DeactivateBuffer(0)
  131. autocmd BufWinEnter \[BufExplorer\] call s:Initialize()
  132. autocmd BufWinLeave \[BufExplorer\] call s:Cleanup()
  133. augroup END
  134. endfunction
  135. " Reset {{{2
  136. function! s:Reset()
  137. " Build initial MRUList. This makes sure all the files specified on the
  138. " command line are picked up correctly.
  139. let s:MRUList = range(1, bufnr('$'))
  140. " Initialize the association of buffers to tabs for any buffers
  141. " that have been created prior to now, e.g., files specified as
  142. " vim command line arguments
  143. call s:CatalogBuffers()
  144. endfunction
  145. " CatalogBuffers {{{2
  146. " Create tab associations for any existing buffers
  147. function! s:CatalogBuffers()
  148. let ct = tabpagenr()
  149. for tab in range(1, tabpagenr('$'))
  150. silent execute 'normal! ' . tab . 'gt'
  151. for buf in tabpagebuflist()
  152. call s:UpdateTabBufData(buf)
  153. endfor
  154. endfor
  155. silent execute 'normal! ' . ct . 'gt'
  156. endfunction
  157. " AssociatedTab {{{2
  158. " Return the number of the tab associated with the specified buffer.
  159. " If the buffer is associated with more than one tab, the first one
  160. " found is returned. If the buffer is not associated with any tabs,
  161. " -1 is returned.
  162. function! s:AssociatedTab(bufnr)
  163. for tab in range(1, tabpagenr('$'))
  164. let list = gettabvar(tab, 'bufexp_buf_list', [])
  165. let idx = index(list, a:bufnr)
  166. if idx != -1
  167. return tab
  168. endif
  169. endfor
  170. return -1
  171. endfunction
  172. " RemoveBufFromOtherTabs {{{2
  173. " Remove the specified buffer from the buffer lists of all tabs
  174. " except the current tab.
  175. function! s:RemoveBufFromOtherTabs(bufnr)
  176. for tab in range(1, tabpagenr('$'))
  177. if tab == tabpagenr()
  178. continue
  179. endif
  180. let list = gettabvar(tab, 'bufexp_buf_list', [])
  181. let idx = index(list, a:bufnr)
  182. if idx == -1
  183. continue
  184. endif
  185. call remove(list, idx)
  186. call settabvar(tab, 'bufexp_buf_list', list)
  187. endfor
  188. endfunction
  189. " AddBufToCurrentTab {{{2
  190. " Add the specified buffer to the list of buffers associated
  191. " with the current tab
  192. function! s:AddBufToCurrentTab(bufnr)
  193. if index(t:bufexp_buf_list, a:bufnr) == -1
  194. call add(t:bufexp_buf_list, a:bufnr)
  195. endif
  196. endfunction
  197. " IsInCurrentTab {{{2
  198. " Returns whether the specified buffer is associated
  199. " with the current tab
  200. function! s:IsInCurrentTab(bufnr)
  201. " It shouldn't happen that the list of buffers is
  202. " not defined but if it does, play it safe and
  203. " include the buffer
  204. if !exists('t:bufexp_buf_list')
  205. return 1
  206. endif
  207. return (index(t:bufexp_buf_list, a:bufnr) != -1)
  208. endfunction
  209. " UpdateTabBufData {{{2
  210. " Update the tab buffer data for the specified buffer
  211. "
  212. " The current tab's list is updated. If a buffer is only
  213. " allowed to be associated with one tab, it is removed
  214. " from the lists of any other tabs with which it may have
  215. " been associated.
  216. "
  217. " The associations between tabs and buffers are maintained
  218. " in separate lists for each tab, which are stored in tab-
  219. " specific variables 't:bufexp_buf_list'.
  220. function! s:UpdateTabBufData(bufnr)
  221. " The first time we add a tab, Vim uses the current buffer
  222. " as its starting page even though we are about to edit a
  223. " new page, and another BufEnter for the new page is triggered
  224. " later. Use this first BufEnter to initialize the list of
  225. " buffers, but don't add the buffer number to the list if
  226. " it is already associated with another tab
  227. "
  228. " Unfortunately, this doesn't work right when the first
  229. " buffer opened in the tab should be associated with it,
  230. " such as when 'tab split +buffer N' is used
  231. if !exists("t:bufexp_buf_list")
  232. let t:bufexp_buf_list = []
  233. if s:AssociatedTab(a:bufnr) != -1
  234. return
  235. endif
  236. endif
  237. call s:AddBufToCurrentTab(a:bufnr)
  238. if g:bufExplorerOnlyOneTab
  239. call s:RemoveBufFromOtherTabs(a:bufnr)
  240. endif
  241. endfunction
  242. " ActivateBuffer {{{2
  243. function! s:ActivateBuffer()
  244. let _bufnr = bufnr("%")
  245. call s:UpdateTabBufData(_bufnr)
  246. call s:MRUPush(_bufnr)
  247. endfunction
  248. " DeactivateBuffer {{{2
  249. function! s:DeactivateBuffer(remove)
  250. let _bufnr = str2nr(expand("<abuf>"))
  251. call s:MRUPop(_bufnr)
  252. endfunction
  253. " MRUPop {{{2
  254. function! s:MRUPop(bufnr)
  255. call filter(s:MRUList, 'v:val != '.a:bufnr)
  256. endfunction
  257. " MRUPush {{{2
  258. function! s:MRUPush(buf)
  259. " Skip temporary buffer with buftype set. Don't add the BufExplorer window
  260. " to the list.
  261. if s:ShouldIgnore(a:buf) == 1
  262. return
  263. endif
  264. " Remove the buffer number from the list if it already exists.
  265. call s:MRUPop(a:buf)
  266. " Add the buffer number to the head of the list.
  267. call insert(s:MRUList, a:buf)
  268. endfunction
  269. " ShouldIgnore {{{2
  270. function! s:ShouldIgnore(buf)
  271. " Ignore temporary buffers with buftype set.
  272. if empty(getbufvar(a:buf, "&buftype") == 0)
  273. return 1
  274. endif
  275. " Ignore buffers with no name.
  276. if empty(bufname(a:buf)) == 1
  277. return 1
  278. endif
  279. " Ignore the BufExplorer buffer.
  280. if fnamemodify(bufname(a:buf), ":t") == s:name
  281. return 1
  282. endif
  283. " Ignore any buffers in the exclude list.
  284. if index(s:MRU_Exclude_List, bufname(a:buf)) >= 0
  285. return 1
  286. endif
  287. " Else return 0 to indicate that the buffer was not ignored.
  288. return 0
  289. endfunction
  290. " Initialize {{{2
  291. function! s:Initialize()
  292. call s:SetLocalSettings()
  293. let s:running = 1
  294. endfunction
  295. " Cleanup {{{2
  296. function! s:Cleanup()
  297. if exists("s:_insertmode")
  298. let &insertmode = s:_insertmode
  299. endif
  300. if exists("s:_showcmd")
  301. let &showcmd = s:_showcmd
  302. endif
  303. if exists("s:_cpo")
  304. let &cpo = s:_cpo
  305. endif
  306. if exists("s:_report")
  307. let &report = s:_report
  308. endif
  309. let s:running = 0
  310. let s:splitMode = ""
  311. delmarks!
  312. endfunction
  313. " SetLocalSettings {{{2
  314. function! s:SetLocalSettings()
  315. let s:_insertmode = &insertmode
  316. set noinsertmode
  317. let s:_showcmd = &showcmd
  318. set noshowcmd
  319. let s:_cpo = &cpo
  320. set cpo&vim
  321. let s:_report = &report
  322. let &report = 10000
  323. setlocal nonumber
  324. setlocal foldcolumn=0
  325. setlocal nofoldenable
  326. setlocal cursorline
  327. setlocal nospell
  328. setlocal nobuflisted
  329. setlocal filetype=bufexplorer
  330. endfunction
  331. " BufExplorerHorizontalSplit {{{2
  332. function! BufExplorerHorizontalSplit()
  333. let s:splitMode = "sp"
  334. execute "BufExplorer"
  335. endfunction
  336. " BufExplorerVerticalSplit {{{2
  337. function! BufExplorerVerticalSplit()
  338. let s:splitMode = "vsp"
  339. execute "BufExplorer"
  340. endfunction
  341. " ToggleBufExplorer {{{2
  342. function! ToggleBufExplorer()
  343. if exists("s:running") && s:running == 1 && bufname(winbufnr(0)) == s:name
  344. call s:Close()
  345. else
  346. call BufExplorer()
  347. endif
  348. endfunction
  349. " BufExplorer {{{2
  350. function! BufExplorer()
  351. let name = s:name
  352. if !has("win32")
  353. " On non-Windows boxes, escape the name so that is shows up correctly.
  354. let name = escape(name, "[]")
  355. endif
  356. " Make sure there is only one explorer open at a time.
  357. if s:running == 1
  358. " Go to the open buffer.
  359. if has("gui")
  360. execute "drop" name
  361. endif
  362. return
  363. endif
  364. " Add zero to ensure the variable is treated as a number.
  365. let s:originBuffer = bufnr("%") + 0
  366. silent let s:raw_buffer_listing = s:GetBufferInfo(0)
  367. " We may have to split the current window.
  368. if s:splitMode != ""
  369. " Save off the original settings.
  370. let [_splitbelow, _splitright] = [&splitbelow, &splitright]
  371. " Set the setting to ours.
  372. let [&splitbelow, &splitright] = [g:bufExplorerSplitBelow, g:bufExplorerSplitRight]
  373. let _size = (s:splitMode == "sp") ? g:bufExplorerSplitHorzSize : g:bufExplorerSplitVertSize
  374. " Split the window either horizontally or vertically.
  375. if _size <= 0
  376. execute 'keepalt ' . s:splitMode
  377. else
  378. execute 'keepalt ' . _size . s:splitMode
  379. endif
  380. " Restore the original settings.
  381. let [&splitbelow, &splitright] = [_splitbelow, _splitright]
  382. endif
  383. if !exists("b:displayMode") || b:displayMode != "winmanager"
  384. " Do not use keepalt when opening bufexplorer to allow the buffer that
  385. " we are leaving to become the new alternate buffer
  386. execute "silent keepjumps hide edit".name
  387. endif
  388. call s:DisplayBufferList()
  389. " Position the cursor in the newly displayed list on the line representing
  390. " the active buffer. The active buffer is the line with the '%' character
  391. " in it.
  392. execute search("%")
  393. endfunction
  394. " DisplayBufferList {{{2
  395. function! s:DisplayBufferList()
  396. " Do not set bufhidden since it wipes out the data if we switch away from
  397. " the buffer using CTRL-^.
  398. setlocal buftype=nofile
  399. setlocal modifiable
  400. setlocal noswapfile
  401. setlocal nowrap
  402. call s:SetupSyntax()
  403. call s:MapKeys()
  404. " Wipe out any existing lines in case BufExplorer buffer exists and the
  405. " user had changed any global settings that might reduce the number of
  406. " lines needed in the buffer.
  407. silent keepjumps 1,$d _
  408. call setline(1, s:CreateHelp())
  409. call s:BuildBufferList()
  410. call cursor(s:firstBufferLine, 1)
  411. if !g:bufExplorerResize
  412. normal! zz
  413. endif
  414. setlocal nomodifiable
  415. endfunction
  416. " MapKeys {{{2
  417. function! s:MapKeys()
  418. if exists("b:displayMode") && b:displayMode == "winmanager"
  419. nnoremap <buffer> <silent> <tab> :call <SID>SelectBuffer()<CR>
  420. endif
  421. nnoremap <script> <silent> <nowait> <buffer> <2-leftmouse> :call <SID>SelectBuffer()<CR>
  422. nnoremap <script> <silent> <nowait> <buffer> <CR> :call <SID>SelectBuffer()<CR>
  423. nnoremap <script> <silent> <nowait> <buffer> <F1> :call <SID>ToggleHelp()<CR>
  424. nnoremap <script> <silent> <nowait> <buffer> <s-cr> :call <SID>SelectBuffer("tab")<CR>
  425. nnoremap <script> <silent> <nowait> <buffer> a :call <SID>ToggleFindActive()<CR>
  426. nnoremap <script> <silent> <nowait> <buffer> b :call <SID>SelectBuffer("ask")<CR>
  427. nnoremap <script> <silent> <nowait> <buffer> d :call <SID>RemoveBuffer("delete")<CR>
  428. xnoremap <script> <silent> <nowait> <buffer> d :call <SID>RemoveBuffer("delete")<CR>
  429. nnoremap <script> <silent> <nowait> <buffer> D :call <SID>RemoveBuffer("wipe")<CR>
  430. xnoremap <script> <silent> <nowait> <buffer> D :call <SID>RemoveBuffer("wipe")<CR>
  431. nnoremap <script> <silent> <nowait> <buffer> f :call <SID>SelectBuffer("split", "sb")<CR>
  432. nnoremap <script> <silent> <nowait> <buffer> F :call <SID>SelectBuffer("split", "st")<CR>
  433. nnoremap <script> <silent> <nowait> <buffer> m :call <SID>MRUListShow()<CR>
  434. nnoremap <script> <silent> <nowait> <buffer> o :call <SID>SelectBuffer()<CR>
  435. nnoremap <script> <silent> <nowait> <buffer> p :call <SID>ToggleSplitOutPathName()<CR>
  436. nnoremap <script> <silent> <nowait> <buffer> q :call <SID>Close()<CR>
  437. nnoremap <script> <silent> <nowait> <buffer> r :call <SID>SortReverse()<CR>
  438. nnoremap <script> <silent> <nowait> <buffer> R :call <SID>ToggleShowRelativePath()<CR>
  439. nnoremap <script> <silent> <nowait> <buffer> s :call <SID>SortSelect()<CR>
  440. nnoremap <script> <silent> <nowait> <buffer> S :call <SID>ReverseSortSelect()<CR>
  441. nnoremap <script> <silent> <nowait> <buffer> t :call <SID>SelectBuffer("tab")<CR>
  442. nnoremap <script> <silent> <nowait> <buffer> T :call <SID>ToggleShowTabBuffer()<CR>
  443. nnoremap <script> <silent> <nowait> <buffer> u :call <SID>ToggleShowUnlisted()<CR>
  444. nnoremap <script> <silent> <nowait> <buffer> v :call <SID>SelectBuffer("split", "vr")<CR>
  445. nnoremap <script> <silent> <nowait> <buffer> V :call <SID>SelectBuffer("split", "vl")<CR>
  446. for k in ["G", "n", "N", "L", "M", "H"]
  447. execute "nnoremap <buffer> <silent>" k ":keepjumps normal!" k."<CR>"
  448. endfor
  449. endfunction
  450. " SetupSyntax {{{2
  451. function! s:SetupSyntax()
  452. if has("syntax")
  453. syn match bufExplorerHelp "^\".*" contains=bufExplorerSortBy,bufExplorerMapping,bufExplorerTitle,bufExplorerSortType,bufExplorerToggleSplit,bufExplorerToggleOpen
  454. syn match bufExplorerOpenIn "Open in \w\+ window" contained
  455. syn match bufExplorerSplit "\w\+ split" contained
  456. syn match bufExplorerSortBy "Sorted by .*" contained contains=bufExplorerOpenIn,bufExplorerSplit
  457. syn match bufExplorerMapping "\" \zs.\+\ze :" contained
  458. syn match bufExplorerTitle "Buffer Explorer.*" contained
  459. syn match bufExplorerSortType "'\w\{-}'" contained
  460. syn match bufExplorerBufNbr /^\s*\d\+/
  461. syn match bufExplorerToggleSplit "toggle split type" contained
  462. syn match bufExplorerToggleOpen "toggle open mode" contained
  463. syn match bufExplorerModBuf /^\s*\d\+.\{4}+.*/
  464. syn match bufExplorerLockedBuf /^\s*\d\+.\{3}[\-=].*/
  465. syn match bufExplorerHidBuf /^\s*\d\+.\{2}h.*/
  466. syn match bufExplorerActBuf /^\s*\d\+.\{2}a.*/
  467. syn match bufExplorerCurBuf /^\s*\d\+.%.*/
  468. syn match bufExplorerAltBuf /^\s*\d\+.#.*/
  469. syn match bufExplorerUnlBuf /^\s*\d\+u.*/
  470. syn match bufExplorerInactBuf /^\s*\d\+ \{7}.*/
  471. hi def link bufExplorerBufNbr Number
  472. hi def link bufExplorerMapping NonText
  473. hi def link bufExplorerHelp Special
  474. hi def link bufExplorerOpenIn Identifier
  475. hi def link bufExplorerSortBy String
  476. hi def link bufExplorerSplit NonText
  477. hi def link bufExplorerTitle NonText
  478. hi def link bufExplorerSortType bufExplorerSortBy
  479. hi def link bufExplorerToggleSplit bufExplorerSplit
  480. hi def link bufExplorerToggleOpen bufExplorerOpenIn
  481. hi def link bufExplorerActBuf Identifier
  482. hi def link bufExplorerAltBuf String
  483. hi def link bufExplorerCurBuf Type
  484. hi def link bufExplorerHidBuf Constant
  485. hi def link bufExplorerLockedBuf Special
  486. hi def link bufExplorerModBuf Exception
  487. hi def link bufExplorerUnlBuf Comment
  488. hi def link bufExplorerInactBuf Comment
  489. endif
  490. endfunction
  491. " ToggleHelp {{{2
  492. function! s:ToggleHelp()
  493. let g:bufExplorerDetailedHelp = !g:bufExplorerDetailedHelp
  494. setlocal modifiable
  495. " Save position.
  496. normal! ma
  497. " Remove old header.
  498. if s:firstBufferLine > 1
  499. execute "keepjumps 1,".(s:firstBufferLine - 1) "d _"
  500. endif
  501. call append(0, s:CreateHelp())
  502. silent! normal! g`a
  503. delmarks a
  504. setlocal nomodifiable
  505. if exists("b:displayMode") && b:displayMode == "winmanager"
  506. call WinManagerForceReSize("BufExplorer")
  507. endif
  508. endfunction
  509. " GetHelpStatus {{{2
  510. function! s:GetHelpStatus()
  511. let ret = '" Sorted by '.((g:bufExplorerReverseSort == 1) ? "reverse " : "").g:bufExplorerSortBy
  512. let ret .= ' | '.((g:bufExplorerFindActive == 0) ? "Don't " : "")."Locate buffer"
  513. let ret .= ((g:bufExplorerShowUnlisted == 0) ? "" : " | Show unlisted")
  514. let ret .= ((g:bufExplorerShowTabBuffer == 0) ? "" : " | Show buffers/tab")
  515. let ret .= ((g:bufExplorerOnlyOneTab == 0) ? "" : " | One tab/buffer")
  516. let ret .= ' | '.((g:bufExplorerShowRelativePath == 0) ? "Absolute" : "Relative")
  517. let ret .= ' '.((g:bufExplorerSplitOutPathName == 0) ? "Full" : "Split")." path"
  518. return ret
  519. endfunction
  520. " CreateHelp {{{2
  521. function! s:CreateHelp()
  522. if g:bufExplorerDefaultHelp == 0 && g:bufExplorerDetailedHelp == 0
  523. let s:firstBufferLine = 1
  524. return []
  525. endif
  526. let header = []
  527. if g:bufExplorerDetailedHelp == 1
  528. call add(header, '" Buffer Explorer ('.g:bufexplorer_version.')')
  529. call add(header, '" --------------------------')
  530. call add(header, '" <F1> : toggle this help')
  531. call add(header, '" <enter> or o or Mouse-Double-Click : open buffer under cursor')
  532. call add(header, '" <shift-enter> or t : open buffer in another tab')
  533. call add(header, '" a : toggle find active buffer')
  534. call add(header, '" b : Fast buffer switching with b<any bufnum>')
  535. call add(header, '" B : toggle if to save/use recent tab or not')
  536. call add(header, '" d : delete buffer')
  537. call add(header, '" D : wipe buffer')
  538. call add(header, '" F : open buffer in another window above the current')
  539. call add(header, '" f : open buffer in another window below the current')
  540. call add(header, '" p : toggle splitting of file and path name')
  541. call add(header, '" q : quit')
  542. call add(header, '" r : reverse sort')
  543. call add(header, '" R : toggle showing relative or full paths')
  544. call add(header, '" s : cycle thru "sort by" fields '.string(s:sort_by).'')
  545. call add(header, '" S : reverse cycle thru "sort by" fields')
  546. call add(header, '" T : toggle if to show only buffers for this tab or not')
  547. call add(header, '" u : toggle showing unlisted buffers')
  548. call add(header, '" V : open buffer in another window on the left of the current')
  549. call add(header, '" v : open buffer in another window on the right of the current')
  550. else
  551. call add(header, '" Press <F1> for Help')
  552. endif
  553. if (!exists("b:displayMode") || b:displayMode != "winmanager") || (b:displayMode == "winmanager" && g:bufExplorerDetailedHelp == 1)
  554. call add(header, s:GetHelpStatus())
  555. call add(header, '"=')
  556. endif
  557. let s:firstBufferLine = len(header) + 1
  558. return header
  559. endfunction
  560. " GetBufferInfo {{{2
  561. function! s:GetBufferInfo(bufnr)
  562. redir => bufoutput
  563. " Show all buffers including the unlisted ones. [!] tells Vim to show the
  564. " unlisted ones.
  565. buffers!
  566. redir END
  567. if a:bufnr > 0
  568. " Since we are only interested in this specified buffer
  569. " remove the other buffers listed
  570. let bufoutput = substitute(bufoutput."\n", '^.*\n\(\s*'.a:bufnr.'\>.\{-}\)\n.*', '\1', '')
  571. endif
  572. let [all, allwidths, listedwidths] = [[], {}, {}]
  573. for n in keys(s:types)
  574. let allwidths[n] = []
  575. let listedwidths[n] = []
  576. endfor
  577. " Loop over each line in the buffer.
  578. for buf in split(bufoutput, '\n')
  579. let bits = split(buf, '"')
  580. " Use first and last components after the split on '"', in case a
  581. " filename with an embedded '"' is present.
  582. let b = {"attributes": bits[0], "line": substitute(bits[-1], '\s*', '', '')}
  583. let name = bufname(str2nr(b.attributes))
  584. let b["hasNoName"] = empty(name)
  585. if b.hasNoName
  586. let name = "[No Name]"
  587. endif
  588. for [key, val] in items(s:types)
  589. let b[key] = fnamemodify(name, val)
  590. endfor
  591. if getftype(b.fullname) == "dir" && g:bufExplorerShowDirectories == 1
  592. let b.shortname = "<DIRECTORY>"
  593. endif
  594. call add(all, b)
  595. for n in keys(s:types)
  596. call add(allwidths[n], s:StringWidth(b[n]))
  597. if b.attributes !~ "u"
  598. call add(listedwidths[n], s:StringWidth(b[n]))
  599. endif
  600. endfor
  601. endfor
  602. let [s:allpads, s:listedpads] = [{}, {}]
  603. for n in keys(s:types)
  604. let s:allpads[n] = repeat(' ', max(allwidths[n]))
  605. let s:listedpads[n] = repeat(' ', max(listedwidths[n]))
  606. endfor
  607. return all
  608. endfunction
  609. " BuildBufferList {{{2
  610. function! s:BuildBufferList()
  611. let lines = []
  612. " Loop through every buffer.
  613. for buf in s:raw_buffer_listing
  614. " Skip unlisted buffers if we are not to show them.
  615. if !g:bufExplorerShowUnlisted && buf.attributes =~ "u"
  616. " Skip unlisted buffers if we are not to show them.
  617. continue
  618. endif
  619. " Skip "No Name" buffers if we are not to show them.
  620. if g:bufExplorerShowNoName == 0 && buf.hasNoName
  621. continue
  622. endif
  623. " Are we to show only buffer(s) for this tab?
  624. if g:bufExplorerShowTabBuffer && (!s:IsInCurrentTab(str2nr(buf.attributes)))
  625. continue
  626. endif
  627. let line = buf.attributes." "
  628. " Are we to split the path and file name?
  629. if g:bufExplorerSplitOutPathName
  630. let type = (g:bufExplorerShowRelativePath) ? "relativepath" : "path"
  631. let path = buf[type]
  632. let pad = (g:bufExplorerShowUnlisted) ? s:allpads.shortname : s:listedpads.shortname
  633. let line .= buf.shortname." ".strpart(pad.path, s:StringWidth(buf.shortname))
  634. else
  635. let type = (g:bufExplorerShowRelativePath) ? "relativename" : "fullname"
  636. let path = buf[type]
  637. let line .= path
  638. endif
  639. let pads = (g:bufExplorerShowUnlisted) ? s:allpads : s:listedpads
  640. if !empty(pads[type])
  641. let line .= strpart(pads[type], s:StringWidth(path))." "
  642. endif
  643. let line .= buf.line
  644. call add(lines, line)
  645. endfor
  646. call setline(s:firstBufferLine, lines)
  647. call s:SortListing()
  648. endfunction
  649. " SelectBuffer {{{2
  650. function! s:SelectBuffer(...)
  651. " Sometimes messages are not cleared when we get here so it looks like an
  652. " error has occurred when it really has not.
  653. "echo ""
  654. let _bufNbr = -1
  655. if (a:0 == 1) && (a:1 == "ask")
  656. " Ask the user for input.
  657. call inputsave()
  658. let cmd = input("Enter buffer number to switch to: ")
  659. call inputrestore()
  660. " Clear the message area from the previous prompt.
  661. redraw | echo
  662. if strlen(cmd) > 0
  663. let _bufNbr = str2nr(cmd)
  664. else
  665. call s:Error("Invalid buffer number, try again.")
  666. return
  667. endif
  668. else
  669. " Are we on a line with a file name?
  670. if line('.') < s:firstBufferLine
  671. execute "normal! \<CR>"
  672. return
  673. endif
  674. let _bufNbr = str2nr(getline('.'))
  675. " Check and see if we are running BufferExplorer via WinManager.
  676. if exists("b:displayMode") && b:displayMode == "winmanager"
  677. let _bufName = expand("#"._bufNbr.":p")
  678. if (a:0 == 1) && (a:1 == "tab")
  679. call WinManagerFileEdit(_bufName, 1)
  680. else
  681. call WinManagerFileEdit(_bufName, 0)
  682. endif
  683. return
  684. endif
  685. endif
  686. if bufexists(_bufNbr)
  687. if bufnr("#") == _bufNbr && !exists("g:bufExplorerChgWin")
  688. return s:Close()
  689. endif
  690. " Get the tab number where this bufer is located in.
  691. let tabNbr = s:GetTabNbr(_bufNbr)
  692. " Are we supposed to open the selected buffer in a tab?
  693. if (a:0 == 1) && (a:1 == "tab")
  694. " Restore [BufExplorer] buffer.
  695. execute "silent buffer!".s:originBuffer
  696. " Was the tab found?
  697. if tabNbr == 0
  698. " _bufNbr is not opened in any tabs. Open a new tab with the
  699. " selected buffer in it.
  700. if v:version > 704 || ( v:version == 704 && has('patch2237') )
  701. " new syntax for last tab as of 7.4.2237
  702. execute "$tab split +buffer" . _bufNbr
  703. else
  704. execute "999tab split +buffer" . _bufNbr
  705. endif
  706. " Workaround for the issue mentioned in UpdateTabBufData.
  707. call s:UpdateTabBufData(_bufNbr)
  708. else
  709. " The _bufNbr is already opened in a tab, go to that tab.
  710. execute tabNbr . "tabnext"
  711. " Focus window.
  712. execute s:GetWinNbr(tabNbr, _bufNbr) . "wincmd w"
  713. endif
  714. " Are we supposed to open the selected buffer in a split?
  715. elseif (a:0 == 2) && (a:1 == "split")
  716. if g:bufExplorerFindActive
  717. call s:Close()
  718. endif
  719. " Was the tab found?
  720. if tabNbr != 0
  721. " Yes, the buffer is located in a tab. Go to that tab instead of
  722. " opening split
  723. execute tabNbr . "tabnext"
  724. else
  725. "Nope, the buffer is not in a tab, open it accordingly
  726. let _bufName = expand("#"._bufNbr.":p")
  727. if (a:2 == "vl")
  728. execute _bufName ?
  729. \ "vert topleft sb ".escape(_bufName, " ") :
  730. \ "vert topleft sb "._bufNbr
  731. elseif (a:2 == "vr")
  732. execute _bufName ?
  733. \ "vert belowright sb ".escape(_bufName, " ") :
  734. \ "vert belowright sb "._bufNbr
  735. elseif (a:2 == "st")
  736. execute _bufName ?
  737. \ "topleft sb ".escape(_bufName, " ") :
  738. \ "topleft sb "._bufNbr
  739. else " = sb
  740. execute _bufName ?
  741. \ "belowright sb ".escape(_bufName, " ") :
  742. \ "belowright sb "._bufNbr
  743. endif
  744. endif
  745. " Switch to selected buffer
  746. execute "keepalt silent b!" _bufNbr
  747. " Default, open in current window
  748. else
  749. " Are we suppose to move to the tab where the active buffer is?
  750. if exists("g:bufExplorerChgWin")
  751. execute g:bufExplorerChgWin."wincmd w"
  752. elseif bufloaded(_bufNbr) && g:bufExplorerFindActive
  753. if g:bufExplorerFindActive
  754. call s:Close()
  755. endif
  756. " Was the tab found?
  757. if tabNbr != 0
  758. " Yes, the buffer is located in a tab. Go to that tab number.
  759. execute tabNbr . "tabnext"
  760. else
  761. "Nope, the buffer is not in a tab. Simply switch to that
  762. "buffer.
  763. let _bufName = expand("#"._bufNbr.":p")
  764. execute _bufName ? "drop ".escape(_bufName, " ") : "buffer "._bufNbr
  765. endif
  766. endif
  767. " Switch to the selected buffer.
  768. execute "keepalt silent b!" _bufNbr
  769. endif
  770. " Make the buffer 'listed' again.
  771. call setbufvar(_bufNbr, "&buflisted", "1")
  772. " Call any associated function references. g:bufExplorerFuncRef may be
  773. " an individual function reference or it may be a list containing
  774. " function references. It will ignore anything that's not a function
  775. " reference.
  776. "
  777. " See :help FuncRef for more on function references.
  778. if exists("g:BufExplorerFuncRef")
  779. if type(g:BufExplorerFuncRef) == 2
  780. keepj call g:BufExplorerFuncRef()
  781. elseif type(g:BufExplorerFuncRef) == 3
  782. for FncRef in g:BufExplorerFuncRef
  783. if type(FncRef) == 2
  784. keepj call FncRef()
  785. endif
  786. endfor
  787. endif
  788. endif
  789. else
  790. call s:Error("Sorry, that buffer no longer exists, please select another")
  791. call s:DeleteBuffer(_bufNbr, "wipe")
  792. endif
  793. endfunction
  794. " RemoveBuffer {{{2
  795. function! s:RemoveBuffer(mode)
  796. " Are we on a line with a file name?
  797. if line('.') < s:firstBufferLine
  798. return
  799. endif
  800. " Do not allow this buffer to be deleted if it is the last one.
  801. if len(s:MRUList) == 1
  802. call s:Error("Sorry, you are not allowed to delete the last buffer")
  803. return
  804. endif
  805. " These commands are to temporarily suspend the activity of winmanager.
  806. if exists("b:displayMode") && b:displayMode == "winmanager"
  807. call WinManagerSuspendAUs()
  808. end
  809. let _bufNbr = str2nr(getline('.'))
  810. if getbufvar(_bufNbr, '&modified') == 1
  811. call s:Error("Sorry, no write since last change for buffer "._bufNbr.", unable to delete")
  812. return
  813. else
  814. " Okay, everything is good, delete or wipe the buffer.
  815. call s:DeleteBuffer(_bufNbr, a:mode)
  816. endif
  817. " Reactivate winmanager autocommand activity.
  818. if exists("b:displayMode") && b:displayMode == "winmanager"
  819. call WinManagerForceReSize("BufExplorer")
  820. call WinManagerResumeAUs()
  821. end
  822. endfunction
  823. " DeleteBuffer {{{2
  824. function! s:DeleteBuffer(buf, mode)
  825. " This routine assumes that the buffer to be removed is on the current line.
  826. try
  827. " Wipe/Delete buffer from Vim.
  828. if a:mode == "wipe"
  829. execute "silent bwipe" a:buf
  830. else
  831. execute "silent bdelete" a:buf
  832. endif
  833. " Delete the buffer from the list on screen.
  834. setlocal modifiable
  835. normal! "_dd
  836. setlocal nomodifiable
  837. " Delete the buffer from the raw buffer list.
  838. call filter(s:raw_buffer_listing, 'v:val.attributes !~ " '.a:buf.' "')
  839. catch
  840. call s:Error(v:exception)
  841. endtry
  842. endfunction
  843. " ListedAndCurrentTab {{{2
  844. " Returns whether the specified buffer is both listed and associated
  845. " with the current tab
  846. function! s:ListedAndCurrentTab(buf)
  847. return buflisted(a:buf) && s:IsInCurrentTab(a:buf)
  848. endfunction
  849. " Close {{{2
  850. function! s:Close()
  851. " Get only the listed buffers associated with the current tab
  852. let listed = filter(copy(s:MRUList), "s:ListedAndCurrentTab(v:val)")
  853. if len(listed) == 0
  854. let listed = filter(range(1, bufnr('$')), "s:ListedAndCurrentTab(v:val)")
  855. endif
  856. " If we needed to split the main window, close the split one.
  857. if s:splitMode != "" && bufwinnr(s:originBuffer) != -1
  858. execute "wincmd c"
  859. endif
  860. " Check to see if there are anymore buffers listed.
  861. if len(listed) == 0
  862. " Since there are no buffers left to switch to, open a new empty
  863. " buffers.
  864. execute "enew"
  865. else
  866. " Since there are buffers left to switch to, switch to the previous and
  867. " then the current.
  868. for b in reverse(listed[0:1])
  869. execute "keepjumps silent b ".b
  870. endfor
  871. endif
  872. " Clear any messages.
  873. echo
  874. endfunction
  875. " ToggleSplitOutPathName {{{2
  876. function! s:ToggleSplitOutPathName()
  877. let g:bufExplorerSplitOutPathName = !g:bufExplorerSplitOutPathName
  878. call s:RebuildBufferList()
  879. call s:UpdateHelpStatus()
  880. endfunction
  881. " ToggleShowRelativePath {{{2
  882. function! s:ToggleShowRelativePath()
  883. let g:bufExplorerShowRelativePath = !g:bufExplorerShowRelativePath
  884. call s:RebuildBufferList()
  885. call s:UpdateHelpStatus()
  886. endfunction
  887. " ToggleShowTabBuffer {{{2
  888. function! s:ToggleShowTabBuffer()
  889. let g:bufExplorerShowTabBuffer = !g:bufExplorerShowTabBuffer
  890. call s:RebuildBufferList(g:bufExplorerShowTabBuffer)
  891. call s:UpdateHelpStatus()
  892. endfunction
  893. " ToggleOnlyOneTab {{{2
  894. function! s:ToggleOnlyOneTab()
  895. let g:bufExplorerOnlyOneTab = !g:bufExplorerOnlyOneTab
  896. call s:RebuildBufferList()
  897. call s:UpdateHelpStatus()
  898. endfunction
  899. " ToggleShowUnlisted {{{2
  900. function! s:ToggleShowUnlisted()
  901. let g:bufExplorerShowUnlisted = !g:bufExplorerShowUnlisted
  902. let num_bufs = s:RebuildBufferList(g:bufExplorerShowUnlisted == 0)
  903. call s:UpdateHelpStatus()
  904. endfunction
  905. " ToggleFindActive {{{2
  906. function! s:ToggleFindActive()
  907. let g:bufExplorerFindActive = !g:bufExplorerFindActive
  908. call s:UpdateHelpStatus()
  909. endfunction
  910. " RebuildBufferList {{{2
  911. function! s:RebuildBufferList(...)
  912. setlocal modifiable
  913. let curPos = getpos('.')
  914. if a:0 && a:000[0] && (line('$') >= s:firstBufferLine)
  915. " Clear the list first.
  916. execute "silent keepjumps ".s:firstBufferLine.',$d _'
  917. endif
  918. let num_bufs = s:BuildBufferList()
  919. call setpos('.', curPos)
  920. setlocal nomodifiable
  921. return num_bufs
  922. endfunction
  923. " UpdateHelpStatus {{{2
  924. function! s:UpdateHelpStatus()
  925. setlocal modifiable
  926. let text = s:GetHelpStatus()
  927. call setline(s:firstBufferLine - 2, text)
  928. setlocal nomodifiable
  929. endfunction
  930. " MRUCmp {{{2
  931. function! s:MRUCmp(line1, line2)
  932. return index(s:MRUList, str2nr(a:line1)) - index(s:MRUList, str2nr(a:line2))
  933. endfunction
  934. " SortReverse {{{2
  935. function! s:SortReverse()
  936. let g:bufExplorerReverseSort = !g:bufExplorerReverseSort
  937. call s:ReSortListing()
  938. endfunction
  939. " SortSelect {{{2
  940. function! s:SortSelect()
  941. let g:bufExplorerSortBy = get(s:sort_by, index(s:sort_by, g:bufExplorerSortBy) + 1, s:sort_by[0])
  942. call s:ReSortListing()
  943. endfunction
  944. " ReverseSortSelect {{{2
  945. function! s:ReverseSortSelect()
  946. let g:bufExplorerSortBy = get(s:sort_by, index(s:sort_by, g:bufExplorerSortBy) - 1, s:sort_by[-1])
  947. call s:ReSortListing()
  948. endfunction
  949. " ReSortListing {{{2
  950. function! s:ReSortListing()
  951. setlocal modifiable
  952. let curPos = getpos('.')
  953. call s:SortListing()
  954. call s:UpdateHelpStatus()
  955. call setpos('.', curPos)
  956. setlocal nomodifiable
  957. endfunction
  958. " SortListing {{{2
  959. function! s:SortListing()
  960. let sort = s:firstBufferLine.",$sort".((g:bufExplorerReverseSort == 1) ? "!": "")
  961. if g:bufExplorerSortBy == "number"
  962. " Easiest case.
  963. execute sort 'n'
  964. elseif g:bufExplorerSortBy == "name"
  965. " Sort by full path first
  966. execute sort 'ir /\zs\f\+\ze\s\+line/'
  967. if g:bufExplorerSplitOutPathName
  968. execute sort 'ir /\d.\{7}\zs\f\+\ze/'
  969. else
  970. execute sort 'ir /\zs[^\/\\]\+\ze\s*line/'
  971. endif
  972. elseif g:bufExplorerSortBy == "fullpath"
  973. if g:bufExplorerSplitOutPathName
  974. " Sort twice - first on the file name then on the path.
  975. execute sort 'ir /\d.\{7}\zs\f\+\ze/'
  976. endif
  977. execute sort 'ir /\zs\f\+\ze\s\+line/'
  978. elseif g:bufExplorerSortBy == "extension"
  979. " Sort by full path...
  980. execute sort 'ir /\zs\f\+\ze\s\+line/'
  981. " Sort by name...
  982. if g:bufExplorerSplitOutPathName
  983. " Sort twice - first on the file name then on the path.
  984. execute sort 'ir /\d.\{7}\zs\f\+\ze/'
  985. endif
  986. " Sort by extension.
  987. execute sort 'ir /\.\zs\w\+\ze\s/'
  988. elseif g:bufExplorerSortBy == "mru"
  989. let l = getline(s:firstBufferLine, "$")
  990. call sort(l, "<SID>MRUCmp")
  991. if g:bufExplorerReverseSort
  992. call reverse(l)
  993. endif
  994. call setline(s:firstBufferLine, l)
  995. endif
  996. endfunction
  997. " MRUListShow {{{2
  998. function! s:MRUListShow()
  999. echomsg "MRUList=".string(s:MRUList)
  1000. endfunction
  1001. " Error {{{2
  1002. " Display a message using ErrorMsg highlight group.
  1003. function! s:Error(msg)
  1004. echohl ErrorMsg
  1005. echomsg a:msg
  1006. echohl None
  1007. endfunction
  1008. " Warning {{{2
  1009. " Display a message using WarningMsg highlight group.
  1010. function! s:Warning(msg)
  1011. echohl WarningMsg
  1012. echomsg a:msg
  1013. echohl None
  1014. endfunction
  1015. " GetTabNbr {{{2
  1016. function! s:GetTabNbr(bufNbr)
  1017. " Searching buffer bufno, in tabs.
  1018. for i in range(tabpagenr("$"))
  1019. if index(tabpagebuflist(i + 1), a:bufNbr) != -1
  1020. return i + 1
  1021. endif
  1022. endfor
  1023. return 0
  1024. endfunction
  1025. " GetWinNbr" {{{2
  1026. function! s:GetWinNbr(tabNbr, bufNbr)
  1027. " window number in tabpage.
  1028. let tablist = tabpagebuflist(a:tabNbr)
  1029. " Number: 0
  1030. " String: 1
  1031. " Funcref: 2
  1032. " List: 3
  1033. " Dictionary: 4
  1034. " Float: 5
  1035. if type(tablist) == 3
  1036. return index(tabpagebuflist(a:tabNbr), a:bufNbr) + 1
  1037. else
  1038. return 1
  1039. endif
  1040. endfunction
  1041. " StringWidth" {{{2
  1042. if exists('*strwidth')
  1043. function s:StringWidth(s)
  1044. return strwidth(a:s)
  1045. endfunction
  1046. else
  1047. function s:StringWidth(s)
  1048. return len(a:s)
  1049. endfunction
  1050. endif
  1051. " Winmanager Integration {{{2
  1052. let g:BufExplorer_title = "\[Buf\ List\]"
  1053. call s:Set("g:bufExplorerResize", 1)
  1054. call s:Set("g:bufExplorerMaxHeight", 25) " Handles dynamic resizing of the window.
  1055. " function! to start display. Set the mode to 'winmanager' for this buffer.
  1056. " This is to figure out how this plugin was called. In a standalone fashion
  1057. " or by winmanager.
  1058. function! BufExplorer_Start()
  1059. let b:displayMode = "winmanager"
  1060. call s:SetLocalSettings()
  1061. call BufExplorer()
  1062. endfunction
  1063. " Returns whether the display is okay or not.
  1064. function! BufExplorer_IsValid()
  1065. return 0
  1066. endfunction
  1067. " Handles dynamic refreshing of the window.
  1068. function! BufExplorer_Refresh()
  1069. let b:displayMode = "winmanager"
  1070. call s:SetLocalSettings()
  1071. call BufExplorer()
  1072. endfunction
  1073. function! BufExplorer_ReSize()
  1074. if !g:bufExplorerResize
  1075. return
  1076. end
  1077. let nlines = min([line("$"), g:bufExplorerMaxHeight])
  1078. execute nlines." wincmd _"
  1079. " The following lines restore the layout so that the last file line is also
  1080. " the last window line. Sometimes, when a line is deleted, although the
  1081. " window size is exactly equal to the number of lines in the file, some of
  1082. " the lines are pushed up and we see some lagging '~'s.
  1083. let pres = getpos(".")
  1084. normal! $
  1085. let _scr = &scrolloff
  1086. let &scrolloff = 0
  1087. normal! z-
  1088. let &scrolloff = _scr
  1089. call setpos(".", pres)
  1090. endfunction
  1091. " Default values {{{2
  1092. call s:Set("g:bufExplorerDisableDefaultKeyMapping", 0) " Do not disable default key mappings.
  1093. call s:Set("g:bufExplorerDefaultHelp", 1) " Show default help?
  1094. call s:Set("g:bufExplorerDetailedHelp", 0) " Show detailed help?
  1095. call s:Set("g:bufExplorerFindActive", 1) " When selecting an active buffer, take you to the window where it is active?
  1096. call s:Set("g:bufExplorerOnlyOneTab", 1) " If ShowTabBuffer = 1, only store the most recent tab for this buffer.
  1097. call s:Set("g:bufExplorerReverseSort", 0) " Sort in reverse order by default?
  1098. call s:Set("g:bufExplorerShowDirectories", 1) " (Dir's are added by commands like ':e .')
  1099. call s:Set("g:bufExplorerShowRelativePath", 0) " Show listings with relative or absolute paths?
  1100. call s:Set("g:bufExplorerShowTabBuffer", 0) " Show only buffer(s) for this tab?
  1101. call s:Set("g:bufExplorerShowUnlisted", 0) " Show unlisted buffers?
  1102. call s:Set("g:bufExplorerShowNoName", 0) " Show 'No Name' buffers?
  1103. call s:Set("g:bufExplorerSortBy", "mru") " Sorting methods are in s:sort_by:
  1104. call s:Set("g:bufExplorerSplitBelow", &splitbelow) " Should horizontal splits be below or above current window?
  1105. call s:Set("g:bufExplorerSplitOutPathName", 1) " Split out path and file name?
  1106. call s:Set("g:bufExplorerSplitRight", &splitright) " Should vertical splits be on the right or left of current window?
  1107. call s:Set("g:bufExplorerSplitVertSize", 0) " Height for a vertical split. If <=0, default Vim size is used.
  1108. call s:Set("g:bufExplorerSplitHorzSize", 0) " Height for a horizontal split. If <=0, default Vim size is used.
  1109. " Default key mapping {{{2
  1110. if !hasmapto('BufExplorer') && g:bufExplorerDisableDefaultKeyMapping == 0
  1111. nnoremap <script> <silent> <unique> <Leader>be :BufExplorer<CR>
  1112. endif
  1113. if !hasmapto('ToggleBufExplorer') && g:bufExplorerDisableDefaultKeyMapping == 0
  1114. nnoremap <script> <silent> <unique> <Leader>bt :ToggleBufExplorer<CR>
  1115. endif
  1116. if !hasmapto('BufExplorerHorizontalSplit') && g:bufExplorerDisableDefaultKeyMapping == 0
  1117. nnoremap <script> <silent> <unique> <Leader>bs :BufExplorerHorizontalSplit<CR>
  1118. endif
  1119. if !hasmapto('BufExplorerVerticalSplit') && g:bufExplorerDisableDefaultKeyMapping == 0
  1120. nnoremap <script> <silent> <unique> <Leader>bv :BufExplorerVerticalSplit<CR>
  1121. endif
  1122. " vim:ft=vim foldmethod=marker sw=4