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