bufexplorer.vim 45 KB

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