bufexplorer.vim 57 KB

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