bufexplorer.vim 57 KB

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