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