Removed trailing white space in source.
[vcscommand.git] / plugin / vcscvs.vim
blob6e6cc6a917e210dd6be8d5499d4854d124086bf8
1 " vim600: set foldmethod=marker:
3 " CVS extension for VCSCommand.
5 " Version:       VCS development
6 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
7 " License:
8 " Copyright (c) 2007 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 " Command documentation {{{2
32 " The following commands only apply to files under CVS source control.
34 " CVSEdit          Performs "cvs edit" on the current file.
36 " CVSEditors       Performs "cvs editors" on the current file.
38 " CVSUnedit        Performs "cvs unedit" on the current file.
40 " CVSWatch         Takes an argument which must be one of [on|off|add|remove].
41 "                  Performs "cvs watch" with the given argument on the current
42 "                  file.
44 " CVSWatchers      Performs "cvs watchers" on the current file.
46 " CVSWatchAdd      Alias for "CVSWatch add"
48 " CVSWatchOn       Alias for "CVSWatch on"
50 " CVSWatchOff      Alias for "CVSWatch off"
52 " CVSWatchRemove   Alias for "CVSWatch remove"
54 " Mapping documentation: {{{2
56 " By default, a mapping is defined for each command.  User-provided mappings
57 " can be used instead by mapping to <Plug>CommandName, for instance:
59 " nnoremap ,ce <Plug>CVSEdit
61 " The default mappings are as follow:
63 "   <Leader>ce CVSEdit
64 "   <Leader>cE CVSEditors
65 "   <Leader>ct CVSUnedit
66 "   <Leader>cwv CVSWatchers
67 "   <Leader>cwa CVSWatchAdd
68 "   <Leader>cwn CVSWatchOn
69 "   <Leader>cwf CVSWatchOff
70 "   <Leader>cwr CVSWatchRemove
72 " Options documentation: {{{2
74 " VCSCommandCVSExec
75 "   This variable specifies the CVS executable.  If not set, it defaults to
76 "   'cvs' executed from the user's executable path.
78 " VCSCommandCVSDiffOpt
79 "   This variable, if set, determines the options passed to the cvs diff
80 "   command.  If not set, it defaults to 'u'.
82 " Section: Plugin header {{{1
84 if exists('VCSCommandDisableAll')
85         finish
86 endif
88 if v:version < 700
89         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
90         finish
91 endif
93 runtime plugin/vcscommand.vim
95 if !executable(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
96         " CVS is not installed
97         finish
98 endif
100 let s:save_cpo=&cpo
101 set cpo&vim
103 " Section: Variable initialization {{{1
105 let s:cvsFunctions = {}
107 " Section: Utility functions {{{1
109 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
110 " Wrapper to VCSCommandDoCommand to add the name of the CVS executable to the
111 " command argument.
112 function! s:DoCommand(cmd, cmdName, statusText, options)
113         if VCSCommandGetVCSType(expand('%')) == 'CVS'
114                 let fullCmd = VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' ' . a:cmd
115                 let ret = VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
117                 if ret > 0
118                         if getline(line('$')) =~ '^cvs \w\+: closing down connection'
119                                 $d
120                                 1
121                         endif
123                 endif
125                 return ret
126         else
127                 throw 'CVS VCSCommand plugin called on non-CVS item.'
128         endif
129 endfunction
131 " Function: GetRevision() {{{2
132 " Function for retrieving the current buffer's revision number.
133 " Returns: Revision number or an empty string if an error occurs.
135 function! GetRevision()
136         if !exists('b:VCSCommandBufferInfo')
137                 let b:VCSCommandBufferInfo =  s:cvsFunctions.GetBufferInfo()
138         endif
140         if len(b:VCSCommandBufferInfo) > 0
141                 return b:VCSCommandBufferInfo[0]
142         else
143                 return ''
144         endif
145 endfunction
147 " Section: VCS function implementations {{{1
149 " Function: s:cvsFunctions.Identify(buffer) {{{2
150 function! s:cvsFunctions.Identify(buffer)
151         let fileName = resolve(bufname(a:buffer))
152         if isdirectory(fileName)
153                 let directoryName = fileName
154         else
155                 let directoryName = fnamemodify(fileName, ':h')
156         endif
157         if strlen(directoryName) > 0
158                 let CVSRoot = directoryName . '/CVS/Root'
159         else
160                 let CVSRoot = 'CVS/Root'
161         endif
162         if filereadable(CVSRoot)
163                 return 1
164         else
165                 return 0
166         endif
167 endfunction
169 " Function: s:cvsFunctions.Add(argList) {{{2
170 function! s:cvsFunctions.Add(argList)
171         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
172 endfunction
174 " Function: s:cvsFunctions.Annotate(argList) {{{2
175 function! s:cvsFunctions.Annotate(argList)
176         if len(a:argList) == 0
177                 if &filetype == 'CVSAnnotate'
178                         " This is a CVSAnnotate buffer.  Perform annotation of the version
179                         " indicated by the current line.
180                         let caption = matchstr(getline('.'),'\v^[0-9.]+')
182                         if VCSCommandGetOption('VCSCommandCVSAnnotateParent', 0) != 0
183                                 if caption != '1.1'
184                                         let revmaj = matchstr(caption,'\v[0-9.]+\ze\.[0-9]+')
185                                         let revmin = matchstr(caption,'\v[0-9.]+\.\zs[0-9]+') - 1
186                                         if revmin == 0
187                                                 " Jump to ancestor branch
188                                                 let caption = matchstr(revmaj,'\v[0-9.]+\ze\.[0-9]+')
189                                         else
190                                                 let caption = revmaj . "." .  revmin
191                                         endif
192                                 endif
193                         endif
195                         let options = ['-r' . caption]
196                 else
197                         " CVS defaults to pulling HEAD, regardless of current branch.
198                         " Therefore, always pass desired revision.
199                         let caption = ''
200                         let options = ['-r' .  GetRevision()]
201                 endif
202         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
203                 let caption = a:argList[0]
204                 let options = ['-r' . caption]
205         else
206                 let caption = join(a:argList)
207                 let options = a:argList
208         endif
210         let resultBuffer = s:DoCommand(join(['-q', 'annotate'] + options), 'annotate', caption, {})
211         if resultBuffer > 0
212                 set filetype=CVSAnnotate
213                 " Remove header lines from standard error
214                 silent v/^\d\+\%(\.\d\+\)\+/d
215         endif
216         return resultBuffer
217 endfunction
219 " Function: s:cvsFunctions.Commit(argList) {{{2
220 function! s:cvsFunctions.Commit(argList)
221         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
222         if resultBuffer == 0
223                 echomsg 'No commit needed.'
224         endif
225         return resultBuffer
226 endfunction
228 " Function: s:cvsFunctions.Delete() {{{2
229 " By default, use the -f option to remove the file first.  If options are
230 " passed in, use those instead.
231 function! s:cvsFunctions.Delete(argList)
232         let options = ['-f']
233         let caption = ''
234         if len(a:argList) > 0
235                 let options = a:argList
236                 let caption = join(a:argList, ' ')
237         endif
238         return s:DoCommand(join(['remove'] + options, ' '), 'delete', caption, {})
239 endfunction
241 " Function: s:cvsFunctions.Diff(argList) {{{2
242 function! s:cvsFunctions.Diff(argList)
243         if len(a:argList) == 0
244                 let revOptions = []
245                 let caption = ''
246         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
247                 let revOptions = ['-r' . join(a:argList, ' -r')]
248                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
249         else
250                 " Pass-through
251                 let caption = join(a:argList, ' ')
252                 let revOptions = a:argList
253         endif
255         let cvsDiffOpt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
256         if cvsDiffOpt == ''
257                 let diffOptions = []
258         else
259                 let diffOptions = ['-' . cvsDiffOpt]
260         endif
262         let resultBuffer = s:DoCommand(join(['diff'] + diffOptions + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
263         if resultBuffer > 0
264                 set filetype=diff
265         else
266                 echomsg 'No differences found'
267         endif
268         return resultBuffer
269 endfunction
271 " Function: s:cvsFunctions.GetBufferInfo() {{{2
272 " Provides version control details for the current file.  Current version
273 " number and current repository version number are required to be returned by
274 " the vcscommand plugin.  This CVS extension adds branch name to the return
275 " list as well.
276 " Returns: List of results:  [revision, repository, branch]
278 function! s:cvsFunctions.GetBufferInfo()
279         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
280         let fileName = bufname(originalBuffer)
281         if isdirectory(fileName)
282                 let tag = ''
283                 if filereadable(fileName . '/CVS/Tag')
284                         let tagFile = readfile(fileName . '/CVS/Tag')
285                         if len(tagFile) == 1
286                                 let tag = substitute(tagFile[0], '^T', '', '')
287                         endif
288                 endif
289                 return [tag]
290         endif
291         let realFileName = fnamemodify(resolve(fileName), ':t')
292         if !filereadable(fileName)
293                 return ['Unknown']
294         endif
295         let oldCwd = VCSCommandChangeToCurrentFileDir(fileName)
296         try
297                 let statusText=system(VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' status "' . realFileName . '"')
298                 if(v:shell_error)
299                         return []
300                 endif
301                 let revision=substitute(statusText, '^\_.*Working revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\)\_.*$', '\1', '')
303                 " We can still be in a CVS-controlled directory without this being a CVS
304                 " file
305                 if match(revision, '^New file!$') >= 0
306                         let revision='New'
307                 elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
308                         return ['Unknown']
309                 endif
311                 let branch=substitute(statusText, '^\_.*Sticky Tag:\s\+\(\d\+\%(\.\d\+\)\+\|\a[A-Za-z0-9-_]*\|(none)\).*$', '\1', '')
312                 let repository=substitute(statusText, '^\_.*Repository revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\|No revision control file\)\_.*$', '\1', '')
313                 let repository=substitute(repository, '^New file!\|No revision control file$', 'New', '')
314                 return [revision, repository, branch]
315         finally
316                 call VCSCommandChdir(oldCwd)
317         endtry
318 endfunction
320 " Function: s:cvsFunctions.Log() {{{2
321 function! s:cvsFunctions.Log(argList)
322         if len(a:argList) == 0
323                 let options = []
324                 let caption = ''
325         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
326                 let options = ['-r' . join(a:argList, ':')]
327                 let caption = options[0]
328         else
329                 " Pass-through
330                 let options = a:argList
331                 let caption = join(a:argList, ' ')
332         endif
334         let resultBuffer=s:DoCommand(join(['log'] + options), 'log', caption, {})
335         if resultBuffer > 0
336                 set filetype=rcslog
337         endif
338         return resultBuffer
339 endfunction
341 " Function: s:cvsFunctions.Revert(argList) {{{2
342 function! s:cvsFunctions.Revert(argList)
343         return s:DoCommand('update -C', 'revert', '', {})
344 endfunction
346 " Function: s:cvsFunctions.Review(argList) {{{2
347 function! s:cvsFunctions.Review(argList)
348         if len(a:argList) == 0
349                 let versiontag = '(current)'
350                 let versionOption = ''
351         else
352                 let versiontag = a:argList[0]
353                 let versionOption = ' -r ' . versiontag . ' '
354         endif
356         let resultBuffer = s:DoCommand('-q update -p' . versionOption, 'review', versiontag, {})
357         if resultBuffer > 0
358                 let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
359         endif
360         return resultBuffer
361 endfunction
363 " Function: s:cvsFunctions.Status(argList) {{{2
364 function! s:cvsFunctions.Status(argList)
365         return s:DoCommand(join(['status'] + a:argList, ' '), 'status', join(a:argList, ' '), {})
366 endfunction
368 " Function: s:cvsFunctions.Update(argList) {{{2
369 function! s:cvsFunctions.Update(argList)
370         return s:DoCommand('update', 'update', '', {})
371 endfunction
373 " Section: CVS-specific functions {{{1
375 " Function: s:CVSEdit() {{{2
376 function! s:CVSEdit()
377         return s:DoCommand('edit', 'cvsedit', '', {})
378 endfunction
380 " Function: s:CVSEditors() {{{2
381 function! s:CVSEditors()
382         return s:DoCommand('editors', 'cvseditors', '', {})
383 endfunction
385 " Function: s:CVSUnedit() {{{2
386 function! s:CVSUnedit()
387         return s:DoCommand('unedit', 'cvsunedit', '', {})
388 endfunction
390 " Function: s:CVSWatch(onoff) {{{2
391 function! s:CVSWatch(onoff)
392         if a:onoff !~ '^\c\%(on\|off\|add\|remove\)$'
393                 echoerr 'Argument to CVSWatch must be one of [on|off|add|remove]'
394                 return -1
395         end
396         return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '', {})
397 endfunction
399 " Function: s:CVSWatchers() {{{2
400 function! s:CVSWatchers()
401         return s:DoCommand('watchers', 'cvswatchers', '', {})
402 endfunction
404 " Annotate setting {{{2
405 let s:cvsFunctions.AnnotateSplitRegex = '): '
407 " Section: Command definitions {{{1
408 " Section: Primary commands {{{2
409 com! CVSEdit call s:CVSEdit()
410 com! CVSEditors call s:CVSEditors()
411 com! CVSUnedit call s:CVSUnedit()
412 com! -nargs=1 CVSWatch call s:CVSWatch(<f-args>)
413 com! CVSWatchAdd call s:CVSWatch('add')
414 com! CVSWatchOn call s:CVSWatch('on')
415 com! CVSWatchOff call s:CVSWatch('off')
416 com! CVSWatchRemove call s:CVSWatch('remove')
417 com! CVSWatchers call s:CVSWatchers()
419 " Section: Plugin command mappings {{{1
421 let s:cvsExtensionMappings = {}
422 let mappingInfo = [
423                         \['CVSEdit', 'CVSEdit', 'e'],
424                         \['CVSEditors', 'CVSEditors', 'E'],
425                         \['CVSUnedit', 'CVSUnedit', 't'],
426                         \['CVSWatchers', 'CVSWatchers', 'wv'],
427                         \['CVSWatchAdd', 'CVSWatch add', 'wa'],
428                         \['CVSWatchOff', 'CVSWatch off', 'wf'],
429                         \['CVSWatchOn', 'CVSWatch on', 'wn'],
430                         \['CVSWatchRemove', 'CVSWatch remove', 'wr']
431                         \]
433 for [pluginName, commandText, shortCut] in mappingInfo
434         execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
435         if !hasmapto('<Plug>' . pluginName)
436                 let s:cvsExtensionMappings[shortCut] = commandText
437         endif
438 endfor
440 " Section: Menu items {{{1
441 amenu <silent> &Plugin.VCS.CVS.&Edit       <Plug>CVSEdit
442 amenu <silent> &Plugin.VCS.CVS.Ed&itors    <Plug>CVSEditors
443 amenu <silent> &Plugin.VCS.CVS.Unedi&t     <Plug>CVSUnedit
444 amenu <silent> &Plugin.VCS.CVS.&Watchers   <Plug>CVSWatchers
445 amenu <silent> &Plugin.VCS.CVS.WatchAdd    <Plug>CVSWatchAdd
446 amenu <silent> &Plugin.VCS.CVS.WatchOn     <Plug>CVSWatchOn
447 amenu <silent> &Plugin.VCS.CVS.WatchOff    <Plug>CVSWatchOff
448 amenu <silent> &Plugin.VCS.CVS.WatchRemove <Plug>CVSWatchRemove
450 " Section: Plugin Registration {{{1
451 call VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
453 let &cpo = s:save_cpo