1 " vim600: set foldmethod=marker:
3 " CVS extension for VCSCommand.
5 " Version: VCS development
6 " Maintainer: Bob Hiestand <bob.hiestand@gmail.com>
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
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.
36 " CVSEditors Performs "cvs editors" on the current file.
38 " CVSUnedit Performs "cvs unedit" on the current file.
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
44 " CVSWatchers Performs "cvs watchers" on the current file.
46 " CVSWatchAdd Alias for "CVSWatch add"
48 " CVSWatchOn Alias for "CVSWatch on"
50 " CVSWatchOff Alias for "CVSWatch off"
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:
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
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'.
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
95 function! s:DoCommand(cmd, cmdName, statusText)
97 if VCSCommandGetVCSType(expand('%')) == 'CVS'
98 let fullCmd = VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' ' . a:cmd
99 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText)
101 throw 'No suitable plugin'
103 catch /No suitable plugin/
104 echohl WarningMsg|echomsg 'Cannot apply CVS commands to this file.'|echohl None
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
116 let directory = fnamemodify(fileName, ':h')
118 if strlen(directory) > 0
119 let CVSRoot = directory . '/CVS/Root'
121 let CVSRoot = 'CVS/Root'
123 if filereadable(CVSRoot)
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, ' '))
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.]+)')
143 let revision=VCSCommandGetRevision()
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.'
153 let revision=a:argList[0]
157 throw 'No annotatation available for new file.'
160 let resultBuffer=s:DoCommand('-q annotate -r ' . revision, 'annotate', revision)
162 set filetype=CVSAnnotate
163 " Remove header lines from standard error
164 silent v/^\d\+\%(\.\d\+\)\+/d
169 " Function: s:cvsFunctions.Commit(argList) {{{2
170 function! s:cvsFunctions.Commit(argList)
171 let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '')
173 echomsg 'No commit needed.'
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, ' '))
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] . ')'
196 let cvsdiffopt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
199 let diffoptionstring = ''
201 let diffoptionstring = ' -' . cvsdiffopt . ' '
204 let resultBuffer = s:DoCommand('diff ' . diffoptionstring . revOptions , 'diff', caption)
208 echomsg 'No differences found'
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
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)
227 let oldCwd=VCSCommandChangeToCurrentFileDir(fileName)
229 let statusText=system(VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' status "' . realFileName . '"')
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
237 if match(revision, '^New file!$') >= 0
239 elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
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]
248 execute 'cd' escape(oldCwd, ' ')
252 " Function: s:cvsFunctions.Log() {{{2
253 function! s:cvsFunctions.Log(argList)
254 if len(a:argList) == 0
255 let versionOption = ''
257 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
258 let versionOption =' -r' . a:argList[0]
259 let caption = a:argList[0]
261 " Multiple options, or the option starts with '-'
262 let caption = join(a:argList, ' ')
263 let versionOption = ' ' . caption
266 let resultBuffer=s:DoCommand('log' . versionOption, 'log', caption)
273 " Function: s:cvsFunctions.Revert(argList) {{{2
274 function! s:cvsFunctions.Revert(argList)
275 return s:DoCommand('update -C', 'revert', '')
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 = ''
284 let versiontag = a:argList[0]
285 let versionOption = ' -r ' . versiontag . ' '
288 let resultBuffer = s:DoCommand('-q update -p' . versionOption, 'review', versiontag)
290 let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
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, ' '))
300 " Function: s:cvsFunctions.Update(argList) {{{2
301 function! s:cvsFunctions.Update(argList)
302 return s:DoCommand('update', 'update', '')
305 " Section: CVS-specific functions {{{1
307 " Function: s:CVSEdit() {{{2
308 function! s:CVSEdit()
309 return s:DoCommand('edit', 'cvsedit', '')
312 " Function: s:CVSEditors() {{{2
313 function! s:CVSEditors()
314 return s:DoCommand('editors', 'cvseditors', '')
317 " Function: s:CVSUnedit() {{{2
318 function! s:CVSUnedit()
319 return s:DoCommand('unedit', 'cvsunedit', '')
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]'
328 return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '')
331 " Function: s:CVSWatchers() {{{2
332 function! s:CVSWatchers()
333 return s:DoCommand('watchers', 'cvswatchers', '')
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 = {}
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']
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
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
383 if exists('g:loaded_VCSCommand')
384 call VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
387 au User VCSLoadExtensions call VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)