summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/luametatex/luametatex-callbacks.tex
blob: 0469d0cc8c485deb78e210b1e0499f203dee15cf (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
% language=us runpath=texruns:manuals/luametatex

\environment luametatex-style

\startcomponent luametatex-callbacks

\startchapter[reference=callbacks,title={\LUA\ callbacks}]

\startsection[title={Registering callbacks}][library=callback]

\topicindex{callbacks}

\libindex{register}
\libindex{list}
\libindex{find}
\libindex{known}

{\em The callbacks are a moving target. Don't bother me with questions about
them. Some are new and/or experimental and therefore not yet documented. In
\CONTEXT\ we can easily adapt interfaces so changes in these have no real effect
on users. Of course in due time all will be official and documented.}

This library has functions that register, find and list callbacks. Callbacks are
\LUA\ functions that are called in well defined places. There are two kinds of
callbacks: those that mix with existing functionality, and those that (when
enabled) replace functionality. In most cases the second category is expected to
behave similar to the built in functionality because in a next step specific data
is expected. For instance, you can replace the hyphenation routine. The function
gets a list that can be hyphenated (or not). The final list should be valid and
is (normally) used for constructing a paragraph. Another function can replace the
ligature builder and|/|or kern routine. Doing something else is possible but in
the end might not give the user the expected outcome.

The first thing you need to do is registering a callback:

\startfunctioncall
id = callback.register(<string> callback_name, <function> func)
id = callback.register(<string> callback_name, nil)
id = callback.register(<string> callback_name, false)
\stopfunctioncall

Here the \syntax {callback_name} is a predefined callback name, see below. The
function returns the internal \type {id} of the callback or \type {nil}, if the
callback could not be registered.

\LUATEX\ internalizes the callback function in such a way that it does not matter
if you redefine a function accidentally.

Callback assignments are always global. You can use the special value \type {nil}
instead of a function for clearing the callback.

For some minor speed gain, you can assign the boolean \type {false} to the
non|-|file related callbacks, doing so will prevent \LUATEX\ from executing
whatever it would execute by default (when no callback function is registered at
all). Be warned: this may cause all sorts of grief unless you know \notabene
{exactly} what you are doing!

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

The keys in the table are the known callback names, the value is a boolean where
\type {true} means that the callback is currently set (active).

\startfunctioncall
<function> f = callback.find(callback_name)
\stopfunctioncall

If the callback is not set, \type {find} returns \type {nil}. The \type {known}
function can be used to check if a callback is supported.

\startfunctioncall
if callback.known("foo") then ... end
\stopfunctioncall

\stopsection

\startsection[title={File related callbacks}][library=callback]

\startsubsection[title={\cbk {find_format_file} and \cbk {find_log_file}}]

\topicindex{callbacks+format file}
\topicindex{callbacks+log file}

These callbacks are called as:

\startfunctioncall
<string> actualname =
    function (<string> askedname)
\stopfunctioncall

The \type {askedname} is a format file for reading (the format file for writing
is always opened in the current directory) or a log file for writing.

\stopsubsection

\startsubsection[title={\cbk {open_data_file}}]

\topicindex{callbacks+opening files}

This callback function gets a filename passed:

\startfunctioncall
<table> env = function (<string> filename)
\stopfunctioncall

The return value is either the boolean value false or a table with two functions.
A mandate \type {reader} function fill be called once for each new line to be
read, the optional \type {close} function will be called once \LUATEX\ is done
with the file.

\LUATEX\ never looks at the rest of the table, so you can use it to store your
private per|-|file data. Both the callback functions will receive the table as
their only argument.

% No longer needed anyway.
%
% \subsection{\cbk {if_end_of_file}}
%
% \topicindex{callbacks+checking files}
%
% This callback has no arguments and your function should return true or false. The
% callback is triggered by \type {\ifeof}. It's up to the macro package to come up
% with a reasonable implementation. By default the test is always true.
%
% \startfunctioncall
% <boolean> eof =
%     function ()
% \stopfunctioncall

\stopsubsection

\stopsection

\startsection[title={Data processing callbacks}][library=callback]

\startsubsection[title={\cbk {process_jobname}}]

\topicindex{callbacks+jobname}

This callback allows you to change the jobname given by \prm {jobname} in \TEX\
and \type {tex.jobname} in Lua. It does not affect the internal job name or the
name of the output or log files.

\startfunctioncall
function(<string> jobname)
    return <string> adjusted_jobname
end
\stopfunctioncall

The only argument is the actual job name; you should not use \type {tex.jobname}
inside this function or infinite recursion may occur. If you return \type {nil},
\LUATEX\ will pretend your callback never happened. This callback does not
replace any internal code.

\stopsubsection

\stopsection

\startsection[title={Node list processing callbacks}][library=callback]

The description of nodes and node lists is in~\in{chapter}[nodes].

\startsubsection[title={\cbk {contribute_filter}}]

\topicindex{callbacks+contributions}

This callback is called when \LUATEX\ adds contents to list:

\startfunctioncall
function(<string> extrainfo)
end
\stopfunctioncall

The string reports the group code. From this you can deduce from
what list you can give a treat.

\starttabulate[|l|p|]
\DB value             \BC explanation                                  \NC \NR
\TB
\NC \type{pre_box}    \NC interline material is being added            \NC \NR
\NC \type{pre_adjust} \NC \prm {vadjust} material is being added       \NC \NR
\NC \type{box}        \NC a typeset box is being added (always called) \NC \NR
\NC \type{adjust}     \NC \prm {vadjust} material is being added       \NC \NR
\LL
\stoptabulate

\stopsubsection

\startsubsection[title={\cbk {buildpage_filter}}]

\topicindex{callbacks+building pages}

This callback is called whenever \LUATEX\ is ready to move stuff to the main
vertical list. You can use this callback to do specialized manipulation of the
page building stage like imposition or column balancing.

\startfunctioncall
function(<string> extrainfo)
end
\stopfunctioncall

The string \type {extrainfo} gives some additional information about what \TEX's
state is with respect to the \quote {current page}. The possible values for the
\cbk {buildpage_filter} callback are:

\starttabulate[|l|p|]
\DB value                  \BC explanation                             \NC \NR
\TB
\NC \type{alignment}       \NC a (partial) alignment is being added    \NC \NR
\NC \type{after_output}    \NC an output routine has just finished     \NC \NR
\NC \type{new_graf}        \NC the beginning of a new paragraph        \NC \NR
\NC \type{vmode_par}       \NC \prm {par} was found in vertical mode   \NC \NR
\NC \type{hmode_par}       \NC \prm {par} was found in horizontal mode \NC \NR
\NC \type{insert}          \NC an insert is added                      \NC \NR
\NC \type{penalty}         \NC a penalty (in vertical mode)            \NC \NR
\NC \type{before_display}  \NC immediately before a display starts     \NC \NR
\NC \type{after_display}   \NC a display is finished                   \NC \NR
\NC \type{end}             \NC \LUATEX\ is terminating (it's all over) \NC \NR
\LL
\stoptabulate

\stopsubsection

\startsubsection[title={\cbk {build_page_insert}}]

\topicindex{callbacks+inserts}

This callback is called when the page builder adds an insert. There is not much
control over this mechanism but this callback permits some last minute
manipulations of the spacing before an insert, something that might be handy when
for instance multiple inserts (types) are appended in a row.

\startfunctioncall
function(<number> n, <number> i)
    return <number> register
end
\stopfunctioncall

with

\starttabulate[|l|p|]
\DB value    \BC explanation             \NC \NR
\TB
\NC \type{n} \NC the insert class        \NC \NR
\NC \type{i} \NC the order of the insert \NC \NR
\LL
\stoptabulate

The return value is a number indicating the skip register to use for the
prepended spacing. This permits for instance a different top space (when \type
{i} equals one) and intermediate space (when \type {i} is larger than one). Of
course you can mess with the insert box but you need to make sure that \LUATEX\
is happy afterwards.

\stopsubsection

\startsubsection[title={\cbk {pre_linebreak_filter}}]

\topicindex{callbacks+linebreaks}

This callback is called just before \LUATEX\ starts converting a list of nodes
into a stack of \prm {hbox}es, after the addition of \prm {parfillskip}.

\startfunctioncall
function(<node> head, <string> groupcode)
    return <node> newhead
end
\stopfunctioncall

The string called \type {groupcode} identifies the nodelist's context within
\TEX's processing. The range of possibilities is given in the table below, but
not all of those can actually appear in \cbk {pre_linebreak_filter}, some are
for the \cbk {hpack_filter} and \cbk {vpack_filter} callbacks that will be
explained in the next two paragraphs.

\starttabulate[|l|p|]
\DB value                \BC explanation                                 \NC \NR
\TB
\NC \type{<empty>}       \NC main vertical list                          \NC \NR
\NC \type{hbox}          \NC \prm {hbox} in horizontal mode              \NC \NR
\NC \type{adjusted_hbox} \NC \prm {hbox} in vertical mode                \NC \NR
\NC \type{vbox}          \NC \prm {vbox}                                 \NC \NR
\NC \type{vtop}          \NC \prm {vtop}                                 \NC \NR
\NC \type{align}         \NC \prm {halign} or \prm {valign}              \NC \NR
\NC \type{disc}          \NC discretionaries                             \NC \NR
\NC \type{insert}        \NC packaging an insert                         \NC \NR
\NC \type{vcenter}       \NC \prm {vcenter}                              \NC \NR
\NC \type{local_box}     \NC \prm {localleftbox} or \prm {localrightbox} \NC \NR
\NC \type{split_off}     \NC top of a \prm {vsplit}                      \NC \NR
\NC \type{split_keep}    \NC remainder of a \prm {vsplit}                \NC \NR
\NC \type{align_set}     \NC alignment cell                              \NC \NR
\NC \type{fin_row}       \NC alignment row                               \NC \NR
\LL
\stoptabulate

As for all the callbacks that deal with nodes, the return value can be one of
three things:

\startitemize
\startitem
    boolean \type {true} signals successful processing
\stopitem
\startitem
    \type {<node>} signals that the \quote {head} node should be replaced by the
    returned node
\stopitem
\startitem
    boolean \type {false} signals that the \quote {head} node list should be
    ignored and flushed from memory
\stopitem
\stopitemize

This callback does not replace any internal code.

\stopsubsection

\startsubsection[title={\cbk {linebreak_filter}}]

\topicindex{callbacks+linebreaks}

This callback replaces \LUATEX's line breaking algorithm.

\startfunctioncall
function(<node> head, <boolean> is_display)
    return <node> newhead
end
\stopfunctioncall

The returned node is the head of the list that will be added to the main vertical
list, the boolean argument is true if this paragraph is interrupted by a
following math display.

If you return something that is not a \type {<node>}, \LUATEX\ will apply the
internal linebreak algorithm on the list that starts at \type {<head>}.
Otherwise, the \type {<node>} you return is supposed to be the head of a list of
nodes that are all allowed in vertical mode, and at least one of those has to
represent an \prm {hbox}. Failure to do so will result in a fatal error.

Setting this callback to \type {false} is possible, but dangerous, because it is
possible you will end up in an unfixable \quote {deadcycles loop}.

\stopsubsection

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

\topicindex{callbacks+contributions}

This callback is called whenever \LUATEX\ adds a box to a vertical list (the
\type {mirrored} argument is obsolete):

\startfunctioncall
function(<node> box, <string> locationcode, <number> prevdepth)
    return list [, prevdepth [, checkdepth ] ]
end
\stopfunctioncall

It is ok to return nothing or \type {nil} in which case you also need to flush
the box or deal with it yourself. The prevdepth is also optional. Locations are
\type {box}, \type {alignment}, \type {equation}, \type {equation_number} and
\type {post_linebreak}. When the third argument returned is \type {true} the
normal prevdepth correction will be applied, based on the first node.

\stopsubsection

\startsubsection[title={\cbk {post_linebreak_filter}}]

\topicindex{callbacks+linebreaks}

This callback is called just after \LUATEX\ has converted a list of nodes into a
stack of \prm {hbox}es.

\startfunctioncall
function(<node> head, <string> groupcode)
    return <node> newhead
end
\stopfunctioncall

This callback does not replace any internal code.

\stopsubsection

\startsubsection[title={\cbk {glyph_run}}]

\topicindex{callbacks+fonts}
\topicindex{callbacks+hyphenation}
\topicindex{callbacks+kerning}
\topicindex{callbacks+ligature building}

When set this callback is triggered when \TEX\ normally handles the ligaturing
and kerning. In \LUATEX\ you use the \typ {hpack_filter} and \typ
{per_linebreak_filter} callbacks for that (where each passes different
arguments). This callback doesn't get triggered when there are no glyphs (in
\LUATEX\ this optimization is controlled by a a variable).

\startfunctioncall
function(<node> head, <string> groupcode, <number> direction])
    return <node> newhead
end
\stopfunctioncall

The traditional \TEX\ font processing is bypassed so you need to take care of that
with the helpers. (For the moment we keep the ligaturing and kerning callbacks but
they are kind of obsolete.)

\stopsubsection

\startsubsection[title={\cbk {hpack_filter}}]

\topicindex{callbacks+packing}

This callback is called when \TEX\ is ready to start boxing some horizontal mode
material. Math items and line boxes are ignored at the moment.

\startfunctioncall
function(<node> head, <string> groupcode, <number> size,
         <string> packtype [, <number> direction] [, <node> attributelist])
    return <node> newhead
end
\stopfunctioncall

The \type {packtype} is either \type {additional} or \type {exactly}. If \type
{additional}, then the \type {size} is a \type {\hbox spread ...} argument. If
\type {exactly}, then the \type {size} is a \type {\hbox to ...}. In both cases,
the number is in scaled points.

This callback does not replace any internal code.

\stopsubsection

\startsubsection[title={\cbk {vpack_filter}}]

\topicindex{callbacks+packing}

This callback is called when \TEX\ is ready to start boxing some vertical mode
material. Math displays are ignored at the moment.

This function is very similar to the \cbk {hpack_filter}. Besides the fact
that it is called at different moments, there is an extra variable that matches
\TEX's \prm {maxdepth} setting.

\startfunctioncall
function(<node> head, <string> groupcode, <number> size, <string> packtype,
        <number> maxdepth [, <number> direction] [, <node> attributelist]))
    return <node> newhead
end
\stopfunctioncall

This callback does not replace any internal code.

\stopsubsection

\startsubsection[title={\cbk {packed_vbox_filter}}]

\topicindex{callbacks+packing}

After the \cbk {vpack_filter} callback (see previous section) is triggered the
box get packed and after that this callback can be configured to kick in.

\startfunctioncall
function(<node> head, <string> groupcode)
    return <node> newhead
end
\stopfunctioncall

This callback does not replace any internal code.

\stopsubsection

\startsubsection[title={\cbk {alignment_filter}}]

\topicindex{callbacks+alignments}

This is an experimental callback that when set is called several times during the
construction of an alignment. The context values are available in \typ
{tex.getalignmentcontextvalues()}.

\startfunctioncall
function(<node> head, <string> context, <node> attributes, <node> preamble)
    -- no return values
end
\stopfunctioncall

There are no sanity checks so if a user messes up the passed node lists the results
can be unpredictable and, as with other node related callbacks, crash the engine.

\stopsubsection

\startsubsection[title={\cbk {localbox_filter}}]

\topicindex{callbacks+local boxes}

Local boxes are a somewhat tricky and error prone feature so use this callback
with care because the paragraph is easily messed up. A line can have a left,
right and middle box where the middle one has no width. The callback gets quite
some parameters passed:

\startfunctioncall
function(<node> linebox, <node> leftbox, <node> rightbox, <node> middlebox,
    <number> linenumber,
    <number> leftskip, <number> rightskip, <number> lefthang, <number> righthang,
    <number> indentation, <number> parinitleftskip, <number> parinitrightskip,
    <number> parfillleftskip, <number> parfillrightskip,
    <number> overshoot)
    -- no return values
end
\stopfunctioncall

This is an experimental callback that will be tested in different \CONTEXT\
mechanisms before it will be declared stable.

\stopsubsection

\startsubsection[title={\cbk {process_rule}}]

\topicindex{callbacks+rules}

This is an experimental callback. It can be used with rules of subtype~4
(user). The callback gets three arguments: the node, the width and the
height. The callback can use \type {pdf.print} to write code to the \PDF\
file but beware of not messing up the final result. No checking is done.

\stopsubsection

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

\topicindex{callbacks+output}

This callback is called when \TEX\ is ready to start boxing the box 255 for \prm
{output}.

\startfunctioncall
function(<node> head, <string> groupcode, <number> size, <string> packtype,
        <number> maxdepth [, <number> direction])
    return <node> newhead
end
\stopfunctioncall

This callback does not replace any internal code.

\stopsubsection

\startsubsection[title={\cbk {hyphenate}}]

\topicindex{callbacks+hyphenation}

This callback is supposed to insert discretionary nodes in the node list it
receives.

\startfunctioncall
function(<node> head, <node> tail)
    -- no return values
end
\stopfunctioncall

Setting this callback to \type {false} will prevent the internal discretionary
insertion pass.

\stopsubsection

\startsubsection[title={\cbk {ligaturing}}]

\topicindex{callbacks+ligature building}

This callback, which expects no return values, has to apply ligaturing to the
node list it receives.

\startfunctioncall
function(<node> head, <node> tail)
    -- no return values
end
\stopfunctioncall

You don't have to worry about return values because the \type {head} node that is
passed on to the callback is guaranteed not to be a glyph_node (if need be, a
temporary node will be prepended), and therefore it cannot be affected by the
mutations that take place. After the callback, the internal value of the \quote
{tail of the list} will be recalculated.

The \type {next} of \type {head} is guaranteed to be non-nil. The \type {next} of
\type {tail} is guaranteed to be nil, and therefore the second callback argument
can often be ignored. It is provided for orthogonality, and because it can
sometimes be handy when special processing has to take place.

Setting this callback to \type {false} will prevent the internal ligature
creation pass. You must not ruin the node list. For instance, the head normally
is a local par node, and the tail a glue. Messing too much can push \LUATEX\ into
panic mode.

\stopsubsection

\startsubsection[title={\cbk {kerning}}]

\topicindex{callbacks+kerning}

This callback has to apply kerning between the nodes in the node list it
receives. See \cbk {ligaturing} for calling conventions.

\startfunctioncall
function(<node> head, <node> tail)
    -- no return values
end
\stopfunctioncall

Setting this callback to \type {false} will prevent the internal kern insertion
pass. You must not ruin the node list. For instance, the head normally is a local
par node, and the tail a glue. Messing too much can push \LUATEX\ into panic
mode.

\stopsubsection

\startsubsection[title={\cbk {append_line_filter}}]

\topicindex{callbacks+lines}

Every time a line is added this callback is triggered, when set. migrated
material and adjusts also qualify as such and the detail relates to the adjust
index.

\startfunctioncall
function(<node> head, <node> tail, <string> context, <number> detail)
    return <node> newhead
end
\stopfunctioncall

A list of possible context values can be queried with \typ
{tex.getappendlinecontextvalues()}.

\stopsubsection

\stopsection

\startsection[title={Paragraph callbacks}]

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

\topicindex{callbacks+paragraphs}

Each paragraph starts with a local par node that keeps track of for instance
the direction. You can hook a callback into the creator:

\startfunctioncall
function(<node> par, <string> location)
    -- no return values
end
\stopfunctioncall

There is no return value and you should make sure that the node stays valid
as otherwise \TEX\ can get confused.

\stopsubsection

\startsubsection[title={\cbk {begin_paragraph}}]

\topicindex{callbacks+paragraphs}

{\em todo}

\stopsubsection

\startsubsection[title={\cbk {paragraph_context}}]

\topicindex{callbacks+paragraphs}

{\em todo}

\stopsubsection

\startsection[title={Math related callbacks}]

\startsubsection[title={\cbk {mlist_to_hlist}}]

\topicindex{callbacks+math}

This callback replaces \LUATEX's math list to node list conversion algorithm.

\startfunctioncall
function(<node> head, <string> display_type, <boolean> need_penalties)
    return <node> newhead
end
\stopfunctioncall

The returned node is the head of the list that will be added to the vertical or
horizontal list, the string argument is either \quote {text} or \quote {display}
depending on the current math mode, the boolean argument is \type {true} if
penalties have to be inserted in this list, \type {false} otherwise.

Setting this callback to \type {false} is bad, it will almost certainly result in
an endless loop.

\stopsubsection

\startsubsection[title={\cbk {math_rule}}]

\topicindex{callbacks+math}
\topicindex{callbacks+ruled}

{\em todo}

\stopsubsection

\startsubsection[title={\cbk {make_extensible}}]

\topicindex{callbacks+math}
\topicindex{callbacks+fonts}

{\em todo}

\stopsubsection

\startsubsection[title={\cbk {register_extensible}}]

\topicindex{callbacks+math}
\topicindex{callbacks+fonts}

{\em todo}

\stopsubsection

\stopsection

\startsection[title={Information reporting callbacks}][library=callback]

\startsubsection[title={\cbk {pre_dump}}]

\topicindex{callbacks+dump}

\startfunctioncall
function()
    -- no return values
end
\stopfunctioncall

This function is called just before dumping to a format file starts. It does not
replace any code and there are neither arguments nor return values.

\stopsubsection

\startsubsection[title={\cbk {start_run}}]

\topicindex{callbacks+job run}

\startfunctioncall
function()
    -- no return values
end
\stopfunctioncall

This callback replaces the code that prints \LUATEX's banner. Note that for
successful use, this callback has to be set in the \LUA\ initialization script,
otherwise it will be seen only after the run has already started.

\stopsubsection

\startsubsection[title={\cbk {stop_run}}]

\topicindex{callbacks+job run}

\startfunctioncall
function()
end
\stopfunctioncall

This callback replaces the code that prints \LUATEX's statistics and \quote
{output written to} messages. The engine can still do housekeeping and therefore
you should not rely on this hook for postprocessing the \PDF\ or log file.

\stopsubsection

\startsubsection[title={\cbk {intercept_tex_error}, \cbk {intercept_lua_error}}]

\topicindex{callbacks+errors}

\startfunctioncall
function()
    -- no return values
end
\stopfunctioncall

This callback is run from inside the \TEX\ error function, and the idea is to
allow you to do some extra reporting on top of what \TEX\ already does (none of
the normal actions are removed). You may find some of the values in the \type
{status} table useful. The \TEX\ related callback gets two arguments: the current
processing mode and a boolean indicating if there was a runaway.

\stopsubsection

\startsubsection[title={\cbk {show_error_message} and \cbk {show_warning_message}}]

\topicindex{callbacks+errors}
\topicindex{callbacks+warnings}

\startfunctioncall
function()
    -- no return values
end
\stopfunctioncall

These callback replaces the code that prints the error message. The usual
interaction after the message is not affected.

\stopsubsection

\startsubsection[title={\cbk {start_file}}]

\topicindex{callbacks+files}

\startfunctioncall
function(category,filename)
    -- no return values
end
\stopfunctioncall

This callback replaces the code that \LUATEX\ prints when a file is opened like
\type {(filename} for regular files. The category is a number:

\starttabulate[|c|l|]
\DB value  \BC meaning \NC \NR
\TB
\NC 1 \NC a normal data file, like a \TEX\ source \NC \NR
\NC 2 \NC a font map coupling font names to resources \NC \NR
\NC 3 \NC an image file (\type {png}, \type {pdf}, etc) \NC \NR
\NC 4 \NC an embedded font subset \NC \NR
\NC 5 \NC a fully embedded font \NC \NR
\LL
\stoptabulate

\stopsubsection

\startsubsection[title={\cbk {stop_file}}]

\topicindex{callbacks+files}

\startfunctioncall
function(category)
    -- no return values
end
\stopfunctioncall

This callback replaces the code that \LUATEX\ prints when a file is closed like
the \type {)} for regular files.

\stopsubsection

\startsubsection[title={\cbk {wrapup_run}}]

\topicindex{callbacks+wrapping up}

This callback is called after the \PDF\ and log files are closed. Use it at your own
risk.

\stopsubsection

\stopsection

\startsection[title={Font-related callbacks}][library=callback]

\startsubsection[title={\cbk {define_font}}]

\topicindex{callbacks+fonts}

\startfunctioncall
function(<string> name, <number> size)
    return <number> id
end
\stopfunctioncall

The string \type {name} is the filename part of the font specification, as given
by the user.

The number \type {size} is a bit special:

\startitemize[packed]
\startitem
    If it is positive, it specifies an \quote{at size} in scaled points.
\stopitem
\startitem
    If it is negative, its absolute value represents a \quote {scaled} setting
    relative to the design size of the font.
\stopitem
\stopitemize

The font can be defined with \type {font.define} which returns a font identifier
that can be returned in the callback. So, contrary to \LUATEX, in \LUAMETATEX\
we only accept a number.

The internal structure of the \type {font} table that is passed to \type
{font.define} is explained in \in {chapter} [fonts]. That table is saved
internally, so you can put extra fields in the table for your later \LUA\ code to
use. In alternative, \type {retval} can be a previously defined fontid. This is
useful if a previous definition can be reused instead of creating a whole new
font structure.

Setting this callback to \type {false} is pointless as it will prevent font
loading completely but will nevertheless generate errors.

\stopsubsection

\startsubsection[title={\cbk {missing_character} and \cbk {process_character}}]

\topicindex{callbacks+fonts}
\topicindex{callbacks+characters}

This callback is triggered when a character node is created and the font doesn't
have the requested character.

\startfunctioncall
function(<node> glyph, <number> font, <number> character)
    -- no return value
end
\stopfunctioncall

The \type {process_character} callback is experimental and gets called when a
glyph node is created and the callback field in a character is set.

\startfunctioncall
function(<number> font, <number> character)
    -- no return value
end
\stopfunctioncall

\stopsubsection

\stopsection

\startsection[title=Reporting]

\startsubsection[title={\cbk {show_whatsit}}]

\topicindex{callbacks+whatsits}

Because we only have a generic whatsit it is up to the macro package to provide
details when tracing them.

\startfunctioncall
function(<node> whatsit, <number> indentation,
    <number> tracinglevel, <number> currentlevel, <number> inputlevel)
    -- no return value
end
\stopfunctioncall

The indentation tells how many periods are to be typeset if you want to be
compatible with the rest of tracing. The tracinglevels indicates if the current
level and\|/or input level are shown cf. \prm {tracinglevels}. Of course one
is free to show whatever in whatever way suits the whatsit best.

\stopsubsection

\startsubsection[title={\cbk {get_attribute}}]

\topicindex{callbacks+attributes}

Because attributes are abstract pairs of indices and values the reported
properties makes not much sense and are very macro package (and user) dependent.
This callback permits more verbose reporting by the engine when tracing is
enabled.

\startfunctioncall
function(<number> index, <number> value)
    return <string>, <string>
end
\stopfunctioncall

\stopsubsection

\startsubsection[title={\cbk {get_noad_class}}]

\topicindex{callbacks+classes}

We have built|-|in math classes but there can also be user defined ones. This
callback can be used to report more meaningful strings instead of numbers when
tracing.

\startfunctioncall
function(<number> class)
    return <string>
end
\stopfunctioncall

\stopsubsection

\startsubsection[title={\cbk {trace_memory}}]

When the engine starts all kind of memory is pre|-|allocated> depending on the
configuration more gets allocated when a category runs out of memory. The
\LUAMETATEX\ engine is more dynamic than \LUATEX. If this callback is set it will
get called as follows:

\startfunctioncall
function(<string> category, <boolean> success)
    -- no return value
end
\stopfunctioncall

The boolean indicates if the allocation has been successful. One can best quit
the run when this one is \type {false}, if the engine doesn't already do that.

\stopsubsection

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

\topicindex{callbacks+packing}

This callback can be used to intercept the overfull messages that can result from
packing a horizontal list (as happens in the par builder). The function takes a
few arguments:

\startfunctioncall
function(<string> incident, <number> detail, <node> head, <number> first,
         <number> last)
    return <node> whatever
end
\stopfunctioncall

The incident is one of \type {overfull}, \type {underfull}, \type {loose} or
\type {tight}. The detail is either the amount of overflow in case of \type
{overfull}, or the badness otherwise. The head is the list that is constructed
(when protrusion or expansion is enabled, this is an intermediate list).
Optionally you can return a node, for instance an overfull rule indicator. That
node will be appended to the list (just like \TEX's own rule would).

\stopsubsection

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

\topicindex{callbacks+packing}

This callback can be used to intercept the overfull messages that can result from
packing a vertical list (as happens in the page builder). The function takes a
few arguments:

\startfunctioncall
function(<string> incident, <number> detail, <node> head, <number> first,
    <number> last)
end
\stopfunctioncall

The incident is one of \type {overfull}, \type {underfull}, \type {loose} or
\type {tight}. The detail is either the amount of overflow in case of \type
{overfull}, or the badness otherwise. The head is the list that is constructed.

\stopsubsection

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

\topicindex{callbacks+lua}

This one can be used to help reporting definitions that relate to \LUA\ calls to
be more meaningful when tracing.

\startfunctioncall
function(<string> name, <number> index)
    return <string>
end
\stopfunctioncall

\stopsubsection

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

\topicindex{callbacks+overload}

This is one of the few callbacks that is aimed at \CONTEXT: it relates to overload
protection of macros and other variables.

\startfunctioncall
function(<boolean> error, <number> overload, <string> csname, <number> flags)
    -- no return values
end
\stopfunctioncall

The overload is determined by:

\starttabulate[|c|l|c|c|c|c|c|]
\DB   \BC         \BC immutable \BC permanent \BC primitive \BC frozen \BC instance \NC \NR
\TB
\NC 1 \NC warning \NC \star     \NC \star     \NC \star     \NC        \NC          \NC \NR
\NC 2 \NC error   \NC \star     \NC \star     \NC \star     \NC        \NC          \NC \NR
\NC 3 \NC warning \NC \star     \NC \star     \NC \star     \NC \star  \NC          \NC \NR
\NC 4 \NC error   \NC \star     \NC \star     \NC \star     \NC \star  \NC          \NC \NR
\NC 5 \NC warning \NC \star     \NC \star     \NC \star     \NC \star  \NC \star    \NC \NR
\NC 6 \NC error   \NC \star     \NC \star     \NC \star     \NC \star  \NC \star    \NC \NR
\LL
\stoptabulate

This relates to the optional prefixed that can be used when defining and setting
quantities and is therefore also a bit of a playground. All macros and aliases in
\CONTEXT\ are classified this way.

\stopsection

\stopchapter

\stopcomponent