host info updates
[distro-setup] / .vim / plugin / vcscvs.vim
1 " vim600: set foldmethod=marker:
2 "
3 " CVS extension for VCSCommand.
4 "
5 " Maintainer: Bob Hiestand <bob.hiestand@gmail.com>
6 " License:
7 " Copyright (c) Bob Hiestand
8 "
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:
15 "
16 " The above copyright notice and this permission notice shall be included in
17 " all copies or substantial portions of the Software.
18 "
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.
26 "
27 " Section: Documentation {{{1
28 "
29 " Command documentation {{{2
30 "
31 " The following commands only apply to files under CVS source control.
32 "
33 " CVSEdit Performs "cvs edit" on the current file.
34 "
35 " CVSEditors Performs "cvs editors" on the current file.
36 "
37 " CVSUnedit Performs "cvs unedit" on the current file.
38 "
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.
42 "
43 " CVSWatchers Performs "cvs watchers" on the current file.
44 "
45 " CVSWatchAdd Alias for "CVSWatch add"
46 "
47 " CVSWatchOn Alias for "CVSWatch on"
48 "
49 " CVSWatchOff Alias for "CVSWatch off"
50 "
51 " CVSWatchRemove Alias for "CVSWatch remove"
52 "
53 " Mapping documentation: {{{2
54 "
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:
57 "
58 " nnoremap ,ce <Plug>CVSEdit
59 "
60 " The default mappings are as follow:
61 "
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
70 "
71 " Options documentation: {{{2
72 "
73 " VCSCommandCVSExec
74 " This variable specifies the CVS executable. If not set, it defaults to
75 " 'cvs' executed from the user's executable path.
76 "
77 " VCSCommandCVSDiffOpt
78 " This variable, if set, determines the options passed to the cvs diff
79 " command. If not set, it defaults to 'u'.
80
81 " Section: Plugin header {{{1
82
83 if exists('VCSCommandDisableAll')
84 finish
85 endif
86
87 if v:version < 700
88 echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
89 finish
90 endif
91
92 runtime plugin/vcscommand.vim
93
94 if !executable(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
95 " CVS is not installed
96 finish
97 endif
98
99 let s:save_cpo=&cpo
100 set cpo&vim
101
102 " Section: Variable initialization {{{1
103
104 let s:cvsFunctions = {}
105
106 " Section: Utility functions {{{1
107
108 " Function: s:Executable() {{{2
109 " Returns the executable used to invoke cvs suitable for use in a shell
110 " command.
111 function! s:Executable()
112 return shellescape(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
113 endfunction
114
115 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
116 " Wrapper to VCSCommandDoCommand to add the name of the CVS executable to the
117 " command argument.
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)
122
123 if ret > 0
124 if getline(line('$')) =~ '^cvs \w\+: closing down connection'
125 $d
126 1
127 endif
128
129 endif
130
131 return ret
132 else
133 throw 'CVS VCSCommand plugin called on non-CVS item.'
134 endif
135 endfunction
136
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.
140
141 function! s:GetRevision()
142 if !exists('b:VCSCommandBufferInfo')
143 let b:VCSCommandBufferInfo = s:cvsFunctions.GetBufferInfo()
144 endif
145
146 if len(b:VCSCommandBufferInfo) > 0
147 return b:VCSCommandBufferInfo[0]
148 else
149 return ''
150 endif
151 endfunction
152
153 " Section: VCS function implementations {{{1
154
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
160 else
161 let directoryName = fnamemodify(fileName, ':h')
162 endif
163 if strlen(directoryName) > 0
164 let CVSRoot = directoryName . '/CVS/Root'
165 else
166 let CVSRoot = 'CVS/Root'
167 endif
168 if filereadable(CVSRoot)
169 return 1
170 else
171 return 0
172 endif
173 endfunction
174
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, ' '), {})
178 endfunction
179
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.]+')
187
188 if VCSCommandGetOption('VCSCommandCVSAnnotateParent', 0) != 0
189 if caption != '1.1'
190 let revmaj = matchstr(caption,'\v[0-9.]+\ze\.[0-9]+')
191 let revmin = matchstr(caption,'\v[0-9.]+\.\zs[0-9]+') - 1
192 if revmin == 0
193 " Jump to ancestor branch
194 let caption = matchstr(revmaj,'\v[0-9.]+\ze\.[0-9]+')
195 else
196 let caption = revmaj . "." . revmin
197 endif
198 endif
199 endif
200
201 let options = ['-r' . caption]
202 else
203 " CVS defaults to pulling HEAD, regardless of current branch.
204 " Therefore, always pass desired revision.
205 let caption = ''
206 let options = ['-r' . s:GetRevision()]
207 endif
208 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
209 let caption = a:argList[0]
210 let options = ['-r' . caption]
211 else
212 let caption = join(a:argList)
213 let options = a:argList
214 endif
215
216 let resultBuffer = s:DoCommand(join(['-q', 'annotate'] + options), 'annotate', caption, {})
217 if resultBuffer > 0
218 " Remove header lines from standard error
219 silent v/^\d\+\%(\.\d\+\)\+/d
220 endif
221 return resultBuffer
222 endfunction
223
224 " Function: s:cvsFunctions.Commit(argList) {{{2
225 function! s:cvsFunctions.Commit(argList)
226 let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
227 if resultBuffer == 0
228 echomsg 'No commit needed.'
229 endif
230 return resultBuffer
231 endfunction
232
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)
237 let options = ['-f']
238 let caption = ''
239 if len(a:argList) > 0
240 let options = a:argList
241 let caption = join(a:argList, ' ')
242 endif
243 return s:DoCommand(join(['remove'] + options, ' '), 'delete', caption, {})
244 endfunction
245
246 " Function: s:cvsFunctions.Diff(argList) {{{2
247 function! s:cvsFunctions.Diff(argList)
248 if len(a:argList) == 0
249 let revOptions = []
250 let caption = ''
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') . ')'
254 else
255 " Pass-through
256 let caption = join(a:argList, ' ')
257 let revOptions = a:argList
258 endif
259
260 let cvsDiffOpt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
261 if cvsDiffOpt == ''
262 let diffOptions = []
263 else
264 let diffOptions = ['-' . cvsDiffOpt]
265 endif
266
267 return s:DoCommand(join(['diff'] + diffOptions + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
268 endfunction
269
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
274 " list as well.
275 " Returns: List of results: [revision, repository, branch]
276
277 function! s:cvsFunctions.GetBufferInfo()
278 let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
279 let fileName = bufname(originalBuffer)
280 if isdirectory(fileName)
281 let tag = ''
282 if filereadable(fileName . '/CVS/Tag')
283 let tagFile = readfile(fileName . '/CVS/Tag')
284 if len(tagFile) == 1
285 let tag = substitute(tagFile[0], '^T', '', '')
286 endif
287 endif
288 return [tag]
289 endif
290 let realFileName = fnamemodify(resolve(fileName), ':t')
291 if !filereadable(fileName)
292 return ['Unknown']
293 endif
294 let oldCwd = VCSCommandChangeToCurrentFileDir(fileName)
295 try
296 let statusText=s:VCSCommandUtility.system(s:Executable() . ' status -- "' . realFileName . '"')
297 if(v:shell_error)
298 return []
299 endif
300 let revision=substitute(statusText, '^\_.*Working revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\)\_.*$', '\1', '')
301
302 " We can still be in a CVS-controlled directory without this being a CVS
303 " file
304 if match(revision, '^New file!$') >= 0
305 let revision='New'
306 elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
307 return ['Unknown']
308 endif
309
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]
314 finally
315 call VCSCommandChdir(oldCwd)
316 endtry
317 endfunction
318
319 " Function: s:cvsFunctions.Log() {{{2
320 function! s:cvsFunctions.Log(argList)
321 if len(a:argList) == 0
322 let options = []
323 let caption = ''
324 elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
325 let options = ['-r' . join(a:argList, ':')]
326 let caption = options[0]
327 else
328 " Pass-through
329 let options = a:argList
330 let caption = join(a:argList, ' ')
331 endif
332
333 return s:DoCommand(join(['log'] + options), 'log', caption, {})
334 endfunction
335
336 " Function: s:cvsFunctions.Revert(argList) {{{2
337 function! s:cvsFunctions.Revert(argList)
338 return s:DoCommand('update -C', 'revert', '', {})
339 endfunction
340
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 = ''
346 else
347 let versiontag = a:argList[0]
348 let versionOption = ' -r ' . versiontag . ' '
349 endif
350
351 return s:DoCommand('-q update -p' . versionOption, 'review', versiontag, {})
352 endfunction
353
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, ' '), {})
357 endfunction
358
359 " Function: s:cvsFunctions.Update(argList) {{{2
360 function! s:cvsFunctions.Update(argList)
361 return s:DoCommand('update', 'update', '', {})
362 endfunction
363
364 " Section: CVS-specific functions {{{1
365
366 " Function: s:CVSEdit() {{{2
367 function! s:CVSEdit()
368 return s:DoCommand('edit', 'cvsedit', '', {})
369 endfunction
370
371 " Function: s:CVSEditors() {{{2
372 function! s:CVSEditors()
373 return s:DoCommand('editors', 'cvseditors', '', {})
374 endfunction
375
376 " Function: s:CVSUnedit() {{{2
377 function! s:CVSUnedit()
378 return s:DoCommand('unedit', 'cvsunedit', '', {})
379 endfunction
380
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]'
385 return -1
386 end
387 return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '', {})
388 endfunction
389
390 " Function: s:CVSWatchers() {{{2
391 function! s:CVSWatchers()
392 return s:DoCommand('watchers', 'cvswatchers', '', {})
393 endfunction
394
395 " Annotate setting {{{2
396 let s:cvsFunctions.AnnotateSplitRegex = '): '
397
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()
409
410 " Section: Plugin command mappings {{{1
411
412 let s:cvsExtensionMappings = {}
413 let mappingInfo = [
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']
422 \]
423
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
428 endif
429 endfor
430
431 " Section: Plugin Registration {{{1
432 let s:VCSCommandUtility = VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
433
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']
444 \]
445 call s:VCSCommandUtility.addMenuItem(s:shortcut, s:command)
446 endfor
447 unlet s:shortcut s:command
448
449 let &cpo = s:save_cpo