bufexplorer.vim 59 KB

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