bufexplorer.vim 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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 yahoo dot com)
  39. " Last Changed: Wednesday, 08 March 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.13"
  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. endfunction
  330. " BufExplorerHorizontalSplit {{{2
  331. function! BufExplorerHorizontalSplit()
  332. let s:splitMode = "sp"
  333. execute "BufExplorer"
  334. endfunction
  335. " BufExplorerVerticalSplit {{{2
  336. function! BufExplorerVerticalSplit()
  337. let s:splitMode = "vsp"
  338. execute "BufExplorer"
  339. endfunction
  340. " ToggleBufExplorer {{{2
  341. function! ToggleBufExplorer()
  342. if exists("s:running") && s:running == 1 && bufname(winbufnr(0)) == s:name
  343. call s:Close()
  344. else
  345. call BufExplorer()
  346. endif
  347. endfunction
  348. " BufExplorer {{{2
  349. function! BufExplorer()
  350. let name = s:name
  351. if !has("win32")
  352. " On non-Windows boxes, escape the name so that is shows up correctly.
  353. let name = escape(name, "[]")
  354. endif
  355. " Make sure there is only one explorer open at a time.
  356. if s:running == 1
  357. " Go to the open buffer.
  358. if has("gui")
  359. execute "drop" name
  360. endif
  361. return
  362. endif
  363. " Add zero to ensure the variable is treated as a number.
  364. let s:originBuffer = bufnr("%") + 0
  365. silent let s:raw_buffer_listing = s:GetBufferInfo(0)
  366. " We may have to split the current window.
  367. if s:splitMode != ""
  368. " Save off the original settings.
  369. let [_splitbelow, _splitright] = [&splitbelow, &splitright]
  370. " Set the setting to ours.
  371. let [&splitbelow, &splitright] = [g:bufExplorerSplitBelow, g:bufExplorerSplitRight]
  372. let _size = (s:splitMode == "sp") ? g:bufExplorerSplitHorzSize : g:bufExplorerSplitVertSize
  373. " Split the window either horizontally or vertically.
  374. if _size <= 0
  375. execute 'keepalt ' . s:splitMode
  376. else
  377. execute 'keepalt ' . _size . s:splitMode
  378. endif
  379. " Restore the original settings.
  380. let [&splitbelow, &splitright] = [_splitbelow, _splitright]
  381. endif
  382. if !exists("b:displayMode") || b:displayMode != "winmanager"
  383. " Do not use keepalt when opening bufexplorer to allow the buffer that
  384. " we are leaving to become the new alternate buffer
  385. execute "silent keepjumps hide edit".name
  386. endif
  387. call s:DisplayBufferList()
  388. " Position the cursor in the newly displayed list on the line representing
  389. " the active buffer. The active buffer is the line with the '%' character
  390. " in it.
  391. execute search("%")
  392. endfunction
  393. " DisplayBufferList {{{2
  394. function! s:DisplayBufferList()
  395. " Do not set bufhidden since it wipes out the data if we switch away from
  396. " the buffer using CTRL-^.
  397. setlocal buftype=nofile
  398. setlocal modifiable
  399. setlocal noswapfile
  400. setlocal nowrap
  401. call s:SetupSyntax()
  402. call s:MapKeys()
  403. " Wipe out any existing lines in case BufExplorer buffer exists and the
  404. " user had changed any global settings that might reduce the number of
  405. " lines needed in the buffer.
  406. silent keepjumps 1,$d _
  407. call setline(1, s:CreateHelp())
  408. call s:BuildBufferList()
  409. call cursor(s:firstBufferLine, 1)
  410. if !g:bufExplorerResize
  411. normal! zz
  412. endif
  413. setlocal nomodifiable
  414. endfunction
  415. " MapKeys {{{2
  416. function! s:MapKeys()
  417. if exists("b:displayMode") && b:displayMode == "winmanager"
  418. nnoremap <buffer> <silent> <tab> :call <SID>SelectBuffer()<CR>
  419. endif
  420. nnoremap <script> <silent> <nowait> <buffer> <2-leftmouse> :call <SID>SelectBuffer()<CR>
  421. nnoremap <script> <silent> <nowait> <buffer> <CR> :call <SID>SelectBuffer()<CR>
  422. nnoremap <script> <silent> <nowait> <buffer> <F1> :call <SID>ToggleHelp()<CR>
  423. nnoremap <script> <silent> <nowait> <buffer> <s-cr> :call <SID>SelectBuffer("tab")<CR>
  424. nnoremap <script> <silent> <nowait> <buffer> B :call <SID>ToggleOnlyOneTab()<CR>
  425. nnoremap <script> <silent> <nowait> <buffer> b :call <SID>SelectBuffer("ask")<CR>
  426. nnoremap <script> <silent> <nowait> <buffer> d :call <SID>RemoveBuffer("delete")<CR>
  427. xnoremap <script> <silent> <nowait> <buffer> d :call <SID>RemoveBuffer("delete")<CR>
  428. nnoremap <script> <silent> <nowait> <buffer> D :call <SID>RemoveBuffer("wipe")<CR>
  429. xnoremap <script> <silent> <nowait> <buffer> D :call <SID>RemoveBuffer("wipe")<CR>
  430. nnoremap <script> <silent> <nowait> <buffer> f :call <SID>ToggleFindActive()<CR>
  431. nnoremap <script> <silent> <nowait> <buffer> m :call <SID>MRUListShow()<CR>
  432. nnoremap <script> <silent> <nowait> <buffer> o :call <SID>SelectBuffer()<CR>
  433. nnoremap <script> <silent> <nowait> <buffer> p :call <SID>ToggleSplitOutPathName()<CR>
  434. nnoremap <script> <silent> <nowait> <buffer> q :call <SID>Close()<CR>
  435. nnoremap <script> <silent> <nowait> <buffer> r :call <SID>SortReverse()<CR>
  436. nnoremap <script> <silent> <nowait> <buffer> R :call <SID>ToggleShowRelativePath()<CR>
  437. nnoremap <script> <silent> <nowait> <buffer> s :call <SID>SortSelect()<CR>
  438. nnoremap <script> <silent> <nowait> <buffer> S :call <SID>ReverseSortSelect()<CR>
  439. nnoremap <script> <silent> <nowait> <buffer> t :call <SID>SelectBuffer("tab")<CR>
  440. nnoremap <script> <silent> <nowait> <buffer> T :call <SID>ToggleShowTabBuffer()<CR>
  441. nnoremap <script> <silent> <nowait> <buffer> u :call <SID>ToggleShowUnlisted()<CR>
  442. for k in ["G", "n", "N", "L", "M", "H"]
  443. execute "nnoremap <buffer> <silent>" k ":keepjumps normal!" k."<CR>"
  444. endfor
  445. endfunction
  446. " SetupSyntax {{{2
  447. function! s:SetupSyntax()
  448. if has("syntax")
  449. syn match bufExplorerHelp "^\".*" contains=bufExplorerSortBy,bufExplorerMapping,bufExplorerTitle,bufExplorerSortType,bufExplorerToggleSplit,bufExplorerToggleOpen
  450. syn match bufExplorerOpenIn "Open in \w\+ window" contained
  451. syn match bufExplorerSplit "\w\+ split" contained
  452. syn match bufExplorerSortBy "Sorted by .*" contained contains=bufExplorerOpenIn,bufExplorerSplit
  453. syn match bufExplorerMapping "\" \zs.\+\ze :" contained
  454. syn match bufExplorerTitle "Buffer Explorer.*" contained
  455. syn match bufExplorerSortType "'\w\{-}'" contained
  456. syn match bufExplorerBufNbr /^\s*\d\+/
  457. syn match bufExplorerToggleSplit "toggle split type" contained
  458. syn match bufExplorerToggleOpen "toggle open mode" contained
  459. syn match bufExplorerModBuf /^\s*\d\+.\{4}+.*/
  460. syn match bufExplorerLockedBuf /^\s*\d\+.\{3}[\-=].*/
  461. syn match bufExplorerHidBuf /^\s*\d\+.\{2}h.*/
  462. syn match bufExplorerActBuf /^\s*\d\+.\{2}a.*/
  463. syn match bufExplorerCurBuf /^\s*\d\+.%.*/
  464. syn match bufExplorerAltBuf /^\s*\d\+.#.*/
  465. syn match bufExplorerUnlBuf /^\s*\d\+u.*/
  466. syn match bufExplorerInactBuf /^\s*\d\+ \{7}.*/
  467. hi def link bufExplorerBufNbr Number
  468. hi def link bufExplorerMapping NonText
  469. hi def link bufExplorerHelp Special
  470. hi def link bufExplorerOpenIn Identifier
  471. hi def link bufExplorerSortBy String
  472. hi def link bufExplorerSplit NonText
  473. hi def link bufExplorerTitle NonText
  474. hi def link bufExplorerSortType bufExplorerSortBy
  475. hi def link bufExplorerToggleSplit bufExplorerSplit
  476. hi def link bufExplorerToggleOpen bufExplorerOpenIn
  477. hi def link bufExplorerActBuf Identifier
  478. hi def link bufExplorerAltBuf String
  479. hi def link bufExplorerCurBuf Type
  480. hi def link bufExplorerHidBuf Constant
  481. hi def link bufExplorerLockedBuf Special
  482. hi def link bufExplorerModBuf Exception
  483. hi def link bufExplorerUnlBuf Comment
  484. hi def link bufExplorerInactBuf Comment
  485. endif
  486. endfunction
  487. " ToggleHelp {{{2
  488. function! s:ToggleHelp()
  489. let g:bufExplorerDetailedHelp = !g:bufExplorerDetailedHelp
  490. setlocal modifiable
  491. " Save position.
  492. normal! ma
  493. " Remove old header.
  494. if s:firstBufferLine > 1
  495. execute "keepjumps 1,".(s:firstBufferLine - 1) "d _"
  496. endif
  497. call append(0, s:CreateHelp())
  498. silent! normal! g`a
  499. delmarks a
  500. setlocal nomodifiable
  501. if exists("b:displayMode") && b:displayMode == "winmanager"
  502. call WinManagerForceReSize("BufExplorer")
  503. endif
  504. endfunction
  505. " GetHelpStatus {{{2
  506. function! s:GetHelpStatus()
  507. let ret = '" Sorted by '.((g:bufExplorerReverseSort == 1) ? "reverse " : "").g:bufExplorerSortBy
  508. let ret .= ' | '.((g:bufExplorerFindActive == 0) ? "Don't " : "")."Locate buffer"
  509. let ret .= ((g:bufExplorerShowUnlisted == 0) ? "" : " | Show unlisted")
  510. let ret .= ((g:bufExplorerShowTabBuffer == 0) ? "" : " | Show buffers/tab")
  511. let ret .= ((g:bufExplorerOnlyOneTab == 0) ? "" : " | One tab/buffer")
  512. let ret .= ' | '.((g:bufExplorerShowRelativePath == 0) ? "Absolute" : "Relative")
  513. let ret .= ' '.((g:bufExplorerSplitOutPathName == 0) ? "Full" : "Split")." path"
  514. return ret
  515. endfunction
  516. " CreateHelp {{{2
  517. function! s:CreateHelp()
  518. if g:bufExplorerDefaultHelp == 0 && g:bufExplorerDetailedHelp == 0
  519. let s:firstBufferLine = 1
  520. return []
  521. endif
  522. let header = []
  523. if g:bufExplorerDetailedHelp == 1
  524. call add(header, '" Buffer Explorer ('.g:bufexplorer_version.')')
  525. call add(header, '" --------------------------')
  526. call add(header, '" <F1> : toggle this help')
  527. call add(header, '" <enter> or o or Mouse-Double-Click : open buffer under cursor')
  528. call add(header, '" <shift-enter> or t : open buffer in another tab')
  529. call add(header, '" B : toggle if to save/use recent tab or not')
  530. call add(header, '" d : delete buffer')
  531. call add(header, '" D : wipe buffer')
  532. call add(header, '" f : toggle find active buffer')
  533. call add(header, '" p : toggle splitting of file and path name')
  534. call add(header, '" q : quit')
  535. call add(header, '" r : reverse sort')
  536. call add(header, '" R : toggle showing relative or full paths')
  537. call add(header, '" s : cycle thru "sort by" fields '.string(s:sort_by).'')
  538. call add(header, '" S : reverse cycle thru "sort by" fields')
  539. call add(header, '" T : toggle if to show only buffers for this tab or not')
  540. call add(header, '" u : toggle showing unlisted buffers')
  541. else
  542. call add(header, '" Press <F1> for Help')
  543. endif
  544. if (!exists("b:displayMode") || b:displayMode != "winmanager") || (b:displayMode == "winmanager" && g:bufExplorerDetailedHelp == 1)
  545. call add(header, s:GetHelpStatus())
  546. call add(header, '"=')
  547. endif
  548. let s:firstBufferLine = len(header) + 1
  549. return header
  550. endfunction
  551. " GetBufferInfo {{{2
  552. function! s:GetBufferInfo(bufnr)
  553. redir => bufoutput
  554. " Show all buffers including the unlisted ones. [!] tells Vim to show the
  555. " unlisted ones.
  556. buffers!
  557. redir END
  558. if a:bufnr > 0
  559. " Since we are only interested in this specified buffer
  560. " remove the other buffers listed
  561. let bufoutput = substitute(bufoutput."\n", '^.*\n\(\s*'.a:bufnr.'\>.\{-}\)\n.*', '\1', '')
  562. endif
  563. let [all, allwidths, listedwidths] = [[], {}, {}]
  564. for n in keys(s:types)
  565. let allwidths[n] = []
  566. let listedwidths[n] = []
  567. endfor
  568. " Loop over each line in the buffer.
  569. for buf in split(bufoutput, '\n')
  570. let bits = split(buf, '"')
  571. " Use first and last components after the split on '"', in case a
  572. " filename with an embedded '"' is present.
  573. let b = {"attributes": bits[0], "line": substitute(bits[-1], '\s*', '', '')}
  574. let name = bufname(str2nr(b.attributes))
  575. let b["hasNoName"] = empty(name)
  576. if b.hasNoName
  577. let name = "[No Name]"
  578. endif
  579. for [key, val] in items(s:types)
  580. let b[key] = fnamemodify(name, val)
  581. endfor
  582. if getftype(b.fullname) == "dir" && g:bufExplorerShowDirectories == 1
  583. let b.shortname = "<DIRECTORY>"
  584. endif
  585. call add(all, b)
  586. for n in keys(s:types)
  587. call add(allwidths[n], s:StringWidth(b[n]))
  588. if b.attributes !~ "u"
  589. call add(listedwidths[n], s:StringWidth(b[n]))
  590. endif
  591. endfor
  592. endfor
  593. let [s:allpads, s:listedpads] = [{}, {}]
  594. for n in keys(s:types)
  595. let s:allpads[n] = repeat(' ', max(allwidths[n]))
  596. let s:listedpads[n] = repeat(' ', max(listedwidths[n]))
  597. endfor
  598. return all
  599. endfunction
  600. " BuildBufferList {{{2
  601. function! s:BuildBufferList()
  602. let lines = []
  603. " Loop through every buffer.
  604. for buf in s:raw_buffer_listing
  605. " Skip unlisted buffers if we are not to show them.
  606. if !g:bufExplorerShowUnlisted && buf.attributes =~ "u"
  607. " Skip unlisted buffers if we are not to show them.
  608. continue
  609. endif
  610. " Skip "No Name" buffers if we are not to show them.
  611. if g:bufExplorerShowNoName == 0 && buf.hasNoName
  612. continue
  613. endif
  614. " Are we to show only buffer(s) for this tab?
  615. if g:bufExplorerShowTabBuffer && (!s:IsInCurrentTab(str2nr(buf.attributes)))
  616. continue
  617. endif
  618. let line = buf.attributes." "
  619. " Are we to split the path and file name?
  620. if g:bufExplorerSplitOutPathName
  621. let type = (g:bufExplorerShowRelativePath) ? "relativepath" : "path"
  622. let path = buf[type]
  623. let pad = (g:bufExplorerShowUnlisted) ? s:allpads.shortname : s:listedpads.shortname
  624. let line .= buf.shortname." ".strpart(pad.path, s:StringWidth(buf.shortname))
  625. else
  626. let type = (g:bufExplorerShowRelativePath) ? "relativename" : "fullname"
  627. let path = buf[type]
  628. let line .= path
  629. endif
  630. let pads = (g:bufExplorerShowUnlisted) ? s:allpads : s:listedpads
  631. if !empty(pads[type])
  632. let line .= strpart(pads[type], s:StringWidth(path))." "
  633. endif
  634. let line .= buf.line
  635. call add(lines, line)
  636. endfor
  637. call setline(s:firstBufferLine, lines)
  638. call s:SortListing()
  639. endfunction
  640. " SelectBuffer {{{2
  641. function! s:SelectBuffer(...)
  642. " Sometimes messages are not cleared when we get here so it looks like an
  643. " error has occurred when it really has not.
  644. "echo ""
  645. let _bufNbr = -1
  646. if (a:0 == 1) && (a:1 == "ask")
  647. " Ask the user for input.
  648. call inputsave()
  649. let cmd = input("Enter buffer number to switch to: ")
  650. call inputrestore()
  651. " Clear the message area from the previous prompt.
  652. redraw | echo
  653. if strlen(cmd) > 0
  654. let _bufNbr = str2nr(cmd)
  655. else
  656. call s:Error("Invalid buffer number, try again.")
  657. return
  658. endif
  659. else
  660. " Are we on a line with a file name?
  661. if line('.') < s:firstBufferLine
  662. execute "normal! \<CR>"
  663. return
  664. endif
  665. let _bufNbr = str2nr(getline('.'))
  666. " Check and see if we are running BufferExplorer via WinManager.
  667. if exists("b:displayMode") && b:displayMode == "winmanager"
  668. let _bufName = expand("#"._bufNbr.":p")
  669. if (a:0 == 1) && (a:1 == "tab")
  670. call WinManagerFileEdit(_bufName, 1)
  671. else
  672. call WinManagerFileEdit(_bufName, 0)
  673. endif
  674. return
  675. endif
  676. endif
  677. if bufexists(_bufNbr)
  678. if bufnr("#") == _bufNbr && !exists("g:bufExplorerChgWin")
  679. return s:Close()
  680. endif
  681. " Are we suppose to open the selected buffer in a tab?
  682. if (a:0 == 1) && (a:1 == "tab")
  683. " Yes, we are to open the selected buffer in a tab.
  684. " Restore [BufExplorer] buffer.
  685. execute "silent buffer!".s:originBuffer
  686. " Get the tab number where this buffer is located in.
  687. let tabNbr = s:GetTabNbr(_bufNbr)
  688. " Was the tab found?
  689. if tabNbr == 0
  690. " _bufNbr is not opened in any tabs. Open a new tab with the selected buffer in it.
  691. execute "999tab split +buffer" . _bufNbr
  692. " Workaround for the issue mentioned in UpdateTabBufData
  693. call s:UpdateTabBufData(_bufNbr)
  694. else
  695. " The _bufNbr is already opened in a tab, go to that tab.
  696. execute tabNbr . "tabnext"
  697. " Focus window.
  698. execute s:GetWinNbr(tabNbr, _bufNbr) . "wincmd w"
  699. endif
  700. else
  701. " No, the user did not ask to open the selected buffer in a tab.
  702. " Are we suppose to move to the tab where the active buffer is?
  703. if exists("g:bufExplorerChgWin")
  704. execute g:bufExplorerChgWin."wincmd w"
  705. elseif bufloaded(_bufNbr) && g:bufExplorerFindActive
  706. if g:bufExplorerFindActive
  707. call s:Close()
  708. endif
  709. " Get the tab number where this buffer is located in.
  710. let tabNbr = s:GetTabNbr(_bufNbr)
  711. " Was the tab found?
  712. if tabNbr != 0
  713. " Yes, the buffer is located in a tab. Go to that tab number.
  714. execute tabNbr . "tabnext"
  715. else
  716. "Nope, the buffer is not in a tab. Simply switch to that
  717. "buffer.
  718. let _bufName = expand("#"._bufNbr.":p")
  719. execute _bufName ? "drop ".escape(_bufName, " ") : "buffer "._bufNbr
  720. endif
  721. endif
  722. " Switch to the selected buffer.
  723. execute "keepalt silent b!" _bufNbr
  724. endif
  725. " Make the buffer 'listed' again.
  726. call setbufvar(_bufNbr, "&buflisted", "1")
  727. " Call any associated function references. g:bufExplorerFuncRef may be
  728. " an individual function reference or it may be a list containing
  729. " function references. It will ignore anything that's not a function
  730. " reference.
  731. "
  732. " See :help FuncRef for more on function references.
  733. if exists("g:BufExplorerFuncRef")
  734. if type(g:BufExplorerFuncRef) == 2
  735. keepj call g:BufExplorerFuncRef()
  736. elseif type(g:BufExplorerFuncRef) == 3
  737. for FncRef in g:BufExplorerFuncRef
  738. if type(FncRef) == 2
  739. keepj call FncRef()
  740. endif
  741. endfor
  742. endif
  743. endif
  744. else
  745. call s:Error("Sorry, that buffer no longer exists, please select another")
  746. call s:DeleteBuffer(_bufNbr, "wipe")
  747. endif
  748. endfunction
  749. " RemoveBuffer {{{2
  750. function! s:RemoveBuffer(mode)
  751. " Are we on a line with a file name?
  752. if line('.') < s:firstBufferLine
  753. return
  754. endif
  755. " Do not allow this buffer to be deleted if it is the last one.
  756. if len(s:MRUList) == 1
  757. call s:Error("Sorry, you are not allowed to delete the last buffer")
  758. return
  759. endif
  760. " These commands are to temporarily suspend the activity of winmanager.
  761. if exists("b:displayMode") && b:displayMode == "winmanager"
  762. call WinManagerSuspendAUs()
  763. end
  764. let _bufNbr = str2nr(getline('.'))
  765. if getbufvar(_bufNbr, '&modified') == 1
  766. call s:Error("Sorry, no write since last change for buffer "._bufNbr.", unable to delete")
  767. return
  768. else
  769. " Okay, everything is good, delete or wipe the buffer.
  770. call s:DeleteBuffer(_bufNbr, a:mode)
  771. endif
  772. " Reactivate winmanager autocommand activity.
  773. if exists("b:displayMode") && b:displayMode == "winmanager"
  774. call WinManagerForceReSize("BufExplorer")
  775. call WinManagerResumeAUs()
  776. end
  777. endfunction
  778. " DeleteBuffer {{{2
  779. function! s:DeleteBuffer(buf, mode)
  780. " This routine assumes that the buffer to be removed is on the current line.
  781. try
  782. " Wipe/Delete buffer from Vim.
  783. if a:mode == "wipe"
  784. execute "silent bwipe" a:buf
  785. else
  786. execute "silent bdelete" a:buf
  787. endif
  788. " Delete the buffer from the list on screen.
  789. setlocal modifiable
  790. normal! "_dd
  791. setlocal nomodifiable
  792. " Delete the buffer from the raw buffer list.
  793. call filter(s:raw_buffer_listing, 'v:val.attributes !~ " '.a:buf.' "')
  794. catch
  795. call s:Error(v:exception)
  796. endtry
  797. endfunction
  798. " ListedAndCurrentTab {{{2
  799. " Returns whether the specified buffer is both listed and associated
  800. " with the current tab
  801. function! s:ListedAndCurrentTab(buf)
  802. return buflisted(a:buf) && s:IsInCurrentTab(a:buf)
  803. endfunction
  804. " Close {{{2
  805. function! s:Close()
  806. " Get only the listed buffers associated with the current tab
  807. let listed = filter(copy(s:MRUList), "s:ListedAndCurrentTab(v:val)")
  808. if len(listed) == 0
  809. let listed = filter(range(1, bufnr('$')), "s:ListedAndCurrentTab(v:val)")
  810. endif
  811. " If we needed to split the main window, close the split one.
  812. if s:splitMode != "" && bufwinnr(s:originBuffer) != -1
  813. execute "wincmd c"
  814. endif
  815. " Check to see if there are anymore buffers listed.
  816. if len(listed) == 0
  817. " Since there are no buffers left to switch to, open a new empty
  818. " buffers.
  819. execute "enew"
  820. else
  821. " Since there are buffers left to switch to, switch to the previous and
  822. " then the current.
  823. for b in reverse(listed[0:1])
  824. execute "keepjumps silent b ".b
  825. endfor
  826. endif
  827. " Clear any messages.
  828. echo
  829. endfunction
  830. " ToggleSplitOutPathName {{{2
  831. function! s:ToggleSplitOutPathName()
  832. let g:bufExplorerSplitOutPathName = !g:bufExplorerSplitOutPathName
  833. call s:RebuildBufferList()
  834. call s:UpdateHelpStatus()
  835. endfunction
  836. " ToggleShowRelativePath {{{2
  837. function! s:ToggleShowRelativePath()
  838. let g:bufExplorerShowRelativePath = !g:bufExplorerShowRelativePath
  839. call s:RebuildBufferList()
  840. call s:UpdateHelpStatus()
  841. endfunction
  842. " ToggleShowTabBuffer {{{2
  843. function! s:ToggleShowTabBuffer()
  844. let g:bufExplorerShowTabBuffer = !g:bufExplorerShowTabBuffer
  845. call s:RebuildBufferList(g:bufExplorerShowTabBuffer)
  846. call s:UpdateHelpStatus()
  847. endfunction
  848. " ToggleOnlyOneTab {{{2
  849. function! s:ToggleOnlyOneTab()
  850. let g:bufExplorerOnlyOneTab = !g:bufExplorerOnlyOneTab
  851. call s:RebuildBufferList()
  852. call s:UpdateHelpStatus()
  853. endfunction
  854. " ToggleShowUnlisted {{{2
  855. function! s:ToggleShowUnlisted()
  856. let g:bufExplorerShowUnlisted = !g:bufExplorerShowUnlisted
  857. let num_bufs = s:RebuildBufferList(g:bufExplorerShowUnlisted == 0)
  858. call s:UpdateHelpStatus()
  859. endfunction
  860. " ToggleFindActive {{{2
  861. function! s:ToggleFindActive()
  862. let g:bufExplorerFindActive = !g:bufExplorerFindActive
  863. call s:UpdateHelpStatus()
  864. endfunction
  865. " RebuildBufferList {{{2
  866. function! s:RebuildBufferList(...)
  867. setlocal modifiable
  868. let curPos = getpos('.')
  869. if a:0 && a:000[0] && (line('$') >= s:firstBufferLine)
  870. " Clear the list first.
  871. execute "silent keepjumps ".s:firstBufferLine.',$d _'
  872. endif
  873. let num_bufs = s:BuildBufferList()
  874. call setpos('.', curPos)
  875. setlocal nomodifiable
  876. return num_bufs
  877. endfunction
  878. " UpdateHelpStatus {{{2
  879. function! s:UpdateHelpStatus()
  880. setlocal modifiable
  881. let text = s:GetHelpStatus()
  882. call setline(s:firstBufferLine - 2, text)
  883. setlocal nomodifiable
  884. endfunction
  885. " MRUCmp {{{2
  886. function! s:MRUCmp(line1, line2)
  887. return index(s:MRUList, str2nr(a:line1)) - index(s:MRUList, str2nr(a:line2))
  888. endfunction
  889. " SortReverse {{{2
  890. function! s:SortReverse()
  891. let g:bufExplorerReverseSort = !g:bufExplorerReverseSort
  892. call s:ReSortListing()
  893. endfunction
  894. " SortSelect {{{2
  895. function! s:SortSelect()
  896. let g:bufExplorerSortBy = get(s:sort_by, index(s:sort_by, g:bufExplorerSortBy) + 1, s:sort_by[0])
  897. call s:ReSortListing()
  898. endfunction
  899. " ReverseSortSelect {{{2
  900. function! s:ReverseSortSelect()
  901. let g:bufExplorerSortBy = get(s:sort_by, index(s:sort_by, g:bufExplorerSortBy) - 1, s:sort_by[-1])
  902. call s:ReSortListing()
  903. endfunction
  904. " ReSortListing {{{2
  905. function! s:ReSortListing()
  906. setlocal modifiable
  907. let curPos = getpos('.')
  908. call s:SortListing()
  909. call s:UpdateHelpStatus()
  910. call setpos('.', curPos)
  911. setlocal nomodifiable
  912. endfunction
  913. " SortListing {{{2
  914. function! s:SortListing()
  915. let sort = s:firstBufferLine.",$sort".((g:bufExplorerReverseSort == 1) ? "!": "")
  916. if g:bufExplorerSortBy == "number"
  917. " Easiest case.
  918. execute sort 'n'
  919. elseif g:bufExplorerSortBy == "name"
  920. " Sort by full path first
  921. execute sort 'ir /\zs\f\+\ze\s\+line/'
  922. if g:bufExplorerSplitOutPathName
  923. execute sort 'ir /\d.\{7}\zs\f\+\ze/'
  924. else
  925. execute sort 'ir /\zs[^\/\\]\+\ze\s*line/'
  926. endif
  927. elseif g:bufExplorerSortBy == "fullpath"
  928. if g:bufExplorerSplitOutPathName
  929. " Sort twice - first on the file name then on the path.
  930. execute sort 'ir /\d.\{7}\zs\f\+\ze/'
  931. endif
  932. execute sort 'ir /\zs\f\+\ze\s\+line/'
  933. elseif g:bufExplorerSortBy == "extension"
  934. " Sort by full path...
  935. execute sort 'ir /\zs\f\+\ze\s\+line/'
  936. " Sort by name...
  937. if g:bufExplorerSplitOutPathName
  938. " Sort twice - first on the file name then on the path.
  939. execute sort 'ir /\d.\{7}\zs\f\+\ze/'
  940. endif
  941. " Sort by extension.
  942. execute sort 'ir /\.\zs\w\+\ze\s/'
  943. elseif g:bufExplorerSortBy == "mru"
  944. let l = getline(s:firstBufferLine, "$")
  945. call sort(l, "<SID>MRUCmp")
  946. if g:bufExplorerReverseSort
  947. call reverse(l)
  948. endif
  949. call setline(s:firstBufferLine, l)
  950. endif
  951. endfunction
  952. " MRUListShow {{{2
  953. function! s:MRUListShow()
  954. echomsg "MRUList=".string(s:MRUList)
  955. endfunction
  956. " Error {{{2
  957. " Display a message using ErrorMsg highlight group.
  958. function! s:Error(msg)
  959. echohl ErrorMsg
  960. echomsg a:msg
  961. echohl None
  962. endfunction
  963. " Warning {{{2
  964. " Display a message using WarningMsg highlight group.
  965. function! s:Warning(msg)
  966. echohl WarningMsg
  967. echomsg a:msg
  968. echohl None
  969. endfunction
  970. " GetTabNbr {{{2
  971. function! s:GetTabNbr(bufNbr)
  972. " Searching buffer bufno, in tabs.
  973. for i in range(tabpagenr("$"))
  974. if index(tabpagebuflist(i + 1), a:bufNbr) != -1
  975. return i + 1
  976. endif
  977. endfor
  978. return 0
  979. endfunction
  980. " GetWinNbr" {{{2
  981. function! s:GetWinNbr(tabNbr, bufNbr)
  982. " window number in tabpage.
  983. let tablist = tabpagebuflist(a:tabNbr)
  984. " Number: 0
  985. " String: 1
  986. " Funcref: 2
  987. " List: 3
  988. " Dictionary: 4
  989. " Float: 5
  990. if type(tablist) == 3
  991. return index(tabpagebuflist(a:tabNbr), a:bufNbr) + 1
  992. else
  993. return 1
  994. endif
  995. endfunction
  996. " StringWidth" {{{2
  997. if exists('*strwidth')
  998. function s:StringWidth(s)
  999. return strwidth(a:s)
  1000. endfunction
  1001. else
  1002. function s:StringWidth(s)
  1003. return len(a:s)
  1004. endfunction
  1005. endif
  1006. " Winmanager Integration {{{2
  1007. let g:BufExplorer_title = "\[Buf\ List\]"
  1008. call s:Set("g:bufExplorerResize", 1)
  1009. call s:Set("g:bufExplorerMaxHeight", 25) " Handles dynamic resizing of the window.
  1010. " function! to start display. Set the mode to 'winmanager' for this buffer.
  1011. " This is to figure out how this plugin was called. In a standalone fashion
  1012. " or by winmanager.
  1013. function! BufExplorer_Start()
  1014. let b:displayMode = "winmanager"
  1015. call s:SetLocalSettings()
  1016. call BufExplorer()
  1017. endfunction
  1018. " Returns whether the display is okay or not.
  1019. function! BufExplorer_IsValid()
  1020. return 0
  1021. endfunction
  1022. " Handles dynamic refreshing of the window.
  1023. function! BufExplorer_Refresh()
  1024. let b:displayMode = "winmanager"
  1025. call s:SetLocalSettings()
  1026. call BufExplorer()
  1027. endfunction
  1028. function! BufExplorer_ReSize()
  1029. if !g:bufExplorerResize
  1030. return
  1031. end
  1032. let nlines = min([line("$"), g:bufExplorerMaxHeight])
  1033. execute nlines." wincmd _"
  1034. " The following lines restore the layout so that the last file line is also
  1035. " the last window line. Sometimes, when a line is deleted, although the
  1036. " window size is exactly equal to the number of lines in the file, some of
  1037. " the lines are pushed up and we see some lagging '~'s.
  1038. let pres = getpos(".")
  1039. normal! $
  1040. let _scr = &scrolloff
  1041. let &scrolloff = 0
  1042. normal! z-
  1043. let &scrolloff = _scr
  1044. call setpos(".", pres)
  1045. endfunction
  1046. " Default values {{{2
  1047. call s:Set("g:bufExplorerDisableDefaultKeyMapping", 0) " Do not disable default key mappings.
  1048. call s:Set("g:bufExplorerDefaultHelp", 1) " Show default help?
  1049. call s:Set("g:bufExplorerDetailedHelp", 0) " Show detailed help?
  1050. call s:Set("g:bufExplorerFindActive", 1) " When selecting an active buffer, take you to the window where it is active?
  1051. call s:Set("g:bufExplorerOnlyOneTab", 1) " If ShowTabBuffer = 1, only store the most recent tab for this buffer.
  1052. call s:Set("g:bufExplorerReverseSort", 0) " Sort in reverse order by default?
  1053. call s:Set("g:bufExplorerShowDirectories", 1) " (Dir's are added by commands like ':e .')
  1054. call s:Set("g:bufExplorerShowRelativePath", 0) " Show listings with relative or absolute paths?
  1055. call s:Set("g:bufExplorerShowTabBuffer", 0) " Show only buffer(s) for this tab?
  1056. call s:Set("g:bufExplorerShowUnlisted", 0) " Show unlisted buffers?
  1057. call s:Set("g:bufExplorerShowNoName", 0) " Show 'No Name' buffers?
  1058. call s:Set("g:bufExplorerSortBy", "mru") " Sorting methods are in s:sort_by:
  1059. call s:Set("g:bufExplorerSplitBelow", &splitbelow) " Should horizontal splits be below or above current window?
  1060. call s:Set("g:bufExplorerSplitOutPathName", 1) " Split out path and file name?
  1061. call s:Set("g:bufExplorerSplitRight", &splitright) " Should vertical splits be on the right or left of current window?
  1062. call s:Set("g:bufExplorerSplitVertSize", 0) " Height for a vertical split. If <=0, default Vim size is used.
  1063. call s:Set("g:bufExplorerSplitHorzSize", 0) " Height for a horizontal split. If <=0, default Vim size is used.
  1064. " Default key mapping {{{2
  1065. if !hasmapto('BufExplorer') && g:bufExplorerDisableDefaultKeyMapping == 0
  1066. nnoremap <script> <silent> <unique> <Leader>be :BufExplorer<CR>
  1067. endif
  1068. if !hasmapto('ToggleBufExplorer') && g:bufExplorerDisableDefaultKeyMapping == 0
  1069. nnoremap <script> <silent> <unique> <Leader>bt :ToggleBufExplorer<CR>
  1070. endif
  1071. if !hasmapto('BufExplorerHorizontalSplit') && g:bufExplorerDisableDefaultKeyMapping == 0
  1072. nnoremap <script> <silent> <unique> <Leader>bs :BufExplorerHorizontalSplit<CR>
  1073. endif
  1074. if !hasmapto('BufExplorerVerticalSplit') && g:bufExplorerDisableDefaultKeyMapping == 0
  1075. nnoremap <script> <silent> <unique> <Leader>bv :BufExplorerVerticalSplit<CR>
  1076. endif
  1077. " vim:ft=vim foldmethod=marker sw=4