bufexplorer.vim 58 KB

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