/
genutils.vim
1980 lines (1769 loc) · 54.8 KB
/
genutils.vim
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
" genutils.vim: Please see plugin/genutils.vim
"
" TODO:
" - fnamemodify() on Unix doesn't expand to full name if the filename doesn't
" really exist on the filesystem.
" - Is setting 'scrolloff' and 'sidescrolloff' to 0 required while moving the
" cursor?
"
" - EscapeCommand() didn't work for David Fishburn.
" - Save/RestoreWindowSettings doesn't work well.
"
" Vim7:
" - Save/RestoreWindowSettings can use winsave/restview() functions.
"
" Make sure line-continuations won't cause any problem. This will be restored
" at the end
let s:save_cpo = &cpo
set cpo&vim
let g:makeArgumentString = 'exec genutils#MakeArgumentString()'
let g:makeArgumentList = 'exec genutils#MakeArgumentList()'
let s:makeArgumentString = ''
function! genutils#MakeArgumentString(...)
if s:makeArgumentString == ''
let s:makeArgumentString = genutils#ExtractFuncListing(s:SNR().
\ '_makeArgumentString', 0, 0)
endif
if a:0 > 0 && a:1 != ''
return substitute(s:makeArgumentString, '\<argumentString\>', a:1, 'g')
else
return s:makeArgumentString
endif
endfunction
let s:makeArgumentList = ''
function! genutils#MakeArgumentList(...)
if s:makeArgumentList == ''
let s:makeArgumentList = genutils#ExtractFuncListing(s:SNR().
\ '_makeArgumentList', 0, 0)
endif
if a:0 > 0 && a:1 != ''
let mkArgLst = substitute(s:makeArgumentList, '\<argumentList\>', a:1, 'g')
if a:0 > 1 && a:2 != ''
let mkArgLst = substitute(s:makeArgumentList,
\ '\(\s\+let __argSeparator = \)[^'."\n".']*', "\\1'".a:2."'", '')
endif
return mkArgLst
else
return s:makeArgumentList
endif
endfunction
function! genutils#ExtractFuncListing(funcName, hLines, tLines)
let listing = genutils#GetVimCmdOutput('func '.a:funcName)
let listing = substitute(listing,
\ '^\%(\s\|'."\n".'\)*function '.a:funcName.'([^)]*)'."\n", '', '')
"let listing = substitute(listing, '\%(\s\|'."\n".'\)*endfunction\%(\s\|'."\n".'\)*$', '', '')
" Leave the last newline character.
let listing = substitute(listing, '\%('."\n".'\)\@<=\s*endfunction\s*$', '', '')
let listing = substitute(listing, '\(\%(^\|'."\n".'\)\s*\)\@<=\d\+',
\ '', 'g')
if a:hLines > 0
let listing = substitute(listing, '^\%([^'."\n".']*'."\n".'\)\{'.
\ a:hLines.'}', '', '')
endif
if a:tLines > 0
let listing = substitute(listing, '\%([^'."\n".']*'."\n".'\)\{'.
\ a:tLines.'}$', '', '')
endif
return listing
endfunction
function! genutils#CreateArgString(argList, sep, ...)
let sep = (a:0 == 0) ? a:sep : a:1 " This is no longer used.
" Matching multvals functionality means, we need to ignore the trailing
" separator.
let argList = split(substitute(a:argList, a:sep.'$', '', ''), a:sep, 1)
let argString = "'"
for nextArg in argList
" FIXME: I think this check is not required. If "'" is the separator, we
" don't expect to see them in the elements.
if a:sep != "'"
let nextArg = substitute(nextArg, "'", "' . \"'\" . '", 'g')
endif
let argString = argString . nextArg . "', '"
endfor
let argString = strpart(argString, 0, strlen(argString) - 3)
return argString
endfunction
" {{{
function! s:_makeArgumentString()
let __argCounter = 1
let argumentString = ''
while __argCounter <= a:0
if type(a:{__argCounter})
let __nextArg = "'" .
\ substitute(a:{__argCounter}, "'", "' . \"'\" . '", "g") . "'"
else
let __nextArg = a:{__argCounter}
endif
let argumentString = argumentString. __nextArg .
\ ((__argCounter == a:0) ? '' : ', ')
let __argCounter = __argCounter + 1
endwhile
unlet __argCounter
if exists('__nextArg')
unlet __nextArg
endif
endfunction
function! s:_makeArgumentList()
let __argCounter = 1
let __argSeparator = ','
let argumentList = ''
while __argCounter <= a:0
let argumentList = argumentList . a:{__argCounter}
if __argCounter != a:0
let argumentList = argumentList . __argSeparator
endif
let __argCounter = __argCounter + 1
endwhile
unlet __argCounter
unlet __argSeparator
endfunction
" }}}
function! genutils#DebugShowArgs(...)
let i = 0
let argString = ''
while i < a:0
let argString = argString . a:{i + 1} . ', '
let i = i + 1
endwhile
let argString = strpart(argString, 0, strlen(argString) - 2)
call input("Args: " . argString)
endfunction
" Window related functions {{{
function! genutils#NumberOfWindows()
let i = 1
while winbufnr(i) != -1
let i = i+1
endwhile
return i - 1
endfunction
" Find the window number for the buffer passed.
" The fileName argument is treated literally, unlike the bufnr() which treats
" the argument as a regex pattern.
function! genutils#FindWindowForBuffer(bufferName, checkUnlisted)
return bufwinnr(genutils#FindBufferForName(a:bufferName))
endfunction
function! genutils#FindBufferForName(fileName)
" The name could be having extra backslashes to protect certain chars (such
" as '#' and '%'), so first expand them.
return s:FindBufferForName(genutils#UnEscape(a:fileName, '#%'))
endfunction
function! s:FindBufferForName(fileName)
let fileName = genutils#Escape(a:fileName, '[?,{')
let _isf = &isfname
try
set isfname-=\
set isfname-=[
let i = bufnr('^' . fileName . '$')
finally
let &isfname = _isf
endtry
return i
endfunction
function! genutils#GetBufNameForAu(bufName)
let bufName = a:bufName
" Autocommands always require forward-slashes.
let bufName = substitute(bufName, "\\\\", '/', 'g')
let bufName = escape(bufName, '*?,{}[ ')
return bufName
endfunction
function! genutils#MoveCursorToWindow(winno)
if genutils#NumberOfWindows() != 1
execute a:winno . " wincmd w"
endif
endfunction
function! genutils#MoveCurLineToWinLine(n)
normal! zt
if a:n == 1
return
endif
let _wrap = &l:wrap
setl nowrap
let n = a:n
if n >= winheight(0)
let n = winheight(0)
endif
let n = n - 1
execute "normal! " . n . "\<C-Y>"
let &l:wrap = _wrap
endfunction
function! genutils#CloseWindow(win, force)
let _eventignore = &eventignore
try
set eventignore=all
call genutils#MarkActiveWindow()
let &eventignore = _eventignore
exec a:win 'wincmd w'
exec 'close'.(a:force ? '!' : '')
set eventignore=all
if a:win < t:curWinnr
let t:curWinnr = t:curWinnr - 1
endif
if a:win < t:prevWinnr
let t:prevWinnr = t:prevWinnr - 1
endif
finally
call genutils#RestoreActiveWindow()
let &eventignore = _eventignore
endtry
endfunction
function! genutils#MarkActiveWindow()
let t:curWinnr = winnr()
" We need to restore the previous-window also at the end.
silent! wincmd p
let t:prevWinnr = winnr()
silent! wincmd p
endfunction
function! genutils#RestoreActiveWindow()
if !exists('t:curWinnr')
return
endif
" Restore the original window.
if winnr() != t:curWinnr
exec t:curWinnr'wincmd w'
endif
if t:curWinnr != t:prevWinnr
exec t:prevWinnr'wincmd w'
wincmd p
endif
endfunction
function! genutils#IsOnlyVerticalWindow()
let onlyVertWin = 1
let _eventignore = &eventignore
try
"set eventignore+=WinEnter,WinLeave
set eventignore=all
call genutils#MarkActiveWindow()
wincmd j
if winnr() != t:curWinnr
let onlyVertWin = 0
else
wincmd k
if winnr() != t:curWinnr
let onlyVertWin = 0
endif
endif
finally
call genutils#RestoreActiveWindow()
let &eventignore = _eventignore
endtry
return onlyVertWin
endfunction
function! genutils#IsOnlyHorizontalWindow()
let onlyHorizWin = 1
let _eventignore = &eventignore
try
set eventignore=all
call genutils#MarkActiveWindow()
wincmd l
if winnr() != t:curWinnr
let onlyHorizWin = 0
else
wincmd h
if winnr() != t:curWinnr
let onlyHorizWin = 0
endif
endif
finally
call genutils#RestoreActiveWindow()
let &eventignore = _eventignore
endtry
return onlyHorizWin
endfunction
function! genutils#MoveCursorToNextInWinStack(dir)
let newwin = genutils#GetNextWinnrInStack(a:dir)
if newwin != 0
exec newwin 'wincmd w'
endif
endfunction
function! genutils#GetNextWinnrInStack(dir)
let newwin = winnr()
let _eventignore = &eventignore
try
set eventignore=all
call genutils#MarkActiveWindow()
let newwin = s:GetNextWinnrInStack(a:dir)
finally
call genutils#RestoreActiveWindow()
let &eventignore = _eventignore
endtry
return newwin
endfunction
function! genutils#MoveCursorToLastInWinStack(dir)
let newwin = genutils#GetLastWinnrInStack(a:dir)
if newwin != 0
exec newwin 'wincmd w'
endif
endfunction
function! genutils#GetLastWinnrInStack(dir)
let newwin = winnr()
let _eventignore = &eventignore
try
set eventignore=all
call genutils#MarkActiveWindow()
while 1
let wn = s:GetNextWinnrInStack(a:dir)
if wn != 0
let newwin = wn
exec newwin 'wincmd w'
else
break
endif
endwhile
finally
call genutils#RestoreActiveWindow()
let &eventignore = _eventignore
endtry
return newwin
endfunction
" Based on the WinStackMv() function posted by Charles E. Campbell, Jr. on vim
" mailing list on Jul 14, 2004.
function! s:GetNextWinnrInStack(dir)
"call Decho("genutils#MoveCursorToNextInWinStack(dir<".a:dir.">)")
let isHorizontalMov = (a:dir ==# 'h' || a:dir ==# 'l') ? 1 : 0
let orgwin = winnr()
let orgdim = s:GetWinDim(a:dir, orgwin)
let _winwidth = &winwidth
let _winheight = &winheight
try
set winwidth=1
set winheight=1
exec 'wincmd' a:dir
let newwin = winnr()
if orgwin == newwin
" No more windows in this direction.
"call Decho("newwin=".newwin." stopped".winheight(newwin)."x".winwidth(newwin))
return 0
endif
if s:GetWinDim(a:dir, newwin) != orgdim
" Window dimension has changed, indicates a move across window stacks.
"call Decho("newwin=".newwin." height changed".winheight(newwin)."x".winwidth(newwin))
return 0
endif
" Determine if changing original window height affects current window
" height.
exec orgwin 'wincmd w'
try
if orgdim == 1
exec 'wincmd' (isHorizontalMov ? '_' : '|')
else
exec 'wincmd' (isHorizontalMov ? '-' : '<')
endif
if s:GetWinDim(a:dir, newwin) != s:GetWinDim(a:dir, orgwin)
"call Decho("newwin=".newwin." different row".winheight(newwin)."x".winwidth(newwin))
return 0
endif
"call Decho("newwin=".newwin." same row".winheight(newwin)."x".winwidth(newwin))
finally
exec (isHorizontalMov ? '' : 'vert') 'resize' orgdim
endtry
"call Decho("genutils#MoveCursorToNextInWinStack")
return newwin
finally
let &winwidth = _winwidth
let &winheight = _winheight
endtry
endfunction
function! s:GetWinDim(dir, win)
return (a:dir ==# 'h' || a:dir ==# 'l') ? winheight(a:win) : winwidth(a:win)
endfunction
function! genutils#OpenWinNoEa(winOpenCmd)
call s:ExecWinCmdNoEa(a:winOpenCmd)
endfunction
function! genutils#CloseWinNoEa(winnr, force)
call s:ExecWinCmdNoEa(a:winnr.'wincmd w | close'.(a:force?'!':''))
endfunction
function! s:ExecWinCmdNoEa(winCmd)
let _eventignore = &eventignore
try
set eventignore=all
call genutils#MarkActiveWindow()
windo let w:_winfixheight = &winfixheight
windo set winfixheight
call genutils#RestoreActiveWindow()
let &eventignore = _eventignore
exec a:winCmd
set eventignore=all
call genutils#MarkActiveWindow()
silent! windo let &winfixheight = w:_winfixheight
silent! windo unlet w:_winfixheight
call genutils#RestoreActiveWindow()
finally
let &eventignore = _eventignore
endtry
endfunction
" Window related functions }}}
function! genutils#SetupScratchBuffer()
setlocal nobuflisted
setlocal noswapfile
setlocal buftype=nofile
" Just in case, this will make sure we are always hidden.
setlocal bufhidden=delete
setlocal nolist
setlocal nonumber
setlocal foldcolumn=0 nofoldenable
setlocal noreadonly
endfunction
function! genutils#CleanDiffOptions()
setlocal nodiff
setlocal noscrollbind
setlocal scrollopt-=hor
setlocal wrap
setlocal foldmethod=manual
setlocal foldcolumn=0
normal zE
endfunction
function! genutils#ArrayVarExists(varName, index)
try
exec "let test = " . a:varName . "{a:index}"
catch /^Vim\%((\a\+)\)\=:E121/
return 0
endtry
return 1
endfunction
function! genutils#Escape(str, chars)
return substitute(a:str, '\\\@<!\(\%(\\\\\)*\)\([' . a:chars .']\)', '\1\\\2',
\ 'g')
endfunction
function! genutils#UnEscape(str, chars)
return substitute(a:str, '\\\@<!\(\\\\\)*\\\([' . a:chars . ']\)',
\ '\1\2', 'g')
endfunction
function! genutils#DeEscape(str)
let str = a:str
let str = substitute(str, '\\\(\\\|[^\\]\)', '\1', 'g')
return str
endfunction
" - For windoze+native, use double-quotes to sorround the arguments and for
" embedded double-quotes, just double them.
" - For windoze+sh, use single-quotes to sorround the aruments and for embedded
" single-quotes, just replace them with '""'""' (if 'shq' or 'sxq' is a
" double-quote) and just '"'"' otherwise. Embedded double-quotes also need
" to be doubled.
" - For Unix+sh, use single-quotes to sorround the arguments and for embedded
" single-quotes, just replace them with '"'"'.
function! genutils#EscapeCommand(cmd, args, pipe)
if type(a:args) == 3
let args = copy(a:args)
else
let args = split(a:args, genutils#CrUnProtectedCharsPattern(' '))
endif
" I am only worried about passing arguments with spaces as they are to the
" external commands, I currently don't care about back-slashes
" (backslashes are normally expected only on windows when 'shellslash'
" option is set, but even then the 'shell' is expected to take care of
" them.). However, for cygwin bash, there is a loss of one level
" of the back-slashes somewhere in the chain of execution (most probably
" between CreateProcess() and cygwin?), so we need to double them.
let shellEnvType = genutils#GetShellEnvType()
if shellEnvType ==# g:ST_WIN_CMD
let quoteChar = '"'
" genutils#Escape the existing double-quotes (by doubling them).
call map(args, "substitute(v:val, '\"', '\"\"', 'g')")
else
let quoteChar = "'"
if shellEnvType ==# g:ST_WIN_SH
" genutils#Escape the existing double-quotes (by doubling them).
call map(args, "substitute(v:val, '\"', '\"\"', 'g')")
endif
" Take care of existing single-quotes (by exposing them, as you can't have
" single-quotes inside a single-quoted string).
if &shellquote == '"' || &shellxquote == '"'
let squoteRepl = "'\"\"'\"\"'"
else
let squoteRepl = "'\"'\"'"
endif
call map(args, "substitute(v:val, \"'\", squoteRepl, 'g')")
endif
" Now sorround the arguments with quotes, considering the protected
" spaces. Skip the && or || construct from doing this.
call map(args, 'v:val=~"^[&|]\\{2}$"?(v:val):(quoteChar.v:val.quoteChar)')
let fullCmd = join(args, ' ')
" We delay adding pipe part so that we can avoid the above processing.
let pipe = ''
if type(a:pipe) == 3 && len(a:pipe) > 0
let pipe = join(a:pipe, ' ')
elseif type(a:pipe) == 1 && a:pipe !~# '^\s*$'
let pipe = a:pipe
endif
if pipe != ''
let fullCmd = fullCmd . ' ' . a:pipe
endif
if a:cmd !~# '^\s*$'
let fullCmd = a:cmd . ' ' . fullCmd
endif
if shellEnvType ==# g:ST_WIN_SH && &shell =~# '\<bash\>'
let fullCmd = substitute(fullCmd, '\\', '\\\\', 'g')
endif
return fullCmd
endfunction
let g:ST_WIN_CMD = 0 | let g:ST_WIN_SH = 1 | let g:ST_UNIX = 2
function! genutils#GetShellEnvType()
" When 'shellslash' option is available, then the platform must be one of
" those that support '\' as a pathsep.
if exists('+shellslash')
if stridx(&shell, 'cmd.exe') != -1 ||
\ stridx(&shell, 'command.com') != -1
return g:ST_WIN_CMD
else
return g:ST_WIN_SH
endif
else
return g:ST_UNIX
endif
endfunction
function! genutils#ExpandStr(str)
let str = substitute(a:str, '"', '\\"', 'g')
exec "let str = \"" . str . "\""
return str
endfunction
function! genutils#QuoteStr(str)
return "'".substitute(a:str, "'", "'.\"'\".'", 'g')."'"
endfunction
function! genutils#GetPreviewWinnr()
let _eventignore = &eventignore
let curWinNr = winnr()
let winnr = -1
try
set eventignore=all
exec "wincmd P"
let winnr = winnr()
catch /^Vim\%((\a\+)\)\=:E441/
" Ignore, winnr is already set to -1.
finally
if winnr() != curWinNr
exec curWinNr.'wincmd w'
endif
let &eventignore = _eventignore
endtry
return winnr
endfunction
" Save/Restore window settings {{{
function! genutils#SaveWindowSettings()
call genutils#SaveWindowSettings2('SaveWindowSettings', 1)
endfunction
function! genutils#RestoreWindowSettings()
call genutils#RestoreWindowSettings2('SaveWindowSettings')
endfunction
function! genutils#ResetWindowSettings()
call genutils#ResetWindowSettings2('SaveWindowSettings')
endfunction
function! genutils#SaveWindowSettings2(id, overwrite)
if genutils#ArrayVarExists("t:winSettings", a:id) && ! a:overwrite
return
endif
let t:winSettings{a:id} = []
call add(t:winSettings{a:id}, genutils#NumberOfWindows())
call add(t:winSettings{a:id}, &lines)
call add(t:winSettings{a:id}, &columns)
call add(t:winSettings{a:id}, winnr())
let i = 1
while winbufnr(i) != -1
call add(t:winSettings{a:id}, winheight(i))
call add(t:winSettings{a:id}, winwidth(i))
let i = i + 1
endwhile
"let g:savedWindowSettings = t:winSettings{a:id} " Debug.
endfunction
function! genutils#RestoreWindowSettings2(id)
" Calling twice fixes most of the resizing issues. This seems to be how the
" :mksession with "winsize" in 'sesionoptions' seems to work.
call s:RestoreWindowSettings2(a:id)
call s:RestoreWindowSettings2(a:id)
endfunction
function! s:RestoreWindowSettings2(id)
"if ! exists("t:winSettings" . a:id)
if ! genutils#ArrayVarExists("t:winSettings", a:id)
return
endif
let nWindows = t:winSettings{a:id}[0]
if nWindows != genutils#NumberOfWindows()
unlet t:winSettings{a:id}
return
endif
let orgLines = t:winSettings{a:id}[1]
let orgCols = t:winSettings{a:id}[2]
let activeWindow = t:winSettings{a:id}[3]
let mainWinSizeSame = (orgLines == &lines && orgCols == &columns)
let winNo = 1
let i = 4
while i < len(t:winSettings{a:id})
let height = t:winSettings{a:id}[i]
let width = t:winSettings{a:id}[i+1]
let height = (mainWinSizeSame ? height :
\ ((&lines * height + (orgLines / 2)) / orgLines))
let width = (mainWinSizeSame ? width :
\ ((&columns * width + (orgCols / 2)) / orgCols))
if winheight(winnr()) != height
exec winNo'resize' height
endif
if winwidth(winnr()) != width
exec 'vert' winNo 'resize' width
endif
let winNo = winNo + 1
let i = i + 2
endwhile
" Restore the current window.
call genutils#MoveCursorToWindow(activeWindow)
"unlet g:savedWindowSettings
endfunction
function! genutils#ResetWindowSettings2(id)
if genutils#ArrayVarExists("t:winSettings", a:id)
unlet t:winSettings{a:id}
endif
endfunction
" Save/Restore window settings }}}
" Save/Restore selection {{{
function! genutils#SaveVisualSelection(id)
let curmode = mode()
if curmode == 'n'
normal! gv
endif
let s:vismode{a:id} = mode()
let s:firstline{a:id} = line("'<")
let s:lastline{a:id} = line("'>")
let s:firstcol{a:id} = col("'<")
let s:lastcol{a:id} = col("'>")
if curmode !=# s:vismode{a:id}
exec "normal \<Esc>"
endif
endfunction
function! genutils#RestoreVisualSelection(id)
if mode() !=# 'n'
exec "normal \<Esc>"
endif
if exists('s:vismode{id}')
exec 'normal' s:firstline{a:id}.'gg'.s:firstcol{a:id}.'|'.
\ s:vismode{a:id}.(s:lastline{a:id}-s:firstline{a:id}).'j'.
\ (s:lastcol{a:id}-s:firstcol{a:id}).'l'
endif
endfunction
" Save/Restore selection }}}
function! genutils#CleanupFileName(fileName)
return genutils#CleanupFileName2(a:fileName, '')
endfunction
function! genutils#CleanupFileName2(fileName, win32ProtectedChars)
let fileName = expand(substitute(a:fileName, '^\s\+\|\s\+$', '', 'g'))
if genutils#OnMS() && (match(fileName, '^//') == 0 ||
\ match(fileName, '^\\\\') == 0)
let uncPath = 1
else
let uncPath = 0
endif
" Remove multiple path separators.
if has('win32')
if a:win32ProtectedChars != ''
let fileName=substitute(fileName, '\\['.a:win32ProtectedChars.']\@!', '/',
\ 'g')
else
let fileName=substitute(fileName, '\\', '/', 'g')
endif
elseif genutils#OnMS()
" On non-win32 systems, the forward-slash is not supported, so leave
" back-slash.
let fileName=substitute(fileName, '\\\{2,}', '\', 'g')
endif
let fileName=substitute(fileName, '/\{2,}', '/', 'g')
" If it was an UNC path, add back an extra slash.
if uncPath
let fileName = '/'.fileName
else
" Expand relative paths and paths containing relative components (takes care
" of ~ also). This also adds the drive letter if it is missing. Special
" case when drive root is used with no trailing slash (e.g., c:), don't
" expand, Vim replaces it with previous directory on that drive. Also
" don't leave any trailing slashes, as they appear conditional to whether
" the path is an existing dir or not.
let fileName = (genutils#OnMS() && fileName =~# '^[a-z]:$') ? fileName.'/' :
\ substitute(fnamemodify(fileName, ':p'), '\(.\)[\\/]$', '\1', '')
endif
if genutils#OnMS()
let fileName=substitute(fileName, '^[A-Z]:', '\L&', '')
endif
return fileName
endfunction
"echo genutils#CleanupFileName('\\a///b/c\') " //a/b/c
"echo genutils#CleanupFileName('C:\a/b/c\d') " c:/a/b/c/d
"echo genutils#CleanupFileName('a/b/c\d') " z:/hari/vimfiles/a/b/c/d
"echo genutils#CleanupFileName('~/a/b/c\d') " z:/hari/a/b/c/d
"echo genutils#CleanupFileName('~/a/b/../c\d') " z:/hari/a/b/c/d
"echo genutils#CleanupFileName('') " z:/hari/vimfiles
"echo genutils#CleanupFileName('/') " z:/
"echo genutils#CleanupFileName('z:') " z:/
"echo genutils#CleanupFileName('z:/') " z:/
"echo genutils#CleanupFileName('C:/windows/') " c:/windows
function! genutils#OnMS()
return has('win32') || has('dos32') || has('win16') || has('dos16') ||
\ has('win95')
endfunction
function! genutils#PathIsAbsolute(path)
let absolute=0
let path = expand(a:path)
if has('unix') || genutils#OnMS()
if match(path, '^/') == 0
let absolute=1
endif
endif
if (! absolute) && genutils#OnMS()
if match(path, "^\\") == 0
let absolute=1
endif
endif
if (! absolute) && genutils#OnMS()
if match(path, "^[A-Za-z]:") == 0
let absolute=1
endif
endif
return absolute
endfunction
function! genutils#PathIsFileNameOnly(path)
return (match(a:path, "\\") < 0) && (match(a:path, "/") < 0)
endfunction
let s:mySNR = ''
function! s:SNR()
if s:mySNR == ''
let s:mySNR = matchstr(expand('<sfile>'), '<SNR>\d\+_\zeSNR$')
endif
return s:mySNR
endfun
"" --- START save/restore position. {{{
function! genutils#SaveSoftPosition(id)
let b:sp_startline_{a:id} = getline(".")
call genutils#SaveHardPosition(a:id)
endfunction
function! genutils#RestoreSoftPosition(id)
0
call genutils#RestoreHardPosition(a:id)
let stLine = b:sp_startline_{a:id}
if getline('.') !=# stLine
if ! search('\V\^'.escape(stLine, "\\").'\$', 'W')
call search('\V\^'.escape(stLine, "\\").'\$', 'bW')
endif
endif
call genutils#MoveCurLineToWinLine(b:sp_winline_{a:id})
endfunction
function! genutils#ResetSoftPosition(id)
unlet b:sp_startline_{a:id}
endfunction
" A synonym for genutils#SaveSoftPosition.
function! genutils#SaveHardPositionWithContext(id)
call genutils#SaveSoftPosition(a:id)
endfunction
" A synonym for genutils#RestoreSoftPosition.
function! genutils#RestoreHardPositionWithContext(id)
call genutils#RestoreSoftPosition(a:id)
endfunction
" A synonym for genutils#ResetSoftPosition.
function! genutils#ResetHardPositionWithContext(id)
call genutils#ResetSoftPosition(a:id)
endfunction
function! genutils#SaveHardPosition(id)
let b:sp_col_{a:id} = virtcol(".")
let b:sp_lin_{a:id} = line(".")
" Avoid accounting for wrapped lines.
let _wrap = &l:wrap
try
setl nowrap
let b:sp_winline_{a:id} = winline()
finally
let &l:wrap = _wrap
endtry
endfunction
function! genutils#RestoreHardPosition(id)
" This doesn't take virtual column.
"call cursor(b:sp_lin_{a:id}, b:sp_col_{a:id})
" Vim7 generates E16 if line number is invalid.
" TODO: Why is this leaving cursor on the last-but-one line when the
" condition meets?
execute ((line('$') < b:sp_lin_{a:id}) ? line('$') :
\ b:sp_lin_{a:id})
"execute b:sp_lin_{a:id}
execute ((line('$') < b:sp_lin_{a:id}) ? line('$') :
\ b:sp_lin_{a:id})
"execute b:sp_lin_{a:id}
execute "normal!" b:sp_col_{a:id} . "|"
call genutils#MoveCurLineToWinLine(b:sp_winline_{a:id})
endfunction
function! genutils#ResetHardPosition(id)
unlet b:sp_col_{a:id}
unlet b:sp_lin_{a:id}
unlet b:sp_winline_{a:id}
endfunction
function! genutils#GetLinePosition(id)
return b:sp_lin_{a:id}
endfunction
function! genutils#GetColPosition(id)
return b:sp_col_{a:id}
endfunction
function! genutils#IsPositionSet(id)
return exists('b:sp_col_' . a:id)
endfunction
"" --- END save/restore position. }}}
""
"" --- START: Notify window close -- {{{
""
let s:notifyWindow = {}
function! genutils#AddNotifyWindowClose(windowTitle, functionName)
let bufName = a:windowTitle
let s:notifyWindow[bufName] = a:functionName
"let g:notifyWindow = s:notifyWindow " Debug.
" Start listening to events.
aug NotifyWindowClose
au!
au WinEnter * :call genutils#CheckWindowClose()
au BufEnter * :call genutils#CheckWindowClose()
aug END
endfunction
function! genutils#RemoveNotifyWindowClose(windowTitle)
let bufName = a:windowTitle
if has_key(s:notifyWindow, bufName)
call remove(s:notifyWindow, bufName)
if len(s:notifyWindow) == 0
"unlet g:notifyWindow " Debug.
aug NotifyWindowClose
au!
aug END
endif
endif
endfunction
function! genutils#CheckWindowClose()
if !exists('s:notifyWindow')
return
endif
" Now iterate over all the registered window titles and see which one's are
" closed.
for nextWin in keys(s:notifyWindow)
if bufwinnr(s:FindBufferForName(nextWin)) == -1
let funcName = s:notifyWindow[nextWin]
" Remove this entry as these are going to be processed. The caller can add
" them back if needed.
unlet s:notifyWindow[nextWin]
"call input("cmd: " . cmd)
call call(funcName, [nextWin])
endif
endfor
endfunction
"function! NotifyWindowCloseF(title)
" call input(a:title . " closed")
"endfunction
"
"function! RunNotifyWindowCloseTest()
" call input("Creating three windows, 'ABC', 'XYZ' and 'b':")
" split ABC
" split X Y Z
" split b
" redraw
" call genutils#AddNotifyWindowClose("ABC", "NotifyWindowCloseF")
" call genutils#AddNotifyWindowClose("X Y Z", "NotifyWindowCloseF")
" call input("notifyWindow: " . string(s:notifyWindow))
" au NotifyWindowClose WinEnter
" call input("Added notifications for 'ABC' and 'XYZ'\n".
" \ "Now closing the windows, you should see ONLY two notifications:")
" quit
" quit
" quit
"endfunction
""
"" --- END: Notify window close -- }}}
""
" TODO: For large ranges, the cmd can become too big, so make it one cmd per
" line.
function! genutils#ShowLinesWithSyntax() range
" This makes sure we start (subsequent) echo's on the first line in the
" command-line area.
"
echo ''
let cmd = ''
let prev_group = ' x ' " Something that won't match any syntax group.
let show_line = a:firstline
let isMultiLine = ((a:lastline - a:firstline) > 1)
while show_line <= a:lastline
let cmd = ''
let length = strlen(getline(show_line))
let column = 1