Initial version of mercurial support.
[vcscommand.git] / plugin / vcshg.vim
blobdeff5cda95fff1a3540eb462fbadb397c7443b58
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:DoCommand(cmd, cmdName, statusText, options) {{{2
71 " Wrapper to VCSCommandDoCommand to add the name of the HG executable to the
72 " command argument.
73 function! s:DoCommand(cmd, cmdName, statusText, options)
74         if VCSCommandGetVCSType(expand('%')) == 'HG'
75                 let fullCmd = VCSCommandGetOption('VCSCommandHGExec', 'hg') . ' ' . a:cmd
76                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
77         else
78                 throw 'HG VCSCommand plugin called on non-HG item.'
79         endif
80 endfunction
82 " Section: VCS function implementations {{{1
84 " Function: s:hgFunctions.Identify(buffer) {{{2
85 function! s:hgFunctions.Identify(buffer)
86         call system(VCSCommandGetOption('VCSCommandHGExec', 'hg') . ' root')
87         if(v:shell_error)
88                 return 0
89         else
90                 return g:VCSCOMMAND_IDENTIFY_INEXACT
91         endif
92 endfunction
94 " Function: s:hgFunctions.Add() {{{2
95 function! s:hgFunctions.Add(argList)
96         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
97 endfunction
99 " Function: s:hgFunctions.Annotate(argList) {{{2
100 function! s:hgFunctions.Annotate(argList)
101         if len(a:argList) == 0
102                 if &filetype == 'HGAnnotate'
103                         " Perform annotation of the version indicated by the current line.
104                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
105                         let options = ' -r' . caption
106                 else
107                         let caption = ''
108                         let options = ''
109                 endif
110         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
111                 let caption = a:argList[0]
112                 let options = ' -r' . caption
113         else
114                 let caption = join(a:argList, ' ')
115                 let options = ' ' . caption
116         endif
118         let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
119         if resultBuffer > 0
120                 set filetype=HGAnnotate
121         endif
122         return resultBuffer
123 endfunction
125 " Function: s:hgFunctions.Commit(argList) {{{2
126 function! s:hgFunctions.Commit(argList)
127         let resultBuffer = s:DoCommand('commit -l "' . a:argList[0] . '"', 'commit', '', {})
128         if resultBuffer == 0
129                 echomsg 'No commit needed.'
130         endif
131 endfunction
133 " Function: s:hgFunctions.Delete() {{{2
134 function! s:hgFunctions.Delete(argList)
135         return s:DoCommand(join(['remove'] + a:argList, ' '), 'remove', join(a:argList, ' '), {})
136 endfunction
138 " Function: s:hgFunctions.Diff(argList) {{{2
139 function! s:hgFunctions.Diff(argList)
140         if len(a:argList) == 0
141                 let revOptions = []
142                 let caption = ''
143         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
144                 let revOptions = ['-r' . join(a:argList, ':')]
145                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
146         else
147                 " Pass-through
148                 let caption = join(a:argList, ' ')
149                 let revOptions = a:argList
150         endif
152         let hgDiffExt = VCSCommandGetOption('VCSCommandHGDiffExt', '')
153         if hgDiffExt == ''
154                 let diffExt = []
155         else
156                 let diffExt = ['--diff-cmd ' . hgDiffExt]
157         endif
159         let hgDiffOpt = VCSCommandGetOption('VCSCommandHGDiffOpt', '')
160         if hgDiffOpt == ''
161                 let diffOptions = []
162         else
163                 let diffOptions = ['-x -' . hgDiffOpt]
164         endif
166         let resultBuffer = s:DoCommand(join(['diff'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
167         if resultBuffer > 0
168                 set filetype=diff
169         else
170                 if hgDiffExt == ''
171                         echomsg 'No differences found'
172                 endif
173         endif
174         return resultBuffer
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 = system(VCSCommandGetOption('VCSCommandHGExec', 'hg') . ' 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 = system(VCSCommandGetOption('VCSCommandHGExec', 'hg') . ' parents "' . fileName . '"')
202         let [revision] = matchlist(parentsText, '^changeset:\s\+\(\S\+\)\n')[1]
204         let logText = system(VCSCommandGetOption('VCSCommandHGExec', 'hg') . ' 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 "       let resultBuffer = s:DoCommand('cat --non-interactive' . versionOption, 'review', versiontag, {})
251         let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag, {})
252         if resultBuffer > 0
253                 let &filetype = getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
254         endif
255         return resultBuffer
256 endfunction
258 " Function: s:hgFunctions.Status(argList) {{{2
259 function! s:hgFunctions.Status(argList)
260         let options = ['-u', '-v']
261         if len(a:argList) == 0
262                 let options = a:argList
263         endif
264         return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
265 endfunction
267 " Function: s:hgFunctions.Update(argList) {{{2
268 function! s:hgFunctions.Update(argList)
269         return s:DoCommand('update', 'update', '', {})
270 endfunction
272 " Section: Plugin Registration {{{1
273 call VCSCommandRegisterModule('HG', expand('<sfile>'), s:hgFunctions, [])
275 let &cpo = s:save_cpo