File: normal.scm

package info (click to toggle)
sdc 1.0.8beta-8
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 1,400 kB
  • ctags: 874
  • sloc: lisp: 8,120; ansic: 967; makefile: 671; perl: 136; sh: 50
file content (1322 lines) | stat: -rw-r--r-- 32,703 bytes parent folder | download
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
(load "bin/compile.sch")
(load "bin/stream.sch")
(load "bin/rdp.sch")
(load "include/language.scm")

;{{{ debugging aid

(define (tell . stuff)
  (newline (current-error-port))
  (apply fprint (current-error-port) stuff))

(define (watch msg)
  (lambda (s)
    (stream-dbg-watch msg s)))

;}}}

;{{{ Configuration variables

(define normalize-make-index-section #f)
(define normalize-make-bib-section #t)

;}}}

(define (ints n) (cons-stream n (ints (+ n 1))))

;{{{ subfiles

(define (subfiles-of prefix . postfix)
  (stream-map
   (lambda (n) (apply string-append prefix (number->string n) postfix))
   (ints 0)))

(define (subfiles) (subfiles-of (string-append doc-basename "-")))

;}}}
;{{{ section numbering

;;; format is #( (converters ...) gap header trailer)
;;;   converter yield strings
;;; the id is the reverse list of the actual number

(define (section-num format id)
  (define (coerce-to-string x)
    (if (string? x)
	x
	"/???/"))
  (if
   (not (pair? id))
   #f
   (do ((conv (vector-ref format 0)
	      (if (null? (cdr conv)) (vector-ref format 0) (cdr conv)))
	(id id (cdr id))
	(res (list (vector-ref format 3))))
       ((null? id) (apply string-append
			  (reverse!
			   (cons (vector-ref format 2) res))))
     (set! res (if (pair? (cdr id))
		   (cons (vector-ref format 1)
			 (cons (coerce-to-string ((car conv) (car id)))
			       res))
		   (cons (coerce-to-string ((car conv) (car id))) res))))))

(define (ordinary-seq-format num)
  (section-num `#((,number->string) "." "" "") num))

(define (appdx-seq-format num)
  (let ((A-1 (- (char->integer #\A) 1)))
    (section-num
     `#((,(lambda (i) (string (integer->char (+ A-1 i))))
	 ,number->string
	 ,number->string) "." "" "")
     num)))

;}}}
;{{{ attribute service

(define (is-start-tag? t) (start-tag? t))
(define (is-end-tag? t) (end-tag? t))
(define (is-data-token? t) (data-token? t))
(define (is-pi-token? t) (eq? (token-type t) 'PI))
(define (is-external-token? t)
  (case (token-type t)
    ((External-Reference NOTATION External-Definition
      STARTSUBDOC ENDSUBDOC) #t)
    (else #f)))

(define (cut-matching-tag action s)
  (letrec ((lvl-cut
	    (lambda (level s)
	      (if
	       (stream-empty? s)
	       s
	       (let ((t (head s)))
		 (cond
		  ((start-tag? t)
		   (cons-stream t (lvl-cut (+ level 1) (tail s))))
		  ((end-tag? t)
		   (if (eqv? level 0)
		       (action s)
		       (cons-stream t (lvl-cut (- level 1) (tail s)))))
		  (else (cons-stream t (lvl-cut level (tail s))))))))))
    (if (start-tag? (head s))
	(cons-stream (head s) (lvl-cut 0 (tail s)))
	(lvl-cut 0 s))))

(define (add-no-arg no)
  (lambda (token)
    `#(,(token-type token)
       ,(token-gi token)
       (#(NO TOKEN ,no) . ,(token-args token)))))

;}}}
;{{{ General GI replacers

;{{{ renaming

(define (rename-tag new-name)
  (lambda (t)
    (let* ((size (vector-length t))
	   (r (make-vector size)))
      (do ((i 0 (+ i 1)))
	  ((eqv? i size) (vector-set! r 1 new-name) r)
	(vector-set! r i (vector-ref t i))))))

;}}}

;}}}
;{{{ General RDP

;{{{ (rdp-replc function variables parser)

(define (rdp-replc function variables parser)
  (lambda (c h s)
    ((rdp-repll
      (apply function (head s) (map (h 'get) variables))
      parser)
     c h s)))

;}}}
;{{{ rdp-watch, rdp-watch-rest

(define (rdp-watch-rest msg)
  (lambda (c h s)
    (rdp-reduce c h (stream-dbg-watch msg s))))

(define (rdp-watch . msg)
  (lambda (c h s)
    (rdp-reduce c h
		(begin
		  (apply fprint (current-error-port)
			 `(,@msg "depth " ,(length c) " " ,(head s)))
		  s))))

;}}}
;{{{ (rdpl f . fx)

(define (rdpl-accu f . fx)
  (apply rdp-accu-list f fx))

(define (rdpl-stream f . fx)
  (apply rdp-begin f fx))

(define rdpl (car (hook 'rdpl 'run rdpl-stream)))

;}}}

;{{{ macro: (matching-tail token stream)

(define-macro (matching-tail token stream)
  `(if (and (end-tag? (head ,stream))
	    (eq? (token-gi (head ,stream))
		 (token-gi ,token)))
       (tail ,stream)
       ,stream))

;}}}
;{{{ (mkendtag sym)

(define mkendtag
  (memoize (lambda (sym) `#(ENDTAG ,sym))
	   eq?))

;}}}
;{{{ (rdpp-keep content)

(define (rdpp-keep content)
  (lambda (c h s)
     (let ((token (head s)))
       (cons-stream
	token
	(content
	 (rdp-push
	  (lambda (c h s)
	    (cons-stream
	     (mkendtag (token-gi token))
	     (rdp-reduce c h (matching-tail token s))))
	  c)
	 h (tail s))))))

;}}}
;{{{ (rdpp-rename new-gi content)

(define (rdpp-rename new-gi content)
  (let ((e-tag (mkendtag new-gi)))
    (lambda (c h s)
      (let ((token (head s)))
	(cons-stream
	 (vector 'STARTTAG new-gi (token-args token))
	 (content
	  (rdp-push
	   (lambda (c h s)
	     (cons-stream
	      e-tag
	      (rdp-reduce c h (matching-tail token s))))
	   c)
	  h (tail s)))))))

;}}}

(define (rdp-accept-match token)
  (lambda (c h s) (rdp-reduce c h (matching-tail token s))))

(define (rdp-otherwise-continue c) (cons '#() (rdp-continue c)))

;{{{ pass-xxx

(define (pass-token-action c h s) (cons-stream (head s)
					       (rdp-reduce c h (tail s))))

(define (pass-data-token) (list '#(DATA) pass-token-action))

(define (pass-pi-token)
  `(#(PI) ,(rdp-hmap (lambda(t) (plain-tr-string (data-token-data t))))))

(define (pass-external-token) (list is-external-token? pass-token-action))

(define (pass-empty-element tags)
  (list (start-gi? tags)
	(lambda (c h s)
	  (cons-stream (head s)
		       (cons-stream (head (tail s))
				    (rdp-reduce c h (tail (tail s))))))))

(define (pass-otherwise) (list '#() pass-token-action))

;}}}

;}}}

;{{{ Biblio Data Base

;;; (normal-bib)   -> lambda (cmd$symbol)
;;;   cmd: 'insert -> lambda (token)
;;;   cmd: 'ref    -> lambda (key$string)
;;;   cmd: 'get    -> lambda ()
;;;                   -> (#( no$number tag$string entry$token) ...)

;{{{ Code

(define (normal-bib-db)
  (letrec
      ((bib-db (gen-ns "Biblography data base"))
       (bib-list '())
       (insert (lambda (token)
		 (let ((tag (xatv token 'TAG)))
		   (bib-db 'bind token tag)
		   token)))
       (ref (lambda (key)
	      (let ((pos (member key bib-list)))
		(if pos (list (car pos) (length pos) 'B)
		    (begin
		      (set! bib-list (cons key bib-list))
		      (list (car bib-list) (length bib-list) 'B))))))
       (get (lambda ()
	      (do ((res '())
		   (missed bib-list (cdr missed))
		   (no (length bib-list) (- no 1)))
		  ((null? missed) res)
		(let ((val (bib-db 'lookup (car missed))))
		  (if val
		      (set! res (cons (vector no (car missed) (cdr val))
				      res))
		      (set! res (cons (vector no (car missed)
					      "NOT in Database")
				      res))))))))
    (lambda (cmd)
      (case cmd
	((insert) insert)
	((ref) ref)
	((get) get)
	(else (error "normal-bib-db" "unknown cmd" cmd))))))

;}}}

(define p-bibdata
  (let ((do-bibl
	 `((,(lambda (token bibdb) ((bibdb 'insert) token) #f) BIBDB))))
    (rdp-repll
     '()
     (rdp-cond*
      `((#(STARTTAG BIBL) ,(rdp-repll do-bibl rdp-leave))
	(#(ENDTAG BIBL) ,(rdp-skip 1))
	(#(DATA) ,(rdp-skip 1)))))))

;}}}
;{{{ external

(define manpage-subdoc-file "include/nman.scm")

(define normalize-lektion->division
  (rdp-call (rdp-process (require-load "include/lektion.scm"))))

(define normalize-as-division
  (define end-div '#(ENDTAG DIVISION))
  (define (s-div t) `#(STARTTAG DIVISION ,(token-args t)))
  (define (s-top t) `#(STARTTAG DIVISION (#(ID IMPLIED #f) . ,(token-args t))))
  (rdp-repll
   `(,s-top ,end-div)
   (letrec
       ((g (rdp-cond*
	    `((#(ENDTAG))
	      (#(STARTTAG (SECT SECT1 SECT2 CHAPT))
	       ,(rdp-repll `(,s-div ,end-div) (rdp-call g)))
	      (#(STARTTAG ABSTRACT)
	       ,(rdp-repll '(#(STARTTAG QUOTE (#(STYLE TOKEN ("DEFAULT"))))
			     #(ENDTAG QUOTE))
			   (rdp-call g)))
	      (#(STARTTAG APPENDIX) ,(rdp-repll '(#f #f) (rdp-call g)))
	      (#(STARTTAG) ,(rdpp-keep (rdp-call g)))
	      ,(pass-otherwise)))))
     g)))

(define (process-external-token content next)
  `((#((NOTATION External-Definition))
     ,(rdp-set-fetch `((EXTERNALS ,(lambda (t e) (e t))))
		      (rdp-skip 1))
     , next)
    (#(External-Reference)
     ,(rdp-repll `((,(lambda (t e) (e t))
		    EXTERNALS))
		 (rdp-pretend 'dummy))
     , next)
    (#(STARTSUBDOC)
     ,(rdp-sgram
       (lambda (s)
	 (cond
	  ((token-in? (head (tail s))
		      '#(STARTTAG (MANPAGE DOCUMENT REPORT)))
	   rdp-leave)
	  ((token-in? (head (tail s)) '#(STARTTAG BIBDATA))
	   (rdp-repll '() p-bibdata))
	  ((token-in? (head (tail s)) '#(ENDSUBDOC))
	   (message 1 "Warning: emtpy subdoc " (token-gi (head s)))
	   (rdp-skip 2))
	  (else
	   (message 0 #"\ntook wrong turn on subdoc " (token-gi (head s))
		    " can't process token " (head (tail s)))
	   (rdp-skip 1))))))
    ))


(define do-external-token
  (lambda (content next)
    (list (pass-external-token))))

(set! do-external-token process-external-token)

;}}}

(define p-data* (rdp-cond* `(,(pass-data-token)
			     ,@(do-external-token (rdp-call p-data*)
						  rdp-leave))))

;{{{ inline

(hook 'external-messages
      'add
      (lambda s
	(if (eq? (vector-ref (car s) 0) 'ERROR)
	    (apply message 0 (vector-ref (car s) 2)))
	s))
	
(define do-inline
  (rdp-sgram
   (lambda (s)
     (letrec ((file #f)
	      (ext-handler #f)
	      (getit (lambda (x)
		       (set! file (head x))
		       x))
	      (get-eh (lambda (x) (set! ext-handler x))))
       (rdp-let
	`((subfiles ,getit ,tail)
	  (EXTERNALS ,get-eh ,identity))
	(rdp-map1
	 (lambda (s)
	   (let* ((sym (string->symbol (string-append "<inline>" file)))
		  (notation (xatv (head s) 'N)))
	     (call-with-output-file
		 file
	       (lambda (port)
		 (stream-for-each
		  (lambda (i)
		    (display (plain-tr-string (data-token-data i)) port))
		  (tail s))))
	     (stream ((ext-handler `#(External-Definition
				      ,sym
				      #(NDATA ,notation ,file ,file)))
		      `#(External-Reference ,sym)))))
	 (rdp-repll `(,identity) p-data*)))))))

(define pl-inline `((#(STARTTAG INLINE) ,do-inline)))

;}}}
;{{{ p-any-element

(define p-any-element
  (let ((parse-any (rdp-call p-any-element)))
    (letrec
      ((cases
	(rdp-cond*
	 `((#(ENDTAG) ,rdp-leave)
	   (#(STARTTAG) ,parse-any)
	   ,@(do-external-token parse-any rdp-leave)
	   ,(pass-otherwise)))))
    (rdpp-keep cases))))

;}}}
;{{{ p-skip-element

(define p-skip-element
  (let ((parse-any (rdp-call p-skip-element)))
    (letrec
      ((cases
	(rdp-cond*
	 `((#(ENDTAG) ,rdp-leave)
	   (#(STARTTAG) ,parse-any)
	   ; ,@(do-external-token parse-any rdp-leave)
	   (#() ,(rdp-skip 1))))))
    (rdpp-keep cases))))

;}}}

;{{{ ENTITIES

;{{{ p-math

(define p-math
  (letrec ((parse (rdp-call doit))
	   (doit (rdp-cond* `((#(STARTTAG (SUB SUP SET))
			       ,(rdpp-keep parse))
			      (#(ENDTAG) ,rdp-leave)
			      ,@(do-external-token parse rdp-leave)
			      ,(pass-otherwise)))))
    (rdpp-keep parse)))

;}}}
;{{{ Refs: pl-refs

(define (pl-refs)
  (define (id-lookup ids id)
    (let ((x (ids 'lookup id)))
      (if x
	  x
	  (begin
	    (message 0 "normalize:ref lookup failed for " id)
	    `(id "???")))))
  (define (do-ref token)
    (let ((tt (case (string->symbol (car (xatv token 't)))
		((X) 'XREF) ((B) 'BREF)
		((M) 'MREF) ((U) 'UREF))))
      `((,(lambda (token ids bibdb)
	    (let* ((id (xatv token 'id))
		   (m (case tt
			((XREF) (ids 'lookup id))
			((MREF UREF) id)
			((BREF) ((bibdb 'ref) id))
			(else #f))))
	      `#(STARTTAG
		 ,tt
		 (#(MARK ,@(if m
			       `(TOKEN ,m)
			       `(PROMISE ,(case tt
					    ((XREF) (delay (id-lookup ids id)))
					    ((BREF) (delay ((bibdb 'ref) id)))
					    (else (delay #f)))))
			 )
		. ,(token-args token)))))
	 IDS BIBDB)
	,(lambda (token) `#(ENDTAG ,tt)))))

  `((#(STARTTAG LABEL)
     ,(rdp-repll `((,(lambda (t id ids)
		       (ids 'bind id (xatv t 'ID))
		       `#(STARTTAG
			   LABEL
			   (#(DIVISION TOKEN ,id)
			    . ,(token-args t))))
		    DIVISION-ID IDS)
		   #(ENDTAG LABEL))
		 (rdp-call p-text)))
    (#(STARTTAG FOOTNOTE) ,(rdpp-keep (rdp-call p-body*)))
    (#(STARTTAG REF) ,(rdp-repll do-ref (rdp-call p-text)))))

;}}}
;{{{ (pl-index . next)

(define pl-index
  (define do-index
    `(,(lambda (token no i)
	 (let ((id (xatv token 'ID))
	       (sub (xat token 'SUB)))
	   (apply (i 'add)
		  (head no)
		  id
		  (if (eq? (arg-type sub) 'IMPLIED)
		      '() (list (arg-val sub)))))
	 `#(STARTTAG INDEX (#(MARK TOKEN ,(head no))
			    . ,(token-args token))))
      INDEX-MARK INDEX))
  (lambda next
    `((#(STARTTAG INDEX) ,(rdp-let `((INDEX-MARK ,identity ,tail))
				   (rdp-repll (list do-index identity)
					      rdp-leave))
			 . ,next))))

;}}}

(define in-text '(STRONG BF IT TT META CODE VAR LANG))

;{{{ p-text

(define (do-foreign token)
  `#(External-Reference
     ,(string->symbol (car (xatv token 'FILE)))))

(define do-ems
  (let ((level 0))
    `(,(lambda (token)
	 (set! level (+ level 1))
	 `#(STARTTAG EM (#(LEVEL TOKEN ,level))))
      ,(lambda (token)
	 (set! level (- level 1))
	 token))))

(define p-text-do-sq
  `((,(lambda (t lang)
	`#(STARTTAG
	   ,(token-gi t)
	   (#(LANG TOKEN ,lang) . ,(token-args t))))
     LANGUAGE)
    , identity))

(define p-text-and-any-element #f)		; to be set from p-text

(define p-text
  (letrec
      ((action
	`(,(pass-data-token)
	  (#(STARTTAG EM) ,(rdp-repll do-ems (rdp-call p-text)))
	  (#(STARTTAG SQ) ,(rdp-repll p-text-do-sq (rdp-call p-text)))
	  (#(STARTTAG ,in-text) ,(rdpp-keep (rdp-call p-text)))
	  ,@(pl-refs)
	  ,@(pl-index)
	  (#(STARTTAG MATH) ,p-math)
	  ,(pass-empty-element '(NL))
	  (#(STARTTAG FOREIGN) ,(rdp-repll (list do-foreign) rdp-leave))
	  ,@(do-external-token (rdp-call p-text) rdp-leave)
	  ,(pass-pi-token)
	  ,@pl-inline
	  ))
       (any (rdp-call p-text-and-any-element)))
    (set! p-text-and-any-element
	  ; assemble smth simmilar to p-any-element
	  (rdpp-keep
	   (rdp-cond* `(,@(do-external-token any rdp-leave)
			,@action
			(#(STARTTAG) ,any)
			(#(ENDTAG))
			,(pass-otherwise)))))
    (rdp-cond* action)))

;}}}

;{{{ p-p

(define (p-try-p-s c h s)
  (let ((st '#(STARTTAG PAR ()))
	(et '#(ENDTAG PAR)))
    (if (stream-empty? s)
	(rdp-reduce c h s)
	(let* ((parsed (p-text
			(rdp-push (lambda (c h s)
				    (cons-stream et (rdp-reduce c h s))) c)
			h
			s)))
	  (cond
	   ((stream-empty? parsed) (rdp-reduce c h s))
	   (else
	    (if (eq? (head parsed) et)
		(rdp-reduce c h s)
		(cons-stream st parsed))))))))

(define (p-try-p-l c h s)
  (let ((st '#(STARTTAG XYL)))
    (if (stream-empty? s)
	(rdp-reduce c h s)
	(let* ((parsed ((rdpl p-text) c h s)))
	  (cond
	   ((stream-empty? parsed) (rdp-reduce c h s))
	   (else
	    (if (null? (head parsed))
		(rdp-reduce c h s)
		(cons-stream (cons st (head parsed)) (tail parsed)))))))))

(define p-try-p p-try-p-s)

(define (p-p-skip-empty-lines s)
  (stream-finde
   (lambda (t)
     (not (and (data-token? t)
	       (equal? (data-token-data t) "\\n"))))
   identity
   s))

(define (p-p c h s)
  (p-body c h (p-p-skip-empty-lines (tail (tail s)))))

;}}}

;{{{ gen-p-list

(define (gen-p-list list-type item-type level-counter)
  (letrec
      ((repls (lambda (n)
		`((,(lambda (t lvl)
		      `#(STARTTAG ,item-type
			 (#(NO TOKEN ,n)
			  #(LEVEL TOKEN ,(head lvl))
			  . ,(token-args t))))
		    ,level-counter)
		  #(ENDTAG ,item-type))))
       (item (lambda (n) (rdp-repll (repls n) (rdp-call p-body*))))
       (items (lambda (n)
		(rdp-iter*
		 (rdp-cond `((#(STARTTAG (ITEM O)) ,(item n))
			     (#(STARTTAG NEWPAGE)
			      ,(rdpp-keep rdp-leave)
			      ,(rdp-call (items n)))
			     ))
		 (delay (items (+ n 1)))))))
    (rdp-let
     `((,level-counter ,tail ,identity))
     (rdpp-rename list-type (items 1)))))

;}}}
;{{{ p-desc

(define p-desc
  (let ((p-body* (rdp-call p-body*)))
    (letrec
	((sequence
	   (rdp-iter*
	    (rdp-cond
	     `((#(STARTTAG DT)
		,(rdpp-keep p-text)
		; The following is a little HACKy: throw away data items
		; and keep going. Normaly the second #(DATA) line should
		; be valid. But then the DD-handling must handle DD tags
		; appearing too late.
		,(rdp-cond*
		  `((#(DATA) ,(rdp-skip 1))
		    (#(DATA) ,(rdp-wrap '#(STARTTAG DD ())
					'#(ENDTAG DD) p-body*))
		    (#(STARTTAG DD) ,(rdpp-keep p-body*))))
		)))
	    (delay sequence))))
      (rdpp-keep sequence))))

;}}}
;{{{ gen-p-faq

(define gen-p-faq
  (lambda (c h s)
    (let ((the-way (((require-load "include/faq.scm")
		     (car (hook 'faq-normalize-style 'run 'desc)))
		    ((h 'get) 'LANGUAGE) (rdp-call p-body*))))
      (the-way c h s))))

;}}}
;{{{ p-figure

(define p-figure
  (let* ((rep (lambda (t no)
		`#(STARTTAG ,(token-gi t)
			    (#(NO TOKEN ,no)
			     . ,(token-args t)))))
	 (repp `(,rep FIGURE-COUNTER)))
    (letrec
	((cc
	  (rdp-call
	   (rdp-cond*
	    `((#(STARTTAG (TABLE GRAPHIC))
	       ,p-text-and-any-element)
	      ,@pl-inline
	      (#(STARTTAG FOREIGN)
	       ,(rdp-process (rdp-repll `(,do-foreign) rdp-leave)))
	      . ,(do-external-token cc cc))))))
      (rdp-let
       `((FIGURE-COUNTER ,identity ,tail))
       (rdp-repll
	`((,(lambda (t o-no ids)
	      (let ((no (head o-no)))
		(ids 'bind no (xatv t 'ID))
		(rep t no)))
	   FIGURE-COUNTER IDS)
	  #(ENDTAG FIGURE))
	cc
	(rdp-cond
	 `((#(STARTTAG CAPTION) ,(rdp-repll (list repp identity) p-text))))
	)))))

;}}}
;{{{ p-slide

(define p-slide
  (rdp-sgram
   (lambda (heading)
     (set! heading #f)			; abuse to avoid additional let
     (rdp-repll
      `(#f ,identity)
      (rdp-cond
       `((#(STARTTAG HEADING)
	  ,(rdp-map1 (lambda (hd)
		       (set! heading hd)
		       empty-stream)
		     (rdp-repll '() p-text)))))
      (rdp-call (rdp-insert
		 `#(STARTTAG SLIDE (,(if heading
					 `#(HEADING TOKEN ,heading)
					 '#(HEADING IMPLIED #f))))))
      (rdp-call p-body*)))))

;}}}

;{{{ p-body*

(define p-body
  (let ((p-body* (rdp-call p-body*)))
    (rdp-cond
     `((#(STARTTAG P) ,p-p)
       (#(STARTTAG (QUOTE NOTE LITERATE))
	,(rdpp-keep p-body*))
       (#(STARTTAG (TABLE)) ,p-text-and-any-element)
       (#(STARTTAG LIST)  ,(gen-p-list 'LIST 'LISTITEM 'LIST-LEVEL))
       (#(STARTTAG ENUM)  ,(gen-p-list 'ENUM 'ENUMITEM 'ENUM-LEVEL))
       (#(STARTTAG DESC)  ,p-desc)
       (#(STARTTAG (VERB RVERB)) ,(rdpp-rename 'VERB p-data*))
       (#(STARTTAG FAQ) ,gen-p-faq)
       (#(STARTTAG SLIDE) ,p-slide)
       (#(STARTTAG FIGURE) ,p-figure)
       ,@(pl-index (rdp-call p-body))
       (,@(pass-empty-element '(NL NEWPAGE)) ,(rdp-call p-body))
       ,@(do-external-token p-body* (rdp-call p-body))
       (#(DATA "\\n") ,(lambda (c h s) (p-body c h (tail s))))
       ; Pass the generated tokens to indicate where index and bib goes.
       (#(PLACE) ,pass-token-action)
       (#() ,p-try-p)
       ))))

; OK, now dive deep.

;{{{ p-body*-split

(define (p-body*-split c h s)
  (define split-st '#(STARTTAG SPLIT ()))
  (define split-et '#(ENDTAG SPLIT))
  (p-body   			; one body
   (rdp-push
    (lambda (c h ns)
      (if (eq? s ns)		; nothing read
	  (rdp-reduce c h ns)		; leave
	  (let* ((try (p-body*-split c h ns)) ; what comes next?
		 (t (if (stream-empty? try) #f (head try))))
	    (if (and (start-tag? t)           ; a start tag
		     ; but no recursive call
		     (not (eq? t split-st)))
		(cons-stream		; put the split in front
		 split-st
		 (cons-stream
		  split-et
		  try))
		try))))			; leave as is
    c)
    h s))

;}}}

(define p-body*-simple (rdp-loop p-body))

;{{{ p-body*-frame

(hook 'p-body*-frame-body* 'set-doc!
      "Defines which style (%Body)* within an virtual
<!element body o o (%Body)* >
is used.")

(define p-body*-frame-body* (car (hook 'p-body*-frame-body* 'run
				       p-body*-split)))

(define p-body*-frame-st '#(STARTTAG BODY ()))
(define p-body*-frame-et '#(ENDTAG BODY))

(define (p-body*-frame c h s)
  (let ((try (p-body*-frame-body*
	      (rdp-push
	       (lambda (c h s)
		 (cons-stream p-body*-frame-et (rdp-reduce c h s)))
	       c)
	      h s)))
    (if (eq? (head try) p-body*-frame-et)
	(tail try)
	(cons-stream
	 p-body*-frame-st
	 try))))

;}}}

(hook 'p-body* 'set-doc!
      "Defines which virtual contents definition to use for entity %Body")
      
(define p-body* (car (hook 'p-body* 'run
			   p-body*-frame)))

;}}}

;}}}

;{{{ pretend special sections

(define (pretend-special-sections c h s)
  (let* ((get (h 'get))
	 (say (get 'PHRASE))
	 (bib (((get 'BIBDB) 'get)))
	 (idx (get 'INDEX))
	 (lang `#(LANG TOKEN ( ,(get 'LANGUAGE) )))
	 (mkst (lambda (gi)
		 `#(STARTTAG ,gi (,lang #(ID IMPLIED #f)))))
	 (mk-bib (and normalize-make-bib-section
		      (pair? bib)))
	 (mk-idx (and normalize-make-index-section
		      (not pretend-special-sections-nidx)
		      (pair? (idx 'value)))))
    (apply
     stream-insert
     (rdp-reduce
      c
      h ;((h 'set) `(BIBDB ,(normal-bib-db)) `(INDEX ,(make-index)))
      (apply
       stream-insert s
       `(,@(if mk-bib
	       `(,(mkst 'DIVISION)
		 #(STARTTAG HEADING ())
		 #(DATA ,(say 'bibl))
		 #(ENDTAG HEADING)
		 #(PLACE BIB ,bib)
		 #(ENDTAG DIVISION))
	       `())
	 ,@(if mk-idx
	       `(,(mkst 'DIVISION)
		 #(STARTTAG HEADING ())
		 #(DATA ,(say 'index))
		 #(ENDTAG HEADING)
		 #(PLACE INDEX ,((idx 'sort) string<?))
		 #(ENDTAG DIVISION))
	       `())
	 )))
     `(,@(if (not mk-bib)
	     `(#(BIBDB ,bib))
	     '())
       ,@(if (not mk-idx)
	     `(#(INDEXDB ,((idx 'sort) string<?)))
	     '())))))

(define normalize-pretend-special-sections-done #f)

; HACK HACK:
; keep the nidx-value from the outermost face attribute

(define pretend-special-sections-nidx #f)

(define (gen-p-pretend-special-sections contents)
  `((#(ENDTAG (APPENDIX DOCUMENT REPORT BOOK))
     ,(rdp-sgram
       (lambda (s)
	 (if normalize-pretend-special-sections-done
	    rdp-leave
	    (begin
	      (set! normalize-pretend-special-sections-done #t)
	      (rdp-begin
	       pretend-special-sections
	       contents))))))))

;}}}
;{{{ (gen-p-head hd div-no)

(define (gen-p-head hd div-no)
  (let* ((s-tag (lambda (token)
		  `#(STARTTAG ,hd (#(NO TOKEN ,div-no)
				   . ,(token-args token)))))
	 (e-tag `#(ENDTAG ,hd))
	 (repl (list s-tag e-tag)))
    (rdpl
     (rdp-cond
      `((#(STARTTAG HEADING) ,(rdp-repll repl p-text)) ; rm the tokens
	(#(DATA) ,(rdp-wrap s-tag e-tag p-text)))))))

;}}}
;{{{ (gen-p-div div hd inner-div)

(define (gen-p-div-bind-sd no)
  (lambda (token ids)
    (let ((name (symbol->string (token-gi token))))
      (ids 'bind no name)
      (gen-ns name))))

(define (gen-p-div-mk-div s)
  (rdp-cond
   `((#(STARTTAG (DOCUMENT REPORT))
      ,(rdp-process normalize-as-division))
     (#(STARTTAG MANPAGE)
      ,(rdp-sgram
	(lambda (sd)
	  (require-load manpage-subdoc-file)
	  (rdp-process (p-manpage->xxx 'DIVISION '#(ID IMPLIED #f))))))
     (#(STARTTAG LEKTION) ,normalize-lektion->division)
     )))

(define (gen-p-div div hd inner-div)
  (let* ((e-div `#(ENDTAG ,div))
	 (terminating (rdp-cond
		       `((#(ENDTAG)) ; should be more exact
				     ; but this would need another parameter
			 (#() ,(rdp-pretend e-div)))))
	 (contents
	  (lambda (div-no)
	    (rdp-let
	     `((DIVISION-ID ,(lambda (t) div-no) ,identity))
	     (rdp-repll			; uncond rm the terminating token
	      `((,(lambda (t ids)
		    (if (not (eq? (xatt t 'ID) 'IMPLIED))
			(ids 'bind div-no (xatv t 'ID)))
		    `#(STARTTAG ,div
				(#(NO TOKEN ,div-no)
				 . ,(token-args t))))
		 IDS)
		,e-div)
	      (gen-p-head hd div-no)
	      (rdpl p-body*)
	      (rdpl (rdp-call (inner-div div-no)))
	      terminating)))))
    (lambda (div-no)
      (rdp-cond
       `((#(STARTTAG) ,(contents div-no))
	 (#(STARTSUBDOC)
	  ,(rdp-let-fetch
	    `((IDS ,(gen-p-div-bind-sd div-no) ,(lambda (t i) i)))
	    (rdp-repll '() ;`(,identity ,identity)
		       (rdp-sgram gen-p-div-mk-div)
		       (contents div-no)))))))))

;}}}
;{{{ (gen-p-divseq tags seq div)

(define gen-p-handle-externals-and-unused-data-lines
  (rdp-cond* `(,@(do-external-token rdp-leave rdp-leave)
	       (#(STARTTAG P) ,(rdp-skip 2))
	       (#(DATA) ,(rdp-skip 1)))))

(define (gen-p-handle-incldiv-g contents)
  `(#((STARTTAG ENDTAG) InclDiv) ,(rdp-skip 1) ,contents))

(define (gen-p-divseq tags seq div)
  (let* ((s-seq `#(STARTTAG ,seq ()))
	 (e-seq `#(ENDTAG ,seq))
	 (open `#(STARTTAG ,tags)))
    (lambda (div-no)
      (let
	  ((what-to-do
	    (rdp-wrap
	     s-seq e-seq
	     (rdp-cond (list (gen-p-handle-incldiv-g rdp-leave)))
	     (rdpl (div (cons 1 div-no)))
	     gen-p-handle-externals-and-unused-data-lines
	     (let loop ((no 2))
	       (rdp-cond `((,open
			    ,(rdpl (div (cons no div-no)))
			    ,gen-p-handle-externals-and-unused-data-lines
			    ,(rdp-call (loop (+ no 1))))
			   (#(STARTSUBDOC)
			    ,(rdpl (div (cons no div-no)))
			    ,gen-p-handle-externals-and-unused-data-lines
			    ,(rdp-call (loop (+ no 1))))
			   ,@(gen-p-pretend-special-sections
			      (rdp-call (loop no)))
			   ,(gen-p-handle-incldiv-g (rdp-call (loop no)))
			   )))
	     )))
      (rdp-cond*			; * because of mixed content
					; see below
       `((,open ,what-to-do)
	 (#(STARTSUBDOC ) ,what-to-do)
	 ; thow away what comes from the mixed content
	 (#(STARTTAG InclDiv) ,what-to-do)
	 (#(DATA) ,(rdp-skip 1))
	 (#(STARTTAG P) ,(rdp-skip 2))
	 ))))))

;}}}

;{{{ divisions

(define p-sectn (gen-p-div 'SECTN 'H4
			   (lambda (div-no) (p-sectns div-no))))

(define p-sectns (gen-p-divseq `(DIVISION )
			       'SECTNS p-sectn))

(define p-sect2 (gen-p-div 'SECT2 'H3 p-sectns))

(define p-sect2s (gen-p-divseq `(SECT2 DIVISION )
			       'SECT2S p-sect2))

(define p-sect1 (gen-p-div 'SECT1 'H2 p-sect2s))

(define p-sect1s (gen-p-divseq `(SECT1 DIVISION )
			       'SECT1S p-sect1))

(define p-sect (gen-p-div 'SECT 'H1 p-sect1s))

(define p-sects (gen-p-divseq `(SECT DIVISION )
			      'SECTS p-sect))

(define (p-fsect div-no)
  (let ((base (reverse! `(F . ,(cdr (reverse div-no))))))
    (rdp-let `((FIGURE-COUNTER
		,(lambda (old)
		   (stream-map (lambda (h) `(,h . ,base)) (ints 1)))
		,identity))
	     (p-sect div-no))))

(define p-fsects (gen-p-divseq `(SECT DIVISION )
			       'SECTS p-fsect))

;(define p-chapt (gen-p-div 'CHAPT 'H0 p-sects))

;(define p-chapts (gen-p-divseq `(CHAPT DIVISION )
;			       'CHAPTS p-chapt))

(define p0-chapt (gen-p-div 'CHAPT 'H0 p-sects))

(define (p-chapt div-no)
  (let ((base (reverse! `(F . ,(cdr (reverse div-no))))))
    (rdp-let `((FIGURE-COUNTER
		,(lambda (old)
		   (stream-map (lambda (h) `(,h . ,base)) (ints 1)))
		,identity))
	     (p0-chapt div-no))))

(define p-chapts (gen-p-divseq `(CHAPT DIVISION )
			       'CHAPTS p-chapt))

(define p-part (gen-p-div 'PART 'HP p-chapts))

(define p-parts (gen-p-divseq `(PART DIVISION )
			       'PARTS p-part))
; appendix

(define p-appdxn (gen-p-div 'APPDXN 'AH4
			    (lambda (div-no) (p-appdxns div-no))))

(define p-appdxns (gen-p-divseq `(DIVISION )
				'APPDXNS p-appdxn))

(define p-appdx2 (gen-p-div 'APPDX2 'AH3 p-appdxns))

(define p-appdx2s (gen-p-divseq `(SECT2 DIVISION )
				'APPDX2S p-appdx2))

(define p-appdx1 (gen-p-div 'APPDX1 'AH2 p-appdx2s))

(define p-appdx1s (gen-p-divseq `(SECT1 DIVISION )
				'APPDX1S p-appdx1))

(define p-appdx (gen-p-div 'APPDX 'AH1 p-appdx1s))

(define p-appdxs (gen-p-divseq `(SECT DIVISION )
			       'APPDXS p-appdx))

(define p-appendix
  (rdpl
   (rdp-cond
    `((#(STARTTAG APPENDIX) ,(rdp-repll '() (p-appdxs '(A))))))))

;}}}
;{{{

(define normalize-default-face '("2S" "1C" "NIDX"))

(define (filter-face-attribute token)
  `#(STARTTAG
     ,(token-gi token)
     (#(FACE TOKEN ,(car (hook
			  'face 'run
			  (let ((f (xat token 'FACE)))
			    (if f (arg-val f) normalize-default-face)))))
      . ,(token-args token))))
			  
;}}}
;{{{ p-document

(define p-document
  (rdpl
   (rdp-repll
    `(,filter-face-attribute ,identity)
    (rdp-begin
     (rdp-cond
      `((#(STARTTAG HEADING) ,(gen-p-head 'DTTL 'NONE)
			     ,(rdpl (rdp-call p-body*))
			     ,(rdpl (p-sects '(S)))
			     ,p-appendix))
     )))))

;}}}
;{{{ p-manpage

(define p-manpage
  (rdp-sgram
   (lambda (s)
     (require-load manpage-subdoc-file)
     ; never ever put these in documents which are manpages at top level!
     (set! normalize-make-index-section #f)
     (set! normalize-make-bib-section #f)

     (if (equal? doc-output "man")
	 (rdp-begin
	  (rdp-process (p-manpage->xxx 'MANPAGE #f))
	  (rdp-repll
	   `(,filter-face-attribute ,identity)
	   (rdp-cond `((#(STARTTAG TITLE) ,(rdpp-keep p-text))))
	   (rdp-cond `((#(STARTTAG SHORT) ,(rdpp-keep p-text))))
	   (p-sects '(S))
	   ))
	 (rdp-begin
	  (rdp-process (p-manpage->xxx 'DOCUMENT #f))
	  p-document)))))

;}}}
;{{{ p-report

(define p-abstract
  (rdpl
   (rdp-cond*
    `((#(STARTTAG ABSTRACT) ,(rdpp-keep (rdp-call p-body*)))))))

(define normalize-report-intro
  (rdpl
   (rdp-repll
    `(,filter-face-attribute ,identity)
    (rdp-begin
     (rdp-cond
      `((#(STARTTAG HEADING) ,(gen-p-head 'RTTL 'NONE) ,p-abstract)))
     (lambda (c h s)
       (let ((try ((rdp-begin p-body* (p-fsects '(S))) c h s)))
	 (if (and (start-tag? (head try))
		  (eq? (token-gi (head try)) 'SECTS))
	     try
	     ((p-fsects '(S))
	      c h
	      (stream-append
	       (stream `#(STARTTAG SECT
				   (#(ID IMPLIED #f)
				    #(LANG TOKEN (,((h 'get) 'LANGUAGE)))))
		       '#(STARTTAG HEADING ())
		       `#(DATA ,(((h 'get) 'PHRASE) 'intro))
		       '#(ENDTAG HEADING))
	       s)))))
     p-appendix
     ))))

(define normalize-report
  (rdpl
   (rdp-repll
    `(,filter-face-attribute ,identity)
    (rdp-cond
     `((#(STARTTAG HEADING) ,(gen-p-head 'RTTL 'NONE)
			    ,p-abstract
			    ,(rdpl (rdp-call p-body*))
			    ,(rdpl (p-sects '(S)))
			    ,p-appendix))))))

;}}}
;{{{ p-book

(define p-book
  (rdpl
   (rdp-repll
    `(,filter-face-attribute ,identity)
    (rdp-cond*
     `((#(STARTTAG HEADING)
	,(gen-p-head 'BTTL 'NONE)
	,(rdpl
	  (rdp-cond* `((#(STARTTAG (INTRO PREFACE)) ,(rdpp-keep p-body*)))))
	,(rdp-cond (list (gen-p-handle-incldiv-g rdp-leave)))
	,gen-p-handle-externals-and-unused-data-lines
	,(rdpl (rdp-cond `((#(STARTTAG PART) ,(p-parts '(P))))))
	,(rdpl (rdp-cond `((#(STARTTAG (CHAPT DIVISION)) ,(p-chapts '(C)))
			   (#(STARTSUBDOC) ,(p-chapts '(C))))))
	,p-appendix))))))

;}}}
;{{{ gen-p-brief

(define gen-p-brief
  (lambda s
    (require-load "include/brief.scm")
    p-brief))

;}}}
;{{{ normalize-switch

(define (normalize-switch s)
  (message 1 "Starting...")
  (let* ((bib (normal-bib-db))
	 (idx (make-index))
	 (nidx (member "NIDX"
		       (hook 'face 'run 
			     (let ((a (xat (head s) 'FACE)))
			       (if a
				   (arg-val a)
				   normalize-default-face)))))
	 (lang (let ((l (xat (head s) 'LANG)))
		 (if l l '#(LANG TOKEN ("EN")))))
	 (say (phrase (car (arg-val lang))))
	 )
    (set! pretend-special-sections-nidx nidx)
    ((rdp-cond
      `((#(STARTTAG DOCUMENT) ,p-document)
	(#(STARTTAG REPORT) ,(rdp-call normalize-report))
	(#(STARTTAG BOOK) ,p-book)
	(#(STARTTAG MANPAGE) ,p-manpage)
	(#(STARTTAG BRIEF) ,(rdp-sgram gen-p-brief))
	(#(STARTTAG LEKTION) ,normalize-lektion->division ,p-document)
	(#(STARTTAG LINUXDOC)
	 ,(rdp-sgram
	   (lambda (s)
	     (require-load "include/linuxdoc.scm")
	     (rdp-process linuxdoc->report)))
	 ,(rdp-call normalize-report))
	))
     `(,(lambda (c h s)
	  (if (not (stream-empty? s))
	      (fprint (current-error-port)
		      #"\nNormalize Error: Out of context: " (head s)))
	  empty-stream
	  ))
     (((make-state-env) 'def)
      `(DIVISION-ID ())
      `(IDS ,(gen-ns "Toplevel Id's"))
      `(BIBDB ,bib)
      `(INDEX-MARK ,(ints 1))
      `(ENUM-LEVEL ,(ints -1))
      `(LIST-LEVEL ,(ints -1))
      `(FIGURE-COUNTER ,(stream-map (lambda (h) `(,h F)) (ints 1)))
      `(INDEX ,idx)
      `(LANGUAGE ,(car (arg-val lang)))
      `(PHRASE ,say)
      `(EXTERNALS ,(external-handler
		    (lambda (token)
		      (hook 'external-messages 'run token))))
      `(subfiles ,(subfiles))
      )
     s)))

;}}}
;{{{ (contents level)

(define (normalize-contents level . variables)
  ; unused stuff ...
  (letrec ((names '((H0 . CH0) (H1 . CH1) (H2 . CH2) (H3 . CH3) (H4 . CH4)
		    (AH1 . CAH1) (AH2 . CAH2) (AH3 . CAH3) (AH4 . CAH4)))
	   (ren (lambda (t)
		  ((rename-tag (cdr (assq (token-gi t) names))) t))))
    (lambda (s)
      (apply
       rdp-parse
       (rdp-wrap
	'#(STARTTAG CONTENTS) '#(ENDTAG CONTENTS)
       (rdp-cond*
	`((#(STARTTAG
	     (HP H0 H1 AH1 
	      . ,(if (> level 1)
		     `(H2 AH2
		       . ,(if (> level 2)
			      `(H3 AH3
				. ,(if (> level 3)
				       `(H4 AH4)
				       '()))
			      '()))
		     '())))
	   ,(rdpp-keep p-text))
	  (#(ENDTAG (BOOK DOCUMENT REPORT MANPAGE)) ,rdp-leave)
	  (#() ,(rdp-skip 1)))))
       s
       variables))))

;}}}

(define (all-doctypes s)
  (car (hook 'normalize-cooked 'run
	     (normalize-switch (car (hook 'normalize-raw 'run s))))))