bufexplorer.vim 42 KB

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