node-par.lmt /size: 3798 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['node-par'] = {
2    version   = 1.001,
3    comment   = "companion to node-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local starttiming = statistics.starttiming
10local stoptiming  = statistics.stoptiming
11
12local sequencers  = utilities.sequencers
13
14-- This is called a lot! I'm a bit reluctant with this one because it is sensitive
15-- for order. In many other callbacks there is no action at the tex end but here ...
16-- Anyway, it has been around for a while now (2019) and so far I had no need for
17-- extensive usage so we're okay.
18
19do
20
21    local actions = nodes.tasks.actions("everypar")
22
23    local function everypar(head)
24        starttiming(builders)
25        head = actions(head)
26        stoptiming(builders)
27        return head
28    end
29
30    callbacks.register("insert_par",everypar,"after paragraph start")
31
32end
33
34-- Originally this one was meant to deal with the indentation (like turn a box into
35-- a skip or prevent it) but that never really was used. The return value still
36-- determines if an indentation box or skip is injected. Will I change that?
37
38do
39
40    local actions = sequencers.new {
41        name         = "paragraph",
42        arguments    = "mode,indented,context",
43        returnvalues = "indented",
44        results      = "indented",
45    }
46
47    sequencers.appendgroup(actions,"before") -- user
48    sequencers.appendgroup(actions,"system") -- private
49    sequencers.appendgroup(actions,"after" ) -- user
50
51    local function paragraph(mode,indented,context) -- context used to be the cmd code
52        local runner = actions.runner
53        if runner then
54            starttiming(builders)
55            indented = runner(mode,indented,context)
56            stoptiming(builders)
57        end
58        return indented
59    end
60
61    callbacks.register("begin_paragraph",paragraph,"before paragraph start")
62
63end
64
65-- This one is a playground for some old metafun gimmicks that I want to improve
66-- while I'm updating the manual to lmtx. but it might also be useful for other
67-- purposes. It fits in the category obscure and probably takes while to stabelize
68-- (if it stays at all). Again, this is one that has the danger of interference,
69-- so when it finally got an action handler it only got a system one.
70
71do
72
73    local actions = sequencers.new {
74        name         = "paragraphcontext",
75        arguments    = "context",
76        returnvalues = "ignore",
77        results      = "ignore",
78    }
79
80    ----------.appendgroup(actions,"before") -- user
81    sequencers.appendgroup(actions,"system") -- private
82    ----------.appendgroup(actions,"after" ) -- user
83
84    local function parcontext(parcontext)
85        local runner = actions.runner
86        if runner then
87            starttiming(builders)
88            local ignore = runner(parcontext)
89            stoptiming(builders)
90            return ignore
91        end
92    end
93
94    callbacks.register("paragraph_context",parcontext,"when the context is dealt with")
95
96end
97
98-- These are node handlers but fit it the overall paragraph handling so we
99-- hook it into the callback here.
100
101-- do
102
103--     local localboxactions = nodes.tasks.actions("localboxes")
104
105--     function localboxfilter(...)
106--         starttiming(builders)
107--         localboxactions(...)
108--         stoptiming(builders)
109--     end
110
111--     callbacks.register("local_box_filter",localboxfilter,"process local boxes")
112
113-- end
114
115-- This means that we now have most callbacks in use, even the ones that I'm not sure
116-- about. It also means that with the above enabled we might have performance now at
117-- its worst. I can optimize this a little but it's not worth the effort (and added
118-- complication).
119