handle ignored files in GetBufferInfo
[vcscommand.git] / plugin / vcshg.vim
blob9dbbde8566976ee0971acb6ea45848c7444d9765
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 if !exists('g:loaded_VCSCommand')
54         runtime plugin/vcscommand.vim
55 endif
57 if !executable(VCSCommandGetOption('VCSCommandHGExec', 'hg'))
58         " HG is not installed
59         finish
60 endif
62 let s:save_cpo=&cpo
63 set cpo&vim
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
73 " command.
74 function! s:Executable()
75         return shellescape(VCSCommandGetOption('VCSCommandHGExec', 'hg'))
76 endfunction
78 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
79 " Wrapper to VCSCommandDoCommand to add the name of the HG executable to the
80 " command argument.
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)
85         else
86                 throw 'HG VCSCommand plugin called on non-HG item.'
87         endif
88 endfunction
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)))
95         try
96                 call s:VCSCommandUtility.system(s:Executable() . ' root')
97                 if(v:shell_error)
98                         return 0
99                 else
100                         return g:VCSCOMMAND_IDENTIFY_INEXACT
101                 endif
102         finally
103                 call VCSCommandChdir(oldCwd)
104         endtry
105 endfunction
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, ' '), {})
110 endfunction
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
119                 else
120                         let caption = ''
121                         let options = ' -un'
122                 endif
123         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
124                 let caption = a:argList[0]
125                 let options = ' -un -r' . caption
126         else
127                 let caption = join(a:argList, ' ')
128                 let options = ' ' . caption
129         endif
131         return s:DoCommand('blame' . options, 'annotate', caption, {})
132 endfunction
134 " Function: s:hgFunctions.Commit(argList) {{{2
135 function! s:hgFunctions.Commit(argList)
136         try
137                 return s:DoCommand('commit -v -l "' . a:argList[0] . '"', 'commit', '', {})
138         catch /Version control command failed.*nothing changed/
139                 echomsg 'No commit needed.'
140         endtry
141 endfunction
143 " Function: s:hgFunctions.Delete() {{{2
144 function! s:hgFunctions.Delete(argList)
145         return s:DoCommand(join(['remove'] + a:argList, ' '), 'remove', join(a:argList, ' '), {})
146 endfunction
148 " Function: s:hgFunctions.Diff(argList) {{{2
149 function! s:hgFunctions.Diff(argList)
150         if len(a:argList) == 0
151                 let revOptions = []
152                 let caption = ''
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') . ')'
156         else
157                 " Pass-through
158                 let caption = join(a:argList, ' ')
159                 let revOptions = a:argList
160         endif
162         let hgDiffExt = VCSCommandGetOption('VCSCommandHGDiffExt', '')
163         if hgDiffExt == ''
164                 let diffExt = []
165         else
166                 let diffExt = ['--diff-cmd ' . hgDiffExt]
167         endif
169         let hgDiffOpt = VCSCommandGetOption('VCSCommandHGDiffOpt', '')
170         if hgDiffOpt == ''
171                 let diffOptions = []
172         else
173                 let diffOptions = ['-x -' . hgDiffOpt]
174         endif
176         return s:DoCommand(join(['diff'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
177 endfunction
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, ' '), {})
182 endfunction
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 . '"')
194         if(v:shell_error)
195                 return []
196         endif
198         " File not under HG control.
199         if statusText =~ '^?'
200                 return ['Unknown']
201         endif
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]
209         if revision == ''
210                 " Error
211                 return ['Unknown']
212         elseif statusText =~ '^A'
213                 return ['New', 'New']
214         else
215                 return [revision, repository]
216         endif
217 endfunction
219 " Function: s:hgFunctions.Log(argList) {{{2
220 function! s:hgFunctions.Log(argList)
221         if len(a:argList) == 0
222                 let options = []
223                 let caption = ''
224         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
225                 let options = ['-r' . join(a:argList, ':')]
226                 let caption = options[0]
227         else
228                 " Pass-through
229                 let options = a:argList
230                 let caption = join(a:argList, ' ')
231         endif
233         let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
234         return resultBuffer
235 endfunction
237 " Function: s:hgFunctions.Revert(argList) {{{2
238 function! s:hgFunctions.Revert(argList)
239         return s:DoCommand('revert', 'revert', '', {})
240 endfunction
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 = ''
247         else
248                 let versiontag = a:argList[0]
249                 let versionOption = ' -r ' . versiontag . ' '
250         endif
252         return s:DoCommand('cat' . versionOption, 'review', versiontag, {})
253 endfunction
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
260         endif
261         return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
262 endfunction
264 " Function: s:hgFunctions.Update(argList) {{{2
265 function! s:hgFunctions.Update(argList)
266         return s:DoCommand('update', 'update', '', {})
267 endfunction
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