File: bhl.el

package info (click to toggle)
bhl 1.7.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge, squeeze
  • size: 556 kB
  • ctags: 402
  • sloc: lisp: 3,662; sh: 129; makefile: 94
file content (4288 lines) | stat: -rw-r--r-- 147,149 bytes parent folder | download | duplicates (3)
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
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
;;; bhl.el --- From (P)lain text to (H)tml and (L)aTeX.
;; Time-stamp: <2004-11-15 22:53:04 guerry>
 
;; Copyright (C) 2002 2003 2004 Bastien Guerry

;; Emacs Lisp Archive Entry
;; Filename: bhl.el
;; Author: Bastien Guerry <bzg AT altern DOT org>
;; Maintainer: Bastien Guerry <bzg AT altern DOT org>
;; Version: 1.7.3
;; Revised: 13/11/2004
;; Created: 16/11/2002
;; Keywords: convert plain text html latex sgml linuxdoc texinfo
;; Description: convert raw text into HTML/SGML/LaTeX/Texinfo/Text
;; URL: http://www.nongnu.org/bhl/

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.

;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.  See the GNU General Public License for more details.

;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA

;;; Commentary:

;; BHL mode is a mode wich enables you to convert plain text files into
;; HTML, LaTeX and SGML (Linuxdoc) files.  This is a simple mode, with
;; simple conversion functions, dedicated to simple source files.

;;; Neighbours:

;; emacs-wiki.el with latex-markup.el, html-markup.el and texinfo-markup.el
;; See the WikiDiscussion on http://www.emacswiki.org.

;;; Installation:

;; With `bhl.tar.gz':
;;   [your_shell]$ tar zxvf bhl-*.tar.gz
;;   [your_shell]$ make && make install
;;   [your_shell]$ make info && make install-info
;;   [your_shell]$ make pdf
;;   [your_shell]$ make html

;; With: `bhl.el':
;;   Just compile `bhl.el' and put it in your load-path.

;; Add to your `~/.emacs.el':
;;   (autoload 'bhl-mode "bhl" "BHL Mode" t)
;;   (add-to-list 'auto-mode-alist '("\\.bhl$" . bhl-mode))

;;; Usage:

;; `bhl2html'..................[C-c C-w]     -> convert BHL to HTML
;; `bhl2latex'.................[C-c C-l]     -> convert BHL to LaTeX
;; `bhl2sgml'..................[C-c C-s]     -> convert BHL to SGML (Linuxdoc)
;; `bhl2txt'...................[C-c C-d]     -> convert BHL to TXT
;; `bhl2texinfo'...............[C-c C-o]     -> convert BHL to Texinfo

;; `bhl-insert-toc'............[C-c C-c t]   -> insert the table of contents
;; `bhl-insert-url'............[C-c C-c h]   -> insert an URL
;; `bhl-insert-image'..........[C-c C-c i]   -> insert an image
;; `bhl-insert-verbatim'.......[C-c C-c v]   -> insert a verbatim environment
;; `bhl-insert-minipage'.......[C-c C-c m]   -> insert a minipage
;; `bhl-insert-tab'............[M-TAB]       -> insert a rigid tab

;; `bhl-change-font-bold'......[C-c C-f C-b] -> change font (bold)
;; `bhl-change-font-emphasis'..[C-c C-f C-e] -> change font (emphasis)
;; `bhl-change-font-underline'.[C-c C-f C-u] -> change font (underline)
;; `bhl-change-font-truetype'..[C-c C-f C-t] -> change font (truetype)
;; `bhl-change-font-bolditalic'[C-c C-f C-_] -> change font (bold-italic)
;; `bhl-change-font-normal'....[C-c C-f C-n] -> change font (normal)

;; `bhl-show-toc'..............[C-c C-t]     -> generate a browsable table of contents
;; `bhl-update-toc'............[C-c M-t]     -> update the prefix labels of sections
;; `bhl-guess-style'...........[C-c M-s]     -> guess the sectioning style of buffer
;; `bhl-show-lol'..............[C-c C-/]     -> generate a browsable list of links

;; `bhl-goto-next-section'.....[C-c C-n]     -> go to the next section
;; `bhl-goto-previous-section'.[C-c C-p]     -> go to the previous section
;; `bhl-goto-next-url-or-wiki'.[C-TAB]       -> go to the next URL or WikiName

;; `bhl-show-version'..........[C-c C-v]     -> display the BHL mode version number

;;; Known problems:

;; * BHL can't update sections if there are only (sub)subsections.

;;; Acknowledgments:

;; Thanks to Thierry Stoehr, Serge Basterot, Christoph Conrad, Peter
;; Kindermann, Mario Lang, Tim Cross.  Special thanks to Daniel
;; P. Katz for the bhl2xxx-batch-wrapper function and other contribs.

;;; Summary:

;; Page 1  - Variables and constants
;; Page 2  - Custom
;; Page 3  - Faces
;; Page 4  - Mode
;; Page 5  - Menu
;; Page 6  - Miscellaneous
;; Page 7  - Table of content
;; Page 8  - Submit bug report
;; Page 9  - bhl2xxx

;;; Code:

(eval-when-compile
  (mapcar 'require '(custom cl easymenu derived mouse font-lock))
  (unless (featurep 'xemacs)
    (require 'texnfo-upd)
    (require 'footnote)))


;;;; 1 - CONSTANTS

(defconst bhl-startup-message-lines
  '("Please use \\[bhl-submit-bug-report] to report bugs."
    "BHL comes with ABSOLUTELY NO WARRANTY."
    "Thanks for using the BHL mode!")
  "Lines to be displayed with `bhl-show-version'.")

(defconst bhl-help-address "bastien1NOSPAM@free.fr"
  "The address of the current maintainer.")

;;;; Regexp constants

(defconst bhl-wiki-names-regexp
  "\\<[A-Z][a-z]+\\([A-Z][a-z]+\\)+\\>"
  "Match a WikiName.")

(defconst bhl-generic-url-regexp
  "\\(url:\\|https?://\\|file:///\\|telnet:\\|mime:\\|s?news:\\|ftp://\\|mailto:\\)\\([^ [\n\t<]+\\>/?\\)"
  "Match a generic URL.")

(defconst bhl-url-regexp
  (concat "\\(\\(" bhl-generic-url-regexp "\\)\\|\\[\\[\\(\\(mailto:\\)?\\([^]\n\t]+\\>/?\\)\\)\\]\\[\\(\\<[^]\t]+\\)\\]\\]\\)")
  "Match a BHL URL.  Designed to work with `bhl-convert-url'.

* When matching a raw URL:
    (match-string 0) : return the whole URL.
    (match-string 3) : return the prefix of the URL (http:// of ftp:// or ...).
    (match-string 4) : return the rest of the URL.

* When matching an URL put into square brackets:
    (match-string 0) : return the URL and its name into brackets.
    (match-string 5) : return only the URL.

  - If the URL is of the form: \"mailto:bastien1NOSPAM@free.fr\":
      (match-string 6) : return \"mailto:\".
      (match-string 7) : return the email.

  - Else:
      (match-string 7) : return the URL.

  (match-string 8) : return the name of the URL.")

(defconst bhl-img-regexp
  "\\[\\[\\([^] \n\t]+\.\\(BMP\\|EPS\\|GIF\\|JP\\(?:E?G\\)\\|P\\(?:BM\\|GM\\|NG\\|PM\\|S\\)\\|TIFF\\|X\\(?:[BP]M\\)\\|bmp\\|eps\\|gif\\|jp\\(?:e?g\\)\\|p\\(?:bm\\|gm\\|ng\\|pm\\|s\\)\\|tiff\\|x\\(?:[bp]m\\)\\)\\)\\]\\]"
  "Match images.")	   ; `image-file-name-regexp' does not suffice

(defconst bhl-title-regexp
  "\\`[ \t\n\r]*\\([^\n]+\\)$"
  "Match the title.")

(defconst bhl-hr-regexp
  "^\\([<>-]\\)---+\\(\\[\\(\\([0-9]+\\)[^] ]*\\)\\]\\)?"
  "Match a horizontal rule.")

(defconst bhl-hr1-regexp
  "^\\([<>=]\\)===+\\(\\[\\(\\([0-9]+\\)[^] ]*\\)\\]\\)?"
  "Match a horizontal rule.")

(defconst bhl-hr2-regexp
  "^\\([<>/]\\)///+\\(\\[\\(\\([0-9]+\\)[^] ]*\\)\\]\\)?"
  "Match a horizontal rule.")

(defconst bhl-minipage-regexp
  "^\\([<>%]\\)\\([-%]\\)%+\\(\\[\\([^] ]+\\)\\]\\)?"
  "Match a starting minipage environment.")

(defconst bhl-list-regexp
  "^\t\t?\t?\t?\t?\\([-*o]\\|[0-9]+\\.\\) "
  "Match lists items.")

(defconst bhl-descrip-regexp
  "^\\(\t+\\)\\([^\n-]+\\) -- "
  "Match items of lists of descriptions.")

(defconst bhl-list-regexp-list
  '(("^\\(\t\t\t\t\t\\)\\([-*o]\\|[0-9]+\.\\) "
     "^\t\t?\t?\t?\\([-*o]\\|[0-9]+\.\\) \\|^[^\n\t ]\\|\\'")
    ("^\\(\t\t\t\t\\)\\([-*o]\\|[0-9]+\.\\) "
     "^\t\t?\t?\\([-*o]\\|[0-9]+\.\\) \\|^[^\n\t ]\\|\\'")
    ("^\\(\t\t\t\\)\\([-*o]\\|[0-9]+\.\\) "
     "^\t\t?\\([-*o]\\|[0-9]+\.\\) \\|^[^\n\t ]\\|\\'")
    ("^\\(\t\t\\)\\([-*o]\\|[0-9]+\.\\) "
     "^\t\\([-*o]\\|[0-9]+\.\\) \\|^[^\n\t ]\\|\\'")
    ("^\\(\t\\)\\([-*o]\\|[0-9]+\.\\) "
     "^[^\n\t ]\\|\\'"))
  "A list of regexp that match list items of third, second and first level.")

(defconst bhl-list-syntax-alist
  '((html ("<ul>" "<li>" "</ul>")
	  ("<ol>" "<li>" "</ol>")
	  ("<dl>" "<dt>" "</dt><dd>" "</dl>"))
    (latex ("\\begin{itemize}" "\\\\item " "\\end{itemize}")
	   ("\\begin{enumerate}" "\\\\item " "\\end{enumerate}")
	   ("\\begin{description}" "\\item [" "] " "\\end{description}"))
    (sgml ("<p><itemize>" "<item>" "</itemize>")
	  ("<p><enum>" "<item>" "</enum>")
	  ("<p><descrip>" "<tag>" "</tag> " "</descrip>"))
    (texi ("@itemize @minus" "@item\n" "@end itemize")
	  ("@enumerate" "@item\n" "@end enumerate")
	  ("@table @emph" "@item " "\n" "@end table")))
  "Alist of syntactic elements composing unordered, ordered and description lists.")

;;; Miscellaneous variables

(defvar bhl-local-lol-list nil)
(defvar bhl-local-latex-class "")
(defvar bhl-local-latex-class-options nil)
(defvar bhl-local-html-style "")
(defvar bhl-local-lang "")
(defvar bhl-local-texi-titlepage-style "")

(defvar bhl-mode-map (make-keymap)
  "Keymap for the BHL major mode.")

(defvar bhl-toc-mode-map (make-keymap)
   "Keymap for the BHL toc mode.")

(defvar bhl-lol-mode-map (make-keymap)
   "Keymap for the BHL lol mode.")

(defvar bhl-version "1.7.1"
  "The current version of the BHL mode.")

(defvar bhl-popup-menu-map
  (if (current-local-map)
      (copy-keymap (current-local-map))
    (make-keymap))
  "A popup menu for BHL.")

(defvar bhl-toc-temporary-depth nil
  "The depth of toc set into the *toc* buffer.")

(defvar bhl-toc-point-list nil
  "The list of points used to browse the toc.")

(defvar bhl-tpl nil
  "The list of point positions corresponding to the list of (sub)sections.
See `bhl-tsl'.")

(defvar bhl-tsl nil
  "The list of sections required to build and browse the toc.
See `bhl-tpl'.")

(defvar bhl-font-lock-keywords nil
  "Keywords to be fontified in the BHL mode.")

(defvar bhl-conversion-log nil
  "This variable stores the log for each conversion.")

(defvar bhl-xxx-conversions-list nil
  "Alist of conversion functions.")

(defvar bhl-tag-regexp-list nil
  "Tags elements understood by BHL.")

(defconst bhl-tag-regexp-list0
  '("[^\\]\\(__\\([^ \t\r\n\"_][^_]*\\)__\\)"
    "[^\\]\\(==\\([^ \t\r\n=\"][^=]*\\)==\\)"
    "[^_\\]\\(_\\([^ \t\r\n_\"][^_]*\\)_\\)[^_]"
    "[^\\]\\(\\*\\([^ \t\r\n*\"][^*]*\\)\\*\\)")
  "Tags elements understood by BHL.")

(defconst bhl-tag-regexp-list1
  '("[^\\]\\(__\\([^ \t\r\n\"_][^\n\r_]*\\)__\\)"
    "[^\\]\\(==\\([^ \t\r\n=\"][^\n\r=]*\\)==\\)"
    "[^_\\]\\(_\\([^ \t\r\n_\"][^\n\r_]*\\)_\\)[^_]"
    "[^\\]\\(\\*\\([^ \t\r\n*\"][^\n\r*]*\\)\\*\\)")
  "Tags elements understood by BHL.")

(defconst bhl-tag-syntax-alist
  '((html ("<u>" "</u>") ("<tt>" "</tt>")
	  ("<em>" "</em>") ("<b>" "</b>"))
    (latex ("\\underline{" "}") ("\\texttt{" "}")
	   ("\\emph{" "}") ("\\textbf{" "}"))
    (sgml ("<it>" "</it>") ("<tt>" "</tt>")
	  ("<em>" "</em>") ("<bf>" "</bf>"))
    (texi ("@i{" "}") ("@t{" "}")
	  ("@emph{" "}") ("@strong{" "}"))
    (txt ("" "") ("" "")
	 ("" "") ("" "")))
  "Alist of font beautifiers tags corresponding to each conversion format.")

(defconst bhl-section-syntax-alist
  '((html ("<h1>" "</h1>") ("<h2>" "</h2>") ("<h3>" "</h3>"))
    (latex ("\\section{" "}") ("\\subsection{" "}")
	   ("\\subsubsection{" "}"))
    (latex-nonum ("\\section\*{" "}") ("\\subsection\*{" "}")
		 ("\\subsubsection\*{" "}"))
    (sgml ("<sect>" "") ("<sect1>" "") ("<sect2>" ""))
    (texi ("@chapter " "") ("@section " "") ("@subsection " ""))
    (txt ("= " " =") ("== " " ==") ("=== " " ===")))
  "Alist of sections tags corresponding  to each conversion format.")

(defvar bhl-sectioning-regexp-list nil
  "Alist of regexp that match sections and subsections.
the first element matches the prefix of a sections title.
the second element matches the prefix of a subsections title.
the third element matches the prefix of a subsections title.")

;; punctuation marks

(defconst bhl-fr-punctuation
  '(("\\(:\\)[ \t\n]" "&nbsp;:" "~:" "@ :")
    ("\\(\\?\\)[ \t\n]" "&nbsp;?" "~?" "@ ?")
    ("\\(!\\)[ \t\n]" "&nbsp;!" "~!" "@ !")
    ("\\(;\\)[ \t\n]" "&nbsp;;" "~;" "@ ;"))
  "A list of punctuation marks respecting the french typographic conventions.")

(defconst bhl-de-punctuation nil
  "A list of punctuation marks respecting the german typographic conventions.")

(defconst bhl-en-punctuation nil
  "A list of punctuation marks respecting the english typographic conventions.")

;; Quotation marks

(defconst bhl-en-quotation-marks
  '(("[ \'\n\t\(]\\(\\)"  "&ldquo;" "``" "``")
    ("[ \'\n\t\(]\\(\"\\)" "&ldquo;" "``" "``")
    ("\\(\\)[ \n\t,\.:;\?!\)]" "&rdquo;" "''" "''")
    ("\\(\"\\)[ \n\t,\.:;\?!\)]" "&rdquo;" "''" "''"))
  "A list of quote chars to convert.")

(defconst bhl-de-quotation-marks
  '(("[ \'\n\t\(]\\(\\)"  "&ldquo;" "``" "``")
    ("[ \'\n\t\(]\\(\"\\)" "&ldquo;" "``" "``")
    ("\\(\\)[ \n\t,\.:;\?!\)]" "&rdquo;" "''" "''")
    ("\\(\"\\)[ \n\t,\.:;\?!\)]" "&rdquo;" "''" "''"))
  "A list of quote chars to convert.")
    
(defconst bhl-fr-quotation-marks
  '(("[ \'\n\t\(]\\(\\)" "&laquo;&nbsp;" "~" "``")
    ("[ \'\n\t\(]\\(\"\\)" "&laquo;&nbsp;" "~" "``")
    ("\\(\\)[ \n\t,\.:;\?!\)]" "&nbsp;&raquo;" "~" "''")
    ("\\(\"\\)[ \n\t,\.:;\?!\)]" "&nbsp;&raquo;" "~" "''"))
  "A list of quote characters to convert.")
    
;; Special characters

(defconst bhl-fr-special-chars
  '(("<"  "&lsaquo;" "<" "<")
    (">"  "&rsaquo;" ">" ">")
    ("oe" "&oelig;" "\\oe{}" "@oe{}")
    ("OE" "&OElig;" "\\OE{}" "@OE{}")
    ("ae" "&aelig;" "\\ae{}" "@ae{}")
    ("AE" "&AElig;" "\\AE{}" "@EA{}"))
  "A list of strings to convert into french ligatured characters.")

(defconst bhl-de-special-chars
  '(("<"  "&lsaquo;" "<" "<")
    (">"  "&rsaquo;" ">" ">")
    ("oe" "&ouml;" "" "@\"o")
    ("Oe" "&Ouml;" "" "@\"O")
    ("ae" "&auml;" "" "ae")
    ("Ae" "&Auml;" "" "Ae")
    ("Ue" "&Uuml;" "" "Ue")
    (""  "&szlig;" "" "@ss{}"))
  "A list of strings to convert into german ligatured characters.")

(defconst bhl-en-special-chars
  '(("<"  "&lsaquo;" "<" "<")
    (">"  "&rsaquo;" ">" ">"))
  "A list of strings to convert into english ligatured characters.")

(defconst bhl-latex-escaped-chars
  '("$" "&" "%" "{" "}"))

(defconst bhl-texi-escaped-chars
  '("@" "{" "}"))

(defconst bhl-escapable-chars
  '("*" "_" "#" "\\"))


;;;; 2 - CUSTOM

(defgroup bhl nil
  "BHL mode: convert plain TXT to HTML, LaTeX and SGML."
  :group 'wp
  :link '(custom-manual "(bhl)Top")
  :link '(url-link "http://www.nongnu.org/bhl")
  :prefix "bhl-")

(defgroup bhl-wiki nil
  "BHL as a local wiki."
  :group 'bhl)

(defgroup bhl2html nil
  "Customize the `bhl2html' output."
  :group 'bhl)

(defgroup bhl2latex nil
  "Customize the `bhl2latex' output."
  :group 'bhl)

(defgroup bhl2texinfo nil
  "Customize the `bhl2texinfo' output."
  :group 'bhl)

(defgroup bhl2sgml nil
  "Customize the `bhl2sgml' output."
  :group 'bhl)

(defgroup bhl2txt nil
  "Customize the `bhl2txt' output."
  :group 'bhl)

(defgroup bhl-toc nil
  "How to handle the table of contents."
  :group 'bhl)

(defgroup bhl-tables nil
  "How BHL must build tables."
  :group 'bhl)

(defgroup bhl-faces nil
  "*BHL faces used in BHL mode."
  :group 'faces
  :group 'bhl)

;;;; Main custom

(defcustom bhl-mode-hook '(turn-on-auto-fill)
  "Normal hook run when entering BHL mode."
  :type '(hook)
  :group 'bhl)

(defcustom bhl-after-conversion-hook '(bhl-initialize-properties)
  "Hook run after any conversion.
The default hook is `bhl-initialize-properties'.
If you remove this function, the conversion properties of the
last converted buffer will be the default conversion properties
for the next converted buffer."
  :type '(hook)
  :group 'bhl)

(defcustom bhl-autoguess-style-flag t
  "Non-nil means autoguess the sectioning style.
When you find a file, BHL tries to guess its sectioning style
and sets the value of `bhl-sectioning-default-style' to the value
of the file's style.

See `bhl-guess-style' and `bhl-sectioning-default-style'."
  :type '(boolean)
  :link '(custom-manual "(bhl)Sections")
  :group 'bhl)

(defcustom bhl-sectioning-default-style 'num
  "The style of sections and (sub)subsections in the BHL file.
NUM indicates numerical style (e.g. \"1.2.\").
ALPHA indicates alphabetical style (e.g. \"A.B.\").
ASTER indicates that you use asteriks (e.g. \"*\").
EQUAL-SIGN indicates that you use equal-sign (e.g. \"=\").
MY indicates your own style, as defined by `bhl-my-sectioning-regexp-list'."
  :type '(radio (const :tag "Numerical" num)
		(const :tag "Alphabetical" alpha)
		(const :tag "Asterisks" aster)
		(const :tag "Equal signs" equal-sign)
		(const :tag "My own style" my))
  :link '(custom-manual "(bhl)Sections")
  :group 'bhl)

(defcustom bhl-my-sectioning-regexp-list
  '("[0-9]+\\. " "[0-9]+\\.[0-9]+\\. " "[0-9]+\\.[0-9]+\\.[0-9]+\\. ")
  "Your own list of regexp that match (sub)sections' prefix.

The default value for this list is equal to the value of
`bhl-sectioning-regexp-list'.

Please pay a special attention to whitespaces.
Don't use any subexpression.
Don't use the \{.\} construct.

If you want to choose your own list for the current sectioning
style, set `bhl-sectioning-default-style' to 'my.  Remind that you cannot use
`bhl-update-toc' anymore if you select your own sectioning style."
  :type '(list (regexp :tag "Section      ")
	       (regexp :tag "Subsection   ")
	       (regexp :tag "Subsubsection"))
  :link '(custom-manual "(bhl)Sections")
  :group 'bhl)

(defcustom bhl-verbatim-ignore
  '(tag comment list description table url wikiname
	images special-char tex-label footnote quote)
  "A list of non-converted elements inside verbatim regions.
Here are the relevant symbols that you can insert in this list:

  tag          : *word* and the like.
  comment      : #comment stings
  list         : * lists
  description  : Description --
  table        : | tables |
  url          : any kind of URL
  wikiname     : WikiNames
  images       : [[image.jpg]]
  special-char : e.g. ligatured \"oe\"
  tex-label    : LaTeX, TeX and LaTeX2e
  footnote     : footnote like this one[1]
  quote        : [tab]A quoted sentence"
  :type '(repeat (symbol :tag "Ignore "))
  :link '(custom-manual "(bhl)Environments")
  :group 'bhl)

(defcustom bhl-ignored-regexps nil
  "A list of regexps.
The lines matching these regexps are automatically skipped."
  :type '(repeat (regexp))
  :group 'bhl)

(defcustom bhl-tags-overlap-flag t
  "Non-nil means that you can use tags on multiple lines."
  :type '(boolean)
  :group 'bhl)

(defcustom bhl-i18n-conventions '("en" t t t)
  "English, french and german conventions.
These conventions are relative to the punctuation,
the quotation marks and some special characters."
  :type '(list (radio :tag "Language"
		      (const :tag "English" "en")
		      (const :tag "French" "fr")
		      (const :tag "German" "de"))
	       (boolean :tag "Punctuation     " :indent 2
			:help-echo "Non-nil means follow punctuation conventions.")
	       (boolean :tag "Quotation marks " :indent 2
			:help-echo "Non-nil means convert quotation marks.")
	       (boolean :tag "Special chars   " :indent 2
			:help-echo "Non-nil means convert special characters."))
  :link '(custom-manual "(bhl)Global options")
  :group 'bhl)

(defcustom bhl-browse-url-function 'browse-url
  "Function to call to browse a URL."
  :type '(function)
  :group 'bhl)

(defcustom bhl-tab-width 3
  "*The default width of the tab character."
  :type '(integer)
  :group 'bhl)

;; BHL as local Wiki

(defcustom bhl-is-a-local-wiki-flag nil
  "Non-nil means that MixedCase words are wikinames."
  :type '(boolean)
  :group 'bhl-wiki)

(defcustom bhl-default-wikifiles-extension ".bhl"
  "The default extension for new files created when following a wiki name."
  :type '(string :tag "Extension ")
  :group 'bhl-wiki)

(defcustom bhl-downcase-wikifiles-names-flag nil
  "Non-nil means that BHL downcases the name of a wiki file."
  :type '(boolean)
  :group 'bhl-wiki)

(defcustom bhl-non-wiki-names-list nil
  "A list of strings that are NOT WikiNames."
  :type '(repeat (string :tag "Non-wiki name "))
  :group 'bhl-wiki)

;;;; Customize the table of contents

(defcustom bhl-default-toc-depth 3
  "*The default depth of the table of contents."
  :type '(integer)
  :link '(custom-manual "(bhl)The table of contents")
  :group 'bhl-toc)

(defcustom bhl-intro-toc "--- Table of contents"
  "*A string inserted just before the table of contents."
  :type '(string)
  :group 'bhl-toc)

(defcustom bhl-end-toc "--- End of the table of contents"
  "*A string inserted just after the table of contents."
  :type '(string)
  :group 'bhl-toc)

(defcustom bhl-toc-location "top"
  "*The place into which the table of contents is inserted."
  :type '(radio (const :tag "Juste after title" "top")
		(const :tag "Juste before end" "bottom")
		(other :tag "On the point" "point"))
  :link '(custom-manual "(bhl)The table of contents")
  :group 'bhl-toc)

;;;; Customize the conversion of tables

(defcustom bhl-table-location "htbp"
  "*The location of the table.
\"here top bottom page\" means put the table here or at the top
of the page or at the bottom of the page or on a separate page."
  :type '(radio (const :tag "here" "h")
		(const :tag "top" "t")
		(const :tag "bottom" "b")
		(const :tag "page" "p")
		(other :tag "here top bottom page" "htbp"))
  :group 'bhl-tables)

(defcustom bhl-table-align "center"
  "*How to align each table."
  :type '(radio (const "left")
		(const "center")
		(const "right"))
  :group 'bhl-tables)

(defcustom bhl-table-cell-align "c"
  "*How to align each cell in a table."
  :type '(radio (const :tag "left" "l")
		(const :tag "center" "c")
		(other :tag "right" "r"))
  :group 'bhl-tables)

;;;; Customize the HTML output.

(defcustom bhl-after-html-conversion-hook nil
  "Hook run after the conversion into HTML.
This hook is run just before `bhl-after-conversion-hook'."
  :type '(hook)
  :group 'bhl2html)

(defcustom bhl2html-properties-list '(t t nil t t)
  "List of properties of the `bhl2html' conversion."
  :type '(list
	  (boolean :tag "Check dubious tags         ")
	  (boolean :tag "Insert sections' prefix    ")
	  (boolean :tag "Ask caption for tables     ")
	  (boolean :tag "Use i18n conventions       ")
	  (boolean :tag "Convert toc                "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2html)

(defcustom bhl-html-conversions-list '(t t t t t t t t nil t t t t t)
  "*A list of conversion functions to perform with `bhl2html'.
Each element of this list corresponds to a conversion function."
  :type '(list
	  (boolean :tag "Convert verbatim regions ")
	  (boolean :tag "Convert horizontal rules ")
	  (boolean :tag "Convert images           ")
	  (boolean :tag "Convert URLs             ")
	  (boolean :tag "Convert definition lists ")
	  (boolean :tag "Convert normal list      ")
	  (boolean :tag "Convert tables           ")
	  (boolean :tag "Convert LaTeX labels     "
		   :help-echo "Convert \"LaTeX\", \"LaTeX2e\" and \"TeX\" properly.")
	  (boolean :tag "Convert footnotes        ")
	  (boolean :tag "Convert minipage         "
		   :help-echo "Minipages are tables in HTML and minipages in LaTeX.")
	  (boolean :tag "Convert comments         ")
	  (boolean :tag "Convert escape sequences ")
	  (boolean :tag "Convert WikiNames        ")
	  (boolean :tag "Convert quote            "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2html)

(defcustom bhl-html-title-tags '("<h1 align=\"center\">" "</h1>")
  "A list of the opening and closing HTML tags for the title."
  :type '(list (string :tag "Opening tag")
	       (string :tag "Closing tag"))
  :group 'bhl2html)

(defcustom bhl-html-subtitle-tags '("<h2 align=\"center\">" "</h2>")
  "A list of the opening and closing HTML tags for the subtitle."
  :type '(list (string :tag "Opening tag")
	       (string :tag "Closing tag"))
  :group 'bhl2html)

(defcustom bhl-html-meta-alist '(("generator" . "bhl2html"))
  "*A list of META tags.
The first field is the value of \"NAME\".
The second field is the value of \"CONTENT\".

Example:

author    (in the first field [key])
Monique   (in the second field [value])

outputs

<meta name=\"author\" content=\"Monique\">"
  :type '(alist :key-type
		(string
		 :tag "Name"
		 :help-echo "Insert the \"NAME\" value of the META tag.")
		:value-type
		(string
		 :tag "Content"
		 :help-echo "Insert the \"CONTENT\" value of the META tag."))
  :group 'bhl2html)

(defcustom bhl-html-link-alist '(("generator-home" . "http://www.nongnu.org/bhl"))
  "*A list of LINK tags.
The first field is the value of \"REL\".
The second field is the value of \"HREF\".

Example:

alternate    (in the first field [key])
indexbis.html   (in the second field [value])

outputs

<link rel=\"alternate\" href=\"indexbis.html\">"
  :type '(alist :key-type
		(string
		 :tag "REL"
		 :help-echo "Insert the \"REL\" value of the LINK tag.")
		:value-type
		(string
		 :tag "HREF"
		 :help-echo "Insert the \"HREF\" value of the LINK tag."))
  :group 'bhl2html)

(defcustom bhl-html-doctype
  "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
  "*The DOCTYPE description of the HTML document."
  :type '(string)
  :group 'bhl2html)

(defcustom bhl-html-content-type
  "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"
  "*The META tag which describes the content type of the HTML document."
  :type '(string)
  :group 'bhl2html)

(defcustom bhl-html-default-style ""
  "*The style to be inserted in the header of the HTML output."
  :type '(string)
  :group 'bhl2html)

(defcustom bhl-html-table-param-alist
  '(("cellpadding" . "3")
    ("cellspacing" . "0")
    ("border" . "1"))
  "*Alist of parameters for the conversion of tables.
The first field is the name of the parameter, the second field
specifies its value.

Example:

\"cellpadding\" (in the first field [key])
\"3\"   (in the second field [value])

outputs

<table cellpadding=\"3\" ...>"
  :type '(alist :key-type (string
			   :tag "Property"
			   :help-echo "Insert the property name.")
		:value-type (string
			     :tag "Value"
			     :help-echo "Insert the value of the property."))
  :group 'bhl2html)

(defcustom bhl-html-para-align "none"
  "*How to align paragraphs."
  :type '(radio (const "left")
		(const "center")
		(const "justify")
		(const "right")
		(const "none"))
  :group 'bhl2html)

(defcustom bhl-html-img-align "center"
  "*How to align each image."
  :type '(radio (const "left")
		(const "center")
		(const "right"))
  :group 'bhl2html)

(defcustom bhl-html-list-item-is-para-flag t
  "*Non-nil means that a list item is a paragraph."
  :type '(boolean)
  :group 'bhl2html)

;;;; Customize the LaTeX output.

(defcustom bhl-after-latex-conversion-hook nil
  "Hook run after the conversion into LaTeX.
This hook is run just before `bhl-after-conversion-hook'."
  :type '(hook)
  :group 'bhl2latex)

(defcustom bhl2latex-properties-list '(t t nil t t)
  "List of properties of the `bhl2latex' conversion."
  :type '(list
	  (boolean :tag "Check dubious tags         ")
	  (boolean :tag "Insert sections' prefix    ")
	  (boolean :tag "Ask caption for tables     ")
	  (boolean :tag "Use i18n conventions       ")
	  (boolean :tag "Convert toc                "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2latex)

(defcustom bhl-latex-conversions-list
  '(t t t t t t t t nil t t t t t)
  "*A list of conversion functions to perform with `bhl2latex'.
Each element of this list corresponds to a conversion function."
  :type '(list
	  (boolean :tag "Convert verbatim regions ")
	  (boolean :tag "Convert horizontal rules ")
	  (boolean :tag "Convert images           ")
	  (boolean :tag "Convert URLs             ")
	  (boolean :tag "Convert definition lists ")
	  (boolean :tag "Convert normal list      ")
	  (boolean :tag "Convert tables           ")
	  (boolean :tag "Convert LaTeX labels     "
		   :help-echo "Convert \"LaTeX\", \"LaTeX2e\" and \"TeX\" properly.")
	  (boolean :tag "Convert footnotes        ")
	  (boolean :tag "Convert minipage         "
		   :help-echo "Minipages are tables in HTML and minipages in LaTeX.")
	  (boolean :tag "Convert comments         ")
	  (boolean :tag "Convert escape sequences ")
  	  (boolean :tag "Convert WikiNames        ")
  	  (boolean :tag "Convert quote            "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2latex)

(defcustom bhl-latex-packages-alist
  '(("inputenc" . "latin1"))
  "*Alist of packages to be included in the LaTeX header.
The first field is the name of the package, the second field
specifies optional elements.

Example:

inputenc (in the first field [key])
latin1   (in the second field [value])

outputs

\\usepackage[latin1]{inputenc}"
  :type '(alist :key-type (string
			   :tag "Package"
			   :help-echo "Insert the name of the LaTeX package.")
		:value-type (string
			     :tag "Options"
			     :help-echo "Insert the values of the package options."))
  :group 'bhl2latex)

(defcustom bhl-latex-default-class "article"
  "*The document's class for the LaTeX output."
  :type '(string)
  :group 'bhl2latex)

(defcustom bhl-latex-default-class-options '("12pt" "a4paper")
  "*The default options for the \documentclass command in the LaTeX output."
  :type '(repeat (string :tag "Option "))
  :group 'bhl2latex)

(defcustom bhl-latex-extra-preambles nil
  "*A list of lines to be included in the LaTeX header."
  :type '(repeat (string :tag "Preamble line"))
  :group 'bhl2latex)
 
(defcustom bhl-latex-extra-body nil
  "*A list of lines to be included before the first section."
  :type '(repeat (string :tag "Pre-body line"))
  :group 'bhl2latex)

;; Customize the SGML output

(defcustom bhl-after-sgml-conversion-hook nil
  "Hook run after the conversion into SGML.
This hook is run just before `bhl-after-conversion-hook'."
  :type '(hook)
  :group 'bhl2sgml)

(defcustom bhl2sgml-properties-list '(t nil t)
  "List of properties of the `bhl2sgml' conversion."
  :type '(list
	  (boolean :tag "Check dubious tags      ")
	  (boolean :tag "Ask caption for tables  ")
	  (boolean :tag "Use i18n conventions    "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2sgml)

(defcustom bhl-sgml-conversions-list
  '(t t t t t t t t nil t t t t t)
  "*A list of conversion functions to perform with `bhl2sgml'.
Each element of this list corresponds to a conversion function."
  :type '(list
	  (boolean :tag "Convert verbatim regions ")
	  (boolean :tag "Convert horizontal rules ")
	  (boolean :tag "Convert images           ")
	  (boolean :tag "Convert URLs             ")
	  (boolean :tag "Convert definition lists ")
	  (boolean :tag "Convert normal list      ")
	  (boolean :tag "Convert tables           ")
	  (boolean :tag "Convert LaTeX labels     "
		   :help-echo "Convert \"LaTeX\", \"LaTeX2e\" and \"TeX\" properly.")
	  (boolean :tag "Convert footnotes        ")
	  (boolean :tag "Convert minipage         "
		   :help-echo "Minipages are tables in HTML and minipages in LaTeX.")
	  (boolean :tag "Convert comments         ")
	  (boolean :tag "Convert escape sequences ")
	  (boolean :tag "Convert WikiNames        ")
	  (boolean :tag "Convert quote            "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2sgml)

(defcustom bhl-sgml-minipage-tag "<code>"
  "The style of any minipage in the SGML output.
The default value is \"<code>\".  This means that a
minipage is formatted as an example of code."
  :type '(string)
  :group 'bhl2sgml)

(defcustom bhl-sgml-list-item-is-para-flag t
  "*Non-nil means that a list item is a paragraph."
  :type '(boolean)
  :group 'bhl2sgml)

;; Customize the TEXI output

(defcustom bhl-after-texi-conversion-hook
  '(texinfo-every-node-update
    bhl-texi-make-menu)
  "Hook run after the conversion into TEXI.
This hook is run just before `bhl-after-conversion-hook'."
  :type '(hook)
  :group 'bhl2texinfo)

(defcustom bhl2texi-properties-list '(t t t)
  "List of properties of the `bhl2texinfo' conversion."
  :type '(list (boolean :tag "Check dubious tags         ")
	       (boolean :tag "Use i18n conventions       ")
	       (boolean :tag "Convert toc                "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2texinfo)

(defcustom bhl-texi-conversions-list
  '(t t t t t t nil t t t t t t t)
  "*A list of conversion functions to perform with `bhl2html'.
Each element of this list corresponds to a conversion function."
  :type '(list
	  (boolean :tag "Convert verbatim regions ")
	  (boolean :tag "Convert horizontal rules ")
	  (boolean :tag "Convert images           ")
	  (boolean :tag "Convert URLs             ")
	  (boolean :tag "Convert definition lists ")
	  (boolean :tag "Convert normal list      ")
	  (boolean :tag "Convert tables           ")
	  (boolean :tag "Convert LaTeX labels     "
		   :help-echo "Convert \"LaTeX\", \"LaTeX2e\" and \"TeX\" properly.")
	  (boolean :tag "Convert footnotes        ")
	  (boolean :tag "Convert minipage         "
		   :help-echo "Minipages are tables in HTML and minipages in LaTeX.")
	  (boolean :tag "Convert comments         "
		   :help-echo "In TEXI output, converting comments means deleting them.")
	  (boolean :tag "Convert escape sequences ")
	  (boolean :tag "Convert WikiNames        ")
	  (boolean :tag "Convert quote            "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2texinfo)

(defcustom bhl-texi-setchapternewpage-flag "on"
  "Non-nil means that chapter are inserted into a new page."
  :type '(radio (const "on")
		(const "off"))
  :group 'bhl2texinfo)

(defcustom bhl-texi-titlepage-style 'classical
  "The style of the titlepage in the TEXI output."
  :type '(radio (const :tag "Classical" classical)
                (const :tag "Centered" centered))
  :group 'bhl2texinfo)

(defcustom bhl-texi-centered-minipage-tag "display"
  "The style of the centered minipage in the TEXI output.
The default value is \"display\".  This means that a so-called
centered minipage is formatted as an example without special font."
  :type '(string)
  :group 'bhl2texinfo)

(defcustom bhl-texi-table-tag "verbatim"
  "The tag for the conversion of tables in the TEXI output.
The default value is \"verbatim\".  If you want to skip tables
when converting into TEXI, you should set this to \"ignore\"."
  :type '(radio (const "verbatim")
		(const "ignore"))
  :group 'bhl2texinfo)

;; Customize the TXT output

(defcustom bhl-after-txt-conversion-hook '(bhl-tab-to-spc)
  "Hook run after the conversion into TXT.
This hook is run just before `bhl-after-conversion-hook'."
  :type '(hook)
  :group 'bhl2txt)

(defcustom bhl2txt-properties-list '(t t t)
  "List of properties of the `bhl2txt' conversion."
  :type '(list (boolean :tag "Center title and sections ")
	       (boolean :tag "Convert toc               ")
	       (boolean :tag "Convert tabs to spaces    "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2txt)

(defcustom bhl-txt-conversions-list
  '(t t t t nil nil t nil nil t t t t nil)
  "*A list of conversion functions to perform with `bhl2html'.
Each element of this list corresponds to a conversion function."
  :type '(list
	  (boolean :tag "Convert verbatim regions ")
	  (boolean :tag "Convert horizontal rules ")
	  (boolean :tag "Convert images           ")
	  (boolean :tag "Convert URLs             ")
	  (boolean :tag "Convert definition lists ")
	  (boolean :tag "Convert normal list      ")
	  (boolean :tag "Convert tables           ")
	  (boolean :tag "Convert LaTeX labels     "
		   :help-echo "Convert \"LaTeX\", \"LaTeX2e\" and \"TeX\" properly.")
	  (boolean :tag "Convert footnotes        ")
	  (boolean :tag "Convert minipage         "
		   :help-echo "Minipages are tables in HTML and minipages in LaTeX.")
	  (boolean :tag "Convert comments         "
		   :help-echo "In TXT output, converting comments means deleting them.")
	  (boolean :tag "Convert escape sequences ")
	  (boolean :tag "Convert WikiNames        ")
	  (boolean :tag "Convert quote            "))
  :link '(custom-manual "(bhl)Properties of conversion")
  :group 'bhl2txt)

;; Set up after custom

(defvar bhl-sectioning-style-alist
  `((num ("[0-9]+\\. "
	  "[0-9]+\\.[0-9]+\\. "
	  "[0-9]+\\.[0-9]+\\.[0-9]+\\. "))
    (alpha ("[A-Z]\\. "
	    "[A-Z]\\.[A-Z]\\. "
	    "[A-Z]\\.[A-Z]\\.[A-Z]\\. "))
    (aster ("\\* " "\\*\\* " "\\*\\*\\* "))
    (equal-sign ("= " "== " "=== "))
    (my ,bhl-my-sectioning-regexp-list)))


;;;; 3 - FACES

(defface bhl-img-face
  '((t (:foreground "aquamarine4")))
  "*The BHL face for images."
  :group 'bhl-faces)

(defface bhl-wiki-face
  '((t (:foreground "aquamarine4")))
  "*The BHL face for WikiNames."
  :group 'bhl-faces)

(defface bhl-url-face
  '((t (:foreground "aquamarine4")))
  "*The BHL face for URL."
  :group 'bhl-faces)

(defface bhl-comment-face
  '((t (:foreground "darkcyan")))
  "*The BHL face for comments."
  :group 'bhl-faces)

(defface bhl-list-face
  '((t (:foreground "royalblue")))
  "*The BHL face for list separators."
  :group 'bhl-faces)

(defface bhl-descrip-face
  '((t (:foreground "royalblue")))
  "*The BHL face for descriptive list separators."
  :group 'bhl-faces)

(defface bhl-hr-face
  '((t (:foreground "darkred")))
  "*The BHL face for horizontal rule."
  :group 'bhl-faces)

(defface bhl-toc-face
  '((t (:foreground "darkcyan")))
  "*The BHL face for the table of contents separators."
  :group 'bhl-faces)

(defface bhl-title-face
  '((t (:weight bold :height 1.9)))
  "*The BHL face for the title."
  :group 'bhl-faces)

(defface bhl-section-face
  '((t (:weight bold :height 1.6)))
  "*The BHL face for the sections."
  :group 'bhl-faces)

(defface bhl-subsection-face
  '((t (:weight bold :height 1.3)))
  "*The BHL face for the subsections."
  :group 'bhl-faces)

(defface bhl-subsubsection-face
  '((t (:weight bold)))
  "*The BHL face for the subsubsections."
  :group 'bhl-faces)

(defface bhl-tt-face
  '((t (:foreground "darkred")))
  "*The BHL face for truetype font text."
  :group 'bhl-faces)

(defface bhl-underline-face
  '((t (:underline t)))
  "*The BHL face for underlined text."
  :group 'bhl-faces)

(defface bhl-bold-face
  '((t (:weight bold)))
  "*The BHL face for bold text."
  :group 'bhl-faces)

(defface bhl-italic-face
  '((t (:slant italic)))
  "*The BHL face for italic text."
  :group 'bhl-faces)

(defface bhl-special-face
  '((t (:foreground "NavajoWhite1")))
  "*The BHL face for minipage and verbatim environment."
  :group 'bhl-faces)

(defvar bhl-img-face 'bhl-img-face
  "*The BHL face for images.")

(defvar bhl-list-face 'bhl-list-face
  "*The BHL face for list separators.")

(defvar bhl-descrip-face 'bhl-descrip-face
  "*The BHL face for descriptive list separators.")

(defvar bhl-hr-face 'bhl-hr-face
  "*The BHL face for horizontal rule.")

(defvar bhl-toc-face 'bhl-toc-face
  "*The BHL face for the table of contents separators.")

(defvar bhl-title-face 'bhl-title-face
  "*The BHL face for the title.")

(defvar bhl-section-face 'bhl-section-face
  "*The BHL face for the sections.")

(defvar bhl-subsection-face 'bhl-subsection-face
  "*The BHL face for the subsections.")

(defvar bhl-subsubsection-face 'bhl-subsubsection-face
  "*The BHL face pour les subsubsections.")

(defvar bhl-tt-face 'bhl-tt-face
  "*The BHL face for truetype font text.")

(defvar bhl-special-face 'bhl-special-face
  "*The BHL face for minipage and verbatim environment.")

(defvar bhl-underline-face 'bhl-underline-face
  "*The BHL face for underlined text.")

(defvar bhl-italic-face 'bhl-italic-face
  "*The BHL face for italic text.")

(defvar bhl-bold-face 'bhl-bold-face
   "*The BHL face for bold text.")


;;;; 4 - MODE

;; XEmacs compatibility issues
(defalias 'bhl-match-string
  (if (fboundp 'match-string-no-properties)
      'match-string-no-properties
    'match-string))

(defun bhl-replace-regexp-in-string (regexp rep string &optional
					    fixedcase literal subexp start)
  "Replace every occurrence for REGEXP with REP in STRING.

Return a new string containing the replacements.

Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
arguments with the same names of function `replace-match'.  If START
is non-nil, start replacements at that index in STRING.

REP is either a string used as the NEWTEXT arg of `replace-match' or a
function.  If it is a function it is applied to each match to generate
the replacement passed to `replace-match'; the `match-data' at this
point are such that match 0 is the function's argument.

To replace only the first match (if any), make REGEXP match up to \\'
and replace a sub-expression, e.g.
  (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
    => \" bar foo\""
  (let ((l (length string))
	(start (or start 0))
	matches str mb me)
    (save-match-data
      (while (and (< start l) (string-match regexp string start))
	(setq mb (match-beginning 0)
	      me (match-end 0))
	(when (= me mb) (setq me (min l (1+ mb))))
	(string-match regexp (setq str (substring string mb me)))
	(setq matches
	      (cons (replace-match (if (stringp rep)
				       rep
				     (funcall rep (match-string 0 str)))
				   fixedcase literal str subexp)
		    (cons (substring string start mb)
			  matches)))
	(setq start me))
      (setq matches (cons (substring string start l) matches))
      (apply #'concat (nreverse matches)))))

(defsubst bhl-mark-active ()
  "Return non-nil if the mark is active."
  (if (featurep 'xemacs)
      (region-active-p)
    mark-active))

(defsubst bhl-add-to-conversion-log (text)
  "Add TEXT to the conversions list."
  (setq bhl-conversion-log
	(add-to-list 'bhl-conversion-log text)))

(defsubst bhl-string-to-anchor (str)
  "Convert the string STR into an anchor (shorter)."
  (substring str 0 (if (< (length str) 10) (length str) 10)))

(defsubst bhl-cleanup-cell (cell)
  "Clean up a CELL by stripping leading and closing tabs/whitespaces."
  (funcall 'bhl-replace-regexp-in-string "^[ \t]*" ""
	   (funcall 'bhl-replace-regexp-in-string "[ \t]*$" "" cell)))

(defsubst bhl-subst-char-in-string (fromchar tochar string)
  "Convert FROMCHAR to TOCHAR in STRING."
  (let ((cpt 0))
    (while (< cpt (length string))
      (when (equal (aref string cpt) fromchar)
	(aset string cpt tochar))
      (setq cpt (1+ cpt))))
  string)

(defsubst bhl-shrink-window-to-fit ()
  "Shrink the current window to fit."
  (or (< (- (- (window-height)
	       (count-lines (point-min) (point-max))) 2) 0)
      (shrink-window
       (- (- (window-height)
	     (count-lines (point-min) (point-max))) 2))))

(defsubst bhl-indent-line ()
  "Indent line function."
  (interactive)
  (let (column)
    (save-excursion
      (forward-line -1)
      (cond ((looking-at "\t[0-9]")
	     (skip-chars-forward " o\t1234567890.*")
	     (setq column (current-column)))
	    ((looking-at "\t")
	     (skip-chars-forward " o\t*")
	     (setq column (current-column)))
	    (nil (setq column (current-column)))))
    (if column (indent-to column))))

;; Convert TABS into SPACES in the TXT output.
(defsubst bhl-tab-to-spc ()
  "Replace tabs with whitespaces."
  (when (get 'txt 'bhl-tas)
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward "^[ ]*\\(\t\\)" nil t)
	(replace-match "   " t t nil 1)
	(goto-char (point-min))))))

(defun bhl-texi-make-menu ()
  "Make menus in the TEXI output."
  (interactive)
  (goto-char (point-min))
  (while (re-search-forward
	  "^@\\(chapter\\|section\\|subsection\\)" nil t)
    (goto-char (match-beginning 0))
    (save-match-data
      (save-excursion (texinfo-make-menu)))
    (goto-char (match-end 0)))
  (goto-char (point-min)))

(defsubst bhl-texi-toggle-accents ()
  "Toggle accents in the TEXI output."
  (interactive)
  (goto-char (point-min))
  (let ((turn (re-search-forward
	       "\\(@\'\\|@`\\|@^\\)" nil t))
	(accents '(("" "@'e")
		   ("" "@`e")
		   ("" "@`a")
		   ("" "@^u")
		   ("" "@^e")
		   ("" "@^o")
		   ("" "@^{@dotless{i}}")
		   ("" "@^a")
		   ("" "@`u")
		   ("" "@,{c}"))))
    (while (car accents)
      (goto-char (point-min))
      (while (search-forward
	      (if turn (cadr (car accents))
		(car (car accents))) nil t)
	(unless (or (get-text-property (match-beginning 0) 'category)
		    (save-excursion
		      (beginning-of-line)
		      (looking-at "@node")))
	  (replace-match (if turn (car (car accents))
			   (cadr (car accents))))))
      (setq accents (cdr accents)))))

(defsubst bhl2html-set-default-plist ()
  "Set the plist of the `bhl2html' conversion."
  (put 'html 'list-item-is-para bhl-html-list-item-is-para-flag)
  (put 'html 'conversions-list bhl-html-conversions-list)
  (put 'html 'lang (car bhl-i18n-conventions))
  (put 'html 'after-conversion-hook 'bhl-after-html-conversion-hook)
  (put 'html 'bhl-check
       (nth 0 bhl2html-properties-list))
  (put 'html 'bhl-prefix
       (nth 1 bhl2html-properties-list))
  (put 'html 'bhl-caption
       (nth 2 bhl2html-properties-list))
  (put 'html 'bhl-i18n
       (nth 3 bhl2html-properties-list))
  (put 'html 'bhl-toc
       (nth 4 bhl2html-properties-list)))
  
(defsubst bhl2latex-set-default-plist ()
  "Set the plist of the `bhl2latex' conversion."
  (put 'latex 'conversions-list bhl-latex-conversions-list)
  (put 'latex 'lang (car bhl-i18n-conventions))
  (put 'latex 'after-conversion-hook 'bhl-after-latex-conversion-hook)
  (put 'latex 'bhl-check
       (nth 0 bhl2latex-properties-list))
  (put 'latex 'bhl-prefix
       (nth 1 bhl2latex-properties-list))
  (put 'latex 'bhl-caption
       (nth 2 bhl2latex-properties-list))
  (put 'latex 'bhl-i18n
       (nth 3 bhl2latex-properties-list))
  (put 'latex 'bhl-toc
       (nth 4 bhl2latex-properties-list)))
  
(defsubst bhl2sgml-set-default-plist ()
  "Set the plist of the `bhl2sgml' conversion."
  (put 'sgml 'list-item-is-para bhl-sgml-list-item-is-para-flag)
  (put 'sgml 'conversions-list bhl-sgml-conversions-list)
  (put 'sgml 'lang (car bhl-i18n-conventions))
  (put 'sgml 'after-conversion-hook 'bhl-after-sgml-conversion-hook)
  (put 'sgml 'bhl-check
       (nth 0 bhl2sgml-properties-list))
  (put 'sgml 'bhl-caption
       (nth 1 bhl2sgml-properties-list))
  (put 'sgml 'bhl-i18n
       (nth 2 bhl2sgml-properties-list)))

(defsubst bhl2texi-set-default-plist ()
  "Set the plist of the `bhl2texinfo' conversion."
  (put 'texi 'conversions-list bhl-texi-conversions-list)
  (put 'texi 'lang (car bhl-i18n-conventions))
  (put 'texi 'after-conversion-hook 'bhl-after-texi-conversion-hook)
  (put 'texi 'bhl-check
       (nth 0 bhl2texi-properties-list))
  (put 'texi 'bhl-i18n
       (nth 1 bhl2texi-properties-list))
  (put 'texi 'bhl-toc
       (nth 2 bhl2texi-properties-list)))

(defsubst bhl2txt-set-default-plist ()
  "Set the plist of the `bhl2txt' conversion."
  (put 'txt 'conversions-list bhl-txt-conversions-list)
  (put 'txt 'after-conversion-hook 'bhl-after-txt-conversion-hook)
  (put 'txt 'bhl-center
       (car bhl2txt-properties-list))
  (put 'txt 'bhl-toc
       (nth 1 bhl2txt-properties-list))
  (put 'txt 'bhl-tas
       (nth 2 bhl2txt-properties-list)))
  
;; Sectioning style
(defun bhl-set-sectioning-regexp-list ()
  "Set `bhl-sectioning-regexp-list'."
  (setq bhl-sectioning-regexp-list
	(if (eq bhl-sectioning-default-style 'my)
	    bhl-my-sectioning-regexp-list
	  (cadr (assq bhl-sectioning-default-style
		      bhl-sectioning-style-alist))))
  (bhl-set-font-lock-keywords))

(defun bhl-guess-style ()
  "Guess the sectioning style of the current buffer.
Set `bhl-sectioning-default-style' to it."
  (interactive)
  (setq bhl-sectioning-default-style
	(bhl-sectioning-guess-style))
  (bhl-set-sectioning-regexp-list))

(defun bhl-sectioning-guess-style ()
  "Return the sectioning style of the current buffer."
  (let (endstyle)
    (mapcar
     (lambda (style)
       (unless endstyle
	 (when (save-excursion
		 (re-search-forward
		  (concat "^" (caadr (assq style bhl-sectioning-style-alist)))
		  nil t))
	   (setq endstyle style))))
     '(num alpha aster equal-sign my))
    (or endstyle bhl-sectioning-default-style)))

;; Fontlock keywords
(defsubst bhl-set-font-lock-keywords ()
  "Set font-lock keywords.
It is necessary to set keywords each time the style of sectioning is changed."
  (setq bhl-font-lock-keywords
	(list
	 (cons bhl-title-regexp bhl-title-face)
	 (cons bhl-list-regexp bhl-list-face)
	 (cons bhl-img-regexp bhl-img-face)
	 (list bhl-descrip-regexp 0 bhl-descrip-face 'append)
	 (cons bhl-hr-regexp bhl-hr-face)
	 (cons bhl-hr1-regexp bhl-hr-face)
	 (cons bhl-hr2-regexp bhl-hr-face)
	 (cons (concat "^" (nth 0 bhl-sectioning-regexp-list) ".*$")
	       bhl-section-face)
	 (cons (concat "^" (nth 1 bhl-sectioning-regexp-list) ".*$")
	       bhl-subsection-face)
	 (cons (concat "^" (nth 2 bhl-sectioning-regexp-list) ".*$")
	       bhl-subsubsection-face)
	 (list (nth 0 bhl-tag-regexp-list) 1 bhl-underline-face 'append)
	 (list (nth 1 bhl-tag-regexp-list) 1 bhl-tt-face 'append)
	 (list (nth 2 bhl-tag-regexp-list) 1 bhl-italic-face 'append)
	 (list (nth 3 bhl-tag-regexp-list) 1 bhl-bold-face 'append)
	 (cons bhl-minipage-regexp bhl-special-face)
	 (cons "^--$" bhl-special-face)
	 (cons bhl-intro-toc bhl-toc-face)
	 (cons bhl-end-toc bhl-toc-face)
	 'bhl-comment-highlight
	 'bhl-quote-highlight
	 'bhl-url-highlight
	 'bhl-generic-url-highlight))
  (font-lock-mode) ;; ok, this is rather stupid, but I can't find another way...
  (font-lock-mode))

(defsubst bhl-wiki-set-font-lock-keywords ()
  "Set font-lock keywords for BHL as a local wiki.
See also `bhl-is-a-local-wiki-flag'."
  (add-to-list 'bhl-font-lock-keywords
	       'bhl-wiki-names-highlight
	       'bhl-non-wiki-names-unhighlight)
  (font-lock-mode)
  (font-lock-mode))

(defsubst bhl-toggle-wiki ()
  "Toggle `bhl-is-a-local-wiki-flag'."
  (interactive)
  (setq bhl-is-a-local-wiki-flag (not bhl-is-a-local-wiki-flag))
  (if bhl-is-a-local-wiki-flag
      (bhl-wiki-set-font-lock-keywords)
    (progn (setq bhl-font-lock-keywords
		 (delq 'bhl-non-wiki-names-unhighlight
		       (delq 'bhl-wiki-names-highlight
			     bhl-font-lock-keywords)))
	   (font-lock-mode)
	   (font-lock-mode))))

(defsubst bhl-toggle-tags-overlap ()
  "Toggle `bhl-tags-overlap-flag'."
  (interactive)
  (setq bhl-tags-overlap-flag (not bhl-tags-overlap-flag))
  (if bhl-tags-overlap-flag
      (setq bhl-tag-regexp-list bhl-tag-regexp-list0)
    (setq bhl-tag-regexp-list bhl-tag-regexp-list1))
  (if bhl-is-a-local-wiki-flag
      (bhl-wiki-set-font-lock-keywords)
    (bhl-set-font-lock-keywords))
  (bhl-mode))
  
(defun bhl-sectioning-switch-style (&optional style)
  "Switch the current sectioning style to STYLE."
  (interactive)
  (let ((sty (or style (intern (completing-read
				"Switch to style: "
				'(("num" 1)
				  ("alpha" 2)
				  ("aster" 3)
				  ("equal-sign" 4))
				nil t)))))
    (bhl-update-toc sty)
    (setq bhl-sectioning-default-style sty)
    (bhl-set-sectioning-regexp-list)
    (bhl-set-font-lock-keywords)
    (when (eq bhl-is-a-local-wiki-flag t)
	(bhl-wiki-set-font-lock-keywords))
    (message "The style of sectioning has been set to \"%s\"."
	     (symbol-name sty))))

(defsubst bhl-initialize-properties ()
  "Initialize the properties of conversions."
  (bhl2html-set-default-plist)
  (bhl2latex-set-default-plist)
  (bhl2sgml-set-default-plist)
  (bhl2txt-set-default-plist)
  (bhl2texi-set-default-plist))

(easy-menu-define bhl-menu bhl-mode-map
  "Menu of the BHL mode"
  '("BHL"
    ;; the main conversion commands
    ("bhl2html"
     ["Convert into HTML" bhl2html]
     "---"
     ["Check dubious tags" (bhl-toggle-property 'bhl-check 'html)
      :style toggle :selected (get 'html 'bhl-check)]
     ["Prefix sections" (bhl-toggle-property 'bhl-prefix 'html)
      :style toggle :selected (get 'html 'bhl-prefix)]
     ["Use i18n conventions" (bhl-toggle-property 'bhl-i18n 'html)
      :style toggle :selected (get 'html 'bhl-i18n)]
     ["Ask tables caption" (bhl-toggle-property 'bhl-caption 'html)
      :style toggle :selected (get 'html 'bhl-caption)]
     ["Convert toc" (bhl-toggle-property 'bhl-toc 'html)
      :style toggle :selected (get 'html 'bhl-toc)])
    ("bhl2latex"
     ["Convert into LaTeX" bhl2latex]
     "---"
     ["Check dubious tags" (bhl-toggle-property 'bhl-check 'latex)
      :style toggle :selected (get 'latex 'bhl-check)]
     ["Prefix sections" (bhl-toggle-property 'bhl-prefix 'latex)
      :style toggle :selected (get 'latex 'bhl-prefix)]
     ["Use i18n conventions" (bhl-toggle-property 'bhl-i18n 'latex)
      :style toggle :selected (get 'latex 'bhl-i18n)]
     ["Ask tables caption" (bhl-toggle-property 'bhl-caption 'latex)
      :style toggle :selected (get 'latex 'bhl-caption)]
     ["Convert toc" (bhl-toggle-property 'bhl-toc 'latex)
      :style toggle :selected (get 'latex 'bhl-toc)])
    ("bhl2texinfo"
     ["Convert into Texinfo" bhl2texinfo]
     "---"
     ["Check dubious tags" (bhl-toggle-property 'bhl-check 'texi)
      :style toggle :selected (get 'texi 'bhl-check)]
     ["Use i18n conventions" (bhl-toggle-property 'bhl-i18n 'texi)
      :style toggle :selected (get 'texi 'bhl-i18n)]
     ["Convert toc" (bhl-toggle-property 'bhl-toc 'texi)
      :style toggle :selected (get 'texi 'bhl-toc)])
    ("bhl2sgml"
     ["Convert into SGML" bhl2sgml]
     "---"
     ["Check dubious tags" (bhl-toggle-property 'bhl-check 'sgml)
      :style toggle :selected (get 'sgml 'bhl-check)]
     ["Use i18n conventions" (bhl-toggle-property 'bhl-i18n 'sgml)
      :style toggle :selected (get 'sgml 'bhl-i18n)]
     ["Ask tables caption" (bhl-toggle-property 'bhl-caption 'sgml)
      :style toggle :selected (get 'sgml 'bhl-caption)])
    ("bhl2txt"
     ["Convert into TXT" bhl2txt]
     "---"
     ["Center sections" (bhl-toggle-property 'bhl-center 'txt)
      :style toggle :selected (get 'txt 'bhl-center)]
     ["Tabs are spaces" (bhl-toggle-property 'bhl-tas 'txt)
      :style toggle :selected (get 'txt 'bhl-tas)]
     ["Convert toc" (bhl-toggle-property 'bhl-toc 'txt)
      :style toggle :selected (get 'txt 'bhl-toc)])
    "---"
    ;; insert commands
    ("Insert"
     ["Insert toc" bhl-insert-toc t]
     ["Insert lol" bhl-insert-lol t]
     ["Insert URL" bhl-insert-url t]
     ["Insert image" bhl-insert-image t]
     ["Insert minipage" bhl-insert-minipage t]
     ["Insert verbatim" bhl-insert-verbatim t]
     ["Insert hr" bhl-insert-hr t])
    ;; changing font commands
    ("Change font"
     ["Bold" bhl-change-font-bold t]
     ["Emphasis" bhl-change-font-emphasis t]
     ["Underline" bhl-change-font-underline t]
     ["TrueType" bhl-change-font-truetype t]
     ["Bold italic" bhl-change-font-bolditalic t]
     "---"
     ["Normal" bhl-change-font-normal t])
    ;; browse the source
    ("Go to"
     ["Next section" bhl-goto-next-section t]
     ["Previous section" bhl-goto-previous-section t]
     ["Next URL or WikiName" bhl-goto-next-url-or-wiki t])
    "---"
    ;; handling the table of contents
    ["Browse the lol" bhl-show-lol t]
    ["Browse the toc" bhl-show-toc t]
    ["Update toc" bhl-update-toc t]
    ("Toc location"
     ["Toc at the top" (setq bhl-toc-location "top")
      :style radio :selected (equal bhl-toc-location "top")]
     ["Toc on the point" (setq bhl-toc-location "point")
      :style radio :selected (equal bhl-toc-location "point")]
     ["Toc at the bottom" (setq bhl-toc-location "bottom")
      :style radio :selected (equal bhl-toc-location "bottom")])
    ("Toc depth"
     ["Sections only" (setq bhl-default-toc-depth 1)
      :style radio :selected (equal bhl-default-toc-depth 1)]
     ["Sections and subsections" (setq bhl-default-toc-depth 2)
      :style radio :selected (equal bhl-default-toc-depth 2)]
     ["Maximum depth [3]" (setq bhl-default-toc-depth 3)
      :style radio :selected (equal bhl-default-toc-depth 3)])
    ;; miscellaneous
    "---"
    ["BHL Wiki" bhl-toggle-wiki
     :style radio :selected (eq bhl-is-a-local-wiki-flag t)]
    ["Tags overlap" bhl-toggle-tags-overlap
     :style radio :selected (eq bhl-tags-overlap-flag t)]
    ("Sectioning style"
     ["Guess" bhl-guess-style t]
     "---"
     ["Numerical" (bhl-sectioning-switch-style 'num)
      :style radio :selected (eq bhl-sectioning-default-style 'num)]
     ["Alphabetical" (bhl-sectioning-switch-style 'alpha)
      :style radio :selected (eq bhl-sectioning-default-style 'alpha)]
     ["Asterisks" (bhl-sectioning-switch-style 'aster)
      :style radio :selected (eq bhl-sectioning-default-style 'aster)]
     ["Equal signs" (bhl-sectioning-switch-style 'equal-sign)
      :style radio :selected (eq bhl-sectioning-default-style 'equal-sign)]
     ["My style" (progn (setq bhl-sectioning-default-style 'my)
			(bhl-set-sectioning-regexp-list)
			(bhl-set-font-lock-keywords)
			(when (eq bhl-is-a-local-wiki-flag t)
			  (bhl-wiki-set-font-lock-keywords))
			(message "The style of sectioning has been set to \"my\"."))
      :style radio :selected (eq bhl-sectioning-default-style 'my)])
    ("I18n conventions"
     ["English" (setcar bhl-i18n-conventions "en")
      :style radio :selected (equal (car bhl-i18n-conventions) "en")]
     ["French" (setcar bhl-i18n-conventions "fr")
      :style radio :selected (equal (car bhl-i18n-conventions) "fr")]
     ["German" (setcar bhl-i18n-conventions "de")
      :style radio :selected (equal (car bhl-i18n-conventions) "de")])
    "---"
    ["Mail buffer" bhl-compose-mail]
    "---"
    ;; miscellaneous (again)
    ["View last conversion log" bhl-view-log t]
    ["Submit bug report" bhl-submit-bug-report t]
    ["Customize BHL" (customize-group "bhl") t]
    ["BHL info manual" (info "bhl.info")]
    ["BHL version" bhl-show-version t]))

;;;###autoload
(define-derived-mode bhl-mode text-mode "BHL"
  "B - Brute, H - HTML, L - LaTeX.

The BHL mode enables you to convert plain text files into
HTML, LaTeX and SGML files.  This is a simple mode, with simple
conversion functions and converts only simple source files.
\\<bhl-mode-map>
+ Sections:
===========

1.<SPC>     : insert a section.
1.1.<SPC>   : insert a subsection.
1.1.1.<SPC> : insert a subsubsection.

A.<SPC>     : insert a section.
A.A.<SPC>   : insert a subsection.
A.A.A.<SPC> : insert a subsubsection.

+ Font change:
==============

*a word*   : bold
_a word_   : emphasis
_*a word*_ : bold-italic
__a word__ : underline
==a word== : truetype

--
This part of text will be inserted as verbatim text.
--

%%[2cm]
This part of text will be put inside a minipage
which width is 2cm (size is optional).
%%

+ Key bindings:
===============

\\[bhl2html] : convert the buffer into HTML.
\\[bhl2sgml] : convert the buffer into SGML (Linuxdoc).
\\[bhl2latex] : convert the buffer into LaTeX.
\\[bhl2texinfo] : convert the buffer into TEXI.
\\[bhl2txt] : convert the buffer into TXT.

\\[bhl-insert-url] : ask for an URL and its name, then insert it.
\\[bhl-insert-image] : ask for an image file name, then insert it.
\\[bhl-insert-hr] : insert a horizontal rule.
\\[bhl-insert-minipage] : insert a minipage.
\\[bhl-insert-verbatim] : insert a verbatim environment.
\\[bhl-insert-toc] : insert the table of contents.
   Take the toc depth as an optional argument.
\\[bhl-insert-tab] : insert a tab rigidly.

\\[bhl-comment-region] : comment the region
\\[universal-argument] \\[bhl-comment-region] : uncomment the region.

\\[bhl-view-log] : view the log of the last conversion.
\\[bhl-compose-mail] : compose mail with the result of bhl2txt on the current buffer.

\\[bhl-show-toc] : browse the table of contents.
\\[bhl-update-toc] : update the sections' prefix.
\\[bhl-show-lol] : browse the list of links.

\\[bhl-goto-next-section] : go to the next section.
\\[universal-argument] \\[bhl-goto-next-section] : go to the next section or (sub)subsection.
\\[bhl-goto-previous-section] : go to the previous section.
\\[universal-argument] \\[bhl-goto-previous-section] : go to the previous section or (sub)subsection.
\\[bhl-goto-next-url-or-wiki] : go to the next URL or WikiName.

\\[bhl-submit-bug-report] : submit a bug report.
\\[bhl-show-version] : display the current version of BHL.

+ List separators:
==================

TAB(S) and \"*\" or \"-\" or \"o\" and a whitespace: a non-ordered list item.
TAB(S) and 0-9 and dot and a whitespace: an ordered list item.
TAB and a definition term and \"--\" and a whitespace: a definition list item.

+ List examples:
================

   * Here is
      - a simple
      - nested list
         o with three levels
         o of nested items.
      1. Here is...
      2. ...an ordered list
   * inside another list.

   A term -- and its definition.
   Another term -- and its definition.

+ bhl-mode-map:
===============

\\{bhl-mode-map}"
  (kill-all-local-variables)
  (use-local-map bhl-mode-map)
  (easy-menu-add bhl-menu)
  (make-local-variable 'font-lock-defaults)
  (make-local-variable 'indent-line-function)
  (setq	bhl-tag-regexp-list (if bhl-tags-overlap-flag
				bhl-tag-regexp-list0
			      bhl-tag-regexp-list1))
  (when bhl-autoguess-style-flag
    (bhl-guess-style))
  (bhl-set-sectioning-regexp-list)
  (when (eq bhl-is-a-local-wiki-flag t)
    (bhl-wiki-set-font-lock-keywords))
  (bhl-initialize-properties)
  (setq indent-line-function 'bhl-indent-line
	tab-width bhl-tab-width
	font-lock-defaults '(bhl-font-lock-keywords t)
	font-lock-multiline t
	mode-name "BHL"
	major-mode 'bhl-mode)
  (mapcar 'run-hooks bhl-mode-hook))

;; Conversion commandse
(define-key bhl-mode-map [(control c) (control w)] 'bhl2html)
(define-key bhl-mode-map [(control c) (control l)] 'bhl2latex)
(define-key bhl-mode-map [(control c) (control s)] 'bhl2sgml)
(define-key bhl-mode-map [(control c) (control d)] 'bhl2txt)
(define-key bhl-mode-map [(control c) (control o)] 'bhl2texinfo)

;; Toc/lol commands
(define-key bhl-mode-map [(control c) (control t)] 'bhl-show-toc)
(define-key bhl-mode-map [(control c) (meta t)] 'bhl-update-toc)
(define-key bhl-mode-map [(control c) (meta s)] 'bhl-guess-style)
(define-key bhl-mode-map [(control c) (control /)] 'bhl-show-lol)

;; Insert
(define-key bhl-mode-map [(control c) (control c) ?t] 'bhl-insert-toc)
(define-key bhl-mode-map [(control c) (control c) ?l] 'bhl-insert-lol)
(define-key bhl-mode-map [(control c) (control c) ?h] 'bhl-insert-url)
(define-key bhl-mode-map [(control c) (control c) ?i] 'bhl-insert-image)
(define-key bhl-mode-map [(control c) (control c) ?m] 'bhl-insert-minipage)
(define-key bhl-mode-map [(control c) (control c) ?v] 'bhl-insert-verbatim)
(define-key bhl-mode-map [(control c) (control c) ?r] 'bhl-insert-hr)
(define-key bhl-mode-map [(meta tab)] 'bhl-insert-tab)

;; Font commands
(define-key bhl-mode-map
  [(control c) ?\;] 'bhl-comment-region)
(define-key bhl-mode-map
  [(control c) (control f) (control b)] 'bhl-change-font-bold)
(define-key bhl-mode-map
  [(control c) (control f) (control e)] 'bhl-change-font-emphasis)
(define-key bhl-mode-map
  [(control c) (control f) (control n)] 'bhl-change-font-normal)
(define-key bhl-mode-map
  [(control c) (control f) (control u)] 'bhl-change-font-underline)
(define-key bhl-mode-map
  [(control c) (control f) (control t)] 'bhl-change-font-truetype)
(define-key bhl-mode-map
  [(control c) (control f) (control _)] 'bhl-change-font-bolditalic)

;; Move inside your document
(define-key bhl-mode-map [?\r] 'newline-and-indent)
(define-key bhl-mode-map [(control c) (control n)] 'bhl-goto-next-section)
(define-key bhl-mode-map [(control c) (control p)] 'bhl-goto-previous-section)
(define-key bhl-mode-map [(control tab)] 'bhl-goto-next-url-or-wiki)

;; Other
(define-key bhl-mode-map [(control down-mouse-3)] 'bhl-popup-menu)
(define-key bhl-mode-map [(control c) (control v)] 'bhl-show-version)
(define-key bhl-mode-map [(control c) (control x) ?m] 'bhl-compose-mail)

;; M-x bhl = M-x bhl-mode
(defalias 'bhl 'bhl-mode)


;;;; 5 - MENU

;;Pop up menu
(defsubst bhl-popup-menu ()
  "Pop up a menu."
  (interactive)
  (eval (x-popup-menu t bhl-popup-menu-map)))

(define-key bhl-popup-menu-map [bhl-compose-mail]
  '("Mail buffer" . bhl-compose-mail))

(define-key bhl-popup-menu-map [bhl-show-lol]
  '("Show lol" . bhl-show-lol))

(define-key bhl-popup-menu-map [bhl-show-toc]
  '("Show toc" . bhl-show-toc))

(define-key bhl-popup-menu-map [bhl2txt]
  '("bhl2txt" . bhl2txt))

(define-key bhl-popup-menu-map [bhl2sgml]
  '("bhl2sgml" . bhl2html))

(define-key bhl-popup-menu-map [bhl2texinfo]
  '("bhl2texinfo" . bhl2texinfo))

(define-key bhl-popup-menu-map [bhl2latex]
  '("bhl2latex" . bhl2latex))

(define-key bhl-popup-menu-map [bhl2html]
  '("bhl2html" . bhl2html))


;;;; 6 - OTHER FUNCTIONS

;; Ignore some lines
(defun bhl-ignore-regexp-line ()
  "Ignore some lines matching regexp."
  (goto-char (point-min))
  (mapcar (lambda (regexp)
	    (save-excursion
	      (let ((kill-whole-line t))
		(while (re-search-forward regexp nil t)
		  (beginning-of-line)
		  (kill-line)))))
	  bhl-ignored-regexps))

;;Font handling:
(defun bhl-change-font (tagbeg tagend)
  "Insert TAGBEG and TAGEND at the beginning/end of a region."
  (let* ((beg (if (bhl-mark-active) (mark) (point)))
	 (end (point))
	 (temp beg))
    (when (< end beg)
      (setq beg end end temp))
    (goto-char beg)
    (while
	(memq t (mapcar (lambda (char)
			  (equal (char-before (point))
				 char))
			'(?* ?= ?_)))
      (delete-char -1)
      (setq end (1- end)))
    (insert tagbeg)
    (goto-char (+ (length tagbeg) end))
    (while
	(memq t (mapcar (lambda (char)
			  (equal (char-after (point))
				 char))
			'(?* ?= ?_)))
      (delete-char 1))
    (save-excursion (insert tagend))
    (setq mark-active nil)))

(defun bhl-change-font-bold ()
  "Change font to bold."
  (interactive)
  (bhl-change-font "*" "*"))

(defun bhl-change-font-emphasis ()
  "Change font to emphasis."
  (interactive)
  (bhl-change-font "_" "_"))

(defun bhl-change-font-truetype ()
  "Change font to truetype."
  (interactive)
  (bhl-change-font "==" "=="))

(defun bhl-change-font-underline ()
  "Change font to underline."
  (interactive)
  (bhl-change-font "__" "__"))

(defun bhl-change-font-bolditalic ()
  "Change font to bold-italic."
  (interactive)
  (bhl-change-font "_*" "*_"))

(defun bhl-change-font-normal ()
  "Strip any font beautifier."
  (interactive)
  (bhl-change-font "" ""))

;; Sectioning

;; Move inside the BHL source
(defun bhl-goto-next-section (&optional arg)
  "Go to the title of the next section.
If ARG is non-nil, go to the title of the next section or (sub)subsection."
  (interactive "P")
  (let ((reg (if arg (concat "^" (nth 0 bhl-sectioning-regexp-list) ".*$"
			     "\\|^" (nth 1 bhl-sectioning-regexp-list) ".*$"
			     "\\|^" (nth 2 bhl-sectioning-regexp-list) ".*$")
	       (concat "^" (nth 0 bhl-sectioning-regexp-list) ".*$"))))
    (re-search-forward reg nil t)))

(defun bhl-goto-previous-section (&optional arg)
  "Go to the title of the previous section.
If ARG is non-nil, go to the title of the previous section or (sub)subsection."
  (interactive "P")
  (let ((reg (if arg (concat "^" (nth 0 bhl-sectioning-regexp-list) ".*$"
			     "\\|^" (nth 1 bhl-sectioning-regexp-list) ".*$"
			     "\\|^" (nth 2 bhl-sectioning-regexp-list) ".*$")
	       (concat "^" (nth 0 bhl-sectioning-regexp-list) ".*$"))))
    (re-search-backward reg nil t)))

;; Follow URLs and Wiki links
(defun bhl-goto-next-url-or-wiki ()
  "Go to the next URL or WikiName."
  (interactive)
  (let ((case-fold-search)
	(reg (concat bhl-url-regexp "\\|"
		     bhl-wiki-names-regexp)))
    (if (looking-at reg)
	(forward-char))
    (or (re-search-forward reg nil t)
	(progn (goto-char (point-min))
	       (re-search-forward reg nil t))))
  (goto-char (match-beginning 0)))

(defun bhl-url-at-point ()
  "Return non-nil if a URL is at point."
  (save-excursion
    (skip-chars-backward "^'\"<>[{}( \t\n")
    (looking-at bhl-generic-url-regexp)))

(defun bhl-visit-url (link)
  "Visit the URL named LINK."
  (funcall bhl-browse-url-function link))

(defun bhl-follow-url-at-point ()
  "Visit the link at point."
  (interactive)
  (when (bhl-url-at-point)
    (cond ((equal (substring (match-string 0) 0 7) "mailto:")
	   (compose-mail (substring (match-string 0) 7)))
	  (t (bhl-visit-url (or (match-string 5) (match-string 0)))))))

(defun bhl-follow-url-at-mouse (event)
  "According to EVENT, visit the link at point."
  (interactive "e")
  (save-excursion
    (when (fboundp 'posn-window)
      (set-buffer (window-buffer (posn-window (event-start event))))
      (goto-char (posn-point (event-start event))))
    (when (bhl-url-at-point)
      (cond ((equal (substring (match-string 0) 0 7) "mailto:")
	     (compose-mail (substring (match-string 0) 7)))
	    (t (bhl-visit-url (or (match-string 5) (match-string 0))))))))

;; Follow WikiNames
(defun bhl-wiki-name-at-point ()
  "Return non-nil if a URL is at point."
  (save-excursion
    (let (case-fold-search)
      (skip-chars-backward "^'\"<>[{}( \t\n")
      (looking-at bhl-wiki-names-regexp))))

(defun bhl-follow-wiki-name-at-point ()
  "Visit the WikiName at point."
  (interactive)
  (when (bhl-wiki-name-at-point)
    (find-file (concat (if bhl-downcase-wikifiles-names-flag
			   (downcase (match-string 0))
			 (match-string 0))
		       bhl-default-wikifiles-extension))
    (delete-other-windows)))

(defun bhl-follow-wiki-name-at-mouse (event)
  "Visit the WikiName at EVENT."
  (interactive "e")
  (save-excursion
    (when (fboundp 'posn-window)
      (set-buffer (window-buffer (posn-window (event-start event))))
      (goto-char (posn-point (event-start event))))
    (when (bhl-wiki-name-at-point)
      (find-file (concat (if bhl-downcase-wikifiles-names-flag
			     (downcase (match-string 0))
			   (match-string 0))
			 bhl-default-wikifiles-extension)))))

(defsubst bhl-wiki-names-highlight (limit)
  "Highlight WikiNames in the BHL buffer limited to LIMIT."
  (let ((map (copy-keymap (current-local-map)))
	case-fold-search)
    (define-key map [(mouse-2)] 'bhl-follow-wiki-name-at-mouse)
    (define-key map [(return)] 'bhl-follow-wiki-name-at-point)
    (while (re-search-forward bhl-wiki-names-regexp limit t)
      (or (member (match-string 0) bhl-non-wiki-names-list)
	  (add-text-properties (match-beginning 0) (match-end 0)
			       `(face bhl-wiki-face
				      rear-nonsticky t
				      local-map ,map mouse-face highlight help-echo
				      "mouse-2: follow WikiName"))))))

(defsubst bhl-non-wiki-names-unhighlight (limit)
  "Unhighlight non-wiki-names found in `bhl-non-wiki-names-list'.
Search is bounded to LIMIT."
  (let (case-fold-search)
    (while (re-search-forward bhl-wiki-names-regexp limit t)
      (when (member (match-string 0) bhl-non-wiki-names-list)
	(remove-text-properties (match-beginning 0) (match-end 0)
				`(face rear-nonsticky local-map
				       mouse-face help-echo))))))

;; Toggle properties
(defun bhl-toggle-property (prop symb)
  "Toggle the property PROP in SYMB."
  (put symb prop (not (get symb prop))))

;; Show BHL version
(defun bhl-show-version ()
  "Display version infos about BHL."
  (interactive)
  (message "BHL mode %s, Copyright (C) 2003 Bastien Guerry; type C-h m for help."
	   bhl-version)
  (if (sit-for 3)
      (let ((lines bhl-startup-message-lines))
	(while (and (sit-for 3) lines)
	  (message (substitute-command-keys (car lines)))
	  (setq lines (cdr lines)))))
  (message ""))

;; Don't INDENT paragraphs. Restrict indentation to lists.
(defsubst bhl-insert-tab ()
  "Tabulation for the BHL mode.
Use this function to indent list items.
Don't indent paragraphs."
  (interactive)
  (indent-to (+ (current-column) tab-width)))

(defun bhl-compose-mail ()
  "Mail the content of the BHL buffer in plain text."
  (interactive)
  (bhl2txt)
  (save-buffer)
  (let ((file (buffer-file-name)))
    (compose-mail)
    (save-excursion
      (goto-char (point-max))
      (setq tab-width 3)
      (insert-file-contents file))))

;; Check for dubious comments
(defun bhl-check-comment (arg)
  "Check for dubious comment strings in the ARG output."
  (let ((regexp (cond ((eq arg 'latex) "%+")
		      ((eq arg 'texi) "@c[ \t\n]")
		      (t "<!--\\|-->"))))
    (goto-char (point-min))
    (while (re-search-forward regexp nil t)
      (bhl-add-to-conversion-log
       (format "Dubious comment \"%s\" at point %s."
	       (match-string 0)
	       (match-beginning 0))))))

;; Check for dubious tags (in verbatim environments)
(defun bhl-check-existing-tags (arg)
  "Check if there is no tag of the ARG format in the source file.
Especially need for verbatim environments."
  (save-excursion
    (goto-char (point-min))
    (let ((reg (cond
		((eq arg 'latex) "\\\\\\(\\w+\\){\\(\\w+\\)}")
		((eq arg 'texi) "@\\(\\w+\\){\\(\\w+\\)}")
		(t "<\\(/?[^>\t\n]+\\)>")))
	  (rep (cond ((eq arg 'latex) "\$\\\\backslash\$\\1\\\\{\\2\\\\}")
		     ((eq arg 'texi) "@@\\1@{\\2@}")
		     (t "&lsaquo;\\1&rsaquo;"))))
      (query-replace-regexp reg rep))))

;; Highlighting functions
(defsubst bhl-comment-highlight (limit)
  "Highlight comment strings limited to LIMIT."
  (while (re-search-forward "^#\\(.*\\)$" limit t)
    (add-text-properties (match-beginning 0) (match-end 0)
			 '(face bhl-comment-face
				category comment)))
  (while (re-search-forward "[^\\]\\(#.*\\)$" limit t)
    (add-text-properties (match-beginning 1) (match-end 1)
			 '(face bhl-comment-face
				category comment))))

(defsubst bhl-quote-highlight (limit)
  "Highlight quote strings limited to LIMIT."
  (while (re-search-forward "^\t.*$" limit t)
    (unless (string-match " --\\|\t+\*\\|\t+[0-9]+\. \\|\t+- \\|\t+o " (match-string 0))
      (add-text-properties (match-beginning 0) (match-end 0)
			   '(category quote)))))

(defsubst bhl-url-highlight (limit)
  "Highlight URLs in the BHL buffer limited to LIMIT."
  (while (re-search-forward bhl-url-regexp limit t)
    (add-text-properties (match-beginning 0) (match-end 0)
			 '(face bhl-url-face category link rear-nonsticky t))))

(defsubst bhl-generic-url-highlight (limit)
  "Highlight URLs in the BHL buffer limited to LIMIT."
  (let ((map (copy-keymap (current-local-map))))
    (define-key map [(mouse-2)] 'bhl-follow-url-at-mouse)
    (define-key map [(return)] 'bhl-follow-url-at-point)
    (while (re-search-forward bhl-generic-url-regexp limit t)
      (add-text-properties (match-beginning 0) (match-end 0)
			   `(category link rear-nonsticky t
				      local-map ,map mouse-face
				      highlight help-echo
				      "mouse-2: follow URL")))))

(defsubst bhl-highlight-toc-buffer ()
  "Highlight the *toc* buffer."
  (save-excursion
    (goto-char (point-min))
    (re-search-forward bhl-intro-toc nil t)
    (add-text-properties (match-beginning 0)
			 (match-end 0)
			 '(face bold))
    (while (re-search-forward
	    (concat "^[ ]*\\("
		    (nth 0 bhl-sectioning-regexp-list) "\\|"
		    (nth 1 bhl-sectioning-regexp-list) "\\|"
		    (nth 2 bhl-sectioning-regexp-list)
		    "\\)\\(.*\\)$") nil t)
      (add-text-properties
       (match-beginning 2) (match-end 2)
       '(mouse-face highlight help-echo
		    "mouse-2: go to this section")))))

;; Convert quote chars in HTML and SGML
(defun bhl-convert-quote-char ()
  "Convert the quote character in HTML and SGML."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "<\\|>" nil t)
      (if (equal (match-string 0) "<")
	  (replace-match "&lsaquo;")
	(replace-match "&rsaquo;")))))

;; Handle footnotes
(defun bhl-convert-footnotes (arg)
  "Convert footnotes into ARG format."
  (let ((ftn-regexp
	 (concat (regexp-quote footnote-start-tag)
		 (eval (intern
			(concat "footnote-"
				(symbol-name footnote-style)
				"-regexp")))
		 (regexp-quote footnote-end-tag))))
    (goto-char (point-min))
    (when (and (re-search-forward "^Footnotes.*$" nil t)
	       (eq arg 'html))
      (replace-match (concat "<p><b>" (match-string 0) "</b>") t t))
    (while (re-search-forward (concat "\\(" ftn-regexp "\\)[ ]*") nil t)
      (let* ((num (match-string 1)))
	(when (eq arg 'html)
	  (replace-match (concat "<p><a name=\"" num
				 "\" href=\"#ftn" num "\">"
				 num "</a>&nbsp;&nbsp;")))
	(funcall 'bhl-replace-footnote num arg
		 (bhl-subst-char-in-string
		  ?\n ?  (buffer-substring-no-properties
			  (point)
			  (progn (re-search-forward "\\[\\|^$" nil t)
				 (goto-char (1- (match-beginning 0)))))))))
    (when (or (eq arg 'latex) (eq arg 'texi))
      (bhl-skip-footnotes))
    (bhl-add-to-conversion-log "Footnotes conversion.")))

(defun bhl-skip-footnotes (&optional arg)
  "Skip footnotes according to ARG format."
  (save-excursion
    (goto-char (point-min))
    (when arg
      (while (re-search-forward
	      (concat (regexp-quote footnote-start-tag)
		      (eval (intern
			     (concat "footnote-"
				     (symbol-name footnote-style)
				     "-regexp")))
		      (regexp-quote footnote-end-tag)) nil t)
	(replace-match "")))
    (goto-char (point-min))
    (when (re-search-forward "^Footnotes.*$" nil t)
      (kill-region (match-beginning 0) (point-max)))))

(defun bhl-replace-footnote (num arg content)
  "Replace the footnote number NUM into ARG format with CONTENT."
  (save-excursion
    (goto-char (point-min))
    (re-search-forward (regexp-quote num) nil t)
    (unless (let ((cat (get-text-property (match-beginning 0) 'category)))
	      (or (and (eq cat 'verbatim)
		       (memq 'footnote bhl-verbatim-ignore))
		  (eq cat 'link)
		  (eq cat 'comment)))
      (cond ((eq arg 'latex)
	     (let ((section
		    (save-match-data
		      (save-excursion
			(beginning-of-line)
			(looking-at (mapconcat
				     (lambda (str) str)
				     bhl-sectioning-regexp-list "\\|"))))))
	       (replace-match (concat (if section "\\protect" "")
				      "\\footnote{" content "}") t t)))
	    ((eq arg 'texi)
	     (replace-match (concat "@footnote{" content "}") t t))
	    ((eq arg 'html)
	     (replace-match
	      (concat "<sup><a name=\"ftn" (match-string 0)
		      "\" href=\"#" (match-string 0) "\">"
		      (match-string 0) "</a></sup>")))))))

(defun bhl-convert-quote (arg)
  "Convert quote environment into ARG format."
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "^\t.*$" nil t)
      (let ((result (bhl-match-string 0))
	    (loc (match-beginning 0))
	    (intro-tag (cond ((eq arg 'latex) "\n\\begin{quote}")
			     ((eq arg 'html) "\n<blockquote>")
			     ((eq arg 'texi) "\n@quotation")
			     ((eq arg 'sgml) "\n<quote>")))
	    (end-tag (cond ((eq arg 'latex) "\\end{quote}\n")
			   ((eq arg 'html) "</blockquote>\n")
			   ((eq arg 'texi) "@end quotation\n")
			   ((eq arg 'sgml) "</quote>\n"))))
	(if (not (or (string-match " --\\|\t+\\* \\|\t+[0-9]+\. \\|\t+- \\|\t+o " result)
		     (and (memq 'quote bhl-verbatim-ignore)
			  (get-text-property loc 'category) 'verbatim)))
	    (progn (forward-line -1)
		   (end-of-line)
		   (insert intro-tag)
		   (re-search-forward "^$\\|^--$\\|^[<>%][-%]%+.*$" nil t)
		   (beginning-of-line)
		   (insert end-tag))
	  (re-search-forward "^$" nil t))))))

;; convert verbatim, minipages and horizontal rules
(defun bhl-convert-verbatim (arg)
  "Convert verbatim environment into ARG format."
  (goto-char (point-min))
  (while (re-search-forward "^--$" nil t)
    (bhl-convert-special arg
			 '("" "<pre>" "<p><verb>" "\\begin{verbatim}" "@verbatim")
			 '("" "</pre>" "</verb>" "\\end{verbatim}" "@end verbatim")
			 "--" "--")))

(defun bhl-convert-minipage (arg)
  "Convert minipage into ARG format."
  (goto-char (point-min))
  (while (re-search-forward bhl-minipage-regexp nil t)
    (let ((border (if (equal (match-string 2) "-")
		      (cond ((eq arg 'texi) "@cartouche\n")
			    ((eq arg 'html) " border: 1pt solid\;")
			    ((eq arg 'latex) "\\fbox{")
			    (t "")) ""))
	  (end-border (cond ((and (eq arg 'latex)
				  (equal (match-string 2) "-")) "}")
			    ((and (eq arg 'texi)
				  (equal (match-string 2) "-"))
			     "\n@end cartouche")))
	  (bhl-intro-minipage (regexp-quote (match-string 0)))
	  justify width intro-minipage end-minipage)
      (cond ((eq arg 'latex)
	     (cond ((equal (match-string 1) "<")
		    (setq justify "flushleft"))
		   ((equal (match-string 1) ">")
		    (setq justify "flushright"))
		   (t (setq justify "center")))
	     (if (match-string 4) (setq width (match-string 4))
	       (setq width "\\textwidth")))
	    ((eq arg 'html)
	     (cond ((equal (match-string 1) "<")
		    (setq justify "left"))
		   ((equal (match-string 1) ">")
		    (setq justify "right"))
		   (t (setq justify "center")))
	     (if (match-string 4) (setq width (match-string 4))
	       (setq width "100%")))
	    ((eq arg 'texi)
	     (cond ((equal (match-string 1) "<")
		    (setq justify "flushleft"))
		   ((equal (match-string 1) ">")
		    (setq justify "flushright"))
		   (t (setq justify bhl-texi-centered-minipage-tag)))
	     (if (match-string 4) (setq width (match-string 4))
	       (setq width "100%"))))
      (setq intro-minipage
	    `("" ,(concat "<div align=\"" justify "\">\n"
			  "<div style=\"padding: 2mm; width: "
			  width ";" border "\" align=\"justify\">")
	      ,(concat "<p>" bhl-sgml-minipage-tag)
	      ,(concat
		"\\vspace{.3cm}\n\\begin{" justify
		"}\n" border
		"\\begin{minipage}[c]{" width "}")
	      ,(concat border "@" justify)))
      (setq end-minipage `("" "</div>\n</div>" "</code>"
			   ,(concat "\\end{minipage}" end-border
				    "\n\\end{" justify "}\n\\vspace{.3cm}")
			   ,(concat "@end " justify end-border)))
      (bhl-convert-special arg intro-minipage
			   end-minipage bhl-intro-minipage "^%%+"))))

(defun bhl-convert-hr (arg)
  "Convert a raw of dashes into an horizontal rule.
ARG is the output format."
  (goto-char (point-min))
  (while (re-search-forward bhl-hr-regexp nil t)
    (let (justify width)
      (cond ((equal (match-string 1) "<")
	     (setq justify "left"))
	    ((equal (match-string 1) ">")
	     (setq justify "right"))
	    (t (setq justify "center")))
      (cond ((eq arg 'txt)
	     (if (match-string 4)
		 (setq width (string-to-number (match-string 4)))
	       (setq width (or fill-column 70)))
	     (let (replacmt)
	       (if (equal justify "right")
		   (setq replacmt (concat (make-string (- (or fill-column 70) width) 32)
					  (make-string width ?-)))
		 (setq replacmt (make-string (or width fill-column 70) ?-)))
	       (replace-match replacmt)
	       (when (equal justify "center")
		 (center-line))))
	    ((eq arg 'latex)
	     (setq width (or (match-string 3) "\\\\textwidth"))
	     (let (flush)
	       (setq flush (if (equal justify "center") "" "flush"))
	       (replace-match
		(concat "\\\\begin{" flush justify "}\n\\\\rule{"
			width "}{1pt}\n\\\\end{" flush justify"}"))))
	    ((eq arg 'html)
	     (setq width (or (match-string 3) "100%"))
	     (replace-match (concat "<hr style=\"height: 1px; width: "
				    width ";\" align=\"" justify "\">")))
	    (t (replace-match ""))))))

(defun bhl-convert-hr1 (arg)
  "Convert a raw of dashes into an horizontal rule.
ARG is the output format."
  (goto-char (point-min))
  (while (re-search-forward bhl-hr1-regexp nil t)
    (let (justify width)
      (cond ((equal (match-string 1) "<")
	     (setq justify "left"))
	    ((equal (match-string 1) ">")
	     (setq justify "right"))
	    (t (setq justify "center")))
      (cond ((eq arg 'txt)
	     (if (match-string 4)
		 (setq width (string-to-number (match-string 4)))
	       (setq width (or fill-column 70)))
	     (let (replacmt)
	       (if (equal justify "right")
		   (setq replacmt (concat (make-string (- (or fill-column 70) width) 32)
					  (make-string width ?-)))
		 (setq replacmt (make-string (or width fill-column 70) ?-)))
	       (replace-match replacmt)
	       (when (equal justify "center")
		 (center-line))))
	    ((eq arg 'latex)
	     (setq width (or (match-string 3) "\\\\textwidth"))
	     (let (flush)
	       (setq flush (if (equal justify "center") "" "flush"))
	       (replace-match
		(concat "\\\\begin{" flush justify "}\n\\\\rule{"
			width "}{5pt}\n\\\\end{" flush justify"}"))))
	    ((eq arg 'html)
	     (setq width (or (match-string 3) "100%"))
	     (replace-match (concat "<hr style=\"height: 5px; width: "
				    width ";\" align=\"" justify "\">")))
	    (t (replace-match ""))))))

(defun bhl-convert-hr2 (arg)
  "Convert a raw of dashes into an horizontal rule.
ARG is the output format."
  (goto-char (point-min))
  (while (re-search-forward bhl-hr2-regexp nil t)
    (let (justify width)
      (cond ((equal (match-string 1) "<")
	     (setq justify "left"))
	    ((equal (match-string 1) ">")
	     (setq justify "right"))
	    (t (setq justify "center")))
      (cond ((eq arg 'txt)
	     (if (match-string 4)
		 (setq width (string-to-number (match-string 4)))
	       (setq width (or fill-column 70)))
	     (let (replacmt)
	       (if (equal justify "right")
		   (setq replacmt (concat (make-string (- (or fill-column 70) width) 32)
					  (make-string width ?-)))
		 (setq replacmt (make-string (or width fill-column 70) ?-)))
	       (replace-match replacmt)
	       (when (equal justify "center")
		 (center-line))))
	    ((eq arg 'latex)
	     (setq width (or (match-string 3) "\\\\textwidth"))
	     (let (flush)
	       (setq flush (if (equal justify "center") "" "flush"))
	       (replace-match
		(concat "\\\\begin{" flush justify "}\n\\\\rule{"
			width "}{10pt}\n\\\\end{" flush justify"}"))))
	    ((eq arg 'html)
	     (setq width (or (match-string 3) "100%"))
	     (replace-match (concat "<hr style=\"height: 10px; width: "
				    width ";\" align=\"" justify "\">")))
	    (t (replace-match ""))))))

(defun bhl-convert-special (arg intro-tag end-tag mark-beg mark-end)
  "Convert a specific region into ARG format.
INTRO-TAG is a list of TXT, HTML, SGML and LaTeX tags to
insert at the beginning of the region.
END-TAG is a list of TXT, HTML, SGML and LaTeX tags  to
insert at the end of the region.
MARK-BEG is the delimiter of the beginning of the region.
MARK-END is the delimiter of the end of the region."
  (save-excursion
    (goto-char (point-min))
    (let ((prefix (cond ((eq arg 'txt) (nth 0 intro-tag))
			((eq arg 'html) (nth 1 intro-tag))
			((eq arg 'sgml) (nth 2 intro-tag))
			((eq arg 'latex) (nth 3 intro-tag))
			((eq arg 'texi) (nth 4 intro-tag))))
	  (suffix (cond ((eq arg 'txt) (nth 0 end-tag))
			((eq arg 'html) (nth 1 end-tag))
			((eq arg 'sgml) (nth 2 end-tag))
			((eq arg 'latex) (nth 3 end-tag))
			((eq arg 'texi) (nth 4 end-tag)))))
      (when (re-search-forward (concat "^\\(" mark-beg "\\)$") nil t)
	(replace-match prefix t t nil 1))
      (when (re-search-forward (concat "^\\(" mark-end "\\)$") nil t)
	(replace-match suffix t t nil 1)))))

;; Insertion functions
(defun bhl-insert-special (arg)
  "Insert a special environment, according to ARG.
When ARG is a dash, insert a horizontal rule.
When ARG is \"%\", insert a minipage."
  (let ((mk "")
	(align (completing-read
		"Align: " '(("left" 1) ("center" 2) ("right" 3))
		nil t "center"))
	(width (read-from-minibuffer "Width: ")))
    (cond ((equal align "left") (setq mk "<"))
	  ((equal align "center") (setq mk arg))
	  ((equal align "right") (setq mk ">")))
    (insert (concat mk (make-string 3 (string-to-char arg))
		    (if (equal width "") "\n" (concat "[" width "]\n"))))))

(defun bhl-insert-url ()
  "Read an URL and its name from the minibuffer."
  (interactive)
  (let ((url (read-from-minibuffer "URL: " ""))
	(name (read-from-minibuffer "Name: ")))
    (if (equal name "")
	(insert url)
      (insert "[[" url "][" name "]]"))
    (message "URL \"%s\" named \"%s\" inserted." url name)))

(defun bhl-insert-image (filename)
  "Read a FILENAME to insert as an image."
  (interactive "FInsert image: ")
  (if (file-directory-p filename)
      (signal 'file-error (list "Opening input file" "file is a directory"
				filename))
    (insert "[[" (file-name-nondirectory filename) "]]")
    (message "Image \"%s\" inserted." (file-name-nondirectory filename))))

(defun bhl-insert-hr ()
  "Insert a horizontal rule."
  (interactive)
  (bhl-insert-special "-")
  (message "Horizontal rule inserted."))

(defun bhl-insert-minipage ()
  "Insert a minipage."
  (interactive)
  (bhl-insert-special "%")
  (save-excursion
    (insert "\n%%%%"))
  (message "Minipage inserted."))

(defun bhl-insert-verbatim ()
  "Insert a verbatim environment."
  (interactive)
  (insert "--\n")
  (save-excursion
    (insert "--"))
  (message "Verbatim environment inserted."))

;; Core of the conversion
(defsubst bhl-replace-section (arg sec beg end &optional num ctr)
  "Replace section's labels with the right tags (HTML or LaTeX).
Here are the arguments:

ARG: the output format;
SEC: a regexp which matches the section;
BEG: a string to be inserted just before the title of the section;
END: a string to be inserted just after the title of the section;

NUM (optional): if nil, no prefix is used for sections' title.
CTR (optional): if t, center line."
  (goto-char (point-min))
  (while (re-search-forward (concat "\\(^" sec "\\)\\(.*$\\)") nil t)
    (let ((str (match-string 0))
	  (strnonum (match-string 2)))
      (replace-match
       (concat beg (if num (match-string 1) "")
	       strnonum end) t t)
      (when ctr (center-line))
      (cond ((eq arg 'html)
	     (beginning-of-line)
	     (insert "<a name=\"" (bhl-string-to-anchor str)
		     "\"></a>\n"))
	    ((eq arg 'texi)
	     (beginning-of-line)
	     (insert "@node " strnonum "\n"))))))

(defsubst bhl-replace-tag (word beg end)
  "Replace tags (HTML or LaTeX).
WORD is the string to be replaced.
BEG and END are the beginning end ending tags."
  (goto-char (point-min))
  (while (re-search-forward word nil t)
    (unless (or (and (text-property-any (match-beginning 2) (match-end 2)
					'category 'verbatim)
		     (memq 'tag bhl-verbatim-ignore))
		(text-property-any (match-beginning 2) (match-end 2)
				   'category 'link)
		(text-property-any (match-beginning 2) (match-end 2)
				   'category 'comment))
      (replace-match (concat beg (match-string 2) end)
		     t t nil 1))))

;; A generic conversion function for punctuation and special characters.
;; By default, special chars are not converted in verbatim environments.
(defun bhl-convert-char (arg lst)
  "Convert into the ARG format some strings from LST."
  (let ((cpt 0) case-fold-search)
    (while (nth cpt lst)
      (goto-char (point-min))
      (while (re-search-forward (car (nth cpt lst)) nil t)
	(unless (let ((cat (get-text-property (match-beginning 0) 'category)))
		  (or (and (eq cat 'verbatim)
			   (memq 'special-char bhl-verbatim-ignore))
		      (eq cat 'link)
		      (eq cat 'comment)))
	  (replace-match
	   (cond ((eq arg 'texi)
		  (cadddr (nth cpt lst)))
		 ((eq arg 'latex)
		  (caddr (nth cpt lst)))
		 ((or (eq arg 'html) (eq arg 'sgml))
		  (if (and (eq arg 'sgml)
			   (or (equal (match-string 0) "oe")
			       (equal (match-string 0) "OE")))
		      (match-string 0)
		    (cadr (nth cpt lst))))
		 (t (match-string 0)))
	   t t nil (if (match-string 1) 1 0))))
      (setq cpt (1+ cpt)))))

(defun bhl-convert-tex (arg)
  "Convert \"LaTeX\", \"LaTeX2e\" and \"TeX\" strings into ARG format."
  (goto-char (point-min))
  (while (re-search-forward "\\<TeX\\>" nil t)
    (unless (get-text-property (1+ (match-beginning 0)) 'category)
      (cond ((eq arg 'texi) (replace-match "@TeX{}" t t))
	    ((eq arg 'latex) (replace-match "\\TeX{}" t t))
	    ((eq arg 'html) (replace-match "T<small>E</small>X" t t))
	    (t (match-string 0) t t))))
  (goto-char (point-min))
  (let ((case-fold-search t))
    (while (re-search-forward "\\<\\(LaTeX\\)\\(2\\)?\\(e\\)?\\>" nil t)
      (unless (let ((cat (get-text-property (match-beginning 1) 'category)))
		(or (and (eq cat 'verbatim)
			 (memq 'tex-label bhl-verbatim-ignore))
		    (eq cat 'link)
		    (eq cat 'comment)))
	(replace-match (cond ((eq arg 'latex)
			      (concat "\\LaTeX" (match-string 3) "{}"))
			     ((eq arg 'texi)
			      (concat "La@TeX{}" (if (match-string 3)
						     (concat (match-string 2)
							     (match-string 3)))))
			     ((eq arg 'html)
			      (concat "L<sup><small>A</small></sup>T<small>E</small>X"
				      (if (match-string 3)
					  (concat (match-string 2) "&epsilon"))))
			     (t (match-string 0))) t t)))))

(defun bhl-convert-img (arg)
  "Convert inserted images into ARG format."
  (goto-char (point-min))
  (while (re-search-forward bhl-img-regexp nil t)
    (unless (let ((cat (get-text-property (1- (match-beginning 0))
					  'category)))
	      (or (and (eq cat 'verbatim)
		       (memq 'images bhl-verbatim-ignore))
		  (eq cat 'comment)))
      (replace-match
       (cond ((eq arg 'html)
	      (concat "<p><img alt=\"" (match-string 1)
		      "\" src=\"" (match-string 1) "\" align=\""
		      bhl-html-img-align "\">"))
	     ((eq arg 'texi) (concat "@c " (match-string 1)))
	     ((eq arg 'latex) (concat "% " (match-string 1)))
	     ((eq arg 'sgml) (concat "<!-- " (match-string 1) " -->"))
	     (t "")) t t))))

(defun bhl-convert-list (arg)
  "Convert lists into ARG format."
  (let ((regexp-list
	 (if (eq arg 'texi)
	     (reverse bhl-list-regexp-list)
	   bhl-list-regexp-list)))
    (while (car regexp-list)
      (let ((regexp1 (caar regexp-list))
	    (regexp2 (cadar regexp-list)))
	(bhl-convert-list-intern arg regexp1 regexp2)
	(setq regexp-list (cdr regexp-list))))))

(defun bhl-convert-list-intern (arg regexp1 regexp2)
  "Convert list in ARG format using REGEXP1 and REGEXP2."
  (goto-char (point-min))
  (while (re-search-forward regexp1 nil t)
    (unless (let ((cat (get-text-property (match-beginning 0) 'category)))
	      (or (and (eq cat 'verbatim)
		       (memq 'list bhl-verbatim-ignore))
		  (eq cat 'comment)))
      (beginning-of-line)
      (let* ((tab (if (eq arg 'texi) ""
		    (bhl-subst-char-in-string
		     ?\t ?  (substring (match-string 1) 1))))
	     (conversion (assq arg bhl-list-syntax-alist))
	     (syntax (if (string-match "[0-9]" (match-string 2))
			 (caddr conversion)
		       (cadr conversion))))
	(insert (concat tab (car syntax) "\n"))
	(while (re-search-forward
		regexp1
		(save-excursion
		  (forward-char 1)
		  (re-search-forward regexp2 nil t)
		  (match-beginning 0)) t)
	  (let ((para (when (and (or (eq arg 'html)
				     (eq arg 'sgml))
				 (get arg 'list-item-is-para)) "<p>")))
	    (replace-match (concat (unless (eq arg 'texi) " ")
				   tab (cadr syntax) para))))
	(forward-char 1)
	(re-search-forward regexp2 nil t)
	(beginning-of-line)
	(skip-chars-backward "\n\t ")
	(insert (concat "\n" tab (caddr syntax)))))))

(defun bhl-convert-description (arg)
  "Convert description lists into ARG format."
  (goto-char (point-min))
  (let ((syntax (cadddr (assq arg bhl-list-syntax-alist))))
    (while (re-search-forward "^\\(\t+\\)\\([^\n-]+\\) -- " nil t)
      (unless (let ((cat (get-text-property (match-beginning 0) 'category)))
		(or (and (eq cat 'verbatim)
			 (memq 'description bhl-verbatim-ignore))
		    (eq cat 'comment)))
	(let ((tab (if (eq arg 'texi) ""
		     (bhl-subst-char-in-string
		      ?\t ?  (substring (match-string 1) 1)))))
	  (beginning-of-line)
	  (insert (concat tab (car syntax) "\n"))
	  (while (re-search-forward
		  bhl-descrip-regexp
		  (save-match-data
		    (save-excursion
		      (forward-char 1)
		      (re-search-forward "^[\\'\"&*_=a-zA-Z0-9<]" nil t))) t)
	    (replace-match
	     (concat (if (eq arg 'texi) "" " ") tab (cadr syntax)
		     (match-string 2)
		     (caddr syntax)
		     (when (and (or (eq arg 'html)
				    (eq arg 'sgml))
				(get arg 'list-item-is-para)) "<p>")) t t))
	  (forward-char 1)
	  (re-search-forward "^[\t ]*$\\|\\'" nil t)
	  (beginning-of-line)
	  (skip-chars-backward "\n\t ")
	  (insert (concat "\n" tab (cadddr syntax))))))))

(defun bhl-convert-url (&optional arg)
  "Convert an URL.  An optional argument ARG specifies the output format."
  (goto-char (point-min))
  (while (re-search-forward bhl-url-regexp nil t)
    (unless (or (and (memq 'url bhl-verbatim-ignore)
		     (get-text-property (1- (match-beginning 0))
					'category) 'verbatim)
		(eq  (get-text-property (1- (match-beginning 0))
					'category) 'comment))
      (cond
       ((eq arg 'txt)
	(cond ((equal (match-string 3) "mailto:")
	       (replace-match (match-string 4) nil t))
	      ((equal (match-string 3) "url:")
	       (replace-match (match-string 4) nil t))
	      ((equal (match-string 6) "mailto:")
	       (replace-match (concat (match-string 7) " ("
				      (match-string 8) ")") nil t))
	      ((match-string 8)
	       (replace-match (concat (match-string 5) " ("
				      (match-string 8) ")") nil t))
	      (t (replace-match (match-string 1) nil t))))
       ((eq arg 'html)
	(cond ((equal (match-string 3) "mailto:")
	       (replace-match
		(concat "<a href=\""
			(match-string 1) "\">"
			(match-string 4) "</a>") nil t))
	      ((equal (match-string 3) "url:")
	       (replace-match
		(concat "<a href=\""
			(match-string 4) "\">"
			(match-string 4) "</a>") nil t))
	      ((replace-match
		(concat "<a href=\""
			(or (match-string 5)
			    (match-string 1)) "\">"
			    (or (match-string 8)
				(match-string 1))"</a>") nil t))))
       ((eq arg 'texi)
	(cond ((equal "url:" (match-string 3))
	       (replace-match (concat "@uref{" (match-string 4) "}")))
	      ((replace-match
		(concat "@uref{" (or (match-string 5)
				     (match-string 1))
			(if (match-string 8)
			    (concat "," (match-string 8) "}") "}"))))))
       ((eq arg 'sgml)
	(cond ((equal "url:" (match-string 3))
	       (replace-match
		(concat "<htmlurl url=\""
			(match-string 4) "\" name=\""
			(match-string 4) "\">")))
	      ((replace-match
		(concat "<htmlurl url=\"" (or (match-string 5)
					      (match-string 1))
			"\" name=\"" (or (match-string 8)
					 (match-string 1)) "\">")))))
       ((eq arg 'latex)
	(cond ((equal (match-string 3) "url:")
	       (replace-match
		(concat "\\url{" (match-string 4) "}") nil t))
	      ((equal (match-string 3) "mailto:")
	       (replace-match
		(concat "\\url{" (match-string 4) "}") nil t))
	      ((equal (match-string 6) "mailto:")
	       (replace-match
		(concat (match-string 8) " (\\url{"
			(match-string 7) "})") nil t))
	      ((match-string 8)
	       (replace-match
		(concat (match-string 8) " (\\url{"
			(match-string 5) "})") nil t))
	      (t (replace-match (concat "\\url{"
					(match-string 1) "}")
				nil t))))))))

;; Map tables
(defsubst bhl-map-table ()
  "Map and delete tables."
  (let ((beg (point))
	(limit (save-excursion (re-search-forward "^[^|]" nil t)))
	table)
    (while (re-search-forward "^|.*$" limit t)
      (setq table (append table (list (split-string (bhl-match-string 0) "|")))))
    (kill-region beg (point))
    table))

(defun bhl-table-beg (arg &optional table caption)
  "Create in ARG format the TABLE beginning tag with CAPTION."
  (cond ((eq arg 'html)
	 (concat
	  "<TABLE align=\"" bhl-table-align "\" "
	  "summary=\"" (or caption "No summary") "\" "
	  (mapconcat
	   (lambda (str)
	     (concat (car str) "=\"" (cdr str) "\""))
	   bhl-html-table-param-alist
	   " ") ">\n"))
	((eq arg 'latex)
	 (concat "\\begin{table}[" bhl-table-location "]\n"
		 (cond ((equal bhl-table-align "left") "\\flushleft\n")
		       ((equal bhl-table-align "center") "\\centering\n")
		       ((equal bhl-table-align "right") "\\flushright\n"))
		 "\\begin{tabular}{"
		 (make-string (length (car table))
			      (string-to-char bhl-table-cell-align))
		 "}\n"))
	((eq arg 'sgml)
	 (concat "<p><table loc=\"" bhl-table-location "\">\n"
		 "<tabular ca=\""
		 (make-string (length (car table))
			      (string-to-char bhl-table-cell-align))
		 "\">\n"))))

(defsubst bhl-convert-table (arg)
  "Convert tables into ARG format."
  (cond ((eq arg 'html)
	 (bhl-html-convert-table))
	((eq arg 'texi)
	 (bhl-texi-convert-table))
	((eq arg 'txt)
	 (bhl-txt-convert-table))
	(t (bhl-xxx-convert-table arg))))

(defun bhl-texi-convert-table ()
  "Convert tables into TEXI format."
  (bhl-txt-convert-table)
  (goto-char (point-min))
  (while (re-search-forward "^|" nil t)
    (unless (let ((cat (get-text-property
			(match-beginning 0) 'category)))
	      (and (eq cat 'verbatim)
		   (memq 'table bhl-verbatim-ignore)))
      (goto-char (match-beginning 0))
      (insert "@" bhl-texi-table-tag "\n")
      (re-search-forward "^[ \t]*$" nil t)
      (insert "@end " bhl-texi-table-tag "\n"))))

(defun bhl-txt-convert-table ()
  "Convert tables into TXT format."
  (goto-char (point-min))
  (save-excursion
    (while (re-search-forward "|[ \t]*@" nil t)
      (unless (let ((cat (get-text-property
			  (match-beginning 0) 'category)))
		(and (eq cat 'verbatim)
		     (memq 'table bhl-verbatim-ignore)))
	(replace-match (bhl-subst-char-in-string ?@ ?  (bhl-match-string 0)) t t))))
  (save-excursion
    (while (re-search-forward "@[ \t]*|" nil t)
      (unless (let ((cat (get-text-property
			  (match-beginning 0) 'category)))
		(and (eq cat 'verbatim)
		     (memq 'table bhl-verbatim-ignore)))
	(replace-match (bhl-subst-char-in-string ?@ ?  (bhl-match-string 0)) t t)))))

(defun bhl-html-convert-table ()
  "Convert tables into HTML format."
  (goto-char (point-min))
  (let* ((cell-align
	  (cond ((equal bhl-table-cell-align "l") "left")
		((equal bhl-table-cell-align "c") "center")
		((equal bhl-table-cell-align "r") "right")))
	 (tdbeg (concat "<td align=\"" cell-align "\">"))
	 (thbeg (concat "<th align=\"" cell-align "\">")))
    (while (re-search-forward "^|" nil t)
      (unless (let ((cat (get-text-property (match-beginning 0) 'category)))
		(and (eq cat 'verbatim)
		     (memq 'table bhl-verbatim-ignore)))
	(beginning-of-line)
	(let* ((caption
		(when (get 'html 'bhl-caption)
		  (read-from-minibuffer "Caption: ")))
	       (tbl (bhl-map-table))
	       (tablebeg (bhl-table-beg 'html tbl caption)))
	  (insert tablebeg)
	  (when caption
	    (insert "<caption>" caption "</caption>\n"))
	  (while (car tbl)
	    (insert " <tr>\n")
	    (mapcar
	     (lambda (str)
	       (insert
		(concat
		 "  "
		 (if (string-match "[ \t]*\\(@.*\\)@.*" str)
		     (progn (aset str (match-beginning 1) ?  )
			    (aset str (match-end 1) ?  )
			    (concat thbeg (bhl-cleanup-cell str)
				    "</th>\n"))
		   (concat tdbeg (bhl-cleanup-cell str)
			   "</td>\n")))))
	     (car tbl))
	    (insert " </tr>\n")
	    (setq tbl (cdr tbl))))
	(insert "</table>\n")))))

(defun bhl-xxx-convert-table (arg)
  "Convert tables into ARG format."
  (goto-char (point-min))
  (let ((sep (if (eq arg 'latex) "&" "|"))
	(endline (if (eq arg 'latex) "\\\\" "@")))
    (while (re-search-forward "^|" nil t)
      (unless (let ((cat (get-text-property (match-beginning 0) 'category)))
		(and (eq cat 'verbatim)
		     (memq 'table bhl-verbatim-ignore)))
	(beginning-of-line)
	(let* ((caption
		(if (get arg 'bhl-caption)
		    (read-from-minibuffer "Caption: ")
		  "No summary" ))
	       (tbl (bhl-map-table)))
	  (insert (bhl-table-beg arg tbl))
	  (mapcar
	   (lambda (str)
	     (let (header)
	       (insert (mapconcat
			(lambda (strg)
			  (if (string-match "[ \t]*\\(@.*\\)@.*" strg)
			      (progn
				(aset strg (match-beginning 1) ?  )
				(aset strg (match-end 1) ?  )
				(setq header (cond ((eq arg 'latex) " \\hline\n")
						   ((eq arg 'sgml) "<hline>\n"))))
			    (setq header "\n"))
			  (bhl-cleanup-cell strg))
			str sep) endline header))) tbl)
	  (setq tbl (cdr tbl))
	  (insert
	   (cond ((eq arg 'latex)
		  (concat "\\end{tabular}\n"
			  (when caption
			    (concat "\\caption{" caption "}\n"))
			  "\\end{table}\n"))
		 (t (concat
		     "</tabular>\n"
		     (when caption
		       (concat "<caption>" caption "</caption>\n"))
		     "</table>\n")))))))))

(defun bhl-html-insert-preamble  (maintitle title subtitle author style lang encoding)
  "Insert the preambule of the HTML output.
MAINTITLE TITLE SUBTITLE AUTHOR STYLE LANG and ENCODING
are passed to this function."
  (insert bhl-html-doctype "\n<html lang=\""
	  (or lang (car bhl-i18n-conventions)) "\">\n<head>\n")
  (if encoding
      (insert "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
	      encoding "\">\n")
    (insert bhl-html-content-type "\n"))
  (insert "<meta name=\"author\" content=\"" author "\">\n")
  (let ((cpt 0))
    (while (< cpt (safe-length bhl-html-meta-alist))
      (insert "<meta name=\"" (car (nth cpt bhl-html-meta-alist))
	      "\" content=\"" (cdr (nth cpt bhl-html-meta-alist)) "\">\n")
      (setq cpt (1+ cpt))))
  (let ((cpt 0))
    (while (< cpt (safe-length bhl-html-link-alist))
      (insert "<link rel=\"" (car (nth cpt bhl-html-link-alist))
	      "\" href=\"" (cdr (nth cpt bhl-html-link-alist)) "\">\n")
      (setq cpt (1+ cpt))))
  (if style
      (insert "<link href=\"" style
	      "\" rel=\"stylesheet\" type=\"text/css\">\n")
    (when (not (equal bhl-html-default-style ""))
      (insert bhl-html-default-style "\n")))
  (insert "<title>" title "</title>\n</head>\n\n<body>\n")
  (search-forward maintitle nil t)
  (replace-match (concat (car bhl-html-title-tags)
			 title (cadr bhl-html-title-tags) "\n") t t)
  (when subtitle
    (insert (car bhl-html-subtitle-tags) subtitle
	    (cadr bhl-html-subtitle-tags))))

(defun bhl-protect-chars (chars)
  "Escape some CHARS in the LaTeX output."
  (goto-char (point-min))
  (let ((repl (if (eq chars bhl-latex-escaped-chars)
		  "\\" "@")))
    (while (car chars)
      (save-excursion
	(while (re-search-forward (concat "\\(.\\|^\\)\\("
					  (regexp-quote (car chars))
					  "\\)") nil t)
	  (unless (let ((cat (get-text-property (match-beginning 1) 'category)))
		    (or (eq cat 'verbatim)
			(eq cat 'comment)
                        (save-excursion
                          (beginning-of-line)
                          (string-match bhl-minipage-regexp (match-string 0)))))
	    (replace-match (if (equal (match-string 1) "\\") (match-string 2)
			     (concat (match-string 1) repl
				   (match-string 2))) t t))))
      (setq chars (cdr chars)))))

(defun bhl-latex-insert-preamble (maintitle title author date class options package)
  "Insert the preambule of the HTML output.
MAINTITLE TITLE AUTHOR DATE CLASS OPTIONS and PACKAGE are passed to
this options."
  (let ((local-class (or class bhl-latex-default-class))
	(local-options (or options bhl-latex-default-class-options)))
    (search-forward maintitle nil t)
    (replace-match "")
    (insert "\\documentclass["
	    (mapconcat (lambda (option) option)
		       local-options ",")
	    "]{" local-class "}\n")
    (let ((cpt 0))
      (while (< cpt (safe-length bhl-latex-packages-alist))
	(insert "\\usepackage")
	(unless (equal "" (cdr (nth cpt bhl-latex-packages-alist)))
	  (insert "[" (cdr (nth cpt bhl-latex-packages-alist)) "]"))
	(insert "{" (car (nth cpt bhl-latex-packages-alist)) "}\n")
	(setq cpt (1+ cpt))))
    (or (not package) (insert package))
    (dolist (line bhl-latex-extra-preambles)
      (insert line "\n"))
    (insert "\n\\begin{document}\n")
    (insert "\n\\title{" title "}")
    (insert "\n\\author{" author "}")
    (when date
      (insert "\n\\date{" date "}"))
    (insert "\n\\maketitle\n")))

(defun bhl-sgml-insert-preamble (maintitle title author)
  "Insert the preambule of the SGML output.
MAINTITLE TITLE and AUTHOR are passed to this function."
  (search-forward maintitle nil t)
  (replace-match "")
  (insert "<!DOCTYPE linuxdoc SYSTEM>\n<article>\n")
  (insert "<title>" title "</title>\n")
  (insert "<author>" author
	  "</author>\n\n")
  (insert "<abstract>")
  (save-excursion
    (re-search-forward (concat "^" (nth 0 bhl-sectioning-regexp-list)) nil t)
    (forward-line -1)
    (insert "</abstract>\n\n")))

(defun bhl-texi-insert-preamble (maintitle title subtitle author date lang encoding)
  "Insert the preambule of the TEXI output.
MAINTITLE TITLE SUBTITLE AUTHOR DATE LANG and ENCODING
are passed to this function."
  (search-forward maintitle nil t)
  (replace-match "")
  (insert "\\input texinfo @c -*-texinfo-*-\n")
  (insert "\n@c %**start of header\n")
  (insert "@setfilename "
	  (concat (file-name-sans-extension
		   (file-name-nondirectory buffer-file-name))
		  ".info" "\n"))
  (insert "@setchapternewpage " bhl-texi-setchapternewpage-flag "\n")
  (insert "@settitle " title "\n")
  (insert "@set AUTHOR " author "\n")
  (when date (insert "@set UPDATED " date "\n"))
  (insert "@documentlanguage " (or lang (car bhl-i18n-conventions)) "\n")
  (when encoding (insert "@documentencoding " encoding "\n"))
  (insert "@c %**end of header\n\n")
  (insert "@titlepage\n")
  (let ((titlepage-style
	 (if (not (equal bhl-local-texi-titlepage-style ""))
	     bhl-local-texi-titlepage-style
	   bhl-texi-titlepage-style)))
    (if (eq titlepage-style 'classical)
	(progn
	  (insert "@title " title "\n")
	  (when subtitle (insert "@subtitle " subtitle "\n"))
	  (insert "@author Copyright @copyright{} "
		  (format-time-string "%Y") " @value{AUTHOR}")
	  (insert (if date " - @value{UPDATED}\n" "\n")))
      (progn
	(insert "@center @titlefont{" title "}\n@sp 10\n")
	(when subtitle (insert "@center " subtitle "\n@sp 1\n"))
	(insert "@center @value{AUTHOR}\n@sp 1")
	(insert (if date "\n@center @value{UPDATED}\n" "\n"))
	(insert "@page\n@vskip 0pt plus 1filll\n")
	(insert "Copyright @copyright{} "
		(format-time-string "%Y")
		" @value{AUTHOR}\n"))))
  (insert "@end titlepage\n\n")
  (insert "@ifnottex\n@node top\n")
  (insert "@top " title "\n@end ifnottex\n\n"))

(defun bhl-parse-title (arg)
  "Find the title of the document for the ARG format."
  (if (save-excursion
	(re-search-forward (concat "^#!" (symbol-name arg)
				   "-title!\\(.*\\)") nil t))
      (bhl-match-string 1)
    (if (save-excursion
	  (re-search-forward "^#!title!\\(.*\\)" nil t))
	(bhl-match-string 1)
      (save-excursion
	(re-search-forward bhl-title-regexp nil t))
      (bhl-match-string 1))))

(defun bhl-insert-preamble (arg)
  "Insert the header of `bhl2html', `bhl2latex' and `bhl2sgml'.
ARG defines the output format."
  (goto-char (point-min))
  (let (bhl-local-author
	bhl-local-date
	bhl-local-subtitle
	bhl-local-lang
	bhl-local-encoding
	bhl-local-html-style
	bhl-local-latex-class
	bhl-local-latex-class-options
	(bhl-local-latex-package (bhl-parse-latex-package))
	(bhl-main-title
	 (save-excursion
	   (re-search-forward bhl-title-regexp nil t)
	   (bhl-match-string 1)))
	(bhl-local-title (bhl-parse-title arg)))
    (bhl-parse-header)
    (unless bhl-local-author (setq bhl-local-author user-full-name))
    (bhl-parse-specifications arg)
    (cond ((eq arg 'html)
	   (bhl-html-insert-preamble
	    bhl-main-title bhl-local-title bhl-local-subtitle
	    bhl-local-author bhl-local-html-style
	    bhl-local-lang bhl-local-encoding))
	  ((eq arg 'latex)
	   (bhl-latex-insert-preamble
	    bhl-main-title bhl-local-title bhl-local-author bhl-local-date
	    bhl-local-latex-class bhl-local-latex-class-options bhl-local-latex-package))
	  ((eq arg 'sgml)
	   (bhl-sgml-insert-preamble
	    bhl-main-title bhl-local-title bhl-local-author))
	  ((eq arg 'texi)
	   (bhl-texi-insert-preamble
	    bhl-main-title bhl-local-title bhl-local-subtitle
	    bhl-local-author bhl-local-date
	    bhl-local-lang bhl-local-encoding))
	  (t (insert "%% ") (end-of-line) (insert " %%")
	     (when (get arg 'bhl-center) (center-line))
	     (when bhl-local-subtitle
	       (insert "\n\n" (concat "% " bhl-local-subtitle " %"))
	       (when (get arg 'bhl-center) (center-line)))))))


;;;; 7 - TABLE OF CONTENTS

;; Make the table of content
(defun bhl-make-toc ()
  "Create the table of contents.
Create the list of section in `bhl-tsl'
and the list of corresponding points in `bhl-tpl'."
  (setq bhl-tsl nil)
  (setq bhl-tpl nil)
  (save-excursion
    (let ((position (point)))
      (goto-char (point-min))
      (while (re-search-forward
	      (concat "^" (nth 0 bhl-sectioning-regexp-list) ".*$") nil t)
	(setq bhl-tsl (append bhl-tsl (list (bhl-match-string 0))))
	(setq bhl-tpl (append bhl-tpl (list (match-end 0))))
	(let ((limit (save-excursion
		       (re-search-forward
			(concat "^" (nth 0 bhl-sectioning-regexp-list)
				".*$\\|\\'") nil t)))
	      ssection  ppection)
	  (while (re-search-forward
		  (concat "^" (nth 1 bhl-sectioning-regexp-list) ".*$")
		  limit t)
 	    (setq ssection (append ssection (list (bhl-match-string 0))))
 	    (setq ppection (append ppection (list (match-end 0))))
	    (let ((limit2 (save-excursion
			    (re-search-forward
			     (concat "^" (nth 1 bhl-sectioning-regexp-list) ".*$\\|^"
				     (nth 0 bhl-sectioning-regexp-list)
				     ".*$\\|\\'") nil t)))
		  sssection pppection)
	      (while (re-search-forward
		      (concat "^" (nth 2 bhl-sectioning-regexp-list) ".*$")
		      limit2 t)
		(setq sssection (append sssection (list (bhl-match-string 0))))
		(setq pppection (append pppection (list (match-end 0)))))
	      (unless (not sssection)
		(setq ssection (append ssection (list sssection)))
		(setq ppection (append ppection (list pppection))))))
	  (unless (not ssection)
	    (setq bhl-tsl (append bhl-tsl (list ssection)))
	    (setq bhl-tpl (append bhl-tpl (list ppection))))))))
  (null bhl-tsl))

;; Convert the sections' prefix according to the target style
(defsubst bhl-cvt-sec (arg convert)
  "Convert ARG according to the CONVERT style."
  (let ((style (or convert bhl-sectioning-default-style)))
    (cond ((eq style 'num)
	   (concat (number-to-string arg) "."))
	  ((eq style 'alpha)
	   (concat (char-to-string (+ 64 arg)) "."))
	  ((eq style 'aster) "*")
	  ((eq style 'equal-sign) "="))))

;; Update the table of content
(defun bhl-update-toc (&optional convert)
  "Update and maybe CONVERT the table of contents."
  (interactive)
  (unless (eq bhl-sectioning-default-style 'my)
    (save-excursion
      (let ((scpt 0)
	    (repl ""))
	(goto-char (point-min))
	(while (re-search-forward
		(concat "^" (nth 0 bhl-sectioning-regexp-list)
			"\\(.*\\)$") nil t)
	  (setq scpt (1+ scpt))
	  (setq repl (bhl-cvt-sec scpt convert))
	  (replace-match (concat repl " " (match-string 1)) t t)
	  (let ((sscpt 0)
		(rrepl "")
		(limit (save-excursion
			 (save-match-data
			   (re-search-forward
			    (concat "^" (nth 0 bhl-sectioning-regexp-list)
				    ".*$\\|\\'") nil t)))))
	    (while (re-search-forward
		    (concat "^" (nth 1 bhl-sectioning-regexp-list)
			    "\\(.*\\)$") limit t)
	      (setq sscpt (1+ sscpt))
	      (setq rrepl (bhl-cvt-sec sscpt convert))
	      (replace-match (concat repl rrepl " " (match-string 1)) t t)
	      (let ((ssscpt 0)
		    (rrrepl "")
		    (limit2 (save-excursion
			      (save-match-data
				(re-search-forward
				 (concat "^" (nth 1 bhl-sectioning-regexp-list)
					 ".*$\\|^" (nth 0 bhl-sectioning-regexp-list)
					 ".*$\\|\\'") nil t)))))
		(while (re-search-forward
			(concat "^" (nth 2 bhl-sectioning-regexp-list)
				"\\(.*\\)$") limit2 t)
		  (setq ssscpt (1+ ssscpt))
		  (setq rrrepl (bhl-cvt-sec ssscpt convert))
		  (replace-match (concat repl rrepl rrrepl
					 " " (match-string 1)) t t))))))))
    (message "Table of contents updated.")))

;; View log in a new buffer
(defun bhl-view-log ()
  "Display the log messages of the last ARG conversion."
  (interactive)
  (with-output-to-temp-buffer "*Log*"
    (princ "BHL conversion log:\n")
    (princ "===================\n\n")
    (let ((cpt 0))
      (when (equal bhl-conversion-log nil)
	(princ "* Last conversion correctly achieved."))
      (while (nth cpt bhl-conversion-log)
	(princ (concat "* " (nth cpt bhl-conversion-log) "\n"))
	(setq cpt (1+ cpt))))))

(defun bhl-parse-point-toc (toc depth)
  "Parse TOC with DEPTH into a list of points."
  (mapcar (lambda (elem)
	    (if (listp elem)
		(unless (< (1- depth) 1)
		  (bhl-parse-point-toc elem (1- depth)))
	      (setq bhl-toc-point-list
		    (append bhl-toc-point-list (list elem)))))
	  toc))

(defun bhl-generate-toc (depth &optional html)
  "Insert a toc of DEPTH, possibly in HTML format."
  (when html (insert "<ul>\n"))
  (setq bhl-toc-point-list nil)
  (bhl-parse-point-toc bhl-tpl depth)
  (bhl-parse-toc bhl-tsl depth 0 html)
  (when html (insert "</ul>\n")))

(defun bhl-parse-toc (toc depth indent html)
  "Parse TOC with DEPTH and INDENT, possibly in HTML format."
  (mapcar
   (lambda (elem)
     (if (listp elem)
	 (unless (< (1- depth) 1)
	   (when html (insert (concat (make-string (* 2 indent) 32)
				      " <ul>\n")))
	   (bhl-parse-toc elem (1- depth) (1+ indent) html)
	   (when html (insert (concat (make-string (* 2 indent) 32)
				      " </ul>\n"))))
       (insert (if html (concat (make-string (* 2 indent) 32)
				" <li><A href=\"#"
				(bhl-string-to-anchor elem)
				"\">" elem "</a></li>\n")
		 (concat (make-string (1+ (* 2 indent)) 32)
			 elem "\n"))))) toc))

(define-key bhl-toc-mode-map [(mouse-2)] 'bhl-toc-mouse-visit)
(define-key bhl-toc-mode-map [?\r] 'bhl-quit-temp-buffer)
(define-key bhl-toc-mode-map [(left)] 'bhl-visit-location-previous)
(define-key bhl-toc-mode-map [(right)] 'bhl-visit-location-next)
(define-key bhl-toc-mode-map [(up)] 'bhl-visit-location-previous)
(define-key bhl-toc-mode-map [(down)] 'bhl-visit-location-next)
(define-key bhl-toc-mode-map "q" 'bhl-quit-temp-buffer)
(define-key bhl-toc-mode-map "f" 'bhl-quit-temp-buffer)
(define-key bhl-toc-mode-map "v" 'bhl-quit-temp-buffer)
(define-key bhl-toc-mode-map "n" 'bhl-visit-location-next)
(define-key bhl-toc-mode-map "p" 'bhl-visit-location-previous)
(define-key bhl-toc-mode-map "<" 'bhl-visit-location-top)
(define-key bhl-toc-mode-map ">" 'bhl-visit-location-bottom)
(define-key bhl-toc-mode-map "?" 'bhl-toc-show-help)
(define-key bhl-toc-mode-map "1" 'bhl-show-toc-1)
(define-key bhl-toc-mode-map "2" 'bhl-show-toc-2)
(define-key bhl-toc-mode-map "3" 'bhl-show-toc-3)
(define-key bhl-toc-mode-map "i" 'bhl-toc-insert-toc)

;; BHL TOC menu
(easy-menu-define bhl-toc-menu bhl-toc-mode-map
  "Menu of the BHL minor mode"
  '("Toc"
    ["Next item" bhl-visit-location-next t]
    ["Previous item" bhl-visit-location-previous t]
    ["Top" bhl-visit-location-top t]
    ["Bottom" bhl-visit-location-bottom t]
    ["Follow" bhl-quit-temp-buffer t]
    "---"
    ("Toc depth"
     ["Sections" bhl-show-toc-1 t]
     ["Subsections" bhl-show-toc-2 t]
     ["Subsubsections" bhl-show-toc-3 t])
    "---"
    ["Quick help" bhl-toc-show-help t]))

;; BHL TOC mode
(defun bhl-toc-mode ()
  "A major mode for browsing the table of content of a BHL document.

\\{bhl-toc-mode-map}"
  (kill-all-local-variables)
  (use-local-map bhl-toc-mode-map)
  (easy-menu-add bhl-toc-menu)
  (setq mode-name "BHL toc")
  (setq major-mode 'bhl-toc-mode))

(defun bhl-show-toc (&optional depth)
  "Create a new buffer *toc* with a browsable table of contents.
The optional argument DEPTH specifies the toc depth."
  (interactive "P")
  (bhl-update-toc)
  (if (not (bhl-make-toc))
      (progn
	(switch-to-buffer-other-window (get-buffer-create "*toc*"))
	(bhl-toc-mode)
	(setq buffer-read-only nil)
	(kill-region (point-min) (point-max))
	(insert bhl-intro-toc " (? for quick help)\n\n")
	(bhl-generate-toc
	 (or depth bhl-default-toc-depth))
	(bhl-highlight-toc-buffer)
	(setq buffer-read-only t)
	(setq bhl-toc-temporary-depth
	      (or depth bhl-default-toc-depth))
	(bhl-shrink-window-to-fit)
	(goto-line 3))
    (message "There is no (sub)section in this buffer.")))

(defun bhl-toc-mouse-visit (event)
  "Visit the item that is clicked on with EVENT."
  (interactive "e")
  (mouse-set-point event)
  (bhl-visit-location (event-end event)))

(defun bhl-visit-location (arg)
  "Visit the toc/lol item of the current line.
ARG is the toc location."
  (cond ((eq arg 'top) (goto-line 3))
	((eq arg 'bottom)
	 (goto-char (point-max))
	 (forward-line -1))
	((numberp arg) (forward-line arg)))
  (beginning-of-line)
  (let* ((frombuffer (buffer-name))
	 (nombre
	  (if (equal frombuffer "*toc*")
	      (nth (- (string-to-number
		       (substring (what-line) 5)) 3)
		   bhl-toc-point-list)
	    (progn (beginning-of-line)
		   (re-search-forward "\\([0-9]+\\) : " nil t)
		   (string-to-int (bhl-match-string 1))))))
    (when (numberp nombre)
      (other-window 1)
      (goto-char nombre)
      (recenter 0)
      (switch-to-buffer-other-window frombuffer))))

(defun bhl-skip-toc (&optional arg)
  "Convert the toc according to the ARG output format.
With no ARG, erase the toc in the source file."
  (interactive)
  (save-match-data
    (save-excursion
      (goto-char (point-min))
      (let ((beg (if (search-forward bhl-intro-toc nil t)
		     (match-beginning 0)))
	    (end (if (search-forward bhl-end-toc nil t)
		     (progn (forward-line 1) (point)))))
	(if (not (eq beg end))
	    (progn
	      (kill-region beg end)
	      (cond ((and (eq arg 'texi)
			  (get arg 'bhl-toc))
		     (insert "@contents\n"))
		    ((and (eq arg 'txt)
			  (get arg 'bhl-toc))
		     (bhl-generate-toc bhl-default-toc-depth))
		    ((and (eq arg 'html)
			  (get arg 'bhl-toc))
		     (bhl-generate-toc bhl-default-toc-depth t))
		    ((and (eq arg 'latex)
			  (get arg 'bhl-prefix)
			  (get arg 'bhl-toc))
		     (insert "\\setcounter{tocdepth}{"
			     (number-to-string bhl-default-toc-depth)
			     "}\n"  "\\tableofcontents\n")))))))))

(defsubst bhl-insert-lol ()
  "Insert the list of links at point."
  (interactive)
  (if (setq bhl-local-lol-list (bhl-map-lol))
      (let ((lol bhl-local-lol-list))
	(while (car lol)
	  (insert (cdar lol) "\n")
	  (setq lol (cdr lol))))
    (message "There is no link in this buffer.")))

(defun bhl-insert-toc (&optional depth)
  "Insert the table of contents in the source file.
Optional argument DEPTH is the toc depth.
Toc place is defined in `bhl-toc-location'."
  (interactive "P")
  (if (not (bhl-make-toc))
      (progn
	(bhl-update-toc)
	(cond ((equal bhl-toc-location "top")
	       (save-excursion
		 (goto-char (point-min))
		 (re-search-forward bhl-title-regexp nil t)
	     (forward-line 1)
	     (bhl-skip-toc)
	     (forward-line 1)
	     (insert bhl-intro-toc "\n\n")
	     (bhl-generate-toc (or depth bhl-default-toc-depth))
	     (insert "\n" bhl-end-toc "\n")))
	  ((equal bhl-toc-location "bottom")
	   (save-excursion
	     (bhl-skip-toc)
	     (goto-char (point-max))
	     (insert bhl-intro-toc "\n\n")
	     (bhl-generate-toc (or depth bhl-default-toc-depth))
	     (insert "\n" bhl-end-toc)))
	  ((equal bhl-toc-location "point")
	   (save-excursion
	     (bhl-skip-toc)
	     (insert bhl-intro-toc "\n\n")
	     (bhl-generate-toc (or depth bhl-default-toc-depth))
	     (insert "\n" bhl-end-toc))))
    (message (concat "Table of content inserted at the "
		     bhl-toc-location ".")))
    (message "There is no (sub)section in this buffer.")))

(defun bhl-toc-insert-toc ()
  "Insert the toc from the *toc* buffer."
  (interactive)
  (bhl-quit-temp-buffer)
  (bhl-insert-toc bhl-toc-temporary-depth))

(defun bhl-show-toc-1 ()
  "Display the toc in the *toc* buffer, DEPTH set to one."
  (interactive)
  (bhl-quit-temp-buffer)
  (bhl-show-toc 1))

(defun bhl-show-toc-2 ()
  "Display the toc in the *toc* buffer, DEPTH set to two."
  (interactive)
  (bhl-quit-temp-buffer)
  (bhl-show-toc 2))

(defun bhl-show-toc-3 ()
  "Display the toc in the *toc* buffer, DEPTH set to three."
  (interactive)
  (bhl-quit-temp-buffer)
  (bhl-show-toc 3))

(defun bhl-toc-show-help ()
  "Display a quick help for the browsable toc."
  (interactive)
  (message "[n] - next | [p] - previous | [<] - begin | [>] - end | [123] - depth | [i] insert toc"))

(defun bhl-visit-location-next ()
  "Visit the next item of the toc/lol buffer."
  (interactive)
  (bhl-visit-location 1))

(defun bhl-visit-location-previous ()
  "Visit the previous item of the toc/lol buffer."
  (interactive)
  (bhl-visit-location -1))

(defun bhl-visit-location-top ()
  "Visit the first item of the toc/lol buffer."
  (interactive)
  (bhl-visit-location 'top))

(defun bhl-visit-location-bottom ()
  "Visit the last item of the toc/lol buffer."
  (interactive)
  (bhl-visit-location 'bottom))

(defun bhl-quit-temp-buffer ()
  "Quit the current (top/lol) buffer."
  (interactive)
  (kill-this-buffer)
  (other-window 1)
  (delete-other-windows))

(define-key bhl-lol-mode-map [(left)] 'bhl-visit-location-previous)
(define-key bhl-lol-mode-map [(right)] 'bhl-visit-location-next)
(define-key bhl-lol-mode-map [(up)] 'bhl-visit-location-previous)
(define-key bhl-lol-mode-map [(down)] 'bhl-visit-location-next)
(define-key bhl-lol-mode-map "q" 'bhl-quit-temp-buffer)
(define-key bhl-lol-mode-map "f" 'bhl-quit-temp-buffer)
(define-key bhl-lol-mode-map "v" 'bhl-quit-temp-buffer)
(define-key bhl-lol-mode-map "n" 'bhl-visit-location-next)
(define-key bhl-lol-mode-map "p" 'bhl-visit-location-previous)
(define-key bhl-lol-mode-map "<" 'bhl-visit-location-top)
(define-key bhl-lol-mode-map ">" 'bhl-visit-location-bottom)
(define-key bhl-lol-mode-map "?" 'bhl-lol-show-help)
(define-key bhl-lol-mode-map "i" 'bhl-lol-insert-lol)

(defsubst bhl-lol-insert-lol ()
  "Insert the list of links from the *lol* buffer."
  (interactive)
  (bhl-quit-temp-buffer)
  (bhl-insert-lol))

(defsubst bhl-lol-show-help ()
  "Show help in the bhl *lol* buffer."
  (interactive)
  (message "[n] - next | [p] - previous | [<] - begin | [>] - end | [i] insert lol | [q] quit"))

;; Lol menu
(easy-menu-define bhl-lol-menu bhl-lol-mode-map
  "Menu of the BHL minor mode"
  '("Lol"
    ["Next item" bhl-visit-location-next t]
    ["Previous item" bhl-visit-location-previous t]
    ["Top" bhl-visit-location-top t]
    ["Bottom" bhl-visit-location-bottom t]
    ["Follow" bhl-quit-temp-buffer t]
    "---"
    ["Quick help" bhl-lol-show-help t]))

;; Lol mode - browse the links of your document
(defun bhl-lol-mode ()
  "A major mode for browsing the list of links (lol) of a BHL document.

\\{bhl-lol-mode-map}"
  (kill-all-local-variables)
  (use-local-map bhl-lol-mode-map)
  (easy-menu-add bhl-lol-menu)
  (setq mode-name "BHL lol")
  (setq major-mode 'bhl-lol-mode))

(defun bhl-show-lol ()
  "Show the lol of the current BHL file."
  (interactive)
  (if (setq bhl-local-lol-list (bhl-map-lol))
      (progn
	(switch-to-buffer-other-window (get-buffer-create "*lol*"))
	(bhl-lol-mode)
	(setq buffer-read-only nil)
	(kill-region (point-min) (point-max))
	(insert "--- List of links (? for quick help)\n\n")
	(save-excursion
	  (add-text-properties (goto-char (point-min))
			       (re-search-forward "ks " nil t)
			       '(face bold)))
	(let* ((lol bhl-local-lol-list)
	       (size-max (length (int-to-string (caar (reverse lol))))))
	  (while (car lol)
	    (insert (make-string
		     (- size-max (length (int-to-string (caar lol)))) ?  )
		    (int-to-string (caar lol))
		    " : " (cdar lol) "\n")
	    (setq lol (cdr lol))))
	(bhl-highlight-url-lol-buffer)
	(bhl-highlight-wiki-lol-buffer)
	(setq buffer-read-only t)
	(bhl-shrink-window-to-fit)
	(goto-line 3))
    (message "There is no link in this buffer.")))

(defun bhl-highlight-url-lol-buffer ()
  "Highlight URLs in the *Lol* buffer."
  (goto-char (point-min))
  (let ((map (copy-keymap (current-local-map))))
    (define-key map [(mouse-2)] 'bhl-follow-url-at-mouse)
    (define-key map [(return)] 'bhl-follow-url-at-point)
    (while (re-search-forward (concat "^[ ]*\\([0-9]+\\) : \\("
				      bhl-generic-url-regexp "\\)$") nil t)
      (add-text-properties (match-beginning 1)
			   (match-end 1)
			   '(face bhl-url-face))
      (add-text-properties (match-beginning 2) (match-end 2)
			   `(category link local-map ,map mouse-face
				      highlight help-echo
				      "mouse-2: follow URL")))))

(defun bhl-highlight-wiki-lol-buffer ()
  "Highlight WikiNames in the *Lol* buffer."
  (goto-char (point-min))
  (let ((map (copy-keymap (current-local-map))))
    (define-key map [(mouse-2)] 'bhl-follow-wiki-name-at-mouse)
    (define-key map [(return)] 'bhl-follow-wiki-name-at-point)
    (while (re-search-forward (concat "^[ ]*\\([0-9]+\\) : \\("
				      bhl-wiki-names-regexp "\\)$") nil t)
      (add-text-properties (match-beginning 1)
			   (match-end 1)
			   '(face bhl-wiki-face))
      (add-text-properties (match-beginning 2) (match-end 2)
			   `(category link local-map ,map mouse-face
				      highlight help-echo
				      "mouse-2: follow WikiName")))))

(defun bhl-map-lol ()
  "Map lol and return a list of links."
  (let (lol-list case-fold-search)
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward (concat bhl-generic-url-regexp "\\|"
					bhl-wiki-names-regexp) nil t)
	(add-to-list 'lol-list
		     (cons (match-beginning 0)
			   (bhl-match-string 0)))))
    (reverse lol-list)))


;;;; 8 - Submit bug report
(defun bhl-submit-bug-report ()
  "Submit a bug report on BHL Mode."
  (interactive)
  (and
   (y-or-n-p "Do you want to submit a report on BHL Mode? ")
   (require 'reporter "reporter" t)
   (reporter-submit-bug-report
    bhl-help-address
    (concat "BHL Mode " bhl-version)
    (list
     ;; report all important variables
     'bhl-mode-hook
     'bhl-sectioning-default-style
     'bhl-verbatim-ignore
     'bhl-my-sectioning-regexp-list
     'bhl2html-properties-list
     'bhl2latex-properties-list
     'bhl2sgml-properties-list
     'bhl2txt-properties-list
     'bhl-html-conversions-list
     'bhl-latex-conversions-list
     'bhl-sgml-conversions-list
     'bhl-txt-conversions-list
     'bhl-i18n-conventions
     'bhl-tab-width
     'bhl-default-toc-depth
     'bhl-toc-location
     'bhl-table-location
     'bhl-table-align
     'bhl-table-cell-align
     'bhl-html-table-param-alist
     'bhl-html-para-align
     'bhl-html-img-align
     'bhl-html-meta-alist
     'bhl-latex-packages-alist
     'bhl-latex-default-class
     'bhl-latex-default-class-options)
    nil
    nil
    "Hi Bastien,\n\n")))


;;;; 9 - BHL2XXX

(defun bhl2html ()
  "Convert the BHL source file into a HTML file."
  (interactive)
  (bhl2xxx 'html))

(defun bhl2latex ()
  "Convert the BHL source file into a LaTeX file."
  (interactive)
  (bhl2xxx 'latex))

(defun bhl2sgml ()
  "Convert the BHL source file into a SGML (Linuxdoc) file."
  (interactive)
  (bhl2xxx 'sgml))

(defun bhl2txt ()
  "Convert the BHL source file into a TXT file.
Strip font tags and URL/IMG square brackets."
  (interactive)
  (bhl2xxx 'txt))

(defun bhl2texinfo ()
  "Convert the BHL source file into a TEXINFO file."
  (interactive)
  (bhl2xxx 'texi))

;; Perform initializations
(defun bhl-convert-init (arg)
  "Initial conversion into ARG format."
  (save-buffer)
  (setq bhl-conversion-log nil)
  (setq bhl-xxx-conversions-list
	(get arg 'conversions-list))
  (let ((bhl-source-file buffer-file-name)
	(bhl-workfile-name
	 (concat (file-name-sans-extension buffer-file-name)
		 (cond ((eq arg 'latex) ".tex")
		       (t (concat "." (symbol-name arg)))))))
    (find-file-other-window bhl-workfile-name)
    (kill-region (point-min) (point-max))
    (setq tab-width 3)
    (insert-file-contents bhl-source-file)))

;; Add properties into the working buffer.
(defun bhl-add-text-properties (arg)
  "Add some text properties to the conversion buffer.
ARG is the output format."
  (goto-char (point-min))
  (save-excursion
    (while (re-search-forward "^--$" nil t)
      (add-text-properties (match-end 0)
			   (progn (re-search-forward "^--$" nil t)
				  (match-beginning 0))
			   '(category verbatim))))
  (save-excursion
    (let (case-fold-search)
      (while (re-search-forward
	      (concat bhl-img-regexp "\\|"
		      bhl-generic-url-regexp "\\|"
		      bhl-wiki-names-regexp) nil t)
	(add-text-properties (match-beginning 0) (match-end 0)
			     '(category link)))))
  (save-excursion
    (while (re-search-forward "[^\\]\\(#.*\\)$" nil t)
      (add-text-properties (match-beginning 1) (match-end 1)
			   '(category comment))))
  (when (eq arg 'latex)
    (save-excursion
      (while (re-search-forward bhl-minipage-regexp nil t)
	(add-text-properties (match-beginning 0) (match-end 0)
			     '(category comment)))))
  (when (eq arg 'texi)
    (save-excursion
      (while (re-search-forward "^|" nil t)
	(unless (eq (get-text-property (match-beginning 0) 'category) 'verbatim)
	  (add-text-properties (match-beginning 0)
			       (re-search-forward "^[ \t]*$" nil t)
			       '(category table)))))))

(defun bhl-convert-misc-1 (arg)
  "Miscellaneous conversions into ARG format.
This conversions are: special char, verbatim, horizontal rules
and LaTeX/TeX labels."
  (when (get arg 'bhl-i18n)
    (when (nth 1 bhl-i18n-conventions)
      (bhl-convert-char
       arg (eval (intern (concat "bhl-"
				 (car bhl-i18n-conventions)
				 "-punctuation")))))
    (when (nth 2 bhl-i18n-conventions)
      (bhl-convert-char arg (eval (intern (concat "bhl-" (car bhl-i18n-conventions)
				 "-quotation-marks")))))
    (when (nth 3 bhl-i18n-conventions)
      (bhl-convert-char
       arg (eval (intern (concat "bhl-" (car bhl-i18n-conventions)
				 "-special-chars")))))
    (bhl-add-to-conversion-log
     (concat "I18n conventions: " (car bhl-i18n-conventions) ".")))
  (if (get arg 'bhl-check)
      (progn (bhl-check-existing-tags arg)
	     (bhl-check-comment arg))
    (bhl-add-to-conversion-log
     "No check of dubious tags."))
  (when (nth 13 bhl-xxx-conversions-list)
    (bhl-convert-quote arg))
  (when (nth 0 bhl-xxx-conversions-list)
    (bhl-convert-verbatim arg))
  (when (nth 1 bhl-xxx-conversions-list)
    (bhl-convert-hr arg))
  (when (nth 1 bhl-xxx-conversions-list)
    (bhl-convert-hr1 arg))
  (when (nth 1 bhl-xxx-conversions-list)
    (bhl-convert-hr2 arg))
  (when (nth 7 bhl-xxx-conversions-list)
    (bhl-convert-tex arg)
    (bhl-add-to-conversion-log
     "LaTeX and TeX labels conversion."))
  (when (nth 8 bhl-xxx-conversions-list)
    (condition-case nil
	(progn (require 'footnote)
	       (if (eq arg 'sgml)
		   (bhl-skip-footnotes arg)
		 (bhl-convert-footnotes arg)))
      (error (bhl-add-to-conversion-log
              "BHL was unable to convert footnotes."))))
  (when (nth 9 bhl-xxx-conversions-list)
    (bhl-convert-minipage arg)))

(defun bhl-convert-misc-2 (arg)
  "Miscellaneous conversions into ARG format.
This conversion are: images, URLs, description lists, lists, tables."
  (when (nth 2 bhl-xxx-conversions-list)
    (bhl-convert-img arg))
  (when (and (nth 12 bhl-xxx-conversions-list)
	     bhl-is-a-local-wiki-flag)
    (bhl-convert-wiki-names arg))
  (when (nth 3 bhl-xxx-conversions-list)
    (bhl-convert-url arg)
    (when (eq arg 'latex)
      (bhl-add-to-conversion-log
       "Please check that you have included the URL package.")))
  (when (nth 4 bhl-xxx-conversions-list)
    (bhl-convert-description arg))
  (when (nth 5 bhl-xxx-conversions-list)
    (bhl-convert-list arg))
  (when (nth 6 bhl-xxx-conversions-list)
    (when (not (get arg 'bhl-caption))
      (bhl-add-to-conversion-log
       "No caption was asked for tables."))
    (bhl-convert-table arg)))

;; Convert paragraphs
(defun bhl-convert-para (arg)
  "Convert paragraphs into ARG format."
  (save-excursion
    (goto-char (point-min))
    (when (eq arg 'sgml)
      (re-search-forward "</abstract>" nil t)
      (end-of-line))
    (while (re-search-forward "^[\\'\"&*_=a-zA-Z0-9@]" nil t)
      (goto-char (match-beginning 0))
      (if (not (or (eq (get-text-property (match-beginning 0) 'category) 'verbatim)
		   (save-excursion (forward-line -1)
				   (looking-at "<\\|[a-zA-Z0-9]"))
		   (memq t (mapcar (lambda (str) (looking-at str))
				   bhl-sectioning-regexp-list))))
	  (save-match-data
	    (cond ((or (eq arg 'sgml)
		       (equal bhl-html-para-align "none"))
		   (insert "<p>\n"))
		  (t (insert "\n<p align=\""
			     bhl-html-para-align "\">")))
	    (re-search-forward "^[ \t]*$" nil t)
	    (insert "</p>\n"))
	(forward-line 1)))))

;; Generic function for sections and tags conversion
(defun bhl-convert-sec-tag (arg)
  "Convert sections and tags into ARG format."
  (when (eq arg 'latex)
    (dolist (line bhl-latex-extra-body)
      (insert line "\n")))
  (let ((bhl-tag-list
	 (cdr (assq arg bhl-tag-syntax-alist)))
	(bhl-sec-list
	 (cdr (assq (if (and (eq arg 'latex)
			     (not (get arg 'bhl-prefix)))
			'latex-nonum arg)
		    bhl-section-syntax-alist))))
    (let ((cpt 0))
      (while (< cpt (length bhl-tag-list))
	(let ((mt (nth cpt bhl-tag-regexp-list))
	      (db (car (nth cpt bhl-tag-list)))
	      (fn (cadr (nth cpt bhl-tag-list))))
	  (bhl-replace-tag mt db fn))
	(setq cpt (1+ cpt))))
    (let ((cpt 0))
      (while (< cpt (length bhl-sec-list))
	(let ((mt (nth cpt bhl-sectioning-regexp-list))
	      (db (car (nth cpt bhl-sec-list)))
	      (fn (cadr (nth cpt bhl-sec-list))))
	  (bhl-replace-section arg mt db fn
			       (when (eq arg 'html)
				 (get arg 'bhl-prefix))
			       (when (eq arg 'txt)
				 (get arg 'bhl-center))))
	(setq cpt (1+ cpt))))))

;; Handling comments
(defun bhl-convert-comment (arg)
  "Convert comment into ARG format."
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward
	    (concat "\\([^\\\"]\\)\\(#\\)\\(.*\\)$") nil t)
      (unless (let ((cat (get-text-property (match-beginning 0) 'category)))
		(and (eq cat 'verbatim)
		     (memq 'comment bhl-verbatim-ignore)))
	(bhl-add-to-conversion-log
	 (format "Comment \"%s...\" converted."
		 (substring (match-string 3) 0
			    (if (< (length (match-string 3)) 20)
				(length (match-string 3))
			      20))))
	(cond ((eq arg 'latex)
	       (replace-match "%" t t nil 2))
	      ((eq arg 'texi)
	       (replace-match "@c " t t nil 2))
	      ((eq arg 'txt)
	       (replace-match ""))
	      (t (replace-match
		  (concat (match-string 1)
			  "<!-- " (match-string 3) " -->") t t)))))))

;; (Un)comment regions
(defun bhl-comment-region (&optional prefix)
  "Comment the region or uncomment it when PREFIX."
  (interactive "P")
  (let* ((beg (region-beginning))
	 (end (region-end)))
    (save-excursion
      (goto-char beg)
      (if prefix
	  (while (re-search-forward "^#\\( \\)?" end t)
	    (setq end (- end (if (match-string 1) 2 1)))
	    (replace-match ""))
	(while (re-search-forward "^[^\n\r]" end t)
	  (setq end (+ 2 end))
	  (replace-match (concat "# " (match-string 0)) t t))))))

(defsubst bhl-hide-comment (&optional arg)
  "Hide comments according to ARG format.
With a prefix argument, unhide comments."
  (interactive "P")
  (save-excursion
    (goto-char (point-min))
    (let ((func (if arg 'remove-text-properties
		  'add-text-properties)))
      (while (re-search-forward "#.*$" nil t)
	(funcall func (match-beginning 0)
		 (match-end 0) '(invisible t))))))

;; Convert escape characters (attention escaping \ for bhl2latex)
(defun bhl-convert-escape-char (arg)
  "Convert escape characters for ARG format."
  (save-excursion
    (mapcar
     '(lambda (str)
	(goto-char (point-min))
	(while (search-forward (concat "\\" str) nil t)
	  (let ((cat (get-text-property (match-beginning 0) 'category)))
	    (unless (or (eq cat 'comment)
			(eq cat 'verbatim)
			(and (eq arg 'latex)
			     (or (equal str "_")
				 (equal str "#"))))
	      (bhl-add-to-conversion-log
	       (format "Escape character at point %d deleted."
		       (match-beginning 0)))
	      (cond ((eq arg 'latex)
		     (if (equal (match-string 0) "\\\\")
			 (replace-match "$\\backslash$" t t)
		       (replace-match (substring str (1- (safe-length str))) t t)))
		    (t (unless (equal (match-string 0) "\\\\")
			 (replace-match (substring str (1- (safe-length str))) t t))))))))
     bhl-escapable-chars)))

;; Convert WikiNames
(defun bhl-convert-wiki-names (arg)
  "Convert WikiNames according to the ARG format."
  (save-excursion
    (goto-char (point-min))
    (let (case-fold-search)
      (while (re-search-forward bhl-wiki-names-regexp nil t)
	(unless (let ((cat (get-text-property (1- (match-beginning 0))
					      'category)))
		  (or (member (match-string 0) bhl-non-wiki-names-list)
		      (and (eq cat 'verbatim)
			   (memq 'wikiname bhl-verbatim-ignore))
		      (eq cat 'comment)))
	  (let* ((wikiname (match-string 0))
		 (dwikiname (if bhl-downcase-wikifiles-names-flag
				(downcase wikiname)
			      wikiname)))
	    (cond ((eq arg 'html)
		   (replace-match (concat "<a href=\"" dwikiname ".html\">"
					  wikiname "</a>") t t))
		  ((eq arg 'texi)
		   (replace-match (concat "@emph{" wikiname "}") t t))
		  ((eq arg 'txt)
		   (replace-match wikiname t t))
		  ((eq arg 'latex)
		   (replace-match (concat "\\emph{" wikiname "}") t t))
		  ((eq arg 'sgml)
		   (replace-match (concat "<htmlurl url=\"" dwikiname ".html\" name=\""
					  wikiname "\">") t t)))))))))

;; Convert some special chars in the title strings
(defun bhl-clean-title (arg title-regexp)
  "Clean the title according to the ARG format and the TITLE-REGEXP."
  (save-excursion
    (goto-char (point-min))
    (let ((string (prog2 (re-search-forward title-regexp nil t)
		      (bhl-match-string 1)
		    (replace-match "" t t nil 1)))
	  (repl-alist (cond ((eq arg 'latex) '(("\\*" . "*")
					       ("&" . "\\&")
					       ("$" . "\\$")
					       ("%" . "\\%")
					       ("{" . "\\{")
					       ("}" . "\\}")))
			    ((eq arg 'texi) '(("\\*" . "*")
					      ("\\_" . "_")
					      ("\\#" . "#")
					      ("" "@'e")
					      ("" "@`e")
					      ("" "@`a")
					      ("" "@^u")
					      ("" "@^e")
					      ("" "@^o")
					      ("" "@^{@dotless{i}}")
					      ("" "@^a")
					      ("" "@`u")
					      ("" "@,{c}")
					      ("@" . "@@")
					      ("{" . "@{")
					      ("}" . "@}")))
			    (t '(("\\*" . "*")
				 ("\\_" . "_")
				 ("\\#" . "#"))))))
      (while (car repl-alist)
	(setq string (bhl-replace-regexp-in-string
		      (regexp-quote (caar repl-alist))
		      (cdar repl-alist)
		      string t t))
	(setq repl-alist (cdr repl-alist)))
      (insert string))))

(defun bhl-clean-title-main (arg)
  "Clean up each title string according to the ARG format."
  (mapcar '(lambda (regexp) (bhl-clean-title arg regexp))
	  `(,bhl-title-regexp
	    "^#!texi-title!\\(.*\\)$"
	    "^#!latex-title!\\(.*\\)$"
	    "^#!html-title!\\(.*\\)$"
	    "^#!sgml-title!\\(.*\\)$"
	    "^#!title!\\(.*\\)$"
	    "^#!subtitle!\\(.*\\)$")))

;; Parse some informations concerning current buffer
(defun bhl-parse-header (&optional nodelete)
  "Parse global informations concerning the current file.
Don't delete lines concerning headers if NODELETE is non-nil."
  (save-excursion
    (mapcar
     (lambda (elem)
       (goto-char (point-min))
       (when (re-search-forward
	      (concat "^#!\\(" elem "\\)![ \t]*\\(.*\\)$")  nil t)
	 (let ((match (bhl-match-string 2)))
	   (set (intern (concat "bhl-local-" elem))
		(if (and (equal elem "date")
			 (string-match "%" match))
		    (format-time-string match)
		  match)))))
     '("date" "author" "title" "subtitle" "lang" "encoding")))
  (when bhl-local-lang
    (setcar bhl-i18n-conventions bhl-local-lang)))
    
;; Convert specification strings
(defun bhl-parse-specifications (arg)
  "Parse specifications strings for ARG output.
A specification string looks like:

#!html!check:noprefix:nocaption:toc

In this example, the `bhl2html' conversion checks for dubious tags,
delete sections' prefix, does not ask for tables' caption and insert
a table of content."
  (save-excursion
    (goto-char (point-min))
    (save-excursion
      (re-search-forward (concat "^#!" (symbol-name arg) "!\\(.*\\)$") nil t)
      (let ((string (bhl-match-string 1)))
	(mapcar (lambda (str)
		  (mapcar (lambda (spec)
			    (cond ((equal str spec)
				   (put arg (intern (concat "bhl-" spec)) t))
				  ((equal str (concat "no" spec))
				   (put arg (intern (concat "bhl-" spec)) nil))
				  (t nil)))
			  '("check" "prefix" "caption" "center" "toc" "i18n" "tas")))
		(split-string string ":"))))
    (when (and (eq arg 'latex)
	       (save-excursion
		 (re-search-forward "^#!latex-class!\\(.*\\)$" nil t)))
      (setq bhl-local-latex-class (bhl-match-string 1)))
    (when (and (eq arg 'latex)
	       (save-excursion
		 (re-search-forward "^#!latex-options!\\(.*\\)$" nil t)))
	(setq bhl-local-latex-class-options
	      (split-string (bhl-match-string 1) ":")))
    (when (and (eq arg 'texi)
	       (save-excursion
		 (re-search-forward "^#!texi-style!\\(.*\\)$" nil t)))
	(setq bhl-local-texi-titlepage-style
	      (intern (bhl-match-string 1))))
    (when (and (eq arg 'html)
	       (save-excursion
		 (re-search-forward "^#!html-style!\\(.*\\)$" nil t)))
      (setq bhl-local-html-style (bhl-match-string 1)))))

(defun bhl-parse-latex-package ()
  "Parse package specifications for the bhl2latex conversion."
  (interactive)
  (when (save-excursion
	  (re-search-forward "^#!latex-packages!\\(.*\\)$" nil t))
    (let ((result "")
	  (str (split-string (bhl-match-string 1) ":")))
      (mapcar
       (lambda (strg)
	 (if (string-match "\\[.*\\]" strg)
	     (setq result
		   (concat result "\\usepackage"
			   (substring strg
				      (match-beginning 0)
				      (match-end 0))
			   "{" (if (equal (match-end 0) (length strg))
				   (substring strg 0 (match-beginning 0))
				 (substring strg (match-end 0))) "}\n"))
	   (setq result
		 (concat result "\\usepackage{" strg "}\n"))))
       str) result)))

;; A wrapper function designed to allow the use of BHL in scripts.
;; Note that Emacs has no very useful and obvious way to read
;; characters from stdin into a buffer, so it will be the caller's job
;; to create a temp file if it wants to be able to take stdin input.
(defun bhl2xxx-batch-wrapper (file format &optional outstream)
  "Convert FILE into FORMAT and send the format to the OUTSTREAM."
  (find-file file)
  (bhl-mode)
  (bhl2xxx format)
  (princ (buffer-string) outstream)
  (terpri))

;; A generic function that converts BHL source files.
(defun bhl2xxx (arg)
  "Convert the BHL source file into a HTML, SGML or LaTeX file.
ARG is the output format.  OFFLINE is non-nil when BHL is called in batch
mode."
  (bhl-convert-init arg)
  (bhl-add-text-properties arg)
  (bhl-clean-title-main arg)
  (bhl-insert-preamble arg)
  (narrow-to-region (if (eq arg 'sgml)
			(search-forward "</abstract>")
		      (point)) (point-max))
  (bhl-ignore-regexp-line)
  (when (eq arg 'latex)
    (bhl-protect-chars bhl-latex-escaped-chars))
  (when (eq arg 'texi)
    (bhl-protect-chars bhl-texi-escaped-chars))
  (bhl-convert-misc-1 arg)
  (when (or (eq arg 'html) (eq arg 'sgml))
    (condition-case nil
	(bhl-convert-para arg)
      (error (bhl-add-to-conversion-log
	      "BHL was unable to convert paragraphs properly."))))
  (bhl-convert-misc-2 arg)
  (bhl-make-toc)
  (bhl-update-toc)
  (bhl-skip-toc arg)
  (bhl-convert-sec-tag arg)
  (when (nth 10 bhl-xxx-conversions-list)
    (bhl-convert-comment arg))
  (when (nth 11 bhl-xxx-conversions-list)
    (bhl-convert-escape-char arg))
  (bhl-add-to-conversion-log
   (concat "Sections' prefix: "
	   (symbol-name (get arg 'bhl-prefix)) "."))
  (widen)
  (goto-char (point-max))
  (insert (cond ((eq arg 'latex)  "\n\n\\end{document}")
		((eq arg 'html)  "\n</body>\n</html>")
		((eq arg 'sgml)  "</article>")
		((eq arg 'texi) "\n@bye")
		(t  "")))
  (run-hooks (get arg 'after-conversion-hook))
  (run-hooks 'bhl-after-conversion-hook)
  (goto-char (point-min))
  (message "Converting into %s format...done. "
	   (symbol-name arg)))

(provide 'bhl)

;;; bhl.el ends here

;; Preambule
;; Backslash cmds latex
;; insert-tab
;; bib \bibliography{mabibli} reftex