This file is indexed.

/usr/lib/X11/xedit/lisp/indent.lsp is in x11-apps 7.7+5+nmu1ubuntu1.

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
;
;; Copyright (c) 2002 by The XFree86 Project, Inc.
;;
;; Permission is hereby granted, free of charge, to any person obtaining a
;; copy of this software and associated documentation files (the "Software"),
;; to deal in the Software without restriction, including without limitation
;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
;; and/or sell copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
;; THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
;; OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;
;; Except as contained in this notice, the name of the XFree86 Project shall
;; not be used in advertising or otherwise to promote the sale, use or other
;; dealings in this Software without prior written authorization from the
;; XFree86 Project.
;;
;; Author: Paulo César Pereira de Andrade
;;
;;
;; $XFree86: xc/programs/xedit/lisp/modules/indent.lsp,v 1.6 2003/01/16 03:50:46 paulo Exp $
;;

(provide "indent")
(require "xedit")
(in-package "XEDIT")

(defconstant indent-spaces '(#\Tab #\Space))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The final indentation function.
;; Parameters:
;;	indent
;;		Number of spaces to insert
;;	offset
;;		Offset to where indentation should be added
;;	no-tabs
;;		If set, tabs aren't inserted
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun indent-text (indent offset &optional no-tabs
		    &aux start line length index current tabs spaces string
			 barrier base result (point (point))
		   )

    ;; Initialize
    (setq
	start	(scan offset :eol :left)
	line	(read-text start (- offset start))
	length	(length line)
	index	(1- length)
	current	0
	base	0
    )

    (and (minusp indent) (setq indent 0))

    ;; Skip any spaces after offset, "paranoia check"
    (while (member (char-after offset) indent-spaces)
	(incf offset)
    )

    ;; Check if there are only spaces before `offset' and the line `start'
    (while (and (>= index 0) (member (char line index) indent-spaces))
	(decf index)
    )

    ;; `index' will be zero if there are only spaces in the `line'
    (setq barrier (+ start (incf index)))

    ;; Calculate `base' unmodifiable indentation, if any
    (dotimes (i index)
	(if (char= (char line i) #\Tab)
	    (incf base (- 8 (rem base 8)))
	    (incf base)
	)
    )

    ;; If any non blank character would need to be deleted
    (and (> base indent) (return-from indent-text nil))

    ;; Calculate `current' indentation
    (setq current base)
    (while (< index length)
	(if (char= (char line index) #\Tab)
	    (incf current (- 8 (rem current 8)))
	    (incf current)
	)
	(incf index)
    )

    ;; Maybe could also "optimize" the indentation even if it is already
    ;; correct, removing spaces "inside" tabs.
    (when (/= indent current)
	(if no-tabs
	    (setq
		length	(- indent base)
		result	(+ barrier length)
		string	(make-string length :initial-element #\Space)
	    )
	    (progn
		(multiple-value-setq (tabs spaces) (floor (- indent base) 8))
		(setq
		    length	(+ tabs spaces)
		    result	(+ barrier length)
		    string	(make-string length :initial-element #\Tab)
		)
		(fill string #\Space :start tabs)
	    )
	)

	(replace-text barrier offset string)
	(and (>= offset point) (>= point barrier) (goto-char result))
    )
)
(compile 'indent-text)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helper function, returns indentation of a given offset
;; If `align' is set, stop once a non blank character is seen, that
;; is, use `offset' only as a line identifier
;; If `resolve' is set, it means that the offset is just a hint, it
;; maybe anywhere in the line
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun offset-indentation (offset &key resolve align
			   &aux
			   char
			   line
			   (start (scan offset :eol :left))
			   (indent 0))
    (if resolve
	(loop
	    (if (characterp (setq char (char-after start)))
		(if (char= char #\Tab)
		    (incf indent (- 8 (rem indent 8)))
		    ;; Not a tab, check if is a space
		    (if (char= char #\Space)
			(incf indent)
			;; Not a tab neither a space
			(return indent)
		    )
		)
		;; EOF found
		(return indent)
	    )
	    ;; Increment offset to check next character
	    (incf start)
	)
	(progn
	    (setq line (read-text start (- offset start)))
	    (dotimes (i (length line) indent)
		(if (char= (setq char (char line i)) #\Tab)
		    (incf indent (- 8 (rem indent 8)))
		    (progn
			(or align (member char indent-spaces)
			    (return indent)
			)
			(incf indent)
		    )
		)
	    )
	)
    )
)
(compile 'offset-indentation)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;  A default/fallback indentation function, just copy indentation
;; of previous line.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun default-indent (syntax syntable)
    (let
	(
	(offset (scan (point) :eol :left))
	start
	left
	right
	)

	syntable	;; XXX hack to not generate warning about unused
			;; variable, should be temporary (until unused
			;; variables can be declared as such)

	(if
	    (or
		;; if indentation is disabled
		(and
		    (hash-table-p (syntax-options syntax))
		    (gethash :disable-indent (syntax-options syntax))
		)
		;; or if not at the start of a new line
		(> (scan offset :eol :right) offset)
	    )
	    (return-from default-indent)
	)

	(setq left offset)
	(loop
	    (setq
		start left
		left (scan start :eol :left :count 2)
		right (scan left :eol :right)
	    )
	    ;; if start of file reached
	    (and (>= left start) (return))
	    (when
		(setq
		    start
		    (position-if-not
			#'(lambda (char) (member char indent-spaces))
			(read-text left (- right left))
		    )
		)

		;; indent the current line
		(indent-text (offset-indentation (+ left start) :align t) offset)
		(return)
	    )
	)
    )
)
(compile 'default-indent)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helper function
;;   Clear line before cursor if it is empty
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun indent-clear-empty-line (&aux left offset right line index)
    (setq
	offset	(scan (point) :eol :left)
	left	(scan offset :eol :left :count 2)
	right	(scan left :eol :right)
    )

    ;; If not at the first line in the file and line is not already empty
    (when (and (/= offset left) (/= left right))
	(setq
	    line	(read-text left (- right left))
	    index	(1- (length line))
	)
	(while (and (>= index 0) (member (char line index) indent-spaces))
	    (decf index)
	)
	;; If line was only spaces
	(and (minusp index) (replace-text left right ""))
    )
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;  Macro to be called whenever an indentation rule decides that
;; the parser is done.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro indent-macro-terminate (&optional result)
    `(return-from ind-terminate-block ,result)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Like indent-terminate, but "rejects" the input for the current line
;; and terminates the loop.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro indent-macro-reject (&optional result)
   `(progn
	(setq ind-state ind-prev-state)
	(return-from ind-terminate-block ,result)
    )
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Like indent-reject, but "rejects" anything before the current token
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro indent-macro-reject-left (&optional result)
   `(progn
	(setq ind-state ind-matches)
	(return-from ind-terminate-block ,result)
    )
)


(defstruct indtoken
    regex			;; a string, character or regex
    token			;; the resulting token, nil or a keyword
    begin			;; begin a new table
    switch			;; switch to another table
    ;; begin and switch fields are used like the ones for the syntax highlight
    ;; syntoken structure.
    label			;; filed at compile time
    code			;; code to execute when it matches
)

(defstruct indtable
    label			;; a keyword, name of the table
    tokens			;; list of indtoken structures
    tables			;; list of indtable structures
    augments			;; augment list
)

(defstruct indaugment
    labels			;; list of keywords labeling tables
)

(defstruct indinit
    variables			;; list of variables and optional initialization
    ;; Format of variables must be suitable to LET*, example of call:
    ;;	(indinit
    ;;	    var1		;; initialized to NIL
    ;;	    (var2 (afun))	;; initialized to the value returned by AFUN
    ;;	)
)

(defstruct indreduce
    token			;; reduced token
    rules			;; list of rules
    label			;; unique label associated with rule, this
				;; field is automatically filled in the
				;; compilation process. this field exists
				;; to allow several indreduce definitions
				;; that result in the same token
    check			;; FORM evaluated, if T apply reduce rule
    code			;; PROGN to be called when a rule matches
)

;; NOTE, unlike "reduce" rules, "resolve" rules cannot be duplicated
(defstruct indresolve
    match			;; the matched token (or a list of tokens)
    code			;; PROGN to apply for this token
)

(defstruct indent
    reduces			;; list of indreduce structures
    tables			;; list of indtable structures
    inits			;; initialization list
    resolves			;; list of indresolve structures
    token-code			;; code to execute when a token matches
    check-code			;; code to execute before applying a reduce rule
    reduce-code			;; code to execute after reduce rule
    resolve-code		;; code to execute when matching a token
)

(defmacro defindent (variable label &rest lists)
   `(if (boundp ',variable)
	,variable
	(progn
	    (proclaim '(special ,variable))
	    (setq ,variable (compile-indent-table ,label ,@lists))
	)
    )
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create an indent token.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro indtoken (pattern token
		    &key icase nospec begin switch code (nosub t))
    (setq pattern (re-comp (eval pattern) :icase icase :nospec nospec :nosub nosub))
    (when (consp (re-exec pattern "" :notbol t :noteol t))
	(error "INDTOKEN: regex ~A matches empty string" pattern)
    )

    ;; result of macro, return token structure
    (make-indtoken
	:regex	pattern
	:token	token
	:begin	begin
	:switch	switch
	:code	code
    )
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create an indentation table. Basically a list of indentation tokens.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun indtable (label &rest definitions)
    ;; check for simple errors
    (unless (keywordp label)
	(error "INDTABLE: ~A is not a keyword" label)
    )
    (dolist (item definitions)
	(unless
	    (or
		(atom item)
		(indtoken-p item)
		(indtable-p item)
		(indaugment-p item)
	    )
	    (error "INDTABLE: invalid indent table argument ~A" item)
	)
    )

    ;; return indent table structure
    (make-indtable
	:label		label
	:tokens		(remove-if-not #'indtoken-p definitions)
	:tables		(remove-if-not #'indtable-p definitions)
	:augments	(remove-if-not #'indaugment-p definitions)
    )
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Add identifier to list of augment tables.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun indaugment (&rest keywords)
    (dolist (keyword keywords)
	(unless (keywordp keyword)
	    (error "INDAUGMENT: bad indent table label ~A" keyword)
	)
    )

    ;; return augment list structure
    (make-indaugment :labels keywords)
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Add variables to initialization list
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro indinit (&rest variables)
    (make-indinit :variables variables)
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create a "reduction rule"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro indreduce (token check rules &rest code &aux nullp consp)
    ;; check for simple errors
    (unless (or (keywordp token) (null token))
	(error "INDREDUCE: ~A is not a keyword" token)
    )
    (dolist (rule rules)
	(or (listp rule) (error "INDREDUCE: invalid indent rule ~A" rule))
	;; XXX This test is not enough, maybe should add some sort of
	;; runtime check to avoid circularity.
	(and (eq token (car rule)) (null (cdr rule))
	    (error "INDREDUCE: ~A reduces to ~A" token)
	)
	(dolist (item rule)
	    (and (or nullp consp) (not (keywordp item))
		(error "INDREDUCE: a keyword must special pattern")
	    )
	    (if (consp item)
		(progn
		    (unless
			(or
			    (and
				(eq (car item) 'not)
				(keywordp (cadr item))
				(null (cddr item))
			    )
			    (and
				(eq (car item) 'or)
				(null (member-if-not #'keywordp (cdr item)))
			    )
			)
			(error "INDREDUCE: syntax error parsing ~A" item)
		    )
		    (setq consp t)
		)
		(progn
		    (setq nullp (null item) consp nil)
		    (unless (or (keywordp item) nullp (eq item t))
			(error "INDREDUCE: ~A is not a keyword" item)
		    )
		)
	    )
	)
;	(and consp
;	    (error "INDREDUCE: pattern must be followed by keyword")
;	)
    )

    ;; result of macro, return indent reduce structure
    (make-indreduce
	:token	token
	:check	check
	:rules	(remove-if #'null rules)
	:code	code
    )
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create a "resolve rule"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro indresolve (match &rest code)
    ;; check for simple errors
    (if (consp match)
	(dolist (token match)
	    (or (keywordp token) (error "INDRESOLVE: ~A is not a keyword" token))
	)
	(or (keywordp match) (error "INDRESOLVE: ~A is not a keyword" match))
    )

    ;; result of macro, return indent resolve structure
    (make-indresolve
	:match	match
	:code	code
    )
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helper function for compile-indent-table. Returns a list of all
;; tables and tokens for a given table, including tokens and tables
;; of children.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun list-indtable-elements (table &aux result sub-result)
    (setq result (cons (indtable-tokens table) (indtable-tables table)))
    (dolist (child (indtable-tables table))
	(setq sub-result (list-indtable-elements child))
	(rplaca result (append (car result) (car sub-result)))
	(rplacd result (append (cdr result) (cdr sub-result)))
    )
    ;; Return pair of all nested tokens and tables
    result
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; First pass adding augumented tokens to a table, done in two passes
;; to respect inheritance order.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun compile-indent-augment-list (table table-list &aux labels augment tokens)

    ;; Create a list of all augment tables.
    (dolist (augment (indtable-augments table))
	(setq labels (append labels (indaugment-labels augment)))
    )

    ;;  Remove duplicates and references to "itself", without warnings?
    (setq
	labels
	(remove (indtable-label table) (remove-duplicates labels :from-end t))
    )

    ;; Check if the specified indent tables exists!
    (dolist (label labels)
	(unless
	    (setq augment (car (member label table-list :key #'indtable-label)))
	    (error "COMPILE-INDENT-AUGMENT-LIST: Cannot augment ~A in ~A"
		label
		(indtable-label table)
	    )
	)

	;; Increase list of tokens.
	(setq tokens (append tokens (indtable-tokens augment)))
    )

    ;;  Store the tokens in the augment list. They will be added
    ;; to the indent table in the second pass.
    (setf (indtable-augments table) tokens)

    ;;  Recurse on every child table.
    (dolist (child (indtable-tables table))
	(compile-indent-augment-list child table-list)
    )
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Last pass adding augmented tokens to a table.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun link-indent-augment-list (table)
    (setf
	(indtable-tokens table)
	(remove-duplicates
	    (nconc (indtable-tokens table) (indtable-augments table))
	    :key	#'indtoken-regex
	    :test	#'equal
	    :from-end	t
	)

	;;  Don't need to keep this list anymore.
	(indtable-augments table)
	()
    )

    (dolist (child (indtable-tables table))
	(link-indent-augment-list child)
    )
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Compile the indent reduction rules
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun compile-indent-reduces (reduces
			       &aux need label check rules reduce
				    check-code reduce-code)
    (dolist (item reduces)
	(setq
	    label	(indreduce-label item)
	    check	(indreduce-check item)
	    rules	(indreduce-rules item)
	    reduce	(indreduce-code  item)
	    need	(and
			    rules
			    (not label)
			    (or
				reduce
				(null check)
				(not (constantp check))
			    )
			)
	)
	(when need
	    (and (null label) (setq label (intern (string (gensym)) 'keyword)))

	    (setf (indreduce-label item) label)

	    (and
		(or (null check)
		    (not (constantp check))
		)
		(setq
		    check	(list (list 'eq '*ind-label* label) check)
		    check-code	(nconc check-code (list check))
		)
	    )

	    (and reduce
		(setq
		    reduce	(cons (list 'eq '*ind-label* label) reduce)
		    reduce-code	(nconc reduce-code (list reduce))
		)
	    )
	)
    )

    ;; XXX Instead of using COND, could/should use CASE
    ;; TODO Implement a smart CASE in the bytecode compiler, if
    ;;	    possible, should generate a hashtable, or a table
    ;;	    of indexes (for example when all elements in the cases
    ;;	    are characters) and then jump directly to the code.
    (if check-code
	(setq check-code (cons 'cond (nconc check-code '((t t)))))
	(setq check-code t)
    )
    (and reduce-code (setq reduce-code (cons 'cond reduce-code)))

    (values check-code reduce-code)
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Compile the indent resolve code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun compile-indent-resolves (resolves &aux match resolve resolve-code)
    (and
	(/=
	    (length resolves)
	    (length (remove-duplicates resolves :key #'indresolve-match))
	)
	;; XXX Could do a more complete job and tell what is wrong...
	(error "COMPILE-INDENT-RESOLVES: duplicated labels")
    )

    (dolist (item resolves)
	(when (setq resolve (indresolve-code item))
	    (setq
		match
		(indresolve-match item)

		resolve
		(cons
		    (if (listp match)
			(list 'member '*ind-token* `',match :test `#'eq)
			(list 'eq '*ind-token* match)
		    )
		    resolve
		)

		resolve-code
		(nconc resolve-code (list resolve))
	    )
	)
    )

    (and resolve-code (cons 'cond resolve-code))
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create an indentation table
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun compile-indent-table (name &rest lists
			     &aux main elements switches begins tables symbols
				  label code token-code check-code reduce-code
				  (inits (remove-if-not #'indinit-p lists))
				  (reduces (remove-if-not #'indreduce-p lists))
				  (resolves (remove-if-not #'indresolve-p lists))
			    )
    (setq
	lists	 (delete-if
		    #'(lambda (object)
			(or
			    (indinit-p object)
			    (indreduce-p object)
			    (indresolve-p object)
			)
		    )
		    lists)
	main	 (apply #'indtable name lists)
	elements (list-indtable-elements main)
	switches (remove-if #'null (car elements) :key #'indtoken-switch)
	begins   (remove-if #'null (car elements) :key #'indtoken-begin)
	tables	 (cons main (cdr elements))
    )

    ;; Check for typos in the keywords, or for not defined indent tables.
    (dolist (item (mapcar #'indtoken-switch switches))
	(unless
	    (or	(and (integerp item) (minusp item))
		(member item tables :key #'indtable-label)
	    )
	    (error "COMPILE-INDENT-TABLE: SWITCH ~A cannot be matched" item)
	)
    )
    (dolist (item (mapcar #'indtoken-begin begins))
	(unless (member item tables :key #'indtable-label)
	    (error "COMPILE-INDENT-TABLE: BEGIN ~A cannot be matched" item)
	)
    )

    ;; Build augment list.
    (compile-indent-augment-list main tables)
    (link-indent-augment-list main)

    ;; Change switch and begin fields to point to the indent table
    (dolist (item switches)
	(if (keywordp (indtoken-switch item))
	    (setf
		(indtoken-switch item)
		(car (member (indtoken-switch item) tables :key #'indtable-label))
	    )
	)
    )
    (dolist (item begins)
	(setf
	    (indtoken-begin item)
	    (car (member (indtoken-begin item) tables :key #'indtable-label))
	)
    )

    ;; Build initialization list
    (dolist (init inits)
	(setq symbols (nconc symbols (indinit-variables init)))
    )

    ;; Build token code
    (dolist (item (car elements))
	(when (setq code (indtoken-code item))
	    (setf
		label
		(intern (string (gensym)) 'keyword)

		(indtoken-label item)
		label

		code
		(list (list 'eq '*ind-label* label) code)

		token-code
		(nconc token-code (list code))
	    )
	)
    )

    (multiple-value-setq
	(check-code reduce-code)
	(compile-indent-reduces reduces)
    )

    (make-indent
	:tables		tables
	:inits		symbols
	:reduces	reduces
	:resolves	resolves
	:token-code	(and token-code (cons 'cond token-code))
	:check-code	check-code
	:reduce-code	reduce-code
	:resolve-code	(compile-indent-resolves resolves)
    )
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Search rule-pattern in match-pattern
;; Returns offset of match, and it's length, if any
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun indent-search-rule (rule-pattern match-pattern
			   &aux start rule rulep matchp test offset length)
    (if (member-if-not #'keywordp rule-pattern)
	;; rule has wildcards
	(progn
	    (setq
		rulep	rule-pattern
		matchp	match-pattern
		start	match-pattern
	    )
	    (loop
		(setq rule (car rulep))
		(cond
		    ;; Special pattern
		    ((consp rule)
			(if (eq (car rule) 'not)
			    (progn
				(setq
				    test	(cadr rule)
				    rulep	(cdr rulep)
				    rule	(car rulep)
				)
				(while
				    (and
					;; something to match
					matchp
					;; NOT match is true
					(not (eq (car matchp) test))
					;; next match is not true
					(not (eq (car matchp) rule))
				    )
				    (setq matchp (cdr matchp))
				)
				(if (eq (car matchp) rule)
				    ;; rule matched
				    (setq
					matchp	(cdr matchp)
					rulep	(cdr rulep)
				    )
				    ;; failed
				    (setq
					rulep	rule-pattern
					matchp	(cdr start)
					start	matchp
				    )
				)
			    )
			    ;; (eq (car rule) 'or)
			    (progn
				(if (member (car matchp) (cdr rule) :test #'eq)
				    (setq rulep (cdr rulep) matchp (cdr matchp))
				    ;; failed
				    (progn
					;; end of match found!
					(and (null matchp) (return))
					;; reset search
					(setq
					    rulep	rule-pattern
					    matchp	(cdr start)
					    start	matchp
					)
				    )
				)
			    )
			)
		    )

		    ;; Skip until end of match-pattern or rule is found
		    ((null rule)
			(setq rulep (cdr rulep))
			;; If matches everything
			(if (null rulep)
			    (progn (setq matchp nil) (return))
			    ;; If next token cannot be matched
			    (unless
				(setq
				    matchp
				    (member (car rulep) matchp :test #'eq)
				)
				(setq rulep rule-pattern)
				(return)
			    )
			)
			(setq rulep (cdr rulep) matchp (cdr matchp))
		    )

		    ;; Matched
		    ((eq rule t)
			;; If there isn't a rule to skip
			(and (null matchp) (return))
			(setq rulep (cdr rulep) matchp (cdr matchp))
		    )

		    ;; Matched
		    ((eq rule (car matchp))
			(setq rulep (cdr rulep) matchp (cdr matchp))
		    )

		    ;; No match
		    (t
			;; end of match found!
			(and (null matchp) (return))
			;; reset search
			(setq
			    rulep	rule-pattern
			    matchp	(cdr start)
			    start	matchp
			)
		    )
		)

		;; if everything matched
		(or rulep (return))
	    )

	    ;; All rules matched
	    (unless rulep
		;; Calculate offset and length of match
		(setq offset 0 length 0)
		(until (eq match-pattern start)
		    (setq
			offset		(1+ offset)
			match-pattern	(cdr match-pattern)
		    )
		)
		(until (eq match-pattern matchp)
		    (setq
			length		(1+ length)
			match-pattern	(cdr match-pattern)
		    )
		)
	    )
	)
	;; no wildcards
	(and (setq offset (search rule-pattern match-pattern :test #'eq))
	     (setq length (length rule-pattern))
	)
    )

    (values offset length)
)
(compile 'indent-search-rule)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Indentation parser
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro indent-macro (ind-definition ind-offset &optional ind-no-tabs)
   `(prog*
	(
	;; Current indentation table
	(ind-table (car (indent-tables ,ind-definition)))

	;; The parser rules
	(ind-reduces (indent-reduces ,ind-definition))

	;; Token list for the table
	(ind-tokens (indtable-tokens ind-table))

	;; Stack of nested tables/states
	ind-stack

	;; indentation to be used
	(*indent* 0)

	;; offset to apply indentation
	*offset*

	;; Number of lines read
	(*ind-lines* 1)

	;; Matched token
	*ind-token*

	;; list of tokens after current match, should not be changed
	*ind-token-list*

	;; label associated with rule
	*ind-label*

	;; offset of match
	*ind-offset*

	;; length of match
	*ind-length*

	;; insert position
	(*ind-point* (point))

	(ind-from (scan ,ind-offset :eol :left))
	(ind-to ,ind-offset)
	(ind-line (read-text ind-from (- ind-to ind-from)))

	;; start of current line
	(*ind-start* ind-from)

	;; State information
	ind-state

	;; For use with (indent-macro-reject)
	ind-prev-state

	;; Matches for the current line
	ind-matches

	;; Matched tokens not yet used
	ind-cache

	;; Pattern being tested
	ind-token

	;; Used when searching for a regex
	ind-match

	;; Table to change
	ind-change

	;; Length of ind-line
	(ind-length (length ind-line))

	;; Don't parse after this offset
	(ind-end ind-length)

	;; Temporary variables used during loops
	ind-left
	ind-right
	ind-tleft
	ind-tright

	;; Set  when start of file is found
	ind-startp

	;; Flag for regex search
	(ind-noteol (< ind-to (scan ind-from :eol :right)))

	;; Initialization variables expanded here
	,@(indent-inits (eval ind-definition))
	)

	;; Initial input already read
	(go :ind-loop)

	;; Just to avoid a warning about unused variable, as this
	;; variable is somewhat redundant as code should already
	;; know before entering indent parser, but useful inside
	;; indent macros.
	*ind-point*

;------------------------------------------------------------------------
; Read a text line
:ind-read
	(setq
	    ind-to	ind-from
	    ind-from	(scan ind-from :eol :left :count 2)
	)
	;; If start of file reached
	(and (= ind-to ind-from) (setq ind-startp t) (go :ind-process))

	(setq
	    *ind-lines*		(1+ *ind-lines*)
	    ind-to		(scan ind-from :eol :right)
	    ind-line		(read-text ind-from (- ind-to ind-from))
	    ind-length		(length ind-line)
	    ind-end		ind-length
	    ind-noteol		nil
	    ind-cache		nil
	    ind-prev-state	ind-state
	)

;------------------------------------------------------------------------
; Loop parsing backwards
:ind-loop
	(setq ind-matches nil)
	(dolist (token ind-tokens)
	    ;; Prepare to loop
	    (setq
		ind-token	(indtoken-regex token)
		ind-left	0
	    )
	    ;; While the pattern matches
	    (loop
		(setq ind-right ind-left)
		(if
		    (consp
			(setq
			    ind-match
			    (re-exec
				ind-token
				ind-line
				:start	ind-left
				:end	ind-end
				:notbol (> ind-left 0)
				:noteol ind-noteol
			    )
			)
		    )

		    ;; Remember about match
		    (setq
			ind-match   (car ind-match)
			ind-left    (cdr ind-match)
			ind-matches (cons (cons token ind-match) ind-matches)
		    )

		    ;; No match
		    (return)
		)
		;; matched an empty string
		(and (= ind-left ind-right) (incf ind-left))

		;; matched a single eol or bol
		(and (>= ind-left ind-end) (return))
	    )
	)

	;; Add new matches to cache
	(when ind-matches
	    (setq
		ind-cache
		(stable-sort
		    (nconc (nreverse ind-matches) ind-cache) #'< :key #'cadr
		)
	    )
	)

	;; If nothing in the cache
	(or ind-cache (go :ind-process))

	(setq
	    ind-left	(cadar ind-cache)
	    ind-right	(cddar ind-cache)
	    ind-matches	(cdr ind-cache)
	)

	;; If only one element in the cache
	(or ind-matches	(go :ind-parse))

	(setq
	    ind-tleft	(cadar ind-matches)
	    ind-tright	(cddar ind-matches)
	)

	;; Remove overlaps
	(loop
	    (if (or (>= ind-tleft ind-right) (<= ind-tright ind-left))
		;; No overlap
		(progn
		    (setq
			ind-left    ind-tleft
			ind-right   ind-tright
			ind-matches (cdr ind-matches)
		    )
		    ;; If everything checked
		    (or ind-matches (return))
		)
		;; Overlap found
		(progn
		    (if (consp (cdr ind-matches))
			;; There are yet items to be checked
			(progn
			    (rplaca ind-matches (cadr ind-matches))
			    (rplacd ind-matches (cddr ind-matches))
			)
			;; Last item
			(progn
			    (rplacd (last ind-cache 2) nil)
			    (return)
			)
		    )
		)
	    )

	    ;; Prepare for next check
	    (setq
		ind-tleft   (cadar ind-matches)
		ind-tright  (cddar ind-matches)
	    )
	)

;------------------------------------------------------------------------
; Process the matched tokens
:ind-parse
	(setq ind-cache (nreverse ind-cache))

:ind-parse-loop
	(or (setq ind-match (car ind-cache)) (go :ind-process))

	(setq
	    ind-cache (cdr ind-cache)
	    ind-token (car ind-match)
	)

	(or (member ind-token ind-tokens :test #'eq)
	    (go :ind-parse-loop)
	)

	;; If a state should be added
	(when (setq ind-change (indtoken-token ind-token))
	    (setq
		ind-left    (cadr ind-match)
		ind-right   (cddr ind-match)

		*ind-offset*
		(+ ind-from ind-left)

		*ind-length*
		(- ind-right ind-left)

		ind-state
		(cons
		    (cons ind-change (cons *ind-offset* *ind-length*))
		    ind-state
		)

		*ind-label*
		(indtoken-label ind-token)
	    )

	    ;; Expand token code
	    ,(indent-token-code (eval ind-definition))
	)

	;; Check if needs to switch to another table
	(when (setq ind-change (indtoken-switch ind-token))
	    ;; Need to switch to a previous table
	    (if (integerp ind-change)
		;; Relative switch
		(while (and ind-stack (minusp ind-change))
		    (setq
			ind-table	(pop ind-stack)
			ind-change	(1+ ind-change)
		    )
		)
		;; Search table in the stack
		(until
		    (or
			(null ind-stack)
			(eq
			    (setq ind-table (pop ind-stack))
			    ind-change
			)
		    )
		)
	    )

	    ;; If no match or stack became empty
	    (and (null ind-table)
		(setq
		    ind-table
		    (car (indent-tables ,ind-definition))
		)
	    )
	)

	;; Check if needs to start a new table
	;; XXX use ind-tleft to reduce number of local variables
	(when (setq ind-tleft (indtoken-begin ind-token))
	    (setq
		ind-change  ind-tleft
		ind-stack   (cons ind-table ind-stack)
		ind-table   ind-change
	    )
	)

	;; If current "indent pattern table" changed
	(when ind-change
	    (setq
		ind-tokens  (indtable-tokens ind-table)
		ind-cache   (nreverse ind-cache)
		ind-end     (cadr ind-match)
		ind-noteol  (> ind-length ind-end)
	    )
	    (go :ind-loop)
	)

	(and ind-cache (go :ind-parse-loop))

;------------------------------------------------------------------------
; Everything checked, process result
:ind-process

	;; If stack is not empty, don't apply rules
	(and ind-stack (not ind-startp) (go :ind-read))

	(block ind-terminate-block
	    (setq ind-cache nil ind-tleft 0 ind-change (mapcar #'car ind-state))
	    (dolist (entry ind-reduces)
		(setq
		    *ind-token* (indreduce-token entry)
		    *ind-label* (indreduce-label entry)
		)
		(dolist (rule (indreduce-rules entry))
		    (loop
			;; Check if reduction can be applied
			(or
			    (multiple-value-setq
				(ind-match ind-length)
				(indent-search-rule rule ind-change)
			    )
			    (return)
			)

			(setq
			    ;; First element matched
			    ind-matches		(nthcdr ind-match ind-state)

			    ;; Offset of match
			    *ind-offset*	(cadar ind-matches)

			    *ind-token-list*	(nthcdr ind-match ind-change)

			    ;; Length of match, note that *ind-length*
			    ;; Will be transformed to zero bellow if
			    ;; the rule is deleting entries.
			    *ind-length*
			    (if (> ind-length 1)
				(progn
				    (setq
					;; XXX using ind-tright, to reduce
					;; number of local variables...
					ind-tright
					(nth (1- ind-length) ind-matches)

					ind-right
					(+  (cadr ind-tright)
					    (cddr ind-tright)
					)
				    )
				    (- ind-right *ind-offset*)
				)
				(cddar ind-matches)
			    )
			)

			;; XXX using ind-tleft as a counter, to reduce
			;; number of used variables...
			(and (>= (incf ind-tleft) 1000)
			    ;; Should never apply so many reduce rules on
			    ;; every iteration, if needs to, something is
			    ;; wrong in the indentation definition...
			    (error "~D INDREDUCE iterations, ~
				   now checking (~A ~A)"
				ind-tleft *ind-token* rule
			    )
			)

			;; Check if should apply the reduction
			(or
			    ;; Expand check code
			    ,(indent-check-code (eval ind-definition))
			    (return)
			)

			(if (null *ind-token*)
			    ;; Remove match
			    (progn
				(setq *ind-length* 0)
				(if (= ind-match 0)
				    ;; Matched the first entry
				    (setq
					ind-state
					(nthcdr ind-length ind-matches)
				    )
				    (progn
					(setq
					    ind-matches
					    (nthcdr (1- ind-match) ind-state)
					)
					(rplacd
					    ind-matches
					    (nthcdr (1+ ind-length) ind-matches)
					)
				    )
				)
			    )

			    ;; Substitute/simplify
			    (progn
				(rplaca (car ind-matches) *ind-token*)
				(when (> ind-length 1)
				    (rplacd (cdar ind-matches) *ind-length*)
				    (rplacd
					ind-matches
					(nthcdr ind-length ind-matches)
				    )
				)
			    )
			)
			(setq
			    ind-cache	    t
			    ind-change	    (mapcar #'car ind-state)
			)

			;; Expand reduce code
			,(indent-reduce-code (eval ind-definition))
		    )
		)
	    )

	    ;; ind-cache will be T if at least one change was done
	    (and ind-cache (go :ind-process))

	    ;; Start of file reached
	    (or ind-startp (go :ind-read))

	)    ;; end of ind-terminate-block


	(block ind-terminate-block
	    (setq *ind-token-list* (mapcar #'car ind-state))
	    (dolist (item ind-state)
		(setq
		    *ind-token*		(car item)
		    *ind-offset*	(cadr item)
		    *ind-length*	(cddr item)
		)
		;; Expand resolve code
		,(indent-resolve-code (eval ind-definition))
		(setq *ind-token-list* (cdr *ind-token-list*))
	    )
	)

	(and (integerp *indent*)
	     (integerp *offset*)
	    (indent-text *indent* *offset* ,ind-no-tabs)
	)
    )
)