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 if !exists('g:loaded_VCSCommand')
54 runtime plugin/vcscommand.vim
57 if !executable(VCSCommandGetOption('VCSCommandHGExec', 'hg'))
65 " Section: Variable initialization {{{1
67 let s:hgFunctions = {}
69 " Section: Utility functions {{{1
71 " Function: s:Executable() {{{2
72 " Returns the executable used to invoke hg suitable for use in a shell
74 function! s:Executable()
75 return VCSCommandGetOption('VCSCommandHGExec', 'hg')
78 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
79 " Wrapper to VCSCommandDoCommand to add the name of the HG executable to the
81 function! s:DoCommand(cmd, cmdName, statusText, options)
82 if VCSCommandGetVCSType(expand('%')) == 'HG'
83 let fullCmd = s:Executable() . ' ' . a:cmd
84 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
86 throw 'HG VCSCommand plugin called on non-HG item.'
90 " Section: VCS function implementations {{{1
92 " Function: s:hgFunctions.Identify(buffer) {{{2
93 function! s:hgFunctions.Identify(buffer)
94 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
96 call s:VCSCommandUtility.system(s:Executable() . ' root')
100 return g:VCSCOMMAND_IDENTIFY_INEXACT
103 call VCSCommandChdir(oldCwd)
107 " Function: s:hgFunctions.Add() {{{2
108 function! s:hgFunctions.Add(argList)
109 return s:DoCommand(join(['add -v'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
112 " Function: s:hgFunctions.Annotate(argList) {{{2
113 function! s:hgFunctions.Annotate(argList)
114 if len(a:argList) == 0
115 if &filetype ==? 'hgannotate'
116 " Perform annotation of the version indicated by the current line.
117 let caption = matchstr(getline('.'),'\v^\s*\w+\s+\zs\d+')
118 let options = ' -un -r' . caption
123 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
124 let caption = a:argList[0]
125 let options = ' -un -r' . caption
127 let caption = join(a:argList, ' ')
128 let options = ' ' . caption
131 return s:DoCommand('blame' . options, 'annotate', caption, {})
134 " Function: s:hgFunctions.Commit(argList) {{{2
135 function! s:hgFunctions.Commit(argList)
137 return s:DoCommand('commit -v -l "' . a:argList[0] . '"', 'commit', '', {})
138 catch /Version control command failed.*nothing changed/
139 echomsg 'No commit needed.'
143 " Function: s:hgFunctions.Delete() {{{2
144 function! s:hgFunctions.Delete(argList)
145 return s:DoCommand(join(['remove'] + a:argList, ' '), 'remove', join(a:argList, ' '), {})
148 " Function: s:hgFunctions.Diff(argList) {{{2
149 function! s:hgFunctions.Diff(argList)
150 if len(a:argList) == 0
153 elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
154 let revOptions = ['-r' . join(a:argList, ':')]
155 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
158 let caption = join(a:argList, ' ')
159 let revOptions = a:argList
162 let hgDiffExt = VCSCommandGetOption('VCSCommandHGDiffExt', '')
166 let diffExt = ['--diff-cmd ' . hgDiffExt]
169 let hgDiffOpt = VCSCommandGetOption('VCSCommandHGDiffOpt', '')
173 let diffOptions = ['-x -' . hgDiffOpt]
176 return s:DoCommand(join(['diff'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
179 " Function: s:hgFunctions.Info(argList) {{{2
180 function! s:hgFunctions.Info(argList)
181 return s:DoCommand(join(['log --limit 1'] + a:argList, ' '), 'log', join(a:argList, ' '), {})
184 " Function: s:hgFunctions.GetBufferInfo() {{{2
185 " Provides version control details for the current file. Current version
186 " number and current repository version number are required to be returned by
187 " the vcscommand plugin.
188 " Returns: List of results: [revision, repository, branch]
190 function! s:hgFunctions.GetBufferInfo()
191 let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
192 let fileName = bufname(originalBuffer)
193 let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -- "' . fileName . '"')
198 " File not under HG control.
199 if statusText =~ '^?'
203 let parentsText = s:VCSCommandUtility.system(s:Executable() . ' parents -- "' . fileName . '"')
204 let revision = matchlist(parentsText, '^changeset:\s\+\(\S\+\)\n')[1]
206 let logText = s:VCSCommandUtility.system(s:Executable() . ' log -- "' . fileName . '"')
207 let repository = matchlist(logText, '^changeset:\s\+\(\S\+\)\n')[1]
212 elseif statusText =~ '^A'
213 return ['New', 'New']
215 return [revision, repository]
219 " Function: s:hgFunctions.Log(argList) {{{2
220 function! s:hgFunctions.Log(argList)
221 if len(a:argList) == 0
224 elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
225 let options = ['-r' . join(a:argList, ':')]
226 let caption = options[0]
229 let options = a:argList
230 let caption = join(a:argList, ' ')
233 let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
237 " Function: s:hgFunctions.Revert(argList) {{{2
238 function! s:hgFunctions.Revert(argList)
239 return s:DoCommand('revert', 'revert', '', {})
242 " Function: s:hgFunctions.Review(argList) {{{2
243 function! s:hgFunctions.Review(argList)
244 if len(a:argList) == 0
245 let versiontag = '(current)'
246 let versionOption = ''
248 let versiontag = a:argList[0]
249 let versionOption = ' -r ' . versiontag . ' '
252 return s:DoCommand('cat' . versionOption, 'review', versiontag, {})
255 " Function: s:hgFunctions.Status(argList) {{{2
256 function! s:hgFunctions.Status(argList)
257 let options = ['-A', '-v']
258 if len(a:argList) != 0
259 let options = a:argList
261 return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
264 " Function: s:hgFunctions.Update(argList) {{{2
265 function! s:hgFunctions.Update(argList)
266 return s:DoCommand('update', 'update', '', {})
269 " Annotate setting {{{2
270 let s:hgFunctions.AnnotateSplitRegex = '\d\+: '
272 " Section: Plugin Registration {{{1
273 let s:VCSCommandUtility = VCSCommandRegisterModule('HG', expand('<sfile>'), s:hgFunctions, [])
275 let &cpo = s:save_cpo