ソースを参照

Merge pull request #128 from drmikehenry/original-window

Add `O` command to open in the original window at BufExplorer launch.
jlanzarotta 9 ヶ月 前
コミット
c116681b27
2 ファイル変更25 行追加3 行削除
  1. 4 0
      doc/bufexplorer.txt
  2. 21 3
      plugin/bufexplorer.vim

+ 4 - 0
doc/bufexplorer.txt

@@ -88,6 +88,8 @@ Commands to use once exploring:
  f             Open selected buffer in another window below the current.
  o             Opens the buffer that is under the cursor into the current
                window.
+ O             Opens the buffer that is under the cursor into the window where
+               BufExplorer was originally launched.
  p             Toggles the showing of a split filename/pathname.
  q             Exit/Close bufexplorer.
  r             Reverses the order the buffers are listed in.
@@ -151,6 +153,7 @@ provides.  The mappings are buffer-local to BufExplorer:
   <Plug>(BufExplorer_Close)                    Close BufExplorer window
   <Plug>(BufExplorer_OpenBuffer)               Open buffer
   <Plug>(BufExplorer_OpenBufferAsk)            Prompt for buffer & open
+  <Plug>(BufExplorer_OpenBufferOriginalWindow) Open buffer in original window
   <Plug>(BufExplorer_OpenBufferSplitAbove)     Horizontal split & open above
   <Plug>(BufExplorer_OpenBufferSplitBelow)     Horizontal split & open below
   <Plug>(BufExplorer_OpenBufferSplitLeft)      Vertical split & open left
@@ -183,6 +186,7 @@ BufExplorer's buffer:
   nmap <nowait> <buffer> f        <Plug>(BufExplorer_OpenBufferSplitBelow)
   nmap <nowait> <buffer> F        <Plug>(BufExplorer_OpenBufferSplitAbove)
   nmap <nowait> <buffer> o        <Plug>(BufExplorer_OpenBuffer)
+  nmap <nowait> <buffer> O        <Plug>(BufExplorer_OpenBufferOriginalWindow)
   nmap <nowait> <buffer> p        <Plug>(BufExplorer_ToggleSplitOutPathName)
   nmap <nowait> <buffer> q        <Plug>(BufExplorer_Close)
   nmap <nowait> <buffer> r        <Plug>(BufExplorer_ToggleReverseSort)

+ 21 - 3
plugin/bufexplorer.vim

@@ -117,7 +117,6 @@ endfunction
 " Script variables {{{2
 let s:MRU_Exclude_List = ["[BufExplorer]","__MRU_Files__","[Buf\ List]"]
 let s:name = '[BufExplorer]'
-let s:originBuffer = 0
 " Buffer number of the BufExplorer window.
 let s:bufExplorerBuffer = 0
 let s:running = 0
@@ -600,9 +599,8 @@ function! BufExplorer()
         return
     endif
 
-    " Add zero to ensure the variable is treated as a number.
-    let s:originBuffer = bufnr("%") + 0
     let s:tabIdAtLaunch = s:MRUEnsureTabId(tabpagenr())
+    let s:windowAtLaunch = winnr()
 
     " Forget any cached MRU ordering from previous invocations.
     unlet! s:mruOrder
@@ -661,6 +659,9 @@ endfunction
 " Tracks `tabId` at BufExplorer launch.
 let s:tabIdAtLaunch = ''
 
+" Tracks window number at BufExplorer launch.
+let s:windowAtLaunch = 0
+
 " DisplayBufferList {{{2
 function! s:DisplayBufferList()
     setlocal buftype=nofile
@@ -704,6 +705,7 @@ function! s:MapKeys()
     nnoremap <silent> <buffer> <Plug>(BufExplorer_Close)                    :call <SID>Close()<CR>
     nnoremap <silent> <buffer> <Plug>(BufExplorer_OpenBuffer)               :call <SID>SelectBuffer()<CR>
     nnoremap <silent> <buffer> <Plug>(BufExplorer_OpenBufferAsk)            :call <SID>SelectBuffer("ask")<CR>
+    nnoremap <silent> <buffer> <Plug>(BufExplorer_OpenBufferOriginalWindow) :call <SID>SelectBuffer("original_window")<CR>
     nnoremap <silent> <buffer> <Plug>(BufExplorer_OpenBufferSplitAbove)     :call <SID>SelectBuffer("split", "st")<CR>
     nnoremap <silent> <buffer> <Plug>(BufExplorer_OpenBufferSplitBelow)     :call <SID>SelectBuffer("split", "sb")<CR>
     nnoremap <silent> <buffer> <Plug>(BufExplorer_OpenBufferSplitLeft)      :call <SID>SelectBuffer("split", "vl")<CR>
@@ -737,6 +739,7 @@ function! s:MapKeys()
     nmap <nowait> <buffer> f                <Plug>(BufExplorer_OpenBufferSplitBelow)
     nmap <nowait> <buffer> F                <Plug>(BufExplorer_OpenBufferSplitAbove)
     nmap <nowait> <buffer> o                <Plug>(BufExplorer_OpenBuffer)
+    nmap <nowait> <buffer> O                <Plug>(BufExplorer_OpenBufferOriginalWindow)
     nmap <nowait> <buffer> p                <Plug>(BufExplorer_ToggleSplitOutPathName)
     nmap <nowait> <buffer> q                <Plug>(BufExplorer_Close)
     nmap <nowait> <buffer> r                <Plug>(BufExplorer_ToggleReverseSort)
@@ -862,6 +865,7 @@ function! s:CreateHelp()
         call add(header, '" D : wipe buffer')
         call add(header, '" F : open buffer in another window above the current')
         call add(header, '" f : open buffer in another window below the current')
+        call add(header, '" O : open buffer in original window')
         call add(header, '" p : toggle splitting of file and path name')
         call add(header, '" q : quit')
         call add(header, '" r : reverse sort')
@@ -1162,6 +1166,15 @@ function! s:MakeLines(table)
 endfunction
 
 " SelectBuffer {{{2
+" Valid arguments:
+"   `()`                    Open in current window.
+"   `("ask")`               Prompt for buffer, then open in current window.
+"   `("original_window")`   Open in original window.
+"   `("split", "st")`       Open in horizontal split above current window.
+"   `("split", "sb")`       Open in horizontal split below current window.
+"   `("split", "vl")`       Open in vertical split left of current window.
+"   `("split", "vr")`       Open in vertical split right of current window.
+"   `("tab")`               Open in a new tab.
 function! s:SelectBuffer(...)
     " Sometimes messages are not cleared when we get here so it looks like an
     " error has occurred when it really has not.
@@ -1237,6 +1250,11 @@ function! s:SelectBuffer(...)
             else " = sb
                 execute "belowright sb "._bufNbr
             endif
+        " Are we supposed to open the selected buffer in the original window?
+        elseif (a:0 == 1) && (a:1 == "original_window")
+            call s:Close()
+            execute s:windowAtLaunch . "wincmd w"
+            execute "keepjumps keepalt silent b!" _bufNbr
         else
             " Request to open in current (BufExplorer) window.
             if g:bufExplorerFindActive && tabNbr > 0