hgrev/plugin/hgrev.vim
author miguel
Wed, 12 Sep 2012 01:49:50 +0100
branchvim-stuff
changeset 26 e3edddb48a91
parent 25 efe9199dbd41
child 27 595320486f69
permissions -rw-r--r--
fixed typo in help bookmark for Author
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
	"
25
efe9199dbd41 try not to mess with buffers that are regular files (eg: netrw)
miguel
parents: 15
diff changeset
    73
efe9199dbd41 try not to mess with buffers that are regular files (eg: netrw)
miguel
parents: 15
diff changeset
    74
	if matchstr(bufname('%'), "^[^:/]\\+://") != ''
efe9199dbd41 try not to mess with buffers that are regular files (eg: netrw)
miguel
parents: 15
diff changeset
    75
		" not on this buffer...
efe9199dbd41 try not to mess with buffers that are regular files (eg: netrw)
miguel
parents: 15
diff changeset
    76
		" (we're unlikely to get lucky finding '.hg' in http:// or similar)
efe9199dbd41 try not to mess with buffers that are regular files (eg: netrw)
miguel
parents: 15
diff changeset
    77
		return
efe9199dbd41 try not to mess with buffers that are regular files (eg: netrw)
miguel
parents: 15
diff changeset
    78
	endif
efe9199dbd41 try not to mess with buffers that are regular files (eg: netrw)
miguel
parents: 15
diff changeset
    79
15
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
	let l:searchpaths = split( expand('%:p:h'), '/' )
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
	let l:dircount = len(l:searchpaths)
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
	let l:root = ''
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
	while l:dircount > 0
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
		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
    85
		if isdirectory( l:root . '/' . '.hg' )
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
			break
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
		endif
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
		let l:dircount = l:dircount - 1
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
	endwhile
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
	if l:dircount == -1
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
		return
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
	endif
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
	let l:key = getcwd() . '/' . bufname('%')
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
	" 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
    98
	"
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
	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
   100
	let l:rev     = system( l:cmd )
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
	let l:hg_exit = v:shell_error
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 l:hg_exit == 0
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
		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
   105
		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
   106
	endif
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
	" Add file repo status.
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
	"
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
	if g:hgrevAddStatus == 1
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
		let l:cmd     = 'hg stat ' . bufname('%')
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
		let l:stat    = system( l:cmd )
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
		let l:hg_exit = v:shell_error
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
		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
   116
			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
   117
			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
   118
		endif
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
	endif
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
	return
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
endfunction
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123
"}}}
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   126
" 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
   127
"
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
if g:hgrevAutoUpdate == 1
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
	autocmd BufReadPost          * call <SID>RefreshMercurialRev()
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
	autocmd BufWritePost         * call <SID>RefreshMercurialRev()
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   131
	autocmd FileChangedShellPost * call <SID>RefreshMercurialRev()
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
endif
285603587539 Initial commit of the HGRev Vim plugin.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133