1 " vim600: set foldmethod=marker:
3 " Vim plugin to assist in working with files under control of CVS or SVN.
6 " Maintainer: Bob Hiestand <bob.hiestand@gmail.com>
8 " Copyright (c) 2007 Bob Hiestand
10 " Permission is hereby granted, free of charge, to any person obtaining a copy
11 " of this software and associated documentation files (the "Software"), to
12 " deal in the Software without restriction, including without limitation the
13 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14 " sell copies of the Software, and to permit persons to whom the Software is
15 " furnished to do so, subject to the following conditions:
17 " The above copyright notice and this permission notice shall be included in
18 " all copies or substantial portions of the Software.
20 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 " Section: Documentation {{{1
30 " 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 " VCSDiff With no arguments, this displays the differences between
68 " the current file and its parent version under source
69 " control in a new scratch buffer.
71 " With one argument, the diff is performed on the
72 " current file against the specified revision.
74 " With two arguments, the diff is performed between the
75 " specified revisions of the current file.
77 " This command uses the 'VCSCommand{VCSType}DiffOpt' variable
78 " to specify diff options. If that variable does not exist,
79 " a plugin-specific default is used. If you wish to have no
80 " options, then set it to the empty string.
82 " VCSGotoOriginal Jumps to the source buffer if the current buffer is a VCS
83 " scratch buffer. If VCSGotoOriginal[!] is used, remove all
84 " VCS scratch buffers associated with the original file.
86 " VCSLock Locks the current file in order to prevent other users from
87 " concurrently modifying it. The exact semantics of this
88 " command depend on the underlying VCS.
90 " VCSLog Displays the version history of the current file in a new
93 " VCSRevert Replaces the modified version of the current file with the
94 " most recent version from the repository.
96 " VCSReview Displays a particular version of the current file in a new
97 " scratch buffer. If no argument is given, the most recent
98 " version of the file on the current branch is retrieved.
100 " VCSStatus Displays versioning information about the current file in a
101 " new scratch buffer.
103 " VCSUnlock Unlocks the current file in order to allow other users from
104 " concurrently modifying it. The exact semantics of this
105 " command depend on the underlying VCS.
107 " VCSUpdate Updates the current file with any relevant changes from the
110 " VCSVimDiff Uses vimdiff to display differences between versions of the
113 " If no revision is specified, the most recent version of the
114 " file on the current branch is used. With one argument,
115 " that argument is used as the revision as above. With two
116 " arguments, the differences between the two revisions is
117 " displayed using vimdiff.
119 " With either zero or one argument, the original buffer is
120 " used to perform the vimdiff. When the scratch buffer is
121 " closed, the original buffer will be returned to normal
124 " Once vimdiff mode is started using the above methods,
125 " additional vimdiff buffers may be added by passing a single
126 " version argument to the command. There may be up to 4
127 " vimdiff buffers total.
129 " Using the 2-argument form of the command resets the vimdiff
130 " to only those 2 versions. Additionally, invoking the
131 " command on a different file will close the previous vimdiff
134 " Mapping documentation: {{{2
136 " By default, a mapping is defined for each command. User-provided mappings
137 " can be used instead by mapping to <Plug>CommandName, for instance:
139 " nmap ,ca <Plug>VCSAdd
141 " The default mappings are as follow:
144 " <Leader>cn VCSAnnotate
145 " <Leader>cc VCSCommit
147 " <Leader>cg VCSGotoOriginal
148 " <Leader>cG VCSGotoOriginal!
151 " <Leader>cr VCSReview
152 " <Leader>cs VCSStatus
153 " <Leader>cu VCSUpdate
154 " <Leader>cU VCSUnlock
155 " <Leader>cv VCSVimDiff
157 " Options documentation: {{{2
159 " Several variables are checked by the script to determine behavior as follow:
161 " VCSCommandCommitOnWrite
162 " This variable, if set to a non-zero value, causes the pending commit to
163 " take place immediately as soon as the log message buffer is written. If
164 " set to zero, only the VCSCommit mapping will cause the pending commit to
165 " occur. If not set, it defaults to 1.
167 " VCSCommandDeleteOnHide
168 " This variable, if set to a non-zero value, causes the temporary VCS result
169 " buffers to automatically delete themselves when hidden.
171 " VCSCommand{VCSType}DiffOpt
172 " This variable, if set, determines the options passed to the diff command
173 " of the underlying VCS. Each VCS plugin defines a default value.
175 " VCSCommandDiffSplit
176 " This variable overrides the VCSCommandSplit variable, but only for buffers
177 " created with VCSVimDiff.
180 " This variable controls whether to split the current window to display a
181 " scratch buffer ('split'), or to display it in the current buffer ('edit').
182 " If not set, it defaults to 'split'.
184 " VCSCommandEnableBufferSetup
185 " This variable, if set to a non-zero value, activates VCS buffer management
186 " mode. This mode means that the buffer variable 'VCSRevision' is set if
187 " the file is VCS-controlled. This is useful for displaying version
188 " information in the status bar. Additional options may be set by
189 " individual VCS plugins.
191 " VCSCommandResultBufferNameExtension
192 " This variable, if set to a non-blank value, is appended to the name of the
193 " VCS command output buffers. For example, '.vcs'. Using this option may
194 " help avoid problems caused by autocommands dependent on file extension.
196 " VCSCommandResultBufferNameFunction
197 " This variable, if set, specifies a custom function for naming VCS command
198 " output buffers. This function will be passed the following arguments:
200 " command - name of the VCS command being executed (such as 'Log' or
203 " originalBuffer - buffer number of the source file.
205 " vcsType - type of VCS controlling this file (such as 'CVS' or 'SVN').
207 " statusText - extra text associated with the VCS action (such as version
211 " This variable controls the orientation of the various window splits that
212 " may occur (such as with VCSVimDiff, when using a VCS command on a VCS
213 " command buffer, or when the 'VCSCommandEdit' variable is set to 'split'.
214 " If set to 'horizontal', the resulting windows will be on stacked on top of
215 " one another. If set to 'vertical', the resulting windows will be
216 " side-by-side. If not set, it defaults to 'horizontal' for all but
217 " VCSVimDiff windows.
219 " Event documentation {{{2
220 " For additional customization, VCSCommand.vim uses User event autocommand
221 " hooks. Each event is in the VCSCommand group, and different patterns
222 " match the various hooks.
224 " For instance, the following could be added to the vimrc to provide a 'q'
225 " mapping to quit a VCS scratch buffer:
228 " au VCSCommand User VCSBufferCreated silent! nmap <unique> <buffer> q :bwipeout<cr>
231 " The following hooks are available:
233 " VCSBufferCreated This event is fired just after a VCS command
234 " output buffer is created. It is executed
235 " within the context of the new buffer.
237 " VCSBufferSetup This event is fired just after VCS buffer setup
238 " occurs, if enabled.
240 " VCSLoadExtensions This event is fired just before the
241 " VCSPluginFinish event. It is intended to be
242 " used only by VCS extensions to register
243 " themselves with VCSCommand if they are sourced
246 " VCSPluginInit This event is fired when the VCSCommand plugin
249 " VCSPluginFinish This event is fired just after the VCSCommand
252 " VCSVimDiffFinish This event is fired just after the VCSVimDiff
253 " command executes to allow customization of,
254 " for instance, window placement and focus.
256 " Section: Plugin header {{{1
258 " loaded_VCSCommand is set to 1 when the initialization begins, and 2 when it
259 " completes. This allows various actions to only be taken by functions after
260 " system initialization.
262 if exists('loaded_VCSCommand')
265 let loaded_VCSCommand = 1
268 echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
272 " Section: Event group setup {{{1
277 augroup VCSCommandCommit
280 " Section: Plugin initialization {{{1
281 silent do VCSCommand User VCSPluginInit
283 " Section: Script variable initialization {{{1
286 let s:pluginFiles = []
287 let s:extendedMappings = {}
288 let s:optionOverrides = {}
289 let s:isEditFileRunning = 0
291 unlet! s:vimDiffRestoreCmd
292 unlet! s:vimDiffSourceBuffer
293 unlet! s:vimDiffScratchList
295 " Section: Utility functions {{{1
297 " Function: s:ExecuteExtensionMapping(mapping) {{{2
298 " Invokes the appropriate extension mapping depending on the type of the
301 function! s:ExecuteExtensionMapping(mapping)
302 let buffer = bufnr('%')
303 let vcsType = VCSCommandGetVCSType(buffer)
304 if !has_key(s:extendedMappings, vcsType)
305 throw 'Unknown VCS type: ' . vcsType
307 if !has_key(s:extendedMappings[vcsType], a:mapping)
308 throw 'This extended mapping is not defined for ' . vcsType
310 silent execute 'normal' ':' . s:extendedMappings[vcsType][a:mapping] . "\<CR>"
313 " Function: s:ExecuteVCSCommand(command, buffer, argList) {{{2
314 " Calls the indicated plugin-specific VCS command on the indicated buffer.
315 " Returns: buffer number of resulting output scratch buffer, or -1 if an error
318 function! s:ExecuteVCSCommand(command, buffer, argList)
320 let vcsType = VCSCommandGetVCSType(a:buffer)
321 if !has_key(s:plugins, vcsType)
322 throw 'Unknown VCS type: ' . vcsType
324 let functionMap = s:plugins[vcsType]
325 if !has_key(functionMap, a:command)
326 throw 'Command ''' . a:command . ''' not implemented for ' . vcsType
328 return functionMap[a:command](a:argList)
330 echohl WarningMsg|echomsg v:exception|echohl None
335 " Function: s:CreateCommandBuffer(cmd, cmdName, originalBuffer, statusText) {{{2
336 " Creates a new scratch buffer and captures the output from execution of the
337 " given command. The name of the scratch buffer is returned.
339 function! s:CreateCommandBuffer(cmd, cmdName, originalBuffer, statusText)
340 let output = system(a:cmd)
342 " HACK: if line endings in the repository have been corrupted, the output
343 " of the command will be confused.
344 let output = substitute(output, "\r", '', 'g')
346 " HACK: CVS diff command does not return proper error codes
347 if v:shell_error && (a:cmdName != 'diff' || getbufvar(a:originalBuffer, 'VCSCommandVCSType') != 'CVS')
348 if strlen(output) == 0
349 throw 'Version control command failed'
351 let output = substitute(output, '\n', ' ', 'g')
352 throw 'Version control command failed: ' . output
355 if strlen(output) == 0
356 " Handle case of no output. In this case, it is important to check the
357 " file status, especially since cvs edit/unedit may change the attributes
358 " of the file with no visible output.
364 call s:EditFile(a:cmdName, a:originalBuffer, a:statusText)
368 " The last command left a blank line at the end of the buffer. If the
369 " last line is folded (a side effect of the 'put') then the attempt to
370 " remove the blank line will kill the last fold.
372 " This could be fixed by explicitly detecting whether the last line is
373 " within a fold, but I prefer to simply unfold the result buffer altogether.
382 " Define the environment and execute user-defined hooks.
384 silent do VCSCommand User VCSBufferCreated
388 " Function: s:GenerateResultBufferName(command, originalBuffer, vcsType, statusText) {{{2
389 " Default method of generating the name for VCS result buffers. This can be
390 " overridden with the VCSResultBufferNameFunction variable.
392 function! s:GenerateResultBufferName(command, originalBuffer, vcsType, statusText)
393 let fileName=bufname(a:originalBuffer)
394 let bufferName = a:vcsType . ' ' . a:command
395 if strlen(a:statusText) > 0
396 let bufferName .= ' ' . a:statusText
398 let bufferName .= ' ' . fileName
400 let versionedBufferName = bufferName
401 while buflisted(versionedBufferName)
403 let versionedBufferName = bufferName . ' (' . counter . ')'
405 return versionedBufferName
408 " Function: s:GenerateResultBufferNameWithExtension(command, originalBuffer, vcsType, statusText) {{{2
409 " Method of generating the name for VCS result buffers that uses the original
410 " file name with the VCS type and command appended as extensions.
412 function! s:GenerateResultBufferNameWithExtension(command, originalBuffer, vcsType, statusText)
413 let fileName=bufname(a:originalBuffer)
414 let bufferName = a:vcsType . ' ' . a:command
415 if strlen(a:statusText) > 0
416 let bufferName .= ' ' . a:statusText
418 let bufferName .= ' ' . fileName . VCSCommandGetOption('VCSCommandResultBufferNameExtension', '.vcs')
420 let versionedBufferName = bufferName
421 while buflisted(versionedBufferName)
423 let versionedBufferName = '(' . counter . ') ' . bufferName
425 return versionedBufferName
428 " Function: s:EditFile(command, originalBuffer, statusText) {{{2
429 " Creates a new buffer of the given name and associates it with the given
432 function! s:EditFile(command, originalBuffer, statusText)
433 let vcsType = getbufvar(a:originalBuffer, 'VCSCommandVCSType')
435 let nameExtension = VCSCommandGetOption('VCSCommandResultBufferNameExtension', '')
436 if nameExtension == ''
437 let nameFunction = VCSCommandGetOption('VCSCommandResultBufferNameFunction', 's:GenerateResultBufferName')
439 let nameFunction = VCSCommandGetOption('VCSCommandResultBufferNameFunction', 's:GenerateResultBufferNameWithExtension')
442 let resultBufferName = call(nameFunction, [a:command, a:originalBuffer, vcsType, a:statusText])
444 " Protect against useless buffer set-up
445 let s:isEditFileRunning += 1
447 let editCommand = VCSCommandGetOption('VCSCommandEdit', 'split')
448 if editCommand == 'split'
449 if VCSCommandGetOption('VCSCommandSplit', 'horizontal') == 'horizontal'
452 vert rightbelow split
455 edit `=resultBufferName`
456 let b:VCSCommandCommand = a:command
457 let b:VCSCommandOriginalBuffer = a:originalBuffer
458 let b:VCSCommandSourceFile = bufname(a:originalBuffer)
459 let b:VCSCommandVCSType = vcsType
463 let &filetype=vcsType . a:command
465 if a:statusText != ''
466 let b:VCSCommandStatusText = a:statusText
469 if VCSCommandGetOption('VCSCommandDeleteOnHide', 0)
473 let s:isEditFileRunning -= 1
478 " Function: s:SetupBuffer() {{{2
479 " Attempts to set the b:VCSCommandBufferInfo variable
481 function! s:SetupBuffer()
482 if (exists('b:VCSCommandBufferSetup') && b:VCSCommandBufferSetup)
483 " This buffer is already set up.
487 if strlen(&buftype) > 0 || !filereadable(@%)
488 " No special status for special buffers.
492 if !VCSCommandGetOption('VCSCommandEnableBufferSetup', 0) || s:isEditFileRunning > 0
493 unlet! b:VCSCommandBufferSetup
498 let vcsType = VCSCommandGetVCSType(bufnr('%'))
499 let b:VCSCommandBufferInfo = s:plugins[vcsType].GetBufferInfo()
500 silent do VCSCommand User VCSBufferSetup
501 catch /No suitable plugin/
502 " This is not a VCS-controlled file.
503 let b:VCSCommandBufferInfo = []
506 let b:VCSCommandBufferSetup=1
509 " Function: s:MarkOrigBufferForSetup(buffer) {{{2
510 " Resets the buffer setup state of the original buffer for a given VCS scratch
512 " Returns: The VCS buffer number in a passthrough mode.
514 function! s:MarkOrigBufferForSetup(buffer)
517 let origBuffer = VCSCommandGetOriginalBuffer(a:buffer)
518 " This should never not work, but I'm paranoid
519 if origBuffer != a:buffer
520 call setbufvar(origBuffer, 'VCSCommandBufferSetup', 0)
526 " Function: s:OverrideOption(option, [value]) {{{2
527 " Provides a temporary override for the given VCS option. If no value is
528 " passed, the override is disabled.
530 function! s:OverrideOption(option, ...)
532 call remove(s:optionOverrides[a:option], -1)
534 if !has_key(s:optionOverrides, a:option)
535 let s:optionOverrides[a:option] = []
537 call add(s:optionOverrides[a:option], a:1)
541 " Function: s:WipeoutCommandBuffers() {{{2
542 " Clears all current VCS output buffers of the specified type for a given source.
544 function! s:WipeoutCommandBuffers(originalBuffer, VCSCommand)
546 while buffer <= bufnr('$')
547 if getbufvar(buffer, 'VCSCommandOriginalBuffer') == a:originalBuffer
548 if getbufvar(buffer, 'VCSCommandCommand') == a:VCSCommand
552 let buffer = buffer + 1
556 " Function: s:VimDiffRestore(vimDiffBuff) {{{2
557 " Checks whether the given buffer is one whose deletion should trigger
558 " restoration of an original buffer after it was diffed. If so, it executes
559 " the appropriate setting command stored with that original buffer.
561 function! s:VimDiffRestore(vimDiffBuff)
562 let s:isEditFileRunning += 1
564 if exists('s:vimDiffSourceBuffer')
565 if a:vimDiffBuff == s:vimDiffSourceBuffer
566 " Original file is being removed.
567 unlet! s:vimDiffSourceBuffer
568 unlet! s:vimDiffRestoreCmd
569 unlet! s:vimDiffScratchList
571 let index = index(s:vimDiffScratchList, a:vimDiffBuff)
573 call remove(s:vimDiffScratchList, index)
574 if len(s:vimDiffScratchList) == 0
575 if exists('s:vimDiffRestoreCmd')
576 " All scratch buffers are gone, reset the original.
577 " Only restore if the source buffer is still in Diff mode
579 let sourceWinNR=bufwinnr(s:vimDiffSourceBuffer)
581 " The buffer is visible in at least one window
582 let currentWinNR = winnr()
583 while winbufnr(sourceWinNR) != -1
584 if winbufnr(sourceWinNR) == s:vimDiffSourceBuffer
585 execute sourceWinNR . 'wincmd w'
586 if getwinvar(0, '&diff')
587 execute s:vimDiffRestoreCmd
590 let sourceWinNR = sourceWinNR + 1
592 execute currentWinNR . 'wincmd w'
594 " The buffer is hidden. It must be visible in order to set the
596 let currentBufNR = bufnr('')
597 execute 'hide buffer' s:vimDiffSourceBuffer
598 if getwinvar(0, '&diff')
599 execute s:vimDiffRestoreCmd
601 execute 'hide buffer' currentBufNR
604 unlet s:vimDiffRestoreCmd
606 " All buffers are gone.
607 unlet s:vimDiffSourceBuffer
608 unlet s:vimDiffScratchList
614 let s:isEditFileRunning -= 1
619 " Section: Generic VCS command functions {{{1
621 " Function: s:VCSCommit() {{{2
622 function! s:VCSCommit(bang, message)
623 let vcsType = VCSCommandGetVCSType(bufnr('%'))
624 if !has_key(s:plugins, vcsType)
625 throw 'Unknown VCS type: ' . vcsType
628 let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
630 " Handle the commit message being specified. If a message is supplied, it
631 " is used; if bang is supplied, an empty message is used; otherwise, the
632 " user is provided a buffer from which to edit the commit message.
634 if strlen(a:message) > 0 || a:bang == '!'
635 return s:VCSFinishCommit([a:message], originalBuffer)
638 call s:EditFile('commitlog', originalBuffer, '')
641 " Create a commit mapping.
643 nnoremap <silent> <buffer> <Plug>VCSCommit :call <SID>VCSFinishCommitWithBuffer()<CR>
645 silent 0put ='VCS: ----------------------------------------------------------------------'
646 silent put ='VCS: Please enter log message. Lines beginning with ''VCS:'' are removed automatically.'
647 silent put ='VCS: To finish the commit, Type <leader>cc (or your own <Plug>VCSCommit mapping)'
649 if VCSCommandGetOption('VCSCommandCommitOnWrite', 1) == 1
651 au VCSCommandCommit BufWriteCmd <buffer> call s:VCSFinishCommitWithBuffer()
652 silent put ='VCS: or write this buffer'
655 silent put ='VCS: ----------------------------------------------------------------------'
660 " Function: s:VCSFinishCommitWithBuffer() {{{2
661 " Wrapper for s:VCSFinishCommit which is called only from a commit log buffer
662 " which removes all lines starting with 'VCS:'.
664 function! s:VCSFinishCommitWithBuffer()
666 let currentBuffer = bufnr('%')
667 let logMessageList = getbufline('%', 1, '$')
668 call filter(logMessageList, 'v:val !~ ''^\s*VCS:''')
669 let resultBuffer = s:VCSFinishCommit(logMessageList, b:VCSCommandOriginalBuffer)
671 execute 'bw' currentBuffer
676 " Function: s:VCSFinishCommit(logMessageList, originalBuffer) {{{2
677 function! s:VCSFinishCommit(logMessageList, originalBuffer)
678 let shellSlashBak = &shellslash
681 let messageFileName = tempname()
682 call writefile(a:logMessageList, messageFileName)
684 let resultBuffer = s:ExecuteVCSCommand('Commit', a:originalBuffer, [messageFileName])
688 return s:MarkOrigBufferForSetup(resultBuffer)
690 call delete(messageFileName)
693 let &shellslash = shellSlashBak
697 " Function: s:VCSGotoOriginal(bang) {{{2
698 function! s:VCSGotoOriginal(bang)
699 let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
700 if originalBuffer > 0
701 let origWinNR = bufwinnr(originalBuffer)
703 execute 'buffer' originalBuffer
705 execute origWinNR . 'wincmd w'
709 let buffmaxnr = bufnr('$')
710 while buffnr <= buffmaxnr
711 if getbufvar(buffnr, 'VCSCommandOriginalBuffer') == originalBuffer
714 let buffnr = buffnr + 1
720 " Function: s:VCSVimDiff(...) {{{2
721 function! s:VCSVimDiff(...)
722 let vcsType = VCSCommandGetVCSType(bufnr('%'))
723 if !has_key(s:plugins, vcsType)
724 throw 'Unknown VCS type: ' . vcsType
726 let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
727 let s:isEditFileRunning = s:isEditFileRunning + 1
729 " If there's already a VimDiff'ed window, restore it.
730 " There may only be one VCSVimDiff original window at a time.
732 if exists('s:vimDiffSourceBuffer') && s:vimDiffSourceBuffer != originalBuffer
733 " Clear the existing vimdiff setup by removing the result buffers.
734 call s:WipeoutCommandBuffers(s:vimDiffSourceBuffer, 'vimdiff')
739 " Reset the vimdiff system, as 2 explicit versions were provided.
740 if exists('s:vimDiffSourceBuffer')
741 call s:WipeoutCommandBuffers(s:vimDiffSourceBuffer, 'vimdiff')
743 let resultBuffer = s:plugins[vcsType].Review([a:1])
745 echomsg 'Can''t open revision ' . a:1
748 let b:VCSCommandCommand = 'vimdiff'
750 let s:vimDiffScratchList = [resultBuffer]
751 " If no split method is defined, cheat, and set it to vertical.
753 call s:OverrideOption('VCSCommandSplit', VCSCommandGetOption('VCSCommandDiffSplit', VCSCommandGetOption('VCSCommandSplit', 'vertical')))
754 let resultBuffer=s:plugins[vcsType].Review([a:2])
756 call s:OverrideOption('VCSCommandSplit')
759 echomsg 'Can''t open revision ' . a:1
762 let b:VCSCommandCommand = 'vimdiff'
764 let s:vimDiffScratchList += [resultBuffer]
767 call s:OverrideOption('VCSCommandEdit', 'split')
769 " Force splitting behavior, otherwise why use vimdiff?
770 call s:OverrideOption('VCSCommandSplit', VCSCommandGetOption('VCSCommandDiffSplit', VCSCommandGetOption('VCSCommandSplit', 'vertical')))
773 let resultBuffer=s:plugins[vcsType].Review([])
775 let resultBuffer=s:plugins[vcsType].Review([a:1])
778 call s:OverrideOption('VCSCommandSplit')
781 call s:OverrideOption('VCSCommandEdit')
784 echomsg 'Can''t open current revision'
787 let b:VCSCommandCommand = 'vimdiff'
790 if !exists('s:vimDiffSourceBuffer')
791 " New instance of vimdiff.
792 let s:vimDiffScratchList = [resultBuffer]
794 " This could have been invoked on a VCS result buffer, not the
797 execute 'buffer' originalBuffer
798 " Store info for later original buffer restore
799 let s:vimDiffRestoreCmd =
800 \ 'call setbufvar('.originalBuffer.', ''&diff'', '.getbufvar(originalBuffer, '&diff').')'
801 \ . '|call setbufvar('.originalBuffer.', ''&foldcolumn'', '.getbufvar(originalBuffer, '&foldcolumn').')'
802 \ . '|call setbufvar('.originalBuffer.', ''&foldenable'', '.getbufvar(originalBuffer, '&foldenable').')'
803 \ . '|call setbufvar('.originalBuffer.', ''&foldmethod'', '''.getbufvar(originalBuffer, '&foldmethod').''')'
804 \ . '|call setbufvar('.originalBuffer.', ''&scrollbind'', '.getbufvar(originalBuffer, '&scrollbind').')'
805 \ . '|call setbufvar('.originalBuffer.', ''&wrap'', '.getbufvar(originalBuffer, '&wrap').')'
806 \ . '|if &foldmethod==''manual''|execute ''normal zE''|endif'
810 " Adding a window to an existing vimdiff
811 let s:vimDiffScratchList += [resultBuffer]
815 let s:vimDiffSourceBuffer = originalBuffer
817 " Avoid executing the modeline in the current buffer after the autocommand.
819 let currentBuffer = bufnr('%')
820 let saveModeline = getbufvar(currentBuffer, '&modeline')
822 call setbufvar(currentBuffer, '&modeline', 0)
823 silent do VCSCommand User VCSVimDiffFinish
825 call setbufvar(currentBuffer, '&modeline', saveModeline)
829 let s:isEditFileRunning = s:isEditFileRunning - 1
833 " Section: Public functions {{{1
835 " Function: VCSCommandGetVCSType(buffer) {{{2
836 " Sets the b:VCSCommandVCSType variable in the current buffer to the
837 " appropriate source control system name.
839 function! VCSCommandGetVCSType(buffer)
840 let vcsType = getbufvar(a:buffer, 'VCSCommandVCSType')
841 if strlen(vcsType) > 0
844 for vcsType in keys(s:plugins)
845 if s:plugins[vcsType].Identify(a:buffer)
846 call setbufvar(a:buffer, 'VCSCommandVCSType', vcsType)
850 throw 'No suitable plugin'
853 " Function: VCSCommandChangeToCurrentFileDir() {{{2
854 " Go to the directory in which the given file is located.
856 function! VCSCommandChangeToCurrentFileDir(fileName)
858 let newCwd=fnamemodify(resolve(a:fileName), ':p:h')
859 if strlen(newCwd) > 0
860 execute 'cd' escape(newCwd, ' ')
865 " Function: VCSCommandGetOriginalBuffer(vcsBuffer) {{{2
866 " Attempts to locate the original file to which VCS operations were applied
867 " for a given buffer.
869 function! VCSCommandGetOriginalBuffer(vcsBuffer)
870 let origBuffer = getbufvar(a:vcsBuffer, 'VCSCommandOriginalBuffer')
872 if bufexists(origBuffer)
875 " Original buffer no longer exists.
876 throw 'Original buffer for this VCS buffer no longer exists.'
884 " Function: VCSCommandRegisterModule(name, file, commandMap) {{{2
885 " Allows VCS modules to register themselves.
887 function! VCSCommandRegisterModule(name, file, commandMap, mappingMap)
888 let s:plugins[a:name] = a:commandMap
889 call add(s:pluginFiles, a:file)
890 let s:extendedMappings[a:name] = a:mappingMap
891 if(!empty(a:mappingMap))
892 for mapname in keys(a:mappingMap)
893 execute 'noremap <silent> <Leader>' . mapname ':call <SID>ExecuteExtensionMapping(''' . mapname . ''')<CR>'
898 " Function: VCSCommandDoCommand(cmd, cmdName, statusText) {{{2
899 " General skeleton for VCS function execution.
900 " Returns: name of the new command buffer containing the command results
902 function! VCSCommandDoCommand(cmd, cmdName, statusText)
903 let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
904 if originalBuffer == -1
905 throw 'Original buffer no longer exists, aborting.'
908 let fileName=bufname(originalBuffer)
910 " Work with netrw or other systems where a directory listing is displayed in
913 if isdirectory(fileName)
914 let realFileName = '.'
916 let realFileName = fnamemodify(resolve(fileName), ':t')
919 let oldCwd=VCSCommandChangeToCurrentFileDir(fileName)
921 let fullCmd = a:cmd . ' "' . realFileName . '"'
922 let resultBuffer=s:CreateCommandBuffer(fullCmd, a:cmdName, originalBuffer, a:statusText)
925 execute 'cd' escape(oldCwd, ' ')
929 " Function: VCSCommandGetOption(name, default) {{{2
930 " Grab a user-specified option to override the default provided. Options are
931 " searched in the window, buffer, then global spaces.
933 function! VCSCommandGetOption(name, default)
934 if has_key(s:optionOverrides, a:name) && len(s:optionOverrides[a:name]) > 0
935 return s:optionOverrides[a:name][-1]
936 elseif exists('w:' . a:name)
938 elseif exists('b:' . a:name)
940 elseif exists('g:' . a:name)
947 " Function: VCSCommandGetRevision() {{{2
948 " Global function for retrieving the current buffer's revision number.
949 " Returns: Revision number or an empty string if an error occurs.
951 function! VCSCommandGetRevision()
952 if !exists('b:VCSCommandBufferInfo')
953 let b:VCSCommandBufferInfo = s:plugins[VCSCommandGetVCSType(bufnr('%'))].GetBufferInfo()
956 if len(b:VCSCommandBufferInfo) > 0
957 return b:VCSCommandBufferInfo[0]
963 " Function: VCSCommandDisableBufferSetup() {{{2
964 " Global function for deactivating the buffer autovariables.
966 function! VCSCommandDisableBufferSetup()
967 let g:VCSCommandEnableBufferSetup = 0
968 silent! augroup! VCSCommandPlugin
971 " Function: VCSCommandEnableBufferSetup() {{{2
972 " Global function for activating the buffer autovariables.
974 function! VCSCommandEnableBufferSetup()
975 let g:VCSCommandEnableBufferSetup = 1
976 augroup VCSCommandPlugin
978 au BufEnter * call s:SetupBuffer()
981 " Only auto-load if the plugin is fully loaded. This gives other plugins a
983 if g:loaded_VCSCommand == 2
988 " Function: VCSCommandGetStatusLine() {{{2
989 " Default (sample) status line entry for VCS-controlled files. This is only
990 " useful if VCS-managed buffer mode is on (see the VCSCommandEnableBufferSetup
991 " variable for how to do this).
993 function! VCSCommandGetStatusLine()
994 if exists('b:VCSCommandCommand')
995 " This is a result buffer. Return nothing because the buffer name
996 " contains information already.
1000 if exists('b:VCSCommandVCSType')
1001 \ && exists('g:VCSCommandEnableBufferSetup')
1002 \ && g:VCSCommandEnableBufferSetup
1003 \ && exists('b:VCSCommandBufferInfo')
1004 return '[' . join(extend([b:VCSCommandVCSType], b:VCSCommandBufferInfo), ' ') . ']'
1010 " Section: Command definitions {{{1
1011 " Section: Primary commands {{{2
1012 com! -nargs=0 VCSAdd call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Add', bufnr('%'), []))
1013 com! -nargs=? VCSAnnotate call s:ExecuteVCSCommand('Annotate', bufnr('%'), [<f-args>])
1014 com! -nargs=? -bang VCSCommit call s:VCSCommit(<q-bang>, <q-args>)
1015 com! -nargs=* VCSDiff call s:ExecuteVCSCommand('Diff', bufnr('%'), [<f-args>])
1016 com! -nargs=0 -bang VCSGotoOriginal call s:VCSGotoOriginal(<q-bang>)
1017 com! -nargs=0 VCSLock call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Lock', bufnr('%'), []))
1018 com! -nargs=? VCSLog call s:ExecuteVCSCommand('Log', bufnr('%'), [<f-args>])
1019 com! -nargs=0 VCSRevert call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Revert', bufnr('%'), []))
1020 com! -nargs=? VCSReview call s:ExecuteVCSCommand('Review', bufnr('%'), [<f-args>])
1021 com! -nargs=0 VCSStatus call s:ExecuteVCSCommand('Status', bufnr('%'), [])
1022 com! -nargs=0 VCSUnlock call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Unlock', bufnr('%'), []))
1023 com! -nargs=0 VCSUpdate call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Update', bufnr('%'), []))
1024 com! -nargs=* VCSVimDiff call s:VCSVimDiff(<f-args>)
1026 " Section: VCS buffer management commands {{{2
1027 com! VCSCommandDisableBufferSetup call VCSCommandDisableBufferSetup()
1028 com! VCSCommandEnableBufferSetup call VCSCommandEnableBufferSetup()
1030 " Allow reloading VCSCommand.vim
1031 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
1033 " Section: Plugin command mappings {{{1
1034 nnoremap <silent> <Plug>VCSAdd :VCSAdd<CR>
1035 nnoremap <silent> <Plug>VCSAnnotate :VCSAnnotate<CR>
1036 nnoremap <silent> <Plug>VCSCommit :VCSCommit<CR>
1037 nnoremap <silent> <Plug>VCSDiff :VCSDiff<CR>
1038 nnoremap <silent> <Plug>VCSGotoOriginal :VCSGotoOriginal<CR>
1039 nnoremap <silent> <Plug>VCSClearAndGotoOriginal :VCSGotoOriginal!<CR>
1040 nnoremap <silent> <Plug>VCSLock :VCSLock<CR>
1041 nnoremap <silent> <Plug>VCSLog :VCSLog<CR>
1042 nnoremap <silent> <Plug>VCSRevert :VCSRevert<CR>
1043 nnoremap <silent> <Plug>VCSReview :VCSReview<CR>
1044 nnoremap <silent> <Plug>VCSStatus :VCSStatus<CR>
1045 nnoremap <silent> <Plug>VCSUnlock :VCSUnlock<CR>
1046 nnoremap <silent> <Plug>VCSUpdate :VCSUpdate<CR>
1047 nnoremap <silent> <Plug>VCSVimDiff :VCSVimDiff<CR>
1049 " Section: Default mappings {{{1
1050 if !hasmapto('<Plug>VCSAdd')
1051 nmap <unique> <Leader>ca <Plug>VCSAdd
1053 if !hasmapto('<Plug>VCSAnnotate')
1054 nmap <unique> <Leader>cn <Plug>VCSAnnotate
1056 if !hasmapto('<Plug>VCSClearAndGotoOriginal')
1057 nmap <unique> <Leader>cG <Plug>VCSClearAndGotoOriginal
1059 if !hasmapto('<Plug>VCSCommit')
1060 nmap <unique> <Leader>cc <Plug>VCSCommit
1062 if !hasmapto('<Plug>VCSDiff')
1063 nmap <unique> <Leader>cd <Plug>VCSDiff
1065 if !hasmapto('<Plug>VCSGotoOriginal')
1066 nmap <unique> <Leader>cg <Plug>VCSGotoOriginal
1068 if !hasmapto('<Plug>VCSLock')
1069 nmap <unique> <Leader>cL <Plug>VCSLock
1071 if !hasmapto('<Plug>VCSLog')
1072 nmap <unique> <Leader>cl <Plug>VCSLog
1074 if !hasmapto('<Plug>VCSRevert')
1075 nmap <unique> <Leader>cq <Plug>VCSRevert
1077 if !hasmapto('<Plug>VCSReview')
1078 nmap <unique> <Leader>cr <Plug>VCSReview
1080 if !hasmapto('<Plug>VCSStatus')
1081 nmap <unique> <Leader>cs <Plug>VCSStatus
1083 if !hasmapto('<Plug>VCSUnlock')
1084 nmap <unique> <Leader>cU <Plug>VCSUnlock
1086 if !hasmapto('<Plug>VCSUpdate')
1087 nmap <unique> <Leader>cu <Plug>VCSUpdate
1089 if !hasmapto('<Plug>VCSVimDiff')
1090 nmap <unique> <Leader>cv <Plug>VCSVimDiff
1093 " Section: Menu items {{{1
1094 amenu <silent> &Plugin.VCS.&Add <Plug>VCSAdd
1095 amenu <silent> &Plugin.VCS.A&nnotate <Plug>VCSAnnotate
1096 amenu <silent> &Plugin.VCS.&Commit <Plug>VCSCommit
1097 amenu <silent> &Plugin.VCS.&Diff <Plug>VCSDiff
1098 amenu <silent> &Plugin.VCS.&Log <Plug>VCSLog
1099 amenu <silent> &Plugin.VCS.Revert <Plug>VCSRevert
1100 amenu <silent> &Plugin.VCS.&Review <Plug>VCSReview
1101 amenu <silent> &Plugin.VCS.&Status <Plug>VCSStatus
1102 amenu <silent> &Plugin.VCS.&Update <Plug>VCSUpdate
1103 amenu <silent> &Plugin.VCS.&VimDiff <Plug>VCSVimDiff
1105 " Section: Autocommands to restore vimdiff state {{{1
1106 augroup VimDiffRestore
1108 au BufUnload * call s:VimDiffRestore(str2nr(expand('<abuf>')))
1111 " Section: Optional activation of buffer management {{{1
1113 if VCSCommandGetOption('VCSCommandEnableBufferSetup', 0)
1114 call VCSCommandEnableBufferSetup()
1117 " Section: Plugin completion {{{1
1119 let loaded_VCSCommand=2
1121 " Load delayed extension plugin registration.
1122 silent do VCSCommand User VCSLoadExtensions
1123 au! VCSCommand User VCSLoadExtensions
1125 silent do VCSCommand User VCSPluginFinish