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