re-enable executable quoting removed in 1.99.43.
[vcscommand.git] / plugin / vcscvs.vim
blobfe6db21c4d8c32db0a5488e0aae5e8b91b9280e4
1 " vim600: set foldmethod=marker:
3 " CVS extension for VCSCommand.
5 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
6 " License:
7 " Copyright (c) Bob Hiestand
9 " Permission is hereby granted, free of charge, to any person obtaining a copy
10 " of this software and associated documentation files (the "Software"), to
11 " deal in the Software without restriction, including without limitation the
12 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13 " sell copies of the Software, and to permit persons to whom the Software is
14 " furnished to do so, subject to the following conditions:
16 " The above copyright notice and this permission notice shall be included in
17 " all copies or substantial portions of the Software.
19 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 " IN THE SOFTWARE.
27 " Section: Documentation {{{1
29 " Command documentation {{{2
31 " The following commands only apply to files under CVS source control.
33 " CVSEdit          Performs "cvs edit" on the current file.
35 " CVSEditors       Performs "cvs editors" on the current file.
37 " CVSUnedit        Performs "cvs unedit" on the current file.
39 " CVSWatch         Takes an argument which must be one of [on|off|add|remove].
40 "                  Performs "cvs watch" with the given argument on the current
41 "                  file.
43 " CVSWatchers      Performs "cvs watchers" on the current file.
45 " CVSWatchAdd      Alias for "CVSWatch add"
47 " CVSWatchOn       Alias for "CVSWatch on"
49 " CVSWatchOff      Alias for "CVSWatch off"
51 " CVSWatchRemove   Alias for "CVSWatch remove"
53 " Mapping documentation: {{{2
55 " By default, a mapping is defined for each command.  User-provided mappings
56 " can be used instead by mapping to <Plug>CommandName, for instance:
58 " nnoremap ,ce <Plug>CVSEdit
60 " The default mappings are as follow:
62 "   <Leader>ce CVSEdit
63 "   <Leader>cE CVSEditors
64 "   <Leader>ct CVSUnedit
65 "   <Leader>cwv CVSWatchers
66 "   <Leader>cwa CVSWatchAdd
67 "   <Leader>cwn CVSWatchOn
68 "   <Leader>cwf CVSWatchOff
69 "   <Leader>cwr CVSWatchRemove
71 " Options documentation: {{{2
73 " VCSCommandCVSExec
74 "   This variable specifies the CVS executable.  If not set, it defaults to
75 "   'cvs' executed from the user's executable path.
77 " VCSCommandCVSDiffOpt
78 "   This variable, if set, determines the options passed to the cvs diff
79 "   command.  If not set, it defaults to 'u'.
81 " Section: Plugin header {{{1
83 if exists('VCSCommandDisableAll')
84         finish
85 endif
87 if v:version < 700
88         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
89         finish
90 endif
92 if !exists('g:loaded_VCSCommand')
93         runtime plugin/vcscommand.vim
94 endif
96 if !executable(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
97         " CVS is not installed
98         finish
99 endif
101 let s:save_cpo=&cpo
102 set cpo&vim
104 " Section: Variable initialization {{{1
106 let s:cvsFunctions = {}
108 " Section: Utility functions {{{1
110 " Function: s:Executable() {{{2
111 " Returns the executable used to invoke cvs suitable for use in a shell
112 " command.
113 function! s:Executable()
114         return shellescape(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
115 endfunction
117 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
118 " Wrapper to VCSCommandDoCommand to add the name of the CVS executable to the
119 " command argument.
120 function! s:DoCommand(cmd, cmdName, statusText, options)
121         if VCSCommandGetVCSType(expand('%')) == 'CVS'
122                 let fullCmd = s:Executable() . ' ' . a:cmd
123                 let ret = VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
125                 if ret > 0
126                         if getline(line('$')) =~ '^cvs \w\+: closing down connection'
127                                 $d
128                                 1
129                         endif
131                 endif
133                 return ret
134         else
135                 throw 'CVS VCSCommand plugin called on non-CVS item.'
136         endif
137 endfunction
139 " Function: s:GetRevision() {{{2
140 " Function for retrieving the current buffer's revision number.
141 " Returns: Revision number or an empty string if an error occurs.
143 function! s:GetRevision()
144         if !exists('b:VCSCommandBufferInfo')
145                 let b:VCSCommandBufferInfo =  s:cvsFunctions.GetBufferInfo()
146         endif
148         if len(b:VCSCommandBufferInfo) > 0
149                 return b:VCSCommandBufferInfo[0]
150         else
151                 return ''
152         endif
153 endfunction
155 " Section: VCS function implementations {{{1
157 " Function: s:cvsFunctions.Identify(buffer) {{{2
158 function! s:cvsFunctions.Identify(buffer)
159         let fileName = resolve(bufname(a:buffer))
160         if isdirectory(fileName)
161                 let directoryName = fileName
162         else
163                 let directoryName = fnamemodify(fileName, ':h')
164         endif
165         if strlen(directoryName) > 0
166                 let CVSRoot = directoryName . '/CVS/Root'
167         else
168                 let CVSRoot = 'CVS/Root'
169         endif
170         if filereadable(CVSRoot)
171                 return 1
172         else
173                 return 0
174         endif
175 endfunction
177 " Function: s:cvsFunctions.Add(argList) {{{2
178 function! s:cvsFunctions.Add(argList)
179         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
180 endfunction
182 " Function: s:cvsFunctions.Annotate(argList) {{{2
183 function! s:cvsFunctions.Annotate(argList)
184         if len(a:argList) == 0
185                 if &filetype ==? 'cvsannotate'
186                         " This is a CVSAnnotate buffer.  Perform annotation of the version
187                         " indicated by the current line.
188                         let caption = matchstr(getline('.'),'\v^[0-9.]+')
190                         if VCSCommandGetOption('VCSCommandCVSAnnotateParent', 0) != 0
191                                 if caption != '1.1'
192                                         let revmaj = matchstr(caption,'\v[0-9.]+\ze\.[0-9]+')
193                                         let revmin = matchstr(caption,'\v[0-9.]+\.\zs[0-9]+') - 1
194                                         if revmin == 0
195                                                 " Jump to ancestor branch
196                                                 let caption = matchstr(revmaj,'\v[0-9.]+\ze\.[0-9]+')
197                                         else
198                                                 let caption = revmaj . "." .  revmin
199                                         endif
200                                 endif
201                         endif
203                         let options = ['-r' . caption]
204                 else
205                         " CVS defaults to pulling HEAD, regardless of current branch.
206                         " Therefore, always pass desired revision.
207                         let caption = ''
208                         let options = ['-r' .  s:GetRevision()]
209                 endif
210         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
211                 let caption = a:argList[0]
212                 let options = ['-r' . caption]
213         else
214                 let caption = join(a:argList)
215                 let options = a:argList
216         endif
218         let resultBuffer = s:DoCommand(join(['-q', 'annotate'] + options), 'annotate', caption, {})
219         if resultBuffer > 0
220                 " Remove header lines from standard error
221                 silent v/^\d\+\%(\.\d\+\)\+/d
222         endif
223         return resultBuffer
224 endfunction
226 " Function: s:cvsFunctions.Commit(argList) {{{2
227 function! s:cvsFunctions.Commit(argList)
228         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
229         if resultBuffer == 0
230                 echomsg 'No commit needed.'
231         endif
232         return resultBuffer
233 endfunction
235 " Function: s:cvsFunctions.Delete() {{{2
236 " By default, use the -f option to remove the file first.  If options are
237 " passed in, use those instead.
238 function! s:cvsFunctions.Delete(argList)
239         let options = ['-f']
240         let caption = ''
241         if len(a:argList) > 0
242                 let options = a:argList
243                 let caption = join(a:argList, ' ')
244         endif
245         return s:DoCommand(join(['remove'] + options, ' '), 'delete', caption, {})
246 endfunction
248 " Function: s:cvsFunctions.Diff(argList) {{{2
249 function! s:cvsFunctions.Diff(argList)
250         if len(a:argList) == 0
251                 let revOptions = []
252                 let caption = ''
253         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
254                 let revOptions = ['-r' . join(a:argList, ' -r')]
255                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
256         else
257                 " Pass-through
258                 let caption = join(a:argList, ' ')
259                 let revOptions = a:argList
260         endif
262         let cvsDiffOpt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
263         if cvsDiffOpt == ''
264                 let diffOptions = []
265         else
266                 let diffOptions = ['-' . cvsDiffOpt]
267         endif
269         return s:DoCommand(join(['diff'] + diffOptions + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
270 endfunction
272 " Function: s:cvsFunctions.GetBufferInfo() {{{2
273 " Provides version control details for the current file.  Current version
274 " number and current repository version number are required to be returned by
275 " the vcscommand plugin.  This CVS extension adds branch name to the return
276 " list as well.
277 " Returns: List of results:  [revision, repository, branch]
279 function! s:cvsFunctions.GetBufferInfo()
280         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
281         let fileName = bufname(originalBuffer)
282         if isdirectory(fileName)
283                 let tag = ''
284                 if filereadable(fileName . '/CVS/Tag')
285                         let tagFile = readfile(fileName . '/CVS/Tag')
286                         if len(tagFile) == 1
287                                 let tag = substitute(tagFile[0], '^T', '', '')
288                         endif
289                 endif
290                 return [tag]
291         endif
292         let realFileName = fnamemodify(resolve(fileName), ':t')
293         if !filereadable(fileName)
294                 return ['Unknown']
295         endif
296         let oldCwd = VCSCommandChangeToCurrentFileDir(fileName)
297         try
298                 let statusText=s:VCSCommandUtility.system(s:Executable() . ' status -- "' . realFileName . '"')
299                 if(v:shell_error)
300                         return []
301                 endif
302                 let revision=substitute(statusText, '^\_.*Working revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\)\_.*$', '\1', '')
304                 " We can still be in a CVS-controlled directory without this being a CVS
305                 " file
306                 if match(revision, '^New file!$') >= 0
307                         let revision='New'
308                 elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
309                         return ['Unknown']
310                 endif
312                 let branch=substitute(statusText, '^\_.*Sticky Tag:\s\+\(\d\+\%(\.\d\+\)\+\|\a[A-Za-z0-9-_]*\|(none)\).*$', '\1', '')
313                 let repository=substitute(statusText, '^\_.*Repository revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\|No revision control file\)\_.*$', '\1', '')
314                 let repository=substitute(repository, '^New file!\|No revision control file$', 'New', '')
315                 return [revision, repository, branch]
316         finally
317                 call VCSCommandChdir(oldCwd)
318         endtry
319 endfunction
321 " Function: s:cvsFunctions.Log() {{{2
322 function! s:cvsFunctions.Log(argList)
323         if len(a:argList) == 0
324                 let options = []
325                 let caption = ''
326         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
327                 let options = ['-r' . join(a:argList, ':')]
328                 let caption = options[0]
329         else
330                 " Pass-through
331                 let options = a:argList
332                 let caption = join(a:argList, ' ')
333         endif
335         return s:DoCommand(join(['log'] + options), 'log', caption, {})
336 endfunction
338 " Function: s:cvsFunctions.Revert(argList) {{{2
339 function! s:cvsFunctions.Revert(argList)
340         return s:DoCommand('update -C', 'revert', '', {})
341 endfunction
343 " Function: s:cvsFunctions.Review(argList) {{{2
344 function! s:cvsFunctions.Review(argList)
345         if len(a:argList) == 0
346                 let versiontag = '(current)'
347                 let versionOption = ''
348         else
349                 let versiontag = a:argList[0]
350                 let versionOption = ' -r ' . versiontag . ' '
351         endif
353         return s:DoCommand('-q update -p' . versionOption, 'review', versiontag, {})
354 endfunction
356 " Function: s:cvsFunctions.Status(argList) {{{2
357 function! s:cvsFunctions.Status(argList)
358         return s:DoCommand(join(['status'] + a:argList, ' '), 'status', join(a:argList, ' '), {})
359 endfunction
361 " Function: s:cvsFunctions.Update(argList) {{{2
362 function! s:cvsFunctions.Update(argList)
363         return s:DoCommand('update', 'update', '', {})
364 endfunction
366 " Section: CVS-specific functions {{{1
368 " Function: s:CVSEdit() {{{2
369 function! s:CVSEdit()
370         return s:DoCommand('edit', 'cvsedit', '', {})
371 endfunction
373 " Function: s:CVSEditors() {{{2
374 function! s:CVSEditors()
375         return s:DoCommand('editors', 'cvseditors', '', {})
376 endfunction
378 " Function: s:CVSUnedit() {{{2
379 function! s:CVSUnedit()
380         return s:DoCommand('unedit', 'cvsunedit', '', {})
381 endfunction
383 " Function: s:CVSWatch(onoff) {{{2
384 function! s:CVSWatch(onoff)
385         if a:onoff !~ '^\c\%(on\|off\|add\|remove\)$'
386                 echoerr 'Argument to CVSWatch must be one of [on|off|add|remove]'
387                 return -1
388         end
389         return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '', {})
390 endfunction
392 " Function: s:CVSWatchers() {{{2
393 function! s:CVSWatchers()
394         return s:DoCommand('watchers', 'cvswatchers', '', {})
395 endfunction
397 " Annotate setting {{{2
398 let s:cvsFunctions.AnnotateSplitRegex = '): '
400 " Section: Command definitions {{{1
401 " Section: Primary commands {{{2
402 com! CVSEdit call s:CVSEdit()
403 com! CVSEditors call s:CVSEditors()
404 com! CVSUnedit call s:CVSUnedit()
405 com! -nargs=1 CVSWatch call s:CVSWatch(<f-args>)
406 com! CVSWatchAdd call s:CVSWatch('add')
407 com! CVSWatchOn call s:CVSWatch('on')
408 com! CVSWatchOff call s:CVSWatch('off')
409 com! CVSWatchRemove call s:CVSWatch('remove')
410 com! CVSWatchers call s:CVSWatchers()
412 " Section: Plugin command mappings {{{1
414 let s:cvsExtensionMappings = {}
415 if !exists("no_plugin_maps")
416         let mappingInfo = [
417                                 \['CVSEdit', 'CVSEdit', 'e'],
418                                 \['CVSEditors', 'CVSEditors', 'E'],
419                                 \['CVSUnedit', 'CVSUnedit', 't'],
420                                 \['CVSWatchers', 'CVSWatchers', 'wv'],
421                                 \['CVSWatchAdd', 'CVSWatch add', 'wa'],
422                                 \['CVSWatchOff', 'CVSWatch off', 'wf'],
423                                 \['CVSWatchOn', 'CVSWatch on', 'wn'],
424                                 \['CVSWatchRemove', 'CVSWatch remove', 'wr']
425                                 \]
427         for [pluginName, commandText, shortCut] in mappingInfo
428                 execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
429                 if !hasmapto('<Plug>' . pluginName)
430                         let s:cvsExtensionMappings[shortCut] = commandText
431                 endif
432         endfor
433 endif
435 " Section: Plugin Registration {{{1
436 let s:VCSCommandUtility = VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
438 " Section: Menu items {{{1
439 for [s:shortcut, s:command] in [
440                         \['CVS.&Edit', '<Plug>CVSEdit'],
441                         \['CVS.Ed&itors', '<Plug>CVSEditors'],
442                         \['CVS.Unedi&t', '<Plug>CVSUnedit'],
443                         \['CVS.&Watchers', '<Plug>CVSWatchers'],
444                         \['CVS.WatchAdd', '<Plug>CVSWatchAdd'],
445                         \['CVS.WatchOn', '<Plug>CVSWatchOn'],
446                         \['CVS.WatchOff', '<Plug>CVSWatchOff'],
447                         \['CVS.WatchRemove', '<Plug>CVSWatchRemove']
448                         \]
449         call s:VCSCommandUtility.addMenuItem(s:shortcut, s:command)
450 endfor
451 unlet s:shortcut s:command
453 let &cpo = s:save_cpo