hgrev/plugin/hgrev.vim
branchvim-stuff
changeset 29 cc3094023778
parent 28 2b198f0a86fe
equal deleted inserted replaced
28:2b198f0a86fe 29:cc3094023778
     1 " vim: set noet nosta sw=4 ts=4 fdm=marker :
       
     2 "
       
     3 " HGRev
       
     4 " Mahlon E. Smith <mahlon@martini.nu>
       
     5 " $Id$
       
     6 "
       
     7 " Simplistic file revision checker, meant for adding current revision
       
     8 " information to the statusbar, a la:
       
     9 "
       
    10 " 	set statusline=[r%{HGRev()}]
       
    11 "
       
    12 
       
    13 if exists( 'hgrev_loaded' )
       
    14 	finish
       
    15 endif
       
    16 let hgrev_loaded = '$Rev$'
       
    17 
       
    18 " }}}
       
    19 " Defaults for misc settings {{{
       
    20 "
       
    21 if !exists( 'g:hgrevFlags' )
       
    22 	let g:hgrevFlags = '-nbt'
       
    23 endif
       
    24 
       
    25 if !exists( 'g:hgrevAddStatus' )
       
    26 	let g:hgrevAddStatus = 1
       
    27 endif
       
    28 
       
    29 if !exists( 'g:hgrevAutoUpdate' )
       
    30 	let g:hgrevAutoUpdate = 0
       
    31 endif
       
    32 
       
    33 if !exists( 'g:hgrevNoRepoChar' )
       
    34 	let g:hgrevNoRepoChar = '-'
       
    35 endif
       
    36 
       
    37 
       
    38 
       
    39 "}}}
       
    40 " Commands {{{
       
    41 "
       
    42 command! RefreshMercurialRev :call <SID>RefreshMercurialRev()
       
    43 
       
    44 
       
    45 " HGRev() {{{
       
    46 " Return the current buffer rev id from the global dictionary.
       
    47 "
       
    48 function! HGRev()
       
    49 	if exists( 'g:hg_revs' )
       
    50 		let l:key = getcwd() . '/' . bufname('%')
       
    51 		return has_key(g:hg_revs, l:key) ? g:hg_revs[l:key] : g:hgrevNoRepoChar
       
    52 	else
       
    53 		call <SID>RefreshMercurialRev()
       
    54 		call HGRev()
       
    55 	endif
       
    56 endfunction
       
    57 
       
    58 
       
    59 " }}}
       
    60 " RefreshMercurialRev() {{{
       
    61 "
       
    62 " Locate the hgroot and fetch the current rev id, populating the global
       
    63 " dictionary.
       
    64 "
       
    65 function! <SID>RefreshMercurialRev()
       
    66 	if ! exists( 'g:hg_revs' )
       
    67 		let g:hg_revs = {}
       
    68 	endif
       
    69 
       
    70 	" Find the closest HG root for the buffer. 'hg root' won't do it, since
       
    71 	" it works off the cwd, and we need the nearest root from the filename.
       
    72 	"
       
    73 
       
    74 	" (we're unlikely to get lucky finding '.hg' in http:// or similar)
       
    75 	"
       
    76 	if matchstr(bufname('%'), "^[^:/]\\+://") != ''
       
    77 		return
       
    78 	endif
       
    79 
       
    80 	let l:searchpaths = split( expand('%:p:h'), '/' )
       
    81 	let l:dircount = len(l:searchpaths)
       
    82 	let l:root = ''
       
    83 	while l:dircount > 0
       
    84 		let l:root = '/' . join( l:searchpaths[0 : l:dircount], '/' )
       
    85 		if isdirectory( l:root . '/' . '.hg' )
       
    86 			break
       
    87 		endif
       
    88 		let l:dircount = l:dircount - 1
       
    89 	endwhile
       
    90 
       
    91 	if l:dircount == -1
       
    92 		return
       
    93 	endif
       
    94 
       
    95 	let l:key = getcwd() . '/' . bufname('%')
       
    96 
       
    97 	" Find the rev for the repo containing the current file buffer.
       
    98 	"
       
    99 	let l:cmd     = 'hg id ' . g:hgrevFlags . ' ' . l:root
       
   100 	let l:rev     = system( l:cmd )
       
   101 	let l:hg_exit = v:shell_error
       
   102 
       
   103 	if l:hg_exit == 0
       
   104 		let l:rev = substitute( l:rev, '\n', '', 'g' )
       
   105 		let g:hg_revs[ l:key ] = l:rev
       
   106 	endif
       
   107 
       
   108 	" Add file repo status.
       
   109 	"
       
   110 	if g:hgrevAddStatus == 1
       
   111 		let l:cmd     = 'hg stat ' . bufname('%')
       
   112 		let l:stat    = system( l:cmd )
       
   113 		let l:hg_exit = v:shell_error
       
   114 
       
   115 		if l:hg_exit == 0 && len(l:stat) > 0
       
   116 			let l:stat = split( l:stat, '\s\+' )[0]
       
   117 			let g:hg_revs[ l:key ] = g:hg_revs[ l:key ] . ' ' . l:stat
       
   118 		endif
       
   119 	endif
       
   120 
       
   121 	return
       
   122 endfunction
       
   123 "}}}
       
   124 
       
   125 
       
   126 " Refresh the rev for the current buffer on reads/writes.
       
   127 "
       
   128 if g:hgrevAutoUpdate == 1
       
   129 	autocmd BufReadPost          * call <SID>RefreshMercurialRev()
       
   130 	autocmd BufWritePost         * call <SID>RefreshMercurialRev()
       
   131 	autocmd FileChangedShellPost * call <SID>RefreshMercurialRev()
       
   132 endif
       
   133