Don't clobber &cp when testing for a vcs command.
[vcscommand.git] / plugin / vcscvs.vim
blob260a44261fc576bae6b16514d0f1c97692a0cbe2
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.
35 "   
36 " CVSEditors       Performs "cvs editors" on the current file.
37 "   
38 " CVSUnedit        Performs "cvs unedit" on the current file.
39 "   
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.
43 "   
44 " CVSWatchers      Performs "cvs watchers" on the current file.
45 "   
46 " CVSWatchAdd      Alias for "CVSWatch add"
47 "   
48 " CVSWatchOn       Alias for "CVSWatch on"
49 "   
50 " CVSWatchOff      Alias for "CVSWatch off"
51 "   
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 v:version < 700
85   echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
86   finish
87 endif
89 runtime plugin/vcscommand.vim
91 if !executable(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
92   " CVS is not installed
93   finish
94 endif
96 let s:save_cpo=&cpo
97 set cpo&vim
99 " Section: Variable initialization {{{1
101 let s:cvsFunctions = {}
103 " Section: Utility functions {{{1
105 " Function: s:DoCommand(cmd, cmdName, statusText) {{{2
106 " Wrapper to VCSCommandDoCommand to add the name of the CVS executable to the
107 " command argument.
108 function! s:DoCommand(cmd, cmdName, statusText)
109   if VCSCommandGetVCSType(expand('%')) == 'CVS'
110     let fullCmd = VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' ' . a:cmd
111     return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText)
112   else
113     throw 'CVS VCSCommand plugin called on non-CVS item.'
114   endif
115 endfunction
117 " Function: GetRevision() {{{2
118 " Function for retrieving the current buffer's revision number.
119 " Returns: Revision number or an empty string if an error occurs.
121 function! GetRevision()
122   if !exists('b:VCSCommandBufferInfo')
123     let b:VCSCommandBufferInfo =  s:cvsFunctions.GetBufferInfo()
124   endif
126   if len(b:VCSCommandBufferInfo) > 0
127     return b:VCSCommandBufferInfo[0]
128   else
129     return ''
130   endif
131 endfunction
133 " Section: VCS function implementations {{{1
135 " Function: s:cvsFunctions.Identify(buffer) {{{2
136 function! s:cvsFunctions.Identify(buffer)
137   let fileName = resolve(bufname(a:buffer))
138   if isdirectory(fileName)
139     let directoryName = fileName
140   else
141     let directoryName = fnamemodify(fileName, ':h')
142   endif
143   if strlen(directoryName) > 0
144     let CVSRoot = directoryName . '/CVS/Root'
145   else
146     let CVSRoot = 'CVS/Root'
147   endif
148   if filereadable(CVSRoot)
149     return 1
150   else
151     return 0
152   endif
153 endfunction
155 " Function: s:cvsFunctions.Add(argList) {{{2
156 function! s:cvsFunctions.Add(argList)
157   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '))
158 endfunction
160 " Function: s:cvsFunctions.Annotate(argList) {{{2
161 function! s:cvsFunctions.Annotate(argList)
162   if len(a:argList) == 0
163     if &filetype == 'CVSAnnotate'
164       " This is a CVSAnnotate buffer.  Perform annotation of the version
165       " indicated by the current line.
166       let caption = matchstr(getline('.'),'\v^[0-9.]+')
168       if VCSCommandGetOption('VCSCommandCVSAnnotateParent', 0) != 0
169         if caption != '1.1'
170           let revmaj = matchstr(caption,'\v[0-9.]+\ze\.[0-9]+')
171           let revmin = matchstr(caption,'\v[0-9.]+\.\zs[0-9]+') - 1
172           if revmin == 0
173             " Jump to ancestor branch
174             let caption = matchstr(revmaj,'\v[0-9.]+\ze\.[0-9]+')
175           else
176             let caption = revmaj . "." .  revmin
177           endif
178         endif
179       endif
181       let options = ['-r' . caption]
182     else
183       " CVS defaults to pulling HEAD, regardless of current branch.
184       " Therefore, always pass desired revision.
185       let caption = ''
186       let options = ['-r' .  GetRevision()]
187     endif
188   elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
189     let caption = a:argList[0]
190     let options = ['-r' . caption]
191   else
192     let caption = join(a:argList)
193     let options = a:argList
194   endif
196   let resultBuffer = s:DoCommand(join(['-q', 'annotate'] + options), 'annotate', caption) 
197   if resultBuffer > 0
198     set filetype=CVSAnnotate
199     " Remove header lines from standard error
200     silent v/^\d\+\%(\.\d\+\)\+/d
201   endif
202   return resultBuffer
203 endfunction
205 " Function: s:cvsFunctions.Commit(argList) {{{2
206 function! s:cvsFunctions.Commit(argList)
207   let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '')
208   if resultBuffer == 0
209     echomsg 'No commit needed.'
210   endif
211   return resultBuffer
212 endfunction
214 " Function: s:cvsFunctions.Delete() {{{2
215 " By default, use the -f option to remove the file first.  If options are
216 " passed in, use those instead.
217 function! s:cvsFunctions.Delete(argList)
218   let options = ['-f']
219   let caption = ''
220   if len(a:argList) > 0
221     let options = a:argList
222     let caption = join(a:argList, ' ')
223   endif
224   return s:DoCommand(join(['remove'] + options, ' '), 'delete', caption)
225 endfunction
227 " Function: s:cvsFunctions.Diff(argList) {{{2
228 function! s:cvsFunctions.Diff(argList)
229   if len(a:argList) == 0
230     let revOptions = []
231     let caption = ''
232   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
233     let revOptions = ['-r' . join(a:argList, ' -r')]
234     let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
235   else
236     " Pass-through
237     let caption = join(a:argList, ' ')
238     let revOptions = a:argList
239   endif
241   let cvsDiffOpt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
242   if cvsDiffOpt == ''
243     let diffOptions = []
244   else
245     let diffOptions = ['-' . cvsDiffOpt]
246   endif
248   let resultBuffer = s:DoCommand(join(['diff'] + diffOptions + revOptions), 'diff', caption)
249   if resultBuffer > 0
250     set filetype=diff
251   else
252     echomsg 'No differences found'
253   endif
254   return resultBuffer
255 endfunction
257 " Function: s:cvsFunctions.GetBufferInfo() {{{2
258 " Provides version control details for the current file.  Current version
259 " number and current repository version number are required to be returned by
260 " the vcscommand plugin.  This CVS extension adds branch name to the return
261 " list as well.
262 " Returns: List of results:  [revision, repository, branch]
264 function! s:cvsFunctions.GetBufferInfo()
265   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
266   let fileName = bufname(originalBuffer)
267   if isdirectory(fileName)
268     let tag = ''
269     if filereadable(fileName . '/CVS/Tag')
270       let tagFile = readfile(fileName . '/CVS/Tag')
271       if len(tagFile) == 1
272         let tag = substitute(tagFile[0], '^T', '', '')
273       endif
274     endif
275     return [tag]
276   endif
277   let realFileName = fnamemodify(resolve(fileName), ':t')
278   if !filereadable(fileName)
279     return ['Unknown']
280   endif
281   let oldCwd = VCSCommandChangeToCurrentFileDir(fileName)
282   try
283     let statusText=system(VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' status "' . realFileName . '"')
284     if(v:shell_error)
285       return []
286     endif
287     let revision=substitute(statusText, '^\_.*Working revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\)\_.*$', '\1', '')
289     " We can still be in a CVS-controlled directory without this being a CVS
290     " file
291     if match(revision, '^New file!$') >= 0 
292       let revision='New'
293     elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
294       return ['Unknown']
295     endif
297     let branch=substitute(statusText, '^\_.*Sticky Tag:\s\+\(\d\+\%(\.\d\+\)\+\|\a[A-Za-z0-9-_]*\|(none)\).*$', '\1', '')
298     let repository=substitute(statusText, '^\_.*Repository revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\|No revision control file\)\_.*$', '\1', '')
299     let repository=substitute(repository, '^New file!\|No revision control file$', 'New', '')
300     return [revision, repository, branch]
301   finally
302     call VCSCommandChdir(oldCwd)
303   endtry
304 endfunction
306 " Function: s:cvsFunctions.Log() {{{2
307 function! s:cvsFunctions.Log(argList)
308   if len(a:argList) == 0
309     let options = []
310     let caption = ''
311   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
312     let options = ['-r' . join(a:argList, ':')]
313     let caption = options[0]
314   else
315     " Pass-through
316     let options = a:argList
317     let caption = join(a:argList, ' ')
318   endif
320   let resultBuffer=s:DoCommand(join(['log'] + options), 'log', caption)
321   if resultBuffer > 0
322     set filetype=rcslog
323   endif
324   return resultBuffer
325 endfunction
327 " Function: s:cvsFunctions.Revert(argList) {{{2
328 function! s:cvsFunctions.Revert(argList)
329   return s:DoCommand('update -C', 'revert', '')
330 endfunction
332 " Function: s:cvsFunctions.Review(argList) {{{2
333 function! s:cvsFunctions.Review(argList)
334   if len(a:argList) == 0
335     let versiontag = '(current)'
336     let versionOption = ''
337   else
338     let versiontag = a:argList[0]
339     let versionOption = ' -r ' . versiontag . ' '
340   endif
342   let resultBuffer = s:DoCommand('-q update -p' . versionOption, 'review', versiontag)
343   if resultBuffer > 0
344     let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
345   endif
346   return resultBuffer
347 endfunction
349 " Function: s:cvsFunctions.Status(argList) {{{2
350 function! s:cvsFunctions.Status(argList)
351   return s:DoCommand(join(['status'] + a:argList, ' '), 'status', join(a:argList, ' '))
352 endfunction
354 " Function: s:cvsFunctions.Update(argList) {{{2
355 function! s:cvsFunctions.Update(argList)
356   return s:DoCommand('update', 'update', '')
357 endfunction
359 " Section: CVS-specific functions {{{1
361 " Function: s:CVSEdit() {{{2
362 function! s:CVSEdit()
363   return s:DoCommand('edit', 'cvsedit', '')
364 endfunction
366 " Function: s:CVSEditors() {{{2
367 function! s:CVSEditors()
368   return s:DoCommand('editors', 'cvseditors', '')
369 endfunction
371 " Function: s:CVSUnedit() {{{2
372 function! s:CVSUnedit()
373   return s:DoCommand('unedit', 'cvsunedit', '')
374 endfunction
376 " Function: s:CVSWatch(onoff) {{{2
377 function! s:CVSWatch(onoff)
378   if a:onoff !~ '^\c\%(on\|off\|add\|remove\)$'
379     echoerr 'Argument to CVSWatch must be one of [on|off|add|remove]'
380     return -1
381   end
382   return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '')
383 endfunction
385 " Function: s:CVSWatchers() {{{2
386 function! s:CVSWatchers()
387   return s:DoCommand('watchers', 'cvswatchers', '')
388 endfunction
390 " Section: Command definitions {{{1
391 " Section: Primary commands {{{2
392 com! CVSEdit call s:CVSEdit()
393 com! CVSEditors call s:CVSEditors()
394 com! CVSUnedit call s:CVSUnedit()
395 com! -nargs=1 CVSWatch call s:CVSWatch(<f-args>)
396 com! CVSWatchAdd call s:CVSWatch('add')
397 com! CVSWatchOn call s:CVSWatch('on')
398 com! CVSWatchOff call s:CVSWatch('off')
399 com! CVSWatchRemove call s:CVSWatch('remove')
400 com! CVSWatchers call s:CVSWatchers()
402 " Section: Plugin command mappings {{{1
404 let s:cvsExtensionMappings = {}
405 let mappingInfo = [
406       \['CVSEdit', 'CVSEdit', 'ce'],
407       \['CVSEditors', 'CVSEditors', 'cE'],
408       \['CVSUnedit', 'CVSUnedit', 'ct'],
409       \['CVSWatchers', 'CVSWatchers', 'cwv'],
410       \['CVSWatchAdd', 'CVSWatch add', 'cwa'],
411       \['CVSWatchOff', 'CVSWatch off', 'cwf'],
412       \['CVSWatchOn', 'CVSWatch on', 'cwn'],
413       \['CVSWatchRemove', 'CVSWatch remove', 'cwr']
414       \]
416 for [pluginName, commandText, shortCut] in mappingInfo
417   execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
418   if !hasmapto('<Plug>' . pluginName)
419     let s:cvsExtensionMappings[shortCut] = commandText
420   endif
421 endfor
423 " Section: Menu items {{{1
424 silent! aunmenu Plugin.VCS.CVS
425 amenu <silent> &Plugin.VCS.CVS.&Edit       <Plug>CVSEdit
426 amenu <silent> &Plugin.VCS.CVS.Ed&itors    <Plug>CVSEditors
427 amenu <silent> &Plugin.VCS.CVS.Unedi&t     <Plug>CVSUnedit
428 amenu <silent> &Plugin.VCS.CVS.&Watchers   <Plug>CVSWatchers
429 amenu <silent> &Plugin.VCS.CVS.WatchAdd    <Plug>CVSWatchAdd
430 amenu <silent> &Plugin.VCS.CVS.WatchOn     <Plug>CVSWatchOn
431 amenu <silent> &Plugin.VCS.CVS.WatchOff    <Plug>CVSWatchOff
432 amenu <silent> &Plugin.VCS.CVS.WatchRemove <Plug>CVSWatchRemove
434 " Section: Plugin Registration {{{1
435 call VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
437 let &cpo = s:save_cpo