/
forms.vim
16970 lines (14873 loc) · 576 KB
/
forms.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
" ============================================================================
" forms.vim
"
" File: forms.vim
" Summary: Vim Form Library
" Author: Richard Emberson <richard.n.embersonATgmailDOTcom>
" Last Modified: 06/30/2012
" Version: 1.3
" Modifications:
" 1.0 : initial public release.
"
" Tested on vim 7.3 on Linux
"
" Depends upon: self.vim
"
" ============================================================================
" Intro: {{{1
" The text-based, console Vim Forms library allows developers to embed
" forms in their scripts. Thats right, these forms all run in a normal
" Vim console window - no need for GVim.
"
" When Vim 7.0 came out, I thought about building a Vim forms library but
" never got around to it. Recently I started using Envim, a Vim binding
" to ENSIME for Scala. While, ENSIME supports a couple of refactoring
" options, none of them were available in Envim. Further, exactly how
" one might go about entering a couple of parameters required by any
" one of the refactoring options was not clear. A solution, of course,
" was to have some sort of forms capability so that each refactoring
" option, when invoked, would allow the user to enter the necessary
" parameters for the implied refactoring operation. So.,,, Here is
" that forms capability.
"
" Many of the layout and structural elements of this forms system are based
" upon Mark Linton's C++ InterViews and Java Biscotti libraries.
" You might think they are from Sun's, now Oracle's, Java Swing library
" but for some of that library, Sun stole code from Fujitsu/Stanford so
" I go back to acknowledge the earlier sources.
"
" While an attempt was made to make this a general forms capability, certainly
" many capabilities of a general GUI form library are missing.
"
" Well, so what.
"
" It is envisioned that this library might have three uses:
" During plugin initialization the user could be presented with a form
" allowing for the entry of necessary tailoring option values.
" Some scripts might require the entry of values prior to their execution,
" like the Envim Scala code refactoring options.
" Lastly, as proof of completeness, along with this forms library are
" two forms. The first mirrors the capability of the GVim menu bar
" while the second mirrors the GVim popup menu.
" For advanced user, the menu bar and popup menu is not really needed,
" but for beginners they might prove useful.
" ============================================================================
" Load Once: {{{1
if &cp || ( exists("g:loaded_forms") && ! g:self#IN_DEVELOPMENT_MODE )
finish
endif
let g:loaded_forms = 'v1.0'
let s:keepcpo = &cpo
set cpo&vim
" ++++++++++++++++++++++++++++++++++++++++++++
" Reload : {{{1
" ++++++++++++++++++++++++++++++++++++++++++++
" ------------------------------------------------------------
" forms#reload: {{{2
" Cals self#reload to force reloading of both forms and self
" libraries:
" call self#reload('forms#')
" call self#reload('self#')
" This function is only available in development mode, i.e.,
" g:self#IN_DEVELOPMENT_MODE == self#IS_TRUE
" To make reloading of autoloaded forms functions simple, one might
" want to define a mapping:
" map <Leader>fr :call forms#reload()
" parameters: None
" ------------------------------------------------------------
if !exists("*forms#reload")
if g:self#IN_DEVELOPMENT_MODE
function forms#reload()
call self#reload('forms#')
call self#reload('self#')
endfunction
endif
endif
" ++++++++++++++++++++++++++++++++++++++++++++
" Configuration Options: {{{1
" These help control of the forms library.
" ++++++++++++++++++++++++++++++++++++++++++++
" If window dump is enabled, the file to be used
if ! exists("g:forms_window_dump_file") || g:self#IN_DEVELOPMENT_MODE
let g:forms_window_dump_file = "VIM_WINDOW"
endif
" Enable/Disable screen dump.
" A window dump occurs when <C-W> is entered.
if ! exists("g:forms_window_dump_enabled") || g:self#IN_DEVELOPMENT_MODE
let g:forms_window_dump_enabled = g:self#IS_TRUE
endif
" ++++++++++++++++++++++++++++++++++++++++++++
" Forms Logging: {{{1
" ++++++++++++++++++++++++++++++++++++++++++++
if ! exists("g:forms_log_file") || g:self#IN_DEVELOPMENT_MODE
let g:forms_log_file = "FORMS_LOG"
endif
if ! exists("g:forms_log_enabled") || g:self#IN_DEVELOPMENT_MODE
let g:forms_log_enabled = g:self#IS_FALSE
endif
function! forms#log(msg)
if g:forms_log_enabled
execute "redir >> " . g:forms_log_file
silent echo a:msg
execute "redir END"
endif
endfunction
" ++++++++++++++++++++++++++++++++++++++++++++
" Gui Font: {{{1
" ++++++++++++++++++++++++++++++++++++++++++++
" Set the font to use if using GVim.
" This font must be fixed width and support the utf-8 box drawing and
" block characters.
if ! exists("g:forms_gui_font")
let g:forms_gui_font="Fixed 20"
endif
"-------------------------------------------------------------------------------
"-------------------------------------------------------------------------------
" HightLight: {{{1
"-------------------------------------------------------------------------------
"-------------------------------------------------------------------------------
" Definitions: {{{2
" ------------------------------------------------------------
if &background == 'light'
if has("gui_running")
" ReverseHi must use same background color, ctermbg, as BackgroundHi
:hi ReverseHi gui=reverse guibg=#dadada
:hi HotSpotHi gui=NONE guibg=#00ff00
:hi ReverseHotSpotHi gui=reverse guibg=#00ff00
:hi FlashHi gui=NONE guibg=#ffff87
:hi ToggleSelectionHi gui=NONE guibg=#5fffff
:hi SelectedHi gui=bold guibg=#5fffff
:hi ButtonHi gui=NONE guibg=#bcbcbc
:hi ButtonFlashHi gui=NONE guibg=#767676
:hi BackgroundHi gui=NONE guibg=#dadada
" TODO derive FrameHi values from BackgroundHi values
:hi FrameHi gui=NONE guifg=#e4e4e4 guibg=#d0d0d0
" TODO derive DropShadowHi values from BackgroundHi values
:hi DropShadowHi gui=NONE guibg=#dadada guifg=#bcbcbc
:hi DisableHi gui=NONE guibg=#ffaf00
:hi MenuHi gui=NONE guibg=#dadada
:hi MenuMnemonicHi gui=underline guibg=#dadada
:hi MenuHotSpotHi gui=NONE guibg=#ff00d7
:hi MenuMnemonicHotSpotHi gui=underline guibg=#ff00d7
elseif &t_Co == 256
:hi ReverseHi cterm=reverse ctermbg=253
:hi HotSpotHi cterm=NONE ctermbg=46
:hi ReverseHotSpotHi cterm=reverse ctermbg=46
:hi FlashHi cterm=NONE ctermbg=228
:hi ToggleSelectionHi cterm=bold ctermbg=87
:hi SelectedHi cterm=bold ctermbg=87
:hi ButtonHi cterm=NONE ctermbg=250
:hi ButtonFlashHi cterm=NONE ctermbg=243
:hi BackgroundHi cterm=NONE ctermbg=253
:hi FrameHi cterm=NONE ctermfg=254 ctermbg=252
:hi DropShadowHi cterm=NONE ctermbg=253 ctermfg=250
:hi DisableHi cterm=NONE ctermbg=214
:hi MenuHi cterm=None ctermbg=253
:hi MenuMnemonicHi cterm=underline ctermbg=253
:hi MenuHotSpotHi cterm=None ctermbg=200
:hi MenuMnemonicHotSpotHi cterm=underline ctermbg=200
else " t_Co < 256
" TODO: I have no idea if these values look ok
:hi ReverseHi cterm=reverse ctermbg=LightGrey
:hi HotSpotHi ctermbg=Green
:hi ReverseHotSpotHi cterm=reverse ctermbg=Green
:hi FlashHi ctermbg=LightYellow
:hi ToggleSelectionHi cterm=bold ctermbg=LightBlue
:hi SelectedHi cterm=bold ctermbg=LightBlue
:hi ButtonHi cterm=NONE ctermbg=Grey
:hi ButtonFlashHi cterm=NONE ctermbg=DarkGrey
:hi BackgroundHi cterm=NONE ctermbg=LightGrey
:hi DisableHi cterm=NONE ctermbg=LightRed
:hi MenuHi cterm=None ctermbg=LightGrey
:hi MenuMnemonicHi cterm=underline ctermbg=LightGrey
:hi MenuHotSpotHi cterm=None ctermbg=LightGrey
:hi MenuMnemonicHotSpotHi cterm=underline ctermbg=LightMagenta
endif
else " &background == 'dark'
if has("gui_running")
:hi ReverseHi gui=reverse guibg=#303030
:hi HotSpotHi gui=NONE guibg=#00ff00
:hi ReverseHotSpotHi gui=reverse guibg=#00ff00
:hi FlashHi gui=NONE guibg=#ffff87
:hi ToggleSelectionHi gui=bold guibg=#5fffff
:hi SelectedHi gui=bold guibg=#5fffff
:hi ButtonHi gui=NONE guibg=#585858
:hi ButtonFlashHi gui=NONE guibg=#9e9e9e
:hi BackgroundHi gui=NONE guibg=#303030
:hi FrameHi gui=NONE guifg=#3a3a3a guibg=#262626
:hi DropShadowHi gui=NONE guibg=#303030 guifg=#080808
:hi DisableHi gui=NONE guibg=#ffaf00
:hi MenuHi gui=None guibg=#303030
:hi MenuMnemonicHi gui=underline guibg=#303030
:hi MenuHotSpotHi gui=None guibg=#ff00d7
:hi MenuMnemonicHotSpotHi gui=underline guibg=#ff00d7
elseif &t_Co == 256
:hi ReverseHi cterm=reverse ctermbg=236
:hi HotSpotHi cterm=NONE ctermbg=46
:hi ReverseHotSpotHi cterm=reverse ctermbg=46
:hi FlashHi cterm=NONE ctermbg=228
:hi ToggleSelectionHi cterm=bold ctermbg=87
:hi SelectedHi cterm=bold ctermbg=87
:hi ButtonHi cterm=NONE ctermbg=240
:hi ButtonFlashHi cterm=NONE ctermbg=247
" :hi BackgroundHi cterm=NONE ctermbg=253
:hi BackgroundHi cterm=NONE ctermbg=236
:hi FrameHi cterm=NONE ctermfg=237 ctermbg=235
:hi DropShadowHi cterm=NONE ctermbg=236 ctermfg=232
:hi DisableHi cterm=NONE ctermbg=214
:hi MenuHi cterm=None ctermbg=236
:hi MenuMnemonicHi cterm=underline ctermbg=236
:hi MenuHotSpotHi cterm=None ctermbg=200
:hi MenuMnemonicHotSpotHi cterm=underline ctermbg=200
else " t_Co < 256
" TODO: I have no idea if these values look ok
:hi ReverseHi cterm=reverse ctermbg=DarkGrey
:hi HotSpotHi ctermbg=Green
:hi ReverseHotSpotHi cterm=reverse ctermbg=Green
:hi FlashHi ctermbg=LightYellow
:hi ToggleSelectionHi cterm=bold ctermbg=LightBlue
:hi SelectedHi cterm=bold ctermbg=LightBlue
:hi ButtonHi cterm=NONE ctermbg=Grey
:hi ButtonFlashHi cterm=NONE ctermbg=LightGrey
:hi BackgroundHi cterm=NONE ctermbg=DarkGrey
:hi DisableHi cterm=NONE ctermbg=DarkGrey
:hi MenuHi cterm=None ctermbg=DarkGrey
:hi MenuMnemonicHi cterm=underline ctermbg=DarkGrey
:hi MenuHotSpotHi cterm=None ctermbg=DarkGrey
:hi MenuMnemonicHotSpotHi cterm=underline ctermbg=LightMagenta
endif
endif " background
" ------------------------------------------------------------
" CleanupHighlights: {{{2
" TODO not used
" ------------------------------------------------------------
function! CleanupHighlights()
if exists("s:hotSpotId")
call matchdelete(s:hotSpotId)
unlet s:hotSpotId
endif
if exists("s:buttonId")
call matchdelete(s:buttonId)
unlet s:buttonId
endif
" if exists("s:toggleSectionId")
" call matchdelete(s:toggleSectionId)
" unlet s:toggleSectionId
" endif
endfunction
" ------------------------------------------------------------
" Reverse: {{{2
" Reverse background point of active focus
" parameters:
" glyph : glyph getting reverse background for point
" line : line of hotspot
" col : column of hotspot
" ------------------------------------------------------------
function! Reverse(glyph, line, col)
" call forms#log("Reverse: TOP")
if exists("a:glyph.__reverseId")
try
call matchdelete(a:glyph.__reverseId)
catch /.*/
" do nothing
endtry
endif
let lcstr = "\\%" . a:line . "l\\%" . a:col . "v"
let a:glyph.__reverseId = matchadd("ReverseHi", lcstr . ".*" . lcstr)
" call forms#log("Reverse: BOTTOM")
endfunction
" ------------------------------------------------------------
" ReverseClear: {{{2
" Clear reverse background
" parameters:
" glyph : glyph clearing reverse background for point
" ------------------------------------------------------------
function! ReverseClear(glyph)
" call forms#log("ReverseClear: TOP")
if exists("a:glyph.__reverseId")
try
call matchdelete(a:glyph.__reverseId)
catch /.*/
" do nothing
endtry
unlet a:glyph.__reverseId
endif
" call forms#log("ReverseClear: BOTTOM")
endfunction
" ------------------------------------------------------------
" ReverseHotSpot: {{{2
" Reverse background point of active focus
" parameters:
" line : line of hotspot
" col : column of hotspot
" ------------------------------------------------------------
function! ReverseHotSpot(line, col)
" call forms#log("ReverseHotSpot: TOP")
if exists("s:reverseHotSpotId")
try
call matchdelete(s:reverseHotSpotId)
catch /.*/
" do nothing
endtry
endif
let lcstr = "\\%" . a:line . "l\\%" . a:col . "v"
let s:reverseHotSpotId = matchadd("ReverseHotSpotHi", lcstr . ".*" . lcstr)
" call forms#log("ReverseHotSpot: BOTTOM")
endfunction
" ------------------------------------------------------------
" ReverseHotSpotClear: {{{2
" Clear reverse background
" parameters: NONE
" ------------------------------------------------------------
function! ReverseHotSpotClear()
" call forms#log("ReverseHotSpotClear: TOP")
if exists("s:reverseHotSpotId")
try
call matchdelete(s:reverseHotSpotId)
catch /.*/
" do nothing
endtry
unlet s:reverseHotSpotId
endif
" call forms#log("ReverseHotSpotClear: BOTTOM")
endfunction
" ------------------------------------------------------------
" HotSpot: {{{2
" Highlight background point of active focus
" parameters:
" line : line of hotspot
" col : column of hotspot
" ------------------------------------------------------------
function! HotSpot(line, col)
" call forms#log("HotSpot: TOP")
if exists("s:hotSpotId")
try
call matchdelete(s:hotSpotId)
catch /.*/
" do nothing
endtry
endif
let lcstr = "\\%" . a:line . "l\\%" . a:col . "v"
let s:hotSpotId = matchadd("HotSpotHi", lcstr . ".*" . lcstr)
" call forms#log("HotSpot: BOTTOM")
endfunction
" ------------------------------------------------------------
" HotRegion: {{{2
" Highlight foreground of active focus
" parameters:
" a : allocation
" ------------------------------------------------------------
function! HotRegion(allocation)
" call forms#log("HotRegion: TOP")
if exists("s:hotSpotId")
try
call matchdelete(s:hotSpotId)
catch /.*/
" do nothing
endtry
endif
let pattern = GetMatchRange(a:allocation)
let s:hotSpotId = matchadd("HotSpotHi", pattern)
" call forms#log("HotRegion: BOTTOM")
endfunction
" ------------------------------------------------------------
" Flash: {{{2
" Highlight error condition for part of a line
" parameters:
" line : line of flash
" start : start column of flash
" end : end column of flash
" ------------------------------------------------------------
function! Flash(line, start, end)
" let startstr = "\\%" . a:line . "l\\%" . a:start . "c"
" let endstr = "\\%" . a:line . "l\\%" . a:end . "c"
let startstr = "\\%" . a:line . "l\\%" . a:start . "v"
let endstr = "\\%" . a:line . "l\\%" . a:end . "v"
let l:flashId = matchadd("FlashHi", startstr . ".*" . endstr)
redraw
sleep 200m
call matchdelete(l:flashId)
unlet l:flashId
endfunction
" ------------------------------------------------------------
" FlashRegion: {{{2
" Highlight error condition for a region
" parameters:
" allocation : allocation of flash
" ------------------------------------------------------------
function! FlashRegion(allocation)
let pattern = GetMatchRange(a:allocation)
let l:flashId = matchadd("FlashHi", pattern)
redraw
sleep 200m
call matchdelete(l:flashId)
unlet l:flashId
endfunction
" ------------------------------------------------------------
" GlyphHilight: {{{2
" Highlight a region and associate with a glyph.
" If the glyph already has a highlight, delete it.
" parameters:
" glyph : glyph to be associated with highlight
" highlight : name of highlight
" allocation : allocation of highlight
" ------------------------------------------------------------
function! GlyphHilight(glyph, highlight, allocation)
" call forms#log("GlyphHilight: TOP")
call GlyphDeleteHi(a:glyph)
let pattern = GetMatchRange(a:allocation)
"call forms#log("GlyphHilight: pattern=" . pattern)
let a:glyph.__matchId = matchadd(a:highlight, pattern)
" call forms#log("GlyphHilight: BOTTOM")
endfunction
function! AugmentGlyphHilight(glyph, highlight, allocation)
" call forms#log("AugmentGlyphHilight: TOP")
let pattern = GetMatchRange(a:allocation)
"call forms#log("AugmentGlyphHilight: pattern=" . pattern)
if ! has_key(a:glyph, '__matchId')
let a:glyph.__matchId = matchadd(a:highlight, pattern)
elseif type(a:glyph.__matchId) == g:self#LIST_TYPE
call add(a:glyph.__matchId, matchadd(a:highlight, pattern))
else
let matchId = a:glyph.__matchId
unlet a:glyph.__matchId
let a:glyph.__matchId = [matchId, matchadd(a:highlight, pattern)]
endif
" call forms#log("GlyphHilight: BOTTOM")
endfunction
" ------------------------------------------------------------
" GlyphDeleteHi: {{{2
" Delete a highlight or list of highlights associate with a
" glyph
" parameters:
" glyph : glyph associated with zero or more highlights
" ------------------------------------------------------------
function! GlyphDeleteHi(glyph)
"call forms#log("GlyphDeleteHi: TOP")
if has_key(a:glyph, '__matchId')
let matchId = a:glyph.__matchId
if type(matchId) == g:self#LIST_TYPE
for m in matchId
call MatchDeleteHi(m)
endfor
else
call MatchDeleteHi(matchId)
endif
unlet a:glyph.__matchId
endif
" call forms#log("GlyphDeleteHi: BOTTOM")
endfunction
" ------------------------------------------------------------
" MatchDeleteHi: {{{2
" Delete a highlight given its id and in case there is no
" highlight associated with the id, do not produce an error.
" parameters:
" matchid : id of a highlight
" ------------------------------------------------------------
function! MatchDeleteHi(matchId)
try
call matchdelete(a:matchId)
catch /.*/
endtry
endfunction
" ------------------------------------------------------------
" GetSelectionId: {{{2
" Create a highlight and return its id.
" parameters:
" allocation : allocation associated with highlight
" ------------------------------------------------------------
function! GetSelectionId(allocation)
let pattern = GetMatchRange(a:allocation)
return matchadd("SelectedHi", pattern)
endfunction
" ------------------------------------------------------------
" ClearSelectionId: {{{2
" Delete a highlight given its id and in case there is no
" highlight associated with the id, do not produce an error.
" parameters:
" sid : id of a highlight
" ------------------------------------------------------------
function! ClearSelectionId(sid)
try
call matchdelete(a:sid)
catch /.*/
endtry
endfunction
" ------------------------------------------------------------
" ButtonFlashHi: {{{2
" Create a highlight flash
" parameters:
" allocation : allocation associated with highlight
" ------------------------------------------------------------
function! ButtonFlashHi(allocation)
" call forms#log("ButtonFlashHi: TOP")
let pattern = GetMatchRange(a:allocation)
let l:buttonflashId = matchadd("ButtonFlashHi", pattern)
redraw
sleep 200m
call matchdelete(l:buttonflashId)
" call forms#log("ButtonFlashHi: BOTTOM")
endfunction
" ------------------------------------------------------------
" ButtonGroupAddHi: {{{2
" Create a highlight and associate it with a button group.
" If the buttongroup already has a highlight, delete it.
" parameters:
" buttongroup : buttongroup to be associated with highlight
" allocation : allocation associated with highlight
" ------------------------------------------------------------
function! ButtonGroupAddHi(buttongroup, allocation)
" call forms#log("ButtonGroupAddHi")
call ButtonGroupDeleteHi(a:buttongroup)
let pattern = GetMatchRange(a:allocation)
let a:buttongroup.__selectId = matchadd("ToggleSelectionHi", pattern)
endfunction
" ------------------------------------------------------------
" ButtonGroupDeleteHi: {{{2
" Delete a highlight associated it with a button group if it
" exists.
" parameters:
" buttongroup : buttongroup associated with a highlight
" ------------------------------------------------------------
function! ButtonGroupDeleteHi(buttongroup)
" call forms#log("ButtonGroupDeleteHi")
if has_key(a:buttongroup, '__selectId')
call matchdelete(a:buttongroup.__selectId)
unlet a:buttongroup.__selectId
endif
endfunction
" ------------------------------------------------------------
" GetMatchRange: {{{2
" Create a match pattern given an allocation. The allocation
" can be on a single line or a region.
" parameters:
" allocation : allocation from which pattern is generated
" ------------------------------------------------------------
function! GetMatchRange(allocation)
let line = a:allocation.line
let column = a:allocation.column
let width = a:allocation.width
let height = a:allocation.height
let lnum1 = line
let lnum2 = line+height-1
let col1 = column
let col2 = column+width-1
if lnum1 == lnum2
let range = '\%'.lnum1.'l\%>'.(col1-1).'v.*\%<'.(col2+1).'v'
else
let range=
\'\%>'.(col1-1).'v'.
\'\%<'.(col2+1).'v'.
\'\%>'.(lnum1-1).'l'.
\'\%<'.(lnum2+1).'l'.
\'.'
endif
return range
endfunction
"-------------------------------------------------------------------------------
"-------------------------------------------------------------------------------
" Forms Input Character Stream management: {{{1
"-------------------------------------------------------------------------------
"-------------------------------------------------------------------------------
" Input characters and Events
" Character:
" A value returned by calling getchar()
" Event:
" A command (often results from mapping a character to command)
" An event is used for both giving commands to glyphs to be executed
" both as input and as return values for some methods and, additionally,
" an event is used to wrap Form results.
" All Events are a Dictionary with a 'type' key indicating the
" command type and, possibly, additional data.
" Event Types
" Exit
" Action: exit current viewer, if top viewer, no results data
" Also used as top-of-stack return value for Viewer
" The character <Esc> is mapped to Exit
" Cancel
" Action: exit form, no results data
" Also used as top-of-stack return value for Viewer
" Command
" Action: exit form, no result data, and execute command
" Context
" Action: generate context help Form with application specific help/info
" and developer tools
" Data: optional Point [line, column]
" The character <RightMouse> is mapped to Context
" Drag
" Action: none
" The character <LeftDrag> is mapped to Drag
" Release
" Action: none
" The character <LeftRelease> is mapped to Release
" NewFocus
" Action: find new focus based upon mouse coordinates
" The character <LeftMouse> is mapped to NewFocus
" Also, if a Viewer is the target of a Select Event, it is mapped
" to NewFocus
" NextFocus
" Action: go to next focus
" The characters <Tab>, <C-n> and <Down> is mapped to NextFocus
" The mouse <ScrollWheelDown> event is mapped to NextFocus
" PrevFocus
" Action: go to previous focus
" The characters <S-Tab>, <C-p> and <Up> is mapped to PrevFocus
" The mouse <ScrollWheelUp> event is mapped to PrevFocus
" FirstFocus
" Action: go to first focus glyph
" The character <Home> is mapped to FirstFocus
" LastFocus
" Action: go to last focus glyph
" The character <End> is mapped to LastFocus
" ReDraw
" Action: redraw Form in window
" ReDrawAll
" Action: redraw complete form
" ReFocus
" Action: View creates a list of glyphs that can get focus
" this event tells viewer to regenerate that list
" ReSize
" Action: Form does a requestedSize call on its body because a child has
" changed size or gone from invisible to visible
" Select
" Action: change focus and possibly glyph specific sub-selection
" Data: Point [line, column]
" SelectDouble
" Action: a left mouse double click occured
" Data: Point [line, column]
" The mouse <2-LeftMouse> event is mapped to SelectDouble
" Sleep
" Action: Viewer Event handling sleeps for given time.
" Data: time: Number (e.g., 10) or String (Number+'m' e.g., 200m)
" Used for visual testing
" Submit
" Action: exit form with result data
" Data: results from form
" Also used as top-of-stack return value for Viewer
" Special Key Types
" Down
" Produced by down key and viewer maps ScrollWheelDown to Down
" Up
" Produced by up key and viewer maps ScrollWheelUp to Up
" Left S-Left C-Left
" Right S-Right C-Right
" ScrollWheelDown S-ScrollWheelDown C-ScrollWheelDown
" ScrollWheelUp S-ScrollWheelUp C-ScrollWheelUp
" Space: mapped to Select
" CR: mapped to Select
" Del: generally, erase character in editor
" BS: generally, move over character in editor
" ------------------------------------------------------------
" forms#CheckInput: {{{2
" Makes sure that an object injected into the input stream
" is either an event (Dictionary with type attribute) or
" is a String or Number.
" Exceptions are thrown if it a illegal input object.
" parameters:
" input : object to be tested
" ------------------------------------------------------------
function! forms#CheckInput(input)
let type = type(a:input)
if type == g:self#DICTIONARY_TYPE
if ! exists("a:input.type")
throw "CheckEvent: No 'type' key for event: " . string(a:input)
endif
elseif type != g:self#STRING_TYPE && type != g:self#NUMBER_TYPE
throw "CheckEvent: Bad type, not DICTIONARY, STRING or NUMBER: " . string(a:input)
endif
endfunction
" ------------------------------------------------------------
" forms#InputsSame: {{{2
" Makes sure that two input objects are the same.
" Should only be called if both input1 and input2 have
" passed CheckEvent.
" For inputs of type event, then this only tests event.type
" equality
" parameters:
" input1 : object to be tested
" input2 : object to be tested
" ------------------------------------------------------------
function! forms#InputsSame(input1, input2)
let type1 = type(a:input1)
let type2 = type(a:input2)
if type1 != type2
return g:self#IS_FALSE
elseif type1 == g:self#DICTIONARY_TYPE
return input1.type == input2.type
elseif type1 == g:self#NUMBER_TYPE
return input1 == input2
else " type1 == g:self#STRING_TYPE
return input1 == input2
endif
endfunction
" ------------------------------------------------------------
" s:inputlist: {{{2
" Input queue holds input objects which will be processed
" before characters are read from Vim's getchar()
" ------------------------------------------------------------
let s:inputlist = []
" ------------------------------------------------------------
" forms#PrependInput: {{{2
" Add input to front of input queue
" parameters:
" input : object to be prepended
" ------------------------------------------------------------
function! forms#PrependInput(input)
call forms#CheckInput(a:input)
call insert(s:inputlist, a:input)
endfunction
" ------------------------------------------------------------
" forms#PrependUniqueInput: {{{2
" Add input to front of input queue but only if it does not
" equals the current front of queue.
" parameters:
" input : object to be prepended
" ------------------------------------------------------------
function! forms#PrependUniqueInput(input)
call forms#CheckInput(a:input)
if len(s:inputlist) == 0 || ! forms#InputsSame(a:input, s:inputlist[0])
call insert(s:inputlist, a:input)
endif
endfunction
" ------------------------------------------------------------
" forms#AppendInput: {{{2
" Add input to back of input queue
" parameters:
" input : object to be appended
" ------------------------------------------------------------
function! forms#AppendInput(input)
call forms#CheckInput(a:input)
call add(s:inputlist, a:input)
endfunction
" ------------------------------------------------------------
" forms#AppendUniqueInput: {{{2
" Add input to back of input queue but only if it does not
" equals the current back of queue.
" parameters:
" input : object to be appended
" ------------------------------------------------------------
function! forms#AppendUniqueInput(input)
call forms#CheckInput(a:input)
if len(s:inputlist) == 0 || ! forms#InputsSame(a:input, s:inputlist[len(s:inputlist)-1])
call add(s:inputlist, a:input)
endif
endfunction
" ------------------------------------------------------------
" forms#ClearVimInputStream: {{{2
" Clears the Vim character input stream but NOT the inputlist
" parameters: NONE
" ------------------------------------------------------------
function! forms#ClearVimInputStream()
while 1
if getchar(0) == 0 | break | endif
endwhile
endfunction
" ------------------------------------------------------------
" forms#ClearInputList: {{{2
" Clears the input list (inputlist)
" parameters: NONE
" ------------------------------------------------------------
function! forms#ClearInputList()
let s:inputlist = []
endfunction
" ------------------------------------------------------------
" forms#GetInput: {{{2
" Get the next input. If the inputlist is not empty remove
" and return its first entry, otherwise return Vim input
" from the getchar() call.
" parameters: NONE
" ------------------------------------------------------------
function! forms#GetInput()
return empty(s:inputlist) ? getchar() : remove(s:inputlist, 0)
if 0
if ! empty(s:inputlist)
return remove(s:inputlist, 0)
else
let c = getchar()
" call forms#log("GetInput: c=" . c)
let next = getchar(0)
" call forms#log("GetInput: o next=" . next)
while next != 0 && c == next
let next = getchar(0)
" call forms#log("GetInput: i next=" . next)
endwhile
if next != 0
call insert(s:inputlist, next)
endif
return c
endif
endif
endfunction
"-------------------------------------------------------------------------------
" Forms Utilities: {{{1
" Gets Utility object that has numerous support methods
" parameters: NONE
"-------------------------------------------------------------------------------
if g:self#IN_DEVELOPMENT_MODE
if exists("g:forms_Util")
unlet g:forms_Util
endif
endif
if !exists("g:forms_Util")
let g:forms_Util = { }
" ------------------------------------------------------------
" g:forms_Util.nullGlyph {{{2
" Gets a null glyph; no actions, size or rendering.
" parameters: NONE
" ------------------------------------------------------------
function! FORMS_UTIL_nullGlyph() dict
if ! exists("g:forms_Util.__nullGlyph")
let g:forms_Util.__nullGlyph = forms#newNullGlyph({})
endif
return g:forms_Util.__nullGlyph
endfunction
let g:forms_Util.nullGlyph = function("FORMS_UTIL_nullGlyph")
" ------------------------------------------------------------
" g:forms_Util.emptyAction {{{2
" Gets action that does nothing
" parameters: NONE
" ------------------------------------------------------------
function! FORMS_UTIL_emptyAction() dict
if ! exists("g:forms_Util.__emptyAction")
let g:forms_Util.__emptyAction = forms#newAction({})
endif
return g:forms_Util.__emptyAction
endfunction
let g:forms_Util.emptyAction = function("FORMS_UTIL_emptyAction")
" ------------------------------------------------------------
" g:forms_Util.checkHAlignment {{{2
" Checks if halignment parameter is a valid horizontal
" alignment value. Either
" float: 0 <= halignment <= 1.0
" character: 'L' 'C' or 'R'
" If the parameter is not valid, and exception is thrown.
" parameters:
" halignment : attribute value being checked
" name : name of component requesting check
" ------------------------------------------------------------
function! FORMS_UTIL_checkHAlignment(halignment, name) dict
if type(a:halignment) == g:self#FLOAT_TYPE
if a:halignment < 0.0
throw "" . a:name . ": alignment float value < 0.0: " . a:halignment
elseif a:halignment > 1.0
throw "" . a:name . ": alignment float value > 1.0: " . a:halignment
endif
elseif type(a:halignment) == g:self#STRING_TYPE
if a:halignment != "L" && a:halignment != "C" && a:halignment != "R"
throw "" . a:name . ": halignment string value != L or C or R: " . a:halignment
endif
else
throw "" . a:name . ": bad halignment type: " . a:halignment
endif
endfunction
let g:forms_Util.checkHAlignment = function("FORMS_UTIL_checkHAlignment")
" ------------------------------------------------------------
" g:forms_Util.checkVAlignment {{{2
" Checks if valignment parameter is a valid vertical
" alignment value. Either
" float: 0 <= valignment <= 1.0
" character: 'T' 'C' or 'B'
" If the parameter is not valid, and exception is thrown.
" parameters:
" valignment : attribute value being checked
" name : name of component requesting check
" ------------------------------------------------------------