|
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 let l:searchpaths = split( expand('%:p:h'), '/' ) |
|
74 let l:dircount = len(l:searchpaths) |
|
75 let l:root = '' |
|
76 while l:dircount > 0 |
|
77 let l:root = '/' . join( l:searchpaths[0 : l:dircount], '/' ) |
|
78 if isdirectory( l:root . '/' . '.hg' ) |
|
79 break |
|
80 endif |
|
81 let l:dircount = l:dircount - 1 |
|
82 endwhile |
|
83 |
|
84 if l:dircount == -1 |
|
85 return |
|
86 endif |
|
87 |
|
88 let l:key = getcwd() . '/' . bufname('%') |
|
89 |
|
90 " Find the rev for the repo containing the current file buffer. |
|
91 " |
|
92 let l:cmd = 'hg id ' . g:hgrevFlags . ' ' . l:root |
|
93 let l:rev = system( l:cmd ) |
|
94 let l:hg_exit = v:shell_error |
|
95 |
|
96 if l:hg_exit == 0 |
|
97 let l:rev = substitute( l:rev, '\n', '', 'g' ) |
|
98 let g:hg_revs[ l:key ] = l:rev |
|
99 endif |
|
100 |
|
101 " Add file repo status. |
|
102 " |
|
103 if g:hgrevAddStatus == 1 |
|
104 let l:cmd = 'hg stat ' . bufname('%') |
|
105 let l:stat = system( l:cmd ) |
|
106 let l:hg_exit = v:shell_error |
|
107 |
|
108 if l:hg_exit == 0 && len(l:stat) > 0 |
|
109 let l:stat = split( l:stat, '\s\+' )[0] |
|
110 let g:hg_revs[ l:key ] = g:hg_revs[ l:key ] . ' ' . l:stat |
|
111 endif |
|
112 endif |
|
113 |
|
114 return |
|
115 endfunction |
|
116 "}}} |
|
117 |
|
118 |
|
119 " Refresh the rev for the current buffer on reads/writes. |
|
120 " |
|
121 if g:hgrevAutoUpdate == 1 |
|
122 autocmd BufReadPost * call <SID>RefreshMercurialRev() |
|
123 autocmd BufWritePost * call <SID>RefreshMercurialRev() |
|
124 autocmd FileChangedShellPost * call <SID>RefreshMercurialRev() |
|
125 endif |
|
126 |