r8@localhost: bob | 2007-02-08 11:02:13 -0600
[vcscommand.git] / plugin / vcscvs.vim
blob51f42d2206308cedc237cad1dd4d17e22d534a23
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>ci 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 " Section: Variable initialization {{{1
88 let s:cvsFunctions = {}
90 " Section: Utility functions {{{1
92 " Function: s:DoCommand(cmd, cmdName, statusText) {{{2
93 " Wrapper to VCSCommandDoCommand to add the name of the CVS executable to the
94 " command argument.
95 function! s:DoCommand(cmd, cmdName, statusText)
96   try
97     if VCSCommandGetVCSType(expand('%')) == 'CVS'
98       let fullCmd = VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' ' . a:cmd
99       return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText)
100     else
101       throw 'No suitable plugin'
102     endif
103   catch /No suitable plugin/
104     echohl WarningMsg|echomsg 'Cannot apply CVS commands to this file.'|echohl None
105   endtry
106 endfunction
108 " Section: VCS function implementations {{{1
110 " Function: s:cvsFunctions.Identify(buffer) {{{2
111 function! s:cvsFunctions.Identify(buffer)
112   let fileName = resolve(bufname(a:buffer))
113   if isdirectory(fileName)
114     let directory = fileName
115   else
116     let directory = fnamemodify(fileName, ':h')
117   endif
118   if strlen(directory) > 0
119     let CVSRoot = directory . '/CVS/Root'
120   else
121     let CVSRoot = 'CVS/Root'
122   endif
123   if filereadable(CVSRoot)
124     return 1
125   else
126     return 0
127   endif
128 endfunction
130 " Function: s:cvsFunctions.Add(argList) {{{2
131 function! s:cvsFunctions.Add(argList)
132   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '))
133 endfunction
135 " Function: s:cvsFunctions.Annotate(argList) {{{2
136 function! s:cvsFunctions.Annotate(argList)
137   if len(a:argList) == 0
138     if &filetype == 'CVSAnnotate'
139       " This is a CVSAnnotate buffer.  Perform annotation of the version
140       " indicated by the current line.
141       let revision = matchstr(getline('.'),'\v%(^[0-9.]+)')
142     else
143       let revision=VCSCommandGetRevision()
144       if revision == ''
145         throw 'Unable to obtain version information.'
146       elseif revision == 'Unknown'
147         throw 'File not under source control'
148       elseif revision == 'New'
149         throw 'No annotatation available for new file.'
150       endif
151     endif
152   else
153     let revision=a:argList[0]
154   endif
156   if revision == 'New'
157     throw 'No annotatation available for new file.'
158   endif
160   let resultBuffer=s:DoCommand('-q annotate -r ' . revision, 'annotate', revision) 
161   if resultBuffer > 0
162     set filetype=CVSAnnotate
163     " Remove header lines from standard error
164     silent v/^\d\+\%(\.\d\+\)\+/d
165   endif
166   return resultBuffer
167 endfunction
169 " Function: s:cvsFunctions.Commit(argList) {{{2
170 function! s:cvsFunctions.Commit(argList)
171   let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '')
172   if resultBuffer == 0
173     echomsg 'No commit needed.'
174   endif
175   return resultBuffer
176 endfunction
178 " Function: s:cvsFunctions.Delete() {{{2
179 function! s:cvsFunctions.Delete(argList)
180   return s:DoCommand(join(['remove -f'] + a:argList, ' '), 'delete', join(a:argList, ' '))
181 endfunction
183 " Function: s:cvsFunctions.Diff(argList) {{{2
184 function! s:cvsFunctions.Diff(argList)
185   if len(a:argList) == 1
186     let revOptions = '-r ' . a:argList[0]
187     let caption = '(' . a:argList[0] . ' : current)'
188   elseif len(a:argList) == 2
189     let revOptions = '-r ' . a:argList[0] . ' -r ' . a:argList[1]
190     let caption = '(' . a:argList[0] . ' : ' . a:argList[1] . ')'
191   else
192     let revOptions = ''
193     let caption = ''
194   endif
196   let cvsdiffopt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
198   if cvsdiffopt == ''
199     let diffoptionstring = ''
200   else
201     let diffoptionstring = ' -' . cvsdiffopt . ' '
202   endif
204   let resultBuffer = s:DoCommand('diff ' . diffoptionstring . revOptions , 'diff', caption)
205   if resultBuffer > 0
206     set filetype=diff
207   else
208     echomsg 'No differences found'
209   endif
210   return resultBuffer
211 endfunction
213 " Function: s:cvsFunctions.GetBufferInfo() {{{2
214 " Provides version control details for the current file.  Current version
215 " number and current repository version number are required to be returned by
216 " the vcscommand plugin.  This CVS extension adds branch name to the return
217 " list as well.
218 " Returns: List of results:  [revision, repository, branch]
220 function! s:cvsFunctions.GetBufferInfo()
221   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
222   let fileName = bufname(originalBuffer)
223   let realFileName = fnamemodify(resolve(fileName), ':t')
224   if !filereadable(fileName)
225     return ['Unknown']
226   endif
227   let oldCwd=VCSCommandChangeToCurrentFileDir(fileName)
228   try
229     let statusText=system(VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' status "' . realFileName . '"')
230     if(v:shell_error)
231       return []
232     endif
233     let revision=substitute(statusText, '^\_.*Working revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\)\_.*$', '\1', '')
235     " We can still be in a CVS-controlled directory without this being a CVS
236     " file
237     if match(revision, '^New file!$') >= 0 
238       let revision='New'
239     elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
240       return ['Unknown']
241     endif
243     let branch=substitute(statusText, '^\_.*Sticky Tag:\s\+\(\d\+\%(\.\d\+\)\+\|\a[A-Za-z0-9-_]*\|(none)\).*$', '\1', '')
244     let repository=substitute(statusText, '^\_.*Repository revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\|No revision control file\)\_.*$', '\1', '')
245     let repository=substitute(repository, '^New file!\|No revision control file$', 'New', '')
246     return [revision, repository, branch]
247   finally
248     execute 'cd' escape(oldCwd, ' ')
249   endtry
250 endfunction
252 " Function: s:cvsFunctions.Log() {{{2
253 function! s:cvsFunctions.Log(argList)
254   if len(a:argList) == 0
255     let versionOption = ''
256     let caption = ''
257   elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
258     let versionOption =' -r' . a:argList[0]
259     let caption = a:argList[0]
260   else
261     " Multiple options, or the option starts with '-'
262     let caption = join(a:argList, ' ')
263     let versionOption = ' ' . caption
264   endif
266   let resultBuffer=s:DoCommand('log' . versionOption, 'log', caption)
267   if resultBuffer > 0
268     set filetype=rcslog
269   endif
270   return resultBuffer
271 endfunction
273 " Function: s:cvsFunctions.Revert(argList) {{{2
274 function! s:cvsFunctions.Revert(argList)
275   return s:DoCommand('update -C', 'revert', '')
276 endfunction
278 " Function: s:cvsFunctions.Review(argList) {{{2
279 function! s:cvsFunctions.Review(argList)
280   if len(a:argList) == 0
281     let versiontag = '(current)'
282     let versionOption = ''
283   else
284     let versiontag = a:argList[0]
285     let versionOption = ' -r ' . versiontag . ' '
286   endif
288   let resultBuffer = s:DoCommand('-q update -p' . versionOption, 'review', versiontag)
289   if resultBuffer > 0
290     let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
291   endif
292   return resultBuffer
293 endfunction
295 " Function: s:cvsFunctions.Status(argList) {{{2
296 function! s:cvsFunctions.Status(argList)
297   return s:DoCommand(join(['status'] + a:argList, ' '), 'status', join(a:argList, ' '))
298 endfunction
300 " Function: s:cvsFunctions.Update(argList) {{{2
301 function! s:cvsFunctions.Update(argList)
302   return s:DoCommand('update', 'update', '')
303 endfunction
305 " Section: CVS-specific functions {{{1
307 " Function: s:CVSEdit() {{{2
308 function! s:CVSEdit()
309   return s:DoCommand('edit', 'cvsedit', '')
310 endfunction
312 " Function: s:CVSEditors() {{{2
313 function! s:CVSEditors()
314   return s:DoCommand('editors', 'cvseditors', '')
315 endfunction
317 " Function: s:CVSUnedit() {{{2
318 function! s:CVSUnedit()
319   return s:DoCommand('unedit', 'cvsunedit', '')
320 endfunction
322 " Function: s:CVSWatch(onoff) {{{2
323 function! s:CVSWatch(onoff)
324   if a:onoff !~ '^\c\%(on\|off\|add\|remove\)$'
325     echoerr 'Argument to CVSWatch must be one of [on|off|add|remove]'
326     return -1
327   end
328   return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '')
329 endfunction
331 " Function: s:CVSWatchers() {{{2
332 function! s:CVSWatchers()
333   return s:DoCommand('watchers', 'cvswatchers', '')
334 endfunction
336 " Section: Command definitions {{{1
337 " Section: Primary commands {{{2
338 com! CVSEdit call s:CVSEdit()
339 com! CVSEditors call s:CVSEditors()
340 com! CVSUnedit call s:CVSUnedit()
341 com! -nargs=1 CVSWatch call s:CVSWatch(<f-args>)
342 com! CVSWatchAdd call s:CVSWatch('add')
343 com! CVSWatchOn call s:CVSWatch('on')
344 com! CVSWatchOff call s:CVSWatch('off')
345 com! CVSWatchRemove call s:CVSWatch('remove')
346 com! CVSWatchers call s:CVSWatchers()
348 " Section: Plugin command mappings {{{1
350 let s:cvsExtensionMappings = {}
351 let mappingInfo = [
352       \['CVSEdit', 'CVSEdit', 'ce'],
353       \['CVSEditors', 'CVSEditors', 'ci'],
354       \['CVSUnedit', 'CVSUnedit', 'ct'],
355       \['CVSWatchers', 'CVSWatchers', 'cwv'],
356       \['CVSWatchAdd', 'CVSWatch add', 'cwa'],
357       \['CVSWatchOff', 'CVSWatch off', 'cwf'],
358       \['CVSWatchOn', 'CVSWatch on', 'cwn'],
359       \['CVSWatchRemove', 'CVSWatch remove', 'cwr']
360       \]
362 for [pluginName, commandText, shortCut] in mappingInfo
363   execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
364   if !hasmapto('<Plug>' . pluginName)
365     let s:cvsExtensionMappings[shortCut] = commandText
366   endif
367 endfor
369 " Section: Menu items {{{1
370 silent! aunmenu Plugin.VCS.CVS
371 amenu <silent> &Plugin.VCS.CVS.&Edit       <Plug>CVSEdit
372 amenu <silent> &Plugin.VCS.CVS.Ed&itors    <Plug>CVSEditors
373 amenu <silent> &Plugin.VCS.CVS.Unedi&t     <Plug>CVSUnedit
374 amenu <silent> &Plugin.VCS.CVS.&Watchers   <Plug>CVSWatchers
375 amenu <silent> &Plugin.VCS.CVS.WatchAdd    <Plug>CVSWatchAdd
376 amenu <silent> &Plugin.VCS.CVS.WatchOn     <Plug>CVSWatchOn
377 amenu <silent> &Plugin.VCS.CVS.WatchOff    <Plug>CVSWatchOff
378 amenu <silent> &Plugin.VCS.CVS.WatchRemove <Plug>CVSWatchRemove
380 " Section: Plugin Registration {{{1
381 " If the vcscommand.vim plugin hasn't loaded, delay registration until it
382 " loads.
383 if exists('g:loaded_VCSCommand')
384   call VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
385 else
386   augroup VCSCommand
387     au User VCSLoadExtensions call VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
388   augroup END
389 endif