-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEnhancedCommentify.vim
1607 lines (1446 loc) · 46.1 KB
/
EnhancedCommentify.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
" EnhancedCommentify.vim
" Maintainer: Meikel Brandmeyer <Brandels_Mikesh@web.de>
" Version: 2.3
" Last Change: Wednesday, February 20th, 2008
" License:
" Copyright (c) 2008 Meikel Brandmeyer, Frankfurt am Main
" All rights reserved.
"
" Redistribution and use in source and binary form are permitted provided
" that the following conditions are met:
"
" 1. Redistribition of source code must retain the above copyright
" notice, this list of conditions and the following disclaimer.
"
" 2. Redistributions in binary form must reproduce the above copyright
" notice, this list of conditions and the following disclaimer in the
" documentation and/or other materials provided with the distribution.
"
" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS "AS IS" AND
" ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
" SUCH DAMAGE.
" Description:
" This is a (well... more or less) simple script to comment lines in a program.
" Currently supported languages are C, C++, PHP, the vim scripting
" language, python, HTML, Perl, LISP, Tex, Shell, CAOS and others.
" Bugfixes:
" 2.3
" Fixed type 'apacha' -> 'apache' (thanks to Brian Neu)
" Fixed nested comment escapes strings. (thanks to Samuel Ferencik)
" Fixed broken keyboard mappings when wcm was set to <Tab>.
" (thanks to xpatriotx)
" 2.2
" Fixed problem with UseSyntax (thanks to Pieter Naaijkens)
" Fixed typo in ParseCommentsOp (commstr -> commStr).
" Fixed support for ocaml (thanks to Zhang Le)
" 2.1
" Fixed problems with alignement when a line contains tabs
" Fixed (resp. cleaned up) issues with overrideEL (thanks to Steve Hall)
" Fixed problems with javascript detection (thanks to Brian Neu)
" Changed Buffer init to BufWinEnter in order to use the modelines.
" 2.0
" Fixed invalid expression '\'' -> "'" (thanks to Zak Beck)
" Setting AltOpen/AltClose to '' (ie. disabling it) would
" insert '/*' resp. '*/' for character in a line (thanks to Ben Kibbey)
" 1.8
" Backslashes in comment symbols should not be escaped.
" typo (commensSymbol -> commentSymbol) (thanks to Steve Butts)
" typo (== -> =)
" Fixed hardwired '|+'-'+|' pair.
" 1.7
" Lines were not correctly decommentified, when there was whitespace
" at the beginning of the line. (thanks to Xiangjiang Ma)
" Fixed error detecting '*sh' filetypes.
" 1.3
" hlsearch was set unconditionally (thanks to Mary Ellen Foster)
" made function silent (thanks to Mare Ellen Foster)
" Changelog:
" 2.3
" Added support for viki/deplate (thanks to Thomas Link)
" Added support for xslt/xsd/mail (thanks to Stefano Zacchiroli)
" Added callback-functionality to enable extensions without the
" need of modification directly in the script.
" 2.2
" Added possibility to override the modes, in which keybindings are
" defined.
" Keybindings may be defined local to every buffer now.
" If a filetype is unknown, one can turn off the keybindings now.
" 2.1
" Removed any cursor movement. The script should now be free of
" side-effects.
" The script now uses &commentstring to determine the right
" comment strings. Fallback is still the ugly if-thingy.
" Script can now interpret &comments in order to add a middle
" string in blocks.
" Added EnhancedCommentifySet for use by other scripts. (Necessary?)
" Added MultiPartBlocks for languages with multipart-comments.
" Added parsing for comments option if using MultiPartBlocks.
" 2.0
" IMPORTANT: EnhancedCommentify is now licensed under BSD license
" for distribution with Cream! However this shouldn't
" change anything...
" useBlockIndent does no longer depend on respectIndent.
" Added code to cope with 'C' in '&cpo'. (thanks to Luc Hermitte
" for pointing this out!)
" Added EnhCommentifyIdentFrontOnly option.
" All options are now handled on a per buffer basis. So options
" can be overriden for different buffers.
" 1.9
" Filetype is now recognized via regular expressions.
" All known filetypes are (more or less) supported.
" Decomments multipart-block comments.
" Added RespectIndent, AlignRight and synID-guessing.
" Switched to buffer variables.
" 1.8
" Added Ada support. (thanks to Preben Randhol)
" Added Latte support.
" Added blocksupport and possibility to specify action (comment or
" decomment). It's also possible to guess the action line by line or
" using the first line of a block.
" Thanks to Xiangjiang Ma and John Orr for the rich feedback on these
" issues.
" Decomments /*foo();*/, when PrettyComments is set.
" Added 'vhdl' and 'verilog'. (thanks to Steve Butts)
" 1.7
" Added different options to control behaviour of the plugin.
" Changed default Keybindings to proper plugin settings.
" 1.6
" Now supports 'm4', 'config', 'automake'
" 'vb', 'aspvbs', 'plsql' (thanks to Zak Beck)
" 1.5
" Now supports 'java', 'xml', 'jproperties'. (thanks to Scott Stirling)
" 1.4
" Lines containing only whitespace are now considered empty.
" Added Tcl support.
" Multipart comments are now escaped with configurable alternative
" strings. Prevents nesting errors (eg. /**/*/ in C)
" 1.3
" Doesn't break lines like
" foo(); /* bar */
" when doing commentify.
" Install Details:
" Simply drop this file into your $HOME/.vim/plugin directory.
if exists("DidEnhancedCommentify")
finish
endif
let DidEnhancedCommentify = 1
let s:savedCpo = &cpo
set cpo-=C
" Note: These must be defined here, since they are used during
" initialisation.
"
" InitBooleanVariable(confVar, scriptVar, defaultVal)
" confVar -- name of the configuration variable
" scriptVar -- name of the variable to set
" defaultVal -- default value
"
" Tests on existence of configuration variable and sets scriptVar
" according to its contents.
"
function s:InitBooleanVariable(confVar, scriptVar, defaultVal)
let regex = a:defaultVal ? 'no*' : 'ye*s*'
if exists(a:confVar) && {a:confVar} =~? regex
let {a:scriptVar} = !a:defaultVal
else
let {a:scriptVar} = a:defaultVal
endif
endfunction
"
" InitStringVariable(confVar, scriptVar, defaultVal)
" confVar -- name of the configuration variable
" scriptVar -- name of the variable to set
" defaultVal -- default value
"
" Tests on existence of configuration variable and sets scriptVar
" to its contents.
"
function s:InitStringVariable(confVar, scriptVar, defaultVal)
if exists(a:confVar)
execute "let ". a:scriptVar ." = ". a:confVar
else
let {a:scriptVar} = a:defaultVal
endif
endfunction
"
" InitScriptVariables(nameSpace)
" nameSpace -- may be "g" for global or "b" for local
"
" Initialises the script variables.
"
function s:InitScriptVariables(nameSpace)
let ns = a:nameSpace " just for abbreviation
let lns = (ns == "g") ? "s" : "b" " 'local namespace'
" Comment escape strings...
call s:InitStringVariable(ns .":EnhCommentifyAltOpen", lns .":ECaltOpen",
\ s:ECaltOpen)
call s:InitStringVariable(ns .":EnhCommentifyAltClose", lns .":ECaltClose",
\ s:ECaltClose)
call s:InitBooleanVariable(ns .":EnhCommentifyIgnoreWS", lns .":ECignoreWS",
\ s:ECignoreWS)
" Adding a space between comment strings and code...
if exists(ns .":EnhCommentifyPretty")
if {ns}:EnhCommentifyPretty =~? 'ye*s*'
let {lns}:ECprettyComments = ' '
let {lns}:ECprettyUnComments = ' \='
else
let {lns}:ECprettyComments = ''
let {lns}:ECprettyUnComments = ''
endif
else
let {lns}:ECprettyComments = s:ECprettyComments
let {lns}:ECprettyUnComments = s:ECprettyUnComments
endif
" Identification string settings...
call s:InitStringVariable(ns .":EnhCommentifyIdentString",
\ lns .":ECidentFront", s:ECidentFront)
let {lns}:ECidentBack =
\ (exists(ns .":EnhCommentifyIdentFrontOnly")
\ && {ns}:EnhCommentifyIdentFrontOnly =~? 'ye*s*')
\ ? ''
\ : {lns}:ECidentFront
" Wether to use syntax items...
call s:InitBooleanVariable(ns .":EnhCommentifyUseSyntax",
\ lns .":ECuseSyntax", s:ECuseSyntax)
" Should the script respect line indentation, when inserting strings?
call s:InitBooleanVariable(ns .":EnhCommentifyRespectIndent",
\ lns .":ECrespectIndent", s:ECrespectIndent)
" Keybindings...
call s:InitBooleanVariable(ns .":EnhCommentifyUseAltKeys",
\ lns .":ECuseAltKeys", s:ECuseAltKeys)
call s:InitBooleanVariable(ns .":EnhCommentifyBindPerBuffer",
\ lns .":ECbindPerBuffer", s:ECbindPerBuffer)
call s:InitBooleanVariable(ns .":EnhCommentifyBindInNormal",
\ lns .":ECbindInNormal", s:ECbindInNormal)
call s:InitBooleanVariable(ns .":EnhCommentifyBindInInsert",
\ lns .":ECbindInInsert", s:ECbindInInsert)
call s:InitBooleanVariable(ns .":EnhCommentifyBindInVisual",
\ lns .":ECbindInVisual", s:ECbindInVisual)
call s:InitBooleanVariable(ns .":EnhCommentifyUserBindings",
\ lns .":ECuserBindings", s:ECuserBindings)
call s:InitBooleanVariable(ns .":EnhCommentifyTraditionalMode",
\ lns .":ECtraditionalMode", s:ECtraditionalMode)
call s:InitBooleanVariable(ns .":EnhCommentifyFirstLineMode",
\ lns .":ECfirstLineMode", s:ECfirstLineMode)
call s:InitBooleanVariable(ns .":EnhCommentifyUserMode",
\ lns .":ECuserMode", s:ECuserMode)
call s:InitBooleanVariable(ns .":EnhCommentifyBindUnknown",
\ lns .":ECbindUnknown", s:ECbindUnknown)
" Block stuff...
call s:InitBooleanVariable(ns .":EnhCommentifyAlignRight",
\ lns .":ECalignRight", s:ECalignRight)
call s:InitBooleanVariable(ns .":EnhCommentifyUseBlockIndent",
\ lns .":ECuseBlockIndent", s:ECuseBlockIndent)
call s:InitBooleanVariable(ns .":EnhCommentifyMultiPartBlocks",
\ lns .":ECuseMPBlock", s:ECuseMPBlock)
call s:InitBooleanVariable(ns .":EnhCommentifyCommentsOp",
\ lns .":ECuseCommentsOp", s:ECuseCommentsOp)
let {lns}:ECsaveWhite = ({lns}:ECrespectIndent
\ || {lns}:ECignoreWS || {lns}:ECuseBlockIndent)
\ ? '\(\s*\)'
\ : ''
if !{lns}:ECrespectIndent
let {lns}:ECuseBlockIndent = 0
endif
if {lns}:ECrespectIndent
let {lns}:ECrespectWhite = '\1'
let {lns}:ECignoreWhite = ''
elseif {lns}:ECignoreWS
let {lns}:ECrespectWhite = ''
let {lns}:ECignoreWhite = '\1'
else
let {lns}:ECrespectWhite = ''
let {lns}:ECignoreWhite = ''
endif
" Using comments option, doesn't make sense without useMPBlock
"if lns == 'b' && b:ECuseCommentsOp
" let b:ECuseMPBlock = 1
"endif
endfunction
"
" EnhancedCommentifySet(option, value, ...)
" option -- which option
" value -- value which will be asigned to the option
"
" The purpose of this function is mainly to act as an interface to the
" outer world. It hides the internally used variables.
"
function EnhancedCommentifySet(option, value)
if a:option == 'AltOpen'
let oldval = b:ECaltOpen
let b:ECaltOpen = a:value
elseif a:option == 'AltClose'
let oldval = b:ECaltClose
let b:ECaltClose = a:value
elseif a:option == 'IdentString'
let oldval = b:ECidentFront
let b:ECidentFront = a:value
elseif a:option == 'IdentFrontOnly'
let oldval = (b:ECidentBack == '') ? 'Yes' : 'No'
let b:ECidentBack = (a:value =~? 'ye*s*') ? '' : b:ECidentFront
elseif a:option == 'RespectIndent'
let oldval = b:ECrespectIndent
let b:ECrespectIndent = (a:value =~? 'ye*s*') ? 1 : 0
elseif a:option == 'IgnoreWS'
let oldval = b:ECignoreWS
let b:ECignoreWS = (a:value =~? 'ye*s*') ? 1 : 0
elseif a:option == 'Pretty'
let oldval = (b:ECprettyComments == ' ') ? 'Yes' : 'No'
if a:value =~? 'ye*s*'
let b:ECprettyComments = ' '
let b:ECprettyUnComments = ' \='
else
let b:ECprettyComments = ''
let b:ECprettyUnComments = ''
endif
elseif a:option == 'MultiPartBlocks'
let oldval = b:ECuseMPBlock
let b:ECuseMPBlock = (a:value =~? 'ye*s*') ? 1 : 0
elseif a:option == 'CommentsOp'
let oldval = b:ECuseCommentsOp
let b:ECuseCommentsOp = (a:value =~? 'ye*s*') ? 1 : 0
elseif a:option == 'UseBlockIndent'
let oldval = b:ECuseBlockIndent
let b:ECuseBlockIndent = (a:value =~? 'ye*s*') ? 1 : 0
elseif a:option == 'AlignRight'
let oldval = b:ECalignRight
let b:ECalignRight = (a:value =~? 'ye*s*') ? 1 : 0
elseif a:option == 'UseSyntax'
let oldval = b:ECuseSyntax
let b:ECuseSyntax = (a:value =~? 'ye*s*') ? 1 : 0
else
if (has("dialog_gui") && has("gui_running"))
call confirm("EnhancedCommentifySet: Unknwon option '"
\ . option . "'")
else
echohl ErrorMsg
echo "EnhancedCommentifySet: Unknown option '". option ."'"
echohl None
endif
endif
if oldval == 1
let oldval = 'Yes'
elseif oldval == 0
let oldval = 'No'
endif
return oldval
endfunction
" Initial settings.
"
" Setting the default options resp. taking user preferences.
if !exists("g:EnhCommentifyUserMode")
\ && !exists("g:EnhCommentifyFirstLineMode")
\ && !exists("g:EnhCommentifyTraditionalMode")
\ && !exists("g:EnhCommentifyUserBindings")
let g:EnhCommentifyTraditionalMode = 'Yes'
endif
" These will be the default settings for the script:
let s:ECaltOpen = "|+"
let s:ECaltClose = "+|"
let s:ECignoreWS = 1
let s:ECprettyComments = ''
let s:ECprettyUnComments = ''
let s:ECidentFront = ''
let s:ECuseSyntax = 0
let s:ECrespectIndent = 0
let s:ECalignRight = 0
let s:ECuseBlockIndent = 0
let s:ECuseMPBlock = 0
let s:ECuseCommentsOp = 0
let s:ECuseAltKeys = 0
let s:ECbindPerBuffer = 0
let s:ECbindInNormal = 1
let s:ECbindInInsert = 1
let s:ECbindInVisual = 1
let s:ECuserBindings = 0
let s:ECtraditionalMode = 0
let s:ECfirstLineMode = 0
let s:ECuserMode = 1
let s:ECbindUnknown = 1
" Now initialise the global defaults with the preferences set
" by the user in his .vimrc. Settings local to a buffer will be
" done later on, when the script is first called in a buffer.
"
call s:InitScriptVariables("g")
" Globally used variables with some initialisation.
" FIXME: explain what they are good for
"
let s:Action = 'guess'
let s:firstOfBlock = 1
let s:blockAction = 'comment'
let s:blockIndentRegex = ''
let s:blockIndent = 0
let s:inBlock = 0
let s:tabConvert = ''
let s:overrideEmptyLines = 0
let s:emptyLines = 'no'
let s:maxLen = 0
function EnhancedCommentifyInitBuffer()
if !exists("b:ECdidBufferInit")
call s:InitScriptVariables("b")
if !exists("b:EnhCommentifyFallbackTest")
let b:EnhCommentifyFallbackTest = 0
endif
call s:GetFileTypeSettings(&ft)
call s:CheckPossibleEmbedding(&ft)
"
" If the filetype is not supported and the user wants us to, we do not
" add keybindings.
"
if s:ECbindPerBuffer
if b:ECcommentOpen != "" || b:ECbindUnknown
call s:SetKeybindings("l")
endif
endif
let b:ECdidBufferInit = 1
let b:ECsyntax = &ft
endif
endfunction
autocmd BufWinEnter,BufNewFile * call EnhancedCommentifyInitBuffer()
"
" EnhancedCommentify(emptyLines, action, ...)
" overrideEL -- commentify empty lines
" may be 'yes', 'no' or '' for guessing
" action -- action which should be executed:
" * guess:
" toggle commetification (old behaviour)
" * comment:
" comment lines
" * decomment:
" decomment lines
" * first:
" use first line of block to determine action
" a:1, a:2 -- first and last line of block, which should be
" processed.
"
" Commentifies the current line.
"
function EnhancedCommentify(overrideEL, action, ...)
if a:overrideEL != ''
let s:overrideEmptyLines = 1
endif
" Now do the buffer initialisation. Every buffer will get
" it's pendant to a global variable (eg. s:ECalignRight -> b:ECalignRight).
" The local variable is actually used, whereas the global variable
" holds the defaults from the user's .vimrc. In this way the settings
" can be overriden for single buffers.
"
" NOTE: Buffer init is done by autocommands now.
"
let b:ECemptyLines = a:overrideEL
" The language is not supported.
if b:ECcommentOpen == ''
if (has("dialog_gui") && has("gui_running"))
call confirm("This filetype is currently _not_ supported!\n"
\ ."Please consider contacting the author in order"
\ ." to add this filetype.", "", 1, "Error")
else
echohl ErrorMsg
echo "This filetype is currently _not_ supported!"
echo "Please consider contacting the author in order to add"
echo "this filetype in future releases!"
echohl None
endif
return
endif
let lnum = line(".")
" Now some initialisations...
let s:Action = a:action
" FIXME: Is there really _no_ function to simplify this???
" (Maybe something like 'let foo = 8x" "'?)
if s:tabConvert == '' && strlen(s:tabConvert) != &tabstop
let s:tabConvert = ''
let i = 0
while i < &tabstop
let s:tabConvert = s:tabConvert .' '
let i = i + 1
endwhile
endif
if a:0 == 2
let s:startBlock = a:1
let s:i = a:1
let s:endBlock = a:2
let s:inBlock = 1
else
let s:startBlock = lnum
let s:i = lnum
let s:endBlock = lnum
let s:inBlock = 0
endif
if b:ECuseSyntax && b:ECpossibleEmbedding
let column = indent(s:startBlock) + 1
if !&expandtab
let rem = column % &tabstop
let column = ((column - rem) / &tabstop) + rem
endif
call s:CheckSyntax(s:startBlock, column)
endif
" Get the indent of the less indented line of the block.
if s:inBlock && (b:ECuseBlockIndent || b:ECalignRight)
call s:DoBlockComputations(s:startBlock, s:endBlock)
endif
while s:i <= s:endBlock
let lineString = getline(s:i)
let lineString = s:TabsToSpaces(lineString)
" If we should comment "empty" lines, we have to add
" the correct indent, if we use blockIndent.
if b:ECemptyLines =~? 'ye*s*'
\ && b:ECuseBlockIndent
\ && lineString =~ "^\s*$"
let i = 0
while i < s:blockIndent
let lineString = " " . lineString
let i = i + 1
endwhile
endif
" Don't comment empty lines.
if lineString !~ "^\s*$"
\ || b:ECemptyLines =~? 'ye*s*'
if b:ECcommentClose != ''
let lineString = s:CommentifyMultiPart(lineString,
\ b:ECcommentOpen,
\ b:ECcommentClose,
\ b:ECcommentMiddle)
else
let lineString = s:CommentifySinglePart(lineString,
\ b:ECcommentOpen)
endif
endif
" Revert the above: If the line is "empty" and we
" used blockIndent, we remove the spaces.
" FIXME: Why does "^\s*$" not work?
if b:ECemptyLines =~? 'ye*s*'
\ && b:ECuseBlockIndent
\ && lineString =~ "^" . s:blockIndentRegex ."\s*$"
let lineString =
\ substitute(lineString, s:blockIndentRegex,
\ '', '')
endif
let lineString = s:SpacesToTabs(lineString)
call setline(s:i, lineString)
let s:i = s:i + 1
let s:firstOfBlock = 0
endwhile
let s:firstOfBlock = 1
endfunction
"
" DoBlockComputations(start, end)
" start -- number of first line
" end -- number of last line
"
" This function does some computations which are necessary for useBlockIndent
" and alignRight. ie. find smallest indent and longest line.
"
function s:DoBlockComputations(start, end)
let i = a:start
let len = 0
let amount = 100000 " this should be enough ...
while i <= a:end
if b:ECuseBlockIndent && getline(i) !~ '^\s*$'
let cur = indent(i)
if cur < amount
let amount = cur
endif
endif
if b:ECalignRight
let cur = s:GetLineLen(s:TabsToSpaces(getline(i)),
\ s:GetLineLen(b:ECcommentOpen, 0)
\ + strlen(b:ECprettyComments))
if b:ECuseMPBlock
let cur = cur + s:GetLineLen(b:ECcommentOpen, 0)
\ + strlen(b:ECprettyComments)
endif
if len < cur
let len = cur
endif
endif
let i = i + 1
endwhile
if b:ECuseBlockIndent
if amount > 0
let regex = '\( \{'. amount .'}\)'
else
let regex = ''
endif
let s:blockIndentRegex = regex
let s:blockIndent = amount
endif
if b:ECalignRight
let s:maxLen = len
endif
endfunction
"
" CheckSyntax(line, column)
" line -- line of line
" column -- column of line
" Check what syntax is active during call of main function. First hit
" wins. If the filetype changes during the block, we ignore that.
" Adjust the filetype if necessary.
"
function s:CheckSyntax(line, column)
let ft = ""
let synFiletype = synIDattr(synID(a:line, a:column, 1), "name")
" FIXME: This feature currently relies on a certain format
" of the names of syntax items: the filetype must be prepended
" in lowwer case letters, followed by at least one upper case
" letter.
if match(synFiletype, '\l\+\u') == 0
let ft = substitute(synFiletype, '^\(\l\+\)\u.*$', '\1', "")
endif
if ft == ""
execute "let specialCase = ". b:EnhCommentifyFallbackTest
if specialCase
let ft = b:EnhCommentifyFallbackValue
else
" Fallback: If nothing holds, use normal filetype!
let ft = &ft
endif
endif
" Nothing changed!
if ft == b:ECsyntax
return
endif
let b:ECsyntax = ft
call s:GetFileTypeSettings(ft)
endfunction
"
" GetFileTypeSettings(ft)
" ft -- filetype
"
" This functions sets some buffer-variables, which control the comment
" strings and 'empty lines'-handling.
"
function s:GetFileTypeSettings(ft)
let fileType = a:ft
" If we find nothing appropriate this is the default.
let b:ECcommentOpen = ''
let b:ECcommentClose = ''
if exists("g:EnhCommentifyCallbackExists")
call EnhCommentifyCallback(fileType)
" Check whether the callback did yield a result.
" If so we use it. The user nows, what he's doing.
if b:ECcommentOpen != ''
return
endif
endif
" I learned about the commentstring option. Let's use it.
" For now we ignore it, if it is "/*%s*/". This is the
" default. We cannot check wether this is default or C or
" something other like CSS, etc. We have to wait, until the
" filetypes adopt this option.
if &commentstring != "/*%s*/" && !b:ECuseSyntax
let b:ECcommentOpen =
\ substitute(&commentstring, '%s.*', "", "")
let b:ECcommentClose =
\ substitute(&commentstring, '.*%s', "", "")
" Multipart comments:
elseif fileType =~ '^\(c\|b\|css\|csc\|cupl\|indent\|jam\|lex\|lifelines\|'.
\ 'lite\|nqc\|phtml\|progress\|rexx\|rpl\|sas\|sdl\|sl\|'.
\ 'strace\|xpm\|yacc\)$'
let b:ECcommentOpen = '/*'
let b:ECcommentClose = '*/'
elseif fileType =~ '^\(html\|xhtml\|xml\|xslt\|xsd\|dtd\|sgmllnx\)$'
let b:ECcommentOpen = '<!--'
let b:ECcommentClose = '-->'
elseif fileType =~ '^\(sgml\|smil\)$'
let b:ECcommentOpen = '<!'
let b:ECcommentClose = '>'
elseif fileType == 'atlas'
let b:ECcommentOpen = 'C'
let b:ECcommentClose = '$'
elseif fileType =~ '^\(catalog\|sgmldecl\)$'
let b:ECcommentOpen = '--'
let b:ECcommentClose = '--'
elseif fileType == 'dtml'
let b:ECcommentOpen = '<dtml-comment>'
let b:ECcommentClose = '</dtml-comment>'
elseif fileType == 'htmlos'
let b:ECcommentOpen = '#'
let b:ECcommentClose = '/#'
elseif fileType =~ '^\(jgraph\|lotos\|mma\|modula2\|modula3\|pascal\|'.
\ 'ocaml\|sml\)$'
let b:ECcommentOpen = '(*'
let b:ECcommentClose = '*)'
elseif fileType == 'jsp'
let b:ECcommentOpen = '<%--'
let b:ECcommentClose = '--%>'
elseif fileType == 'model'
let b:ECcommentOpen = '$'
let b:ECcommentClose = '$'
elseif fileType == 'st'
let b:ECcommentOpen = '"'
let b:ECcommentClose = '"'
elseif fileType =~ '^\(tssgm\|tssop\)$'
let b:ECcommentOpen = 'comment = "'
let b:ECcommentClose = '"'
" Singlepart comments:
elseif fileType =~ '^\(ox\|cpp\|php\|php.laravel\|java\|verilog\|acedb\|ch\|clean\|'.
\ 'clipper\|cs\|dot\|dylan\|hercules\|idl\|ishd\|javascript\|'.
\ 'kscript\|mel\|named\|openroad\|pccts\|pfmain\|pike\|'.
\ 'pilrc\|plm\|pov\|rc\|scilab\|specman\|tads\|tsalt\|uc\|'.
\ 'xkb\)$'
let b:ECcommentOpen = '//'
let b:ECcommentClose = ''
elseif fileType =~ '^\(vim\|abel\)$'
let b:ECcommentOpen = '"'
let b:ECcommentClose = ''
elseif fileType =~ '^\(lisp\|scheme\|scsh\|amiga\|asm\|asm68k\|bindzone\|'.
\ 'def\|dns\|dosini\|dracula\|dsl\|idlang\|iss\|jess\|kix\|'.
\ 'masm\|monk\|nasm\|ncf\|omnimark\|pic\|povini\|rebol\|'.
\ 'registry\|samba\|skill\|smith\|tags\|tasm\|tf\|winbatch\|'.
\ 'wvdial\|z8a\)$'
let b:ECcommentOpen = ';'
let b:ECcommentClose = ''
elseif fileType =~ '^\(python\|perl\|[^w]*sh$\|tcl\|jproperties\|make\|'.
\ 'robots\|apache\|apachestyle\|awk\|bc\|cfg\|cl\|conf\|'.
\ 'crontab\|diff\|ecd\|elmfilt\|eterm\|expect\|exports\|'.
\ 'fgl\|fvwm\|gdb\|gnuplot\|gtkrc\|hb\|hog\|ia64\|icon\|'.
\ 'inittab\|lftp\|lilo\|lout\|lss\|lynx\|maple\|mush\|'.
\ 'muttrc\|nsis\|ora\|pcap\|pine\|po\|procmail\|'.
\ 'psf\|ptcap\|r\|radiance\|ratpoison\|readline\remind\|'.
\ 'ruby\|screen\|sed\|sm\|snnsnet\|snnspat\|snnsres\|spec\|'.
\ 'squid\|terminfo\|tidy\|tli\|tsscl\|vgrindefs\|vrml\|'.
\ 'wget\|wml\|xf86conf\|xmath\)$'
let b:ECcommentOpen = '#'
let b:ECcommentClose = ''
elseif fileType == 'webmacro'
let b:ECcommentOpen = '##'
let b:ECcommentClose = ''
elseif fileType == 'ppwiz'
let b:ECcommentOpen = ';;'
let b:ECcommentClose = ''
elseif fileType == 'latte'
let b:ECcommentOpen = '\\;'
let b:ECcommentClose = ''
elseif fileType =~ '^\(tex\|abc\|erlang\|ist\|lprolog\|matlab\|mf\|'.
\ 'postscr\|ppd\|prolog\|simula\|slang\|slrnrc\|slrnsc\|'.
\ 'texmf\|viki\|virata\)$'
let b:ECcommentOpen = '%'
let b:ECcommentClose = ''
elseif fileType =~ '^\(caos\|cterm\|form\|foxpro\|sicad\|snobol4\)$'
let b:ECcommentOpen = '*'
let b:ECcommentClose = ''
elseif fileType =~ '^\(m4\|config\|automake\)$'
let b:ECcommentOpen = 'dnl '
let b:ECcommentClose = ''
elseif fileType =~ '^\(vb\|aspvbs\|ave\|basic\|elf\|lscript\)$'
let b:ECcommentOpen = "'"
let b:ECcommentClose = ''
elseif fileType =~ '^\(plsql\|vhdl\|ahdl\|ada\|asn\|csp\|eiffel\|gdmo\|'.
\ 'haskell\|lace\|lua\|mib\|sather\|sql\|sqlforms\|sqlj\|'.
\ 'stp\)$'
let b:ECcommentOpen = '--'
let b:ECcommentClose = ''
elseif fileType == 'abaqus'
let b:ECcommentOpen = '**'
let b:ECcommentClose = ''
elseif fileType =~ '^\(aml\|natural\|vsejcl\)$'
let b:ECcommentOpen = '/*'
let b:ECcommentClose = ''
elseif fileType == 'ampl'
let b:ECcommentOpen = '\\#'
let b:ECcommentClose = ''
elseif fileType == 'bdf'
let b:ECcommentOpen = 'COMMENT '
let b:ECcommentClose = ''
elseif fileType == 'btm'
let b:ECcommentOpen = '::'
let b:ECcommentClose = ''
elseif fileType == 'dcl'
let b:ECcommentOpen = '$!'
let b:ECcommentClose = ''
elseif fileType == 'dosbatch'
let b:ECcommentOpen = 'rem '
let b:ECcommentClose = ''
elseif fileType == 'focexec'
let b:ECcommentOpen = '-*'
let b:ECcommentClose = ''
elseif fileType == 'forth'
let b:ECcommentOpen = '\\ '
let b:ECcommentClose = ''
elseif fileType =~ '^\(fortran\|inform\|sqr\|uil\|xdefaults\|'.
\ 'xmodmap\|xpm2\)$'
let b:ECcommentOpen = '!'
let b:ECcommentClose = ''
elseif fileType == 'gp'
let b:ECcommentOpen = '\\\\'
let b:ECcommentClose = ''
elseif fileType =~ '^\(master\|nastran\|sinda\|spice\|tak\|trasys\)$'
let b:ECcommentOpen = '$'
let b:ECcommentClose = ''
elseif fileType == 'nroff' || fileType == 'groff'
let b:ECcommentOpen = ".\\\\\""
let b:ECcommentClose = ''
elseif fileType == 'opl'
let b:ECcommentOpen = 'REM '
let b:ECcommentClose = ''
elseif fileType == 'texinfo'
let b:ECcommentOpen = '@c '
let b:ECcommentClose = ''
elseif fileType == 'mail'
let b:ECcommentOpen = '>'
let b:ECcommentClose = ''
endif
if b:ECuseCommentsOp
let b:ECcommentMiddle =
\ s:ParseCommentsOp(b:ECcommentOpen, b:ECcommentClose)
if b:ECcommentMiddle == ''
let b:ECuseCommentsOp = 0
endif
else
let b:ECcommentMiddle = ''
endif
if !s:overrideEmptyLines
call s:CommentEmptyLines(fileType)
endif
endfunction
"
" ParseCommentsOp(commentOpen, commentClose)
" commentOpen -- comment-open string
" commentClose-- comment-close string
"
" Try to extract the middle comment string from &comments. First hit wins.
" If nothing is found '' is returned.
"
function s:ParseCommentsOp(commentOpen, commentClose)
let commStr = &comments
let offset = 0
let commentMiddle = ''
while commStr != ''
"
" First decompose &omments into consecutive s-, m- and e-parts.
"
let s = stridx(commStr, 's')
if s == -1
return ''
endif
let commStr = strpart(commStr, s)
let comma = stridx(commStr, ',')
if comma == -1
return ''
endif
let sPart = strpart(commStr, 0, comma)
let commStr = strpart(commStr, comma)
let m = stridx(commStr, 'm')
if m == -1
return ''
endif
let commStr = strpart(commStr, m)
let comma = stridx(commStr, ',')
if comma == -1
return ''
endif
let mPart = strpart(commStr, 0, comma)
let commStr = strpart(commStr, comma)
let e = stridx(commStr, 'e')
if e == -1
return ''
endif
let commStr = strpart(commStr, e)
let comma = stridx(commStr, ',')
if comma == -1
let comma = strlen(commStr)
endif
let ePart = strpart(commStr, 0, comma)
let commStr = strpart(commStr, comma)
"
" Now check wether this is what we want:
" Are the comment string the same?
"
let sColon = stridx(sPart, ':')
let eColon = stridx(ePart, ':')
if sColon == -1 || eColon == -1
return ''
endif
if strpart(sPart, sColon + 1) != a:commentOpen
\ || strpart(ePart, eColon + 1) != a:commentClose
continue
endif
let mColon = stridx(mPart, ':')
if mColon == -1
return ''
endif
let commentMiddle = strpart(mPart, mColon + 1)
"
" Check for any alignement.
"
let i = 1
while sPart[i] != ':'
if sPart[i] == 'r'
let offset = strlen(a:commentOpen) - strlen(commentMiddle)
break
elseif sPart[i] == 'l'
let offset = 0
break
elseif s:isDigit(sPart[i])
let j = 1
while s:isDigit(sPart[i + j])
let j = j + 1
endwhile
let offset = 1 * strpart(sPart, i, j)
break
endif
let i = i + 1
endwhile
if offset == 0
let i = 1
while ePart[i] != ':'
if ePart[i] == 'r'
let offset = strlen(a:commentClose) - strlen(commentMiddle)
break
elseif ePart[i] == 'l'
let offset = 0
break
elseif s:isDigit(ePart[i])
let j = 1
while s:isDigit(ePart[i + j])
let j = j + 1
endwhile
let offset = 1 * strpart(ePart, i, j)
break
endif
let i = i + 1
endwhile
endif