bufexplorer.vim 45 KB

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