bufexplorer.vim 42 KB

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