File: vcscommand.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 (1045 lines) | stat: -rw-r--r-- 37,401 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
" vim600: set foldmethod=marker:
"
" Vim plugin to assist in working with files under control of CVS or SVN.
"
" Last Change:
" Version:       VCS development
" Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
" License:       This file is placed in the public domain.
"
" Section: Documentation {{{1
"
" Provides functions to invoke various source control commands on the current
" file (either the current buffer, or, in the case of an directory buffer, the
" directory and all subdirectories associated with the current buffer).  The
" output of the commands is captured in a new scratch window.
"
" This plugin needs additional extension plugins, each  specific to a source
" control system, to function.  Those plugins should be placed in a
" subdirectory of the standard plugin directory named 'vcscommand'.  Several
" options include the name of the version control system in the option name.
" Such options use the placeholder text '{VCSType}', which would be replaced
" in actual usage with 'CVS' or 'SVN', for instance.
"
" Command documentation {{{2
"
" VCSAdd           Adds the current file to source control.
"
" VCSAnnotate      Displays the current file with each line annotated with the
"                  version in which it was most recently changed.  If an
"                  argument is given, the argument is used as a revision
"                  number to display.  If not given an argument, it uses the
"                  most recent version of the file on the current branch.
"                  Additionally, if the current buffer is a VCSAnnotate buffer
"                  already, the version number on the current line is used.
"
" VCSCommit[!]     Commits changes to the current file to source control.
"
"                  If called with arguments, the arguments are the log message.
"
"                  If '!' is used, an empty log message is committed.
"
"                  If called with no arguments, this is a two-step command.
"                  The first step opens a buffer to accept a log message.
"                  When that buffer is written, it is automatically closed and
"                  the file is committed using the information from that log
"                  message.  The commit can be abandoned if the log message
"                  buffer is deleted or wiped before being written.
"
" VCSDiff          With no arguments, this displays the differences between
"                  the current file and its parent version under source
"                  control in a new scratch buffer.
"
"                  With one argument, the diff is performed on the
"                  current file against the specified revision.
"
"                  With two arguments, the diff is performed between the
"                  specified revisions of the current file.
"
"                  This command uses the 'VCSCommand{VCSType}DiffOpt' variable
"                  to specify diff options.  If that variable does not exist,
"                  a plugin-specific default is used.  If you wish to have no
"                  options, then set it to the empty string.
"
" VCSGotoOriginal  Jumps to the source buffer if the current buffer is a VCS
"                  scratch buffer.  If VCSGotoOriginal[!] is used, remove all
"                  VCS scratch buffers associated with the original file.
"
" VCSLock          Locks the current file in order to prevent other users from
"                  concurrently modifying it.  The exact semantics of this
"                  command depend on the underlying VCS.
"
" VCSLog           Displays the version history of the current file in a new
"                  scratch buffer.
"
" VCSRevert        Replaces the modified version of the current file with the
"                  most recent version from the repository.
"
" VCSReview        Displays a particular version of the current file in a new
"                  scratch buffer.  If no argument is given, the most recent
"                  version of the file on the current branch is retrieved.
"
" VCSStatus        Displays versioning information about the current file in a
"                  new scratch buffer.
"
" VCSUnlock        Unlocks the current file in order to allow other users from
"                  concurrently modifying it.  The exact semantics of this
"                  command depend on the underlying VCS.
"
" VCSUpdate        Updates the current file with any relevant changes from the
"                  repository.
"
" VCSVimDiff       Uses vimdiff to display differences between versions of the
"                  current file.
"
"                  If no revision is specified, the most recent version of the
"                  file on the current branch is used.  With one argument,
"                  that argument is used as the revision as above.  With two
"                  arguments, the differences between the two revisions is
"                  displayed using vimdiff.
"
"                  With either zero or one argument, the original buffer is
"                  used to perform the vimdiff.  When the scratch buffer is
"                  closed, the original buffer will be returned to normal
"                  mode.
"
"                  Once vimdiff mode is started using the above methods,
"                  additional vimdiff buffers may be added by passing a single
"                  version argument to the command.  There may be up to 4
"                  vimdiff buffers total.
"
"                  Using the 2-argument form of the command resets the vimdiff
"                  to only those 2 versions.  Additionally, invoking the
"                  command on a different file will close the previous vimdiff
"                  buffers.
"
" 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:
"
" nmap ,ca <Plug>VCSAdd
"
" The default mappings are as follow:
"
"   <Leader>ca VCSAdd
"   <Leader>cn VCSAnnotate
"   <Leader>cc VCSCommit
"   <Leader>cd VCSDiff
"   <Leader>cg VCSGotoOriginal
"   <Leader>cG VCSGotoOriginal!
"   <Leader>cl VCSLog
"   <Leader>cL VCSLock
"   <Leader>cr VCSReview
"   <Leader>cs VCSStatus
"   <Leader>cu VCSUpdate
"   <Leader>cU VCSUnlock
"   <Leader>cv VCSVimDiff
"
" Options documentation: {{{2
"
" Several variables are checked by the script to determine behavior as follow:
"
" VCSCommandCommitOnWrite
"   This variable, if set to a non-zero value, causes the pending commit to
"   take place immediately as soon as the log message buffer is written.  If
"   set to zero, only the VCSCommit mapping will cause the pending commit to
"   occur.  If not set, it defaults to 1.
"
" VCSCommandDeleteOnHide
"   This variable, if set to a non-zero value, causes the temporary VCS result
"   buffers to automatically delete themselves when hidden.
"
" VCSCommand{VCSType}DiffOpt
"   This variable, if set, determines the options passed to the diff command
"   of the underlying VCS.  Each VCS plugin defines a default value.
"
" VCSCommandDiffSplit
"   This variable overrides the VCSCommandSplit variable, but only for buffers
"   created with VCSVimDiff.
"
" VCSCommandEdit
"   This variable controls whether to split the current window to display a
"   scratch buffer ('split'), or to display it in the current buffer ('edit').
"   If not set, it defaults to 'split'.
"
" VCSCommandEnableBufferSetup
"   This variable, if set to a non-zero value, activates VCS buffer management
"   mode.  This mode means that the buffer variable 'VCSRevision' is set if
"   the file is VCS-controlled.  This is useful for displaying version
"   information in the status bar.  Additional options may be set by
"   individual VCS plugins.
"
" VCSCommandSplit
"   This variable controls the orientation of the various window splits that
"   may occur (such as with VCSVimDiff, when using a VCS command on a VCS
"   command buffer, or when the 'VCSCommandEdit' variable is set to 'split'.
"   If set to 'horizontal', the resulting windows will be on stacked on top of
"   one another.  If set to 'vertical', the resulting windows will be
"   side-by-side.  If not set, it defaults to 'horizontal' for all but
"   VCSVimDiff windows.
"
" Event documentation {{{2
"   For additional customization, VCSCommand.vim uses User event autocommand
"   hooks.  Each event is in the VCSCommand group, and different patterns
"   match the various hooks.
"
"   For instance, the following could be added to the vimrc to provide a 'q'
"   mapping to quit a VCS scratch buffer:
"
"   augroup VCSCommand
"     au VCSCommand User VCSBufferCreated silent! nmap <unique> <buffer> q :bwipeout<cr> 
"   augroup END
"
"   The following hooks are available:
"
"   VCSBufferCreated           This event is fired just after a VCS command
"                              output buffer is created.  It is executed
"                              within the context of the new buffer.
"
"   VCSBufferSetup             This event is fired just after VCS buffer setup
"                              occurs, if enabled.
"
"   VCSLoadExtensions          This event is fired just before the
"                              VCSPluginFinish event.  It is intended to be
"                              used only by VCS extensions to register
"                              themselves with VCSCommand if they are sourced
"                              first.
"
"   VCSPluginInit              This event is fired when the VCSCommand plugin
"                              first loads.
"
"   VCSPluginFinish            This event is fired just after the VCSCommand
"                              plugin loads.
"
"   VCSVimDiffFinish           This event is fired just after the VCSVimDiff
"                              command executes to allow customization of,
"                              for instance, window placement and focus.
"
" Section: Plugin header {{{1

" loaded_VCSCommand is set to 1 when the initialization begins, and 2 when it
" completes.  This allows various actions to only be taken by functions after
" system initialization.

if exists('loaded_VCSCommand')
   finish
endif
let loaded_VCSCommand = 1

if v:version < 700
  echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
  finish
endif

" Section: Event group setup {{{1

augroup VCSCommand
augroup END

augroup VCSCommandCommit
augroup END

" Section: Plugin initialization {{{1
silent do VCSCommand User VCSPluginInit

" Section: Script variable initialization {{{1

let s:plugins = {}
let s:pluginFiles = []
let s:extendedMappings = {}
let s:optionOverrides = {}
let s:isEditFileRunning = 0

unlet! s:vimDiffRestoreCmd
unlet! s:vimDiffSourceBuffer
unlet! s:vimDiffScratchList

" Section: Utility functions {{{1

" Function: s:ExecuteExtensionMapping(mapping) {{{2
" Invokes the appropriate extension mapping depending on the type of the
" current buffer.

function! s:ExecuteExtensionMapping(mapping)
  let buffer = bufnr('%')
  let vcsType = VCSCommandGetVCSType(buffer)
  if !has_key(s:extendedMappings, vcsType)
    throw 'Unknown VCS type:  ' . vcsType
  endif
  if !has_key(s:extendedMappings[vcsType], a:mapping)
    throw 'This extended mapping is not defined for ' . vcsType
  endif
  silent execute 'normal' ':' .  s:extendedMappings[vcsType][a:mapping] . "\<CR>"
endfunction

" Function: s:ExecuteVCSCommand(command, buffer, argList) {{{2
" Calls the indicated plugin-specific VCS command on the indicated buffer.
" Returns: buffer number of resulting output scratch buffer, or -1 if an error
" occurs.

function! s:ExecuteVCSCommand(command, buffer, argList)
  try
    let vcsType = VCSCommandGetVCSType(a:buffer)
    if !has_key(s:plugins, vcsType)
      throw 'Unknown VCS type:  ' . vcsType
    endif
    let functionMap = s:plugins[vcsType]
    if !has_key(functionMap, a:command)
      throw 'Command ''' . a:command . ''' not implemented for ' . vcsType
    endif
    return functionMap[a:command](a:argList)
  catch
    echohl WarningMsg|echomsg v:exception|echohl None
    return -1
  endtry
endfunction

" Function: s:CreateCommandBuffer(cmd, cmdName, originalBuffer, statusText) {{{2
" Creates a new scratch buffer and captures the output from execution of the
" given command.  The name of the scratch buffer is returned.

function! s:CreateCommandBuffer(cmd, cmdName, originalBuffer, statusText)
  let output = system(a:cmd)

  " HACK:  if line endings in the repository have been corrupted, the output
  " of the command will be confused.
  let output = substitute(output, "\r", '', 'g')

  " HACK:  CVS diff command does not return proper error codes
  if v:shell_error && (a:cmdName != 'diff' || getbufvar(a:originalBuffer, 'VCSCommandVCSType') != 'CVS')
    if strlen(output) == 0
      throw 'Version control command failed'
    else
      let output = substitute(output, '\n', '  ', 'g')
      throw 'Version control command failed:  ' . output
    endif
  endif
  if strlen(output) == 0
    " Handle case of no output.  In this case, it is important to check the
    " file status, especially since cvs edit/unedit may change the attributes
    " of the file with no visible output.

    checktime
    return 0
  endif

  call s:EditFile(a:cmdName, a:originalBuffer, a:statusText)

  silent 0put=output

  " The last command left a blank line at the end of the buffer.  If the
  " last line is folded (a side effect of the 'put') then the attempt to
  " remove the blank line will kill the last fold.
  "
  " This could be fixed by explicitly detecting whether the last line is
  " within a fold, but I prefer to simply unfold the result buffer altogether.

  if has('folding')
    normal zR
  endif

  $d
  1

  " Define the environment and execute user-defined hooks.

  silent do VCSCommand User VCSBufferCreated
  return bufnr('%')
endfunction

" Function: s:EditFile(command, originalBuffer, statusText) {{{2
" Creates a new buffer of the given name and associates it with the given
" original buffer.

function! s:EditFile(command, originalBuffer, statusText)
  let fileName=bufname(a:originalBuffer)
  let vcsType = getbufvar(a:originalBuffer, 'VCSCommandVCSType')
  let bufferName = vcsType . ' ' . a:command
  if strlen(a:statusText) > 0
    let bufferName .= ' ' . a:statusText
  endif
  let bufferName .= ' ' . fileName
  let counter = 0
  let versionedBufferName = bufferName
  while buflisted(versionedBufferName)
    let counter += 1
    let versionedBufferName = bufferName . ' (' . counter . ')'
  endwhile

  " Protect against useless buffer set-up
  let s:isEditFileRunning += 1
  try
    let editCommand = VCSCommandGetOption('VCSCommandEdit', 'split')
    if editCommand == 'split'
      if VCSCommandGetOption('VCSCommandSplit', 'horizontal') == 'horizontal'
        rightbelow split
      else
        vert rightbelow split
      endif
    endif
    edit `=versionedBufferName`
    let b:VCSCommandCommand = a:command
    let b:VCSCommandOriginalBuffer = a:originalBuffer
    let b:VCSCommandSourceFile = fileName
    let b:VCSCommandVCSType = vcsType
    set buftype=nofile
    set noswapfile
    set filetype=

    if a:statusText != ''
      let b:VCSCommandStatusText = a:statusText
    endif

    if VCSCommandGetOption('VCSCommandDeleteOnHide', 0)
      set bufhidden=delete
    endif
  finally
    let s:isEditFileRunning -= 1
  endtry

endfunction

" Function: s:SetupBuffer() {{{2
" Attempts to set the b:VCSCommandBufferInfo variable

function! s:SetupBuffer()
  if (exists('b:VCSCommandBufferSetup') && b:VCSCommandBufferSetup)
    " This buffer is already set up.
    return
  endif

  if strlen(&buftype) > 0 || !filereadable(@%)
    " No special status for special buffers.
    return
  endif

  if !VCSCommandGetOption('VCSCommandEnableBufferSetup', 0) || s:isEditFileRunning > 0
    unlet! b:VCSCommandBufferSetup
    return
  endif

  try
    let vcsType = VCSCommandGetVCSType(bufnr('%'))
    let b:VCSCommandBufferInfo =  s:plugins[vcsType].GetBufferInfo()
    silent do VCSCommand User VCSBufferSetup
  catch /No suitable plugin/
    " This is not a VCS-controlled file.
    let b:VCSCommandBufferInfo = []
  endtry

  let b:VCSCommandBufferSetup=1
endfunction

" Function: s:MarkOrigBufferForSetup(buffer) {{{2
" Resets the buffer setup state of the original buffer for a given VCS scratch
" buffer.
" Returns:  The VCS buffer number in a passthrough mode.

function! s:MarkOrigBufferForSetup(buffer)
  checktime
  if a:buffer > 0 
    let origBuffer = VCSCommandGetOriginalBuffer(a:buffer)
    " This should never not work, but I'm paranoid
    if origBuffer != a:buffer
      call setbufvar(origBuffer, 'VCSCommandBufferSetup', 0)
    endif
  endif
  return a:buffer
endfunction

" Function: s:OverrideOption(option, [value]) {{{2
" Provides a temporary override for the given VCS option.  If no value is
" passed, the override is disabled.

function! s:OverrideOption(option, ...)
  if a:0 == 0
    call remove(s:optionOverrides[a:option], -1)
  else
    if !has_key(s:optionOverrides, a:option)
      let s:optionOverrides[a:option] = []
    endif
    call add(s:optionOverrides[a:option], a:1)
  endif
endfunction

" Function: s:WipeoutCommandBuffers() {{{2
" Clears all current VCS output buffers of the specified type for a given source.

function! s:WipeoutCommandBuffers(originalBuffer, VCSCommand)
  let buffer = 1
  while buffer <= bufnr('$')
    if getbufvar(buffer, 'VCSCommandOriginalBuffer') == a:originalBuffer
      if getbufvar(buffer, 'VCSCommandCommand') == a:VCSCommand
        execute 'bw' buffer
      endif
    endif
    let buffer = buffer + 1
  endwhile
endfunction

function! s:VimDiffRestore(vimDiffBuff)
  let s:isEditFileRunning += 1
  try
    if exists('s:vimDiffSourceBuffer')
      if a:vimDiffBuff == s:vimDiffSourceBuffer
        " Original file is being removed.
        unlet! s:vimDiffSourceBuffer
        unlet! s:vimDiffRestoreCmd
        unlet! s:vimDiffScratchList
      else
        let index = index(s:vimDiffScratchList, a:vimDiffBuff)
        if index >= 0
          call remove(s:vimDiffScratchList, index)
          if len(s:vimDiffScratchList) == 0
            if exists('s:vimDiffRestoreCmd')
              " All scratch buffers are gone, reset the original.
              " Only restore if the source buffer is still in Diff mode

              let sourceWinNR=bufwinnr(s:vimDiffSourceBuffer)
              if sourceWinNR != -1
                " The buffer is visible in at least one window
                let currentWinNR = winnr()
                while winbufnr(sourceWinNR) != -1
                  if winbufnr(sourceWinNR) == s:vimDiffSourceBuffer
                    execute sourceWinNR . 'wincmd w'
                    if getwinvar(0, '&diff')
                      execute s:vimDiffRestoreCmd
                    endif
                  endif
                  let sourceWinNR = sourceWinNR + 1
                endwhile
                execute currentWinNR . 'wincmd w'
              else
                " The buffer is hidden.  It must be visible in order to set the
                " diff option.
                let currentBufNR = bufnr('')
                execute 'hide buffer' s:vimDiffSourceBuffer
                if getwinvar(0, '&diff')
                  execute s:vimDiffRestoreCmd
                endif
                execute 'hide buffer' currentBufNR
              endif

              unlet s:vimDiffRestoreCmd
            endif 
            " All buffers are gone.
            unlet s:vimDiffSourceBuffer
            unlet s:vimDiffScratchList
          endif
        endif
      endif
    endif
  finally
    let s:isEditFileRunning -= 1
  endtry
endfunction


" Section: Generic VCS command functions {{{1

" Function: s:VCSCommit() {{{2
function! s:VCSCommit(bang, message)
  let vcsType = VCSCommandGetVCSType(bufnr('%'))
  if !has_key(s:plugins, vcsType)
    throw 'Unknown VCS type:  ' . vcsType
  endif

  let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))

  " Handle the commit message being specified.  If a message is supplied, it
  " is used; if bang is supplied, an empty message is used; otherwise, the
  " user is provided a buffer from which to edit the commit message.

  if strlen(a:message) > 0 || a:bang == '!'
    return s:VCSFinishCommit([a:message], originalBuffer)
  endif

  call s:EditFile('commit log', originalBuffer, '')
  set ft=vcscommit

  " Create a commit mapping.

  nnoremap <silent> <buffer> <Plug>VCSCommit :call <SID>VCSFinishCommitWithBuffer()<CR>

  silent 0put ='VCS: ----------------------------------------------------------------------'
  silent put ='VCS: Please enter log message.  Lines beginning with ''VCS:'' are removed automatically.'
  silent put ='VCS: To finish the commit, Type <leader>cc (or your own <Plug>VCSCommit mapping)'

  if VCSCommandGetOption('VCSCommandCommitOnWrite', 1) == 1
    set buftype=acwrite
    au VCSCommandCommit BufWriteCmd <buffer> call s:VCSFinishCommitWithBuffer()
    silent put ='VCS: or write this buffer'
  endif

  silent put ='VCS: ----------------------------------------------------------------------'
  $
  set nomodified
endfunction

" Function: s:VCSFinishCommitWithBuffer() {{{2
" Wrapper for s:VCSFinishCommit which is called only from a commit log buffer
" which removes all lines starting with 'VCS:'.

function! s:VCSFinishCommitWithBuffer()
  set nomodified
  let currentBuffer = bufnr('%') 
  let logMessageList = getbufline('%', 1, '$')
  call filter(logMessageList, 'v:val !~ ''^\s*VCS:''')
  let resultBuffer = s:VCSFinishCommit(logMessageList, b:VCSCommandOriginalBuffer)
  if resultBuffer >= 0
    execute 'bw' currentBuffer
  endif
  return resultBuffer
endfunction

" Function: s:VCSFinishCommit(logMessageList, originalBuffer) {{{2
function! s:VCSFinishCommit(logMessageList, originalBuffer)
  let shellSlashBak = &shellslash
  try
    set shellslash
    let messageFileName = tempname()
    call writefile(a:logMessageList, messageFileName)
    try
      let resultBuffer = s:ExecuteVCSCommand('Commit', a:originalBuffer, [messageFileName])
      if resultBuffer < 0
        return resultBuffer
      endif
      return s:MarkOrigBufferForSetup(resultBuffer)
    finally
      call delete(messageFileName)
    endtry
  finally
    let &shellslash = shellSlashBak
  endtry
endfunction

" Function: s:VCSGotoOriginal(bang) {{{2
function! s:VCSGotoOriginal(bang)
  let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
  if originalBuffer > 0
    let origWinNR = bufwinnr(originalBuffer)
    if origWinNR == -1
      execute 'buffer' originalBuffer
    else
      execute origWinNR . 'wincmd w'
    endif
    if a:bang == '!'
      let buffnr = 1
      let buffmaxnr = bufnr('$')
      while buffnr <= buffmaxnr
        if getbufvar(buffnr, 'VCSCommandOriginalBuffer') == originalBuffer
          execute 'bw' buffnr
        endif
        let buffnr = buffnr + 1
      endwhile
    endif
  endif
endfunction

" Function: s:VCSVimDiff(...) {{{2
function! s:VCSVimDiff(...)
  let vcsType = VCSCommandGetVCSType(bufnr('%'))
  if !has_key(s:plugins, vcsType)
    throw 'Unknown VCS type:  ' . vcsType
  endif
  let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
  let s:isEditFileRunning = s:isEditFileRunning + 1
  try
    " If there's already a VimDiff'ed window, restore it.
    " There may only be one VCSVimDiff original window at a time.

    if exists('s:vimDiffSourceBuffer') && s:vimDiffSourceBuffer != originalBuffer
      " Clear the existing vimdiff setup by removing the result buffers.
      call s:WipeoutCommandBuffers(s:vimDiffSourceBuffer, 'vimdiff')
    endif

    " Split and diff
    if(a:0 == 2)
      " Reset the vimdiff system, as 2 explicit versions were provided.
      if exists('s:vimDiffSourceBuffer')
        call s:WipeoutCommandBuffers(s:vimDiffSourceBuffer, 'vimdiff')
      endif
      let resultBuffer = s:plugins[vcsType].Review([a:1])
      if resultBuffer < 0
        echomsg 'Can''t open revision ' . a:1
        return resultBuffer
      endif
      let b:VCSCommandCommand = 'vimdiff'
      diffthis
      let s:vimDiffScratchList = [resultBuffer]
      " If no split method is defined, cheat, and set it to vertical.
      try
        call s:OverrideOption('VCSCommandSplit', VCSCommandGetOption('VCSCommandDiffSplit', VCSCommandGetOption('VCSCommandSplit', 'vertical')))
        let resultBuffer=s:plugins[vcsType].Review([a:2])
      finally
        call s:OverrideOption('VCSCommandSplit')
      endtry
      if resultBuffer < 0
        echomsg 'Can''t open revision ' . a:1
        return resultBuffer
      endif
      let b:VCSCommandCommand = 'vimdiff'
      diffthis
      let s:vimDiffScratchList += [resultBuffer]
    else
      " Add new buffer
      call s:OverrideOption('VCSCommandEdit', 'split')
      try
        " Force splitting behavior, otherwise why use vimdiff?
        call s:OverrideOption('VCSCommandSplit', VCSCommandGetOption('VCSCommandDiffSplit', VCSCommandGetOption('VCSCommandSplit', 'vertical')))
        try
          if(a:0 == 0)
            let resultBuffer=s:plugins[vcsType].Review([])
          else
            let resultBuffer=s:plugins[vcsType].Review([a:1])
          endif
        finally
          call s:OverrideOption('VCSCommandSplit')
        endtry
      finally
        call s:OverrideOption('VCSCommandEdit')
      endtry
      if resultBuffer < 0
        echomsg 'Can''t open current revision'
        return resultBuffer
      endif
      let b:VCSCommandCommand = 'vimdiff'
      diffthis

      if !exists('s:vimDiffSourceBuffer')
        " New instance of vimdiff.
        let s:vimDiffScratchList = [resultBuffer]

        " This could have been invoked on a VCS result buffer, not the
        " original buffer.
        wincmd W
        execute 'buffer' originalBuffer
        " Store info for later original buffer restore
        let s:vimDiffRestoreCmd = 
              \    'call setbufvar('.originalBuffer.', ''&diff'', '.getbufvar(originalBuffer, '&diff').')'
              \ . '|call setbufvar('.originalBuffer.', ''&foldcolumn'', '.getbufvar(originalBuffer, '&foldcolumn').')'
              \ . '|call setbufvar('.originalBuffer.', ''&foldenable'', '.getbufvar(originalBuffer, '&foldenable').')'
              \ . '|call setbufvar('.originalBuffer.', ''&foldmethod'', '''.getbufvar(originalBuffer, '&foldmethod').''')'
              \ . '|call setbufvar('.originalBuffer.', ''&scrollbind'', '.getbufvar(originalBuffer, '&scrollbind').')'
              \ . '|call setbufvar('.originalBuffer.', ''&wrap'', '.getbufvar(originalBuffer, '&wrap').')'
              \ . '|if &foldmethod==''manual''|execute ''normal zE''|endif'
        diffthis
        wincmd w
      else
        " Adding a window to an existing vimdiff
        let s:vimDiffScratchList += [resultBuffer]
      endif
    endif

    let s:vimDiffSourceBuffer = originalBuffer

    " Avoid executing the modeline in the current buffer after the autocommand.

    let currentBuffer = bufnr('%')
    let saveModeline = getbufvar(currentBuffer, '&modeline')
    try
      call setbufvar(currentBuffer, '&modeline', 0)
      silent do VCSCommand User VCSVimDiffFinish
    finally
      call setbufvar(currentBuffer, '&modeline', saveModeline)
    endtry
    return resultBuffer
  finally
    let s:isEditFileRunning = s:isEditFileRunning - 1
  endtry
endfunction

" Section: Public functions {{{1

" Function: VCSCommandGetVCSType(buffer) {{{2
" Sets the b:VCSCommandVCSType variable in the current buffer to the
" appropriate source control system name.

function! VCSCommandGetVCSType(buffer)
  let vcsType = getbufvar(a:buffer, 'VCSCommandVCSType')
  if strlen(vcsType) > 0
    return vcsType
  endif
  for vcsType in keys(s:plugins)
    if s:plugins[vcsType].Identify(a:buffer)
      call setbufvar(a:buffer, 'VCSCommandVCSType', vcsType)
      return vcsType
    endif
  endfor
  throw 'No suitable plugin'
endfunction

" Function: VCSCommandChangeToCurrentFileDir() {{{2
" Go to the directory in which the given file is located.

function! VCSCommandChangeToCurrentFileDir(fileName)
  let oldCwd=getcwd()
  let newCwd=fnamemodify(resolve(a:fileName), ':p:h')
  if strlen(newCwd) > 0
    execute 'cd' escape(newCwd, ' ')
  endif
  return oldCwd
endfunction

" Function: VCSCommandGetOriginalBuffer(vcsBuffer) {{{2
" Attempts to locate the original file to which VCS operations were applied
" for a given buffer.

function! VCSCommandGetOriginalBuffer(vcsBuffer)
  let origBuffer = getbufvar(a:vcsBuffer, 'VCSCommandOriginalBuffer')
  if origBuffer
    if bufexists(origBuffer)
      return origBuffer
    else
      " Original buffer no longer exists.
      throw 'Original buffer for this VCS buffer no longer exists.'
    endif
  else
    " No original buffer
    return a:vcsBuffer
  endif
endfunction

" Function: VCSCommandRegisterModule(name, file, commandMap) {{{2
" Allows VCS modules to register themselves.

function! VCSCommandRegisterModule(name, file, commandMap, mappingMap)
  let s:plugins[a:name] = a:commandMap
  call add(s:pluginFiles, a:file)
  let s:extendedMappings[a:name] = a:mappingMap
  if(!empty(a:mappingMap))
    for mapname in keys(a:mappingMap)
      execute 'noremap <silent> <Leader>' . mapname ':call <SID>ExecuteExtensionMapping(''' . mapname . ''')<CR>'
    endfor
  endif
endfunction

" Function: VCSCommandDoCommand(cmd, cmdName, statusText) {{{2
" General skeleton for VCS function execution.
" Returns: name of the new command buffer containing the command results

function! VCSCommandDoCommand(cmd, cmdName, statusText)
  let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
  if originalBuffer == -1 
    throw 'Original buffer no longer exists, aborting.'
  endif

  let fileName=bufname(originalBuffer)

  " Work with netrw or other systems where a directory listing is displayed in
  " a buffer.

  if isdirectory(fileName)
    let realFileName = '.'
  else
    let realFileName = fnamemodify(resolve(fileName), ':t')
  endif

  let oldCwd=VCSCommandChangeToCurrentFileDir(fileName)
  try
    let fullCmd = a:cmd . ' "' . realFileName . '"'
    let resultBuffer=s:CreateCommandBuffer(fullCmd, a:cmdName, originalBuffer, a:statusText)
    return resultBuffer
  finally
    execute 'cd' escape(oldCwd, ' ')
  endtry
endfunction

" Function: VCSCommandGetOption(name, default) {{{2
" Grab a user-specified option to override the default provided.  Options are
" searched in the window, buffer, then global spaces.

function! VCSCommandGetOption(name, default)
  if has_key(s:optionOverrides, a:name) && len(s:optionOverrides[a:name]) > 0
    return s:optionOverrides[a:name][-1]
  elseif exists('w:' . a:name)
    return w:{a:name}
  elseif exists('b:' . a:name)
    return b:{a:name}
  elseif exists('g:' . a:name)
    return g:{a:name}
  else
    return a:default
  endif
endfunction

" Function: VCSCommandGetRevision() {{{2
" Global function for retrieving the current buffer's revision number.
" Returns: Revision number or an empty string if an error occurs.

function! VCSCommandGetRevision()
  if !exists('b:VCSCommandBufferInfo')
    let b:VCSCommandBufferInfo =  s:plugins[VCSCommandGetVCSType(bufnr('%'))].GetBufferInfo()
  endif

  if len(b:VCSCommandBufferInfo) > 0
    return b:VCSCommandBufferInfo[0]
  else
    return ''
  endif
endfunction

" Function: VCSCommandDisableBufferSetup() {{{2
" Global function for deactivating the buffer autovariables.

function! VCSCommandDisableBufferSetup()
  let g:VCSCommandEnableBufferSetup = 0
  silent! augroup! VCSCommandPlugin
endfunction

" Function: VCSCommandEnableBufferSetup() {{{2
" Global function for activating the buffer autovariables.

function! VCSCommandEnableBufferSetup()
  let g:VCSCommandEnableBufferSetup = 1
  augroup VCSCommandPlugin
    au!
    au BufEnter * call s:SetupBuffer()
  augroup END

  " Only auto-load if the plugin is fully loaded.  This gives other plugins a
  " chance to run.
  if g:loaded_VCSCommand == 2
    call s:SetupBuffer()
  endif
endfunction

" Function: VCSCommandGetStatusLine() {{{2
" Default (sample) status line entry for VCS-controlled files.  This is only
" useful if VCS-managed buffer mode is on (see the VCSCommandEnableBufferSetup
" variable for how to do this).

function! VCSCommandGetStatusLine()
  if exists('b:VCSCommandCommand')
    " This is a result buffer.  Return nothing because the buffer name
    " contains information already.
    return ''
  endif

  if exists('b:VCSCommandVCSType')
        \ && exists('g:VCSCommandEnableBufferSetup')
        \ && g:VCSCommandEnableBufferSetup
        \ && exists('b:VCSCommandBufferInfo')
    return '[' . join(extend([b:VCSCommandVCSType], b:VCSCommandBufferInfo), ' ') . ']'
  else
    return ''
  endif
endfunction

" Section: Command definitions {{{1
" Section: Primary commands {{{2
com! -nargs=0 VCSAdd call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Add', bufnr('%'), []))
com! -nargs=? VCSAnnotate call s:ExecuteVCSCommand('Annotate', bufnr('%'), [<f-args>])
com! -nargs=? -bang VCSCommit call s:VCSCommit(<q-bang>, <q-args>)
com! -nargs=* VCSDiff call s:ExecuteVCSCommand('Diff', bufnr('%'), [<f-args>])
com! -nargs=0 -bang VCSGotoOriginal call s:VCSGotoOriginal(<q-bang>)
com! -nargs=0 VCSLock call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Lock', bufnr('%'), []))
com! -nargs=? VCSLog call s:ExecuteVCSCommand('Log', bufnr('%'), [<f-args>])
com! -nargs=0 VCSRevert call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Revert', bufnr('%'), []))
com! -nargs=? VCSReview call s:ExecuteVCSCommand('Review', bufnr('%'), [<f-args>])
com! -nargs=0 VCSStatus call s:ExecuteVCSCommand('Status', bufnr('%'), [])
com! -nargs=0 VCSUnlock call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Unlock', bufnr('%'), []))
com! -nargs=0 VCSUpdate call s:MarkOrigBufferForSetup(s:ExecuteVCSCommand('Update', bufnr('%'), []))
com! -nargs=* VCSVimDiff call s:VCSVimDiff(<f-args>)

" Section: VCS buffer management commands {{{2
com! VCSCommandDisableBufferSetup call VCSCommandDisableBufferSetup()
com! VCSCommandEnableBufferSetup call VCSCommandEnableBufferSetup()

" Allow reloading VCSCommand.vim
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

" Section: Plugin command mappings {{{1
nnoremap <silent> <Plug>VCSAdd :VCSAdd<CR>
nnoremap <silent> <Plug>VCSAnnotate :VCSAnnotate<CR>
nnoremap <silent> <Plug>VCSCommit :VCSCommit<CR>
nnoremap <silent> <Plug>VCSDiff :VCSDiff<CR>
nnoremap <silent> <Plug>VCSGotoOriginal :VCSGotoOriginal<CR>
nnoremap <silent> <Plug>VCSClearAndGotoOriginal :VCSGotoOriginal!<CR>
nnoremap <silent> <Plug>VCSLock :VCSLock<CR>
nnoremap <silent> <Plug>VCSLog :VCSLog<CR>
nnoremap <silent> <Plug>VCSRevert :VCSRevert<CR>
nnoremap <silent> <Plug>VCSReview :VCSReview<CR>
nnoremap <silent> <Plug>VCSStatus :VCSStatus<CR>
nnoremap <silent> <Plug>VCSUnlock :VCSUnlock<CR>
nnoremap <silent> <Plug>VCSUpdate :VCSUpdate<CR>
nnoremap <silent> <Plug>VCSVimDiff :VCSVimDiff<CR>

" Section: Default mappings {{{1
if !hasmapto('<Plug>VCSAdd')
  nmap <unique> <Leader>ca <Plug>VCSAdd
endif
if !hasmapto('<Plug>VCSAnnotate')
  nmap <unique> <Leader>cn <Plug>VCSAnnotate
endif
if !hasmapto('<Plug>VCSClearAndGotoOriginal')
  nmap <unique> <Leader>cG <Plug>VCSClearAndGotoOriginal
endif
if !hasmapto('<Plug>VCSCommit')
  nmap <unique> <Leader>cc <Plug>VCSCommit
endif
if !hasmapto('<Plug>VCSDiff')
  nmap <unique> <Leader>cd <Plug>VCSDiff
endif
if !hasmapto('<Plug>VCSGotoOriginal')
  nmap <unique> <Leader>cg <Plug>VCSGotoOriginal
endif
if !hasmapto('<Plug>VCSLock')
  nmap <unique> <Leader>cL <Plug>VCSLock
endif
if !hasmapto('<Plug>VCSLog')
  nmap <unique> <Leader>cl <Plug>VCSLog
endif
if !hasmapto('<Plug>VCSRevert')
  nmap <unique> <Leader>cq <Plug>VCSRevert
endif
if !hasmapto('<Plug>VCSReview')
  nmap <unique> <Leader>cr <Plug>VCSReview
endif
if !hasmapto('<Plug>VCSStatus')
  nmap <unique> <Leader>cs <Plug>VCSStatus
endif
if !hasmapto('<Plug>VCSUnlock')
  nmap <unique> <Leader>cU <Plug>VCSUnlock
endif
if !hasmapto('<Plug>VCSUpdate')
  nmap <unique> <Leader>cu <Plug>VCSUpdate
endif
if !hasmapto('<Plug>VCSVimDiff')
  nmap <unique> <Leader>cv <Plug>VCSVimDiff
endif

" Section: Menu items {{{1
amenu <silent> &Plugin.VCS.&Add        <Plug>VCSAdd
amenu <silent> &Plugin.VCS.A&nnotate   <Plug>VCSAnnotate
amenu <silent> &Plugin.VCS.&Commit     <Plug>VCSCommit
amenu <silent> &Plugin.VCS.&Diff       <Plug>VCSDiff
amenu <silent> &Plugin.VCS.&Log        <Plug>VCSLog
amenu <silent> &Plugin.VCS.Revert      <Plug>VCSRevert
amenu <silent> &Plugin.VCS.&Review     <Plug>VCSReview
amenu <silent> &Plugin.VCS.&Status     <Plug>VCSStatus
amenu <silent> &Plugin.VCS.&Update     <Plug>VCSUpdate
amenu <silent> &Plugin.VCS.&VimDiff    <Plug>VCSVimDiff

" Section: Autocommands to restore vimdiff state {{{1
augroup VimDiffRestore
  au!
  au BufUnload * call s:VimDiffRestore(str2nr(expand('<abuf>')))
augroup END

" Section: Optional activation of buffer management {{{1

if VCSCommandGetOption('VCSCommandEnableBufferSetup', 0)
  call VCSCommandEnableBufferSetup()
endif

" Section: Plugin completion {{{1

let loaded_VCSCommand=2

" Load delayed extension plugin registration.
silent do VCSCommand User VCSLoadExtensions
au! VCSCommand User VCSLoadExtensions

silent do VCSCommand User VCSPluginFinish