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 if !exists('g:loaded_VCSCommand')
93 runtime plugin/vcscommand.vim
96 if !executable(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
97 " CVS is not installed
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
113 function! s:Executable()
114 return VCSCommandGetOption('VCSCommandCVSExec', 'cvs')
117 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
118 " Wrapper to VCSCommandDoCommand to add the name of the CVS executable to the
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)
126 if getline(line('$')) =~ '^cvs \w\+: closing down connection'
135 throw 'CVS VCSCommand plugin called on non-CVS item.'
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()
148 if len(b:VCSCommandBufferInfo) > 0
149 return b:VCSCommandBufferInfo[0]
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
163 let directoryName = fnamemodify(fileName, ':h')
165 if strlen(directoryName) > 0
166 let CVSRoot = directoryName . '/CVS/Root'
168 let CVSRoot = 'CVS/Root'
170 if filereadable(CVSRoot)
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, ' '), {})
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
192 let revmaj = matchstr(caption,'\v[0-9.]+\ze\.[0-9]+')
193 let revmin = matchstr(caption,'\v[0-9.]+\.\zs[0-9]+') - 1
195 " Jump to ancestor branch
196 let caption = matchstr(revmaj,'\v[0-9.]+\ze\.[0-9]+')
198 let caption = revmaj . "." . revmin
203 let options = ['-r' . caption]
205 " CVS defaults to pulling HEAD, regardless of current branch.
206 " Therefore, always pass desired revision.
208 let options = ['-r' . s:GetRevision()]
210 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
211 let caption = a:argList[0]
212 let options = ['-r' . caption]
214 let caption = join(a:argList)
215 let options = a:argList
218 let resultBuffer = s:DoCommand(join(['-q', 'annotate'] + options), 'annotate', caption, {})
220 " Remove header lines from standard error
221 silent v/^\d\+\%(\.\d\+\)\+/d
226 " Function: s:cvsFunctions.Commit(argList) {{{2
227 function! s:cvsFunctions.Commit(argList)
228 let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
230 echomsg 'No commit needed.'
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)
241 if len(a:argList) > 0
242 let options = a:argList
243 let caption = join(a:argList, ' ')
245 return s:DoCommand(join(['remove'] + options, ' '), 'delete', caption, {})
248 " Function: s:cvsFunctions.Diff(argList) {{{2
249 function! s:cvsFunctions.Diff(argList)
250 if len(a:argList) == 0
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') . ')'
258 let caption = join(a:argList, ' ')
259 let revOptions = a:argList
262 let cvsDiffOpt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
266 let diffOptions = ['-' . cvsDiffOpt]
269 return s:DoCommand(join(['diff'] + diffOptions + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
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
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)
284 if filereadable(fileName . '/CVS/Tag')
285 let tagFile = readfile(fileName . '/CVS/Tag')
287 let tag = substitute(tagFile[0], '^T', '', '')
292 let realFileName = fnamemodify(resolve(fileName), ':t')
293 if !filereadable(fileName)
296 let oldCwd = VCSCommandChangeToCurrentFileDir(fileName)
298 let statusText=s:VCSCommandUtility.system(s:Executable() . ' status -- "' . realFileName . '"')
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
306 if match(revision, '^New file!$') >= 0
308 elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
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]
317 call VCSCommandChdir(oldCwd)
321 " Function: s:cvsFunctions.Log() {{{2
322 function! s:cvsFunctions.Log(argList)
323 if len(a:argList) == 0
326 elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
327 let options = ['-r' . join(a:argList, ':')]
328 let caption = options[0]
331 let options = a:argList
332 let caption = join(a:argList, ' ')
335 return s:DoCommand(join(['log'] + options), 'log', caption, {})
338 " Function: s:cvsFunctions.Revert(argList) {{{2
339 function! s:cvsFunctions.Revert(argList)
340 return s:DoCommand('update -C', 'revert', '', {})
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 = ''
349 let versiontag = a:argList[0]
350 let versionOption = ' -r ' . versiontag . ' '
353 return s:DoCommand('-q update -p' . versionOption, 'review', versiontag, {})
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, ' '), {})
361 " Function: s:cvsFunctions.Update(argList) {{{2
362 function! s:cvsFunctions.Update(argList)
363 return s:DoCommand('update', 'update', '', {})
366 " Section: CVS-specific functions {{{1
368 " Function: s:CVSEdit() {{{2
369 function! s:CVSEdit()
370 return s:DoCommand('edit', 'cvsedit', '', {})
373 " Function: s:CVSEditors() {{{2
374 function! s:CVSEditors()
375 return s:DoCommand('editors', 'cvseditors', '', {})
378 " Function: s:CVSUnedit() {{{2
379 function! s:CVSUnedit()
380 return s:DoCommand('unedit', 'cvsunedit', '', {})
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]'
389 return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '', {})
392 " Function: s:CVSWatchers() {{{2
393 function! s:CVSWatchers()
394 return s:DoCommand('watchers', 'cvswatchers', '', {})
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")
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']
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
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']
449 call s:VCSCommandUtility.addMenuItem(s:shortcut, s:command)
451 unlet s:shortcut s:command
453 let &cpo = s:save_cpo