Force nocompatible for plugins.
[vcscommand.git] / plugin / vcscommand.vim
blob281e373f1c15121bae7967defe43000aba4b41e8
1 " vim600: set foldmethod=marker:
3 " Vim plugin to assist in working with files under control of CVS or SVN.
5 " Version:       Beta 18
6 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
7 " License:
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
26 " IN THE SOFTWARE.
28 " Section: Documentation {{{1
30 " Provides functions to invoke various source control commands on the current
31 " file (either the current buffer, or, in the case of an directory buffer, the
32 " directory and all subdirectories associated with the current buffer).  The
33 " output of the commands is captured in a new scratch window.
35 " This plugin needs additional extension plugins, each  specific to a source
36 " control system, to function.  Those plugins should be placed in a
37 " subdirectory of the standard plugin directory named 'vcscommand'.  Several
38 " options include the name of the version control system in the option name.
39 " Such options use the placeholder text '{VCSType}', which would be replaced
40 " in actual usage with 'CVS' or 'SVN', for instance.
42 " Command documentation {{{2
44 " VCSAdd           Adds the current file to source control.
46 " VCSAnnotate      Displays the current file with each line annotated with the
47 "                  version in which it was most recently changed.  If an
48 "                  argument is given, the argument is used as a revision
49 "                  number to display.  If not given an argument, it uses the
50 "                  most recent version of the file on the current branch.
51 "                  Additionally, if the current buffer is a VCSAnnotate buffer
52 "                  already, the version number on the current line is used.
54 " VCSCommit[!]     Commits changes to the current file to source control.
56 "                  If called with arguments, the arguments are the log message.
58 "                  If '!' is used, an empty log message is committed.
60 "                  If called with no arguments, this is a two-step command.
61 "                  The first step opens a buffer to accept a log message.
62 "                  When that buffer is written, it is automatically closed and
63 "                  the file is committed using the information from that log
64 "                  message.  The commit can be abandoned if the log message
65 "                  buffer is deleted or wiped before being written.
67 " VCSDelete        Deletes the current file and removes it from source control.
69 " VCSDiff          With no arguments, this displays the differences between
70 "                  the current file and its parent version under source
71 "                  control in a new scratch buffer.
73 "                  With one argument, the diff is performed on the
74 "                  current file against the specified revision.
76 "                  With two arguments, the diff is performed between the
77 "                  specified revisions of the current file.
79 "                  This command uses the 'VCSCommand{VCSType}DiffOpt' variable
80 "                  to specify diff options.  If that variable does not exist,
81 "                  a plugin-specific default is used.  If you wish to have no
82 "                  options, then set it to the empty string.
84 " VCSGotoOriginal  Jumps to the source buffer if the current buffer is a VCS
85 "                  scratch buffer.  If VCSGotoOriginal[!] is used, remove all
86 "                  VCS scratch buffers associated with the original file.
88 " VCSInfo          Displays extended information about the current file in a
89 "                  new scratch buffer. 
91 " VCSLock          Locks the current file in order to prevent other users from
92 "                  concurrently modifying it.  The exact semantics of this
93 "                  command depend on the underlying VCS.
95 " VCSLog           Displays the version history of the current file in a new
96 "                  scratch buffer.
98 " VCSRevert        Replaces the modified version of the current file with the
99 "                  most recent version from the repository.
101 " VCSReview        Displays a particular version of the current file in a new
102 "                  scratch buffer.  If no argument is given, the most recent
103 "                  version of the file on the current branch is retrieved.
105 " VCSStatus        Displays versioning information about the current file in a
106 "                  new scratch buffer.
108 " VCSUnlock        Unlocks the current file in order to allow other users from
109 "                  concurrently modifying it.  The exact semantics of this
110 "                  command depend on the underlying VCS.
112 " VCSUpdate        Updates the current file with any relevant changes from the
113 "                  repository.
115 " VCSVimDiff       Uses vimdiff to display differences between versions of the
116 "                  current file.
118 "                  If no revision is specified, the most recent version of the
119 "                  file on the current branch is used.  With one argument,
120 "                  that argument is used as the revision as above.  With two
121 "                  arguments, the differences between the two revisions is
122 "                  displayed using vimdiff.
124 "                  With either zero or one argument, the original buffer is
125 "                  used to perform the vimdiff.  When the scratch buffer is
126 "                  closed, the original buffer will be returned to normal
127 "                  mode.
129 "                  Once vimdiff mode is started using the above methods,
130 "                  additional vimdiff buffers may be added by passing a single
131 "                  version argument to the command.  There may be up to 4
132 "                  vimdiff buffers total.
134 "                  Using the 2-argument form of the command resets the vimdiff
135 "                  to only those 2 versions.  Additionally, invoking the
136 "                  command on a different file will close the previous vimdiff
137 "                  buffers.
139 " Mapping documentation: {{{2
141 " By default, a mapping is defined for each command.  User-provided mappings
142 " can be used instead by mapping to <Plug>CommandName, for instance:
144 " nmap ,ca <Plug>VCSAdd
146 " The default mappings are as follow:
148 "   <Leader>ca VCSAdd
149 "   <Leader>cn VCSAnnotate
150 "   <Leader>cc VCSCommit
151 "   <Leader>cD VCSDelete
152 "   <Leader>cd VCSDiff
153 "   <Leader>cg VCSGotoOriginal
154 "   <Leader>cG VCSGotoOriginal!
155 "   <Leader>ci VCSInfo
156 "   <Leader>cl VCSLog
157 "   <Leader>cL VCSLock
158 "   <Leader>cr VCSReview
159 "   <Leader>cs VCSStatus
160 "   <Leader>cu VCSUpdate
161 "   <Leader>cU VCSUnlock
162 "   <Leader>cv VCSVimDiff
164 " Options documentation: {{{2
166 " Several variables are checked by the script to determine behavior as follow:
168 " VCSCommandCommitOnWrite
169 "   This variable, if set to a non-zero value, causes the pending commit to
170 "   take place immediately as soon as the log message buffer is written.  If
171 "   set to zero, only the VCSCommit mapping will cause the pending commit to
172 "   occur.  If not set, it defaults to 1.
174 " VCSCommandDeleteOnHide
175 "   This variable, if set to a non-zero value, causes the temporary VCS result
176 "   buffers to automatically delete themselves when hidden.
178 " VCSCommand{VCSType}DiffOpt
179 "   This variable, if set, determines the options passed to the diff command
180 "   of the underlying VCS.  Each VCS plugin defines a default value.
182 " VCSCommandDiffSplit
183 "   This variable overrides the VCSCommandSplit variable, but only for buffers
184 "   created with VCSVimDiff.
186 " VCSCommandDisableMappings
187 "   This variable, if set to a non-zero value, prevents the default command
188 "   mappings from being set.
190 " VCSCommandDisableExtensionMappings
191 "   This variable, if set to a non-zero value, prevents the default command
192 "   mappings from being set for commands specific to an individual VCS.
194 " VCSCommandEdit
195 "   This variable controls whether to split the current window to display a
196 "   scratch buffer ('split'), or to display it in the current buffer ('edit').
197 "   If not set, it defaults to 'split'.
199 " VCSCommandEnableBufferSetup
200 "   This variable, if set to a non-zero value, activates VCS buffer management
201 "   mode.  This mode means that the buffer variable 'VCSRevision' is set if
202 "   the file is VCS-controlled.  This is useful for displaying version
203 "   information in the status bar.  Additional options may be set by
204 "   individual VCS plugins.
206 " VCSCommandResultBufferNameExtension
207 "   This variable, if set to a non-blank value, is appended to the name of the
208 "   VCS command output buffers.  For example, '.vcs'.  Using this option may
209 "   help avoid problems caused by autocommands dependent on file extension.
211 " VCSCommandResultBufferNameFunction
212 "   This variable, if set, specifies a custom function for naming VCS command
213 "   output buffers.  This function will be passed the following arguments:
215 "   command - name of the VCS command being executed (such as 'Log' or
216 "   'Diff').
218 "   originalBuffer - buffer number of the source file.
220 "   vcsType - type of VCS controlling this file (such as 'CVS' or 'SVN').
222 "   statusText - extra text associated with the VCS action (such as version
223 "   numbers).
225 " VCSCommandSplit
226 "   This variable controls the orientation of the various window splits that
227 "   may occur (such as with VCSVimDiff, when using a VCS command on a VCS
228 "   command buffer, or when the 'VCSCommandEdit' variable is set to 'split'.
229 "   If set to 'horizontal', the resulting windows will be on stacked on top of
230 "   one another.  If set to 'vertical', the resulting windows will be
231 "   side-by-side.  If not set, it defaults to 'horizontal' for all but
232 "   VCSVimDiff windows.
234 " Event documentation {{{2
235 "   For additional customization, VCSCommand.vim uses User event autocommand
236 "   hooks.  Each event is in the VCSCommand group, and different patterns
237 "   match the various hooks.
239 "   For instance, the following could be added to the vimrc to provide a 'q'
240 "   mapping to quit a VCS scratch buffer:
242 "   augroup VCSCommand
243 "     au VCSCommand User VCSBufferCreated silent! nmap <unique> <buffer> q :bwipeout<cr> 
244 "   augroup END
246 "   The following hooks are available:
248 "   VCSBufferCreated           This event is fired just after a VCS command
249 "                              output buffer is created.  It is executed
250 "                              within the context of the new buffer.
252 "   VCSBufferSetup             This event is fired just after VCS buffer setup
253 "                              occurs, if enabled.
255 "   VCSPluginInit              This event is fired when the VCSCommand plugin
256 "                              first loads.
258 "   VCSPluginFinish            This event is fired just after the VCSCommand
259 "                              plugin loads.
261 "   VCSVimDiffFinish           This event is fired just after the VCSVimDiff
262 "                              command executes to allow customization of,
263 "                              for instance, window placement and focus.
265 " Section: Plugin header {{{1
267 " loaded_VCSCommand is set to 1 when the initialization begins, and 2 when it
268 " completes.  This allows various actions to only be taken by functions after
269 " system initialization.
271 if exists('loaded_VCSCommand')
272    finish
273 endif
274 let loaded_VCSCommand = 1
276 if v:version < 700
277   echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
278   finish
279 endif
281 let s:save_cpo=&cpo
282 set cpo&vim
284 " Section: Event group setup {{{1
286 augroup VCSCommand
287 augroup END
289 augroup VCSCommandCommit
290 augroup END
292 " Section: Plugin initialization {{{1
293 silent do VCSCommand User VCSPluginInit
295 " Section: Script variable initialization {{{1
297 let s:plugins = {}
298 let s:pluginFiles = []
299 let s:extendedMappings = {}
300 let s:optionOverrides = {}
301 let s:isEditFileRunning = 0
303 unlet! s:vimDiffRestoreCmd
304 unlet! s:vimDiffSourceBuffer
305 unlet! s:vimDiffScratchList
307 " Section: Utility functions {{{1
309 " Function: s:ReportError(mapping) {{{2
310 " Displays the given error in a consistent faction.  This is intended to be
311 " invoked from a catch statement.
313 function! s:ReportError(error)
314   echohl WarningMsg|echomsg 'VCSCommand:  ' . a:error|echohl None
315 endfunction
317 " Function: s:ExecuteExtensionMapping(mapping) {{{2
318 " Invokes the appropriate extension mapping depending on the type of the
319 " current buffer.
321 function! s:ExecuteExtensionMapping(mapping)
322   let buffer = bufnr('%')
323   let vcsType = VCSCommandGetVCSType(buffer)
324   if !has_key(s:extendedMappings, vcsType)
325     throw 'Unknown VCS type:  ' . vcsType
326   endif
327   if !has_key(s:extendedMappings[vcsType], a:mapping)
328     throw 'This extended mapping is not defined for ' . vcsType
329   endif
330   silent execute 'normal' ':' .  s:extendedMappings[vcsType][a:mapping] . "\<CR>"
331 endfunction
333 " Function: s:ExecuteVCSCommand(command, argList) {{{2
334 " Calls the indicated plugin-specific VCS command on the current buffer.
335 " Returns: buffer number of resulting output scratch buffer, or -1 if an error
336 " occurs.
338 function! s:ExecuteVCSCommand(command, argList, verifyBuffer)
339   try
340     let buffer = bufnr('%')
342     let vcsType = VCSCommandGetVCSType(buffer)
343     if !has_key(s:plugins, vcsType)
344       throw 'Unknown VCS type:  ' . vcsType
345     endif
347     let originalBuffer = VCSCommandGetOriginalBuffer(buffer)
348     let bufferName = bufname(originalBuffer)
350     " It is already known that the directory is under VCS control.  No further
351     " checks are needed.  Otherwise, perform some basic sanity checks to avoid
352     " VCS-specific error messages from confusing things.
353     if !isdirectory(bufferName)
354       if !filereadable(bufferName)
355         throw 'No such file ' . bufferName
356       endif
357       if(a:verifyBuffer)
358         if isdirectory(bufferName)
359         endif
360         let revision = VCSCommandGetRevision()
361         if revision == ''
362           throw 'Unable to obtain version information.'
363         elseif revision == 'Unknown'
364           throw 'Item not under source control'
365         elseif revision == 'New'
366           throw 'Operation not available on newly-added item.'
367         endif
368       endif
369     endif
371     let functionMap = s:plugins[vcsType]
372     if !has_key(functionMap, a:command)
373       throw 'Command ''' . a:command . ''' not implemented for ' . vcsType
374     endif
375     return functionMap[a:command](a:argList)
376   catch
377     call s:ReportError(v:exception)
378     return -1
379   endtry
380 endfunction
382 " Function: s:GenerateResultBufferName(command, originalBuffer, vcsType, statusText) {{{2
383 " Default method of generating the name for VCS result buffers.  This can be
384 " overridden with the VCSResultBufferNameFunction variable.
386 function! s:GenerateResultBufferName(command, originalBuffer, vcsType, statusText)
387   let fileName = bufname(a:originalBuffer)
388   let bufferName = a:vcsType . ' ' . a:command
389   if strlen(a:statusText) > 0
390     let bufferName .= ' ' . a:statusText
391   endif
392   let bufferName .= ' ' . fileName
393   let counter = 0
394   let versionedBufferName = bufferName
395   while buflisted(versionedBufferName)
396     let counter += 1
397     let versionedBufferName = bufferName . ' (' . counter . ')'
398   endwhile
399   return versionedBufferName
400 endfunction
402 " Function: s:GenerateResultBufferNameWithExtension(command, originalBuffer, vcsType, statusText) {{{2
403 " Method of generating the name for VCS result buffers that uses the original
404 " file name with the VCS type and command appended as extensions.
406 function! s:GenerateResultBufferNameWithExtension(command, originalBuffer, vcsType, statusText)
407   let fileName = bufname(a:originalBuffer)
408   let bufferName = a:vcsType . ' ' . a:command
409   if strlen(a:statusText) > 0
410     let bufferName .= ' ' . a:statusText
411   endif
412   let bufferName .= ' ' . fileName . VCSCommandGetOption('VCSCommandResultBufferNameExtension', '.vcs')
413   let counter = 0
414   let versionedBufferName = bufferName
415   while buflisted(versionedBufferName)
416     let counter += 1
417     let versionedBufferName = '(' . counter . ') ' . bufferName
418   endwhile
419   return versionedBufferName
420 endfunction
422 " Function: s:EditFile(command, originalBuffer, statusText) {{{2
423 " Creates a new buffer of the given name and associates it with the given
424 " original buffer.
426 function! s:EditFile(command, originalBuffer, statusText)
427   let vcsType = getbufvar(a:originalBuffer, 'VCSCommandVCSType')
429   let nameExtension = VCSCommandGetOption('VCSCommandResultBufferNameExtension', '')
430   if nameExtension == ''
431     let nameFunction = VCSCommandGetOption('VCSCommandResultBufferNameFunction', 's:GenerateResultBufferName')
432   else
433     let nameFunction = VCSCommandGetOption('VCSCommandResultBufferNameFunction', 's:GenerateResultBufferNameWithExtension')
434   endif
436   let resultBufferName = call(nameFunction, [a:command, a:originalBuffer, vcsType, a:statusText])
438   " Protect against useless buffer set-up
439   let s:isEditFileRunning += 1
440   try
441     let editCommand = VCSCommandGetOption('VCSCommandEdit', 'split')
442     if editCommand == 'split'
443       if VCSCommandGetOption('VCSCommandSplit', 'horizontal') == 'horizontal'
444         rightbelow split
445       else
446         vert rightbelow split
447       endif
448     endif
449     edit `=resultBufferName`
450     let b:VCSCommandCommand = a:command
451     let b:VCSCommandOriginalBuffer = a:originalBuffer
452     let b:VCSCommandSourceFile = bufname(a:originalBuffer)
453     let b:VCSCommandVCSType = vcsType
455     set buftype=nofile
456     set noswapfile
457     let &filetype = vcsType . a:command
459     if a:statusText != ''
460       let b:VCSCommandStatusText = a:statusText
461     endif
463     if VCSCommandGetOption('VCSCommandDeleteOnHide', 0)
464       set bufhidden=delete
465     endif
466   finally
467     let s:isEditFileRunning -= 1
468   endtry
470 endfunction
472 " Function: s:SetupBuffer() {{{2
473 " Attempts to set the b:VCSCommandBufferInfo variable
475 function! s:SetupBuffer()
476   if (exists('b:VCSCommandBufferSetup') && b:VCSCommandBufferSetup)
477     " This buffer is already set up.
478     return
479   endif
481   if !isdirectory(@%) && (strlen(&buftype) > 0 || !filereadable(@%))
482     " No special status for special buffers other than directory buffers.
483     return
484   endif
486   if !VCSCommandGetOption('VCSCommandEnableBufferSetup', 0) || s:isEditFileRunning > 0
487     unlet! b:VCSCommandBufferSetup
488     return
489   endif
491   try
492     let vcsType = VCSCommandGetVCSType(bufnr('%'))
493     let b:VCSCommandBufferInfo =  s:plugins[vcsType].GetBufferInfo()
494     silent do VCSCommand User VCSBufferSetup
495   catch /No suitable plugin/
496     " This is not a VCS-controlled file.
497     let b:VCSCommandBufferInfo = []
498   endtry
500   let b:VCSCommandBufferSetup = 1
501 endfunction
503 " Function: s:MarkOrigBufferForSetup(buffer) {{{2
504 " Resets the buffer setup state of the original buffer for a given VCS scratch
505 " buffer.
506 " Returns:  The VCS buffer number in a passthrough mode.
508 function! s:MarkOrigBufferForSetup(buffer)
509   checktime
510   if a:buffer > 0 
511     let origBuffer = VCSCommandGetOriginalBuffer(a:buffer)
512     " This should never not work, but I'm paranoid
513     if origBuffer != a:buffer
514       call setbufvar(origBuffer, 'VCSCommandBufferSetup', 0)
515     endif
516   endif
517   return a:buffer
518 endfunction
520 " Function: s:OverrideOption(option, [value]) {{{2
521 " Provides a temporary override for the given VCS option.  If no value is
522 " passed, the override is disabled.
524 function! s:OverrideOption(option, ...)
525   if a:0 == 0
526     call remove(s:optionOverrides[a:option], -1)
527   else
528     if !has_key(s:optionOverrides, a:option)
529       let s:optionOverrides[a:option] = []
530     endif
531     call add(s:optionOverrides[a:option], a:1)
532   endif
533 endfunction
535 " Function: s:WipeoutCommandBuffers() {{{2
536 " Clears all current VCS output buffers of the specified type for a given source.
538 function! s:WipeoutCommandBuffers(originalBuffer, VCSCommand)
539   let buffer = 1
540   while buffer <= bufnr('$')
541     if getbufvar(buffer, 'VCSCommandOriginalBuffer') == a:originalBuffer
542       if getbufvar(buffer, 'VCSCommandCommand') == a:VCSCommand
543         execute 'bw' buffer
544       endif
545     endif
546     let buffer = buffer + 1
547   endwhile
548 endfunction
550 " Function: s:VimDiffRestore(vimDiffBuff) {{{2
551 " Checks whether the given buffer is one whose deletion should trigger
552 " restoration of an original buffer after it was diffed.  If so, it executes
553 " the appropriate setting command stored with that original buffer.
555 function! s:VimDiffRestore(vimDiffBuff)
556   let s:isEditFileRunning += 1
557   try
558     if exists('s:vimDiffSourceBuffer')
559       if a:vimDiffBuff == s:vimDiffSourceBuffer
560         " Original file is being removed.
561         unlet! s:vimDiffSourceBuffer
562         unlet! s:vimDiffRestoreCmd
563         unlet! s:vimDiffScratchList
564       else
565         let index = index(s:vimDiffScratchList, a:vimDiffBuff)
566         if index >= 0
567           call remove(s:vimDiffScratchList, index)
568           if len(s:vimDiffScratchList) == 0
569             if exists('s:vimDiffRestoreCmd')
570               " All scratch buffers are gone, reset the original.
571               " Only restore if the source buffer is still in Diff mode
573               let sourceWinNR = bufwinnr(s:vimDiffSourceBuffer)
574               if sourceWinNR != -1
575                 " The buffer is visible in at least one window
576                 let currentWinNR = winnr()
577                 while winbufnr(sourceWinNR) != -1
578                   if winbufnr(sourceWinNR) == s:vimDiffSourceBuffer
579                     execute sourceWinNR . 'wincmd w'
580                     if getwinvar(0, '&diff')
581                       execute s:vimDiffRestoreCmd
582                     endif
583                   endif
584                   let sourceWinNR = sourceWinNR + 1
585                 endwhile
586                 execute currentWinNR . 'wincmd w'
587               else
588                 " The buffer is hidden.  It must be visible in order to set the
589                 " diff option.
590                 let currentBufNR = bufnr('')
591                 execute 'hide buffer' s:vimDiffSourceBuffer
592                 if getwinvar(0, '&diff')
593                   execute s:vimDiffRestoreCmd
594                 endif
595                 execute 'hide buffer' currentBufNR
596               endif
598               unlet s:vimDiffRestoreCmd
599             endif 
600             " All buffers are gone.
601             unlet s:vimDiffSourceBuffer
602             unlet s:vimDiffScratchList
603           endif
604         endif
605       endif
606     endif
607   finally
608     let s:isEditFileRunning -= 1
609   endtry
610 endfunction
612 " Section: Generic VCS command functions {{{1
614 " Function: s:VCSCommit() {{{2
615 function! s:VCSCommit(bang, message)
616   try
617     let vcsType = VCSCommandGetVCSType(bufnr('%'))
618     if !has_key(s:plugins, vcsType)
619       throw 'Unknown VCS type:  ' . vcsType
620     endif
622     let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
624     " Handle the commit message being specified.  If a message is supplied, it
625     " is used; if bang is supplied, an empty message is used; otherwise, the
626     " user is provided a buffer from which to edit the commit message.
628     if strlen(a:message) > 0 || a:bang == '!'
629       return s:VCSFinishCommit([a:message], originalBuffer)
630     endif
632     call s:EditFile('commitlog', originalBuffer, '')
633     set ft=vcscommit
635     " Create a commit mapping.
637     nnoremap <silent> <buffer> <Plug>VCSCommit :call <SID>VCSFinishCommitWithBuffer()<CR>
639     silent 0put ='VCS: ----------------------------------------------------------------------'
640     silent put ='VCS: Please enter log message.  Lines beginning with ''VCS:'' are removed automatically.'
641     silent put ='VCS: To finish the commit, Type <leader>cc (or your own <Plug>VCSCommit mapping)'
643     if VCSCommandGetOption('VCSCommandCommitOnWrite', 1) == 1
644       set buftype=acwrite
645       au VCSCommandCommit BufWriteCmd <buffer> call s:VCSFinishCommitWithBuffer()
646       silent put ='VCS: or write this buffer'
647     endif
649     silent put ='VCS: ----------------------------------------------------------------------'
650     $
651     set nomodified
652   catch
653     call s:ReportError(v:exception)
654     return -1
655   endtry
656 endfunction
658 " Function: s:VCSFinishCommitWithBuffer() {{{2
659 " Wrapper for s:VCSFinishCommit which is called only from a commit log buffer
660 " which removes all lines starting with 'VCS:'.
662 function! s:VCSFinishCommitWithBuffer()
663   set nomodified
664   let currentBuffer = bufnr('%') 
665   let logMessageList = getbufline('%', 1, '$')
666   call filter(logMessageList, 'v:val !~ ''^\s*VCS:''')
667   let resultBuffer = s:VCSFinishCommit(logMessageList, b:VCSCommandOriginalBuffer)
668   if resultBuffer >= 0
669     execute 'bw' currentBuffer
670   endif
671   return resultBuffer
672 endfunction
674 " Function: s:VCSFinishCommit(logMessageList, originalBuffer) {{{2
675 function! s:VCSFinishCommit(logMessageList, originalBuffer)
676   let shellSlashBak = &shellslash
677   try
678     set shellslash
679     let messageFileName = tempname()
680     call writefile(a:logMessageList, messageFileName)
681     try
682       let resultBuffer = s:ExecuteVCSCommand('Commit', [messageFileName], 0)
683       if resultBuffer < 0
684         return resultBuffer
685       endif
686       return s:MarkOrigBufferForSetup(resultBuffer)
687     finally
688       call delete(messageFileName)
689     endtry
690   finally
691     let &shellslash = shellSlashBak
692   endtry
693 endfunction
695 " Function: s:VCSGotoOriginal(bang) {{{2
696 function! s:VCSGotoOriginal(bang)
697   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
698   if originalBuffer > 0
699     let origWinNR = bufwinnr(originalBuffer)
700     if origWinNR == -1
701       execute 'buffer' originalBuffer
702     else
703       execute origWinNR . 'wincmd w'
704     endif
705     if a:bang == '!'
706       let buffnr = 1
707       let buffmaxnr = bufnr('$')
708       while buffnr <= buffmaxnr
709         if getbufvar(buffnr, 'VCSCommandOriginalBuffer') == originalBuffer
710           execute 'bw' buffnr
711         endif
712         let buffnr = buffnr + 1
713       endwhile
714     endif
715   endif
716 endfunction
718 " Function: s:VCSVimDiff(...) {{{2
719 function! s:VCSVimDiff(...)
720   try
721     let vcsType = VCSCommandGetVCSType(bufnr('%'))
722     if !has_key(s:plugins, vcsType)
723       throw 'Unknown VCS type:  ' . vcsType
724     endif
725     let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
726     let s:isEditFileRunning = s:isEditFileRunning + 1
727     try
728       " If there's already a VimDiff'ed window, restore it.
729       " There may only be one VCSVimDiff original window at a time.
731       if exists('s:vimDiffSourceBuffer') && s:vimDiffSourceBuffer != originalBuffer
732         " Clear the existing vimdiff setup by removing the result buffers.
733         call s:WipeoutCommandBuffers(s:vimDiffSourceBuffer, 'vimdiff')
734       endif
736       " Split and diff
737       if(a:0 == 2)
738         " Reset the vimdiff system, as 2 explicit versions were provided.
739         if exists('s:vimDiffSourceBuffer')
740           call s:WipeoutCommandBuffers(s:vimDiffSourceBuffer, 'vimdiff')
741         endif
742         let resultBuffer = s:plugins[vcsType].Review([a:1])
743         if resultBuffer < 0
744           echomsg 'Can''t open revision ' . a:1
745           return resultBuffer
746         endif
747         let b:VCSCommandCommand = 'vimdiff'
748         diffthis
749         let s:vimDiffScratchList = [resultBuffer]
750         " If no split method is defined, cheat, and set it to vertical.
751         try
752           call s:OverrideOption('VCSCommandSplit', VCSCommandGetOption('VCSCommandDiffSplit', VCSCommandGetOption('VCSCommandSplit', 'vertical')))
753           let resultBuffer = s:plugins[vcsType].Review([a:2])
754         finally
755           call s:OverrideOption('VCSCommandSplit')
756         endtry
757         if resultBuffer < 0
758           echomsg 'Can''t open revision ' . a:1
759           return resultBuffer
760         endif
761         let b:VCSCommandCommand = 'vimdiff'
762         diffthis
763         let s:vimDiffScratchList += [resultBuffer]
764       else
765         " Add new buffer
766         call s:OverrideOption('VCSCommandEdit', 'split')
767         try
768           " Force splitting behavior, otherwise why use vimdiff?
769           call s:OverrideOption('VCSCommandSplit', VCSCommandGetOption('VCSCommandDiffSplit', VCSCommandGetOption('VCSCommandSplit', 'vertical')))
770           try
771             if(a:0 == 0)
772               let resultBuffer = s:plugins[vcsType].Review([])
773             else
774               let resultBuffer = s:plugins[vcsType].Review([a:1])
775             endif
776           finally
777             call s:OverrideOption('VCSCommandSplit')
778           endtry
779         finally
780           call s:OverrideOption('VCSCommandEdit')
781         endtry
782         if resultBuffer < 0
783           echomsg 'Can''t open current revision'
784           return resultBuffer
785         endif
786         let b:VCSCommandCommand = 'vimdiff'
787         diffthis
789         if !exists('s:vimDiffSourceBuffer')
790           " New instance of vimdiff.
791           let s:vimDiffScratchList = [resultBuffer]
793           " This could have been invoked on a VCS result buffer, not the
794           " original buffer.
795           wincmd W
796           execute 'buffer' originalBuffer
797           " Store info for later original buffer restore
798           let s:vimDiffRestoreCmd = 
799                 \    'call setbufvar('.originalBuffer.', ''&diff'', '.getbufvar(originalBuffer, '&diff').')'
800                 \ . '|call setbufvar('.originalBuffer.', ''&foldcolumn'', '.getbufvar(originalBuffer, '&foldcolumn').')'
801                 \ . '|call setbufvar('.originalBuffer.', ''&foldenable'', '.getbufvar(originalBuffer, '&foldenable').')'
802                 \ . '|call setbufvar('.originalBuffer.', ''&foldmethod'', '''.getbufvar(originalBuffer, '&foldmethod').''')'
803                 \ . '|call setbufvar('.originalBuffer.', ''&scrollbind'', '.getbufvar(originalBuffer, '&scrollbind').')'
804                 \ . '|call setbufvar('.originalBuffer.', ''&wrap'', '.getbufvar(originalBuffer, '&wrap').')'
805                 \ . '|if &foldmethod==''manual''|execute ''normal zE''|endif'
806           diffthis
807           wincmd w
808         else
809           " Adding a window to an existing vimdiff
810           let s:vimDiffScratchList += [resultBuffer]
811         endif
812       endif
814       let s:vimDiffSourceBuffer = originalBuffer
816       " Avoid executing the modeline in the current buffer after the autocommand.
818       let currentBuffer = bufnr('%')
819       let saveModeline = getbufvar(currentBuffer, '&modeline')
820       try
821         call setbufvar(currentBuffer, '&modeline', 0)
822         silent do VCSCommand User VCSVimDiffFinish
823       finally
824         call setbufvar(currentBuffer, '&modeline', saveModeline)
825       endtry
826       return resultBuffer
827     finally
828       let s:isEditFileRunning = s:isEditFileRunning - 1
829     endtry
830   catch
831     call s:ReportError(v:exception)
832     return -1
833   endtry
834 endfunction
836 " Section: Public functions {{{1
838 " Function: VCSCommandGetVCSType() {{{2
839 " Sets the b:VCSCommandVCSType variable in the given buffer to the
840 " appropriate source control system name.
842 function! VCSCommandGetVCSType(buffer)
843   let vcsType = getbufvar(a:buffer, 'VCSCommandVCSType')
844   if strlen(vcsType) > 0
845     return vcsType
846   endif
847   for vcsType in keys(s:plugins)
848     if s:plugins[vcsType].Identify(a:buffer)
849       call setbufvar(a:buffer, 'VCSCommandVCSType', vcsType)
850       return vcsType
851     endif
852   endfor
853   throw 'No suitable plugin'
854 endfunction
856 " Function: VCSCommandChdir(directory) {{{2
857 " Changes the current directory, respecting :lcd changes.
859 function! VCSCommandChdir(directory)
860   let command = 'cd'
861   if exists("*haslocaldir") && haslocaldir()
862     let command = 'lcd'
863   endif
864   execute command escape(a:directory, ' ')
865 endfunction
867 " Function: VCSCommandChangeToCurrentFileDir() {{{2
868 " Go to the directory in which the given file is located.
870 function! VCSCommandChangeToCurrentFileDir(fileName)
871   let oldCwd = getcwd()
872   let newCwd = fnamemodify(resolve(a:fileName), ':p:h')
873   if strlen(newCwd) > 0
874     call VCSCommandChdir(newCwd)
875   endif
876   return oldCwd
877 endfunction
879 " Function: VCSCommandGetOriginalBuffer(vcsBuffer) {{{2
880 " Attempts to locate the original file to which VCS operations were applied
881 " for a given buffer.
883 function! VCSCommandGetOriginalBuffer(vcsBuffer)
884   let origBuffer = getbufvar(a:vcsBuffer, 'VCSCommandOriginalBuffer')
885   if origBuffer
886     if bufexists(origBuffer)
887       return origBuffer
888     else
889       " Original buffer no longer exists.
890       throw 'Original buffer for this VCS buffer no longer exists.'
891     endif
892   else
893     " No original buffer
894     return a:vcsBuffer
895   endif
896 endfunction
898 " Function: VCSCommandRegisterModule(name, file, commandMap) {{{2
899 " Allows VCS modules to register themselves.
901 function! VCSCommandRegisterModule(name, file, commandMap, mappingMap)
902   let s:plugins[a:name] = a:commandMap
903   call add(s:pluginFiles, a:file)
904   let s:extendedMappings[a:name] = a:mappingMap
905   if !empty(a:mappingMap)
906         \ && !VCSCommandGetOption('VCSCommandDisableMappings', 0)
907         \ && !VCSCommandGetOption('VCSCommandDisableExtensionMappings', 0)
908     for mapname in keys(a:mappingMap)
909       execute 'noremap <silent> <Leader>' . mapname ':call <SID>ExecuteExtensionMapping(''' . mapname . ''')<CR>'
910     endfor
911   endif
912 endfunction
914 " Function: VCSCommandDoCommand(cmd, cmdName, statusText) {{{2
915 " General skeleton for VCS function execution.
916 " Returns: name of the new command buffer containing the command results
918 function! VCSCommandDoCommand(cmd, cmdName, statusText)
919   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
920   if originalBuffer == -1 
921     throw 'Original buffer no longer exists, aborting.'
922   endif
924   let fileName = resolve(bufname(originalBuffer))
926   " Work with netrw or other systems where a directory listing is displayed in
927   " a buffer.
929   if isdirectory(fileName)
930     let realFileName = '.'
931   else
932     let realFileName = fnamemodify(fileName, ':t')
933   endif
935   " Change to the directory of the current buffer.  This is done for CVS, but
936   " is left in for other systems as it does not affect them negatively.
938   let oldCwd = VCSCommandChangeToCurrentFileDir(fileName)
939   try
940     let output = system(a:cmd . ' "' . realFileName . '"')
941   finally
942     call VCSCommandChdir(oldCwd)
943   endtry
945   " HACK:  if line endings in the repository have been corrupted, the output
946   " of the command will be confused.
947   let output = substitute(output, "\r", '', 'g')
949   " HACK:  CVS diff command does not return proper error codes
950   if v:shell_error && (a:cmdName != 'diff' || getbufvar(originalBuffer, 'VCSCommandVCSType') != 'CVS')
951     if strlen(output) == 0
952       throw 'Version control command failed'
953     else
954       let output = substitute(output, '\n', '  ', 'g')
955       throw 'Version control command failed:  ' . output
956     endif
957   endif
958   if strlen(output) == 0
959     " Handle case of no output.  In this case, it is important to check the
960     " file status, especially since cvs edit/unedit may change the attributes
961     " of the file with no visible output.
963     checktime
964     return 0
965   endif
967   call s:EditFile(a:cmdName, originalBuffer, a:statusText)
969   silent 0put=output
971   " The last command left a blank line at the end of the buffer.  If the
972   " last line is folded (a side effect of the 'put') then the attempt to
973   " remove the blank line will kill the last fold.
974   "
975   " This could be fixed by explicitly detecting whether the last line is
976   " within a fold, but I prefer to simply unfold the result buffer altogether.
978   if has('folding')
979     normal zR
980   endif
982   $d
983   1
985   " Define the environment and execute user-defined hooks.
987   silent do VCSCommand User VCSBufferCreated
988   return bufnr('%')
989 endfunction
991 " Function: VCSCommandGetOption(name, default) {{{2
992 " Grab a user-specified option to override the default provided.  Options are
993 " searched in the window, buffer, then global spaces.
995 function! VCSCommandGetOption(name, default)
996   if has_key(s:optionOverrides, a:name) && len(s:optionOverrides[a:name]) > 0
997     return s:optionOverrides[a:name][-1]
998   elseif exists('w:' . a:name)
999     return w:{a:name}
1000   elseif exists('b:' . a:name)
1001     return b:{a:name}
1002   elseif exists('g:' . a:name)
1003     return g:{a:name}
1004   else
1005     return a:default
1006   endif
1007 endfunction
1009 " Function: VCSCommandGetRevision() {{{2
1010 " Global function for retrieving the current buffer's revision number.
1011 " Returns: Revision number or an empty string if an error occurs.
1013 function! VCSCommandGetRevision()
1014   if !exists('b:VCSCommandBufferInfo')
1015     let b:VCSCommandBufferInfo =  s:plugins[VCSCommandGetVCSType(bufnr('%'))].GetBufferInfo()
1016   endif
1018   if len(b:VCSCommandBufferInfo) > 0
1019     return b:VCSCommandBufferInfo[0]
1020   else
1021     return ''
1022   endif
1023 endfunction
1025 " Function: VCSCommandDisableBufferSetup() {{{2
1026 " Global function for deactivating the buffer autovariables.
1028 function! VCSCommandDisableBufferSetup()
1029   let g:VCSCommandEnableBufferSetup = 0
1030   silent! augroup! VCSCommandPlugin
1031 endfunction
1033 " Function: VCSCommandEnableBufferSetup() {{{2
1034 " Global function for activating the buffer autovariables.
1036 function! VCSCommandEnableBufferSetup()
1037   let g:VCSCommandEnableBufferSetup = 1
1038   augroup VCSCommandPlugin
1039     au!
1040     au BufEnter * call s:SetupBuffer()
1041   augroup END
1043   " Only auto-load if the plugin is fully loaded.  This gives other plugins a
1044   " chance to run.
1045   if g:loaded_VCSCommand == 2
1046     call s:SetupBuffer()
1047   endif
1048 endfunction
1050 " Function: VCSCommandGetStatusLine() {{{2
1051 " Default (sample) status line entry for VCS-controlled files.  This is only
1052 " useful if VCS-managed buffer mode is on (see the VCSCommandEnableBufferSetup
1053 " variable for how to do this).
1055 function! VCSCommandGetStatusLine()
1056   if exists('b:VCSCommandCommand')
1057     " This is a result buffer.  Return nothing because the buffer name
1058     " contains information already.
1059     return ''
1060   endif
1062   if exists('b:VCSCommandVCSType')
1063         \ && exists('g:VCSCommandEnableBufferSetup')
1064         \ && g:VCSCommandEnableBufferSetup
1065         \ && exists('b:VCSCommandBufferInfo')
1066     return '[' . join(extend([b:VCSCommandVCSType], b:VCSCommandBufferInfo), ' ') . ']'
1067   else
1068     return ''
1069   endif
1070 endfunction
1072 " Section: Command definitions {{{1
1073 " Section: Primary commands {{{2
1074 com! -nargs=* VCSAdd call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Add', [<f-args>], 0))
1075 com! -nargs=* VCSAnnotate call s:ExecuteVCSCommand('Annotate', [<f-args>], 1)
1076 com! -nargs=? -bang VCSCommit call s:VCSCommit(<q-bang>, <q-args>)
1077 com! -nargs=* VCSDelete call s:ExecuteVCSCommand('Delete', [<f-args>], 1)
1078 com! -nargs=* VCSDiff call s:ExecuteVCSCommand('Diff', [<f-args>], 1)
1079 com! -nargs=0 -bang VCSGotoOriginal call s:VCSGotoOriginal(<q-bang>)
1080 com! -nargs=* VCSInfo call s:ExecuteVCSCommand('Info', [<f-args>], 1)
1081 com! -nargs=* VCSLock call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Lock', [<f-args>], 1))
1082 com! -nargs=* VCSLog call s:ExecuteVCSCommand('Log', [<f-args>], 1)
1083 com! -nargs=0 VCSRevert call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Revert', [], 1))
1084 com! -nargs=? VCSReview call s:ExecuteVCSCommand('Review', [<f-args>], 1)
1085 com! -nargs=* VCSStatus call s:ExecuteVCSCommand('Status', [<f-args>], 1)
1086 com! -nargs=* VCSUnlock call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Unlock', [<f-args>], 1))
1087 com! -nargs=0 VCSUpdate call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Update', [], 1))
1088 com! -nargs=* VCSVimDiff call s:VCSVimDiff(<f-args>)
1090 " Section: VCS buffer management commands {{{2
1091 com! VCSCommandDisableBufferSetup call VCSCommandDisableBufferSetup()
1092 com! VCSCommandEnableBufferSetup call VCSCommandEnableBufferSetup()
1094 " Allow reloading VCSCommand.vim
1095 com! VCSReload let savedPluginFiles = s:pluginFiles|aunmenu Plugin.VCS|unlet! g:loaded_VCSCommand|runtime plugin/vcscommand.vim|for file in savedPluginFiles|execute 'source' file|endfor
1097 " Section: Plugin command mappings {{{1
1098 nnoremap <silent> <Plug>VCSAdd :VCSAdd<CR>
1099 nnoremap <silent> <Plug>VCSAnnotate :VCSAnnotate<CR>
1100 nnoremap <silent> <Plug>VCSCommit :VCSCommit<CR>
1101 nnoremap <silent> <Plug>VCSDelete :VCSDelete<CR>
1102 nnoremap <silent> <Plug>VCSDiff :VCSDiff<CR>
1103 nnoremap <silent> <Plug>VCSGotoOriginal :VCSGotoOriginal<CR>
1104 nnoremap <silent> <Plug>VCSClearAndGotoOriginal :VCSGotoOriginal!<CR>
1105 nnoremap <silent> <Plug>VCSInfo :VCSInfo<CR>
1106 nnoremap <silent> <Plug>VCSLock :VCSLock<CR>
1107 nnoremap <silent> <Plug>VCSLog :VCSLog<CR>
1108 nnoremap <silent> <Plug>VCSRevert :VCSRevert<CR>
1109 nnoremap <silent> <Plug>VCSReview :VCSReview<CR>
1110 nnoremap <silent> <Plug>VCSStatus :VCSStatus<CR>
1111 nnoremap <silent> <Plug>VCSUnlock :VCSUnlock<CR>
1112 nnoremap <silent> <Plug>VCSUpdate :VCSUpdate<CR>
1113 nnoremap <silent> <Plug>VCSVimDiff :VCSVimDiff<CR>
1115 " Section: Default mappings {{{1
1117 if !VCSCommandGetOption('VCSCommandDisableMappings', 0)
1118   if !hasmapto('<Plug>VCSAdd')
1119     nmap <unique> <Leader>ca <Plug>VCSAdd
1120   endif
1121   if !hasmapto('<Plug>VCSAnnotate')
1122     nmap <unique> <Leader>cn <Plug>VCSAnnotate
1123   endif
1124   if !hasmapto('<Plug>VCSClearAndGotoOriginal')
1125     nmap <unique> <Leader>cG <Plug>VCSClearAndGotoOriginal
1126   endif
1127   if !hasmapto('<Plug>VCSCommit')
1128     nmap <unique> <Leader>cc <Plug>VCSCommit
1129   endif
1130   if !hasmapto('<Plug>VCSDelete')
1131     nmap <unique> <Leader>cD <Plug>VCSDelete
1132   endif
1133   if !hasmapto('<Plug>VCSDiff')
1134     nmap <unique> <Leader>cd <Plug>VCSDiff
1135   endif
1136   if !hasmapto('<Plug>VCSGotoOriginal')
1137     nmap <unique> <Leader>cg <Plug>VCSGotoOriginal
1138   endif
1139   if !hasmapto('<Plug>VCSInfo')
1140     nmap <unique> <Leader>ci <Plug>VCSInfo
1141   endif
1142   if !hasmapto('<Plug>VCSLock')
1143     nmap <unique> <Leader>cL <Plug>VCSLock
1144   endif
1145   if !hasmapto('<Plug>VCSLog')
1146     nmap <unique> <Leader>cl <Plug>VCSLog
1147   endif
1148   if !hasmapto('<Plug>VCSRevert')
1149     nmap <unique> <Leader>cq <Plug>VCSRevert
1150   endif
1151   if !hasmapto('<Plug>VCSReview')
1152     nmap <unique> <Leader>cr <Plug>VCSReview
1153   endif
1154   if !hasmapto('<Plug>VCSStatus')
1155     nmap <unique> <Leader>cs <Plug>VCSStatus
1156   endif
1157   if !hasmapto('<Plug>VCSUnlock')
1158     nmap <unique> <Leader>cU <Plug>VCSUnlock
1159   endif
1160   if !hasmapto('<Plug>VCSUpdate')
1161     nmap <unique> <Leader>cu <Plug>VCSUpdate
1162   endif
1163   if !hasmapto('<Plug>VCSVimDiff')
1164     nmap <unique> <Leader>cv <Plug>VCSVimDiff
1165   endif
1166 endif
1168 " Section: Menu items {{{1
1169 amenu <silent> &Plugin.VCS.&Add        <Plug>VCSAdd
1170 amenu <silent> &Plugin.VCS.A&nnotate   <Plug>VCSAnnotate
1171 amenu <silent> &Plugin.VCS.&Commit     <Plug>VCSCommit
1172 amenu <silent> &Plugin.VCS.Delete      <Plug>VCSDelete
1173 amenu <silent> &Plugin.VCS.&Diff       <Plug>VCSDiff
1174 amenu <silent> &Plugin.VCS.&Info       <Plug>VCSInfo
1175 amenu <silent> &Plugin.VCS.&Log        <Plug>VCSLog
1176 amenu <silent> &Plugin.VCS.Revert      <Plug>VCSRevert
1177 amenu <silent> &Plugin.VCS.&Review     <Plug>VCSReview
1178 amenu <silent> &Plugin.VCS.&Status     <Plug>VCSStatus
1179 amenu <silent> &Plugin.VCS.&Update     <Plug>VCSUpdate
1180 amenu <silent> &Plugin.VCS.&VimDiff    <Plug>VCSVimDiff
1182 " Section: Autocommands to restore vimdiff state {{{1
1183 augroup VimDiffRestore
1184   au!
1185   au BufUnload * call s:VimDiffRestore(str2nr(expand('<abuf>')))
1186 augroup END
1188 " Section: Optional activation of buffer management {{{1
1190 if VCSCommandGetOption('VCSCommandEnableBufferSetup', 0)
1191   call VCSCommandEnableBufferSetup()
1192 endif
1194 " Section: VIM shutdown hook {{{1
1196 " Close all result buffers when VIM exits, to prevent them from being restored
1197 " via viminfo.
1199 " Function: s:CloseAllResultBuffers() {{{2
1200 " Closes all vcscommand result buffers.
1201 function! s:CloseAllResultBuffers()
1202   " This avoids using bufdo as that may load buffers already loaded in another
1203   " vim process, resulting in an error.
1204   let buffnr = 1
1205   let buffmaxnr = bufnr('$')
1206   while buffnr <= buffmaxnr
1207     if getbufvar(buffnr, 'VCSCommandOriginalBuffer') != "" 
1208       execute 'bw' buffnr
1209     endif
1210     let buffnr = buffnr + 1
1211   endwhile
1212 endfunction
1214 augroup VCSCommandVIMShutdown
1215   au!
1216   au VimLeavePre * call s:CloseAllResultBuffers()
1217 augroup END
1219 " Section: Plugin completion {{{1
1221 let loaded_VCSCommand = 2
1223 silent do VCSCommand User VCSPluginFinish
1225 let &cpo = s:save_cpo