bufexplorer.vim 45 KB

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