bufexplorer.vim 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. "============================================================================
  2. " Copyright: Copyright (c) 2001-2025, 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 (my name at gmail dot com)
  39. " Last Changed: Wednesday, 05 March 2025
  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.8.0"
  76. " Plugin Code {{{1
  77. " Check for Vim version {{{2
  78. if !exists("g:bufExplorerVersionWarn")
  79. let g:bufExplorerVersionWarn = 1
  80. endif
  81. " Make sure we are using the correct version of Vim. If not, do not load the
  82. " plugin.
  83. if v:version < 704
  84. if g:bufExplorerVersionWarn
  85. echohl WarningMsg
  86. echo "Sorry, bufexplorer ".g:bufexplorer_version." required Vim 7.4 or greater."
  87. echohl None
  88. endif
  89. finish
  90. endif
  91. " Create commands {{{2
  92. command! BufExplorer :call BufExplorer()
  93. command! ToggleBufExplorer :call ToggleBufExplorer()
  94. command! BufExplorerHorizontalSplit :call BufExplorerHorizontalSplit()
  95. command! BufExplorerVerticalSplit :call BufExplorerVerticalSplit()
  96. " Set {{{2
  97. function! s:Set(var, default)
  98. if !exists(a:var)
  99. if type(a:default)
  100. execute "let" a:var "=" string(a:default)
  101. else
  102. execute "let" a:var "=" a:default
  103. endif
  104. return 1
  105. endif
  106. return 0
  107. endfunction
  108. " Script variables {{{2
  109. let s:MRU_Exclude_List = ["[BufExplorer]","__MRU_Files__","[Buf\ List]"]
  110. let s:name = '[BufExplorer]'
  111. let s:originBuffer = 0
  112. " Buffer number of the BufExplorer window.
  113. let s:bufExplorerBuffer = 0
  114. let s:running = 0
  115. let s:sort_by = ["number", "name", "fullpath", "mru", "extension"]
  116. let s:splitMode = ""
  117. let s:didSplit = 0
  118. let s:types = ["fullname", "homename", "path", "relativename", "relativepath", "shortname"]
  119. " Setup the autocommands that handle stuff. {{{2
  120. augroup BufExplorer
  121. autocmd!
  122. autocmd WinEnter * call s:DoWinEnter()
  123. autocmd BufEnter * call s:DoBufEnter()
  124. autocmd BufDelete * call s:DoBufDelete()
  125. if exists('##TabClosed')
  126. autocmd TabClosed * call s:DoTabClosed()
  127. endif
  128. autocmd BufWinEnter \[BufExplorer\] call s:Initialize()
  129. autocmd BufWinLeave \[BufExplorer\] call s:Cleanup()
  130. augroup END
  131. " AssignTabId {{{2
  132. " Assign a `tabId` to the given tab.
  133. function! s:AssignTabId(tabNbr)
  134. " Create a unique `tabId` based on the current time and an incrementing
  135. " counter value that helps ensure uniqueness.
  136. let tabId = reltimestr(reltime()) . ':' . s:tabIdCounter
  137. call settabvar(a:tabNbr, 'bufexp_tabId', tabId)
  138. let s:tabIdCounter = (s:tabIdCounter + 1) % 1000000000
  139. return tabId
  140. endfunction
  141. let s:tabIdCounter = 0
  142. " GetTabId {{{2
  143. " Retrieve the `tabId` for the given tab (or '' if the tab has no `tabId`).
  144. function! s:GetTabId(tabNbr)
  145. return gettabvar(a:tabNbr, 'bufexp_tabId', '')
  146. endfunction
  147. " MRU data structure {{{2
  148. " An MRU data structure is a dictionary that holds a circular doubly linked list
  149. " of `item` values. The dictionary contains three keys:
  150. " 'head': a sentinel `item` representing the head of the list.
  151. " 'next': a dictionary mapping an `item` to the next `item` in the list.
  152. " 'prev': a dictionary mapping an `item` to the previous `item` in the list.
  153. " E.g., an MRU holding buffer numbers will use `0` (an invalid buffer number) as
  154. " `head`. With the buffer numbers `1`, `2`, and `3`, an example MRU would be:
  155. "
  156. " +--<---------<---------<---------<---------<+
  157. " `next` | |
  158. " +--> +---+ --> +---+ --> +---+ --> +---+ -->+
  159. " `head` | 0 | | 1 | | 2 | | 3 |
  160. " +<-- +---+ <-- +---+ <-- +---+ <-- +---+ <--+
  161. " `prev` | |
  162. " +->-------->--------->--------->--------->--+
  163. "
  164. " `head` allows the chosen sentinel item to differ in value and type; for
  165. " example, `head` could be the string '.', allowing an MRU of strings (such as
  166. " for `TabId` values).
  167. "
  168. " Note that dictionary keys are always strings. Integers may be used, but they
  169. " are converted to strings when used (and `keys(theDictionary)` will be a
  170. " list of strings, not of integers).
  171. " MRUNew {{{2
  172. function! s:MRUNew(head)
  173. let [next, prev] = [{}, {}]
  174. let next[a:head] = a:head
  175. let prev[a:head] = a:head
  176. return { 'head': a:head, 'next': next, 'prev': prev }
  177. endfunction
  178. " MRULen {{{2
  179. function! s:MRULen(mru)
  180. " Do not include the always-present `mru.head` item.
  181. return len(a:mru.next) - 1
  182. endfunction
  183. " MRURemoveMustExist {{{2
  184. " `item` must exist in `mru`.
  185. function! s:MRURemoveMustExist(mru, item)
  186. let [next, prev] = [a:mru.next, a:mru.prev]
  187. let prevItem = prev[a:item]
  188. let nextItem = next[a:item]
  189. let next[prevItem] = nextItem
  190. let prev[nextItem] = prevItem
  191. unlet next[a:item]
  192. unlet prev[a:item]
  193. endfunction
  194. " MRURemove {{{2
  195. " `item` need not exist in `mru`.
  196. function! s:MRURemove(mru, item)
  197. if has_key(a:mru.next, a:item)
  198. call s:MRURemoveMustExist(a:mru, a:item)
  199. endif
  200. endfunction
  201. " MRUAdd {{{2
  202. function! s:MRUAdd(mru, item)
  203. let [next, prev] = [a:mru.next, a:mru.prev]
  204. let prevItem = a:mru.head
  205. let nextItem = next[prevItem]
  206. if a:item != nextItem
  207. call s:MRURemove(a:mru, a:item)
  208. let next[a:item] = nextItem
  209. let prev[a:item] = prevItem
  210. let next[prevItem] = a:item
  211. let prev[nextItem] = a:item
  212. endif
  213. endfunction
  214. " MRUGetItems {{{2
  215. " Return list of up to `maxItems` items in MRU order.
  216. " `maxItems == 0` => unlimited.
  217. function! s:MRUGetItems(mru, maxItems)
  218. let [head, next] = [a:mru.head, a:mru.next]
  219. let items = []
  220. let item = next[head]
  221. while item != head
  222. if a:maxItems > 0 && len(items) >= a:maxItems
  223. break
  224. endif
  225. call add(items, item)
  226. let item = next[item]
  227. endwhile
  228. return items
  229. endfunction
  230. " MRUGetOrdering {{{2
  231. " Return dictionary mapping up to `maxItems` from `item` to MRU order.
  232. " `maxItems == 0` => unlimited.
  233. function! s:MRUGetOrdering(mru, maxItems)
  234. let [head, next] = [a:mru.head, a:mru.next]
  235. let items = {}
  236. let order = 0
  237. let item = next[head]
  238. while item != head
  239. if a:maxItems > 0 && order >= a:maxItems
  240. break
  241. endif
  242. let items[item] = order
  243. let order = order + 1
  244. let item = next[item]
  245. endwhile
  246. return items
  247. endfunction
  248. " MRU trackers {{{2
  249. " `.head` value for tab MRU:
  250. let s:tabIdHead = '.'
  251. " Track MRU buffers globally (independent of tabs).
  252. let s:bufMru = s:MRUNew(0)
  253. " Track MRU buffers for each tab, indexed by `tabId`.
  254. " `s:bufMruByTab[tabId] -> MRU structure`.
  255. let s:bufMruByTab = {}
  256. " Track MRU tabs for each buffer, indexed by `bufNbr`.
  257. " `s:tabMruByBuf[burNbr] -> MRU structure`.
  258. let s:tabMruByBuf = {}
  259. " MRURemoveBuf {{{2
  260. function! s:MRURemoveBuf(bufNbr)
  261. call s:MRURemove(s:bufMru, a:bufNbr)
  262. if has_key(s:tabMruByBuf, a:bufNbr)
  263. let mru = s:tabMruByBuf[a:bufNbr]
  264. let [head, next] = [mru.head, mru.next]
  265. let tabId = next[head]
  266. while tabId != head
  267. call s:MRURemoveMustExist(s:bufMruByTab[tabId], a:bufNbr)
  268. let tabId = next[tabId]
  269. endwhile
  270. unlet s:tabMruByBuf[a:bufNbr]
  271. endif
  272. endfunction
  273. " MRURemoveTab {{{2
  274. function! s:MRURemoveTab(tabId)
  275. if has_key(s:bufMruByTab, a:tabId)
  276. let mru = s:bufMruByTab[a:tabId]
  277. let [head, next] = [mru.head, mru.next]
  278. let bufNbr = next[head]
  279. while bufNbr != head
  280. call s:MRURemoveMustExist(s:tabMruByBuf[bufNbr], a:tabId)
  281. let bufNbr = next[bufNbr]
  282. endwhile
  283. unlet s:bufMruByTab[a:tabId]
  284. endif
  285. endfunction
  286. " MRUAddBufTab {{{2
  287. function! s:MRUAddBufTab(bufNbr, tabId)
  288. if s:ShouldIgnore(a:bufNbr)
  289. return
  290. endif
  291. call s:MRUAdd(s:bufMru, a:bufNbr)
  292. if !has_key(s:bufMruByTab, a:tabId)
  293. let s:bufMruByTab[a:tabId] = s:MRUNew(0)
  294. endif
  295. let bufMru = s:bufMruByTab[a:tabId]
  296. call s:MRUAdd(bufMru, a:bufNbr)
  297. if !has_key(s:tabMruByBuf, a:bufNbr)
  298. let s:tabMruByBuf[a:bufNbr] = s:MRUNew(s:tabIdHead)
  299. endif
  300. let tabMru = s:tabMruByBuf[a:bufNbr]
  301. call s:MRUAdd(tabMru, a:tabId)
  302. endfunction
  303. " MRUTabForBuf {{{2
  304. " Return `tabId` most recently used by `bufNbr`.
  305. " If no `tabId` is found for `bufNbr`, return `s:tabIdHead`.
  306. function! s:MRUTabForBuf(bufNbr)
  307. let tabMru = get(s:tabMruByBuf, a:bufNbr, s:alwaysEmptyTabMru)
  308. return tabMru.next[tabMru.head]
  309. endfunction
  310. " An always-empty MRU for tabs as a default when looking up
  311. " `s:tabMruByBuf[bufNbr]` for an unknown `bufNbr`.
  312. let s:alwaysEmptyTabMru = s:MRUNew(s:tabIdHead)
  313. " MRUTabHasSeenBuf {{{2
  314. " Return true if `tabId` has ever seen `bufNbr`.
  315. function! s:MRUTabHasSeenBuf(tabId, bufNbr)
  316. let mru = get(s:bufMruByTab, a:tabId, s:alwaysEmptyBufMru)
  317. return has_key(mru.next, a:bufNbr)
  318. endfunction
  319. " MRUTabShouldShowBuf {{{2
  320. " Return true if `tabId` should show `bufNbr`.
  321. " This is a function of current display modes.
  322. function! s:MRUTabShouldShowBuf(tabId, bufNbr)
  323. if !g:bufExplorerShowTabBuffer
  324. " We are showing buffers from all tabs.
  325. return 1
  326. elseif g:bufExplorerOnlyOneTab
  327. " We are showing buffers that were most recently seen in this tab.
  328. return s:MRUTabForBuf(a:bufNbr) == a:tabId
  329. else
  330. " We are showing buffers that have ever been seen in this tab.
  331. return s:MRUTabHasSeenBuf(a:tabId, a:bufNbr)
  332. endif
  333. endfunction
  334. " MRUListedBuffersForTab {{{2
  335. " Return list of up to `maxBuffers` listed buffers in MRU order for the tab.
  336. " `maxBuffers == 0` => unlimited.
  337. function! s:MRUListedBuffersForTab(tabId, maxBuffers)
  338. let bufNbrs = []
  339. let mru = get(s:bufMruByTab, a:tabId, s:alwaysEmptyBufMru)
  340. let [head, next] = [mru.head, mru.next]
  341. let bufNbr = next[head]
  342. while bufNbr != head
  343. if a:maxBuffers > 0 && len(bufNbrs) >= a:maxBuffers
  344. break
  345. endif
  346. if buflisted(bufNbr) && s:MRUTabShouldShowBuf(a:tabId, bufNbr)
  347. call add(bufNbrs, bufNbr)
  348. endif
  349. let bufNbr = next[bufNbr]
  350. endwhile
  351. return bufNbrs
  352. endfunction
  353. " An always-empty MRU for buffers as a default when looking up
  354. " `s:bufMruByTab[tabId]` for an unknown `tabId`.
  355. let s:alwaysEmptyBufMru = s:MRUNew(0)
  356. " MRUOrderForBuf {{{2
  357. " Return the position of `bufNbr` in the current MRU ordering.
  358. " This is a function of the current display mode. When showing buffers from all
  359. " tabs, it's the global MRU order; otherwise, it the MRU order for the tab at
  360. " BufExplorer launch. The latter includes all buffers seen in this tab, which
  361. " is sufficient whether `g:bufExplorerOnlyOneTab` is true or false.
  362. function! s:MRUOrderForBuf(bufNbr)
  363. if !exists('s:mruOrder')
  364. if g:bufExplorerShowTabBuffer
  365. let mru = get(s:bufMruByTab, s:tabIdAtLaunch, s:alwaysEmptyBufMru)
  366. else
  367. let mru = s:bufMru
  368. endif
  369. let s:mruOrder = s:MRUGetOrdering(mru, 0)
  370. endif
  371. return get(s:mruOrder, a:bufNbr, len(s:mruOrder))
  372. endfunction
  373. " MRUEnsureTabId {{{2
  374. function! s:MRUEnsureTabId(tabNbr)
  375. let tabId = s:GetTabId(a:tabNbr)
  376. if tabId == ''
  377. let tabId = s:AssignTabId(a:tabNbr)
  378. for bufNbr in tabpagebuflist(a:tabNbr)
  379. call s:MRUAddBufTab(bufNbr, tabId)
  380. endfor
  381. endif
  382. return tabId
  383. endfunction
  384. " MRUGarbageCollectBufs {{{2
  385. " Requires `s:raw_buffer_listing`.
  386. function! s:MRUGarbageCollectBufs()
  387. for bufNbr in values(s:bufMru.next)
  388. if bufNbr != 0 && !has_key(s:raw_buffer_listing, bufNbr)
  389. call s:MRURemoveBuf(bufNbr)
  390. endif
  391. endfor
  392. endfunction
  393. " MRUGarbageCollectTabs {{{2
  394. function! s:MRUGarbageCollectTabs()
  395. let numTabs = tabpagenr('$')
  396. let liveTabIds = {}
  397. for tabNbr in range(1, numTabs)
  398. let tabId = s:GetTabId(tabNbr)
  399. if tabId != ''
  400. let liveTabIds[tabId] = 1
  401. endif
  402. endfor
  403. for tabId in keys(s:bufMruByTab)
  404. if tabId != s:tabIdHead && !has_key(liveTabIds, tabId)
  405. call s:MRURemoveTab(tabId)
  406. endif
  407. endfor
  408. endfunction
  409. " DoWinEnter {{{2
  410. function! s:DoWinEnter()
  411. let bufNbr = str2nr(expand("<abuf>"))
  412. let tabNbr = tabpagenr()
  413. let tabId = s:GetTabId(tabNbr)
  414. " Ignore `WinEnter` for a newly created tab; this event comes when creating
  415. " a new tab, and the buffer at that moment is one that is about to be
  416. " replaced by the buffer to which we are switching; this latter buffer will
  417. " be handled by the forthcoming `BufEnter` event.
  418. if tabId != ''
  419. call s:MRUAddBufTab(bufNbr, tabId)
  420. endif
  421. endfunction
  422. " DoBufEnter {{{2
  423. function! s:DoBufEnter()
  424. let bufNbr = str2nr(expand("<abuf>"))
  425. let tabNbr = tabpagenr()
  426. let tabId = s:MRUEnsureTabId(tabNbr)
  427. call s:MRUAddBufTab(bufNbr, tabId)
  428. endfunction
  429. " DoBufDelete {{{2
  430. function! s:DoBufDelete()
  431. let bufNbr = str2nr(expand("<abuf>"))
  432. call s:MRURemoveBuf(bufNbr)
  433. endfunction
  434. " DoTabClosed {{{2
  435. function! s:DoTabClosed()
  436. call s:MRUGarbageCollectTabs()
  437. endfunction
  438. " ShouldIgnore {{{2
  439. function! s:ShouldIgnore(buf)
  440. " Ignore temporary buffers with buftype set.
  441. if empty(getbufvar(a:buf, "&buftype")) == 0
  442. return 1
  443. endif
  444. " Ignore buffers with no name.
  445. if empty(bufname(a:buf)) == 1
  446. return 1
  447. endif
  448. " Ignore the BufExplorer buffer.
  449. if fnamemodify(bufname(a:buf), ":t") == s:name
  450. return 1
  451. endif
  452. " Ignore any buffers in the exclude list.
  453. if index(s:MRU_Exclude_List, bufname(a:buf)) >= 0
  454. return 1
  455. endif
  456. " Else return 0 to indicate that the buffer was not ignored.
  457. return 0
  458. endfunction
  459. " Initialize {{{2
  460. function! s:Initialize()
  461. call s:SetLocalSettings()
  462. let s:running = 1
  463. endfunction
  464. " Cleanup {{{2
  465. function! s:Cleanup()
  466. if exists("s:_insertmode")
  467. let &insertmode = s:_insertmode
  468. endif
  469. if exists("s:_showcmd")
  470. let &showcmd = s:_showcmd
  471. endif
  472. if exists("s:_cpo")
  473. let &cpo = s:_cpo
  474. endif
  475. if exists("s:_report")
  476. let &report = s:_report
  477. endif
  478. let s:running = 0
  479. let s:splitMode = ""
  480. let s:didSplit = 0
  481. delmarks!
  482. endfunction
  483. " SetLocalSettings {{{2
  484. function! s:SetLocalSettings()
  485. let s:_insertmode = &insertmode
  486. set noinsertmode
  487. let s:_showcmd = &showcmd
  488. set noshowcmd
  489. let s:_cpo = &cpo
  490. set cpo&vim
  491. let s:_report = &report
  492. let &report = 10000
  493. setlocal nonumber
  494. setlocal foldcolumn=0
  495. setlocal nofoldenable
  496. setlocal cursorline
  497. setlocal nospell
  498. setlocal nobuflisted
  499. setlocal filetype=bufexplorer
  500. endfunction
  501. " BufExplorerHorizontalSplit {{{2
  502. function! BufExplorerHorizontalSplit()
  503. let s:splitMode = "sp"
  504. execute "BufExplorer"
  505. let s:splitMode = ""
  506. endfunction
  507. " BufExplorerVerticalSplit {{{2
  508. function! BufExplorerVerticalSplit()
  509. let s:splitMode = "vsp"
  510. execute "BufExplorer"
  511. let s:splitMode = ""
  512. endfunction
  513. " ToggleBufExplorer {{{2
  514. function! ToggleBufExplorer()
  515. if exists("s:running") && s:running == 1 && bufname(winbufnr(0)) == s:name
  516. call s:Close()
  517. else
  518. call BufExplorer()
  519. endif
  520. endfunction
  521. " BufExplorer {{{2
  522. function! BufExplorer()
  523. let name = s:name
  524. if !has("win32")
  525. " On non-Windows boxes, escape the name so that is shows up correctly.
  526. let name = escape(name, "[]")
  527. endif
  528. " Make sure there is only one explorer open at a time.
  529. if s:running == 1
  530. " Go to the open buffer.
  531. if has("gui")
  532. execute "drop" name
  533. endif
  534. return
  535. endif
  536. " Add zero to ensure the variable is treated as a number.
  537. let s:originBuffer = bufnr("%") + 0
  538. let s:tabIdAtLaunch = s:MRUEnsureTabId(tabpagenr())
  539. " Forget any cached MRU ordering from previous invocations.
  540. unlet! s:mruOrder
  541. let s:raw_buffer_listing = s:GetBufferInfo(0)
  542. call s:MRUGarbageCollectBufs()
  543. call s:MRUGarbageCollectTabs()
  544. " We may have to split the current window.
  545. if s:splitMode != ""
  546. " Save off the original settings.
  547. let [_splitbelow, _splitright] = [&splitbelow, &splitright]
  548. " Set the setting to ours.
  549. let [&splitbelow, &splitright] = [g:bufExplorerSplitBelow, g:bufExplorerSplitRight]
  550. let _size = (s:splitMode == "sp") ? g:bufExplorerSplitHorzSize : g:bufExplorerSplitVertSize
  551. " Split the window either horizontally or vertically.
  552. if _size <= 0
  553. execute 'keepalt ' . s:splitMode
  554. else
  555. execute 'keepalt ' . _size . s:splitMode
  556. endif
  557. " Restore the original settings.
  558. let [&splitbelow, &splitright] = [_splitbelow, _splitright]
  559. " Remember that a split was triggered
  560. let s:didSplit = 1
  561. endif
  562. if !exists("b:displayMode") || b:displayMode != "winmanager"
  563. " Do not use keepalt when opening bufexplorer to allow the buffer that
  564. " we are leaving to become the new alternate buffer
  565. execute "silent keepjumps hide edit".name
  566. endif
  567. " Record BufExplorer's buffer number.
  568. let s:bufExplorerBuffer = bufnr('%')
  569. call s:DisplayBufferList()
  570. " Position the cursor in the newly displayed list on the line representing
  571. " the active buffer. The active buffer is the line with the '%' character
  572. " in it.
  573. execute search("%")
  574. if exists('#User#BufExplorer_Started')
  575. " Notify that BufExplorer has started. This is an opportunity to make
  576. " custom buffer-local mappings and the like.
  577. doautocmd User BufExplorer_Started
  578. endif
  579. endfunction
  580. " Tracks `tabId` at BufExplorer launch.
  581. let s:tabIdAtLaunch = ''
  582. " DisplayBufferList {{{2
  583. function! s:DisplayBufferList()
  584. setlocal buftype=nofile
  585. setlocal modifiable
  586. setlocal noreadonly
  587. setlocal noswapfile
  588. setlocal nowrap
  589. setlocal bufhidden=wipe
  590. call s:SetupSyntax()
  591. call s:MapKeys()
  592. " Wipe out any existing lines in case BufExplorer buffer exists and the
  593. " user had changed any global settings that might reduce the number of
  594. " lines needed in the buffer.
  595. silent keepjumps 1,$d _
  596. call setline(1, s:CreateHelp())
  597. call s:BuildBufferList()
  598. call cursor(s:firstBufferLine, 1)
  599. if !g:bufExplorerResize
  600. normal! zz
  601. endif
  602. setlocal nomodifiable
  603. endfunction
  604. " RedisplayBufferList {{{2
  605. function! s:RedisplayBufferList()
  606. call s:RebuildBufferList()
  607. call s:UpdateHelpStatus()
  608. endfunction
  609. " MapKeys {{{2
  610. function! s:MapKeys()
  611. if exists("b:displayMode") && b:displayMode == "winmanager"
  612. nnoremap <buffer> <silent> <tab> :call <SID>SelectBuffer()<CR>
  613. endif
  614. nnoremap <script> <silent> <nowait> <buffer> <2-leftmouse> :call <SID>SelectBuffer()<CR>
  615. nnoremap <script> <silent> <nowait> <buffer> <CR> :call <SID>SelectBuffer()<CR>
  616. nnoremap <script> <silent> <nowait> <buffer> <F1> :call <SID>ToggleHelp()<CR>
  617. nnoremap <script> <silent> <nowait> <buffer> <s-cr> :call <SID>SelectBuffer("tab")<CR>
  618. nnoremap <script> <silent> <nowait> <buffer> a :call <SID>ToggleFindActive()<CR>
  619. nnoremap <script> <silent> <nowait> <buffer> b :call <SID>SelectBuffer("ask")<CR>
  620. nnoremap <script> <silent> <nowait> <buffer> B :call <SID>ToggleOnlyOneTab()<CR>
  621. nnoremap <script> <silent> <nowait> <buffer> d :call <SID>RemoveBuffer("delete")<CR>
  622. nnoremap <script> <silent> <nowait> <buffer> D :call <SID>RemoveBuffer("wipe")<CR>
  623. nnoremap <script> <silent> <nowait> <buffer> f :call <SID>SelectBuffer("split", "sb")<CR>
  624. nnoremap <script> <silent> <nowait> <buffer> F :call <SID>SelectBuffer("split", "st")<CR>
  625. nnoremap <script> <silent> <nowait> <buffer> o :call <SID>SelectBuffer()<CR>
  626. nnoremap <script> <silent> <nowait> <buffer> p :call <SID>ToggleSplitOutPathName()<CR>
  627. nnoremap <script> <silent> <nowait> <buffer> q :call <SID>Close()<CR>
  628. nnoremap <script> <silent> <nowait> <buffer> r :call <SID>SortReverse()<CR>
  629. nnoremap <script> <silent> <nowait> <buffer> R :call <SID>ToggleShowRelativePath()<CR>
  630. nnoremap <script> <silent> <nowait> <buffer> s :call <SID>SortSelect()<CR>
  631. nnoremap <script> <silent> <nowait> <buffer> S :call <SID>ReverseSortSelect()<CR>
  632. nnoremap <script> <silent> <nowait> <buffer> t :call <SID>SelectBuffer("tab")<CR>
  633. nnoremap <script> <silent> <nowait> <buffer> T :call <SID>ToggleShowTabBuffer()<CR>
  634. nnoremap <script> <silent> <nowait> <buffer> u :call <SID>ToggleShowUnlisted()<CR>
  635. nnoremap <script> <silent> <nowait> <buffer> v :call <SID>SelectBuffer("split", "vr")<CR>
  636. nnoremap <script> <silent> <nowait> <buffer> V :call <SID>SelectBuffer("split", "vl")<CR>
  637. nnoremap <script> <silent> <nowait> <buffer> X :call <SID>ToggleShowTerminal()<CR>
  638. for k in ["G", "n", "N", "L", "M", "H"]
  639. execute "nnoremap <buffer> <silent>" k ":keepjumps normal!" k."<CR>"
  640. endfor
  641. endfunction
  642. " SetupSyntax {{{2
  643. function! s:SetupSyntax()
  644. if has("syntax")
  645. syn match bufExplorerHelp "^\".*" contains=bufExplorerSortBy,bufExplorerMapping,bufExplorerTitle,bufExplorerSortType,bufExplorerToggleSplit,bufExplorerToggleOpen
  646. syn match bufExplorerOpenIn "Open in \w\+ window" contained
  647. syn match bufExplorerSplit "\w\+ split" contained
  648. syn match bufExplorerSortBy "Sorted by .*" contained contains=bufExplorerOpenIn,bufExplorerSplit
  649. syn match bufExplorerMapping "\" \zs.\+\ze :" contained
  650. syn match bufExplorerTitle "Buffer Explorer.*" contained
  651. syn match bufExplorerSortType "'\w\{-}'" contained
  652. syn match bufExplorerBufNbr /^\s*\d\+/
  653. syn match bufExplorerToggleSplit "toggle split type" contained
  654. syn match bufExplorerToggleOpen "toggle open mode" contained
  655. syn match bufExplorerModBuf /^\s*\d\+.\{4}+.*/
  656. syn match bufExplorerLockedBuf /^\s*\d\+.\{3}[\-=].*/
  657. syn match bufExplorerHidBuf /^\s*\d\+.\{2}h.*/
  658. syn match bufExplorerActBuf /^\s*\d\+.\{2}a.*/
  659. syn match bufExplorerCurBuf /^\s*\d\+.%.*/
  660. syn match bufExplorerAltBuf /^\s*\d\+.#.*/
  661. syn match bufExplorerUnlBuf /^\s*\d\+u.*/
  662. syn match bufExplorerInactBuf /^\s*\d\+ \{7}.*/
  663. hi def link bufExplorerBufNbr Number
  664. hi def link bufExplorerMapping NonText
  665. hi def link bufExplorerHelp Special
  666. hi def link bufExplorerOpenIn Identifier
  667. hi def link bufExplorerSortBy String
  668. hi def link bufExplorerSplit NonText
  669. hi def link bufExplorerTitle NonText
  670. hi def link bufExplorerSortType bufExplorerSortBy
  671. hi def link bufExplorerToggleSplit bufExplorerSplit
  672. hi def link bufExplorerToggleOpen bufExplorerOpenIn
  673. hi def link bufExplorerActBuf Identifier
  674. hi def link bufExplorerAltBuf String
  675. hi def link bufExplorerCurBuf Type
  676. hi def link bufExplorerHidBuf Constant
  677. hi def link bufExplorerLockedBuf Special
  678. hi def link bufExplorerModBuf Exception
  679. hi def link bufExplorerUnlBuf Comment
  680. hi def link bufExplorerInactBuf Comment
  681. endif
  682. endfunction
  683. " ToggleHelp {{{2
  684. function! s:ToggleHelp()
  685. let g:bufExplorerDetailedHelp = !g:bufExplorerDetailedHelp
  686. setlocal modifiable
  687. " Save position.
  688. normal! ma
  689. " Remove old header.
  690. if s:firstBufferLine > 1
  691. execute "keepjumps 1,".(s:firstBufferLine - 1) "d _"
  692. endif
  693. call append(0, s:CreateHelp())
  694. silent! normal! g`a
  695. delmarks a
  696. setlocal nomodifiable
  697. if exists("b:displayMode") && b:displayMode == "winmanager"
  698. call WinManagerForceReSize("BufExplorer")
  699. endif
  700. endfunction
  701. " GetHelpStatus {{{2
  702. function! s:GetHelpStatus()
  703. let ret = '" Sorted by '.((g:bufExplorerReverseSort == 1) ? "reverse " : "").g:bufExplorerSortBy
  704. let ret .= ' | '.((g:bufExplorerFindActive == 0) ? "Don't " : "")."Locate buffer"
  705. let ret .= ((g:bufExplorerShowUnlisted == 0) ? "" : " | Show unlisted")
  706. let ret .= ((g:bufExplorerShowTabBuffer == 0) ? "" : " | Show buffers/tab")
  707. let ret .= ((g:bufExplorerOnlyOneTab == 0) ? "" : " | One tab/buffer")
  708. let ret .= ' | '.((g:bufExplorerShowRelativePath == 0) ? "Absolute" : "Relative")
  709. let ret .= ' '.((g:bufExplorerSplitOutPathName == 0) ? "Full" : "Split")." path"
  710. let ret .= ((g:bufExplorerShowTerminal == 0) ? "" : " | Show terminal")
  711. return ret
  712. endfunction
  713. " CreateHelp {{{2
  714. function! s:CreateHelp()
  715. if g:bufExplorerDefaultHelp == 0 && g:bufExplorerDetailedHelp == 0
  716. let s:firstBufferLine = 1
  717. return []
  718. endif
  719. let header = []
  720. if g:bufExplorerDetailedHelp == 1
  721. call add(header, '" Buffer Explorer ('.g:bufexplorer_version.')')
  722. call add(header, '" --------------------------')
  723. call add(header, '" <F1> : toggle this help')
  724. call add(header, '" <enter> or o or Mouse-Double-Click : open buffer under cursor')
  725. call add(header, '" <shift-enter> or t : open buffer in another tab')
  726. call add(header, '" a : toggle find active buffer')
  727. call add(header, '" b : Fast buffer switching with b<any bufnum>')
  728. call add(header, '" B : toggle showing buffers only on their MRU tabs')
  729. call add(header, '" d : delete buffer')
  730. call add(header, '" D : wipe buffer')
  731. call add(header, '" F : open buffer in another window above the current')
  732. call add(header, '" f : open buffer in another window below the current')
  733. call add(header, '" p : toggle splitting of file and path name')
  734. call add(header, '" q : quit')
  735. call add(header, '" r : reverse sort')
  736. call add(header, '" R : toggle showing relative or full paths')
  737. call add(header, '" s : cycle thru "sort by" fields '.string(s:sort_by).'')
  738. call add(header, '" S : reverse cycle thru "sort by" fields')
  739. call add(header, '" T : toggle showing all buffers/only buffers used on this tab')
  740. call add(header, '" u : toggle showing unlisted buffers')
  741. call add(header, '" V : open buffer in another window on the left of the current')
  742. call add(header, '" v : open buffer in another window on the right of the current')
  743. call add(header, '" X : toggle showing terminal buffers')
  744. else
  745. call add(header, '" Press <F1> for Help')
  746. endif
  747. if (!exists("b:displayMode") || b:displayMode != "winmanager") || (b:displayMode == "winmanager" && g:bufExplorerDetailedHelp == 1)
  748. call add(header, s:GetHelpStatus())
  749. call add(header, '"=')
  750. endif
  751. let s:firstBufferLine = len(header) + 1
  752. return header
  753. endfunction
  754. " CalculateBufferDetails {{{2
  755. " Calculate `buf`-related details.
  756. " Only these fields of `buf` must be defined on entry:
  757. " - `._bufnr`
  758. " - `.attributes`
  759. " - `.line`
  760. function! s:CalculateBufferDetails(buf)
  761. let buf = a:buf
  762. let name = bufname(buf._bufnr)
  763. let buf["hasNoName"] = empty(name)
  764. if buf.hasNoName
  765. let name = "[No Name]"
  766. endif
  767. let buf.isterminal = getbufvar(buf._bufnr, '&buftype') == 'terminal'
  768. if buf.isterminal
  769. " Neovim uses paths with `term://` prefix, where the provided path
  770. " is the current working directory when the terminal was launched, e.g.:
  771. " - Unix:
  772. " term://~/tmp/sort//1464953:/bin/bash
  773. " - Windows:
  774. " term://C:\apps\nvim-win64\bin//6408:C:\Windows\system32\cmd.exe
  775. " Vim uses paths starting with `!`, with no provided path, e.g.:
  776. " - Unix:
  777. " !/bin/bash
  778. " - Windows:
  779. " !C:\Windows\system32\cmd.exe
  780. " Use the terminal's current working directory as the `path`.
  781. " For `shortname`, use `!PID:shellName`, prefixed with `!` as Vim does,
  782. " and without the shell's path for brevity, e.g.:
  783. " `/bin/bash` -> `!bash`
  784. " `1464953:/bin/bash` -> `!1464953:bash`
  785. " `C:\Windows\system32\cmd.exe` -> `!cmd.exe`
  786. " `6408:C:\Windows\system32\cmd.exe` -> `!6408:cmd.exe`
  787. " Neovim-style name format:
  788. " term://(cwd)//(pid):(shellPath)
  789. " e.g.:
  790. " term://~/tmp/sort//1464953:/bin/bash
  791. " `cwd` is the directory at terminal launch.
  792. let termNameParts = matchlist(name, '\v\c^term://(.*)//(\d+):(.*)$')
  793. if len(termNameParts) > 0
  794. let [cwd, pidStr, shellPath] = termNameParts[1:3]
  795. let pid = str2nr(pidStr)
  796. let shellName = fnamemodify(shellPath, ':t')
  797. else
  798. " Default to Vim's current working directory.
  799. let cwd = '.'
  800. let shellName = fnamemodify(name, ':t')
  801. let pid = -1
  802. if exists('*term_getjob') && exists('*job_info')
  803. let job = term_getjob(buf._bufnr)
  804. if job != v:null
  805. let pid = job_info(job).process
  806. endif
  807. endif
  808. endif
  809. if pid < 0
  810. let shortname = '!' . shellName
  811. else
  812. let shortname = '!' . pid . ':' . shellName
  813. " On some systems having a `/proc` filesystem (e.g., Linux, *BSD,
  814. " Solaris), each process has a `cwd` symlink for the current working
  815. " directory. `resolve()` will return the actual current working
  816. " directory if possible; otherwise, it will return the symlink path
  817. " unchanged.
  818. let cwd_symlink = '/proc/' . pid . '/cwd'
  819. let resolved_cwd = resolve(cwd_symlink)
  820. if resolved_cwd != cwd_symlink
  821. let cwd = resolved_cwd
  822. endif
  823. endif
  824. let slashed_path = fnamemodify(cwd, ':p')
  825. let buf.fullname = slashed_path . shortname
  826. let buf.shortname = shortname
  827. let homepath = fnamemodify(slashed_path, ':~:h')
  828. let buf.path = homepath
  829. let buf.homename = fnamemodify(buf.fullname, ':~')
  830. let buf.relativepath = fnamemodify(slashed_path, ':~:.:h')
  831. let buf.relativename = fnamemodify(buf.fullname, ':~:.')
  832. return
  833. endif
  834. let buf.fullname = simplify(fnamemodify(name, ':p'))
  835. let buf.isdir = getftype(buf.fullname) == "dir"
  836. if buf.isdir
  837. " `buf.fullname` ends with a path separator; this will be
  838. " removed via the first `:h` applied to `buf.fullname` (except
  839. " for the root directory, where the path separator will remain).
  840. let parent = fnamemodify(buf.fullname, ':h:h')
  841. let buf.shortname = fnamemodify(buf.fullname, ':h:t')
  842. " Special case for root directory: fnamemodify('/', ':h:t') == ''
  843. if buf.shortname == ''
  844. let buf.shortname = '.'
  845. endif
  846. " Must perform shortening (`:~`, `:.`) before `:h`.
  847. let buf.homename = fnamemodify(buf.fullname, ':~:h')
  848. let buf.relativename = fnamemodify(buf.fullname, ':~:.:h')
  849. else
  850. let parent = fnamemodify(buf.fullname, ':h')
  851. let buf.shortname = fnamemodify(buf.fullname, ':t')
  852. let buf.homename = fnamemodify(buf.fullname, ':~')
  853. let buf.relativename = fnamemodify(buf.fullname, ':~:.')
  854. endif
  855. " `:p` on `parent` adds back the path separator which permits more
  856. " effective shortening (`:~`, `:.`), but `:h` is required afterward
  857. " to trim this separator.
  858. let buf.path = fnamemodify(parent, ':p:~:h')
  859. let buf.relativepath = fnamemodify(parent, ':p:~:.:h')
  860. endfunction
  861. " GetBufferInfo {{{2
  862. " Return dictionary `{ bufNbr : buf }`.
  863. " - If `onlyBufNbr > 0`, dictionary will contain at most that buffer.
  864. " On return, only these fields are set for each `buf`:
  865. " - `._bufnr`
  866. " - `.attributes`
  867. " - `.line`
  868. " Other fields will be populated by `s:CalculateBufferDetails()`.
  869. function! s:GetBufferInfo(onlyBufNbr)
  870. redir => bufoutput
  871. " Below, `:silent buffers` allows capturing the output via `:redir` but
  872. " prevents display to the user.
  873. if a:onlyBufNbr > 0 && buflisted(a:onlyBufNbr)
  874. " We care only about the listed buffer `a:onlyBufNbr`, so no need to
  875. " enumerate unlisted buffers.
  876. silent buffers
  877. else
  878. " Use `!` to show all buffers including the unlisted ones.
  879. silent buffers!
  880. endif
  881. redir END
  882. if a:onlyBufNbr > 0
  883. " Since we are only interested in this specified buffer remove the
  884. " other buffers listed.
  885. " Use a very-magic pattern starting with a newline and a run of zero or
  886. " more spaces/tabs:
  887. let onlyLinePattern = '\v\n\s*'
  888. " Continue with the buffer number followed by a non-digit character
  889. " (which will be a buffer attribute character such as `u` or ` `).
  890. let onlyLinePattern .= a:onlyBufNbr . '\D'
  891. " Finish with a run of zero or more non-newline characters plus newline:
  892. let onlyLinePattern .= '[^\n]*\n'
  893. let bufoutput = matchstr("\n" . bufoutput . "\n", onlyLinePattern)
  894. endif
  895. let all = {}
  896. " Loop over each line in the buffer.
  897. for line in split(bufoutput, '\n')
  898. let bits = split(line, '"')
  899. " Use first and last components after the split on '"', in case a
  900. " filename with an embedded '"' is present.
  901. let buf = {"attributes": bits[0], "line": substitute(bits[-1], '\s*', '', '')}
  902. let buf._bufnr = str2nr(buf.attributes)
  903. let all[buf._bufnr] = buf
  904. endfor
  905. return all
  906. endfunction
  907. " BuildBufferList {{{2
  908. function! s:BuildBufferList()
  909. let table = []
  910. " Loop through every buffer.
  911. for buf in values(s:raw_buffer_listing)
  912. " `buf.attributes` must exist, but we defer the expensive work of
  913. " calculating other buffer details (e.g., `buf.fullname`) until we know
  914. " the user wants to view this buffer.
  915. " Skip BufExplorer's buffer.
  916. if buf._bufnr == s:bufExplorerBuffer
  917. continue
  918. endif
  919. " Skip unlisted buffers if we are not to show them.
  920. if !g:bufExplorerShowUnlisted && buf.attributes =~ "u"
  921. " Skip unlisted buffers if we are not to show them.
  922. continue
  923. endif
  924. " Ensure buffer details are computed for this buffer.
  925. if !has_key(buf, 'fullname')
  926. call s:CalculateBufferDetails(buf)
  927. endif
  928. " Skip 'No Name' buffers if we are not to show them.
  929. if g:bufExplorerShowNoName == 0 && buf.hasNoName
  930. continue
  931. endif
  932. " Should we show this buffer in this tab?
  933. if !s:MRUTabShouldShowBuf(s:tabIdAtLaunch, buf._bufnr)
  934. continue
  935. endif
  936. " Skip terminal buffers if we are not to show them.
  937. if !g:bufExplorerShowTerminal && buf.isterminal
  938. continue
  939. endif
  940. " Skip directory buffers if we are not to show them.
  941. if !g:bufExplorerShowDirectories && buf.isdir
  942. continue
  943. endif
  944. let row = [buf.attributes]
  945. if exists("g:loaded_webdevicons")
  946. let row += [WebDevIconsGetFileTypeSymbol(buf.fullname, buf.isdir)]
  947. endif
  948. " Are we to split the path and file name?
  949. if g:bufExplorerSplitOutPathName
  950. let type = (g:bufExplorerShowRelativePath) ? "relativepath" : "path"
  951. let row += [buf.shortname, buf[type]]
  952. else
  953. let type = (g:bufExplorerShowRelativePath) ? "relativename" : "homename"
  954. let row += [buf[type]]
  955. endif
  956. let row += [buf.line]
  957. call add(table, row)
  958. endfor
  959. let lines = s:MakeLines(table)
  960. call setline(s:firstBufferLine, lines)
  961. let firstMissingLine = s:firstBufferLine + len(lines)
  962. if line('$') >= firstMissingLine
  963. " Clear excess lines starting with `firstMissingLine`.
  964. execute "silent keepjumps ".firstMissingLine.',$d _'
  965. endif
  966. call s:SortListing()
  967. endfunction
  968. " MakeLines {{{2
  969. function! s:MakeLines(table)
  970. if len(a:table) == 0
  971. return []
  972. endif
  973. let lines = []
  974. " To avoid trailing whitespace, do not pad the final column.
  975. let numColumnsToPad = len(a:table[0]) - 1
  976. let maxWidths = repeat([0], numColumnsToPad)
  977. for row in a:table
  978. let i = 0
  979. while i < numColumnsToPad
  980. let maxWidths[i] = max([maxWidths[i], s:StringWidth(row[i])])
  981. let i = i + 1
  982. endwhile
  983. endfor
  984. let pads = []
  985. for w in maxWidths
  986. call add(pads, repeat(' ', w))
  987. endfor
  988. for row in a:table
  989. let i = 0
  990. while i < numColumnsToPad
  991. let row[i] .= strpart(pads[i], s:StringWidth(row[i]))
  992. let i = i + 1
  993. endwhile
  994. call add(lines, join(row, ' '))
  995. endfor
  996. return lines
  997. endfunction
  998. " SelectBuffer {{{2
  999. function! s:SelectBuffer(...)
  1000. " Sometimes messages are not cleared when we get here so it looks like an
  1001. " error has occurred when it really has not.
  1002. "echo ""
  1003. let _bufNbr = -1
  1004. if (a:0 == 1) && (a:1 == "ask")
  1005. " Ask the user for input.
  1006. call inputsave()
  1007. let cmd = input("Enter buffer number to switch to: ")
  1008. call inputrestore()
  1009. " Clear the message area from the previous prompt.
  1010. redraw | echo
  1011. if strlen(cmd) > 0
  1012. let _bufNbr = str2nr(cmd)
  1013. else
  1014. call s:Error("Invalid buffer number, try again.")
  1015. return
  1016. endif
  1017. else
  1018. " Are we on a line with a file name?
  1019. if line('.') < s:firstBufferLine
  1020. execute "normal! \<CR>"
  1021. return
  1022. endif
  1023. let _bufNbr = str2nr(getline('.'))
  1024. " Check and see if we are running BufferExplorer via WinManager.
  1025. if exists("b:displayMode") && b:displayMode == "winmanager"
  1026. let _bufName = expand("#"._bufNbr.":p")
  1027. if (a:0 == 1) && (a:1 == "tab")
  1028. call WinManagerFileEdit(_bufName, 1)
  1029. else
  1030. call WinManagerFileEdit(_bufName, 0)
  1031. endif
  1032. return
  1033. endif
  1034. endif
  1035. if bufexists(_bufNbr)
  1036. " Get the tab number where this buffer is located in.
  1037. let tabNbr = s:GetTabNbr(_bufNbr)
  1038. if exists("g:bufExplorerChgWin") && g:bufExplorerChgWin <=winnr("$")
  1039. execute g:bufExplorerChgWin."wincmd w"
  1040. execute "keepjumps keepalt silent b!" _bufNbr
  1041. " Are we supposed to open the selected buffer in a tab?
  1042. elseif (a:0 == 1) && (a:1 == "tab")
  1043. call s:Close()
  1044. " Open a new tab with the selected buffer in it.
  1045. if v:version > 704 || ( v:version == 704 && has('patch2237') )
  1046. " new syntax for last tab as of 7.4.2237
  1047. execute "$tab split +buffer" . _bufNbr
  1048. else
  1049. execute "999tab split +buffer" . _bufNbr
  1050. endif
  1051. " Are we supposed to open the selected buffer in a split?
  1052. elseif (a:0 == 2) && (a:1 == "split")
  1053. call s:Close()
  1054. if (a:2 == "vl")
  1055. execute "vert topleft sb "._bufNbr
  1056. elseif (a:2 == "vr")
  1057. execute "vert belowright sb "._bufNbr
  1058. elseif (a:2 == "st")
  1059. execute "topleft sb "._bufNbr
  1060. else " = sb
  1061. execute "belowright sb "._bufNbr
  1062. endif
  1063. else
  1064. " Request to open in current (BufExplorer) window.
  1065. if g:bufExplorerFindActive && tabNbr > 0
  1066. " Close BufExplorer window and switch to existing tab/window.
  1067. call s:Close()
  1068. execute tabNbr . "tabnext"
  1069. execute bufwinnr(_bufNbr) . "wincmd w"
  1070. else
  1071. " Use BufExplorer window for the buffer.
  1072. execute "keepjumps keepalt silent b!" _bufNbr
  1073. endif
  1074. endif
  1075. " Make the buffer 'listed' again.
  1076. call setbufvar(_bufNbr, "&buflisted", "1")
  1077. " Call any associated function references. g:bufExplorerFuncRef may be
  1078. " an individual function reference or it may be a list containing
  1079. " function references. It will ignore anything that's not a function
  1080. " reference.
  1081. "
  1082. " See :help FuncRef for more on function references.
  1083. if exists("g:BufExplorerFuncRef")
  1084. if type(g:BufExplorerFuncRef) == 2
  1085. keepj call g:BufExplorerFuncRef()
  1086. elseif type(g:BufExplorerFuncRef) == 3
  1087. for FncRef in g:BufExplorerFuncRef
  1088. if type(FncRef) == 2
  1089. keepj call FncRef()
  1090. endif
  1091. endfor
  1092. endif
  1093. endif
  1094. else
  1095. call s:Error("Sorry, that buffer no longer exists, please select another")
  1096. call s:DeleteBuffer(_bufNbr, "wipe")
  1097. endif
  1098. endfunction
  1099. " RemoveBuffer {{{2
  1100. " Valid `mode` values:
  1101. " - "delete"
  1102. " - "force_delete"
  1103. " - "wipe"
  1104. " - "force_wipe"
  1105. function! s:RemoveBuffer(mode)
  1106. " Are we on a line with a file name?
  1107. if line('.') < s:firstBufferLine
  1108. return
  1109. endif
  1110. let mode = a:mode
  1111. let forced = mode =~# '^force_'
  1112. " These commands are to temporarily suspend the activity of winmanager.
  1113. if exists("b:displayMode") && b:displayMode == "winmanager"
  1114. call WinManagerSuspendAUs()
  1115. end
  1116. let bufNbr = str2nr(getline('.'))
  1117. let buf = s:raw_buffer_listing[bufNbr]
  1118. if !forced && (buf.isterminal || getbufvar(bufNbr, '&modified'))
  1119. if buf.isterminal
  1120. let msg = "Buffer " . bufNbr . " is a terminal"
  1121. else
  1122. let msg = "No write since last change for buffer " . bufNbr
  1123. endif
  1124. " Calling confirm() requires Vim built with dialog option.
  1125. if !has("dialog_con") && !has("dialog_gui")
  1126. call s:Error(msg . "; cannot remove without 'force'")
  1127. return
  1128. endif
  1129. let answer = confirm(msg . "; Remove anyway?", "&Yes\n&No", 2)
  1130. if answer == 1
  1131. let mode = 'force_' . mode
  1132. else
  1133. return
  1134. endif
  1135. endif
  1136. " Okay, everything is good, delete or wipe the buffer.
  1137. call s:DeleteBuffer(bufNbr, mode)
  1138. " Reactivate winmanager autocommand activity.
  1139. if exists("b:displayMode") && b:displayMode == "winmanager"
  1140. call WinManagerForceReSize("BufExplorer")
  1141. call WinManagerResumeAUs()
  1142. end
  1143. endfunction
  1144. " DeleteBuffer {{{2
  1145. " Valid `mode` values:
  1146. " - "delete"
  1147. " - "force_delete"
  1148. " - "wipe"
  1149. " - "force_wipe"
  1150. function! s:DeleteBuffer(bufNbr, mode)
  1151. " This routine assumes that the buffer to be removed is on the current line.
  1152. if a:mode =~# 'delete$' && bufexists(a:bufNbr) && !buflisted(a:bufNbr)
  1153. call s:Error('Buffer ' . a:bufNbr
  1154. \ . ' is unlisted; must `wipe` to remove')
  1155. return
  1156. endif
  1157. try
  1158. " Wipe/Delete buffer from Vim.
  1159. if a:mode == "wipe"
  1160. execute "silent bwipe" a:bufNbr
  1161. elseif a:mode == "force_wipe"
  1162. execute "silent bwipe!" a:bufNbr
  1163. elseif a:mode == "force_delete"
  1164. execute "silent bdelete!" a:bufNbr
  1165. else
  1166. execute "silent bdelete" a:bufNbr
  1167. endif
  1168. catch
  1169. call s:Error(v:exception)
  1170. endtry
  1171. if bufexists(a:bufNbr)
  1172. " Buffer is still present. We may have failed to wipe it, or it may
  1173. " have changed attributes (as `:bd` only makes a buffer unlisted).
  1174. " Regather information on this buffer, update the buffer list, and
  1175. " redisplay.
  1176. let info = s:GetBufferInfo(a:bufNbr)
  1177. let s:raw_buffer_listing[a:bufNbr] = info[a:bufNbr]
  1178. call s:RedisplayBufferList()
  1179. else
  1180. " Delete the buffer from the list on screen.
  1181. setlocal modifiable
  1182. normal! "_dd
  1183. setlocal nomodifiable
  1184. " Delete the buffer from the raw buffer list.
  1185. unlet s:raw_buffer_listing[a:bufNbr]
  1186. endif
  1187. endfunction
  1188. " Close {{{2
  1189. function! s:Close()
  1190. " Get only the listed buffers associated with the current tab (up to 2).
  1191. let listed = s:MRUListedBuffersForTab(s:tabIdAtLaunch, 2)
  1192. " If we needed to split the main window, close the split one.
  1193. if s:didSplit
  1194. execute "wincmd c"
  1195. endif
  1196. " Check to see if there are anymore buffers listed.
  1197. if len(listed) == 0
  1198. " Since there are no buffers left to switch to, open a new empty
  1199. " buffers.
  1200. execute "enew"
  1201. else
  1202. " Since there are buffers left to switch to, switch to the previous and
  1203. " then the current.
  1204. for b in reverse(listed[0:1])
  1205. execute "keepjumps silent b ".b
  1206. endfor
  1207. endif
  1208. " Clear any messages.
  1209. echo
  1210. endfunction
  1211. " ToggleShowTerminal {{{2
  1212. function! s:ToggleShowTerminal()
  1213. let g:bufExplorerShowTerminal = !g:bufExplorerShowTerminal
  1214. call s:RedisplayBufferList()
  1215. endfunction
  1216. " ToggleSplitOutPathName {{{2
  1217. function! s:ToggleSplitOutPathName()
  1218. let g:bufExplorerSplitOutPathName = !g:bufExplorerSplitOutPathName
  1219. call s:RedisplayBufferList()
  1220. endfunction
  1221. " ToggleShowRelativePath {{{2
  1222. function! s:ToggleShowRelativePath()
  1223. let g:bufExplorerShowRelativePath = !g:bufExplorerShowRelativePath
  1224. call s:RedisplayBufferList()
  1225. endfunction
  1226. " ToggleShowTabBuffer {{{2
  1227. function! s:ToggleShowTabBuffer()
  1228. " Forget any cached MRU ordering, as it depends on
  1229. " `g:bufExplorerShowTabBuffer`.
  1230. unlet! s:mruOrder
  1231. let g:bufExplorerShowTabBuffer = !g:bufExplorerShowTabBuffer
  1232. call s:RedisplayBufferList()
  1233. endfunction
  1234. " ToggleOnlyOneTab {{{2
  1235. function! s:ToggleOnlyOneTab()
  1236. let g:bufExplorerOnlyOneTab = !g:bufExplorerOnlyOneTab
  1237. call s:RedisplayBufferList()
  1238. endfunction
  1239. " ToggleShowUnlisted {{{2
  1240. function! s:ToggleShowUnlisted()
  1241. let g:bufExplorerShowUnlisted = !g:bufExplorerShowUnlisted
  1242. call s:RedisplayBufferList()
  1243. endfunction
  1244. " ToggleFindActive {{{2
  1245. function! s:ToggleFindActive()
  1246. let g:bufExplorerFindActive = !g:bufExplorerFindActive
  1247. call s:UpdateHelpStatus()
  1248. endfunction
  1249. " RebuildBufferList {{{2
  1250. function! s:RebuildBufferList()
  1251. setlocal modifiable
  1252. let curPos = getpos('.')
  1253. let num_bufs = s:BuildBufferList()
  1254. call setpos('.', curPos)
  1255. setlocal nomodifiable
  1256. return num_bufs
  1257. endfunction
  1258. " UpdateHelpStatus {{{2
  1259. function! s:UpdateHelpStatus()
  1260. setlocal modifiable
  1261. let text = s:GetHelpStatus()
  1262. call setline(s:firstBufferLine - 2, text)
  1263. setlocal nomodifiable
  1264. endfunction
  1265. " Key_number {{{2
  1266. function! s:Key_number(line)
  1267. let _bufnr = str2nr(a:line)
  1268. let key = [printf('%9d', _bufnr)]
  1269. return key
  1270. endfunction
  1271. " Key_name {{{2
  1272. function! s:Key_name(line)
  1273. let _bufnr = str2nr(a:line)
  1274. let buf = s:raw_buffer_listing[_bufnr]
  1275. let key = [buf.shortname, buf.fullname]
  1276. return key
  1277. endfunction
  1278. " Key_fullpath {{{2
  1279. function! s:Key_fullpath(line)
  1280. let _bufnr = str2nr(a:line)
  1281. let buf = s:raw_buffer_listing[_bufnr]
  1282. let key = [buf.fullname]
  1283. return key
  1284. endfunction
  1285. " Key_extension {{{2
  1286. function! s:Key_extension(line)
  1287. let _bufnr = str2nr(a:line)
  1288. let buf = s:raw_buffer_listing[_bufnr]
  1289. let extension = fnamemodify(buf.shortname, ':e')
  1290. let key = [extension, buf.shortname, buf.fullname]
  1291. return key
  1292. endfunction
  1293. " Key_mru {{{2
  1294. function! s:Key_mru(line)
  1295. let _bufnr = str2nr(a:line)
  1296. let buf = s:raw_buffer_listing[_bufnr]
  1297. let pos = s:MRUOrderForBuf(_bufnr)
  1298. return [printf('%9d', pos), buf.fullname]
  1299. endfunction
  1300. " SortByKeyFunc {{{2
  1301. function! s:SortByKeyFunc(keyFunc)
  1302. let keyedLines = []
  1303. for line in getline(s:firstBufferLine, "$")
  1304. let key = eval(a:keyFunc . '(line)')
  1305. call add(keyedLines, join(key + [line], "\1"))
  1306. endfor
  1307. " Ignore case when sorting by passing `1`:
  1308. call sort(keyedLines, 1)
  1309. if g:bufExplorerReverseSort
  1310. call reverse(keyedLines)
  1311. endif
  1312. let lines = []
  1313. for keyedLine in keyedLines
  1314. call add(lines, split(keyedLine, "\1")[-1])
  1315. endfor
  1316. call setline(s:firstBufferLine, lines)
  1317. endfunction
  1318. " SortReverse {{{2
  1319. function! s:SortReverse()
  1320. let g:bufExplorerReverseSort = !g:bufExplorerReverseSort
  1321. call s:ReSortListing()
  1322. endfunction
  1323. " SortSelect {{{2
  1324. function! s:SortSelect()
  1325. let g:bufExplorerSortBy = get(s:sort_by, index(s:sort_by, g:bufExplorerSortBy) + 1, s:sort_by[0])
  1326. call s:ReSortListing()
  1327. endfunction
  1328. " ReverseSortSelect {{{2
  1329. function! s:ReverseSortSelect()
  1330. let g:bufExplorerSortBy = get(s:sort_by, index(s:sort_by, g:bufExplorerSortBy) - 1, s:sort_by[-1])
  1331. call s:ReSortListing()
  1332. endfunction
  1333. " ReSortListing {{{2
  1334. function! s:ReSortListing()
  1335. setlocal modifiable
  1336. let curPos = getpos('.')
  1337. call s:SortListing()
  1338. call s:UpdateHelpStatus()
  1339. call setpos('.', curPos)
  1340. setlocal nomodifiable
  1341. endfunction
  1342. " SortListing {{{2
  1343. function! s:SortListing()
  1344. call s:SortByKeyFunc("<SID>Key_" . g:bufExplorerSortBy)
  1345. endfunction
  1346. " Error {{{2
  1347. " Display a message using ErrorMsg highlight group.
  1348. function! s:Error(msg)
  1349. echohl ErrorMsg
  1350. echomsg a:msg
  1351. echohl None
  1352. endfunction
  1353. " Warning {{{2
  1354. " Display a message using WarningMsg highlight group.
  1355. function! s:Warning(msg)
  1356. echohl WarningMsg
  1357. echomsg a:msg
  1358. echohl None
  1359. endfunction
  1360. " GetTabNbr {{{2
  1361. function! s:GetTabNbr(bufNbr)
  1362. " Prefer current tab.
  1363. if bufwinnr(a:bufNbr) > 0
  1364. return tabpagenr()
  1365. endif
  1366. " Searching buffer bufno, in tabs.
  1367. for i in range(tabpagenr("$"))
  1368. if index(tabpagebuflist(i + 1), a:bufNbr) != -1
  1369. return i + 1
  1370. endif
  1371. endfor
  1372. return 0
  1373. endfunction
  1374. " GetWinNbr" {{{2
  1375. function! s:GetWinNbr(tabNbr, bufNbr)
  1376. " window number in tabpage.
  1377. let tablist = tabpagebuflist(a:tabNbr)
  1378. " Number: 0
  1379. " String: 1
  1380. " Funcref: 2
  1381. " List: 3
  1382. " Dictionary: 4
  1383. " Float: 5
  1384. if type(tablist) == 3
  1385. return index(tabpagebuflist(a:tabNbr), a:bufNbr) + 1
  1386. else
  1387. return 1
  1388. endif
  1389. endfunction
  1390. " StringWidth" {{{2
  1391. if exists('*strwidth')
  1392. function s:StringWidth(s)
  1393. return strwidth(a:s)
  1394. endfunction
  1395. else
  1396. function s:StringWidth(s)
  1397. return len(a:s)
  1398. endfunction
  1399. endif
  1400. " Winmanager Integration {{{2
  1401. let g:BufExplorer_title = "\[Buf\ List\]"
  1402. call s:Set("g:bufExplorerResize", 1)
  1403. call s:Set("g:bufExplorerMaxHeight", 25) " Handles dynamic resizing of the window.
  1404. " Evaluate a Vimscript expression in the context of this file.
  1405. " This enables debugging of script-local variables and functions from outside
  1406. " the plugin, e.g.:
  1407. " :echo BufExplorer_eval('s:bufMru')
  1408. function! BufExplorer_eval(expr)
  1409. return eval(a:expr)
  1410. endfunction
  1411. " Execute a Vimscript statement in the context of this file.
  1412. " This enables setting script-local variables from outside the plugin, e.g.:
  1413. " :call BufExplorer_execute('let s:bufMru = s:MRUNew(0)')
  1414. function! BufExplorer_execute(statement)
  1415. execute a:statement
  1416. endfunction
  1417. " function! to start display. Set the mode to 'winmanager' for this buffer.
  1418. " This is to figure out how this plugin was called. In a standalone fashion
  1419. " or by winmanager.
  1420. function! BufExplorer_Start()
  1421. let b:displayMode = "winmanager"
  1422. call s:SetLocalSettings()
  1423. call BufExplorer()
  1424. endfunction
  1425. " Returns whether the display is okay or not.
  1426. function! BufExplorer_IsValid()
  1427. return 0
  1428. endfunction
  1429. " Handles dynamic refreshing of the window.
  1430. function! BufExplorer_Refresh()
  1431. let b:displayMode = "winmanager"
  1432. call s:SetLocalSettings()
  1433. call BufExplorer()
  1434. endfunction
  1435. function! BufExplorer_ReSize()
  1436. if !g:bufExplorerResize
  1437. return
  1438. end
  1439. let nlines = min([line("$"), g:bufExplorerMaxHeight])
  1440. execute nlines." wincmd _"
  1441. " The following lines restore the layout so that the last file line is also
  1442. " the last window line. Sometimes, when a line is deleted, although the
  1443. " window size is exactly equal to the number of lines in the file, some of
  1444. " the lines are pushed up and we see some lagging '~'s.
  1445. let pres = getpos(".")
  1446. normal! $
  1447. let _scr = &scrolloff
  1448. let &scrolloff = 0
  1449. normal! z-
  1450. let &scrolloff = _scr
  1451. call setpos(".", pres)
  1452. endfunction
  1453. " Default values {{{2
  1454. call s:Set("g:bufExplorerDisableDefaultKeyMapping", 0) " Do not disable default key mappings.
  1455. call s:Set("g:bufExplorerDefaultHelp", 1) " Show default help?
  1456. call s:Set("g:bufExplorerDetailedHelp", 0) " Show detailed help?
  1457. call s:Set("g:bufExplorerFindActive", 1) " When selecting an active buffer, take you to the window where it is active?
  1458. call s:Set("g:bufExplorerOnlyOneTab", 1) " Show buffer only on MRU tab? (Applies when `g:bufExplorerShowTabBuffer` is true.)
  1459. call s:Set("g:bufExplorerReverseSort", 0) " Sort in reverse order by default?
  1460. call s:Set("g:bufExplorerShowDirectories", 1) " (Dir's are added by commands like ':e .')
  1461. call s:Set("g:bufExplorerShowRelativePath", 0) " Show listings with relative or absolute paths?
  1462. call s:Set("g:bufExplorerShowTabBuffer", 0) " Show only buffer(s) for this tab?
  1463. call s:Set("g:bufExplorerShowUnlisted", 0) " Show unlisted buffers?
  1464. call s:Set("g:bufExplorerShowNoName", 0) " Show 'No Name' buffers?
  1465. call s:Set("g:bufExplorerSortBy", "mru") " Sorting methods are in s:sort_by:
  1466. call s:Set("g:bufExplorerSplitBelow", &splitbelow) " Should horizontal splits be below or above current window?
  1467. call s:Set("g:bufExplorerSplitOutPathName", 1) " Split out path and file name?
  1468. call s:Set("g:bufExplorerSplitRight", &splitright) " Should vertical splits be on the right or left of current window?
  1469. call s:Set("g:bufExplorerSplitVertSize", 0) " Height for a vertical split. If <=0, default Vim size is used.
  1470. call s:Set("g:bufExplorerSplitHorzSize", 0) " Height for a horizontal split. If <=0, default Vim size is used.
  1471. call s:Set("g:bufExplorerShowTerminal", 1) " Show terminal buffers?
  1472. " Default key mapping {{{2
  1473. if !hasmapto('BufExplorer') && g:bufExplorerDisableDefaultKeyMapping == 0
  1474. nnoremap <script> <silent> <unique> <Leader>be :BufExplorer<CR>
  1475. endif
  1476. if !hasmapto('ToggleBufExplorer') && g:bufExplorerDisableDefaultKeyMapping == 0
  1477. nnoremap <script> <silent> <unique> <Leader>bt :ToggleBufExplorer<CR>
  1478. endif
  1479. if !hasmapto('BufExplorerHorizontalSplit') && g:bufExplorerDisableDefaultKeyMapping == 0
  1480. nnoremap <script> <silent> <unique> <Leader>bs :BufExplorerHorizontalSplit<CR>
  1481. endif
  1482. if !hasmapto('BufExplorerVerticalSplit') && g:bufExplorerDisableDefaultKeyMapping == 0
  1483. nnoremap <script> <silent> <unique> <Leader>bv :BufExplorerVerticalSplit<CR>
  1484. endif
  1485. " vim:ft=vim foldmethod=marker sw=4