forked from WolfgangMehner/vim-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-support.vim
More file actions
4299 lines (4299 loc) · 135 KB
/
git-support.vim
File metadata and controls
4299 lines (4299 loc) · 135 KB
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
"===============================================================================
"
" File: git-support.vim
"
" Description: Provides access to Git's functionality from inside Vim.
"
" See help file gitsupport.txt .
"
" VIM Version: 7.0+
" Author: Wolfgang Mehner, [email protected]
" Organization:
" Version: see variable g:GitSupport_Version below
" Created: 06.10.2012
" Revision: 29.12.2013
" License: Copyright (c) 2012-2014, Wolfgang Mehner
" This program is free software; you can redistribute it and/or
" modify it under the terms of the GNU General Public License as
" published by the Free Software Foundation, version 2 of the
" License.
" This program is distributed in the hope that it will be
" useful, but WITHOUT ANY WARRANTY; without even the implied
" warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
" PURPOSE.
" See the GNU General Public License version 2 for more details.
"===============================================================================
"
"-------------------------------------------------------------------------------
" Basic checks. {{{1
"-------------------------------------------------------------------------------
"
" need at least 7.0
if v:version < 700
echohl WarningMsg
echo 'The plugin git-support.vim needs Vim version >= 7.'
echohl None
finish
endif
"
" prevent duplicate loading
" need compatible
if &cp || ( exists('g:GitSupport_Version') && ! exists('g:GitSupport_DevelopmentOverwrite') )
finish
endif
let g:GitSupport_Version= '0.9.3pre' " version number of this script; do not change
"
"-------------------------------------------------------------------------------
" Auxiliary functions. {{{1
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" s:ApplyDefaultSetting : Write default setting to a global variable. {{{2
"
" Parameters:
" varname - name of the variable (string)
" value - default value (string)
" Returns:
" -
"
" If g:<varname> does not exists, assign:
" g:<varname> = value
"-------------------------------------------------------------------------------
"
function! s:ApplyDefaultSetting ( varname, value )
if ! exists ( 'g:'.a:varname )
exe 'let g:'.a:varname.' = '.string( a:value )
endif
endfunction " ---------- end of function s:ApplyDefaultSetting ----------
"
"-------------------------------------------------------------------------------
" s:AssembleCmdLine : Assembles a cmd-line with the cursor in the right place. {{{2
"
" Parameters:
" part1 - part left of the cursor (string)
" part2 - part right of the cursor (string)
" left - used to move the cursor left (string, optional)
" Returns:
" cmd_line - the command line (string)
"-------------------------------------------------------------------------------
"
function! s:AssembleCmdLine ( part1, part2, ... )
if a:0 == 0 || a:1 == ''
let left = "\<Left>"
else
let left = a:1
endif
return a:part1.a:part2.repeat( left, s:UnicodeLen( a:part2 ) )
endfunction " ---------- end of function s:AssembleCmdLine ----------
"
"-------------------------------------------------------------------------------
" s:ChangeCWD : Check the buffer and the CWD. {{{2
"
" Parameters:
" [ bufnr, dir ] - data (list: integer and string, optional)
" Returns:
" -
"
" Example:
" First check the current working directory:
" let data = s:CheckCWD ()
" then jump to the Git buffer:
" call s:OpenGitBuffer ( 'Git - <name>' )
" then call this function to correctly set the directory of the buffer:
" call s:ChangeCWD ( data )
"
" Usage:
" The function s:CheckCWD queries the working directory of the buffer your
" starting out in, which is the buffer where you called the Git command. The
" call to s:OpenGitBuffer then opens the requested buffer or jumps to it if it
" already exists. Finally, s:ChangeCWD sets the working directory of the Git
" buffer.
" The buffer 'data' is a list, containing first the number of the current buffer
" at the time s:CheckCWD was called, and second the name of the directory.
"
" When called without parameters, changes to the directory stored in
" 'b:GitSupport_CWD'.
"-------------------------------------------------------------------------------
"
function! s:ChangeCWD ( ... )
"
" call originated from outside the Git buffer?
" also the case for a new buffer
if a:0 == 0
if ! exists ( 'b:GitSupport_CWD' )
call s:ErrorMsg ( 'Not inside a Git buffer.' )
return
endif
elseif bufnr('%') != a:1[0]
"echomsg '2 - call from outside: '.a:1[0]
let b:GitSupport_CWD = a:1[1]
else
"echomsg '2 - call from inside: '.bufnr('%')
" noop
endif
"
" change buffer
"echomsg '3 - changing to: '.b:GitSupport_CWD
exe 'lchdir '.fnameescape( b:GitSupport_CWD )
endfunction " ---------- end of function s:ChangeCWD ----------
"
"-------------------------------------------------------------------------------
" s:CheckCWD : Check the buffer and the CWD. {{{2
"
" Parameters:
" -
" Returns:
" [ bufnr, dir ] - data (list: integer and string)
"
" Usage: see s:ChangeCWD
"-------------------------------------------------------------------------------
"
function! s:CheckCWD ()
"echomsg '1 - calling from: '.getcwd()
return [ bufnr('%'), getcwd() ]
endfunction " ---------- end of function s:CheckCWD ----------
"
"-------------------------------------------------------------------------------
" s:ErrorMsg : Print an error message. {{{2
"
" Parameters:
" line1 - a line (string)
" line2 - a line (string)
" ... - ...
" Returns:
" -
"-------------------------------------------------------------------------------
"
function! s:ErrorMsg ( ... )
echohl WarningMsg
for line in a:000
echomsg line
endfor
echohl None
endfunction " ---------- end of function s:ErrorMsg ----------
"
"-------------------------------------------------------------------------------
" s:EscapeCurrent : Escape the name of the current file for the shell, {{{2
" and prefix it with "--".
"
" Parameters:
" -
" Returns:
" file_argument - the escaped filename (string)
"-------------------------------------------------------------------------------
"
function! s:EscapeCurrent ()
return '-- '.shellescape ( expand ( '%' ) )
endfunction " ---------- end of function s:EscapeCurrent ----------
"
"-------------------------------------------------------------------------------
" s:GetGlobalSetting : Get a setting from a global variable. {{{2
"
" Parameters:
" varname - name of the variable (string)
" Returns:
" -
"
" If g:<varname> exists, assign:
" s:<varname> = g:<varname>
"-------------------------------------------------------------------------------
"
function! s:GetGlobalSetting ( varname )
if exists ( 'g:'.a:varname )
exe 'let s:'.a:varname.' = g:'.a:varname
endif
endfunction " ---------- end of function s:GetGlobalSetting ----------
"
"-------------------------------------------------------------------------------
" s:GitCmdLineArgs : Split command-line parameters into a list. {{{2
"
" Parameters:
" args - the arguments in one string (string)
" Returns:
" [ <arg1>, <arg2>, ... ] - the split arguments (list of strings)
"
" In case of an error, a list with one empty string is returned:
" [ '' ]
"-------------------------------------------------------------------------------
"
function! s:GitCmdLineArgs ( args )
"
let [ sh_err, text ] = s:StandardRun ( 'rev-parse', '-- '.a:args, 't' )
"
if sh_err == 0
return split ( text, '\n' )
else
call s:ErrorMsg ( "Can not parse the command line arguments:\n\n".text )
return [ '' ]
endif
"
endfunction " ---------- end of function s:GitCmdLineArgs ----------
"
"-------------------------------------------------------------------------------
" s:GitGetConfig : Get an option. {{{2
"
" Parameters:
" option - name of the option (string)
" source - where to get the option from (string, optional)
" Returns:
" value - the value of the option (string)
"
" The possible sources are:
" 'local' - current repository
" 'global' - global settings
" 'system' - system settings
"-------------------------------------------------------------------------------
"
function! s:GitGetConfig ( option, ... )
"
let args = ''
"
if a:0 > 0
if a:1 == ''
" noop
elseif a:1 == 'local'
" noop
elseif a:1 == 'global'
let args = '--global '
elseif a:1 == 'system'
let args = '--system '
else
call s:ErrorMsg ( "Unknown option: ".a:1 )
return ''
endif
endif
"
let args .= '--get '.a:option
"
let [ sh_err, text ] = s:StandardRun ( 'config', args, 't' )
"
" from the help:
" the section or key is invalid (ret=1)
if sh_err == 1 || text == ''
if has_key ( s:Config_DefaultValues, a:option )
return s:Config_DefaultValues[ a:option ]
else
return ''
endif
elseif sh_err == 0
return text
else
call s:ErrorMsg ( "Can not query the option: ".text )
return ''
endif
"
endfunction " ---------- end of function s:GitGetConfig ----------
"
"-------------------------------------------------------------------------------
" s:GitRepoDir : Get the base directory of a repository. {{{2
"
" Parameters:
" file - get another path than the top-level directory (string, optional)
" Returns:
" path - the name of the base directory (string)
"
" The possible options for 'file' are:
" 'top' - the top-level directory (default)
" 'git/<file>' - a file in the git directory <top-level>/.git/<file>,
" respects $GIT_DIR
"-------------------------------------------------------------------------------
"
function! s:GitRepoDir ( ... )
"
let get_cmd = 'rev-parse'
let get_arg = '--show-toplevel'
let postfix = ''
"
let dir = 'top'
"
if a:0 == 0 || a:1 == '' || a:1 == 'top'
let [ sh_err, text ] = s:StandardRun ( 'rev-parse', '--show-toplevel', 't' )
elseif a:1 =~ '^git/'
let dir = a:1
let [ sh_err, text ] = s:StandardRun ( 'rev-parse', '--git-dir', 't' )
"
if sh_err == 0
let text = substitute ( a:1, 'git', escape( text, '\&' ), '' )
endif
else
call s:ErrorMsg ( "Unknown option: ".a:1 )
return ''
endif
"
if sh_err == 0
return fnamemodify ( text, ':p' )
else
call s:ErrorMsg ( "Can not query the directory \"".dir."\":","",text )
return ''
endif
"
endfunction " ---------- end of function s:GitRepoDir ----------
"
"-------------------------------------------------------------------------------
" s:ImportantMsg : Print an important message. {{{2
"
" Parameters:
" line1 - a line (string)
" line2 - a line (string)
" ... - ...
" Returns:
" -
"-------------------------------------------------------------------------------
"
function! s:ImportantMsg ( ... )
echohl Search
echo join ( a:000, "\n" )
echohl None
endfunction " ---------- end of function s:ImportantMsg ----------
"
"-------------------------------------------------------------------------------
" s:OpenFile : Open a file or jump to its window. {{{2
"
" Parameters:
" filename - the name of the file (string)
" line - line number (integer, optional)
" column - column version number (integer, optional)
" Returns:
" -
"
" If the file is already open, jump to its window. Otherwise open a window
" showing the file. If the line number is given, jump to this line in the
" buffer. If the column number is given, jump to the column.
"-------------------------------------------------------------------------------
"
function! s:OpenFile ( filename, ... )
"
let filename = resolve ( fnamemodify ( a:filename, ':p' ) )
"
if bufwinnr ( '^'.filename.'$' ) == -1
" open buffer
belowright new
exe "edit ".fnameescape( filename )
else
" jump to window
exe bufwinnr( '^'.filename.'$' ).'wincmd w'
endif
"
if a:0 >= 1
" jump to line
let pos = getpos( '.' )
let pos[1] = a:1 " line
if a:0 >= 2
let pos[2] = a:2 " col
endif
call setpos( '.', pos )
endif
"
if foldlevel('.') && g:Git_OpenFoldAfterJump == 'yes'
normal! zv
endif
endfunction " ---------- end of function s:OpenFile ----------
"
"-------------------------------------------------------------------------------
" s:Question : Ask the user a question. {{{2
"
" Parameters:
" prompt - prompt, shown to the user (string)
" highlight - "normal" or "warning" (string, default "normal")
" Returns:
" retval - the user input (integer)
"
" The possible values of 'retval' are:
" 1 - answer was yes ("y")
" 0 - answer was no ("n")
" -1 - user aborted ("ESC" or "CTRL-C")
"-------------------------------------------------------------------------------
"
function! s:Question ( text, ... )
"
let ret = -2
"
" highlight prompt
if a:0 == 0 || a:1 == 'normal'
echohl Search
elseif a:1 == 'warning'
echohl Error
else
echoerr 'Unknown option : "'.a:1.'"'
return
endif
"
" question
echo a:text.' [y/n]: '
"
" answer: "y", "n", "ESC" or "CTRL-C"
while ret == -2
let c = nr2char( getchar() )
"
if c == "y"
let ret = 1
elseif c == "n"
let ret = 0
elseif c == "\<ESC>" || c == "\<C-C>"
let ret = -1
endif
endwhile
"
" reset highlighting
echohl None
"
return ret
endfunction " ---------- end of function s:Question ----------
"
"-------------------------------------------------------------------------------
" s:StandardRun : execute 'git <cmd> ...' {{{2
"
" Parameters:
" cmd - the Git command to run (string), this is not the Git executable!
" param - the parameters (string)
" flags - all set flags (string)
" allowed - all allowed flags (string, default: 'cet')
" Returns:
" [ ret, text ] - the status code and text produced by the command (string),
" only if the flag 't' is set
"
" Flags are characters. The parameter 'flags' is a concatenation of all set
" flags, the parameter 'allowed' is a concatenation of all allowed flags.
"
" Flags:
" c - ask for confirmation
" e - expand empty 'param' to current buffer
" t - return the text instead of echoing it
"-------------------------------------------------------------------------------
"
function! s:StandardRun( cmd, param, flags, ... )
"
if a:0 == 0
let flag_check = '[^cet]'
else
let flag_check = '[^'.a:1.']'
endif
"
if a:flags =~ flag_check
return s:ErrorMsg ( 'Unknown flag "'.matchstr( a:flags, flag_check ).'".' )
endif
"
if a:flags =~ 'e' && empty( a:param ) | let param = s:EscapeCurrent()
else | let param = a:param
endif
"
let cmd = s:Git_Executable.' '.a:cmd.' '.param
"
if a:flags =~ 'c' && s:Question ( 'Execute "git '.a:cmd.' '.param.'"?' ) != 1
echo "aborted"
return
endif
"
let text = system ( cmd )
"
if a:flags =~ 't'
return [ v:shell_error, substitute ( text, '\_s*$', '', '' ) ]
elseif v:shell_error != 0
echo "\"".cmd."\" failed:\n\n".text | " failure
elseif text =~ '^\_s*$'
echo "ran successfully" | " success
else
echo "ran successfully:\n".text | " success
endif
"
endfunction " ---------- end of function s:StandardRun ----------
"
"-------------------------------------------------------------------------------
" s:UnicodeLen : Number of characters in a Unicode string. {{{2
"
" Parameters:
" str - a string (string)
" Returns:
" len - the length (integer)
"
" Returns the correct length in the presence of Unicode characters which take
" up more than one byte.
"-------------------------------------------------------------------------------
"
function! s:UnicodeLen ( str )
return len(split(a:str,'.\zs'))
endfunction " ---------- end of function s:UnicodeLen ----------
"
"-------------------------------------------------------------------------------
" s:VersionLess : Compare two version numbers. {{{2
"
" Parameters:
" v1 - 1st version number (string)
" v2 - 2nd version number (string)
" Returns:
" less - true, if v1 < v2 (string)
"-------------------------------------------------------------------------------
"
function! s:VersionLess ( v1, v2 )
"
let l1 = matchlist( a:v1, '^\(\d\+\)\.\(\d\+\)\%(\.\(\d\+\)\)\?\%(\.\(\d\+\)\)\?' )
let l2 = matchlist( a:v2, '^\(\d\+\)\.\(\d\+\)\%(\.\(\d\+\)\)\?\%(\.\(\d\+\)\)\?' )
"
if empty( l1 ) || empty( l2 )
echoerr 'Can not compare version numbers "'.a:v1.'" and "'.a:v2.'".'
return
endif
"
for i in range( 1, 4 )
" all previous numbers have been identical!
if empty(l2[i])
" l1[i] is empty as well or "0" -> versions are the same
" l1[i] is not empty -> v1 can not be less
return 0
elseif empty(l1[i])
" only l1[i] is empty -> v2 must be larger, unless l2[i] is "0"
return l2[i] != 0
elseif str2nr(l1[i]) != str2nr( l2[i] )
return str2nr(l1[i]) < str2nr( l2[i] )
endif
endfor
"
echoerr 'Something went wrong while comparing "'.a:v1.'" and "'.a:v2.'".'
return -1
endfunction " ---------- end of function s:VersionLess ----------
" }}}2
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" Custom menus. {{{1
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" s:GenerateCustomMenu : Generate custom menu entries. {{{2
"
" Parameters:
" prefix - defines the menu the entries will be placed in (string)
" data - custom menu entries (list of lists of strings)
" Returns:
" -
"
" See :help g:Git_CustomMenu for a description of the format 'data' uses.
"-------------------------------------------------------------------------------
"
function! s:GenerateCustomMenu ( prefix, data )
"
for [ entry_l, entry_r, cmd ] in a:data
" escape special characters and assemble entry
let entry_l = escape ( entry_l, ' |\' )
let entry_l = substitute ( entry_l, '\.\.', '\\.', 'g' )
let entry_r = escape ( entry_r, ' .|\' )
"
if entry_r == '' | let entry = a:prefix.'.'.entry_l
else | let entry = a:prefix.'.'.entry_l.'<TAB>'.entry_r
endif
"
if cmd == ''
let cmd = ':'
endif
"
let silent = '<silent> '
"
" prepare command
if cmd =~ '<CURSOR>'
let mlist = matchlist ( cmd, '^\(.\+\)<CURSOR>\(.\{-}\)$' )
let cmd = s:AssembleCmdLine ( mlist[1], mlist[2], '<Left>' )
let silent = ''
elseif cmd =~ '<EXECUTE>$'
let cmd = substitute ( cmd, '<EXECUTE>$', '<CR>', '' )
endif
"
let cmd = substitute ( cmd, '<WORD>', '<cword>', 'g' )
let cmd = substitute ( cmd, '<FILE>', '<cfile>', 'g' )
let cmd = substitute ( cmd, '<BUFFER>', '%', 'g' )
"
exe 'anoremenu '.silent.entry.' '.cmd
exe 'vnoremenu '.silent.entry.' <C-C>'.cmd
endfor
"
endfunction " ---------- end of function s:GenerateCustomMenu ----------
" }}}2
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" Test: Custom cmdline completion. {{{1
"-------------------------------------------------------------------------------
"
" Debug:
let g:GitSupport_LastCmdlineComplete = []
"
"-------------------------------------------------------------------------------
" s:GitS_CmdlineComplete : Git-specific command line completion. {{{2
"-------------------------------------------------------------------------------
"
function! GitS_CmdlineComplete ( ArgLead, CmdLine, CursorPos )
"
let git_cmd = tolower ( matchstr ( a:CmdLine, '^Git\zs\w*' ) )
"
if git_cmd == ''
let git_cmd = matchstr ( a:CmdLine, '^Git\s\+\zs\w*' )
endif
"
" files
let filelist = split ( glob ( a:ArgLead.'*' ), "\n" )
"
for i in range( 0, len(filelist)-1 )
if isdirectory ( filelist[i] )
let filelist[i] .= '/'
endif
endfor
"
" git objects: branched, tags, remotes
let gitlist = []
"
" branches
let gitlist += split ( s:StandardRun ( 'branch', '-a', 't' ), '\_[* ]\+\%(remotes/\)\?' )[1]
"
" tags
let gitlist += split ( s:StandardRun ( 'tag', '', 't' ), "\n" )[1]
"
" remotes
let gitlist += split ( s:StandardRun ( 'remote', '', 't' ), "\n" )[1]
"
call filter ( gitlist, '0 == match ( v:val, "\\V'.escape(a:ArgLead,'\').'" )' )
"
let g:GitSupport_LastCmdlineComplete = [ git_cmd, a:ArgLead, gitlist ]
"
" return filelist
return escape ( join ( filelist + gitlist, "\n" ), ' \?*"' )
"
endfunction " ---------- end of function GitS_CmdlineComplete ----------
" }}}2
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" Modul setup. {{{1
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" command lists, help topics {{{2
"
let s:GitCommands = [
\ 'add', 'add--interactive', 'am', 'annotate', 'apply',
\ 'archive', 'bisect', 'bisect--helper', 'blame', 'branch',
\ 'bundle', 'cat-file', 'check-attr', 'checkout', 'checkout-index',
\ 'check-ref-format', 'cherry', 'cherry-pick', 'citool', 'clean',
\ 'clone', 'commit', 'commit-tree', 'config', 'count-objects',
\ 'credential-cache', 'credential-cache--daemon', 'credential-store', 'daemon', 'describe',
\ 'diff', 'diff-files', 'diff-index', 'difftool', 'difftool--helper',
\ 'diff-tree', 'fast-export', 'fast-import', 'fetch', 'fetch-pack',
\ 'filter-branch', 'fmt-merge-msg', 'for-each-ref', 'format-patch', 'fsck',
\ 'fsck-objects', 'gc', 'get-tar-commit-id', 'grep', 'gui',
\ 'gui--askpass', 'hash-object', 'help', 'http-backend', 'http-fetch',
\ 'http-push', 'imap-send', 'index-pack', 'init', 'init-db',
\ 'instaweb', 'log', 'lost-found', 'ls-files', 'ls-remote',
\ 'ls-tree', 'mailinfo', 'mailsplit', 'merge', 'merge-base',
\ 'merge-file', 'merge-index', 'merge-octopus', 'merge-one-file', 'merge-ours',
\ 'merge-recursive', 'merge-resolve', 'merge-subtree', 'mergetool', 'merge-tree',
\ 'mktag', 'mktree', 'mv', 'name-rev', 'notes',
\ 'pack-objects', 'pack-redundant', 'pack-refs', 'patch-id', 'peek-remote',
\ 'prune', 'prune-packed', 'pull', 'push', 'quiltimport',
\ 'read-tree', 'rebase', 'receive-pack', 'reflog', 'relink',
\ 'remote', 'remote-ext', 'remote-fd', 'remote-ftp', 'remote-ftps',
\ 'remote-http', 'remote-https', 'remote-testgit', 'repack', 'replace',
\ 'repo-config', 'request-pull', 'rerere', 'reset', 'revert',
\ 'rev-list', 'rev-parse', 'rm', 'send-pack', 'shell',
\ 'sh-i18n--envsubst', 'shortlog', 'show', 'show-branch', 'show-index',
\ 'show-ref', 'stage', 'stash', 'status', 'stripspace',
\ 'submodule', 'symbolic-ref', 'tag', 'tar-tree', 'unpack-file',
\ 'unpack-objects', 'update-index', 'update-ref', 'update-server-info', 'upload-archive',
\ 'upload-pack', 'var', 'verify-pack', 'verify-tag', 'web--browse',
\ 'whatchanged', 'write-tree',
\ ]
"
let s:HelpTopics = s:GitCommands + [
\ 'attributes', 'cli', 'core-tutorial', 'cvs-migration', 'diffcore',
\ 'gitk', 'glossary', 'hooks', 'ignore', 'modules',
\ 'namespaces', 'repository-layout', 'tutorial', 'tutorial-2', 'workflows'
\ ]
"
function! GitS_HelpTopicsComplete ( ArgLead, CmdLine, CursorPos )
return filter( copy( s:HelpTopics ), 'v:val =~ "\\V\\<'.escape(a:ArgLead,'\').'\\w\\*"' )
endfunction " ---------- end of function GitS_HelpTopicsComplete ----------
"
" configuration defaults {{{2
" - only defaults which are relevant for Git-Support are listed here
"
let s:Config_DefaultValues = {
\ 'help.format' : 'man',
\ 'status.relativePaths' : 'true'
\ }
"
" platform specifics {{{2
"
let s:MSWIN = has("win16") || has("win32") || has("win64") || has("win95")
let s:UNIX = has("unix") || has("macunix") || has("win32unix")
"
if s:MSWIN
"
"-------------------------------------------------------------------------------
" MS Windows
"-------------------------------------------------------------------------------
"
if match( substitute( expand('<sfile>'), '\\', '/', 'g' ),
\ '\V'.substitute( expand('$HOME'), '\\', '/', 'g' ) ) == 0
" user installation assumed
let s:installation = 'local'
else
" system wide installation
let s:installation = 'system'
endif
"
let s:plugin_dir = substitute( expand('<sfile>:p:h:h'), '\\', '/', 'g' )
"
else
"
"-------------------------------------------------------------------------------
" Linux/Unix
"-------------------------------------------------------------------------------
"
if match( expand('<sfile>'), '\V'.resolve(expand('$HOME')) ) == 0
" user installation assumed
let s:installation = 'local'
else
" system wide installation
let s:installation = 'system'
endif
"
let s:plugin_dir = expand('<sfile>:p:h:h')
"
endif
"
" settings {{{2
"
let s:Git_LoadMenus = 'yes' " load the menus?
let s:Git_RootMenu = '&Git' " name of the root menu
"
let s:Git_CmdLineOptionsFile = s:plugin_dir.'/git-support/data/options.txt'
"
if ! exists ( 's:MenuVisible' )
let s:MenuVisible = 0 " menus are not visible at the moment
endif
"
let s:Git_CustomMenu = [
\ [ '&grep, word under cursor', ':GitGrepTop', ':GitGrepTop <WORD><EXECUTE>' ],
\ [ '&grep, version x..y', ':GitGrepTop', ':GitGrepTop -i "Version[^[:digit:]]\+<CURSOR>"' ],
\ [ '-SEP1-', '', '' ],
\ [ '&log, grep commit msg..', ':GitLog', ':GitLog -i --grep="<CURSOR>"' ],
\ [ '&log, grep diff word', ':GitLog', ':GitLog -p -S "<CURSOR>"' ],
\ [ '&log, grep diff line', ':GitLog', ':GitLog -p -G "<CURSOR>"' ],
\ [ '-SEP2-', '', '' ],
\ [ '&merge, fast-forward only', ':GitMerge', ':GitMerge --ff-only <CURSOR>' ],
\ [ '&merge, no commit', ':GitMerge', ':GitMerge --no-commit <CURSOR>' ],
\ [ '&merge, abort', ':GitMerge', ':GitMerge --abort<EXECUTE>' ],
\ ]
"
if s:MSWIN
let s:Git_BinPath = 'C:\Program Files\Git\bin\'
else
let s:Git_BinPath = ''
endif
"
call s:GetGlobalSetting ( 'Git_BinPath' )
"
if s:MSWIN
let s:Git_BinPath = substitute ( s:Git_BinPath, '[^\\/]$', '&\\', '' )
"
let s:Git_Executable = s:Git_BinPath.'git.exe' " Git executable
let s:Git_GitKExecutable = s:Git_BinPath.'tclsh.exe' " GitK executable
let s:Git_GitKScript = s:Git_BinPath.'gitk' " GitK script
else
let s:Git_BinPath = substitute ( s:Git_BinPath, '[^\\/]$', '&/', '' )
"
let s:Git_Executable = s:Git_BinPath.'git' " Git executable
let s:Git_GitKExecutable = s:Git_BinPath.'gitk' " GitK executable
let s:Git_GitKScript = '' " GitK script (do not specify separate script by default)
endif
"
call s:GetGlobalSetting ( 'Git_Executable' )
call s:GetGlobalSetting ( 'Git_GitKExecutable' )
call s:GetGlobalSetting ( 'Git_GitKScript' )
call s:GetGlobalSetting ( 'Git_LoadMenus' )
call s:GetGlobalSetting ( 'Git_RootMenu' )
call s:GetGlobalSetting ( 'Git_CustomMenu' )
"
call s:ApplyDefaultSetting ( 'Git_CheckoutExpandEmpty', 'no' )
call s:ApplyDefaultSetting ( 'Git_DiffExpandEmpty', 'no' )
call s:ApplyDefaultSetting ( 'Git_ResetExpandEmpty', 'no' )
call s:ApplyDefaultSetting ( 'Git_OpenFoldAfterJump', 'yes' )
call s:ApplyDefaultSetting ( 'Git_StatusStagedOpenDiff', 'cached' )
"
let s:Enabled = 1 " Git enabled?
let s:DisabledMessage = "Git-Support not working:"
let s:DisabledReason = ""
"
let s:EnabledGitK = 1 " gitk enabled?
let s:DisableGitKMessage = "gitk not avaiable:"
let s:DisableGitKReason = ""
"
let s:EnabledGitBash = 1 " git bash enabled?
let s:DisableGitBashMessage = "git bash not avaiable:"
let s:DisableGitBashReason = ""
"
let s:FoundGitKScript = 1
let s:GitKScriptReason = ""
"
let s:GitVersion = "" " Git Version
let s:GitHelpFormat = "" " 'man' or 'html'
"
" git bash
if s:MSWIN
let s:Git_GitBashExecutable = s:Git_BinPath.'sh.exe'
call s:GetGlobalSetting ( 'Git_GitBashExecutable' )
else
if exists ( 'g:Xterm_Executable' )
let s:Git_GitBashExecutable = g:Xterm_Executable
else
let s:Git_GitBashExecutable = 'xterm'
endif
call s:GetGlobalSetting ( 'Git_GitBashExecutable' )
call s:ApplyDefaultSetting ( 'Xterm_Options', '-fa courier -fs 12 -geometry 80x24' )
endif
"
" check git executable {{{2
"
function! s:CheckExecutable ( name, exe )
"
let executable = a:exe
let enabled = 1
let reason = ""
"
if executable =~ '^LANG=\w\+\s.'
let [ lang, executable ] = matchlist ( executable, '^\(LANG=\w\+\)\s\+\(.\+\)$' )[1:2]
if ! executable ( executable )
let enabled = 0
let reason = a:name." not executable: ".executable
endif
let executable = lang.' '.shellescape( executable )
elseif executable =~ '^\(["'']\)\zs.\+\ze\1'
if ! executable ( matchstr ( executable, '^\(["'']\)\zs.\+\ze\1' ) )
let enabled = 0
let reason = a:name." not executable: ".executable
endif
else
if ! executable ( executable )
let enabled = 0
let reason = a:name." not executable: ".executable
endif
let executable = shellescape( executable )
endif
"
return [ executable, enabled, reason ]
endfunction " ---------- end of function s:CheckExecutable ----------
"
function! s:CheckFile ( shortname, filename, esc )
"
let filename = a:filename
let found = 1
let message = ""
"
if ! filereadable ( filename )
let found = 0
let message = a:shortname." not found: ".filename
endif
let filename = shellescape( filename )
"
return [ filename, found, message ]
endfunction " ---------- end of function s:CheckFile ----------
"
let [ s:Git_Executable, s:Enabled, s:DisabledReason ] = s:CheckExecutable( 'git', s:Git_Executable )
let [ s:Git_GitKExecutable, s:EnabledGitK, s:DisableGitKReason ] = s:CheckExecutable( 'gitk', s:Git_GitKExecutable )
if ! empty ( s:Git_GitKScript )
let [ s:Git_GitKScript, s:FoundGitKScript, s:GitKScriptReason ] = s:CheckFile( 'gitk script', s:Git_GitKScript, 1 )
endif
let [ s:Git_GitBashExecutable, s:EnabledGitBash, s:DisableGitBashReason ] = s:CheckExecutable ( 'git bash', s:Git_GitBashExecutable )
"
" check Git version {{{2
"
" added in 1.7.2:
" - "git status --ignored"
" - "git status -s -b"
let s:HasStatusIgnore = 0
let s:HasStatusBranch = 0
"
" changed in 1.8.5:
" - output of "git status" without leading "#" char.
let s:HasStatus185Format = 0
"
if s:Enabled
let s:GitVersion = s:StandardRun( '', ' --version', 't' )[1]
if s:GitVersion =~ 'git version [0-9.]\+'
let s:GitVersion = matchstr( s:GitVersion, 'git version \zs[0-9.]\+' )
"
if ! s:VersionLess ( s:GitVersion, '1.7.2' )
let s:HasStatusIgnore = 1
let s:HasStatusBranch = 1
endif
"
if ! s:VersionLess ( s:GitVersion, '1.8.5' )
let s:HasStatus185Format = 1
endif
"
else
call s:ErrorMsg ( 'Can not obtain the version number of Git.' )
endif
endif
"
" check Git help.format {{{2
"
if s:Enabled
let s:GitHelpFormat = s:GitGetConfig( 'help.format' )
"
if s:GitHelpFormat == 'web'
let s:GitHelpFormat = 'html'
endif
endif
"
" standard help text {{{2
"
let s:HelpTxtStd = "S-F1 : help\n"
let s:HelpTxtStd .= "q : close\n"
let s:HelpTxtStd .= "u : update"
"
let s:HelpTxtStdNoUpdate = "S-F1 : help\n"
let s:HelpTxtStdNoUpdate .= "q : close"
"
" custom commands {{{2
"
if s:Enabled
command! -nargs=* -complete=file -bang GitAdd :call GitS_Add(<q-args>,'<bang>'=='!'?'ef':'e')
command! -nargs=* -complete=file -range=0 GitBlame :call GitS_Blame('update',<q-args>,<line1>,<line2>)
command! -nargs=* -complete=file GitBranch :call GitS_Branch(<q-args>,'')
command! -nargs=* -complete=file GitCheckout :call GitS_Checkout(<q-args>,'c')
command! -nargs=* -complete=file GitCommit :call GitS_Commit('direct',<q-args>,'')
command! -nargs=? -complete=file GitCommitFile :call GitS_Commit('file',<q-args>,'')
command! -nargs=0 GitCommitMerge :call GitS_Commit('merge','','')
command! -nargs=+ GitCommitMsg :call GitS_Commit('msg',<q-args>,'')
command! -nargs=* -complete=file GitDiff :call GitS_Diff('update',<q-args>)
command! -nargs=* GitFetch :call GitS_Fetch(<q-args>,'')
command! -nargs=+ -complete=file GitGrep :call GitS_Grep('update',<q-args>)
command! -nargs=+ -complete=file GitGrepTop :call GitS_Grep('top',<q-args>)
command! -nargs=* -complete=customlist,GitS_HelpTopicsComplete GitHelp :call GitS_Help('update',<q-args>)
command! -nargs=* -complete=file GitLog :call GitS_Log('update',<q-args>)
command! -nargs=* GitMerge :call GitS_Merge('direct',<q-args>,'')
command! -nargs=* GitMergeUpstream :call GitS_Merge('upstream',<q-args>,'')
command! -nargs=* -complete=file GitMove :call GitS_Move(<q-args>,'')
command! -nargs=* -complete=file GitMv :call GitS_Move(<q-args>,'')
command! -nargs=* GitPull :call GitS_Pull(<q-args>,'')
command! -nargs=* GitPush :call GitS_Push(<q-args>,'')
command! -nargs=* -complete=file GitRemote :call GitS_Remote(<q-args>,'')
command! -nargs=* -complete=file GitRemove :call GitS_Remove(<q-args>,'e')
command! -nargs=* -complete=file GitRm :call GitS_Remove(<q-args>,'e')
command! -nargs=* -complete=file GitReset :call GitS_Reset(<q-args>,'')
command! -nargs=* -complete=file GitShow :call GitS_Show('update',<q-args>)
command! -nargs=* GitStash :call GitS_Stash(<q-args>,'')
command! -nargs=* GitSlist :call GitS_Stash('list '.<q-args>,'')
command! -nargs=? -complete=file GitStatus :call GitS_Status('update',<q-args>)
command! -nargs=* GitTag :call GitS_Tag(<q-args>,'')
command -nargs=* -complete=file -bang Git :call GitS_Run(<q-args>,'<bang>'=='!'?'b':'')
command! -nargs=* -complete=file GitRun :call GitS_Run(<q-args>,'')
command! -nargs=* -complete=file GitBuf :call GitS_Run(<q-args>,'b')
command! -nargs=* -complete=file GitK :call GitS_GitK(<q-args>)
command! -nargs=* -complete=file GitBash :call GitS_GitBash(<q-args>)
command! -nargs=0 GitSupportHelp :call GitS_PluginHelp("gitsupport")
command! -nargs=? -bang GitSupportSettings :call GitS_PluginSettings(('<bang>'=='!')+str2nr(<q-args>))
"
else
command -nargs=* -bang Git :call GitS_Help('disabled')
command! -nargs=* GitRun :call GitS_Help('disabled')
command! -nargs=* GitBuf :call GitS_Help('disabled')
command! -nargs=* GitHelp :call GitS_Help('disabled')
command! -nargs=0 GitSupportHelp :call GitS_PluginHelp("gitsupport")