summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/mk/mk-dirtytricks.tex
blob: 30f9ff648fd13f9709b10afba015b51c91d2d8a1 (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
% language=uk

\startcomponent mk-dirtytricks

\environment mk-environment

\chapter {Dirty tricks}

If you ever laid your hands on the \TeX book, the words \quote {dirty tricks} will
forever be associated with an appendix of that book. There is no doubt that you need
to know a bit of the internals of \TEX\ in order to master this kind of trickyness.

In this chaper I will show a few dirty \LUATEX\ tricks. It also gives an impression
of what kind of discussions Taco and I had when discussing what kind of support should
be build in the interface.

\subject{afterlua}

When we look at \LUA\ from the \TEX\ end, we can do things like:

\startbuffer
\def\test#1{%
    \setbox0=\hbox{\directlua0{tex.sprint(math.pi*#1)}}%
    pi: \the\wd0\space\the\ht0\space\the\dp0\par
}
\stopbuffer

\typebuffer \blank \getbuffer \blank

But what if we are at the \LUA\ end and want to let \TEX\ handle things?  Imagine
the following call:

\startbuffer
\setbox0\hbox{} \dimen0=0pt \ctxlua {
    tex.sprint("\string\\setbox0=\string\\hbox{123}")
    tex.sprint("\string\\the\string\\wd0")
}
\stopbuffer

\typebuffer

This gives: \ignorespaces \getbuffer. This may give you the impression that \TEX\
kicks in immediately, but the following example demonstrates otherwise:

\startbuffer
\setbox0\hbox{} \dimen0=0pt \ctxlua {
    tex.sprint("\string\\setbox0=\string\\hbox{123}")
    tex.dimen[0] = tex.box[0].width
    tex.sprint("\string\\the\string\\dimen0")
}
\stopbuffer

\typebuffer

This gives: \getbuffer. When still in \LUA, we never get to see the width
of the box.

A way out of this is the following rather straightforward approach:

\starttyping
function test(n)
    function follow_up()
        tex.sprint(tex.box[0].width)
    end
    tex.sprint("\\setbox0=\\hbox{123}\\directlua 0 {follow_up()}")
end
\stoptyping

We can provide a more convenient solution for this:

\starttyping
after_lua = { } -- could also be done with closures

function the_afterlua(...)
    for _, fun in ipairs(after_lua) do
        fun(...)
    end
    after_lua = { }
end

function afterlua(f)
    after_lua[#after_lua+1] = f
end

function theafterlua(...)
    tex.sprint("\\directlua 0 {the_afterlua("
        .. table.concat({...},',') .. ")}")
end
\stoptyping

If you look closely, you will see that we can (optionally) pass arguments
to the function \type {theafterlua}. Usage now becomes:

\starttyping
function test(n)
    afterlua(function(...)
        tex.sprint(string.format("pi: %s %s %s\\par",...     ))
    end)
    afterlua(function(wd,ht,dp)
        tex.sprint(string.format("ip: %s %s %s\\par",dp,ht,wd))
    end)
    tex.sprint(string.format("\\setbox0=\\hbox{%s}",math.pi*n))
    local box_0 = tex.box[0]
    theafterlua(box_0.width,box_0.height,box_0.depth)
end
\stoptyping

The last call may confuse you but since it does a print to \TEX, it is
in fact a delayed action. A cleaner implementation is the following:

\starttyping
local delayed = { }

local function flushdelayed(...)
    delayed = { }
    for i=1, #t do
        t[i](...)
    end
end

function lua.delay(f)
    delayed[#delayed+1] = f
end

function lua.flush(...)
    tex.sprint("\\directlua{flushdelayed(" ..
        table.concat({...},',') .. ")}")
end
\stoptyping

Usage is similar:

\starttyping
function test(n)
    lua.delay(function(...)
        tex.sprint(string.format("pi: %s %s %s\\par",...))
    end)
    tex.sprint(string.format("\\setbox0=\\hbox{%s}",math.pi*n))
    local box_0 = tex.box[0]
    lua.flush(box_0.width,box_0.height,box_0.depth)
end
\stoptyping

\stopcomponent