Merge tag 'v1.99.47' into bh/nerdtree
[vcscommand.git] / plugin / vcscvs.vim
blob7516316f0f074f4f9fef7ac4be659ec715f41105
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(path) {{{2
158 function! s:cvsFunctions.Identify(path)
159         if isdirectory(a:path)
160                 let directoryName = a:path
161         else
162                 let directoryName = fnamemodify(a:path, ':h')
163         endif
164         if strlen(directoryName) > 0
165                 let CVSRoot = directoryName . '/CVS/Root'
166         else
167                 let CVSRoot = 'CVS/Root'
168         endif
169         if filereadable(CVSRoot)
170                 return 1
171         else
172                 return 0
173         endif
174 endfunction
176 " Function: s:cvsFunctions.Add(argList) {{{2
177 function! s:cvsFunctions.Add(argList)
178         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
179 endfunction
181 " Function: s:cvsFunctions.Annotate(argList) {{{2
182 function! s:cvsFunctions.Annotate(argList)
183         if len(a:argList) == 0
184                 if &filetype ==? 'cvsannotate'
185                         " This is a CVSAnnotate buffer.  Perform annotation of the version
186                         " indicated by the current line.
187                         let caption = matchstr(getline('.'),'\v^[0-9.]+')
189                         if VCSCommandGetOption('VCSCommandCVSAnnotateParent', 0) != 0
190                                 if caption != '1.1'
191                                         let revmaj = matchstr(caption,'\v[0-9.]+\ze\.[0-9]+')
192                                         let revmin = matchstr(caption,'\v[0-9.]+\.\zs[0-9]+') - 1
193                                         if revmin == 0
194                                                 " Jump to ancestor branch
195                                                 let caption = matchstr(revmaj,'\v[0-9.]+\ze\.[0-9]+')
196                                         else
197                                                 let caption = revmaj . "." .  revmin
198                                         endif
199                                 endif
200                         endif
202                         let options = ['-r' . caption]
203                 else
204                         " CVS defaults to pulling HEAD, regardless of current branch.
205                         " Therefore, always pass desired revision.
206                         let caption = ''
207                         let options = ['-r' .  s:GetRevision()]
208                 endif
209         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
210                 let caption = a:argList[0]
211                 let options = ['-r' . caption]
212         else
213                 let caption = join(a:argList)
214                 let options = a:argList
215         endif
217         let resultBuffer = s:DoCommand(join(['-q', 'annotate'] + options), 'annotate', caption, {})
218         if resultBuffer > 0
219                 " Remove header lines from standard error
220                 silent v/^\d\+\%(\.\d\+\)\+/d
221         endif
222         return resultBuffer
223 endfunction
225 " Function: s:cvsFunctions.Commit(argList) {{{2
226 function! s:cvsFunctions.Commit(argList)
227         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
228         if resultBuffer == 0
229                 echomsg 'No commit needed.'
230         endif
231         return resultBuffer
232 endfunction
234 " Function: s:cvsFunctions.Delete() {{{2
235 " By default, use the -f option to remove the file first.  If options are
236 " passed in, use those instead.
237 function! s:cvsFunctions.Delete(argList)
238         let options = ['-f']
239         let caption = ''
240         if len(a:argList) > 0
241                 let options = a:argList
242                 let caption = join(a:argList, ' ')
243         endif
244         return s:DoCommand(join(['remove'] + options, ' '), 'delete', caption, {})
245 endfunction
247 " Function: s:cvsFunctions.Diff(argList) {{{2
248 function! s:cvsFunctions.Diff(argList)
249         if len(a:argList) == 0
250                 let revOptions = []
251                 let caption = ''
252         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
253                 let revOptions = ['-r' . join(a:argList, ' -r')]
254                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
255         else
256                 " Pass-through
257                 let caption = join(a:argList, ' ')
258                 let revOptions = a:argList
259         endif
261         let cvsDiffOpt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
262         if cvsDiffOpt == ''
263                 let diffOptions = []
264         else
265                 let diffOptions = ['-' . cvsDiffOpt]
266         endif
268         return s:DoCommand(join(['diff'] + diffOptions + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
269 endfunction
271 " Function: s:cvsFunctions.GetBufferInfo() {{{2
272 " Provides version control details for the current file.  Current version
273 " number and current repository version number are required to be returned by
274 " the vcscommand plugin.  This CVS extension adds branch name to the return
275 " list as well.
276 " Returns: List of results:  [revision, repository, branch]
278 function! s:cvsFunctions.GetBufferInfo()
279         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
280         let fileName = bufname(originalBuffer)
281         if isdirectory(fileName)
282                 let tag = ''
283                 if filereadable(fileName . '/CVS/Tag')
284                         let tagFile = readfile(fileName . '/CVS/Tag')
285                         if len(tagFile) == 1
286                                 let tag = substitute(tagFile[0], '^T', '', '')
287                         endif
288                 endif
289                 return [tag]
290         endif
291         let realFileName = fnamemodify(resolve(fileName), ':t')
292         if !filereadable(fileName)
293                 return ['Unknown']
294         endif
295         let oldCwd = VCSCommandChangeToCurrentFileDir(fileName)
296         try
297                 let statusText=s:VCSCommandUtility.system(s:Executable() . ' status -- "' . realFileName . '"')
298                 if(v:shell_error)
299                         return []
300                 endif
301                 let revision=substitute(statusText, '^\_.*Working revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\)\_.*$', '\1', '')
303                 " We can still be in a CVS-controlled directory without this being a CVS
304                 " file
305                 if match(revision, '^New file!$') >= 0
306                         let revision='New'
307                 elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
308                         return ['Unknown']
309                 endif
311                 let branch=substitute(statusText, '^\_.*Sticky Tag:\s\+\(\d\+\%(\.\d\+\)\+\|\a[A-Za-z0-9-_]*\|(none)\).*$', '\1', '')
312                 let repository=substitute(statusText, '^\_.*Repository revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\|No revision control file\)\_.*$', '\1', '')
313                 let repository=substitute(repository, '^New file!\|No revision control file$', 'New', '')
314                 return [revision, repository, branch]
315         finally
316                 call VCSCommandChdir(oldCwd)
317         endtry
318 endfunction
320 " Function: s:cvsFunctions.Log() {{{2
321 function! s:cvsFunctions.Log(argList)
322         if len(a:argList) == 0
323                 let options = []
324                 let caption = ''
325         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
326                 let options = ['-r' . join(a:argList, ':')]
327                 let caption = options[0]
328         else
329                 " Pass-through
330                 let options = a:argList
331                 let caption = join(a:argList, ' ')
332         endif
334         return s:DoCommand(join(['log'] + options), 'log', caption, {})
335 endfunction
337 " Function: s:cvsFunctions.Revert(argList) {{{2
338 function! s:cvsFunctions.Revert(argList)
339         return s:DoCommand('update -C', 'revert', '', {})
340 endfunction
342 " Function: s:cvsFunctions.Review(argList) {{{2
343 function! s:cvsFunctions.Review(argList)
344         if len(a:argList) == 0
345                 let versiontag = '(current)'
346                 let versionOption = ''
347         else
348                 let versiontag = a:argList[0]
349                 let versionOption = ' -r ' . versiontag . ' '
350         endif
352         return s:DoCommand('-q update -p' . versionOption, 'review', versiontag, {})
353 endfunction
355 " Function: s:cvsFunctions.Status(argList) {{{2
356 function! s:cvsFunctions.Status(argList)
357         return s:DoCommand(join(['status'] + a:argList, ' '), 'status', join(a:argList, ' '), {})
358 endfunction
360 " Function: s:cvsFunctions.Update(argList) {{{2
361 function! s:cvsFunctions.Update(argList)
362         return s:DoCommand('update', 'update', '', {})
363 endfunction
365 " Section: CVS-specific functions {{{1
367 " Function: s:CVSEdit() {{{2
368 function! s:CVSEdit()
369         return s:DoCommand('edit', 'cvsedit', '', {})
370 endfunction
372 " Function: s:CVSEditors() {{{2
373 function! s:CVSEditors()
374         return s:DoCommand('editors', 'cvseditors', '', {})
375 endfunction
377 " Function: s:CVSUnedit() {{{2
378 function! s:CVSUnedit()
379         return s:DoCommand('unedit', 'cvsunedit', '', {})
380 endfunction
382 " Function: s:CVSWatch(onoff) {{{2
383 function! s:CVSWatch(onoff)
384         if a:onoff !~ '^\c\%(on\|off\|add\|remove\)$'
385                 echoerr 'Argument to CVSWatch must be one of [on|off|add|remove]'
386                 return -1
387         end
388         return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '', {})
389 endfunction
391 " Function: s:CVSWatchers() {{{2
392 function! s:CVSWatchers()
393         return s:DoCommand('watchers', 'cvswatchers', '', {})
394 endfunction
396 " Annotate setting {{{2
397 let s:cvsFunctions.AnnotateSplitRegex = '): '
399 " Section: Command definitions {{{1
400 " Section: Primary commands {{{2
401 com! CVSEdit call s:CVSEdit()
402 com! CVSEditors call s:CVSEditors()
403 com! CVSUnedit call s:CVSUnedit()
404 com! -nargs=1 CVSWatch call s:CVSWatch(<f-args>)
405 com! CVSWatchAdd call s:CVSWatch('add')
406 com! CVSWatchOn call s:CVSWatch('on')
407 com! CVSWatchOff call s:CVSWatch('off')
408 com! CVSWatchRemove call s:CVSWatch('remove')
409 com! CVSWatchers call s:CVSWatchers()
411 " Section: Plugin command mappings {{{1
413 let s:cvsExtensionMappings = {}
414 if !exists("no_plugin_maps")
415         let mappingInfo = [
416                                 \['CVSEdit', 'CVSEdit', 'e'],
417                                 \['CVSEditors', 'CVSEditors', 'E'],
418                                 \['CVSUnedit', 'CVSUnedit', 't'],
419                                 \['CVSWatchers', 'CVSWatchers', 'wv'],
420                                 \['CVSWatchAdd', 'CVSWatch add', 'wa'],
421                                 \['CVSWatchOff', 'CVSWatch off', 'wf'],
422                                 \['CVSWatchOn', 'CVSWatch on', 'wn'],
423                                 \['CVSWatchRemove', 'CVSWatch remove', 'wr']
424                                 \]
426         for [pluginName, commandText, shortCut] in mappingInfo
427                 execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
428                 if !hasmapto('<Plug>' . pluginName)
429                         let s:cvsExtensionMappings[shortCut] = commandText
430                 endif
431         endfor
432 endif
434 " Section: Plugin Registration {{{1
435 let s:VCSCommandUtility = VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
437 " Section: Menu items {{{1
438 for [s:shortcut, s:command] in [
439                         \['CVS.&Edit', '<Plug>CVSEdit'],
440                         \['CVS.Ed&itors', '<Plug>CVSEditors'],
441                         \['CVS.Unedi&t', '<Plug>CVSUnedit'],
442                         \['CVS.&Watchers', '<Plug>CVSWatchers'],
443                         \['CVS.WatchAdd', '<Plug>CVSWatchAdd'],
444                         \['CVS.WatchOn', '<Plug>CVSWatchOn'],
445                         \['CVS.WatchOff', '<Plug>CVSWatchOff'],
446                         \['CVS.WatchRemove', '<Plug>CVSWatchRemove']
447                         \]
448         call s:VCSCommandUtility.addMenuItem(s:shortcut, s:command)
449 endfor
450 unlet s:shortcut s:command
452 let &cpo = s:save_cpo