当新缓冲区在预览窗口中打开时,Autoclose缓冲区占用Vim的预览窗口

时间:2014-04-04 04:10:57

标签: vim unite.vim

我经常使用Unite.vim滚动浏览Vim中的文件列表。有时我使用Unite的auto-preview功能让我在滚动它时在预览窗口中打开每个文件。如果我滚动浏览大量文件,这会给我留下一堆我不需要的开放缓冲区。当在预览窗口中打开新缓冲区时,如何使当前占用预览窗口的缓冲区关闭?

1 个答案:

答案 0 :(得分:1)

通过查看Unite源代码,复制预览功能,并直接在此功能上进行修改,我可以解决这个问题,以便垂直打开预览窗口:

function! s:preview.func(candidate)
  let buflisted = buflisted(
        \ unite#util#escape_file_searching(
        \ a:candidate.action__path))
  if filereadable(a:candidate.action__path)
    " If execute this command, unite.vim will be affected by events.
    noautocmd silent execute 'vert pedit!'  " added vert
          \ fnameescape(a:candidate.action__path)
    call SetWidthToHalfScreen()  " added to resize preview window
    let prev_winnr = winnr('#')
    let winnr = winnr()
    wincmd P
    doautoall BufRead
    setlocal nomodified
    execute prev_winnr.'wincmd w'
    execute winnr.'wincmd w'
  endif
  if !buflisted
    call unite#add_previewed_buffer_list(
        \ bufnr(unite#util#escape_file_searching(
        \       a:candidate.action__path)))
  endif
endfunction
call unite#custom#action("openable", "preview", s:preview)
call unite#custom#action("file", "preview", s:preview)

function! SetWidthToHalfScreen()
  let diff = 100 - winwidth(winnr())
  exec "vert resize " . (diff > 0 ? '+' : '-') . abs(diff)
endfunction