bufexplorer.vim 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. "============================================================================
  2. " Copyright: Copyright (c) 2001-2017, 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 (delux256-vim at yahoo dot com)
  39. " Last Changed: Monday, 10 April 2017
  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.4.14"
  76. " Plugin Code {{{1
  77. " Check for Vim version {{{2
  78. if v:version < 700
  79. echohl WarningMsg
  80. echo "Sorry, bufexplorer ".g:bufexplorer_version." required Vim 7.0 or greater."
  81. echohl None
  82. finish
  83. endif
  84. " Check to see if the version of Vim has the correct patch applied, if not, do
  85. " not used <nowait>.
  86. if v:version > 703 || v:version == 703 && has('patch1261') && has('patch1264')
  87. " We are good to go.
  88. else
  89. echohl WarningMsg
  90. echo "Sorry, bufexplorer ".g:bufexplorer_version." required Vim 7.3 or greater with patch1261 and patch1264."
  91. echohl None
  92. finish
  93. endif
  94. " Create commands {{{2
  95. command! BufExplorer :call BufExplorer()
  96. command! ToggleBufExplorer :call ToggleBufExplorer()
  97. command! BufExplorerHorizontalSplit :call BufExplorerHorizontalSplit()
  98. command! BufExplorerVerticalSplit :call BufExplorerVerticalSplit()
  99. " Set {{{2
  100. function! s:Set(var, default)
  101. if !exists(a:var)
  102. if type(a:default)
  103. execute "let" a:var "=" string(a:default)
  104. else
  105. execute "let" a:var "=" a:default
  106. endif
  107. return 1
  108. endif
  109. return 0
  110. endfunction
  111. " Script variables {{{2
  112. let s:MRU_Exclude_List = ["[BufExplorer]","__MRU_Files__","[Buf\ List]"]
  113. let s:MRUList = []
  114. let s:name = '[BufExplorer]'
  115. let s:originBuffer = 0
  116. let s:running = 0
  117. let s:sort_by = ["number", "name", "fullpath", "mru", "extension"]
  118. let s:splitMode = ""
  119. let s:types = {"fullname": ':p', "path": ':p:h', "relativename": ':~:.', "relativepath": ':~:.:h', "shortname": ':t'}
  120. " Setup the autocommands that handle the MRUList and other stuff. {{{2
  121. autocmd VimEnter * call s:Setup()
  122. " Setup {{{2
  123. function! s:Setup()
  124. call s:Reset()
  125. " Now that the MRUList is created, add the other autocmds.
  126. augroup BufExplorer
  127. autocmd!
  128. autocmd BufEnter,BufNew * call s:ActivateBuffer()
  129. autocmd BufWipeOut * call s:DeactivateBuffer(1)
  130. autocmd BufDelete * call s:DeactivateBuffer(0)
  131. autocmd BufWinEnter \[BufExplorer\] call s:Initialize()
  132. autocmd BufWinLeave \[BufExplorer\] call s:Cleanup()
  133. augroup END
  134. endfunction
  135. " Reset {{{2
  136. function! s:Reset()
  137. " Build initial MRUList. This makes sure all the files specified on the
  138. " command line are picked up correctly.
  139. let s:MRUList = range(1, bufnr('$'))
  140. " Initialize the association of buffers to tabs for any buffers
  141. " that have been created prior to now, e.g., files specified as
  142. " vim command line arguments
  143. call s:CatalogBuffers()
  144. endfunction
  145. " CatalogBuffers {{{2
  146. " Create tab associations for any existing buffers
  147. function! s:CatalogBuffers()
  148. let ct = tabpagenr()
  149. for tab in range(1, tabpagenr('$'))
  150. silent execute 'normal! ' . tab . 'gt'
  151. for buf in tabpagebuflist()
  152. call s:UpdateTabBufData(buf)
  153. endfor
  154. endfor
  155. silent execute 'normal! ' . ct . 'gt'
  156. endfunction
  157. " AssociatedTab {{{2
  158. " Return the number of the tab associated with the specified buffer.
  159. " If the buffer is associated with more than one tab, the first one
  160. " found is returned. If the buffer is not associated with any tabs,
  161. " -1 is returned.
  162. function! s:AssociatedTab(bufnr)
  163. for tab in range(1, tabpagenr('$'))
  164. let list = gettabvar(tab, 'bufexp_buf_list', [])
  165. let idx = index(list, a:bufnr)
  166. if idx != -1
  167. return tab
  168. endif
  169. endfor
  170. return -1
  171. endfunction
  172. " RemoveBufFromOtherTabs {{{2
  173. " Remove the specified buffer from the buffer lists of all tabs
  174. " except the current tab.
  175. function! s:RemoveBufFromOtherTabs(bufnr)
  176. for tab in range(1, tabpagenr('$'))
  177. if tab == tabpagenr()
  178. continue
  179. endif
  180. let list = gettabvar(tab, 'bufexp_buf_list', [])
  181. let idx = index(list, a:bufnr)
  182. if idx == -1
  183. continue
  184. endif
  185. call remove(list, idx)
  186. call settabvar(tab, 'bufexp_buf_list', list)
  187. endfor
  188. endfunction
  189. " AddBufToCurrentTab {{{2
  190. " Add the specified buffer to the list of buffers associated
  191. " with the current tab
  192. function! s:AddBufToCurrentTab(bufnr)
  193. if index(t:bufexp_buf_list, a:bufnr) == -1
  194. call add(t:bufexp_buf_list, a:bufnr)
  195. endif
  196. endfunction
  197. " IsInCurrentTab {{{2
  198. " Returns whether the specified buffer is associated
  199. " with the current tab
  200. function! s:IsInCurrentTab(bufnr)
  201. " It shouldn't happen that the list of buffers is
  202. " not defined but if it does, play it safe and
  203. " include the buffer
  204. if !exists('t:bufexp_buf_list')
  205. return 1
  206. endif
  207. return (index(t:bufexp_buf_list, a:bufnr) != -1)
  208. endfunction
  209. " UpdateTabBufData {{{2
  210. " Update the tab buffer data for the specified buffer
  211. "
  212. " The current tab's list is updated. If a buffer is only
  213. " allowed to be associated with one tab, it is removed
  214. " from the lists of any other tabs with which it may have
  215. " been associated.
  216. "
  217. " The associations between tabs and buffers are maintained
  218. " in separate lists for each tab, which are stored in tab-
  219. " specific variables 't:bufexp_buf_list'.
  220. function! s:UpdateTabBufData(bufnr)
  221. " The first time we add a tab, Vim uses the current buffer
  222. " as its starting page even though we are about to edit a
  223. " new page, and another BufEnter for the new page is triggered
  224. " later. Use this first BufEnter to initialize the list of
  225. " buffers, but don't add the buffer number to the list if
  226. " it is already associated with another tab
  227. "
  228. " Unfortunately, this doesn't work right when the first
  229. " buffer opened in the tab should be associated with it,
  230. " such as when 'tab split +buffer N' is used
  231. if !exists("t:bufexp_buf_list")
  232. let t:bufexp_buf_list = []
  233. if s:AssociatedTab(a:bufnr) != -1
  234. return
  235. endif
  236. endif
  237. call s:AddBufToCurrentTab(a:bufnr)
  238. if g:bufExplorerOnlyOneTab
  239. call s:RemoveBufFromOtherTabs(a:bufnr)
  240. endif
  241. endfunction
  242. " ActivateBuffer {{{2
  243. function! s:ActivateBuffer()
  244. let _bufnr = bufnr("%")
  245. call s:UpdateTabBufData(_bufnr)
  246. call s:MRUPush(_bufnr)
  247. endfunction
  248. " DeactivateBuffer {{{2
  249. function! s:DeactivateBuffer(remove)
  250. let _bufnr = str2nr(expand("<abuf>"))
  251. call s:MRUPop(_bufnr)
  252. endfunction
  253. " MRUPop {{{2
  254. function! s:MRUPop(bufnr)
  255. call filter(s:MRUList, 'v:val != '.a:bufnr)
  256. endfunction
  257. " MRUPush {{{2
  258. function! s:MRUPush(buf)
  259. " Skip temporary buffer with buftype set. Don't add the BufExplorer window
  260. " to the list.
  261. if s:ShouldIgnore(a:buf) == 1
  262. return
  263. endif
  264. " Remove the buffer number from the list if it already exists.
  265. call s:MRUPop(a:buf)
  266. " Add the buffer number to the head of the list.
  267. call insert(s:MRUList, a:buf)
  268. endfunction
  269. " ShouldIgnore {{{2
  270. function! s:ShouldIgnore(buf)
  271. " Ignore temporary buffers with buftype set.
  272. if empty(getbufvar(a:buf, "&buftype") == 0)
  273. return 1
  274. endif
  275. " Ignore buffers with no name.
  276. if empty(bufname(a:buf)) == 1
  277. return 1
  278. endif
  279. " Ignore the BufExplorer buffer.
  280. if fnamemodify(bufname(a:buf), ":t") == s:name
  281. return 1
  282. endif
  283. " Ignore any buffers in the exclude list.
  284. if index(s:MRU_Exclude_List, bufname(a:buf)) >= 0
  285. return 1
  286. endif
  287. " Else return 0 to indicate that the buffer was not ignored.
  288. return 0
  289. endfunction
  290. " Initialize {{{2
  291. function! s:Initialize()
  292. call s:SetLocalSettings()
  293. let s:running = 1
  294. endfunction
  295. " Cleanup {{{2
  296. function! s:Cleanup()
  297. if exists("s:_insertmode")
  298. let &insertmode = s:_insertmode
  299. endif
  300. if exists("s:_showcmd")
  301. let &showcmd = s:_showcmd
  302. endif
  303. if exists("s:_cpo")
  304. let &cpo = s:_cpo
  305. endif
  306. if exists("s:_report")
  307. let &report = s:_report
  308. endif
  309. let s:running = 0
  310. let s:splitMode = ""
  311. delmarks!
  312. endfunction
  313. " SetLocalSettings {{{2
  314. function! s:SetLocalSettings()
  315. let s:_insertmode = &insertmode
  316. set noinsertmode
  317. let s:_showcmd = &showcmd
  318. set noshowcmd
  319. let s:_cpo = &cpo
  320. set cpo&vim
  321. let s:_report = &report
  322. let &report = 10000
  323. setlocal nonumber
  324. setlocal foldcolumn=0
  325. setlocal nofoldenable
  326. setlocal cursorline
  327. setlocal nospell
  328. setlocal nobuflisted
  329. setlocal filetype=bufexplorer
  330. endfunction
  331. " BufExplorerHorizontalSplit {{{2
  332. function! BufExplorerHorizontalSplit()
  333. let s:splitMode = "sp"
  334. execute "BufExplorer"
  335. endfunction
  336. " BufExplorerVerticalSplit {{{2
  337. function! BufExplorerVerticalSplit()
  338. let s:splitMode = "vsp"
  339. execute "BufExplorer"
  340. endfunction
  341. " ToggleBufExplorer {{{2
  342. function! ToggleBufExplorer()
  343. if exists("s:running") && s:running == 1 && bufname(winbufnr(0)) == s:name
  344. call s:Close()
  345. else
  346. call BufExplorer()
  347. endif
  348. endfunction
  349. " BufExplorer {{{2
  350. function! BufExplorer()
  351. let name = s:name
  352. if !has("win32")
  353. " On non-Windows boxes, escape the name so that is shows up correctly.
  354. let name = escape(name, "[]")
  355. endif
  356. " Make sure there is only one explorer open at a time.
  357. if s:running == 1
  358. " Go to the open buffer.
  359. if has("gui")
  360. execute "drop" name
  361. endif
  362. return
  363. endif
  364. " Add zero to ensure the variable is treated as a number.
  365. let s:originBuffer = bufnr("%") + 0
  366. silent let s:raw_buffer_listing = s:GetBufferInfo(0)
  367. " We may have to split the current window.
  368. if s:splitMode != ""
  369. " Save off the original settings.
  370. let [_splitbelow, _splitright] = [&splitbelow, &splitright]
  371. " Set the setting to ours.
  372. let [&splitbelow, &splitright] = [g:bufExplorerSplitBelow, g:bufExplorerSplitRight]
  373. let _size = (s:splitMode == "sp") ? g:bufExplorerSplitHorzSize : g:bufExplorerSplitVertSize
  374. " Split the window either horizontally or vertically.
  375. if _size <= 0
  376. execute 'keepalt ' . s:splitMode
  377. else
  378. execute 'keepalt ' . _size . s:splitMode
  379. endif
  380. " Restore the original settings.
  381. let [&splitbelow, &splitright] = [_splitbelow, _splitright]
  382. endif
  383. if !exists("b:displayMode") || b:displayMode != "winmanager"
  384. " Do not use keepalt when opening bufexplorer to allow the buffer that
  385. " we are leaving to become the new alternate buffer
  386. execute "silent keepjumps hide edit".name
  387. endif
  388. call s:DisplayBufferList()
  389. " Position the cursor in the newly displayed list on the line representing
  390. " the active buffer. The active buffer is the line with the '%' character
  391. " in it.
  392. execute search("%")
  393. endfunction
  394. " DisplayBufferList {{{2
  395. function! s:DisplayBufferList()
  396. " Do not set bufhidden since it wipes out the data if we switch away from
  397. " the buffer using CTRL-^.
  398. setlocal buftype=nofile
  399. setlocal modifiable
  400. setlocal noswapfile
  401. setlocal nowrap
  402. call s:SetupSyntax()
  403. call s:MapKeys()
  404. " Wipe out any existing lines in case BufExplorer buffer exists and the
  405. " user had changed any global settings that might reduce the number of
  406. " lines needed in the buffer.
  407. silent keepjumps 1,$d _
  408. call setline(1, s:CreateHelp())
  409. call s:BuildBufferList()
  410. call cursor(s:firstBufferLine, 1)
  411. if !g:bufExplorerResize
  412. normal! zz
  413. endif
  414. setlocal nomodifiable
  415. endfunction
  416. " MapKeys {{{2
  417. function! s:MapKeys()
  418. if exists("b:displayMode") && b:displayMode == "winmanager"
  419. nnoremap <buffer> <silent> <tab> :call <SID>SelectBuffer()<CR>
  420. endif
  421. nnoremap <script> <silent> <nowait> <buffer> <2-leftmouse> :call <SID>SelectBuffer()<CR>
  422. nnoremap <script> <silent> <nowait> <buffer> <CR> :call <SID>SelectBuffer()<CR>
  423. nnoremap <script> <silent> <nowait> <buffer> <F1> :call <SID>ToggleHelp()<CR>
  424. nnoremap <script> <silent> <nowait> <buffer> <s-cr> :call <SID>SelectBuffer("tab")<CR>
  425. nnoremap <script> <silent> <nowait> <buffer> B :call <SID>ToggleOnlyOneTab()<CR>
  426. nnoremap <script> <silent> <nowait> <buffer> b :call <SID>SelectBuffer("ask")<CR>
  427. nnoremap <script> <silent> <nowait> <buffer> d :call <SID>RemoveBuffer("delete")<CR>
  428. xnoremap <script> <silent> <nowait> <buffer> d :call <SID>RemoveBuffer("delete")<CR>
  429. nnoremap <script> <silent> <nowait> <buffer> D :call <SID>RemoveBuffer("wipe")<CR>
  430. xnoremap <script> <silent> <nowait> <buffer> D :call <SID>RemoveBuffer("wipe")<CR>
  431. nnoremap <script> <silent> <nowait> <buffer> f :call <SID>ToggleFindActive()<CR>
  432. nnoremap <script> <silent> <nowait> <buffer> m :call <SID>MRUListShow()<CR>
  433. nnoremap <script> <silent> <nowait> <buffer> o :call <SID>SelectBuffer()<CR>
  434. nnoremap <script> <silent> <nowait> <buffer> p :call <SID>ToggleSplitOutPathName()<CR>
  435. nnoremap <script> <silent> <nowait> <buffer> q :call <SID>Close()<CR>
  436. nnoremap <script> <silent> <nowait> <buffer> r :call <SID>SortReverse()<CR>
  437. nnoremap <script> <silent> <nowait> <buffer> R :call <SID>ToggleShowRelativePath()<CR>
  438. nnoremap <script> <silent> <nowait> <buffer> s :call <SID>SortSelect()<CR>
  439. nnoremap <script> <silent> <nowait> <buffer> S :call <SID>ReverseSortSelect()<CR>
  440. nnoremap <script> <silent> <nowait> <buffer> t :call <SID>SelectBuffer("tab")<CR>
  441. nnoremap <script> <silent> <nowait> <buffer> T :call <SID>ToggleShowTabBuffer()<CR>
  442. nnoremap <script> <silent> <nowait> <buffer> u :call <SID>ToggleShowUnlisted()<CR>
  443. for k in ["G", "n", "N", "L", "M", "H"]
  444. execute "nnoremap <buffer> <silent>" k ":keepjumps normal!" k."<CR>"
  445. endfor
  446. endfunction
  447. " SetupSyntax {{{2
  448. function! s:SetupSyntax()
  449. if has("syntax")
  450. syn match bufExplorerHelp "^\".*" contains=bufExplorerSortBy,bufExplorerMapping,bufExplorerTitle,bufExplorerSortType,bufExplorerToggleSplit,bufExplorerToggleOpen
  451. syn match bufExplorerOpenIn "Open in \w\+ window" contained
  452. syn match bufExplorerSplit "\w\+ split" contained
  453. syn match bufExplorerSortBy "Sorted by .*" contained contains=bufExplorerOpenIn,bufExplorerSplit
  454. syn match bufExplorerMapping "\" \zs.\+\ze :" contained
  455. syn match bufExplorerTitle "Buffer Explorer.*" contained
  456. syn match bufExplorerSortType "'\w\{-}'" contained
  457. syn match bufExplorerBufNbr /^\s*\d\+/
  458. syn match bufExplorerToggleSplit "toggle split type" contained
  459. syn match bufExplorerToggleOpen "toggle open mode" contained
  460. syn match bufExplorerModBuf /^\s*\d\+.\{4}+.*/
  461. syn match bufExplorerLockedBuf /^\s*\d\+.\{3}[\-=].*/
  462. syn match bufExplorerHidBuf /^\s*\d\+.\{2}h.*/
  463. syn match bufExplorerActBuf /^\s*\d\+.\{2}a.*/
  464. syn match bufExplorerCurBuf /^\s*\d\+.%.*/
  465. syn match bufExplorerAltBuf /^\s*\d\+.#.*/
  466. syn match bufExplorerUnlBuf /^\s*\d\+u.*/
  467. syn match bufExplorerInactBuf /^\s*\d\+ \{7}.*/
  468. hi def link bufExplorerBufNbr Number
  469. hi def link bufExplorerMapping NonText
  470. hi def link bufExplorerHelp Special
  471. hi def link bufExplorerOpenIn Identifier
  472. hi def link bufExplorerSortBy String
  473. hi def link bufExplorerSplit NonText
  474. hi def link bufExplorerTitle NonText
  475. hi def link bufExplorerSortType bufExplorerSortBy
  476. hi def link bufExplorerToggleSplit bufExplorerSplit
  477. hi def link bufExplorerToggleOpen bufExplorerOpenIn
  478. hi def link bufExplorerActBuf Identifier
  479. hi def link bufExplorerAltBuf String
  480. hi def link bufExplorerCurBuf Type
  481. hi def link bufExplorerHidBuf Constant
  482. hi def link bufExplorerLockedBuf Special
  483. hi def link bufExplorerModBuf Exception
  484. hi def link bufExplorerUnlBuf Comment
  485. hi def link bufExplorerInactBuf Comment
  486. endif
  487. endfunction
  488. " ToggleHelp {{{2
  489. function! s:ToggleHelp()
  490. let g:bufExplorerDetailedHelp = !g:bufExplorerDetailedHelp
  491. setlocal modifiable
  492. " Save position.
  493. normal! ma
  494. " Remove old header.
  495. if s:firstBufferLine > 1
  496. execute "keepjumps 1,".(s:firstBufferLine - 1) "d _"
  497. endif
  498. call append(0, s:CreateHelp())
  499. silent! normal! g`a
  500. delmarks a
  501. setlocal nomodifiable
  502. if exists("b:displayMode") && b:displayMode == "winmanager"
  503. call WinManagerForceReSize("BufExplorer")
  504. endif
  505. endfunction
  506. " GetHelpStatus {{{2
  507. function! s:GetHelpStatus()
  508. let ret = '" Sorted by '.((g:bufExplorerReverseSort == 1) ? "reverse " : "").g:bufExplorerSortBy
  509. let ret .= ' | '.((g:bufExplorerFindActive == 0) ? "Don't " : "")."Locate buffer"
  510. let ret .= ((g:bufExplorerShowUnlisted == 0) ? "" : " | Show unlisted")
  511. let ret .= ((g:bufExplorerShowTabBuffer == 0) ? "" : " | Show buffers/tab")
  512. let ret .= ((g:bufExplorerOnlyOneTab == 0) ? "" : " | One tab/buffer")
  513. let ret .= ' | '.((g:bufExplorerShowRelativePath == 0) ? "Absolute" : "Relative")
  514. let ret .= ' '.((g:bufExplorerSplitOutPathName == 0) ? "Full" : "Split")." path"
  515. return ret
  516. endfunction
  517. " CreateHelp {{{2
  518. function! s:CreateHelp()
  519. if g:bufExplorerDefaultHelp == 0 && g:bufExplorerDetailedHelp == 0
  520. let s:firstBufferLine = 1
  521. return []
  522. endif
  523. let header = []
  524. if g:bufExplorerDetailedHelp == 1
  525. call add(header, '" Buffer Explorer ('.g:bufexplorer_version.')')
  526. call add(header, '" --------------------------')
  527. call add(header, '" <F1> : toggle this help')
  528. call add(header, '" <enter> or o or Mouse-Double-Click : open buffer under cursor')
  529. call add(header, '" <shift-enter> or t : open buffer in another tab')
  530. call add(header, '" B : toggle if to save/use recent tab or not')
  531. call add(header, '" d : delete buffer')
  532. call add(header, '" D : wipe buffer')
  533. call add(header, '" f : toggle find active buffer')
  534. call add(header, '" p : toggle splitting of file and path name')
  535. call add(header, '" q : quit')
  536. call add(header, '" r : reverse sort')
  537. call add(header, '" R : toggle showing relative or full paths')
  538. call add(header, '" s : cycle thru "sort by" fields '.string(s:sort_by).'')
  539. call add(header, '" S : reverse cycle thru "sort by" fields')
  540. call add(header, '" T : toggle if to show only buffers for this tab or not')
  541. call add(header, '" u : toggle showing unlisted buffers')
  542. else
  543. call add(header, '" Press <F1> for Help')
  544. endif
  545. if (!exists("b:displayMode") || b:displayMode != "winmanager") || (b:displayMode == "winmanager" && g:bufExplorerDetailedHelp == 1)
  546. call add(header, s:GetHelpStatus())
  547. call add(header, '"=')
  548. endif
  549. let s:firstBufferLine = len(header) + 1
  550. return header
  551. endfunction
  552. " GetBufferInfo {{{2
  553. function! s:GetBufferInfo(bufnr)
  554. redir => bufoutput
  555. " Show all buffers including the unlisted ones. [!] tells Vim to show the
  556. " unlisted ones.
  557. buffers!
  558. redir END
  559. if a:bufnr > 0
  560. " Since we are only interested in this specified buffer
  561. " remove the other buffers listed
  562. let bufoutput = substitute(bufoutput."\n", '^.*\n\(\s*'.a:bufnr.'\>.\{-}\)\n.*', '\1', '')
  563. endif
  564. let [all, allwidths, listedwidths] = [[], {}, {}]
  565. for n in keys(s:types)
  566. let allwidths[n] = []
  567. let listedwidths[n] = []
  568. endfor
  569. " Loop over each line in the buffer.
  570. for buf in split(bufoutput, '\n')
  571. let bits = split(buf, '"')
  572. " Use first and last components after the split on '"', in case a
  573. " filename with an embedded '"' is present.
  574. let b = {"attributes": bits[0], "line": substitute(bits[-1], '\s*', '', '')}
  575. let name = bufname(str2nr(b.attributes))
  576. let b["hasNoName"] = empty(name)
  577. if b.hasNoName
  578. let name = "[No Name]"
  579. endif
  580. for [key, val] in items(s:types)
  581. let b[key] = fnamemodify(name, val)
  582. endfor
  583. if getftype(b.fullname) == "dir" && g:bufExplorerShowDirectories == 1
  584. let b.shortname = "<DIRECTORY>"
  585. endif
  586. call add(all, b)
  587. for n in keys(s:types)
  588. call add(allwidths[n], s:StringWidth(b[n]))
  589. if b.attributes !~ "u"
  590. call add(listedwidths[n], s:StringWidth(b[n]))
  591. endif
  592. endfor
  593. endfor
  594. let [s:allpads, s:listedpads] = [{}, {}]
  595. for n in keys(s:types)
  596. let s:allpads[n] = repeat(' ', max(allwidths[n]))
  597. let s:listedpads[n] = repeat(' ', max(listedwidths[n]))
  598. endfor
  599. return all
  600. endfunction
  601. " BuildBufferList {{{2
  602. function! s:BuildBufferList()
  603. let lines = []
  604. " Loop through every buffer.
  605. for buf in s:raw_buffer_listing
  606. " Skip unlisted buffers if we are not to show them.
  607. if !g:bufExplorerShowUnlisted && buf.attributes =~ "u"
  608. " Skip unlisted buffers if we are not to show them.
  609. continue
  610. endif
  611. " Skip "No Name" buffers if we are not to show them.
  612. if g:bufExplorerShowNoName == 0 && buf.hasNoName
  613. continue
  614. endif
  615. " Are we to show only buffer(s) for this tab?
  616. if g:bufExplorerShowTabBuffer && (!s:IsInCurrentTab(str2nr(buf.attributes)))
  617. continue
  618. endif
  619. let line = buf.attributes." "
  620. " Are we to split the path and file name?
  621. if g:bufExplorerSplitOutPathName
  622. let type = (g:bufExplorerShowRelativePath) ? "relativepath" : "path"
  623. let path = buf[type]
  624. let pad = (g:bufExplorerShowUnlisted) ? s:allpads.shortname : s:listedpads.shortname
  625. let line .= buf.shortname." ".strpart(pad.path, s:StringWidth(buf.shortname))
  626. else
  627. let type = (g:bufExplorerShowRelativePath) ? "relativename" : "fullname"
  628. let path = buf[type]
  629. let line .= path
  630. endif
  631. let pads = (g:bufExplorerShowUnlisted) ? s:allpads : s:listedpads
  632. if !empty(pads[type])
  633. let line .= strpart(pads[type], s:StringWidth(path))." "
  634. endif
  635. let line .= buf.line
  636. call add(lines, line)
  637. endfor
  638. call setline(s:firstBufferLine, lines)
  639. call s:SortListing()
  640. endfunction
  641. " SelectBuffer {{{2
  642. function! s:SelectBuffer(...)
  643. " Sometimes messages are not cleared when we get here so it looks like an
  644. " error has occurred when it really has not.
  645. "echo ""
  646. let _bufNbr = -1
  647. if (a:0 == 1) && (a:1 == "ask")
  648. " Ask the user for input.
  649. call inputsave()
  650. let cmd = input("Enter buffer number to switch to: ")
  651. call inputrestore()
  652. " Clear the message area from the previous prompt.
  653. redraw | echo
  654. if strlen(cmd) > 0
  655. let _bufNbr = str2nr(cmd)
  656. else
  657. call s:Error("Invalid buffer number, try again.")
  658. return
  659. endif
  660. else
  661. " Are we on a line with a file name?
  662. if line('.') < s:firstBufferLine
  663. execute "normal! \<CR>"
  664. return
  665. endif
  666. let _bufNbr = str2nr(getline('.'))
  667. " Check and see if we are running BufferExplorer via WinManager.
  668. if exists("b:displayMode") && b:displayMode == "winmanager"
  669. let _bufName = expand("#"._bufNbr.":p")
  670. if (a:0 == 1) && (a:1 == "tab")
  671. call WinManagerFileEdit(_bufName, 1)
  672. else
  673. call WinManagerFileEdit(_bufName, 0)
  674. endif
  675. return
  676. endif
  677. endif
  678. if bufexists(_bufNbr)
  679. if bufnr("#") == _bufNbr && !exists("g:bufExplorerChgWin")
  680. return s:Close()
  681. endif
  682. " Are we suppose to open the selected buffer in a tab?
  683. if (a:0 == 1) && (a:1 == "tab")
  684. " Yes, we are to open the selected buffer in a tab.
  685. " Restore [BufExplorer] buffer.
  686. execute "silent buffer!".s:originBuffer
  687. " Get the tab number where this buffer is located in.
  688. let tabNbr = s:GetTabNbr(_bufNbr)
  689. " Was the tab found?
  690. if tabNbr == 0
  691. " _bufNbr is not opened in any tabs. Open a new tab with the selected buffer in it.
  692. if v:version > 704 || ( v:version == 704 && has('patch2237') )
  693. " new syntax for last tab as of 7.4.2237
  694. execute "$tab split +buffer" . _bufNbr
  695. else
  696. execute "999tab split +buffer" . _bufNbr
  697. endif
  698. " Workaround for the issue mentioned in UpdateTabBufData
  699. call s:UpdateTabBufData(_bufNbr)
  700. else
  701. " The _bufNbr is already opened in a tab, go to that tab.
  702. execute tabNbr . "tabnext"
  703. " Focus window.
  704. execute s:GetWinNbr(tabNbr, _bufNbr) . "wincmd w"
  705. endif
  706. else
  707. " No, the user did not ask to open the selected buffer in a tab.
  708. " Are we suppose to move to the tab where the active buffer is?
  709. if exists("g:bufExplorerChgWin")
  710. execute g:bufExplorerChgWin."wincmd w"
  711. elseif bufloaded(_bufNbr) && g:bufExplorerFindActive
  712. if g:bufExplorerFindActive
  713. call s:Close()
  714. endif
  715. " Get the tab number where this buffer is located in.
  716. let tabNbr = s:GetTabNbr(_bufNbr)
  717. " Was the tab found?
  718. if tabNbr != 0
  719. " Yes, the buffer is located in a tab. Go to that tab number.
  720. execute tabNbr . "tabnext"
  721. else
  722. "Nope, the buffer is not in a tab. Simply switch to that
  723. "buffer.
  724. let _bufName = expand("#"._bufNbr.":p")
  725. execute _bufName ? "drop ".escape(_bufName, " ") : "buffer "._bufNbr
  726. endif
  727. endif
  728. " Switch to the selected buffer.
  729. execute "keepalt silent b!" _bufNbr
  730. endif
  731. " Make the buffer 'listed' again.
  732. call setbufvar(_bufNbr, "&buflisted", "1")
  733. " Call any associated function references. g:bufExplorerFuncRef may be
  734. " an individual function reference or it may be a list containing
  735. " function references. It will ignore anything that's not a function
  736. " reference.
  737. "
  738. " See :help FuncRef for more on function references.
  739. if exists("g:BufExplorerFuncRef")
  740. if type(g:BufExplorerFuncRef) == 2
  741. keepj call g:BufExplorerFuncRef()
  742. elseif type(g:BufExplorerFuncRef) == 3
  743. for FncRef in g:BufExplorerFuncRef
  744. if type(FncRef) == 2
  745. keepj call FncRef()
  746. endif
  747. endfor
  748. endif
  749. endif
  750. else
  751. call s:Error("Sorry, that buffer no longer exists, please select another")
  752. call s:DeleteBuffer(_bufNbr, "wipe")
  753. endif
  754. endfunction
  755. " RemoveBuffer {{{2
  756. function! s:RemoveBuffer(mode)
  757. " Are we on a line with a file name?
  758. if line('.') < s:firstBufferLine
  759. return
  760. endif
  761. " Do not allow this buffer to be deleted if it is the last one.
  762. if len(s:MRUList) == 1
  763. call s:Error("Sorry, you are not allowed to delete the last buffer")
  764. return
  765. endif
  766. " These commands are to temporarily suspend the activity of winmanager.
  767. if exists("b:displayMode") && b:displayMode == "winmanager"
  768. call WinManagerSuspendAUs()
  769. end
  770. let _bufNbr = str2nr(getline('.'))
  771. if getbufvar(_bufNbr, '&modified') == 1
  772. call s:Error("Sorry, no write since last change for buffer "._bufNbr.", unable to delete")
  773. return
  774. else
  775. " Okay, everything is good, delete or wipe the buffer.
  776. call s:DeleteBuffer(_bufNbr, a:mode)
  777. endif
  778. " Reactivate winmanager autocommand activity.
  779. if exists("b:displayMode") && b:displayMode == "winmanager"
  780. call WinManagerForceReSize("BufExplorer")
  781. call WinManagerResumeAUs()
  782. end
  783. endfunction
  784. " DeleteBuffer {{{2
  785. function! s:DeleteBuffer(buf, mode)
  786. " This routine assumes that the buffer to be removed is on the current line.
  787. try
  788. " Wipe/Delete buffer from Vim.
  789. if a:mode == "wipe"
  790. execute "silent bwipe" a:buf
  791. else
  792. execute "silent bdelete" a:buf
  793. endif
  794. " Delete the buffer from the list on screen.
  795. setlocal modifiable
  796. normal! "_dd
  797. setlocal nomodifiable
  798. " Delete the buffer from the raw buffer list.
  799. call filter(s:raw_buffer_listing, 'v:val.attributes !~ " '.a:buf.' "')
  800. catch
  801. call s:Error(v:exception)
  802. endtry
  803. endfunction
  804. " ListedAndCurrentTab {{{2
  805. " Returns whether the specified buffer is both listed and associated
  806. " with the current tab
  807. function! s:ListedAndCurrentTab(buf)
  808. return buflisted(a:buf) && s:IsInCurrentTab(a:buf)
  809. endfunction
  810. " Close {{{2
  811. function! s:Close()
  812. " Get only the listed buffers associated with the current tab
  813. let listed = filter(copy(s:MRUList), "s:ListedAndCurrentTab(v:val)")
  814. if len(listed) == 0
  815. let listed = filter(range(1, bufnr('$')), "s:ListedAndCurrentTab(v:val)")
  816. endif
  817. " If we needed to split the main window, close the split one.
  818. if s:splitMode != "" && bufwinnr(s:originBuffer) != -1
  819. execute "wincmd c"
  820. endif
  821. " Check to see if there are anymore buffers listed.
  822. if len(listed) == 0
  823. " Since there are no buffers left to switch to, open a new empty
  824. " buffers.
  825. execute "enew"
  826. else
  827. " Since there are buffers left to switch to, switch to the previous and
  828. " then the current.
  829. for b in reverse(listed[0:1])
  830. execute "keepjumps silent b ".b
  831. endfor
  832. endif
  833. " Clear any messages.
  834. echo
  835. endfunction
  836. " ToggleSplitOutPathName {{{2
  837. function! s:ToggleSplitOutPathName()
  838. let g:bufExplorerSplitOutPathName = !g:bufExplorerSplitOutPathName
  839. call s:RebuildBufferList()
  840. call s:UpdateHelpStatus()
  841. endfunction
  842. " ToggleShowRelativePath {{{2
  843. function! s:ToggleShowRelativePath()
  844. let g:bufExplorerShowRelativePath = !g:bufExplorerShowRelativePath
  845. call s:RebuildBufferList()
  846. call s:UpdateHelpStatus()
  847. endfunction
  848. " ToggleShowTabBuffer {{{2
  849. function! s:ToggleShowTabBuffer()
  850. let g:bufExplorerShowTabBuffer = !g:bufExplorerShowTabBuffer
  851. call s:RebuildBufferList(g:bufExplorerShowTabBuffer)
  852. call s:UpdateHelpStatus()
  853. endfunction
  854. " ToggleOnlyOneTab {{{2
  855. function! s:ToggleOnlyOneTab()
  856. let g:bufExplorerOnlyOneTab = !g:bufExplorerOnlyOneTab
  857. call s:RebuildBufferList()
  858. call s:UpdateHelpStatus()
  859. endfunction
  860. " ToggleShowUnlisted {{{2
  861. function! s:ToggleShowUnlisted()
  862. let g:bufExplorerShowUnlisted = !g:bufExplorerShowUnlisted
  863. let num_bufs = s:RebuildBufferList(g:bufExplorerShowUnlisted == 0)
  864. call s:UpdateHelpStatus()
  865. endfunction
  866. " ToggleFindActive {{{2
  867. function! s:ToggleFindActive()
  868. let g:bufExplorerFindActive = !g:bufExplorerFindActive
  869. call s:UpdateHelpStatus()
  870. endfunction
  871. " RebuildBufferList {{{2
  872. function! s:RebuildBufferList(...)
  873. setlocal modifiable
  874. let curPos = getpos('.')
  875. if a:0 && a:000[0] && (line('$') >= s:firstBufferLine)
  876. " Clear the list first.
  877. execute "silent keepjumps ".s:firstBufferLine.',$d _'
  878. endif
  879. let num_bufs = s:BuildBufferList()
  880. call setpos('.', curPos)
  881. setlocal nomodifiable
  882. return num_bufs
  883. endfunction
  884. " UpdateHelpStatus {{{2
  885. function! s:UpdateHelpStatus()
  886. setlocal modifiable
  887. let text = s:GetHelpStatus()
  888. call setline(s:firstBufferLine - 2, text)
  889. setlocal nomodifiable
  890. endfunction
  891. " MRUCmp {{{2
  892. function! s:MRUCmp(line1, line2)
  893. return index(s:MRUList, str2nr(a:line1)) - index(s:MRUList, str2nr(a:line2))
  894. endfunction
  895. " SortReverse {{{2
  896. function! s:SortReverse()
  897. let g:bufExplorerReverseSort = !g:bufExplorerReverseSort
  898. call s:ReSortListing()
  899. endfunction
  900. " SortSelect {{{2
  901. function! s:SortSelect()
  902. let g:bufExplorerSortBy = get(s:sort_by, index(s:sort_by, g:bufExplorerSortBy) + 1, s:sort_by[0])
  903. call s:ReSortListing()
  904. endfunction
  905. " ReverseSortSelect {{{2
  906. function! s:ReverseSortSelect()
  907. let g:bufExplorerSortBy = get(s:sort_by, index(s:sort_by, g:bufExplorerSortBy) - 1, s:sort_by[-1])
  908. call s:ReSortListing()
  909. endfunction
  910. " ReSortListing {{{2
  911. function! s:ReSortListing()
  912. setlocal modifiable
  913. let curPos = getpos('.')
  914. call s:SortListing()
  915. call s:UpdateHelpStatus()
  916. call setpos('.', curPos)
  917. setlocal nomodifiable
  918. endfunction
  919. " SortListing {{{2
  920. function! s:SortListing()
  921. let sort = s:firstBufferLine.",$sort".((g:bufExplorerReverseSort == 1) ? "!": "")
  922. if g:bufExplorerSortBy == "number"
  923. " Easiest case.
  924. execute sort 'n'
  925. elseif g:bufExplorerSortBy == "name"
  926. " Sort by full path first
  927. execute sort 'ir /\zs\f\+\ze\s\+line/'
  928. if g:bufExplorerSplitOutPathName
  929. execute sort 'ir /\d.\{7}\zs\f\+\ze/'
  930. else
  931. execute sort 'ir /\zs[^\/\\]\+\ze\s*line/'
  932. endif
  933. elseif g:bufExplorerSortBy == "fullpath"
  934. if g:bufExplorerSplitOutPathName
  935. " Sort twice - first on the file name then on the path.
  936. execute sort 'ir /\d.\{7}\zs\f\+\ze/'
  937. endif
  938. execute sort 'ir /\zs\f\+\ze\s\+line/'
  939. elseif g:bufExplorerSortBy == "extension"
  940. " Sort by full path...
  941. execute sort 'ir /\zs\f\+\ze\s\+line/'
  942. " Sort by name...
  943. if g:bufExplorerSplitOutPathName
  944. " Sort twice - first on the file name then on the path.
  945. execute sort 'ir /\d.\{7}\zs\f\+\ze/'
  946. endif
  947. " Sort by extension.
  948. execute sort 'ir /\.\zs\w\+\ze\s/'
  949. elseif g:bufExplorerSortBy == "mru"
  950. let l = getline(s:firstBufferLine, "$")
  951. call sort(l, "<SID>MRUCmp")
  952. if g:bufExplorerReverseSort
  953. call reverse(l)
  954. endif
  955. call setline(s:firstBufferLine, l)
  956. endif
  957. endfunction
  958. " MRUListShow {{{2
  959. function! s:MRUListShow()
  960. echomsg "MRUList=".string(s:MRUList)
  961. endfunction
  962. " Error {{{2
  963. " Display a message using ErrorMsg highlight group.
  964. function! s:Error(msg)
  965. echohl ErrorMsg
  966. echomsg a:msg
  967. echohl None
  968. endfunction
  969. " Warning {{{2
  970. " Display a message using WarningMsg highlight group.
  971. function! s:Warning(msg)
  972. echohl WarningMsg
  973. echomsg a:msg
  974. echohl None
  975. endfunction
  976. " GetTabNbr {{{2
  977. function! s:GetTabNbr(bufNbr)
  978. " Searching buffer bufno, in tabs.
  979. for i in range(tabpagenr("$"))
  980. if index(tabpagebuflist(i + 1), a:bufNbr) != -1
  981. return i + 1
  982. endif
  983. endfor
  984. return 0
  985. endfunction
  986. " GetWinNbr" {{{2
  987. function! s:GetWinNbr(tabNbr, bufNbr)
  988. " window number in tabpage.
  989. let tablist = tabpagebuflist(a:tabNbr)
  990. " Number: 0
  991. " String: 1
  992. " Funcref: 2
  993. " List: 3
  994. " Dictionary: 4
  995. " Float: 5
  996. if type(tablist) == 3
  997. return index(tabpagebuflist(a:tabNbr), a:bufNbr) + 1
  998. else
  999. return 1
  1000. endif
  1001. endfunction
  1002. " StringWidth" {{{2
  1003. if exists('*strwidth')
  1004. function s:StringWidth(s)
  1005. return strwidth(a:s)
  1006. endfunction
  1007. else
  1008. function s:StringWidth(s)
  1009. return len(a:s)
  1010. endfunction
  1011. endif
  1012. " Winmanager Integration {{{2
  1013. let g:BufExplorer_title = "\[Buf\ List\]"
  1014. call s:Set("g:bufExplorerResize", 1)
  1015. call s:Set("g:bufExplorerMaxHeight", 25) " Handles dynamic resizing of the window.
  1016. " function! to start display. Set the mode to 'winmanager' for this buffer.
  1017. " This is to figure out how this plugin was called. In a standalone fashion
  1018. " or by winmanager.
  1019. function! BufExplorer_Start()
  1020. let b:displayMode = "winmanager"
  1021. call s:SetLocalSettings()
  1022. call BufExplorer()
  1023. endfunction
  1024. " Returns whether the display is okay or not.
  1025. function! BufExplorer_IsValid()
  1026. return 0
  1027. endfunction
  1028. " Handles dynamic refreshing of the window.
  1029. function! BufExplorer_Refresh()
  1030. let b:displayMode = "winmanager"
  1031. call s:SetLocalSettings()
  1032. call BufExplorer()
  1033. endfunction
  1034. function! BufExplorer_ReSize()
  1035. if !g:bufExplorerResize
  1036. return
  1037. end
  1038. let nlines = min([line("$"), g:bufExplorerMaxHeight])
  1039. execute nlines." wincmd _"
  1040. " The following lines restore the layout so that the last file line is also
  1041. " the last window line. Sometimes, when a line is deleted, although the
  1042. " window size is exactly equal to the number of lines in the file, some of
  1043. " the lines are pushed up and we see some lagging '~'s.
  1044. let pres = getpos(".")
  1045. normal! $
  1046. let _scr = &scrolloff
  1047. let &scrolloff = 0
  1048. normal! z-
  1049. let &scrolloff = _scr
  1050. call setpos(".", pres)
  1051. endfunction
  1052. " Default values {{{2
  1053. call s:Set("g:bufExplorerDisableDefaultKeyMapping", 0) " Do not disable default key mappings.
  1054. call s:Set("g:bufExplorerDefaultHelp", 1) " Show default help?
  1055. call s:Set("g:bufExplorerDetailedHelp", 0) " Show detailed help?
  1056. call s:Set("g:bufExplorerFindActive", 1) " When selecting an active buffer, take you to the window where it is active?
  1057. call s:Set("g:bufExplorerOnlyOneTab", 1) " If ShowTabBuffer = 1, only store the most recent tab for this buffer.
  1058. call s:Set("g:bufExplorerReverseSort", 0) " Sort in reverse order by default?
  1059. call s:Set("g:bufExplorerShowDirectories", 1) " (Dir's are added by commands like ':e .')
  1060. call s:Set("g:bufExplorerShowRelativePath", 0) " Show listings with relative or absolute paths?
  1061. call s:Set("g:bufExplorerShowTabBuffer", 0) " Show only buffer(s) for this tab?
  1062. call s:Set("g:bufExplorerShowUnlisted", 0) " Show unlisted buffers?
  1063. call s:Set("g:bufExplorerShowNoName", 0) " Show 'No Name' buffers?
  1064. call s:Set("g:bufExplorerSortBy", "mru") " Sorting methods are in s:sort_by:
  1065. call s:Set("g:bufExplorerSplitBelow", &splitbelow) " Should horizontal splits be below or above current window?
  1066. call s:Set("g:bufExplorerSplitOutPathName", 1) " Split out path and file name?
  1067. call s:Set("g:bufExplorerSplitRight", &splitright) " Should vertical splits be on the right or left of current window?
  1068. call s:Set("g:bufExplorerSplitVertSize", 0) " Height for a vertical split. If <=0, default Vim size is used.
  1069. call s:Set("g:bufExplorerSplitHorzSize", 0) " Height for a horizontal split. If <=0, default Vim size is used.
  1070. " Default key mapping {{{2
  1071. if !hasmapto('BufExplorer') && g:bufExplorerDisableDefaultKeyMapping == 0
  1072. nnoremap <script> <silent> <unique> <Leader>be :BufExplorer<CR>
  1073. endif
  1074. if !hasmapto('ToggleBufExplorer') && g:bufExplorerDisableDefaultKeyMapping == 0
  1075. nnoremap <script> <silent> <unique> <Leader>bt :ToggleBufExplorer<CR>
  1076. endif
  1077. if !hasmapto('BufExplorerHorizontalSplit') && g:bufExplorerDisableDefaultKeyMapping == 0
  1078. nnoremap <script> <silent> <unique> <Leader>bs :BufExplorerHorizontalSplit<CR>
  1079. endif
  1080. if !hasmapto('BufExplorerVerticalSplit') && g:bufExplorerDisableDefaultKeyMapping == 0
  1081. nnoremap <script> <silent> <unique> <Leader>bv :BufExplorerVerticalSplit<CR>
  1082. endif
  1083. " vim:ft=vim foldmethod=marker sw=4