vcshg: Added split annotate view.
[vcscommand.git] / plugin / vcshg.vim
bloba053f5ed9d6882133f2b0b877ac60077e063afa5
1 " vim600: set foldmethod=marker:
3 " Mercurial extension for VCSCommand.
5 " Version:       VCS development
6 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
7 " License:
8 " Copyright (c) 2009 Bob Hiestand
10 " Permission is hereby granted, free of charge, to any person obtaining a copy
11 " of this software and associated documentation files (the "Software"), to
12 " deal in the Software without restriction, including without limitation the
13 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14 " sell copies of the Software, and to permit persons to whom the Software is
15 " furnished to do so, subject to the following conditions:
17 " The above copyright notice and this permission notice shall be included in
18 " all copies or substantial portions of the Software.
20 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 " IN THE SOFTWARE.
28 " Section: Documentation {{{1
30 " Options documentation: {{{2
32 " VCSCommandHGExec
33 "   This variable specifies the mercurial executable.  If not set, it defaults
34 "   to 'hg' executed from the user's executable path.
36 " VCSCommandHGDiffExt
37 "   This variable, if set, sets the external diff program used by Subversion.
39 " VCSCommandHGDiffOpt
40 "   This variable, if set, determines the options passed to the hg diff
41 "   command (such as 'u', 'w', or 'b').
43 " Section: Plugin header {{{1
45 if exists('VCSCommandDisableAll')
46         finish
47 endif
49 if v:version < 700
50         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
51         finish
52 endif
54 runtime plugin/vcscommand.vim
56 if !executable(VCSCommandGetOption('VCSCommandHGExec', 'hg'))
57         " HG is not installed
58         finish
59 endif
61 let s:save_cpo=&cpo
62 set cpo&vim
64 " Section: Variable initialization {{{1
66 let s:hgFunctions = {}
68 " Section: Utility functions {{{1
70 " Function: s:Executable() {{{2
71 " Returns the executable used to invoke hg suitable for use in a shell
72 " command.
73 function! s:Executable()
74         return shellescape(VCSCommandGetOption('VCSCommandHGExec', 'hg'))
75 endfunction
77 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
78 " Wrapper to VCSCommandDoCommand to add the name of the HG executable to the
79 " command argument.
80 function! s:DoCommand(cmd, cmdName, statusText, options)
81         if VCSCommandGetVCSType(expand('%')) == 'HG'
82                 let fullCmd = s:Executable() . ' ' . a:cmd
83                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
84         else
85                 throw 'HG VCSCommand plugin called on non-HG item.'
86         endif
87 endfunction
89 " Section: VCS function implementations {{{1
91 " Function: s:hgFunctions.Identify(buffer) {{{2
92 function! s:hgFunctions.Identify(buffer)
93         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
94         try
95                 call s:VCSCommandUtility.system(s:Executable() . ' root')
96                 if(v:shell_error)
97                         return 0
98                 else
99                         return g:VCSCOMMAND_IDENTIFY_INEXACT
100                 endif
101         finally
102                 call VCSCommandChdir(oldCwd)
103         endtry
104 endfunction
106 " Function: s:hgFunctions.Add() {{{2
107 function! s:hgFunctions.Add(argList)
108         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
109 endfunction
111 " Function: s:hgFunctions.Annotate(argList) {{{2
112 function! s:hgFunctions.Annotate(argList)
113         if len(a:argList) == 0
114                 if &filetype == 'HGAnnotate'
115                         " Perform annotation of the version indicated by the current line.
116                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
117                         let options = ' -r' . caption
118                 else
119                         let caption = ''
120                         let options = ' -un'
121                 endif
122         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
123                 let caption = a:argList[0]
124                 let options = ' -un -r' . caption
125         else
126                 let caption = join(a:argList, ' ')
127                 let options = ' ' . caption
128         endif
130         let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
131         if resultBuffer > 0
132                 set filetype=HGAnnotate
133         endif
134         return resultBuffer
135 endfunction
137 " Function: s:hgFunctions.Commit(argList) {{{2
138 function! s:hgFunctions.Commit(argList)
139         let resultBuffer = s:DoCommand('commit -l "' . a:argList[0] . '"', 'commit', '', {})
140         if resultBuffer == 0
141                 echomsg 'No commit needed.'
142         endif
143 endfunction
145 " Function: s:hgFunctions.Delete() {{{2
146 function! s:hgFunctions.Delete(argList)
147         return s:DoCommand(join(['remove'] + a:argList, ' '), 'remove', join(a:argList, ' '), {})
148 endfunction
150 " Function: s:hgFunctions.Diff(argList) {{{2
151 function! s:hgFunctions.Diff(argList)
152         if len(a:argList) == 0
153                 let revOptions = []
154                 let caption = ''
155         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
156                 let revOptions = ['-r' . join(a:argList, ':')]
157                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
158         else
159                 " Pass-through
160                 let caption = join(a:argList, ' ')
161                 let revOptions = a:argList
162         endif
164         let hgDiffExt = VCSCommandGetOption('VCSCommandHGDiffExt', '')
165         if hgDiffExt == ''
166                 let diffExt = []
167         else
168                 let diffExt = ['--diff-cmd ' . hgDiffExt]
169         endif
171         let hgDiffOpt = VCSCommandGetOption('VCSCommandHGDiffOpt', '')
172         if hgDiffOpt == ''
173                 let diffOptions = []
174         else
175                 let diffOptions = ['-x -' . hgDiffOpt]
176         endif
178         let resultBuffer = s:DoCommand(join(['diff'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
179         if resultBuffer > 0
180                 set filetype=diff
181         else
182                 if hgDiffExt == ''
183                         echomsg 'No differences found'
184                 endif
185         endif
186         return resultBuffer
187 endfunction
189 " Function: s:hgFunctions.Info(argList) {{{2
190 function! s:hgFunctions.Info(argList)
191         return s:DoCommand(join(['log --limit 1'] + a:argList, ' '), 'log', join(a:argList, ' '), {})
192 endfunction
194 " Function: s:hgFunctions.GetBufferInfo() {{{2
195 " Provides version control details for the current file.  Current version
196 " number and current repository version number are required to be returned by
197 " the vcscommand plugin.
198 " Returns: List of results:  [revision, repository, branch]
200 function! s:hgFunctions.GetBufferInfo()
201         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
202         let fileName = bufname(originalBuffer)
203         let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -- "' . fileName . '"')
204         if(v:shell_error)
205                 return []
206         endif
208         " File not under HG control.
209         if statusText =~ '^?'
210                 return ['Unknown']
211         endif
213         let parentsText = s:VCSCommandUtility.system(s:Executable() . ' parents -- "' . fileName . '"')
214         let revision = matchlist(parentsText, '^changeset:\s\+\(\S\+\)\n')[1]
216         let logText = s:VCSCommandUtility.system(s:Executable() . ' log -- "' . fileName . '"')
217         let repository = matchlist(logText, '^changeset:\s\+\(\S\+\)\n')[1]
219         if revision == ''
220                 " Error
221                 return ['Unknown']
222         elseif statusText =~ '^A'
223                 return ['New', 'New']
224         else
225                 return [revision, repository]
226         endif
227 endfunction
229 " Function: s:hgFunctions.Log(argList) {{{2
230 function! s:hgFunctions.Log(argList)
231         if len(a:argList) == 0
232                 let options = []
233                 let caption = ''
234         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
235                 let options = ['-r' . join(a:argList, ':')]
236                 let caption = options[0]
237         else
238                 " Pass-through
239                 let options = a:argList
240                 let caption = join(a:argList, ' ')
241         endif
243         let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
244         return resultBuffer
245 endfunction
247 " Function: s:hgFunctions.Revert(argList) {{{2
248 function! s:hgFunctions.Revert(argList)
249         return s:DoCommand('revert', 'revert', '', {})
250 endfunction
252 " Function: s:hgFunctions.Review(argList) {{{2
253 function! s:hgFunctions.Review(argList)
254         if len(a:argList) == 0
255                 let versiontag = '(current)'
256                 let versionOption = ''
257         else
258                 let versiontag = a:argList[0]
259                 let versionOption = ' -r ' . versiontag . ' '
260         endif
262 "       let resultBuffer = s:DoCommand('cat --non-interactive' . versionOption, 'review', versiontag, {})
263         let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag, {})
264         if resultBuffer > 0
265                 let &filetype = getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
266         endif
267         return resultBuffer
268 endfunction
270 " Function: s:hgFunctions.Status(argList) {{{2
271 function! s:hgFunctions.Status(argList)
272         let options = ['-u', '-v']
273         if len(a:argList) == 0
274                 let options = a:argList
275         endif
276         return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
277 endfunction
279 " Function: s:hgFunctions.Update(argList) {{{2
280 function! s:hgFunctions.Update(argList)
281         return s:DoCommand('update', 'update', '', {})
282 endfunction
284 " Annotate setting {{{2
285 let s:hgFunctions.AnnotateSplitRegex = '\d\+: '
287 " Section: Plugin Registration {{{1
288 let s:VCSCommandUtility = VCSCommandRegisterModule('HG', expand('<sfile>'), s:hgFunctions, [])
290 let &cpo = s:save_cpo