bufexplorer.vim 57 KB

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