Source

vermilion / plugin / vermilion.vim

Full commit
" ============================================================================
" File:        vermilion.vim
" Description: A Vim plugin for working with Mercurial.
" Maintainer:  Steve Losh <steve@stevelosh.com>
" License:     MIT/X11 -- look it up.
" Notes:       This plugin is still in alpha.  It will eat your data.
"
" ============================================================================

"{{{ Init

"if !exists('g:vermilion_debug') && (exists('g:vermilion_disable') || exists('loaded_vermilion') || &cp)"{{{
    "finish
"endif
"let loaded_vermilion = 1"}}}

if !exists('g:vermilion_debug')"{{{
    let g:vermilion_debug = 0
endif"}}}
if !exists('g:vermilion_width')"{{{
    let g:vermilion_width = 50
endif"}}}

"}}}

"{{{ Buffer/Window Management

function! s:GoToWindowForBufferName(name)"{{{
    if bufwinnr(bufnr(a:name)) != -1
        exe bufwinnr(bufnr(a:name)) . "wincmd w"
        return 1
    else
        return 0
    endif
endfunction"}}}

function! s:ResizeVermilionPane(width, backto)"{{{
    call s:GoToWindowForBufferName('__Vermilion__')
    exe "vertical resize " . a:width
    exe a:backto . "wincmd w"
endfunction"}}}
function! s:OpenVermilionPane()"{{{
    let existing_vermilion_buffer = bufnr("__Vermilion__")

    if existing_vermilion_buffer == -1
        exe "topleft vnew __Vermilion__"
        call s:ResizeVermilionPane(g:vermilion_width, winnr())
    else
        let existing_vermilion_window = bufwinnr(existing_vermilion_buffer)

        if existing_vermilion_window != -1
            if winnr() != existing_vermilion_window
                exe existing_vermilion_window . "wincmd w"
            endif
        else
            exe "topleft vsplit +buffer" . existing_vermilion_buffer
            call s:ResizeVermilionPane(g:vermilion_width, winnr())
        endif
    endif

    call s:SetupVermilionPane()
endfunction"}}}
function! s:CloseVermilionPane()"{{{
    if s:GoToWindowForBufferName('__Vermilion__')
        quit
    endif

    exe bufwinnr(g:vermilion_target_n) . "wincmd w"
endfunction"}}}
function! s:VermilionPaneIsVisible()"{{{
    if bufwinnr(bufnr("__Vermilion__")) != -1
        return 1
    else
        return 0
    endif
endfunction"}}}
function! s:SetupVermilionPane()"{{{
    setlocal buftype=nofile
    setlocal bufhidden=hide
    setlocal noswapfile
    setlocal nobuflisted
    setlocal nomodifiable
    setlocal nolist
    setlocal nonumber
    setlocal norelativenumber
    setlocal nowrap
endfunction"}}}

"}}}

"{{{ Log

function! s:RunLog(verbose, filename)"{{{
    setlocal modifiable
    normal! ggdG
    if a:verbose
        execute "read !hg log --template '[{node|short}] {desc|firstline|strip} ({author|person})\\n'" a:filename
    else
        execute "read !hg log --template '[{rev}] {desc|firstline|strip}\\n'" a:filename
    endif
    setlocal nomodifiable
endfunction"}}}
function! s:ToggleLog(verbose, onfile)"{{{
    if s:VermilionPaneIsVisible()
        if bufwinnr(bufnr("__Vermilion__")) != bufwinnr('')
            let g:vermilion_target_n = bufnr('')
            let g:vermilion_target_f = @%
        endif
        call s:CloseVermilionPane()
    else
        let g:vermilion_target_n = bufnr('')
        let g:vermilion_target_f = @%
        call s:OpenVermilionPane()

        if a:onfile
            call s:RunLog(a:verbose, g:vermilion_target_f)
        else
            call s:RunLog(a:verbose, '')
        endif
    endif
endfunction"}}}

"}}}
"{{{ Graphlog

function! s:RunGraphlog(verbose)"{{{
    setlocal modifiable
    normal! ggdG
    if a:verbose
        read !hg glog --template '[{node|short}] {desc|firstline|strip} ({author|person})\n'
    else
        read !hg glog --template '[{rev}] {desc|firstline|strip}\n'
    endif
    setlocal nomodifiable
endfunction"}}}
function! s:ToggleGraphlog(verbose)"{{{
    if s:VermilionPaneIsVisible()
        if bufwinnr(bufnr("__Vermilion__")) != bufwinnr('')
            let g:vermilion_target_n = bufnr('')
            let g:vermilion_target_f = @%
        endif
        call s:CloseVermilionPane()
    else
        let g:vermilion_target_n = bufnr('')
        let g:vermilion_target_f = @%
        call s:OpenVermilionPane()
        call s:RunGraphlog(a:verbose)
    endif
endfunction"}}}

"}}}

"{{{ Misc

autocmd BufNewFile,BufRead __Vermilion__ call s:SetupVermilionPane()

command! -nargs=0 VermilionClose           call s:CloseVermilionPane()
command! -nargs=0 VermilionLog             call s:ToggleLog(0, 0)
command! -nargs=0 VermilionLogVerbose      call s:ToggleLog(1, 0)
command! -nargs=0 VermilionLogFile         call s:ToggleLog(0, 1)
command! -nargs=0 VermilionLogVerboseFile  call s:ToggleLog(1, 1)
command! -nargs=0 VermilionGraphlog        call s:ToggleGraphlog(0)
command! -nargs=0 VermilionGraphlogVerbose call s:ToggleGraphlog(1)

nnoremap vvv  :VermilionClose<cr>
nnoremap vvl  :VermilionLog<cr>
nnoremap vvL  :VermilionLogVerbose<cr>
nnoremap vvfl :VermilionLogFile<cr>
nnoremap vvfL :VermilionLogVerboseFile<cr>
nnoremap vvg  :VermilionGraphlog<cr>
nnoremap vvG  :VermilionGraphlogVerbose<cr>

"}}}