popwin.el の Vim 版考えている

需要があるようなのでちょっと考えている。
と、いっても popwin.el はよく知らないので、それと同等の機能になるかはわかりませんが。
とりあえず、簡易的に実装してみた感じ。

let s:config = {
\   "help" : {
\       "match" : {
\           "filetype" : "help"
\       },
\       "is_close_focus_out" : 1,
\       "height" : 20,
\       "move" : "bottom",
\   },
\   "unite" : {
\       "match" : {
\           "bufname" : '\[unite\]',
\       },
\       "is_close_focus_out" : 1,
\   },
\   "quickrun" : {
\       "match" : {
\           "bufname" : '\[quickrun output\]',
\       },
\       "is_close_focus_out" : 0,
\   },
\}


function! s:matcher(match, context)
    return (has_key(a:match, "bufname")  ? a:context.bufname =~# a:match.bufname : 1)
\       && (has_key(a:match, "filetype") ? a:context.filetype =~# a:match.filetype : 1)
endfunction


function! s:make_current_context()
    return {
\       "filetype" : &filetype,
\       "bufname"  : bufname("%")
\   }
endfunction


function! s:get_config(context)
    for [name, config] in items(s:config)
        if s:matcher(get(config, "match", {}), a:context)
            return [name, config]
        endif
    endfor
    return ["", {}]
endfunction


function! s:on_window_open()
    let context = s:make_current_context()
    let [name, config] = s:get_config(context)
    if empty(config)
        return
    endif

    let w:config = config
    let w:name = name

    if has_key(config, "width")
        execute "vertical resize" config.height
    endif

    if has_key(config, "height")
        execute "resize" config.height
    endif

    let move_keys = {
\       "top"    : "\<C-w>K",
\       "bottom" : "\<C-w>J",
\       "left"   : "\<C-w>H",
\       "right"  : "\<C-w>L",
\   }
    
    let move_key = get(move_keys, get(config, "move", ""), "")
    if !empty(move_key)
        execute "normal!" move_key
    endif

endfunction


function! s:on_close()
    let name = get(w:, "name")
    if empty(name)
        return
    endif
    if get(s:config[name], "is_close_focus_out", 0)
        close
    endif
endfunction


augroup test
    autocmd!
    autocmd BufWinEnter * call s:on_window_open()
    autocmd WinLeave * call s:on_close()
augroup END


いま考えているのはこんな感じ。
実装は置いておいて、設定方法は最初のうちにある程度固めておきたいところ。
どうするのがいいかなぁ。