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