This file is indexed.

/usr/share/vim-scripts/plugin/EnhancedCommentify.vim is in vim-scripts 20130814ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
" 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\|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

	while offset > 0
	    let commentMiddle = " " . commentMiddle
	    let offset = offset - 1
	endwhile

	break
    endwhile

    return commentMiddle
endfunction

"
" isDigit(char)
"
" Nomen est Omen.
"
function s:isDigit(char)
    let r = 0

    let charVal = char2nr(a:char)

    if charVal >= 48 && charVal <= 57
	let r = 1
    endif

    return r
endfunction

"
" CommentEmptyLines(ft)
"	ft	    -- filetype of current buffer
"
" Decides, if empty lines should be commentified or not. Add the filetype,
" you want to change, to the apropriate if-clause.
"
function s:CommentEmptyLines(ft)
    " FIXME: Quick hack (tm)!
    if 0
	" Add special filetypes here.
    elseif b:ECcommentClose == ''
	let b:ECemptyLines = 'yes'
    else
	let b:ECemptyLines = 'no'
    endif
endfunction

"
" CheckPossibleEmbedding(ft)
"	ft	-- the filetype of current buffer
"
" Check wether it makes sense to allow checking for the synIDs.
" Eg. C will never have embedded code...
"
function s:CheckPossibleEmbedding(ft)
    if a:ft =~ '^\(php\|vim\|latte\|html\)$'
	let b:ECpossibleEmbedding = 1
    else
	" Since getting the synID is slow, we set the default to 'no'!
	" There are also some 'broken' languages like the filetype for
	" autoconf's configure.in's ('config'). 
	let b:ECpossibleEmbedding = 0
    endif
endfunction

"
" CommentifyMultiPart(lineString, commentStart, commentEnd, action)
"	lineString	-- line to commentify
"	commentStart	-- comment-start string, eg '/*'
"	commentEnd	-- comment-end string, eg. '*/'
"	commentMiddle	-- comment-middle string, eg. ' *'	
"
" This function commentifies code of languages, which have multipart
" comment strings, eg. '/*' - '*/' of C.
"
function s:CommentifyMultiPart(lineString, commentStart,
	    \ commentEnd, commentMiddle)
    if s:Action == 'guess' || s:Action == 'first' || b:ECuseMPBlock
	let todo = s:DecideWhatToDo(a:lineString, a:commentStart, a:commentEnd)
    else
	let todo = s:Action
    endif

    if todo == 'decomment'
	return s:UnCommentify(a:lineString, a:commentStart,
		    \ a:commentEnd, a:commentMiddle)
    else
	return s:Commentify(a:lineString, a:commentStart,
		    \ a:commentEnd, a:commentMiddle)
    endif
endfunction

"
" CommentifySinglePart(lineString, commentSymbol)
"	lineString	-- line to commentify
"	commentSymbol	-- comment string, eg '#'
"
" This function is used for all languages, whose comment strings
" consist only of one string at the beginning of a line.
"
function s:CommentifySinglePart(lineString, commentSymbol)
    if s:Action == 'guess' || s:Action == 'first'
	let todo = s:DecideWhatToDo(a:lineString, a:commentSymbol)
    else
	let todo = s:Action
    endif

    if todo == 'decomment'
	return s:UnCommentify(a:lineString, a:commentSymbol)
    else
	return s:Commentify(a:lineString, a:commentSymbol)
    endif
endfunction

"
" Escape(lineString, commentStart, commentEnd)
"
" Escape already present symbols.
"
function s:Escape(lineString, commentStart, commentEnd)
    let line = a:lineString

    if b:ECaltOpen != ''
	let line = substitute(line, s:EscapeString(a:commentStart),
		    \ b:ECaltOpen, "g")
    endif
    if b:ECaltClose != ''
	let line = substitute(line, s:EscapeString(a:commentEnd),
		    \ b:ECaltClose, "g")
    endif

    return line
endfunction

"
" UnEscape(lineString, commentStart, commentEnd)
"
" Unescape already present escape symbols.
"
function s:UnEscape(lineString, commentStart, commentEnd)
    let line = a:lineString

    " We translate only the first and the last occurrence
    " of this resp. escape string. Commenting a line several
    " times and decommenting it again breaks things.
    if b:ECaltOpen != ''
	let line = substitute(line, s:EscapeString(b:ECaltOpen),
		    \ a:commentStart, "")
    endif
    if b:ECaltClose != ''
	let esAltClose = s:EscapeString(b:ECaltClose)
	let line = substitute(line, esAltClose
		    \ . "\\(.*" . esAltClose . "\\)\\@!",
		    \ a:commentEnd, "")
    endif

    return line
endfunction

"
" Commentify(lineString, commentSymbol, [commentEnd])
"       lineString	-- the line in work
"	commentSymbol	-- string to insert at the beginning of the line
"	commentEnd	-- string to insert at the end of the line
"			   may be omitted
"
" This function inserts the start- (and if given the end-) string of the
" comment in the current line.
"
function s:Commentify(lineString, commentSymbol, ...)
    let line = a:lineString
    let j = 0

    " If a end string is present, insert it too.
    if a:0 > 0
	" First we have to escape any comment already contained in the line,
	" since (at least for C) comments are not allowed to nest.
	let line = s:Escape(line, a:commentSymbol, a:1)

	if b:ECuseCommentsOp && b:ECuseMPBlock
		    \ && a:0 > 1
		    \ && s:i > s:startBlock
	    let line = substitute(line, s:LookFor('commentmiddle'),
			\ s:SubstituteWith('commentmiddle', a:2), "")
	endif

	if !b:ECuseMPBlock || (b:ECuseMPBlock && s:i == s:endBlock)
	    " Align the closing part to the right.
	    if b:ECalignRight && s:inBlock
		let len = s:GetLineLen(line, strlen(a:commentSymbol)
			    \ + strlen(b:ECprettyComments))
		while j < s:maxLen - len
		    let line = line .' '
		    let j = j + 1
		endwhile
	    endif

	    let line = substitute(line, s:LookFor('commentend'),
			\ s:SubstituteWith('commentend', a:1), "")
	endif
    endif

    " insert the comment symbol
    if !b:ECuseMPBlock || a:0 == 0 || (b:ECuseMPBlock && s:i == s:startBlock) 
	let line = substitute(line, s:LookFor('commentstart'),
		    \ s:SubstituteWith('commentstart', a:commentSymbol), "")
    endif

    return line
endfunction

"
" UnCommentify(lineString, commentSymbol, [commentEnd])
"	lineString	-- the line in work
"	commentSymbol	-- string to remove at the beginning of the line
"	commentEnd	-- string to remove at the end of the line
"			   may be omitted
"
" This function removes the start- (and if given the end-) string of the
" comment in the current line.
"
function s:UnCommentify(lineString, commentSymbol, ...)
    let line = a:lineString

    " remove the first comment symbol found on a line
    if a:0 == 0 || !b:ECuseMPBlock || (b:ECuseMPBlock && s:i == s:startBlock) 
	let line = substitute(line, s:LookFor('decommentstart',
		    \	a:commentSymbol),
		    \ s:SubstituteWith('decommentstart'), "")
    endif

    " If a end string is present, we have to remove it, too.
    if a:0 > 0
	" First, we remove the trailing comment symbol.
	if !b:ECuseMPBlock || (b:ECuseMPBlock && s:i == s:endBlock) 
	    let line = substitute(line, s:LookFor('decommentend', a:1),
			\ s:SubstituteWith('decommentend'), "")

	    " Remove any trailing whitespace, if we used alignRight.
	    if b:ECalignRight
		let line = substitute(line, ' *$', '', "")
	    endif
	endif

	" Maybe we added a middle string. Remove it here.
	if b:ECuseCommentsOp && b:ECuseMPBlock
		    \ && a:0 > 1
		    \ && s:i > s:startBlock
	    let line = substitute(line, s:LookFor('decommentmiddle', a:2),
			\ s:SubstituteWith('decommentmiddle'), "")
	endif

	" Remove escaped inner comments.
	let line = s:UnEscape(line, a:commentSymbol, a:1)
    endif

    return line
endfunction

"
" GetLineLen(line, offset)
"	line	    -- line of which length should be computed
"	offset	    -- maybe a shift of the line to the right
"
" Expands '\t' to it's tabstop value.
"
function s:GetLineLen(line, offset)
    let len = a:offset
    let i = 0

    while a:line[i] != ""
	if a:line[i] == "\t"
	    let len = (((len / &tabstop) + 1) * &tabstop)
	else
	    let len = len + 1
	endif
	let i = i + 1
    endwhile

    return len
endfunction

"
" EscapeString(string)
"	string	    -- string to process
"
" Escapes characters in 'string', which have some function in
" regular expressions, with a '\'.
"
" Returns the escaped string.
"
function s:EscapeString(string)
    return escape(a:string, "*{}[]$^-")
endfunction

"
" LookFor(what, ...)
"	what	    -- what type of regular expression
"			* checkstart:
"			* checkend:
"			  check for comment at start/end of line
"			* commentstart:
"			* commentend:
"			  insert comment strings
"			* decommentstart:
"			* decommentend:
"			  remove comment strings
"	a:1	    -- comment string
"
function s:LookFor(what, ...)
    if b:ECuseBlockIndent && s:inBlock
	let handleWhitespace = s:blockIndentRegex
    else
	let handleWhitespace = b:ECsaveWhite
    endif

    if a:what == 'checkstart'
	let regex = '^'. b:ECsaveWhite . s:EscapeString(a:1)
		    \ . s:EscapeString(b:ECidentFront)
    elseif a:what == 'checkend'
	let regex = s:EscapeString(b:ECidentBack)
		    \ . s:EscapeString(a:1) . b:ECsaveWhite . '$'
    elseif a:what == 'commentstart'
	let regex = '^'. handleWhitespace
    elseif a:what == 'commentmiddle'
	let regex = '^'. handleWhitespace
    elseif a:what == 'commentend'
	let regex = '$'
    elseif a:what == 'decommentstart'
	let regex = '^'. b:ECsaveWhite . s:EscapeString(a:1)
    		    \ . s:EscapeString(b:ECidentFront) . b:ECprettyUnComments
    elseif a:what == 'decommentmiddle'
	let regex = '^'. b:ECsaveWhite . s:EscapeString(a:1)
		    \ . s:EscapeString(b:ECidentFront) . b:ECprettyUnComments
    elseif a:what == 'decommentend'
	let regex = b:ECprettyUnComments . s:EscapeString(b:ECidentBack)
		    \ . s:EscapeString(a:1) . b:ECsaveWhite .'$'
    endif

    return regex
endfunction

"
" SubstituteWith(what, ...)
"	what	    -- what type of regular expression
"			* commentstart:
"			* commentend:
"			  insert comment strings
"			* decommentstart:
"			* decommentend:
"			  remove comment strings
"	a:1	    -- comment string
"
function s:SubstituteWith(what, ...)
    if a:what == 'commentstart'
		\ || a:what == 'commentmiddle'
		\ || a:what == 'commentend'
	let commentSymbol = a:1
    else
	let commentSymbol = ''
    endif

    if b:ECuseBlockIndent && s:inBlock
	let handleWhitespace = '\1' . commentSymbol
    else
	let handleWhitespace = b:ECrespectWhite . commentSymbol
		    \ . b:ECignoreWhite
    endif

    if a:what == 'commentstart'
	let regex = handleWhitespace . b:ECidentFront
		    \ . b:ECprettyComments
    elseif a:what == 'commentmiddle'
	let regex = handleWhitespace . b:ECidentFront
		    \ . b:ECprettyComments
    elseif a:what == 'commentend'
	let regex = b:ECprettyComments . b:ECidentBack . a:1
    elseif a:what == 'decommentstart'
		\ || a:what == 'decommentmiddle'
		\ || a:what == 'decommentend'
	let regex = handleWhitespace
    endif

    return regex
endfunction

"
"
" DecideWhatToDo(lineString, commentStart, ...)
"	lineString	-- first line of block
"	commentStart	-- comment start symbol
"	a:1		-- comment end symbol
"
function s:DecideWhatToDo(lineString, commentStart, ...)
    " If we checked already, we return our previous result.
    if !s:firstOfBlock
		\ && (s:Action == 'first'
		\	|| (b:ECuseMPBlock && s:inBlock && a:0))
	return s:blockAction
    endif

    let s:blockAction = 'comment'

    if s:inBlock && a:0 && b:ECuseMPBlock
	let first = getline(s:startBlock)
	let last = getline(s:endBlock)

	if first =~ s:LookFor('checkstart', a:commentStart)
		\ && first !~ s:LookFor('checkend', a:1)
		\ && last !~ s:LookFor('checkstart', a:commentStart)
		\ && last =~ s:LookFor('checkend', a:1)
	    let s:blockAction = 'decomment'
	endif

	return s:blockAction
    endif

    if a:lineString =~ s:LookFor('checkstart', a:commentStart)
	let s:blockAction = 'decomment'
    endif

    if a:0
	if a:lineString !~ s:LookFor('checkend', a:1)
	    let s:blockAction = 'comment'
	endif
    endif

    let s:firstOfBlock = 0
    return s:blockAction
endfunction

"
" TabsToSpaces(str)
"	str	    -- string to convert
"
" Convert leading tabs of given string to spaces.
"
function s:TabsToSpaces(str)
    let string = a:str

    " FIXME: Can we use something like retab? I don't think so,
    " because retab changes every whitespace in the line, but we
    " wan't to modify only the leading spaces. Is this a problem?
    while string =~ '^\( *\)\t'
	let string = substitute(string, '^\( *\)\t', '\1'. s:tabConvert, "")
    endwhile

    return string
endfunction

"
" SpacesToTabs(str)
"	str	    -- string to convert
"
" Convert leading spaces of given string to tabs.
"
function s:SpacesToTabs(str)
    let string = a:str

    if !&expandtab
	while string =~ '^\(\t*\)'. s:tabConvert
	    let string = substitute(string, '^\(\t*\)'. s:tabConvert,
			\ '\1\t', "")
	endwhile
    endif

    return string
endfunction

"
" EnhCommentifyFallback4Embedded(test, fallback)
"	test	    -- test for the special case
"	fallback    -- filetype instead of normal fallback
"
" This function is global. It should be called from filetype
" plugins like php, where the normal fallback behaviour may
" not work. One may use 'synFiletype' to reference the guessed
" filetype via synID.
"
function EnhCommentifyFallback4Embedded(test, fallback)
    let b:EnhCommentifyFallbackTest = a:test
    let b:EnhCommentifyFallbackValue = a:fallback
endfunction

"
" Keyboard mappings.
"
noremap <Plug>Comment
	    \ :call EnhancedCommentify('', 'comment')<CR>
noremap <Plug>DeComment
	    \ :call EnhancedCommentify('', 'decomment')<CR>
noremap <Plug>Traditional
	    \ :call EnhancedCommentify('', 'guess')<CR>
noremap <Plug>FirstLine
	    \ :call EnhancedCommentify('', 'first')<CR>

noremap <Plug>VisualComment
	    \ <Esc>:call EnhancedCommentify('', 'comment',
	    \   line("'<"), line("'>"))<CR>
noremap <Plug>VisualDeComment
	    \ <Esc>:call EnhancedCommentify('', 'decomment',
	    \   line("'<"), line("'>"))<CR>
noremap <Plug>VisualTraditional
	    \ <Esc>:call EnhancedCommentify('', 'guess',
	    \   line("'<"), line("'>"))<CR>
noremap <Plug>VisualFirstLine
	    \ <Esc>:call EnhancedCommentify('', 'first',
	    \   line("'<"), line("'>"))<CR>
"
" Finally set keybindings.
"
" SetKeybindings(where)
"	where	    -- "l" for local to the buffer, "g" for global
"
function s:SetKeybindings(where)
    if a:where == "l"
	let where = "<buffer>"
	let ns = "b"
    else
	let where = ""
	let ns = "s"
    endif

    execute "let userBindings = ". ns .":ECuserBindings"
    execute "let useAltKeys = ". ns .":ECuseAltKeys"
    execute "let traditionalMode = ". ns .":ECtraditionalMode"
    execute "let firstLineMode = ". ns .":ECfirstLineMode"
    execute "let bindInNormal = ". ns .":ECbindInNormal"
    execute "let bindInInsert = ". ns .":ECbindInInsert"
    execute "let bindInVisual = ". ns .":ECbindInVisual"

    if userBindings
	"
	" *** Put your personal bindings here! ***
	"
    else
	if useAltKeys
	    let s:c = '<M-c>'
	    let s:x = '<M-x>'
	    let s:C = '<M-v>'
	    let s:X = '<M-y>'
	else
	    let s:c = '<Leader>c'
	    let s:x = '<Leader>x'
	    let s:C = '<Leader>C'
	    let s:X = '<Leader>X'
	endif

	if traditionalMode
	    let s:Method = 'Traditional'
	elseif firstLineMode
	    let s:Method = 'FirstLine'
	else
	    let s:Method = 'Comment'

	    " Decomment must be defined here. Everything else is mapped below.
	    if bindInNormal 
		execute 'nmap '. where .' <silent> <unique> '. s:C
			    \ .' <Plug>DeCommentj'
		execute 'nmap '. where .' <silent> <unique> '. s:X
			    \ .' <Plug>DeComment'
	    endif

	    if bindInInsert
		execute 'imap '. where .' <silent> <unique> '. s:C
			    \ .' <Esc><Plug>DeCommentji'
		execute 'imap '. where .' <silent> <unique> '. s:X
			    \ .' <Esc><Plug>DeCommenti'
	    endif

	    if bindInVisual
		execute 'vmap '. where .' <silent> <unique> '. s:C
			    \ .' <Plug>VisualDeCommentj'
		execute 'vmap '. where .' <silent> <unique> '. s:X
			    \ .' <Plug>VisualDeComment'
	    endif
	endif

	if bindInNormal
	    execute 'nmap '. where .' <silent> <unique> '. s:c
			\ .' <Plug>'. s:Method .'j'
	    execute 'nmap '. where .' <silent> <unique> '. s:x
			\ .' <Plug>'. s:Method
	endif

	if bindInInsert
	    execute 'imap '. where .' <silent> <unique> '. s:c
			\ .' <Esc><Plug>'. s:Method .'ji'
	    execute 'imap '. where .' <silent> <unique> '. s:x
			\ .' <Esc><Plug>'. s:Method
	endif

	if bindInVisual
	    execute 'vmap <silent> <unique> '. s:c
			\ .' <Plug>Visual'. s:Method .'j'
	    execute 'vmap '. where .' <silent> <unique> '. s:x
			\ .' <Plug>Visual'. s:Method
	endif
    endif
endfunction

if !s:ECbindPerBuffer
    call s:SetKeybindings("g")
endif

let &cpo = s:savedCpo

" vim: set sts=4 sw=4 ts=8 :