1 " vim600: set foldmethod=marker:
3 " Mercurial extension for VCSCommand.
5 " Maintainer: Bob Hiestand <bob.hiestand@gmail.com>
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
27 " Section: Documentation {{{1
29 " Options documentation: {{{2
32 " This variable specifies the mercurial executable. If not set, it defaults
33 " to 'hg' executed from the user's executable path.
36 " This variable, if set, sets the external diff program used by Subversion.
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')
49 echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
53 runtime plugin/vcscommand.vim
55 if !executable(VCSCommandGetOption('VCSCommandHGExec', 'hg'))
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
72 function! s:Executable()
73 return shellescape(VCSCommandGetOption('VCSCommandHGExec', 'hg'))
76 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
77 " Wrapper to VCSCommandDoCommand to add the name of the HG executable to the
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)
84 throw 'HG VCSCommand plugin called on non-HG item.'
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)))
94 call s:VCSCommandUtility.system(s:Executable() . ' root')
98 return g:VCSCOMMAND_IDENTIFY_INEXACT
101 call VCSCommandChdir(oldCwd)
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, ' '), {})
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+\zs\d+')
116 let options = ' -r' . caption
121 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
122 let caption = a:argList[0]
123 let options = ' -un -r' . caption
125 let caption = join(a:argList, ' ')
126 let options = ' ' . caption
129 return s:DoCommand('blame' . options, 'annotate', caption, {})
132 " Function: s:hgFunctions.Commit(argList) {{{2
133 function! s:hgFunctions.Commit(argList)
135 return s:DoCommand('commit -v -l "' . a:argList[0] . '"', 'commit', '', {})
136 catch /Version control command failed.*nothing changed/
137 echomsg 'No commit needed.'
141 " Function: s:hgFunctions.Delete() {{{2
142 function! s:hgFunctions.Delete(argList)
143 return s:DoCommand(join(['remove'] + a:argList, ' '), 'remove', join(a:argList, ' '), {})
146 " Function: s:hgFunctions.Diff(argList) {{{2
147 function! s:hgFunctions.Diff(argList)
148 if len(a:argList) == 0
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') . ')'
156 let caption = join(a:argList, ' ')
157 let revOptions = a:argList
160 let hgDiffExt = VCSCommandGetOption('VCSCommandHGDiffExt', '')
164 let diffExt = ['--diff-cmd ' . hgDiffExt]
167 let hgDiffOpt = VCSCommandGetOption('VCSCommandHGDiffOpt', '')
171 let diffOptions = ['-x -' . hgDiffOpt]
174 return s:DoCommand(join(['diff'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
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, ' '), {})
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 . '"')
196 " File not under HG control.
197 if statusText =~ '^?'
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]
210 elseif statusText =~ '^A'
211 return ['New', 'New']
213 return [revision, repository]
217 " Function: s:hgFunctions.Log(argList) {{{2
218 function! s:hgFunctions.Log(argList)
219 if len(a:argList) == 0
222 elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
223 let options = ['-r' . join(a:argList, ':')]
224 let caption = options[0]
227 let options = a:argList
228 let caption = join(a:argList, ' ')
231 let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
235 " Function: s:hgFunctions.Revert(argList) {{{2
236 function! s:hgFunctions.Revert(argList)
237 return s:DoCommand('revert', 'revert', '', {})
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 = ''
246 let versiontag = a:argList[0]
247 let versionOption = ' -r ' . versiontag . ' '
250 return s:DoCommand('cat' . versionOption, 'review', versiontag, {})
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
259 return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
262 " Function: s:hgFunctions.Update(argList) {{{2
263 function! s:hgFunctions.Update(argList)
264 return s:DoCommand('update', 'update', '', {})
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