1 " vim600: set foldmethod=marker:
3 " CVS extension for VCSCommand.
5 " Maintainer: Bob Hiestand <bob.hiestand@gmail.com>
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
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
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:
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
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')
88 echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
92 runtime plugin/vcscommand.vim
94 if !executable(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
95 " CVS is not installed
102 " Section: Variable initialization {{{1
104 let s:cvsFunctions = {}
106 " Section: Utility functions {{{1
108 " Function: s:Executable() {{{2
109 " Returns the executable used to invoke cvs suitable for use in a shell
111 function! s:Executable()
112 return shellescape(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
115 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
116 " Wrapper to VCSCommandDoCommand to add the name of the CVS executable to the
118 function! s:DoCommand(cmd, cmdName, statusText, options)
119 if VCSCommandGetVCSType(expand('%')) == 'CVS'
120 let fullCmd = s:Executable() . ' ' . a:cmd
121 let ret = VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
124 if getline(line('$')) =~ '^cvs \w\+: closing down connection'
133 throw 'CVS VCSCommand plugin called on non-CVS item.'
137 " Function: s:GetRevision() {{{2
138 " Function for retrieving the current buffer's revision number.
139 " Returns: Revision number or an empty string if an error occurs.
141 function! s:GetRevision()
142 if !exists('b:VCSCommandBufferInfo')
143 let b:VCSCommandBufferInfo = s:cvsFunctions.GetBufferInfo()
146 if len(b:VCSCommandBufferInfo) > 0
147 return b:VCSCommandBufferInfo[0]
153 " Section: VCS function implementations {{{1
155 " Function: s:cvsFunctions.Identify(buffer) {{{2
156 function! s:cvsFunctions.Identify(buffer)
157 let fileName = resolve(bufname(a:buffer))
158 if isdirectory(fileName)
159 let directoryName = fileName
161 let directoryName = fnamemodify(fileName, ':h')
163 if strlen(directoryName) > 0
164 let CVSRoot = directoryName . '/CVS/Root'
166 let CVSRoot = 'CVS/Root'
168 if filereadable(CVSRoot)
175 " Function: s:cvsFunctions.Add(argList) {{{2
176 function! s:cvsFunctions.Add(argList)
177 return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
180 " Function: s:cvsFunctions.Annotate(argList) {{{2
181 function! s:cvsFunctions.Annotate(argList)
182 if len(a:argList) == 0
183 if &filetype == 'CVSannotate'
184 " This is a CVSAnnotate buffer. Perform annotation of the version
185 " indicated by the current line.
186 let caption = matchstr(getline('.'),'\v^[0-9.]+')
188 if VCSCommandGetOption('VCSCommandCVSAnnotateParent', 0) != 0
190 let revmaj = matchstr(caption,'\v[0-9.]+\ze\.[0-9]+')
191 let revmin = matchstr(caption,'\v[0-9.]+\.\zs[0-9]+') - 1
193 " Jump to ancestor branch
194 let caption = matchstr(revmaj,'\v[0-9.]+\ze\.[0-9]+')
196 let caption = revmaj . "." . revmin
201 let options = ['-r' . caption]
203 " CVS defaults to pulling HEAD, regardless of current branch.
204 " Therefore, always pass desired revision.
206 let options = ['-r' . s:GetRevision()]
208 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
209 let caption = a:argList[0]
210 let options = ['-r' . caption]
212 let caption = join(a:argList)
213 let options = a:argList
216 let resultBuffer = s:DoCommand(join(['-q', 'annotate'] + options), 'annotate', caption, {})
218 " Remove header lines from standard error
219 silent v/^\d\+\%(\.\d\+\)\+/d
224 " Function: s:cvsFunctions.Commit(argList) {{{2
225 function! s:cvsFunctions.Commit(argList)
226 let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
228 echomsg 'No commit needed.'
233 " Function: s:cvsFunctions.Delete() {{{2
234 " By default, use the -f option to remove the file first. If options are
235 " passed in, use those instead.
236 function! s:cvsFunctions.Delete(argList)
239 if len(a:argList) > 0
240 let options = a:argList
241 let caption = join(a:argList, ' ')
243 return s:DoCommand(join(['remove'] + options, ' '), 'delete', caption, {})
246 " Function: s:cvsFunctions.Diff(argList) {{{2
247 function! s:cvsFunctions.Diff(argList)
248 if len(a:argList) == 0
251 elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
252 let revOptions = ['-r' . join(a:argList, ' -r')]
253 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
256 let caption = join(a:argList, ' ')
257 let revOptions = a:argList
260 let cvsDiffOpt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
264 let diffOptions = ['-' . cvsDiffOpt]
267 return s:DoCommand(join(['diff'] + diffOptions + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
270 " Function: s:cvsFunctions.GetBufferInfo() {{{2
271 " Provides version control details for the current file. Current version
272 " number and current repository version number are required to be returned by
273 " the vcscommand plugin. This CVS extension adds branch name to the return
275 " Returns: List of results: [revision, repository, branch]
277 function! s:cvsFunctions.GetBufferInfo()
278 let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
279 let fileName = bufname(originalBuffer)
280 if isdirectory(fileName)
282 if filereadable(fileName . '/CVS/Tag')
283 let tagFile = readfile(fileName . '/CVS/Tag')
285 let tag = substitute(tagFile[0], '^T', '', '')
290 let realFileName = fnamemodify(resolve(fileName), ':t')
291 if !filereadable(fileName)
294 let oldCwd = VCSCommandChangeToCurrentFileDir(fileName)
296 let statusText=s:VCSCommandUtility.system(s:Executable() . ' status -- "' . realFileName . '"')
300 let revision=substitute(statusText, '^\_.*Working revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\)\_.*$', '\1', '')
302 " We can still be in a CVS-controlled directory without this being a CVS
304 if match(revision, '^New file!$') >= 0
306 elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
310 let branch=substitute(statusText, '^\_.*Sticky Tag:\s\+\(\d\+\%(\.\d\+\)\+\|\a[A-Za-z0-9-_]*\|(none)\).*$', '\1', '')
311 let repository=substitute(statusText, '^\_.*Repository revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\|No revision control file\)\_.*$', '\1', '')
312 let repository=substitute(repository, '^New file!\|No revision control file$', 'New', '')
313 return [revision, repository, branch]
315 call VCSCommandChdir(oldCwd)
319 " Function: s:cvsFunctions.Log() {{{2
320 function! s:cvsFunctions.Log(argList)
321 if len(a:argList) == 0
324 elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
325 let options = ['-r' . join(a:argList, ':')]
326 let caption = options[0]
329 let options = a:argList
330 let caption = join(a:argList, ' ')
333 return s:DoCommand(join(['log'] + options), 'log', caption, {})
336 " Function: s:cvsFunctions.Revert(argList) {{{2
337 function! s:cvsFunctions.Revert(argList)
338 return s:DoCommand('update -C', 'revert', '', {})
341 " Function: s:cvsFunctions.Review(argList) {{{2
342 function! s:cvsFunctions.Review(argList)
343 if len(a:argList) == 0
344 let versiontag = '(current)'
345 let versionOption = ''
347 let versiontag = a:argList[0]
348 let versionOption = ' -r ' . versiontag . ' '
351 return s:DoCommand('-q update -p' . versionOption, 'review', versiontag, {})
354 " Function: s:cvsFunctions.Status(argList) {{{2
355 function! s:cvsFunctions.Status(argList)
356 return s:DoCommand(join(['status'] + a:argList, ' '), 'status', join(a:argList, ' '), {})
359 " Function: s:cvsFunctions.Update(argList) {{{2
360 function! s:cvsFunctions.Update(argList)
361 return s:DoCommand('update', 'update', '', {})
364 " Section: CVS-specific functions {{{1
366 " Function: s:CVSEdit() {{{2
367 function! s:CVSEdit()
368 return s:DoCommand('edit', 'cvsedit', '', {})
371 " Function: s:CVSEditors() {{{2
372 function! s:CVSEditors()
373 return s:DoCommand('editors', 'cvseditors', '', {})
376 " Function: s:CVSUnedit() {{{2
377 function! s:CVSUnedit()
378 return s:DoCommand('unedit', 'cvsunedit', '', {})
381 " Function: s:CVSWatch(onoff) {{{2
382 function! s:CVSWatch(onoff)
383 if a:onoff !~ '^\c\%(on\|off\|add\|remove\)$'
384 echoerr 'Argument to CVSWatch must be one of [on|off|add|remove]'
387 return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '', {})
390 " Function: s:CVSWatchers() {{{2
391 function! s:CVSWatchers()
392 return s:DoCommand('watchers', 'cvswatchers', '', {})
395 " Annotate setting {{{2
396 let s:cvsFunctions.AnnotateSplitRegex = '): '
398 " Section: Command definitions {{{1
399 " Section: Primary commands {{{2
400 com! CVSEdit call s:CVSEdit()
401 com! CVSEditors call s:CVSEditors()
402 com! CVSUnedit call s:CVSUnedit()
403 com! -nargs=1 CVSWatch call s:CVSWatch(<f-args>)
404 com! CVSWatchAdd call s:CVSWatch('add')
405 com! CVSWatchOn call s:CVSWatch('on')
406 com! CVSWatchOff call s:CVSWatch('off')
407 com! CVSWatchRemove call s:CVSWatch('remove')
408 com! CVSWatchers call s:CVSWatchers()
410 " Section: Plugin command mappings {{{1
412 let s:cvsExtensionMappings = {}
414 \['CVSEdit', 'CVSEdit', 'e'],
415 \['CVSEditors', 'CVSEditors', 'E'],
416 \['CVSUnedit', 'CVSUnedit', 't'],
417 \['CVSWatchers', 'CVSWatchers', 'wv'],
418 \['CVSWatchAdd', 'CVSWatch add', 'wa'],
419 \['CVSWatchOff', 'CVSWatch off', 'wf'],
420 \['CVSWatchOn', 'CVSWatch on', 'wn'],
421 \['CVSWatchRemove', 'CVSWatch remove', 'wr']
424 for [pluginName, commandText, shortCut] in mappingInfo
425 execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
426 if !hasmapto('<Plug>' . pluginName)
427 let s:cvsExtensionMappings[shortCut] = commandText
431 " Section: Plugin Registration {{{1
432 let s:VCSCommandUtility = VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
434 " Section: Menu items {{{1
435 for [s:shortcut, s:command] in [
436 \['CVS.&Edit', '<Plug>CVSEdit'],
437 \['CVS.Ed&itors', '<Plug>CVSEditors'],
438 \['CVS.Unedi&t', '<Plug>CVSUnedit'],
439 \['CVS.&Watchers', '<Plug>CVSWatchers'],
440 \['CVS.WatchAdd', '<Plug>CVSWatchAdd'],
441 \['CVS.WatchOn', '<Plug>CVSWatchOn'],
442 \['CVS.WatchOff', '<Plug>CVSWatchOff'],
443 \['CVS.WatchRemove', '<Plug>CVSWatchRemove']
445 call s:VCSCommandUtility.addMenuItem(s:shortcut, s:command)
447 unlet s:shortcut s:command
449 let &cpo = s:save_cpo