summaryrefslogtreecommitdiff
path: root/systems/doc/luatex/luatex-tex.tex
blob: dacaec0cb92df69694dbb4113f9a1222ed3aac45 (plain)
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
% language=uk

% lua.newtable

\environment luatex-style

\startcomponent luatex-tex

\startchapter[reference=tex,title={The \TEX\ related libraries}]

\startsection[title={The \type {lua} library}][library=lua]

\startsubsection[title={Version information}]

\topicindex{libraries+\type{lua}}
\topicindex{version}

\libindex{version}

This library contains one read|-|only item:

\starttyping
<string> s = lua.version
\stoptyping

This returns the \LUA\ version identifier string. The value is currently
\directlua {tex.print(lua.version)}.

\stopsubsection

\startsubsection[title={Bytecode registers}]

\topicindex{bytecodes}
\topicindex{registers+bytecodes}

\libindex{bytecode}
\libindex{setbytecode}
\libindex{getbytecode}

\LUA\ registers can be used to store \LUA\ code chunks. The accepted values for
assignments are functions and \type {nil}. Likewise, the retrieved value is
either a function or \type {nil}.

\starttyping
lua.bytecode[<number> n] = <function> f
lua.bytecode[<number> n]()
\stoptyping

The contents of the \type {lua.bytecode} array is stored inside the format file
as actual \LUA\ bytecode, so it can also be used to preload \LUA\ code. The
function must not contain any upvalues. The associated function calls are:

\startfunctioncall
<function> f = lua.getbytecode(<number> n)
lua.setbytecode(<number> n, <function> f)
\stopfunctioncall

Note: Since a \LUA\ file loaded using \type {loadfile(filename)} is essentially
an anonymous function, a complete file can be stored in a bytecode register like
this:

\startfunctioncall
lua.bytecode[n] = loadfile(filename)
\stopfunctioncall

Now all definitions (functions, variables) contained in the file can be
created by executing this bytecode register:

\startfunctioncall
lua.bytecode[n]()
\stopfunctioncall

Note that the path of the file is stored in the \LUA\ bytecode to be used in
stack backtraces and therefore dumped into the format file if the above code is
used in \INITEX. If it contains private information, i.e. the user name, this
information is then contained in the format file as well. This should be kept in
mind when preloading files into a bytecode register in \INITEX.

\stopsubsection

\startsubsection[title={Chunk name registers}]

\libindex{name}
\libindex{setluaname}
\libindex{getluaname}

There is an array of 65536 (0--65535) potential chunk names for use with the
\prm {directlua} and \lpr {latelua} primitives.

\startfunctioncall
lua.name[<number> n] = <string> s
<string> s = lua.name[<number> n]
\stopfunctioncall

If you want to unset a \LUA\ name, you can assign \type {nil} to it. The function
accessors are:

\startfunctioncall
lua.setluaname(<string> s,<number> n])
<string> s = lua.getluaname(<number> n)
\stopfunctioncall

\stopsubsection

\startsubsection[title={Introspection}]

\libindex{getstacktop}
\libindex{getcalllevel}

The \type {getstacktop} and\type {getcalllevel} functions return numbers
indicating how much nesting is going on. They are only of use as breakpoints when
checking some mechanism going haywire.

\stopsubsection

\stopsection

\startsection[title={The \type {status} library}][library=status]

\topicindex{libraries+\type{status}}

\libindex{list}
\libindex{resetmessages}
\libindex{setexitcode}

This contains a number of run|-|time configuration items that you may find useful
in message reporting, as well as an iterator function that gets all of the names
and values as a table.

\startfunctioncall
<table> info = status.list()
\stopfunctioncall

The keys in the table are the known items, the value is the current value. Almost
all of the values in \type {status} are fetched through a metatable at run|-|time
whenever they are accessed, so you cannot use \type {pairs} on \type {status},
but you {\it can\/} use \type {pairs} on \type {info}, of course. If you do not
need the full list, you can also ask for a single item by using its name as an
index into \type {status}. The current list is:

\starttabulate[|l|p|]
\DB key                       \BC explanation \NC \NR
\TB
\NC \type{banner}             \NC terminal display banner \NC \NR
\NC \type{best_page_break}    \NC the current best break (a node) \NC \NR
\NC \type{buf_size}           \NC current allocated size of the line buffer \NC \NR
\NC \type{callbacks}          \NC total number of executed callbacks so far \NC \NR
\NC \type{cs_count}           \NC number of control sequences \NC \NR
\NC \type{dest_names_size}    \NC \PDF\ destination table size \NC \NR
\NC \type{dvi_gone}           \NC written \DVI\ bytes \NC \NR
\NC \type{dvi_ptr}            \NC not yet written \DVI\ bytes \NC \NR
\NC \type{dyn_used}           \NC token (multi|-|word) memory in use  \NC \NR
\NC \type{filename}           \NC name of the current input file \NC \NR
\NC \type{fix_mem_end}        \NC maximum number of used tokens \NC \NR
\NC \type{fix_mem_min}        \NC minimum number of allocated words for tokens \NC \NR
\NC \type{fix_mem_max}        \NC maximum number of allocated words for tokens \NC \NR
\NC \type{font_ptr}           \NC number of active fonts \NC \NR
\NC \type{hash_extra}         \NC extra allowed hash \NC \NR
\NC \type{hash_size}          \NC size of hash \NC \NR
\NC \type{indirect_callbacks} \NC number of those that were themselves a result of other callbacks (e.g. file readers) \NC \NR
\NC \type{ini_version}        \NC \type {true} if this is an \INITEX\ run \NC \NR
\NC \type{init_pool_ptr}      \NC \INITEX\ string pool index \NC \NR
\NC \type{init_str_ptr}       \NC number of \INITEX\ strings \NC \NR
\NC \type{input_ptr}          \NC the level of input we're at \NC \NR
\NC \type{inputid}            \NC numeric id of the current input \NC \NR
\NC \type{largest_used_mark}  \NC max referenced marks class \NC \NR
\NC \type{lasterrorcontext}   \NC last error context string (with newlines) \NC \NR
\NC \type{lasterrorstring}    \NC last \TEX\ error string \NC \NR
\NC \type{lastluaerrorstring} \NC last \LUA\ error string \NC \NR
\NC \type{lastwarningstring}  \NC last warning tag, normally an indication of in what part\NC \NR
\NC \type{lastwarningtag}     \NC last warning string\NC \NR
\NC \type{linenumber}         \NC location in the current input file \NC \NR
\NC \type{log_name}           \NC name of the log file \NC \NR
\NC \type{luabytecode_bytes}  \NC number of bytes in \LUA\ bytecode registers \NC \NR
\NC \type{luabytecodes}       \NC number of active \LUA\ bytecode registers \NC \NR
\NC \type{luastate_bytes}     \NC number of bytes in use by \LUA\ interpreters \NC \NR
\NC \type{luatex_engine}      \NC the \LUATEX\ engine identifier \NC \NR
\NC \type{luatex_hashchars}   \NC length to which \LUA\ hashes strings ($2^n$) \NC \NR
\NC \type{luatex_hashtype}    \NC the hash method used (in \LUAJITTEX) \NC \NR
\NC \type{luatex_version}     \NC the \LUATEX\ version number \NC \NR
\NC \type{luatex_revision}    \NC the \LUATEX\ revision string \NC \NR
\NC \type{max_buf_stack}      \NC max used buffer position \NC \NR
\NC \type{max_in_stack}       \NC max used input stack entries \NC \NR
\NC \type{max_nest_stack}     \NC max used nesting stack entries \NC \NR
\NC \type{max_param_stack}    \NC max used parameter stack entries \NC \NR
\NC \type{max_save_stack}     \NC max used save stack entries \NC \NR
\NC \type{max_strings}        \NC maximum allowed strings \NC \NR
\NC \type{nest_size}          \NC nesting stack size \NC \NR
\NC \type{node_mem_usage}     \NC a string giving insight into currently used nodes \NC \NR
\NC \type{obj_ptr}            \NC max \PDF\ object pointer \NC \NR
\NC \type{obj_tab_size}       \NC \PDF\ object table size \NC \NR
\NC \type{output_active}      \NC \type {true} if the \prm {output} routine is active \NC \NR
\NC \type{output_file_name}   \NC name of the \PDF\ or \DVI\ file \NC \NR
\NC \type{param_size}         \NC parameter stack size \NC \NR
\NC \type{pdf_dest_names_ptr} \NC max \PDF\ destination pointer \NC \NR
\NC \type{pdf_gone}           \NC written \PDF\ bytes \NC \NR
\NC \type{pdf_mem_ptr}        \NC max \PDF\ memory used \NC \NR
\NC \type{pdf_mem_size}       \NC \PDF\ memory size \NC \NR
\NC \type{pdf_os_cntr}        \NC max \PDF\ object stream pointer \NC \NR
\NC \type{pdf_os_objidx}      \NC \PDF\ object stream index \NC \NR
\NC \type{pdf_ptr}            \NC not yet written \PDF\ bytes \NC \NR
\NC \type{pool_ptr}           \NC string pool index \NC \NR
\NC \type{pool_size}          \NC current size allocated for string characters \NC \NR
\NC \type{save_size}          \NC save stack size \NC \NR
\NC \type{shell_escape}       \NC \type {0} means disabled, \type {1} means anything is permitted, and \type {2} is restricted \NC \NR
\NC \type{safer_option}       \NC \type {1} means safer is enforced \NC \NR
\NC \type{kpse_used}          \NC \type {1} means that kpse is used \NC \NR
\NC \type{stack_size}         \NC input stack size \NC \NR
\NC \type{str_ptr}            \NC number of strings \NC \NR
\NC \type{total_pages}        \NC number of written pages \NC \NR
\NC \type{var_mem_max}        \NC number of allocated words for nodes \NC \NR
\NC \type{var_used}           \NC variable (one|-|word) memory in use \NC \NR
\NC \type{lc_collate}         \NC the value of \type {LC_COLLATE}  at startup time (becomes \type {C} at startup) \NC \NR
\NC \type{lc_ctype}           \NC the value of \type {LC_CTYPE}    at startup time (becomes \type {C} at startup) \NC \NR
%NC \type{lc_monetary}        \NC the value of \type {LC_MONETARY} at startup time \NC \NR
\NC \type{lc_numeric}         \NC the value of \type {LC_NUMERIC}  at startup time \NC \NR
%NC \type{lc_time}            \NC the value of \type {LC_TIME}     at startup time (becomes \type {C} at startup) \NC \NR
\LL
\stoptabulate

The error and warning messages can be wiped with the \type {resetmessages}
function. A return value can be set with \type {setexitcode}.

\stopsection

\startsection[title={The \type {tex} library}][library=tex]

\startsubsection[title={Introduction}]

\topicindex{libraries+\type{tex}}

The \type {tex} table contains a large list of virtual internal \TEX\
parameters that are partially writable.

The designation \quote {virtual} means that these items are not properly defined
in \LUA, but are only front\-ends that are handled by a metatable that operates
on the actual \TEX\ values. As a result, most of the \LUA\ table operators (like
\type {pairs} and \type {#}) do not work on such items.

At the moment, it is possible to access almost every parameter that you can use
after \prm {the}, is a single tokens or is sort of special in \TEX. This excludes
parameters that need extra arguments, like \type {\the\scriptfont}. The subset
comprising simple integer and dimension registers are writable as well as
readable (like \prm {tracingcommands} and \prm {parindent}).

\stopsubsection

\startsubsection[title={Internal parameter values, \type {set} and \type {get}}]

\topicindex{parameters+internal}

\libindex{set}           \libindex{get}

For all the parameters in this section, it is possible to access them directly
using their names as index in the \type {tex} table, or by using one of the
functions \type {tex.get} and \type {tex.set}.

The exact parameters and return values differ depending on the actual parameter,
and so does whether \type {tex.set} has any effect. For the parameters that {\em
can} be set, it is possible to use \type {global} as the first argument to \type
{tex.set}; this makes the assignment global instead of local.

\startfunctioncall
tex.set (["global",] <string> n, ...)
... = tex.get (<string> n)
\stopfunctioncall

Glue is kind of special because there are five values involved. The return value
is a \nod {glue_spec} node but when you pass \type {false} as last argument to
\type {tex.get} you get the width of the glue and when you pass \type {true} you
get all five values. Otherwise you get a node which is a copy of the internal
value so you are responsible for its freeing at the \LUA\ end. When you set a
glue quantity you can either pass a \nod {glue_spec} or upto five numbers. If
you pass \type {true} to \type {get} you get 5 values returned for a glue and
when you pass \type {false} you only get the width returned.

\subsubsection{Integer parameters}

The integer parameters accept and return \LUA\ numbers. These are read|-|write:

\starttwocolumns
\starttyping
tex.adjdemerits
tex.binoppenalty
tex.brokenpenalty
tex.catcodetable
tex.clubpenalty
tex.day
tex.defaulthyphenchar
tex.defaultskewchar
tex.delimiterfactor
tex.displaywidowpenalty
tex.doublehyphendemerits
tex.endlinechar
tex.errorcontextlines
tex.escapechar
tex.exhyphenpenalty
tex.fam
tex.finalhyphendemerits
tex.floatingpenalty
tex.globaldefs
tex.hangafter
tex.hbadness
tex.holdinginserts
tex.hyphenpenalty
tex.interlinepenalty
tex.language
tex.lastlinefit
tex.lefthyphenmin
tex.linepenalty
tex.localbrokenpenalty
tex.localinterlinepenalty
tex.looseness
tex.mag
tex.maxdeadcycles
tex.month
tex.newlinechar
tex.outputpenalty
tex.pausing
tex.postdisplaypenalty
tex.predisplaydirection
tex.predisplaypenalty
tex.pretolerance
tex.relpenalty
tex.righthyphenmin
tex.savinghyphcodes
tex.savingvdiscards
tex.showboxbreadth
tex.showboxdepth
tex.time
tex.tolerance
tex.tracingassigns
tex.tracingcommands
tex.tracinggroups
tex.tracingifs
tex.tracinglostchars
tex.tracingmacros
tex.tracingnesting
tex.tracingonline
tex.tracingoutput
tex.tracingpages
tex.tracingparagraphs
tex.tracingrestores
tex.tracingscantokens
tex.tracingstats
tex.uchyph
tex.vbadness
tex.widowpenalty
tex.year
\stoptyping
\stoptwocolumns

These are read|-|only:

\startthreecolumns
\starttyping
tex.deadcycles
tex.insertpenalties
tex.parshape
tex.prevgraf
tex.spacefactor
\stoptyping
\stopthreecolumns

\subsubsection{Dimension parameters}

The dimension parameters accept \LUA\ numbers (signifying scaled points) or
strings (with included dimension). The result is always a number in scaled
points. These are read|-|write:

\startthreecolumns
\starttyping
tex.boxmaxdepth
tex.delimitershortfall
tex.displayindent
tex.displaywidth
tex.emergencystretch
tex.hangindent
tex.hfuzz
tex.hoffset
tex.hsize
tex.lineskiplimit
tex.mathsurround
tex.maxdepth
tex.nulldelimiterspace
tex.overfullrule
tex.pagebottomoffset
tex.pageheight
tex.pageleftoffset
tex.pagerightoffset
tex.pagetopoffset
tex.pagewidth
tex.parindent
tex.predisplaysize
tex.scriptspace
tex.splitmaxdepth
tex.vfuzz
tex.voffset
tex.vsize
tex.prevdepth
tex.prevgraf
tex.spacefactor
\stoptyping
\stopthreecolumns

These are read|-|only:

\startthreecolumns
\starttyping
tex.pagedepth
tex.pagefilllstretch
tex.pagefillstretch
tex.pagefilstretch
tex.pagegoal
tex.pageshrink
tex.pagestretch
tex.pagetotal
\stoptyping
\stopthreecolumns

Beware: as with all \LUA\ tables you can add values to them. So, the following is
valid:

\starttyping
tex.foo = 123
\stoptyping

When you access a \TEX\ parameter a look up takes place. For read||only variables
that means that you will get something back, but when you set them you create a
new entry in the table thereby making the original invisible.

There are a few special cases that we make an exception for: \type {prevdepth},
\type {prevgraf} and \type {spacefactor}. These normally are accessed via the
\type {tex.nest} table:

\starttyping
tex.nest[tex.nest.ptr].prevdepth   = p
tex.nest[tex.nest.ptr].spacefactor = s
\stoptyping

However, the following also works:

\starttyping
tex.prevdepth   = p
tex.spacefactor = s
\stoptyping

Keep in mind that when you mess with node lists directly at the \LUA\ end you
might need to update the top of the nesting stack's \type {prevdepth} explicitly
as there is no way \LUATEX\ can guess your intentions. By using the accessor in
the \type {tex} tables, you get and set the values at the top of the nesting
stack.

\subsubsection{Direction parameters}

The direction parameters are read|-|only and return a \LUA\ string.

\startthreecolumns
\starttyping
tex.bodydir
tex.mathdir
tex.pagedir
tex.pardir
tex.textdir
\stoptyping
\stopthreecolumns

\subsubsection{Glue parameters}

The glue parameters accept and return a userdata object that represents a \nod {glue_spec} node.

\startthreecolumns
\starttyping
tex.abovedisplayshortskip
tex.abovedisplayskip
tex.baselineskip
tex.belowdisplayshortskip
tex.belowdisplayskip
tex.leftskip
tex.lineskip
tex.parfillskip
tex.parskip
tex.rightskip
tex.spaceskip
tex.splittopskip
tex.tabskip
tex.topskip
tex.xspaceskip
\stoptyping
\stopthreecolumns

\subsubsection{Muglue parameters}

All muglue parameters are to be used read|-|only and return a \LUA\ string.

\startthreecolumns
\starttyping
tex.medmuskip
tex.thickmuskip
tex.thinmuskip
\stoptyping
\stopthreecolumns

\subsubsection{Tokenlist parameters}

The tokenlist parameters accept and return \LUA\ strings. \LUA\ strings are
converted to and from token lists using \prm {the} \prm {toks} style expansion:
all category codes are either space (10) or other (12). It follows that assigning
to some of these, like \quote {tex.output}, is actually useless, but it feels bad
to make exceptions in view of a coming extension that will accept full|-|blown
token strings.

\startthreecolumns
\starttyping
tex.errhelp
tex.everycr
tex.everydisplay
tex.everyeof
tex.everyhbox
tex.everyjob
tex.everymath
tex.everypar
tex.everyvbox
tex.output
\stoptyping
\stopthreecolumns

\stopsubsection

\startsubsection[title={Convert commands}]

\topicindex{convert commands}

All \quote {convert} commands are read|-|only and return a \LUA\ string. The
supported commands at this moment are:

\starttwocolumns
\starttyping
tex.eTeXVersion
tex.eTeXrevision
tex.formatname
tex.jobname
tex.luatexbanner
tex.luatexrevision
tex.fontname(number)
tex.uniformdeviate(number)
tex.number(number)
tex.romannumeral(number)
tex.fontidentifier(number)
\stoptyping
\stoptwocolumns

If you are wondering why this list looks haphazard; these are all the cases of
the \quote {convert} internal command that do not require an argument, as well as
the ones that require only a simple numeric value. The special (\LUA|-|only) case
of \type {tex.fontidentifier} returns the \type {csname} string that matches a
font id number (if there is one).

\stopsubsection

\startsubsection[title={Last item commands}]

\topicindex{last items}

All \quote {last item} commands are read|-|only and return a number. The
supported commands at this moment are:

\startthreecolumns
\starttyping
tex.lastpenalty
tex.lastkern
tex.lastskip
tex.lastnodetype
tex.inputlineno
tex.lastxpos
tex.lastypos
tex.randomseed
tex.luatexversion
tex.eTeXminorversion
tex.eTeXversion
tex.currentgrouplevel
tex.currentgrouptype
tex.currentiflevel
tex.currentiftype
tex.currentifbranch
\stoptyping
\stopthreecolumns

\stopsubsection

\startsubsection[title={Accessing registers: \type {set*}, \type {get*} and \type {is*}}]

\topicindex{attributes}
\topicindex{registers}

\libindex{attribute}  \libindex{setattribute}  \libindex{getattribute}  \libindex{isattribute}
\libindex{count}      \libindex{setcount}      \libindex{getcount}      \libindex{iscount}
\libindex{dimen}      \libindex{setdimen}      \libindex{getdimen}      \libindex{isdimen}
\libindex{skip}       \libindex{setskip}       \libindex{getskip}       \libindex{isskip}
\libindex{muskip}     \libindex{setmuskip}     \libindex{getmuskip}     \libindex{ismuskip}
\libindex{glue}       \libindex{setglue}       \libindex{getglue}       \libindex{isglue}
\libindex{muglue}     \libindex{setmuglue}     \libindex{getmuglue}     \libindex{ismuglue}
\libindex{toks}       \libindex{settoks}       \libindex{gettoks}       \libindex{istoks}
\libindex{box}        \libindex{setbox}        \libindex{getbox}        \libindex{isbox}

\libindex{scantoks}

\TEX's attributes (\lpr {attribute}), counters (\prm {count}), dimensions (\prm
{dimen}), skips (\prm {skip}, \prm {muskip}) and token (\prm {toks}) registers
can be accessed and written to using two times five virtual sub|-|tables of the
\type {tex} table:

\startthreecolumns
\starttyping
tex.attribute
tex.count
tex.dimen
tex.skip
tex.glue
tex.muskip
tex.muglue
tex.toks
\stoptyping
\stopthreecolumns

It is possible to use the names of relevant \lpr {attributedef}, \prm {countdef},
\prm {dimendef}, \prm {skipdef}, or \prm {toksdef} control sequences as indices
to these tables:

\starttyping
tex.count.scratchcounter = 0
enormous = tex.dimen['maxdimen']
\stoptyping

In this case, \LUATEX\ looks up the value for you on the fly. You have to use a
valid \prm {countdef} (or \lpr {attributedef}, or \prm {dimendef}, or \prm
{skipdef}, or \prm {toksdef}), anything else will generate an error (the intent
is to eventually also allow \type {<chardef tokens>} and even macros that expand
into a number).

\startitemize

    \startitem
        The count registers accept and return \LUA\ numbers.
    \stopitem

    \startitem
        The dimension registers accept \LUA\ numbers (in scaled points) or
        strings (with an included absolute dimension; \type {em} and \type {ex}
        and \type {px} are forbidden). The result is always a number in scaled
        points.
    \stopitem

    \startitem
        The token registers accept and return \LUA\ strings. \LUA\ strings are
        converted to and from token lists using \prm {the} \prm {toks} style
        expansion: all category codes are either space (10) or other (12).
    \stopitem

    \startitem
        The skip registers accept and return \nod {glue_spec} userdata node
        objects (see the description of the node interface elsewhere in this
        manual).
    \stopitem

    \startitem
        The glue registers are just skip registers but instead of userdata
        are verbose.
    \stopitem

    \startitem
        Like the counts, the attribute registers accept and return \LUA\ numbers.
    \stopitem

\stopitemize

As an alternative to array addressing, there are also accessor functions defined
for all cases, for example, here is the set of possibilities for \prm {skip}
registers:

\startfunctioncall
tex.setskip (["global",] <number> n, <node> s)
tex.setskip (["global",] <string> s, <node> s)
<node> s = tex.getskip (<number> n)
<node> s = tex.getskip (<string> s)
\stopfunctioncall

We have similar setters for \type {count}, \type {dimen}, \type {muskip}, and
\type {toks}. Counters and dimen are represented by numbers, skips and muskips by
nodes, and toks by strings.

Again the glue variants are not using the \nod {glue_spec} userdata nodes. The
\type {setglue} function accepts upto 5 arguments: width, stretch, shrink,
stretch order and shrink order. If you pass no values or if a value is not a
number the corresponding property will become a zero. The \type {getglue}
function reports all properties, unless the second argument is \type {false} in
which care only the width is returned.

Here is an example using a threesome:

\startfunctioncall
local d = tex.getdimen("foo")
if tex.isdimen("bar") then
    tex.setdimen("bar",d)
end
\stopfunctioncall

There are six extra skip (glue) related helpers:

\startfunctioncall
tex.setglue (["global"], <number> n,
    width, stretch, shrink, stretch_order, shrink_order)
tex.setglue (["global"], <string> s,
    width, stretch, shrink, stretch_order, shrink_order)
width, stretch, shrink, stretch_order, shrink_order =
    tex.getglue (<number> n)
width, stretch, shrink, stretch_order, shrink_order =
    tex.getglue (<string> s)
\stopfunctioncall

The other two are \type {tex.setmuglue} and \type {tex.getmuglue}.

There are such helpers for \type {dimen}, \type {count}, \type {skip}, \type
{muskip}, \type {box} and \type {attribute} registers but the glue ones
are special because they have to deal with more properties.

As with the general \type {get} and \type {set} function discussed before, for
the skip registers \type {getskip} returns a node and \type {getglue} returns
numbers, while \type {setskip} accepts a node and \type {setglue} expects upto five
numbers. Again, when you pass \type {false} as second argument to \type {getglue}
you only get the width returned. The same is true for the \type {mu} variants
\type {getmuskip}, \type {setmuskip}, \type {getmuskip} and\type {setmuskip}.

For tokens registers we have an alternative where a catcode table is specified:

\startfunctioncall
tex.scantoks(0,3,"$e=mc^2$")
tex.scantoks("global",0,"$\int\limits^1_2$")
\stopfunctioncall

In the function|-|based interface, it is possible to define values globally by
using the string \type {global} as the first function argument.

\stopsubsection

\startsubsection[title={Character code registers: \type {[get|set]*code[s]}}]

\topicindex{characters+codes}

\libindex{lccode}    \libindex{setlccode}    \libindex{getlccode}
\libindex{uccode}    \libindex{setuccode}    \libindex{getuccode}
\libindex{sfcode}    \libindex{setsfcode}    \libindex{getsfcode}
\libindex{catcode}   \libindex{setcatcode}   \libindex{getcatcode}
\libindex{mathcode}  \libindex{setmathcode}  \libindex{getmathcode}
\libindex{delcode}   \libindex{setdelcode}   \libindex{getdelcode}

\libindex{setdelcodes}   \libindex{getdelcodes}
\libindex{setmathcodes}  \libindex{getmathcodes}

\TEX's character code tables (\prm {lccode}, \prm {uccode}, \prm {sfcode}, \prm
{catcode}, \prm {mathcode}, \prm {delcode}) can be accessed and written to using
six virtual subtables of the \type {tex} table

\startthreecolumns
\starttyping
tex.lccode
tex.uccode
tex.sfcode
tex.catcode
tex.mathcode
tex.delcode
\stoptyping
\stopthreecolumns

The function call interfaces are roughly as above, but there are a few twists.
\type {sfcode}s are the simple ones:

\startfunctioncall
tex.setsfcode (["global",] <number> n, <number> s)
<number> s = tex.getsfcode (<number> n)
\stopfunctioncall

The function call interface for \type {lccode} and \type {uccode} additionally
allows you to set the associated sibling at the same time:

\startfunctioncall
tex.setlccode (["global"], <number> n, <number> lc)
tex.setlccode (["global"], <number> n, <number> lc, <number> uc)
<number> lc = tex.getlccode (<number> n)
tex.setuccode (["global"], <number> n, <number> uc)
tex.setuccode (["global"], <number> n, <number> uc, <number> lc)
<number> uc = tex.getuccode (<number> n)
\stopfunctioncall

The function call interface for \type {catcode} also allows you to specify a
category table to use on assignment or on query (default in both cases is the
current one):

\startfunctioncall
tex.setcatcode (["global"], <number> n, <number> c)
tex.setcatcode (["global"], <number> cattable, <number> n, <number> c)
<number> lc = tex.getcatcode (<number> n)
<number> lc = tex.getcatcode (<number> cattable, <number> n)
\stopfunctioncall

The interfaces for \type {delcode} and \type {mathcode} use small array tables to
set and retrieve values:

\startfunctioncall
tex.setmathcode (["global"], <number> n, <table> mval )
<table> mval = tex.getmathcode (<number> n)
tex.setdelcode (["global"], <number> n, <table> dval )
<table> dval = tex.getdelcode (<number> n)
\stopfunctioncall

Where the table for \type {mathcode} is an array of 3 numbers, like this:

\starttyping
{
    <number> class,
    <number> family,
    <number> character
}
\stoptyping

And the table for \type {delcode} is an array with 4 numbers, like this:

\starttyping
{
    <number> small_fam,
    <number> small_char,
    <number> large_fam,
    <number> large_char
}
\stoptyping

You can also avoid the table:

\startfunctioncall
tex.setmathcode (["global"], <number> n, <number> class,
    <number> family, <number> character)
class, family, char =
    tex.getmathcodes (<number> n)
tex.setdelcode (["global"], <number> n, <number> smallfam,
    <number> smallchar, <number> largefam, <number> largechar)
smallfam, smallchar, largefam, largechar =
    tex.getdelcodes (<number> n)
\stopfunctioncall

Normally, the third and fourth values in a delimiter code assignment will be zero
according to \lpr {Udelcode} usage, but the returned table can have values there
(if the delimiter code was set using \prm {delcode}, for example). Unset \type
{delcode}'s can be recognized because \type {dval[1]} is $-1$.

\stopsubsection

\startsubsection[title={Box registers: \type {[get|set]box}}]

\topicindex{registers}
\topicindex{boxes}

\libindex{box}
\libindex{setbox}  \libindex{getbox}

It is possible to set and query actual boxes, coming for instance from \prm
{hbox}, \prm {vbox} or \prm {vtop}, using the node interface as defined in the
\type {node} library:

\starttyping
tex.box
\stoptyping

for array access, or

\starttyping
tex.setbox(["global",] <number> n, <node> s)
tex.setbox(["global",] <string> cs, <node> s)
<node> n = tex.getbox(<number> n)
<node> n = tex.getbox(<string> cs)
\stoptyping

for function|-|based access. In the function-based interface, it is possible to
define values globally by using the string \type {global} as the first function
argument.

Be warned that an assignment like

\starttyping
tex.box[0] = tex.box[2]
\stoptyping

does not copy the node list, it just duplicates a node pointer. If \type {\box2}
will be cleared by \TEX\ commands later on, the contents of \type {\box0} becomes
invalid as well. To prevent this from happening, always use \type
{node.copy_list} unless you are assigning to a temporary variable:

\starttyping
tex.box[0] = node.copy_list(tex.box[2])
\stoptyping

\stopsubsection

\startsubsection[title={Reusing boxes: \type {[use|save]boxresource} and \type {getboxresourcedimensions}}]

\topicindex{boxes+reuse}

\libindex{useboxresource}
\libindex{saveboxresource}
\libindex{getboxresourcedimensions}

The following function will register a box for reuse (this is modelled after so
called xforms in \PDF). You can (re)use the box with \lpr {useboxresource} or
by creating a rule node with subtype~2.

\starttyping
local index = tex.saveboxresource(n,attributes,resources,immediate,type,margin)
\stoptyping

The optional second and third arguments are strings, the fourth is a boolean. The
fifth argument is a type. When set to non|-|zero the \type {/Type} entry is
omitted. A value of 1 or 3 still writes a \type {/BBox}, while 2 or 3 will write
a \type {/Matrix}. The sixth argument is the (virtual) margin that extends beyond
the effective boundingbox as seen by \TEX. Instead of a box number one can also
pass a \type {[h|v]list} node.

You can generate the reference (a rule type) with:

\starttyping
local reused = tex.useboxresource(n,wd,ht,dp)
\stoptyping

The dimensions are optional and the final ones are returned as extra values. The
following is just a bonus (no dimensions returned means that the resource is
unknown):

\starttyping
local w, h, d, m = tex.getboxresourcedimensions(n)
\stoptyping

This returns the width, height, depth and margin of the resource.

\stopsubsection

\startsubsection[title={\type {triggerbuildpage}}]

\topicindex{pages}

\libindex{triggerbuildpage}

You should not expect to much from the \type {triggerbuildpage} helpers because
often \TEX\ doesn't do much if it thinks nothing has to be done, but it might be
useful for some applications. It just does as it says it calls the internal
function that build a page, given that there is something to build.

\stopsubsection

\startsubsection[title={\type {splitbox}}]

\topicindex{boxes+split}

\libindex{splitbox}

You can split a box:

\starttyping
local vlist = tex.splitbox(n,height,mode)
\stoptyping

The remainder is kept in the original box and a packaged vlist is returned. This
operation is comparable to the \prm {vsplit} operation. The mode can be \type
{additional} or \type {exactly} and concerns the split off box.

\stopsubsection

\startsubsection[title={Accessing math parameters: \type {[get|set]math}}]

\topicindex{math+parameters}
\topicindex{parameters+math}

\libindex{setmath}
\libindex{getmath}

It is possible to set and query the internal math parameters using:

\startfunctioncall
tex.setmath(["global",] <string> n, <string> t, <number> n)
<number> n = tex.getmath(<string> n, <string> t)
\stopfunctioncall

As before an optional first parameter \type {global} indicates a global
assignment.

The first string is the parameter name minus the leading \quote {Umath}, and the
second string is the style name minus the trailing \quote {style}. Just to be
complete, the values for the math parameter name are:

\starttyping
quad                axis                operatorsize
overbarkern         overbarrule         overbarvgap
underbarkern        underbarrule        underbarvgap
radicalkern         radicalrule         radicalvgap
radicaldegreebefore radicaldegreeafter  radicaldegreeraise
stackvgap           stacknumup          stackdenomdown
fractionrule        fractionnumvgap     fractionnumup
fractiondenomvgap   fractiondenomdown   fractiondelsize
limitabovevgap      limitabovebgap      limitabovekern
limitbelowvgap      limitbelowbgap      limitbelowkern
underdelimitervgap  underdelimiterbgap
overdelimitervgap   overdelimiterbgap
subshiftdrop        supshiftdrop        subshiftdown
subsupshiftdown     subtopmax           supshiftup
supbottommin        supsubbottommax     subsupvgap
spaceafterscript    connectoroverlapmin
ordordspacing       ordopspacing        ordbinspacing     ordrelspacing
ordopenspacing      ordclosespacing     ordpunctspacing   ordinnerspacing
opordspacing        opopspacing         opbinspacing      oprelspacing
opopenspacing       opclosespacing      oppunctspacing    opinnerspacing
binordspacing       binopspacing        binbinspacing     binrelspacing
binopenspacing      binclosespacing     binpunctspacing   bininnerspacing
relordspacing       relopspacing        relbinspacing     relrelspacing
relopenspacing      relclosespacing     relpunctspacing   relinnerspacing
openordspacing      openopspacing       openbinspacing    openrelspacing
openopenspacing     openclosespacing    openpunctspacing  openinnerspacing
closeordspacing     closeopspacing      closebinspacing   closerelspacing
closeopenspacing    closeclosespacing   closepunctspacing closeinnerspacing
punctordspacing     punctopspacing      punctbinspacing   punctrelspacing
punctopenspacing    punctclosespacing   punctpunctspacing punctinnerspacing
innerordspacing     inneropspacing      innerbinspacing   innerrelspacing
inneropenspacing    innerclosespacing   innerpunctspacing innerinnerspacing
\stoptyping

The values for the style parameter are:

\starttyping
display       crampeddisplay
text          crampedtext
script        crampedscript
scriptscript  crampedscriptscript
\stoptyping

The value is either a number (representing a dimension or number) or a glue spec
node representing a muskip for \type {ordordspacing} and similar spacing
parameters.

\stopsubsection

\startsubsection[title={Special list heads: \type {[get|set]list}}]

\topicindex{lists}

\libindex{lists}
\libindex{setlist}
\libindex{getlist}

The virtual table \type {tex.lists} contains the set of internal registers that
keep track of building page lists.

\starttabulate[|l|p|]
\DB field                      \BC explanation \NC \NR
\TB
\NC \type{page_ins_head}       \NC circular list of pending insertions \NC \NR
\NC \type{contrib_head}        \NC the recent contributions \NC \NR
\NC \type{page_head}           \NC the current page content \NC \NR
%NC \type{temp_head}           \NC \NC \NR
\NC \type{hold_head}           \NC used for held-over items for next page \NC \NR
\NC \type{adjust_head}         \NC head of the current \prm {vadjust} list \NC \NR
\NC \type{pre_adjust_head}     \NC head of the current \type {\vadjust pre} list \NC \NR
%NC \type{align_head}          \NC \NC \NR
\NC \type{page_discards_head}  \NC head of the discarded items of a page break \NC \NR
\NC \type{split_discards_head} \NC head of the discarded items in a vsplit \NC \NR
\LL
\stoptabulate

The getter and setter functions are \type {getlist} and \type {setlist}. You have
to be careful with what you set as \TEX\ can have expectations with regards to
how a list is constructed or in what state it is.

\stopsubsection

\startsubsection[title={Semantic nest levels: \type {getnest} and \type {ptr}}]

\topicindex{nesting}

\libindex{nest}
\libindex{ptr}
%libindex{setnest} % only a message
\libindex{getnest}

The virtual table \type {nest} contains the currently active semantic nesting
state. It has two main parts: a zero-based array of userdata for the semantic
nest itself, and the numerical value \type {ptr}, which gives the highest
available index. Neither the array items in \type {nest[]} nor \type {ptr} can be
assigned to (as this would confuse the typesetting engine beyond repair), but you
can assign to the individual values inside the array items, e.g.\ \type
{tex.nest[tex.nest.ptr].prevdepth}.

\type {tex.nest[tex.nest.ptr]} is the current nest state, \type {nest[0]} the
outermost (main vertical list) level. The getter function is \type {getnest}. You
can pass a number (which gives you a list), nothing or \type {top}, which returns
the topmost list, or the string \type {ptr} which gives you the index of the
topmost list.

The known fields are:

\starttabulate[|l|l|l|p|]
\DB key                \BC type    \BC modes \BC explanation \NC \NR
\TB
\NC \type{mode}        \NC number  \NC all   \NC the meaning of these numbers depends on the engine
                                                 and sometimes even the version; you can use \typ
                                                 {tex.getmodevalues()} to get the mapping: positive
                                                 values signal vertical, horizontal and math mode,
                                                 while negative values indicate inner and inline
                                                 variants \NC \NR
\NC \type{modeline}    \NC number  \NC all   \NC source input line where this mode was entered in,
                                                 negative inside the output routine \NC \NR
\NC \type{head}        \NC node    \NC all   \NC the head of the current list \NC \NR
\NC \type{tail}        \NC node    \NC all   \NC the tail of the current list \NC \NR
\NC \type{prevgraf}    \NC number  \NC vmode \NC number of lines in the previous paragraph \NC \NR
\NC \type{prevdepth}   \NC number  \NC vmode \NC depth of the previous paragraph \NC \NR
\NC \type{spacefactor} \NC number  \NC hmode \NC the current space factor \NC \NR
\NC \type{dirs}        \NC node    \NC hmode \NC used for temporary storage by the line break algorithm\NC \NR
\NC \type{noad}        \NC node    \NC mmode \NC used for temporary storage of a pending fraction numerator,
                                                 for \prm {over} etc. \NC \NR
\NC \type{delimptr}    \NC node    \NC mmode \NC used for temporary storage of the previous math delimiter,
                                                 for \prm {middle} \NC \NR
\NC \type{mathdir}     \NC boolean \NC mmode \NC true when during math processing the \lpr {mathdir} is not
                                                 the same as the surrounding \lpr {textdir} \NC \NR
\NC \type{mathstyle}   \NC number  \NC mmode \NC the current \lpr {mathstyle} \NC \NR
\LL
\stoptabulate

\stopsubsection

\startsubsection[reference=sec:luaprint,title={Print functions}]

\topicindex{printing}

The \type {tex} table also contains the three print functions that are the major
interface from \LUA\ scripting to \TEX. The arguments to these three functions
are all stored in an in|-|memory virtual file that is fed to the \TEX\ scanner as
the result of the expansion of \prm {directlua}.

The total amount of returnable text from a \prm {directlua} command is only
limited by available system \RAM. However, each separate printed string has to
fit completely in \TEX's input buffer. The result of using these functions from
inside callbacks is undefined at the moment.

\subsubsection{\type {print}}

\libindex{print}

\startfunctioncall
tex.print(<string> s, ...)
tex.print(<number> n, <string> s, ...)
tex.print(<table> t)
tex.print(<number> n, <table> t)
\stopfunctioncall

Each string argument is treated by \TEX\ as a separate input line. If there is a
table argument instead of a list of strings, this has to be a consecutive array
of strings to print (the first non-string value will stop the printing process).

The optional parameter can be used to print the strings using the catcode regime
defined by \lpr {catcodetable}~\type {n}. If \type {n} is $-1$, the currently
active catcode regime is used. If \type {n} is $-2$, the resulting catcodes are
the result of \prm {the} \prm {toks}: all category codes are 12 (other) except for
the space character, that has category code 10 (space). Otherwise, if \type {n}
is not a valid catcode table, then it is ignored, and the currently active
catcode regime is used instead.

The very last string of the very last \type {tex.print} command in a \prm
{directlua} will not have the \prm {endlinechar} appended, all others do.

\subsubsection{\type {sprint}}

\libindex{sprint}

\startfunctioncall
tex.sprint(<string> s, ...)
tex.sprint(<number> n, <string> s, ...)
tex.sprint(<table> t)
tex.sprint(<number> n, <table> t)
\stopfunctioncall

Each string argument is treated by \TEX\ as a special kind of input line that
makes it suitable for use as a partial line input mechanism:

\startitemize[packed]
\startitem
    \TEX\ does not switch to the \quote {new line} state, so that leading spaces
    are not ignored.
\stopitem
\startitem
    No \prm {endlinechar} is inserted.
\stopitem
\startitem
    Trailing spaces are not removed. Note that this does not prevent \TEX\ itself
    from eating spaces as result of interpreting the line. For example, in

    \starttyping
    before\directlua{tex.sprint("\\relax")tex.sprint(" inbetween")}after
    \stoptyping

    the space before \type {in between} will be gobbled as a result of the \quote
    {normal} scanning of \prm {relax}.
\stopitem
\stopitemize

If there is a table argument instead of a list of strings, this has to be a
consecutive array of strings to print (the first non-string value will stop the
printing process).

The optional argument sets the catcode regime, as with \type {tex.print}. This
influences the string arguments (or numbers turned into strings).

Although this needs to be used with care, you can also pass token or node
userdata objects. These get injected into the stream. Tokens had best be valid
tokens, while nodes need to be around when they get injected. Therefore it is
important to realize the following:

\startitemize
\startitem
    When you inject a token, you need to pass a valid token userdata object. This
    object will be collected by \LUA\ when it no longer is referenced. When it gets
    printed to \TEX\ the token itself gets copied so there is no interference with the
    \LUA\ garbage collection. You manage the object yourself. Because tokens are
    actually just numbers, there is no real extra overhead at the \TEX\ end.
\stopitem
\startitem
    When you inject a node, you need to pass a valid node userdata object. The
    node related to the object will not be collected by \LUA\ when it no longer
    is referenced. It lives on at the \TEX\ end in its own memory space. When it
    gets printed to \TEX\ the node reference is used assuming that node stays
    around. There is no \LUA\ garbage collection involved. Again, you manage the
    object yourself. The node itself is freed when \TEX\ is done with it.
\stopitem
\stopitemize

If you consider the last remark you might realize that we have a problem when a
printed mix of strings, tokens and nodes is reused. Inside \TEX\ the sequence
becomes a linked list of input buffers. So, \type {"123"} or \type {"\foo{123}"}
gets read and parsed on the fly, while \typ {<token userdata>} already is
tokenized and effectively is a token list now. A \typ {<node userdata>} is also
tokenized into a token list but it has a reference to a real node. Normally this
goes fine. But now assume that you store the whole lot in a macro: in that case
the tokenized node can be flushed many times. But, after the first such flush the
node is used and its memory freed. You can prevent this by using copies which is
controlled by setting \lpr {luacopyinputnodes} to a non|-|zero value. This is one
of these fuzzy areas you have to live with if you really mess with these low
level issues.

\subsubsection{\type {tprint}}

\libindex{tprint}

\startfunctioncall
tex.tprint({<number> n, <string> s, ...}, {...})
\stopfunctioncall

This function is basically a shortcut for repeated calls to \type
{tex.sprint(<number> n, <string> s, ...)}, once for each of the supplied argument
tables.

\subsubsection{\type {cprint}}

\libindex{cprint}

This function takes a number indicating the to be used catcode, plus either a
table of strings or an argument list of strings that will be pushed into the
input stream.

\startfunctioncall
tex.cprint( 1," 1: $&{\\foo}") tex.print("\\par") -- a lot of \bgroup s
tex.cprint( 2," 2: $&{\\foo}") tex.print("\\par") -- matching \egroup s
tex.cprint( 9," 9: $&{\\foo}") tex.print("\\par") -- all get ignored
tex.cprint(10,"10: $&{\\foo}") tex.print("\\par") -- all become spaces
tex.cprint(11,"11: $&{\\foo}") tex.print("\\par") -- letters
tex.cprint(12,"12: $&{\\foo}") tex.print("\\par") -- other characters
tex.cprint(14,"12: $&{\\foo}") tex.print("\\par") -- comment triggers
\stopfunctioncall

% \subsubsection{\type {write}, \type {twrite}, \type {nwrite}}
\subsubsection{\type {write}}

\libindex{write}
% \libindex{twrite}
% \libindex{nwrite}

\startfunctioncall
tex.write(<string> s, ...)
tex.write(<table> t)
\stopfunctioncall

Each string argument is treated by \TEX\ as a special kind of input line that
makes it suitable for use as a quick way to dump information:

\startitemize
\item All catcodes on that line are either \quote{space} (for '~') or \quote
      {character} (for all others).
\item There is no \prm {endlinechar} appended.
\stopitemize

If there is a table argument instead of a list of strings, this has to be a
consecutive array of strings to print (the first non-string value will stop the
printing process).

% The functions \type {twrite} and \type {nwrite} can be used to write a token or
% node back to \TEX\, possibly intermixed with regular strings that will be
% tokenized. You have to make sure that you pass the right data because sometimes
% \TEX\ has expectations that need to be met.

\stopsubsection

\startsubsection[title={Helper functions}]

\subsubsection{\type {round}}

\topicindex {helpers}

\libindex{round}

\startfunctioncall
<number> n = tex.round(<number> o)
\stopfunctioncall

Rounds \LUA\ number \type {o}, and returns a number that is in the range of a
valid \TEX\ register value. If the number starts out of range, it generates a
\quote {number too big} error as well.

\subsubsection{\type {scale}}

\libindex{scale}

\startfunctioncall
<number> n = tex.scale(<number> o, <number> delta)
<table> n = tex.scale(table o, <number> delta)
\stopfunctioncall

Multiplies the \LUA\ numbers \type {o} and \nod {delta}, and returns a rounded
number that is in the range of a valid \TEX\ register value. In the table
version, it creates a copy of the table with all numeric top||level values scaled
in that manner. If the multiplied number(s) are of range, it generates
\quote{number too big} error(s) as well.

Note: the precision of the output of this function will depend on your computer's
architecture and operating system, so use with care! An interface to \LUATEX's
internal, 100\% portable scale function will be added at a later date.

\subsubsection{\type {number} and \type {romannumeral}}

\libindex{number}
\libindex{romannumeral}

These are the companions to the primitives \prm {number} and \prm
{romannumeral}. They can be used like:

\startfunctioncall
tex.print(tex.romannumeral(123))
\stopfunctioncall

\subsubsection{\type {fontidentifier} and \type {fontname}}

\libindex{fontidentifier}
\libindex{fontname}

The first one returns the name only, the second one reports the size too.

\startfunctioncall
tex.print(tex.fontname(tex.fontname))
tex.print(tex.fontname(tex.fontidentidier))
\stopfunctioncall

\subsubsection{\type {sp}}

\libindex{sp}

\startfunctioncall
<number> n = tex.sp(<number> o)
<number> n = tex.sp(<string> s)
\stopfunctioncall

Converts the number \type {o} or a string \type {s} that represents an explicit
dimension into an integer number of scaled points.

For parsing the string, the same scanning and conversion rules are used that
\LUATEX\ would use if it was scanning a dimension specifier in its \TEX|-|like
input language (this includes generating errors for bad values), expect for the
following:

\startitemize[n]
\startitem
    only explicit values are allowed, control sequences are not handled
\stopitem
\startitem
    infinite dimension units (\type {fil...}) are forbidden
\stopitem
\startitem
    \type {mu} units do not generate an error (but may not be useful either)
\stopitem
\stopitemize

\subsubsection{\type {tex.getlinenumber} and \type {tex.setlinenumber}}

\libindex{getlinenumber}
\libindex{setlinenumber}

You can mess with the current line number:

\startfunctioncall
local n = tex.getlinenumber()
tex.setlinenumber(n+10)
\stopfunctioncall

which can be shortcut to:

\startfunctioncall
tex.setlinenumber(10,true)
\stopfunctioncall

This might be handy when you have a callback that read numbers from a file and
combines them in one line (in which case an error message probably has to refer
to the original line). Interference with \TEX's internal handling of numbers is
of course possible.

\subsubsection{\type {error} and \type {show_context}}

\topicindex{errors}

\libindex{error}
\libindex{show_context}

\startfunctioncall
tex.error(<string> s)
tex.error(<string> s, <table> help)
\stopfunctioncall

This creates an error somewhat like the combination of \prm {errhelp} and \prm
{errmessage} would. During this error, deletions are disabled.

The array part of the \type {help} table has to contain strings, one for each
line of error help.

In case of an error the \type {show_context} function will show the current
context where we're at (in the expansion).

\subsubsection{\type {run}, \type {finish}}

\libindex{run}
\libindex{finish}

These two functions start the interpretations and force its end. A runs normally
boils down to \TEX\ entering the so called main loop. A token is fetched and
depending on it current meaning some actions takes place. Sometimes that actions
comes immediately, sometimes more scanning is needed. Quite often tokens get
pushed back into the input. This all means that the \TEX\ scanner is constantly
pushing and popping input states, but in the end after all the action is done
returns to the main loop.

\subsubsection{\type {runtoks}}

Because of the fact that \TEX\ is in a complex dance of expanding, dealing with
fonts, typesetting paragraphs, messing around with boxes, building pages, and so
on, you cannot easily run a nested \TEX\ run (read nested main loop). However,
there is an option to force a local run with \type {runtoks}. The content of the
given token list register gets expanded locally after which we return to where we
triggered this expansion, at the \LUA\ end. Instead a function can get passed
that does some work. You have to make sure that at the end \TEX\ is in a sane
state and this is not always trivial. A more complex mechanism would complicate
\TEX\ itself (and probably also harm performance) so this simple local expansion
loop has to do.

\startfunctioncall
tex.runtoks(<token register>)
tex.runtoks(<lua function>)
\stopfunctioncall

When the \prm {tracingnesting} parameter is set to a value larger than~2 some
information is reported about the state of the local loop.

This function has two optional arguments in case a token register is passed:

\startfunctioncall
tex.runtoks(<token register>,force,grouped)
\stopfunctioncall

Inside for instance an \type {\edef} the \type {runtoks} function behaves (at
least tries to) like it were an \type {\the}. This prevents unwanted side
effects: normally in such an definition tokens remain tokens and (for instance)
characters don't become nodes. With the second argument you can force the local
main loop, no matter what. The third argument adds a level of grouping.

You can quit the local loop with \type {\endlocalcontrol} or from the \LUA\ end
with \type {tex.quittoks}. In that case you end one level up! Of course in the
end that can mean that you arrive at the main level in which case an extra end
will trigger a redundancy warning (not an abort!).

\subsubsection{\type {forcehmode}}

\libindex{forcehmode}

An example of a (possible error triggering) complication is that \TEX\ expects to
be in some state, say horizontal mode, and you have to make sure it is when you
start feeding back something from \LUA\ into \TEX. Normally a user will not run
into issues but when you start writing tokens or nodes or have a nested run there
can be situations that you need to run \type {forcehmode}. There is no recipe for
this and intercepting possible cases would weaken \LUATEX's flexibility.

\subsubsection{\type {hashtokens}}

\libindex{hashtokens}

\topicindex{hash}

\startfunctioncall
for i,v in pairs (tex.hashtokens()) do ... end
\stopfunctioncall

Returns a list of names. This can be useful for debugging, but note that this
also reports control sequences that may be unreachable at this moment due to
local redefinitions: it is strictly a dump of the hash table. You can use \type
{token.create} to inspect properties, for instance when the \type {command} key
in a created table equals \type {123}, you have the \type {cmdname} value \type
{undefined_cs}.

\subsubsection{\type {definefont}}

\topicindex{fonts+defining}

\libindex{definefont}

\startfunctioncall
tex.definefont(<string> csname, <number> fontid)
tex.definefont(<boolean> global, <string> csname, <number> fontid)
\stopfunctioncall

Associates \type {csname} with the internal font number \type {fontid}. The
definition is global if (and only if) \type {global} is specified and true (the
setting of \type {globaldefs} is not taken into account).

\stopsubsection

\startsubsection[reference=luaprimitives,title={Functions for dealing with primitives}]

\subsubsection{\type {enableprimitives}}

\libindex{enableprimitives}

\topicindex{initialization}
\topicindex{primitives}

\startfunctioncall
tex.enableprimitives(<string> prefix, <table> primitive names)
\stopfunctioncall

This function accepts a prefix string and an array of primitive names. For each
combination of \quote {prefix} and \quote {name}, the \type
{tex.enableprimitives} first verifies that \quote {name} is an actual primitive
(it must be returned by one of the \type {tex.extraprimitives} calls explained
below, or part of \TEX82, or \prm {directlua}). If it is not, \type
{tex.enableprimitives} does nothing and skips to the next pair.

But if it is, then it will construct a csname variable by concatenating the
\quote {prefix} and \quote {name}, unless the \quote {prefix} is already the
actual prefix of \quote {name}. In the latter case, it will discard the \quote
{prefix}, and just use \quote {name}.

Then it will check for the existence of the constructed csname. If the csname is
currently undefined (note: that is not the same as \prm {relax}), it will
globally define the csname to have the meaning: run code belonging to the
primitive \quote {name}. If for some reason the csname is already defined, it
does nothing and tries the next pair.

An example:

\starttyping
tex.enableprimitives('LuaTeX', {'formatname'})
\stoptyping

will define \type {\LuaTeXformatname} with the same intrinsic meaning as the
documented primitive \lpr {formatname}, provided that the control sequences \type
{\LuaTeXformatname} is currently undefined.

When \LUATEX\ is run with \type {--ini} only the \TEX82 primitives and \prm
{directlua} are available, so no extra primitives {\bf at all}.

If you want to have all the new functionality available using their default
names, as it is now, you will have to add

\starttyping
\ifx\directlua\undefined \else
    \directlua {tex.enableprimitives('',tex.extraprimitives ())}
\fi
\stoptyping

near the beginning of your format generation file. Or you can choose different
prefixes for different subsets, as you see fit.

Calling some form of \type {tex.enableprimitives} is highly important though,
because if you do not, you will end up with a \TEX82-lookalike that can run \LUA\
code but not do much else. The defined csnames are (of course) saved in the
format and will be available at runtime.

\subsubsection{\type {extraprimitives}}

\libindex{extraprimitives}

\startfunctioncall
<table> t = tex.extraprimitives(<string> s, ...)
\stopfunctioncall

This function returns a list of the primitives that originate from the engine(s)
given by the requested string value(s). The possible values and their (current)
return values are given in the following table. In addition the somewhat special
primitives \quote{\tex{ }}, \quote{\tex {/}} and \quote{\type {-}} are defined.

\startluacode
function document.showprimitives(tag)
    local t = tex.extraprimitives(tag)
    table.sort(t)
    for i=1,#t do
        local v = t[i]
        if v ~= ' ' and v ~= "/" and v ~= "-" then
            context.type(v)
            context.space()
        end
    end
end
\stopluacode

\starttabulate[|l|pl|]
\DB name    \BC values \NC \NR
\TB
\NC tex     \NC \ctxlua{document.showprimitives('tex')    } \NC \NR
\NC core    \NC \ctxlua{document.showprimitives('core')   } \NC \NR
\NC etex    \NC \ctxlua{document.showprimitives('etex')   } \NC \NR
\NC luatex  \NC \ctxlua{document.showprimitives('luatex') } \NC \NR
\LL
\stoptabulate

Note that \type {luatex} does not contain \type {directlua}, as that is
considered to be a core primitive, along with all the \TEX82 primitives, so it is
part of the list that is returned from \type {'core'}.

Running \type {tex.extraprimitives} will give you the complete list of
primitives \type {-ini} startup. It is exactly equivalent to \type
{tex.extraprimitives("etex","luatex")}.

\subsubsection{\type {primitives}}

\libindex{primitives}

\startfunctioncall
<table> t = tex.primitives()
\stopfunctioncall

This function returns a list of all primitives that \LUATEX\ knows about.

\stopsubsection

\startsubsection[title={Core functionality interfaces}]

\subsubsection{\type {badness}}

\libindex{badness}

\startfunctioncall
<number> b = tex.badness(<number> t, <number> s)
\stopfunctioncall

This helper function is useful during linebreak calculations. \type {t} and \type
{s} are scaled values; the function returns the badness for when total \type {t}
is supposed to be made from amounts that sum to \type {s}. The returned number is
a reasonable approximation of \mathematics {100(t/s)^3};

\subsubsection{\type {tex.resetparagraph}}

\topicindex {paragraphs+reset}

\libindex{resetparagraph}

This function resets the parameters that \TEX\ normally resets when a new paragraph
is seen.

\subsubsection{\type {linebreak}}

\topicindex {linebreaks}

\libindex{linebreak}

\startfunctioncall
local <node> nodelist, <table> info =
    tex.linebreak(<node> listhead, <table> parameters)
\stopfunctioncall

The understood parameters are as follows:

\starttabulate[|l|l|p|]
\DB name                        \BC type            \BC explanation \NC \NR
\TB
\NC \type{pardir}               \NC string          \NC \NC \NR
\NC \type{pretolerance}         \NC number          \NC \NC \NR
\NC \type{tracingparagraphs}    \NC number          \NC \NC \NR
\NC \type{tolerance}            \NC number          \NC \NC \NR
\NC \type{looseness}            \NC number          \NC \NC \NR
\NC \type{hyphenpenalty}        \NC number          \NC \NC \NR
\NC \type{exhyphenpenalty}      \NC number          \NC \NC \NR
\NC \type{pdfadjustspacing}     \NC number          \NC \NC \NR
\NC \type{adjdemerits}          \NC number          \NC \NC \NR
\NC \type{pdfprotrudechars}     \NC number          \NC \NC \NR
\NC \type{linepenalty}          \NC number          \NC \NC \NR
\NC \type{lastlinefit}          \NC number          \NC \NC \NR
\NC \type{doublehyphendemerits} \NC number          \NC \NC \NR
\NC \type{finalhyphendemerits}  \NC number          \NC \NC \NR
\NC \type{hangafter}            \NC number          \NC \NC \NR
\NC \type{interlinepenalty}     \NC number or table \NC if a table, then it is an array like \prm {interlinepenalties} \NC \NR
\NC \type{clubpenalty}          \NC number or table \NC if a table, then it is an array like \prm {clubpenalties} \NC \NR
\NC \type{widowpenalty}         \NC number or table \NC if a table, then it is an array like \prm {widowpenalties} \NC \NR
\NC \type{brokenpenalty}        \NC number          \NC \NC \NR
\NC \type{emergencystretch}     \NC number          \NC in scaled points \NC \NR
\NC \type{hangindent}           \NC number          \NC in scaled points \NC \NR
\NC \type{hsize}                \NC number          \NC in scaled points \NC \NR
\NC \type{leftskip}             \NC glue_spec node  \NC \NC \NR
\NC \type{rightskip}            \NC glue_spec node  \NC \NC \NR
\NC \type{parshape}             \NC table           \NC \NC \NR
\LL
\stoptabulate

Note that there is no interface for \prm {displaywidowpenalties}, you have to
pass the right choice for \type {widowpenalties} yourself.

It is your own job to make sure that \type {listhead} is a proper paragraph list:
this function does not add any nodes to it. To be exact, if you want to replace
the core line breaking, you may have to do the following (when you are not
actually working in the \cbk {pre_linebreak_filter} or \cbk {linebreak_filter}
callbacks, or when the original list starting at listhead was generated in
horizontal mode):

\startitemize
\startitem
    add an \quote {indent box} and perhaps a \nod {local_par} node at the start
    (only if you need them)
\stopitem
\startitem
    replace any found final glue by an infinite penalty (or add such a penalty,
    if the last node is not a glue)
\stopitem
\startitem
    add a glue node for the \prm {parfillskip} after that penalty node
\stopitem
\startitem
    make sure all the \type {prev} pointers are OK
\stopitem
\stopitemize

The result is a node list, it still needs to be vpacked if you want to assign it
to a \prm {vbox}. The returned \type {info} table contains four values that are
all numbers:

\starttabulate[|l|p|]
\DB name      \BC explanation \NC \NR
\TB
\NC prevdepth \NC depth of the last line in the broken paragraph \NC \NR
\NC prevgraf  \NC number of lines in the broken paragraph \NC \NR
\NC looseness \NC the actual looseness value in the broken paragraph \NC \NR
\NC demerits  \NC the total demerits of the chosen solution  \NC \NR
\LL
\stoptabulate

Note there are a few things you cannot interface using this function: You cannot
influence font expansion other than via \type {pdfadjustspacing}, because the
settings for that take place elsewhere. The same is true for hbadness and hfuzz
etc. All these are in the \type {hpack} routine, and that fetches its own
variables via globals.

\subsubsection{\type {shipout}}

\topicindex {shipout}

\libindex{shipout}

\startfunctioncall
tex.shipout(<number> n)
\stopfunctioncall

Ships out box number \type {n} to the output file, and clears the box register.

\subsubsection{\type {getpagestate}}

\topicindex {pages}

\libindex{getpagestate}

This helper reports the current page state: \type {empty}, \type {box_there} or
\type {inserts_only} as integer value.

\subsubsection{\type {getlocallevel}}

\topicindex {nesting}

\libindex{getlocallevel}

This integer reports the current level of the local loop. It's only useful for
debugging and the (relative state) numbers can change with the implementation.

\stopsubsection

\startsubsection[title={Randomizers}]

\libindex{lua_math_random}
\libindex{lua_math_randomseed}
\libindex{init_rand}
\libindex{normal_rand}
\libindex{uniform_rand}
\libindex{uniformdeviate}

For practical reasons \LUATEX\ has its own random number generator. The original
\LUA\ random function is available as \typ {tex.lua_math_random}. You can
initialize with a new seed with \type {init_rand} (\typ {lua_math_randomseed} is
equivalent to this one.

There are three generators: \type {normal_rand} (no argument is used), \type
{uniform_rand} (takes a number that will get rounded before being used) and \type
{uniformdeviate} which behaves like the primitive and expects a scaled integer, so

\startfunctioncall
tex.print(tex.uniformdeviate(65536)/65536)
\stopfunctioncall

will give a random number between zero and one.

\stopsubsection

\startsubsection[reference=synctex,title={Functions related to synctex}]

\topicindex {synctex}

\libindex{set_synctex_mode}      \libindex{get_synctex_mode}
\libindex{set_synctex_no_files}
\libindex{set_synctex_tag}       \libindex{get_synctex_tag}   \libindex{force_synctex_tag}
\libindex{set_synctex_line}      \libindex{get_synctex_line}  \libindex{force_synctex_line}

The next helpers only make sense when you implement your own synctex logic. Keep in
mind that the library used in editors assumes a certain logic and is geared for
plain and \LATEX, so after a decade users expect a certain behaviour.

\starttabulate[|l|p|]
\DB name                        \BC explanation \NC \NR
\TB
\NC \type{set_synctex_mode}     \NC \type {0} is the default and used normal synctex
                                    logic, \type {1} uses the values set by the next
                                    helpers while \type {2} also sets these for glyph
                                    nodes; \type{3} sets glyphs and glue and \type {4}
                                    sets only glyphs \NC \NR
\NC \type{set_synctex_tag}      \NC set the current tag (file) value (obeys save stack) \NC \NR
\NC \type{set_synctex_line}     \NC set the current line value (obeys save stack) \NC \NR
\NC \type{set_synctex_no_files} \NC disable synctex file logging \NC \NR
\NC \type{get_synctex_mode}     \NC returns the current mode (for values see above) \NC \NR
\NC \type{get_synctex_tag}      \NC get the currently set value of tag (file) \NC \NR
\NC \type{get_synctex_line}     \NC get the currently set value of line \NC \NR
\NC \type{force_synctex_tag}    \NC overload the tag (file) value (\type {0} resets) \NC \NR
\NC \type{force_synctex_line}   \NC overload the line value  (\type {0} resets) \NC \NR
\LL
\stoptabulate

The last one is somewhat special. Due to the way files are registered in \SYNCTEX\ we need
to explicitly disable that feature if we provide our own alternative if we want to avoid
that overhead. Passing a value of 1 disables registering.

\stopsubsection

\stopsection

\startsection[title={The \type {texconfig} table},reference=texconfig][library=texconfig]

\topicindex{libraries+\type{texconfig}}

\topicindex {configuration}

This is a table that is created empty. A startup \LUA\ script could
fill this table with a number of settings that are read out by
the executable after loading and executing the startup file.

\starttabulate[|l|l|l|p|]
\DB key                   \BC type     \BC default \BC explanation \NC \NR
\TB
\NC \type{kpse_init}      \NC boolean  \NC true
\NC
    \type {false} totally disables \KPATHSEA\ initialisation, and enables
    interpretation of the following numeric key--value pairs. (only ever unset
    this if you implement {\it all\/} file find callbacks!)
\NC \NR
\NC
    \type{shell_escape} \NC string \NC \type {'f'} \NC Use \type {'y'} or \type
    {'t'} or \type {'1'} to enable \type {\write18} unconditionally, \type {'p'}
    to enable the commands that are listed in \type {shell_escape_commands}
\NC \NR
\NC
    shell_escape_commands \NC string \NC \NC Comma-separated list of command
    names that may be executed by \type {\write18} even if \type {shell_escape}
    is set to \type {'p'}. Do {\it not\/} use spaces around commas, separate any
    required command arguments by using a space, and use the \ASCII\ double quote
    (\type {"}) for any needed argument or path quoting
\NC \NR
\NC \type{string_vacancies} \NC number   \NC  75000  \NC cf.\ web2c docs \NC \NR
\NC \type{pool_free}        \NC number   \NC   5000  \NC cf.\ web2c docs \NC \NR
\NC \type{max_strings}      \NC number   \NC  15000  \NC cf.\ web2c docs \NC \NR
\NC \type{strings_free}     \NC number   \NC    100  \NC cf.\ web2c docs \NC \NR
\NC \type{nest_size}        \NC number   \NC     50  \NC cf.\ web2c docs \NC \NR
\NC \type{max_in_open}      \NC number   \NC     15  \NC cf.\ web2c docs \NC \NR
\NC \type{param_size}       \NC number   \NC     60  \NC cf.\ web2c docs \NC \NR
\NC \type{save_size}        \NC number   \NC   4000  \NC cf.\ web2c docs \NC \NR
\NC \type{stack_size}       \NC number   \NC    300  \NC cf.\ web2c docs \NC \NR
\NC \type{dvi_buf_size}     \NC number   \NC  16384  \NC cf.\ web2c docs \NC \NR
\NC \type{error_line}       \NC number   \NC     79  \NC cf.\ web2c docs \NC \NR
\NC \type{half_error_line}  \NC number   \NC     50  \NC cf.\ web2c docs \NC \NR
\NC \type{max_print_line}   \NC number   \NC     79  \NC cf.\ web2c docs \NC \NR
\NC \type{hash_extra}       \NC number   \NC      0  \NC cf.\ web2c docs \NC \NR
\NC \type{pk_dpi}           \NC number   \NC     72  \NC cf.\ web2c docs \NC \NR
\NC \type{trace_file_names} \NC boolean  \NC true
\NC
    \type {false} disables \TEX's normal file open|-|close feedback (the
    assumption is that callbacks will take care of that)
\NC \NR
\NC \type{file_line_error}  \NC boolean  \NC false
\NC
    do \type {file:line} style error messages
\NC \NR
\NC \type{halt_on_error}    \NC boolean  \NC false
\NC
    abort run on the first encountered error
\NC \NR
\NC \type{formatname}       \NC string   \NC
\NC
    if no format name was given on the command line, this key will be tested first
    instead of simply quitting
\NC \NR
\NC \type{jobname}        \NC string   \NC
\NC
    if no input file name was given on the command line, this key will be tested
    first instead of simply giving up
\NC \NR
\LL
\stoptabulate

Note: the numeric values that match web2c parameters are only used if \type
{kpse_init} is explicitly set to \type {false}. In all other cases, the normal
values from \type {texmf.cnf} are used.

\stopsection

\startsection[title={The \type {texio} library}][library=texio]

\topicindex{libraries+\type{texio}}
\topicindex{\IO}

This library takes care of the low|-|level I/O interface: writing to the log file
and|/|or console.

\startsubsection[title={\type {write}}]

\libindex{write}

\startfunctioncall
texio.write(<string> target | <integer> stream, <string> s, ...)
texio.write(<string> s, ...)
\stopfunctioncall

Without the \type {target} argument, writes all given strings to the same
location(s) \TEX\ writes messages to at this moment. If \prm {batchmode} is in
effect, it writes only to the log, otherwise it writes to the log and the
terminal. The optional \type {target} can be one of four  possibilities: \type
{term}, \type {log}, \type {term and log} or a valid integer of a stream 
opened for writing.

Note: If several strings are given, and if the first of these strings is or might
be one of the targets above, the \type {target} must be specified explicitly to
prevent \LUA\ from interpreting the first string as the target.

\stopsubsection

\startsubsection[title={\type {write_nl}}]

\libindex{write_nl}

\startfunctioncall
texio.write_nl(<string> target, <string> s, ...)
texio.write_nl(<string> s, ...)
\stopfunctioncall

This function behaves like \type {texio.write}, but make sure that the given
strings will appear at the beginning of a new line. You can pass a single empty
string if you only want to move to the next line.

\stopsubsection

\startsubsection[title={\type {setescape}}]

\libindex{setescape}

You can disable \type {^^} escaping of control characters by passing a value of
zero.

\stopsubsection

\startsubsection[title={\type {closeinput}}]

\libindex{closeinput}

This function that should be used with care. It acts as \prm {endinput} but at
the \LUA\ end. You can use it to (sort of) force a jump back to \TEX. Normally a
\LUA\ will just collect prints and at the end bump an input level and flush these
prints. This function can help you stay at the current level but you need to know
what you're doing (or more precise: what \TEX\ is doing with input).

\stopsubsection

\stopsection

\startsection[title={The \type {token} library}][library=token]

\startsubsection[title={The scanner}]

\topicindex{libraries+\type{token}}
\topicindex{tokens}

\libindex{scan_keyword}
\libindex{scan_keywordcs}
\libindex{scan_int}
\libindex{scan_real}
\libindex{scan_float}
\libindex{scan_dimen}
\libindex{scan_glue}
\libindex{scan_toks}
\libindex{scan_code}
\libindex{scan_string}
\libindex{scan_argument}
\libindex{scan_word}
\libindex{scan_csname}
\libindex{scan_list}

The token library provides means to intercept the input and deal with it at the
\LUA\ level. The library provides a basic scanner infrastructure that can be used
to write macros that accept a wide range of arguments. This interface is on
purpose kept general and as performance is quite ok. One can build additional
parsers without too much overhead. It's up to macro package writers to see how
they can benefit from this as the main principle behind \LUATEX\ is to provide a
minimal set of tools and no solutions. The scanner functions are probably the
most intriguing.

\starttabulate[|l|l|p|]
\DB function              \BC argument           \BC result \NC \NR
\TB
\NC \type{scan_keyword}   \NC string             \NC returns true if the given keyword is gobbled; as with
                                                     the regular \TEX\ keyword scanner this is case insensitive
                                                     (and \ASCII\ based) \NC \NR
\NC \type{scan_keywordcs} \NC string             \NC returns true if the given keyword is gobbled; this variant
                                                     is case sensitive and also suitable for \UTF8 \NC \NR
\NC \type{scan_int}       \NC                    \NC returns an integer \NC \NR
\NC \type{scan_real}      \NC                    \NC returns a number from e.g.\ \type {1},  \type {1.1}, \type {.1} with optional collapsed signs \NC \NR
\NC \type{scan_float}     \NC                    \NC returns a number from e.g.\ \type {1},  \type {1.1}, \type {.1}, \type {1.1E10}, , \type {.1e-10} with optional collapsed signs \NC \NR
\NC \type{scan_dimen}     \NC infinity, mu-units \NC returns a number representing a dimension and or two numbers being the filler and order \NC \NR
\NC \type{scan_glue}      \NC mu-units           \NC returns a glue spec node \NC \NR
\NC \type{scan_toks}      \NC definer, expand    \NC returns a table of tokens tokens \NC \NR
\NC \type{scan_code}      \NC bitset             \NC returns a character if its category is in the given bitset (representing catcodes) \NC \NR
\NC \type{scan_string}    \NC                    \NC returns a string given between \type {{}}, as \type {\macro} or as sequence of characters with catcode 11 or 12 \NC \NR
\NC \type{scan_argument}  \NC                    \NC this one is simular to \type {scanstring} but also accepts a \type {\cs}
                                                     (which then get expanded) \NC \NR
\NC \type{scan_word}      \NC                    \NC returns a sequence of characters with catcode 11 or 12 as string \NC \NR
\NC \type{scan_csname}    \NC                    \NC returns \type {foo} after scanning \type {\foo} \NC \NR
\NC \type{scan_list}      \NC                    \NC picks up a box specification and returns a \type {[h|v]list} node \NC \NR
\LL
\stoptabulate

The scanners can be considered stable apart from the one scanning for a token.
The \type {scan_code} function takes an optional number, the \type {keyword}
function a normal \LUA\ string. The \type {infinity} boolean signals that we also
permit \type {fill} as dimension and the \type {mu-units} flags the scanner that
we expect math units. When scanning tokens we can indicate that we are defining a
macro, in which case the result will also provide information about what
arguments are expected and in the result this is separated from the meaning by a
separator token. The \type {expand} flag determines if the list will be expanded.

The string scanner scans for something between curly braces and expands on the
way, or when it sees a control sequence it will return its meaning. Otherwise it
will scan characters with catcode \type {letter} or \type {other}. So, given the
following definition:

\startbuffer
\def\bar{bar}
\def\foo{foo-\bar}
\stopbuffer

\typebuffer \getbuffer

we get:

\starttabulate[|l|Tl|l|]
\DB name \BC result \NC \NR
\TB
\NC \type {\directlua{token.scan_string()}{foo}} \NC \directlua{context("{\\red\\type {"..token.scan_string().."}}")} {foo} \NC full expansion \NC \NR
\NC \type {\directlua{token.scan_string()}foo}   \NC \directlua{context("{\\red\\type {"..token.scan_string().."}}")} foo   \NC letters and others \NC \NR
\NC \type {\directlua{token.scan_string()}\foo}  \NC \directlua{context("{\\red\\type {"..token.scan_string().."}}")}\foo   \NC meaning \NC \NR
\LL
\stoptabulate

The \type {\foo} case only gives the meaning, but one can pass an already
expanded definition (\prm {edef}'d). In the case of the braced variant one can of
course use the \prm {detokenize} and \prm {unexpanded} primitives since there we
do expand.

The \type {scan_word} scanner can be used to implement for instance a number scanner:

\starttyping
function token.scan_number(base)
    return tonumber(token.scan_word(),base)
end
\stoptyping

This scanner accepts any valid \LUA\ number so it is a way to pick up floats
in the input.

You can use the \LUA\ interface as follows:

\starttyping
\directlua {
    function mymacro(n)
        ...
    end
}

\def\mymacro#1{%
    \directlua {
        mymacro(\number\dimexpr#1)
    }%
}

\mymacro{12pt}
\mymacro{\dimen0}
\stoptyping

You can also do this:

\starttyping
\directlua {
    function mymacro()
        local d = token.scan_dimen()
        ...
    end
}

\def\mymacro{%
    \directlua {
        mymacro()
    }%
}

\mymacro 12pt
\mymacro \dimen0
\stoptyping

It is quite clear from looking at the code what the first method needs as
argument(s). For the second method you need to look at the \LUA\ code to see what
gets picked up. Instead of passing from \TEX\ to \LUA\ we let \LUA\ fetch from
the input stream.

In the first case the input is tokenized and then turned into a string, then it
is passed to \LUA\ where it gets interpreted. In the second case only a function
call gets interpreted but then the input is picked up by explicitly calling the
scanner functions. These return proper \LUA\ variables so no further conversion
has to be done. This is more efficient but in practice (given what \TEX\ has to
do) this effect should not be overestimated. For numbers and dimensions it saves
a bit but for passing strings conversion to and from tokens has to be done anyway
(although we can probably speed up the process in later versions if needed).

\stopsubsection

\startsubsection[title= {Picking up one token}]

\libindex {get_next}
\libindex {scan_token}
\libindex {expand}

The scanners look for a sequence. When you want to pick up one token from the
input you use \type {get_next}. This creates a token with the (low level)
properties as discussed next. This token is just the next one. If you want to
enforce expansion first you can use \type {scan_token}. Internally tokens are
characterized by a number that packs a lot of information. In order to access
the bits of information a token is wrapped in a userdata object.

The \type {expand} function will trigger expansion of the next token in the
input. This can be quite unpredictable but when you call it you probably know
enough about \TEX\ not to be too worried about that. It basically is a call to
the internal expand related function.

\stopsubsection

\startsubsection[title={Creating tokens}]

\libindex{create}
\libindex{new}

\libindex{is_defined}
\libindex{is_token}
\libindex{biggest_char}

\libindex{commands}
\libindex{command_id}

\libindex{get_command}
\libindex{get_cmdname}
\libindex{get_csname}
\libindex{get_id}
\libindex{get_active}
\libindex{get_expandable}
\libindex{get_protected}
\libindex{get_mode}
\libindex{get_index}
\libindex{get_tok}

\libindex{get_next}

The creator function can be used as follows:

\starttyping
local t = token.create("relax")
\stoptyping

This gives back a token object that has the properties of the \prm {relax}
primitive. The possible properties of tokens are:

\starttabulate[|l|p|]
\DB name \BC explanation \NC \NR
\TB
\NC \type {command}    \NC a number representing the internal command number \NC \NR
\NC \type {cmdname}    \NC the type of the command (for instance the catcode in case of a
                           character or the classifier that determines the internal
                           treatment \NC \NR
\NC \type {csname}     \NC the associated control sequence (if applicable) \NC \NR
\NC \type {id}         \NC the unique id of the token \NC \NR
\NC \type {tok}        \NC the full token number as stored in \TEX \NC \NR
\NC \type {active}     \NC a boolean indicating the active state of the token \NC \NR
\NC \type {expandable} \NC a boolean indicating if the token (macro) is expandable \NC \NR
\NC \type {protected}  \NC a boolean indicating if the token (macro) is protected \NC \NR
\NC \type {mode}       \NC a number either representing a character or another entity \NC \NR
\NC \type {index}      \NC a number running from 0x0000 upto 0xFFFF indicating a \TEX\ register index \NC \NR
\LL
\stoptabulate

Alternatively you can use a getter \type {get_<fieldname>} to access a property
of a token.

The numbers that represent a catcode are the same as in \TEX\ itself, so using
this information assumes that you know a bit about \TEX's internals. The other
numbers and names are used consistently but are not frozen. So, when you use them
for comparing you can best query a known primitive or character first to see the
values.

You can ask for a list of commands:

\starttyping
local t = token.commands()
\stoptyping

The id of a token class can be queried as follows:

\starttyping
local id = token.command_id("math_shift")
\stoptyping

If you really know what you're doing you can create character tokens by not
passing a string but a number:

\starttyping
local letter_x = token.create(string.byte("x"))
local other_x  = token.create(string.byte("x"),12)
\stoptyping

Passing weird numbers can give side effects so don't expect too much help with
that. As said, you need to know what you're doing. The best way to explore the
way these internals work is to just look at how primitives or macros or \prm
{chardef}'d commands are tokenized. Just create a known one and inspect its
fields. A variant that ignores the current catcode table is:

\starttyping
local whatever = token.new(123,12)
\stoptyping

You can test if a control sequence is defined with \type {is_defined}, which
accepts a string and returns a boolean:

\starttyping
local okay = token.is_defined("foo")
\stoptyping

The largest character possible is returned by \type {biggest_char}, just in case you
need to know that boundary condition.

\stopsubsection

\startsubsection[title={Macros}]

\topicindex {macros}

\libindex{set_macro}
\libindex{get_macro}
\libindex{get_meaning}
\libindex{set_char}
\libindex{set_lua}
\libindex{get_functions_table}

The \type {set_macro} function can get upto 4 arguments:

\starttyping
set_macro("csname","content")
set_macro("csname","content","global")
set_macro("csname")
\stoptyping

You can pass a catcodetable identifier as first argument:

\starttyping
set_macro(catcodetable,"csname","content")
set_macro(catcodetable,"csname","content","global")
set_macro(catcodetable,"csname")
\stoptyping

The results are like:

\starttyping
 \def\csname{content}
\gdef\csname{content}
 \def\csname{}
\stoptyping

The \type {get_macro} function can be used to get the content of a macro while
the \type {get_meaning} function gives the meaning including the argument
specification (as usual in \TEX\ separated by \type {->}).

The \type {set_char} function can be used to do a \prm {chardef} at the
\LUA\ end, where invalid assignments are silently ignored:

\starttyping
set_char("csname",number)
set_char("csname",number,"global")
\stoptyping

A special one is the following:

\starttyping
set_lua("mycode",id)
set_lua("mycode",id,"global","protected")
\stoptyping

This creates a token that refers to a \LUA\ function with an entry in the table
that you can access with \type {lua.get_functions_table}. It is the companion
to \lpr {luadef}.

\stopsubsection

\startsubsection[title={Pushing back}]

\libindex{get_next}
\libindex{put_next}

There is a (for now) experimental putter:

\starttyping
local t1 = token.get_next()
local t2 = token.get_next()
local t3 = token.get_next()
local t4 = token.get_next()
-- watch out, we flush in sequence
token.put_next { t1, t2 }
-- but this one gets pushed in front
token.put_next ( t3, t4 )
\stoptyping

When we scan \type {wxyz!} we get \type {yzwx!} back. The argument is either a table
with tokens or a list of tokens. The \type {token.expand} function will trigger
expansion but what happens really depends on what you're doing where.

\stopsubsection

\startsubsection[title={Nota bene}]

When scanning for the next token you need to keep in mind that we're not scanning
like \TEX\ does: expanding, changing modes and doing things as it goes. When we
scan with \LUA\ we just pick up tokens. Say that we have:

\starttyping
\bar
\stoptyping

but \type {\bar} is undefined. Normally \TEX\ will then issue an error message.
However, when we have:

\starttyping
\def\foo{\bar}
\stoptyping

We get no error, unless we expand \type {\foo} while \type {\bar} is still
undefined. What happens is that as soon as \TEX\ sees an undefined macro it will
create a hash entry and when later it gets defined that entry will be reused. So,
\type {\bar} really exists but can be in an undefined state.

\startbuffer[demo]
bar        : \directlua{tex.print(token.scan_csname())}\bar
foo        : \directlua{tex.print(token.scan_csname())}\foo
myfirstbar : \directlua{tex.print(token.scan_csname())}\myfirstbar
\stopbuffer

\startlines
\getbuffer[demo]
\stoplines

This was entered as:

\typebuffer[demo]

The reason that you see \type {bar} reported and not \type {myfirstbar} is that
\type {\bar} was already used in a previous paragraph.

If we now say:

\startbuffer
\def\foo{}
\stopbuffer

\typebuffer \getbuffer

we get:

\startlines
\getbuffer[demo]
\stoplines

And if we say

\startbuffer
\def\foo{\bar}
\stopbuffer

\typebuffer \getbuffer

we get:

\startlines
\getbuffer[demo]
\stoplines

When scanning from \LUA\ we are not in a mode that defines (undefined) macros at
all. There we just get the real primitive undefined macro token.

\startbuffer
\directlua{local t = token.get_next() tex.print(t.id.." "..t.tok)}\myfirstbar
\directlua{local t = token.get_next() tex.print(t.id.." "..t.tok)}\mysecondbar
\directlua{local t = token.get_next() tex.print(t.id.." "..t.tok)}\mythirdbar
\stopbuffer

\startlines
\getbuffer
\stoplines

This was generated with:

\typebuffer

So, we do get a unique token because after all we need some kind of \LUA\ object
that can be used and garbage collected, but it is basically the same one,
representing an undefined control sequence.

\stopsubsection

\stopsection

\startsection[title={The \type {kpse} library}][library=kpse]

\topicindex{libraries+\type{kpse}}

This library provides two separate, but nearly identical interfaces to the
\KPATHSEA\ file search functionality: there is a \quote {normal} procedural
interface that shares its kpathsea instance with \LUATEX\ itself, and an object
oriented interface that is completely on its own.

\startsubsection[title={\type {set_program_name} and \type {new}}]

\libindex{set_program_name}
\libindex{default_texmfcnf}
\libindex{new}

The way the library looks up variables is driven by the \type {texmf.cmf} file
where the currently set program name acts as filter. You can check what file is
used by with \type {default_texmfcnf}.

Before the search library can be used at all, its database has to be initialized.
There are three possibilities, two of which belong to the procedural interface.

First, when \LUATEX\ is used to typeset documents, this initialization happens
automatically and the \KPATHSEA\ executable and program names are set to \type
{luatex} (that is, unless explicitly prohibited by the user's startup script.
See~\in {section} [init] for more details).

Second, in \TEXLUA\ mode, the initialization has to be done explicitly via the
\type {kpse.set_program_name} function, which sets the \KPATHSEA\ executable
(and optionally program) name.

\startfunctioncall
kpse.set_program_name(<string> name)
kpse.set_program_name(<string> name, <string> progname)
\stopfunctioncall

The second argument controls the use of the \quote {dotted} values in the \type
{texmf.cnf} configuration file, and defaults to the first argument.

Third, if you prefer the object oriented interface, you have to call a different
function. It has the same arguments, but it returns a userdata variable.

\startfunctioncall
local kpathsea = kpse.new(<string> name)
local kpathsea = kpse.new(<string> name, <string> progname)
\stopfunctioncall

Apart from these two functions, the calling conventions of the interfaces are
identical. Depending on the chosen interface, you either call \type
{kpse.find_file} or \type {kpathsea:find_file}, with identical arguments and
return values.

\stopsubsection

\startsubsection[title={\type {record_input_file} and \type {record_output_file}}]

\topicindex {files+recording}

\libindex{record_input_file}
\libindex{record_output_file}

These two function can be used to register used files. Because callbacks can load
files themselves you might need these helpers (if you use recording at all).

\startfunctioncall
kpse.record_input_file(<string> name)
kpse.record_output_file(<string> name)
\stopfunctioncall

\stopsubsection

\startsubsection[title={\type {find_file}}]

\topicindex {files+finding}

\libindex {find_file}

The most often used function in the library is \type {find_file}:

\startfunctioncall
<string> f = kpse.find_file(<string> filename)
<string> f = kpse.find_file(<string> filename, <string> ftype)
<string> f = kpse.find_file(<string> filename, <boolean> mustexist)
<string> f = kpse.find_file(<string> filename, <string> ftype, <boolean> mustexist)
<string> f = kpse.find_file(<string> filename, <string> ftype, <number> dpi)
\stopfunctioncall

Arguments:

\startitemize[intro]

\sym{filename}

the name of the file you want to find, with or without extension.

\sym{ftype}

maps to the \type {-format} argument of \KPSEWHICH. The supported \type {ftype}
values are the same as the ones supported by the standalone \type {kpsewhich}
program: \startluacode
    local list = {
        "afm",
        "base",
        "bib",
        "bitmap font",
        "bst",
        "cid maps",
        "clua",
        "cmap files",
        "cnf",
        "cweb",
        "dvips config",
        "enc files",
        "fmt",
        "font feature files",
        "gf",
        "graphic|/|figure",
        "ist",
        "lig files",
        "ls-R",
        "lua",
        "map",
        "mem",
        "MetaPost support",
        "mf",
        "mfpool",
        "mft",
        "misc fonts",
        "mlbib",
        "mlbst",
        "mp",
        "mppool",
        "ocp",
        "ofm",
        "opentype fonts",
        "opl",
        "other binary files",
        "other text files",
        "otp",
        "ovf",
        "ovp",
        "pdftex config",
        "pk",
        "PostScript header",
        "subfont definition files",
        "tex",
        "TeX system documentation",
        "TeX system sources",
        "texmfscripts",
        "texpool",
        "tfm",
        "Troff fonts",
        "truetype fonts",
        "type1 fonts",
        "type42 fonts",
        "vf",
        "web",
        "web2c files",
    }
    table.sort(list)
    context("{\\tttf \letterpercent, t}",list)
\stopluacode

The default type is \type {tex}. Note: this is different from \KPSEWHICH, which
tries to deduce the file type itself from looking at the supplied extension.

\sym{mustexist}

is similar to \KPSEWHICH's \type {-must-exist}, and the default is \type {false}.
If you specify \type {true} (or a non|-|zero integer), then the \KPSE\ library
will search the disk as well as the \type {ls-R} databases.

\sym{dpi}

This is used for the size argument of the formats \type {pk}, \type {gf}, and
\type {bitmap font}. \stopitemize

\stopsubsection

\startsubsection[title={\type {lookup}}]

\libindex{lookup}

A more powerful (but slower) generic method for finding files is also available.
It returns a string for each found file.

\startfunctioncall
<string> f, ... = kpse.lookup(<string> filename, <table> options)
\stopfunctioncall

The options match commandline arguments from \type {kpsewhich}:

\starttabulate[|l|l|p|]
\DB key              \BC type     \BC explanation \NC \NR
\TB
\NC \type{debug}     \NC number   \NC set debugging flags for this lookup\NC     \NR
\NC \type{format}    \NC string   \NC use specific file type (see list above)\NC \NR
\NC \type{dpi}       \NC number   \NC use this resolution for this lookup; default 600\NC \NR
\NC \type{path}      \NC string   \NC search in the given path\NC \NR
\NC \type{all}       \NC boolean  \NC output all matches, not just the first\NC \NR
\NC \type{mustexist} \NC boolean  \NC search the disk as well as ls-R if necessary\NC \NR
\NC \type{mktexpk}   \NC boolean  \NC disable/enable mktexpk generation for this lookup\NC \NR
\NC \type{mktextex}  \NC boolean  \NC disable/enable mktextex generation for this lookup\NC \NR
\NC \type{mktexmf}   \NC boolean  \NC disable/enable mktexmf generation for this lookup\NC \NR
\NC \type{mktextfm}  \NC boolean  \NC disable/enable mktextfm generation for this lookup\NC \NR
\NC \type{subdir}    \NC string
                         or table \NC only output matches whose directory part
                                      ends with the given string(s) \NC \NR
\LL
\stoptabulate

\stopsubsection

\startsubsection[title={\type {init_prog}}]

\topicindex {initialization+bitmaps}

\libindex{init_prog}

Extra initialization for programs that need to generate bitmap fonts.

\startfunctioncall
kpse.init_prog(<string> prefix, <number> base_dpi, <string> mfmode)
kpse.init_prog(<string> prefix, <number> base_dpi, <string> mfmode, <string> fallback)
\stopfunctioncall

\stopsubsection

\startsubsection[title={\type {readable_file}}]

\libindex{readable_file}

Test if an (absolute) file name is a readable file.

\startfunctioncall
<string> f = kpse.readable_file(<string> name)
\stopfunctioncall

The return value is the actual absolute filename you should use, because the disk
name is not always the same as the requested name, due to aliases and
system|-|specific handling under e.g.\ \MSDOS. Returns \type {nil} if the file
does not exist or is not readable.

\stopsubsection

\startsubsection[title={\type {expand_path}}]

\libindex{expand_path}

Like kpsewhich's \type {-expand-path}:

\startfunctioncall
<string> r = kpse.expand_path(<string> s)
\stopfunctioncall

\stopsubsection

\startsubsection[title={\type {expand_var}}]

\libindex{expand_var}

Like kpsewhich's  \type {-expand-var}:

\startfunctioncall
<string> r = kpse.expand_var(<string> s)
\stopfunctioncall

\stopsubsection

\startsubsection[title={\type {expand_braces}}]

\libindex{expand_braces}

Like kpsewhich's \type {-expand-braces}:

\startfunctioncall
<string> r = kpse.expand_braces(<string> s)
\stopfunctioncall

\stopsubsection

\startsubsection[title={\type {show_path}}]

\libindex{show_path}

Like kpsewhich's \type {-show-path}:

\startfunctioncall
<string> r = kpse.show_path(<string> ftype)
\stopfunctioncall

\stopsubsection

\startsubsection[title={\type {var_value}}]

\libindex{var_value}

Like kpsewhich's \type {-var-value}:

\startfunctioncall
<string> r = kpse.var_value(<string> s)
\stopfunctioncall

\stopsubsection

\startsubsection[title={\type {version}}]

\libindex{version}

Returns the kpathsea version string.

\startfunctioncall
<string> r = kpse.version()
\stopfunctioncall

\stopsubsection

\stopsection

\stopchapter

\stopcomponent