File: vcscvs.vim

package info (click to toggle)
vim-scripts 7-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,228 kB
  • ctags: 1,793
  • sloc: perl: 109; makefile: 30; sh: 11
file content (362 lines) | stat: -rw-r--r-- 11,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
" vim600: set foldmethod=marker:
"
" CVS extension for VCSCommand.
"
" Last Change:
" Version:       VCS development
" Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
" License:       This file is placed in the public domain.
"
" Section: Documentation {{{1
"
" Command documentation {{{2
"
" The following commands only apply to files under CVS source control.
"
" CVSEdit          Performs "cvs edit" on the current file.
"   
" CVSEditors       Performs "cvs editors" on the current file.
"   
" CVSUnedit        Performs "cvs unedit" on the current file.
"   
" CVSWatch         Takes an argument which must be one of [on|off|add|remove].
"                  Performs "cvs watch" with the given argument on the current
"                  file.
"   
" CVSWatchers      Performs "cvs watchers" on the current file.
"   
" CVSWatchAdd      Alias for "CVSWatch add"
"   
" CVSWatchOn       Alias for "CVSWatch on"
"   
" CVSWatchOff      Alias for "CVSWatch off"
"   
" CVSWatchRemove   Alias for "CVSWatch remove"
"
" Mapping documentation: {{{2
"
" By default, a mapping is defined for each command.  User-provided mappings
" can be used instead by mapping to <Plug>CommandName, for instance:
"
" nnoremap ,ce <Plug>CVSEdit
"
" The default mappings are as follow:
"
"   <Leader>ce CVSEdit
"   <Leader>ci CVSEditors
"   <Leader>ct CVSUnedit
"   <Leader>cwv CVSWatchers
"   <Leader>cwa CVSWatchAdd
"   <Leader>cwn CVSWatchOn
"   <Leader>cwf CVSWatchOff
"   <Leader>cwr CVSWatchRemove
"
" Options documentation: {{{2
"
" VCSCommandCVSExec
"   This variable specifies the CVS executable.  If not set, it defaults to
"   'cvs' executed from the user's executable path.
"
" VCSCommandCVSDiffOpt
"   This variable, if set, determines the options passed to the cvs diff
"   command.  If not set, it defaults to 'u'.

if v:version < 700
  finish
endif

" Section: Variable initialization {{{1

let s:cvsFunctions = {}

" Section: Utility functions {{{1

" Function: s:DoCommand(cmd, cmdName, statusText) {{{2
" Wrapper to VCSCommandDoCommand to add the name of the CVS executable to the
" command argument.
function! s:DoCommand(cmd, cmdName, statusText)
  try
    if VCSCommandGetVCSType(expand('%')) == 'CVS'
      let fullCmd = VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' ' . a:cmd
      return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText)
    else
      throw 'No suitable plugin'
    endif
  catch /No suitable plugin/
    echohl WarningMsg|echomsg 'Cannot apply CVS commands to this file.'|echohl None
  endtry
endfunction

" Section: VCS function implementations {{{1

" Function: s:cvsFunctions.Identify(buffer) {{{2
function! s:cvsFunctions.Identify(buffer)
  let fileName = resolve(bufname(a:buffer))
  if isdirectory(fileName)
    let directory = fileName
  else
    let directory = fnamemodify(fileName, ':h')
  endif
  if strlen(directory) > 0
    let CVSRoot = directory . '/CVS/Root'
  else
    let CVSRoot = 'CVS/Root'
  endif
  if filereadable(CVSRoot)
    return 1
  else
    return 0
  endif
endfunction

" Function: s:cvsFunctions.Add() {{{2
function! s:cvsFunctions.Add(argList)
  return s:DoCommand('add', 'add', '')
endfunction

" Function: s:cvsFunctions.Annotate(argList) {{{2
function! s:cvsFunctions.Annotate(argList)
  if len(a:argList) == 0
    if &filetype == 'CVSAnnotate'
      " This is a CVSAnnotate buffer.  Perform annotation of the version
      " indicated by the current line.
      let revision = matchstr(getline('.'),'\v%(^[0-9.]+)')
    else
      let revision=VCSCommandGetRevision()
      if revision == ''
        throw 'Unable to obtain version information.'
      elseif revision == 'Unknown'
        throw 'File not under source control'
      elseif revision == 'New'
        throw 'No annotatation available for new file.'
      endif
    endif
  else
    let revision=a:argList[0]
  endif

  if revision == 'New'
    throw 'No annotatation available for new file.'
  endif

  let resultBuffer=s:DoCommand('-q annotate -r ' . revision, 'annotate', revision) 
  if resultBuffer > 0
    set filetype=CVSAnnotate
    " Remove header lines from standard error
    silent v/^\d\+\%(\.\d\+\)\+/d
  endif
  return resultBuffer
endfunction

" Function: s:cvsFunctions.Commit(argList) {{{2
function! s:cvsFunctions.Commit(argList)
  let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '')
  if resultBuffer == 0
    echomsg 'No commit needed.'
  endif
  return resultBuffer
endfunction

" Function: s:cvsFunctions.Diff(argList) {{{2
function! s:cvsFunctions.Diff(argList)
  if len(a:argList) == 1
    let revOptions = '-r ' . a:argList[0]
    let caption = '(' . a:argList[0] . ' : current)'
  elseif len(a:argList) == 2
    let revOptions = '-r ' . a:argList[0] . ' -r ' . a:argList[1]
    let caption = '(' . a:argList[0] . ' : ' . a:argList[1] . ')'
  else
    let revOptions = ''
    let caption = ''
  endif

  let cvsdiffopt=VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')

  if cvsdiffopt == ''
    let diffoptionstring=''
  else
    let diffoptionstring=' -' . cvsdiffopt . ' '
  endif

  let resultBuffer = s:DoCommand('diff ' . diffoptionstring . revOptions , 'diff', caption)
  if resultBuffer > 0
    set filetype=diff
  else
    echomsg 'No differences found'
  endif
  return resultBuffer
endfunction

" Function: s:cvsFunctions.GetBufferInfo() {{{2
" Provides version control details for the current file.  Current version
" number and current repository version number are required to be returned by
" the vcscommand plugin.  This CVS extension adds branch name to the return
" list as well.
" Returns: List of results:  [revision, repository, branch]

function! s:cvsFunctions.GetBufferInfo()
  let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
  let fileName = bufname(originalBuffer)
  let realFileName = fnamemodify(resolve(fileName), ':t')
  if !filereadable(fileName)
    return ['Unknown']
  endif
  let oldCwd=VCSCommandChangeToCurrentFileDir(fileName)
  try
    let statusText=system(VCSCommandGetOption('VCSCommandCVSExec', 'cvs') . ' status "' . realFileName . '"')
    if(v:shell_error)
      return []
    endif
    let revision=substitute(statusText, '^\_.*Working revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\)\_.*$', '\1', '')

    " We can still be in a CVS-controlled directory without this being a CVS
    " file
    if match(revision, '^New file!$') >= 0 
      let revision='New'
    elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
      return ['Unknown']
    endif

    let branch=substitute(statusText, '^\_.*Sticky Tag:\s\+\(\d\+\%(\.\d\+\)\+\|\a[A-Za-z0-9-_]*\|(none)\).*$', '\1', '')
    let repository=substitute(statusText, '^\_.*Repository revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\|No revision control file\)\_.*$', '\1', '')
    let repository=substitute(repository, '^New file!\|No revision control file$', 'New', '')
    return [revision, repository, branch]
  finally
    execute 'cd' escape(oldCwd, ' ')
  endtry
endfunction

" Function: s:cvsFunctions.Log() {{{2
function! s:cvsFunctions.Log(argList)
  if len(a:argList) == 0
    let versionOption = ''
    let caption = ''
  else
    let versionOption=' -r' . a:argList[0]
    let caption = a:argList[0]
  endif

  let resultBuffer=s:DoCommand('log' . versionOption, 'log', caption)
  if resultBuffer > 0
    set filetype=rcslog
  endif
  return resultBuffer
endfunction

" Function: s:cvsFunctions.Revert(argList) {{{2
function! s:cvsFunctions.Revert(argList)
  return s:DoCommand('update -C', 'revert', '')
endfunction

" Function: s:cvsFunctions.Review(argList) {{{2
function! s:cvsFunctions.Review(argList)
  if len(a:argList) == 0
    let versiontag = '(current)'
    let versionOption = ''
  else
    let versiontag = a:argList[0]
    let versionOption = ' -r ' . versiontag . ' '
  endif

  let resultBuffer = s:DoCommand('-q update -p' . versionOption, 'review', versiontag)
  if resultBuffer > 0
    let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
  endif
  return resultBuffer
endfunction

" Function: s:cvsFunctions.Status(argList) {{{2
function! s:cvsFunctions.Status(argList)
  return s:DoCommand('status', 'status', '')
endfunction

" Function: s:cvsFunctions.Update(argList) {{{2
function! s:cvsFunctions.Update(argList)
  return s:DoCommand('update', 'update', '')
endfunction

" Section: CVS-specific functions {{{1

" Function: s:CVSEdit() {{{2
function! s:CVSEdit()
  return s:DoCommand('edit', 'cvsedit', '')
endfunction

" Function: s:CVSEditors() {{{2
function! s:CVSEditors()
  return s:DoCommand('editors', 'cvseditors', '')
endfunction

" Function: s:CVSUnedit() {{{2
function! s:CVSUnedit()
  return s:DoCommand('unedit', 'cvsunedit', '')
endfunction

" Function: s:CVSWatch(onoff) {{{2
function! s:CVSWatch(onoff)
  if a:onoff !~ '^\c\%(on\|off\|add\|remove\)$'
    echoerr 'Argument to CVSWatch must be one of [on|off|add|remove]'
    return -1
  end
  return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '')
endfunction

" Function: s:CVSWatchers() {{{2
function! s:CVSWatchers()
  return s:DoCommand('watchers', 'cvswatchers', '')
endfunction

" Section: Command definitions {{{1
" Section: Primary commands {{{2
com! CVSEdit call s:CVSEdit()
com! CVSEditors call s:CVSEditors()
com! CVSUnedit call s:CVSUnedit()
com! -nargs=1 CVSWatch call s:CVSWatch(<f-args>)
com! CVSWatchAdd call s:CVSWatch('add')
com! CVSWatchOn call s:CVSWatch('on')
com! CVSWatchOff call s:CVSWatch('off')
com! CVSWatchRemove call s:CVSWatch('remove')
com! CVSWatchers call s:CVSWatchers()

" Section: Plugin command mappings {{{1

let s:cvsExtensionMappings = {}
let mappingInfo = [
      \['CVSEdit', 'CVSEdit', 'ce'],
      \['CVSEditors', 'CVSEditors', 'ci'],
      \['CVSUnedit', 'CVSUnedit', 'ct'],
      \['CVSWatchers', 'CVSWatchers', 'cwv'],
      \['CVSWatchAdd', 'CVSWatch add', 'cwa'],
      \['CVSWatchOff', 'CVSWatch off', 'cwf'],
      \['CVSWatchOn', 'CVSWatch on', 'cwn'],
      \['CVSWatchRemove', 'CVSWatch remove', 'cwr']
      \]

for [pluginName, commandText, shortCut] in mappingInfo
  execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
  if !hasmapto('<Plug>' . pluginName)
    let s:cvsExtensionMappings[shortCut] = commandText
  endif
endfor

" Section: Menu items {{{1
silent! aunmenu Plugin.VCS.CVS
amenu <silent> &Plugin.VCS.CVS.&Edit       <Plug>CVSEdit
amenu <silent> &Plugin.VCS.CVS.Ed&itors    <Plug>CVSEditors
amenu <silent> &Plugin.VCS.CVS.Unedi&t     <Plug>CVSUnedit
amenu <silent> &Plugin.VCS.CVS.&Watchers   <Plug>CVSWatchers
amenu <silent> &Plugin.VCS.CVS.WatchAdd    <Plug>CVSWatchAdd
amenu <silent> &Plugin.VCS.CVS.WatchOn     <Plug>CVSWatchOn
amenu <silent> &Plugin.VCS.CVS.WatchOff    <Plug>CVSWatchOff
amenu <silent> &Plugin.VCS.CVS.WatchRemove <Plug>CVSWatchRemove

" Section: Plugin Registration {{{1
" If the vcscommand.vim plugin hasn't loaded, delay registration until it
" loads.
if exists('g:loaded_VCSCommand')
  call VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
else
  augroup VCSCommand
    au User VCSLoadExtensions call VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
  augroup END
endif