summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/hybrid/hybrid-inserts.tex
blob: d483f1305d24fe3a0fe68e187ad73e17af479097 (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
% language=uk

\startcomponent hybrid-inserts

\environment hybrid-environment

\startchapter[title={Deeply nested notes}]

\startsection [title={Introduction}]

One of the mechanisms that is not on a users retina when he or she starts using
\TEX\ is \quote {inserts}. An insert is material that is entered at one point but
will appear somewhere else in the output. Footnotes for instance can be
implemented using inserts. You create a reference symbol in the running text and
put note text at the bottom of the page or at the end of a chapter or document.
But as you don't want to do that moving around of notes yourself \TEX\ provides
macro writers with the inserts mechanism that will do some of the housekeeping.
Inserts are quite clever in the sense that they are taken into account when \TEX\
splits off a page. A single insert can even be split over two or more pages.

Other examples of inserts are floats that move to the top or bottom of the page
depending on requirements and|/|or available space. Of course the macro package
is responsible for packaging such a float (for instance an image) but by finally
putting it in an insert \TEX\ itself will attempt to deal with accumulated floats
and help you move kept over floats to following pages. When the page is finally
assembled (in the output routine) the inserts for that page become available and
can be put at the spot where they belong. In the process \TEX\ has made sure that
we have the right amount of space available.

However, let's get back to notes. In \CONTEXT\ we can have many variants of them,
each taken care of by its own class of inserts. This works quite well, as long as
a note is visible for \TEX\ which means as much as: ends up in the main page
flow. Consider the following situation:

\starttyping
before \footnote{the note} after
\stoptyping

When the text is typeset, a symbol is placed directly after the word \quote
{before} and the note itself ends up at the bottom of the page. It also works
when we wrap the text in an horizontal box:

\starttyping
\hbox{before \footnote{the note} after}
\stoptyping

But it fails as soon as we go further:

\starttyping
\hbox{\hbox{before \footnote{the note} after}}
\stoptyping

Here we get the reference but no note. This also fails:

\starttyping
\vbox{before \footnote{the note} after}
\stoptyping

Can you imagine what happens if we do the following?

\starttyping
\starttabulate
\NC knuth \NC test \footnote{knuth} \input knuth \NC \NR
\NC tufte \NC test \footnote{tufte} \input tufte \NC \NR
\NC ward  \NC test \footnote{ward}  \input ward  \NC \NR
\stoptabulate
\stoptyping

This mechanism uses alignments as well as quite some boxes. The paragraphs are
nicely split over pages but still appear as boxes to \TEX\ which make inserts
invisible. Only the three symbols would remain visible. But because in \CONTEXT\
we know when notes tend to disappear, we take some provisions, and contrary to
what you might expect the notes actually do show up. However, they are flushed in
such a way that they end up on the page where the table ends. Normally this is no
big deal as we will often use local notes that end up at the end of the table
instead of the bottom of the page, but still.

The mechanism to deal with notes in \CONTEXT\ is somewhat complex at the source
code level. To mention a few properties we have to deal with:

\startitemize[packed]
\startitem Notes are collected and can be accessed any time. \stopitem
\startitem Notes are flushed either directly or delayed. \stopitem
\startitem Notes can be placed anywhere, any time, perhaps in subsets. \stopitem
\startitem Notes can be associated to lines in paragraphs. \stopitem
\startitem Notes can be placed several times with different layouts. \stopitem
\stopitemize

So, we have some control over flushing and placement, but real synchronization
between for instance table entries having notes and the note content ending up on
the same page is impossible.

In the \LUATEX\ team we have been discussing more control over inserts and we
will definitely deal with that in upcoming releases as more control is needed for
complex multi|-|column document layouts. But as we have some other priorities
these extensions have to wait.

As a prelude to them I experimented a bit with making these deeply buried inserts
visible. Of course I use \LUA\ for this as \TEX\ itself does not provide the kind
of access we need for this kind of of manipulations.

\stopsection

\startsection [title={Deep down inside}]

Say that we have the following boxed footnote. How does that end up in \LUATEX ?

\starttyping
\vbox{a\footnote{b}c}
\stoptyping

Actually it depends on the macro package but the principles remain the same. In
\LUATEX\ 0.50 and the \CONTEXT\ version used at the time of this writing we get
(nested) linked list that prints as follows:

\starttyping
<node   26 <  862 >  nil : vlist 0>
  <node  401 <  838 >  507 : hlist 1>
    <node   30 <  611 >  580 : whatsit 6>
    <node  611 <  580 >  493 : hlist 0>
    <node  580 <  493 >  653 : glyph 256>
    <node  493 <  653 >  797 : penalty 0>
    <node  653 <  797 >  424 : kern 1>
    <node  797 <  424 >  826 : hlist 2>
      <node  445 <  563 >  nil : hlist 2>
        <node  420 <  817 >  821 : whatsit 35>
        <node  817 <  821 >  nil : glyph 256>
    <node  507 <  826 > 1272 : kern 1>
    <node  826 < 1272 > 1333 : glyph 256>
    <node 1272 < 1333 >  830 : penalty 0>
    <node 1333 <  830 >  888 : glue 15>
    <node  830 <  888 >  nil : glue 9>
  <node  838 <  507 >  nil : ins 131>
\stoptyping

The numbers are internal references to the node memory pool. Each line represents
a node:

\starttyping
<node prev_index < index > next_index : type subtype>
\stoptyping

The whatsits carry directional information and the deeply nested hlist is the
note symbol. If we forget about whatsits, kerns and penalties, we can simplify
this listing to:

\starttyping
<node   26 <  862 >  nil : vlist 0>
  <node  401 <  838 >  507 : hlist 1>
    <node  580 <  493 >  653 : glyph 256>
    <node  797 <  424 >  826 : hlist 2>
      <node  445 <  563 >  nil : hlist 2>
        <node  817 <  821 >  nil : glyph 256>
    <node  826 < 1272 > 1333 : glyph 256>
  <node  838 <  507 >  nil : ins 131>
\stoptyping

So, we have a vlist (the \type {\vbox}), which has one line being a hlist. Inside
we have a glyph (the \quote{a}) followed by the raised symbol (the
\quote{\high{1}}) and next comes the second glyph (the \quote{b}). But watch how
the insert ends up at the end of the line. Although the insert will not show up
in the document, it sits there waiting to be used. So we have:

\starttyping
<node   26 <  862 >  nil : vlist 0>
  <node  401 <  838 >  507 : hlist 1>
  <node  838 <  507 >  nil : ins 131>
\stoptyping

but we need:

\starttyping
<node   26 <  862 >  nil : vlist 0>
  <node  401 <  838 >  507 : hlist 1>
<node  838 <  507 >  nil : ins 131>
\stoptyping

Now, we could use the fact that inserts end up at the end of the line, but as we
need to recursively identify them anyway, we cannot actually use this fact to
optimize the code.

In case you wonder how multiple inserts look like, here is an example:

\starttyping
\vbox{a\footnote{b}\footnote{c}d}
\stoptyping

This boils down to:

\starttyping
<node   26 < 1324 >  nil : vlist 0>
  <node  401 < 1348 >  507 : hlist 1>
  <node 1348 <  507 >  457 : ins 131>
  <node  507 <  457 >  nil : ins 131>
\stoptyping

In case you wonder what more can end up at the end, vertically adjusted material
(\type {\vadjust}) as well as marks (\type {\mark}) also get that treatment.

\starttyping
\vbox{a\footnote{b}\vadjust{c}\footnote{d}e\mark{f}}
\stoptyping

As you see, we start with the line itself, followed by a mixture of inserts and
vertically adjusted content (that will be placed before that line). This trace
also shows the list 2~levels deep.

\starttyping
<node   26 < 1324 >  nil : vlist 0>
  <node  401 < 1348 >  507 : hlist 1>
  <node 1348 <  507 >  862 : ins 131>
  <node  507 <  862 >  240 : hlist 1>
  <node  862 <  240 > 2288 : ins 131>
  <node  240 < 2288 >  nil : mark 0>
\stoptyping

Currently vadjust nodes have the same subtype as an ordinary hlist but in
\LUATEX\ versions beyond 0.50 they will have a dedicated subtype.

We can summarize the pattern of one \quote {line} in a vertical list as:

\starttyping
[hlist][insert|mark|vadjust]*[penalty|glue]+
\stoptyping

In case you wonder what happens with for instance specials, literals (and other
whatits): these end up in the hlist that holds the line. Only inserts, marks and
vadjusts migrate to the outer level, but as they stay inside the vlist, they are
not visible to the page builder unless we're dealing with the main vertical list.
Compare:

\starttyping
this is a regular paragraph possibly with inserts and they
will be visible as the lines are appended to the main
vertical list \par
\stoptyping

with:

\starttyping
but \vbox {this is a nested paragraph where inserts will
stay with the box} and not migrate here \par
\stoptyping

So much for the details; let's move on the how we can get
around this phenomenon.

\stopsection

\startsection [title={Some \LUATEX\ magic}]

The following code is just the first variant I made and \CONTEXT\ ships with a
more extensive variant. Also, in \CONTEXT\ this is part of a larger suite of
manipulative actions but it does not make much sense (at least not now) to
discuss this framework here.

We start with defining a couple of convenient shortcuts.

\starttyping
local hlist = node.id('hlist')
local vlist = node.id('vlist')
local ins   = node.id('ins')
\stoptyping

We can write a more compact solution but splitting up the functionality better
shows what we're doing. The main migration function hooks into the callback \type
{build_page}. Contrary to other callbacks that do phases in building lists and
pages this callback does not expect the head of a list as argument. Instead, we
operate directly on the additions to the main vertical list which is accessible
as \type {tex.lists.contrib_head}.

\starttyping
local deal_with_inserts -- forward reference

local function migrate_inserts(where)
    local current = tex.lists.contrib_head
    while current do
        local id = current.id
        if id == vlist or id == hlist then
            current = deal_with_inserts(current)
        end
        current = current.next
    end
end

callback.register('buildpage_filter',migrate_inserts)
\stoptyping

So, effectively we scan for vertical and horizontal lists and deal with embedded
inserts when we find them. In \CONTEXT\ the migratory function is just one of the
functions that is applied to this filter.

We locate inserts and collect them in a list with \type {first} and \type {last}
as head and tail and do so recursively. When we have run into inserts we insert
them after the horizontal or vertical list that had embedded them.

\starttyping
local locate -- forward reference

deal_with_inserts = function(head)
    local h, first, last = head.list, nil, nil
    while h do
        local id = h.id
        if id == vlist or id == hlist then
            h, first, last = locate(h,first,last)
        end
        h = h.next
    end
    if first then
        local n = head.next
        head.next = first
        first.prev = head
        if n then
            last.next = n
            n.prev = last
        end
        return last
    else
        return head
    end
end
\stoptyping

The \type {locate} function removes inserts and adds them to a new list, that is
passed on down in recursive calls and eventually is returned back to the caller.

\starttyping
locate = function(head,first,last)
    local current = head
    while current do
        local id = current.id
        if id == vlist or id == hlist then
            current.list, first, last = locate(current.list,first,last)
            current = current.next
        elseif id == ins then
            local insert = current
            head, current = node.remove(head,current)
            insert.next = nil
            if first then
                insert.prev = last
                last.next = insert
            else
                insert.prev = nil
                first = insert
            end
            last = insert
        else
            current = current.next
        end
    end
    return head, first, last
end
\stoptyping

As we can encounter the content several times in a row, it makes sense to mark
already processed inserts. This can for instance be done by setting an attribute.
Of course one has to make sure that this attribute is not used elsewhere.

\starttyping
if not node.has_attribute(current,8061) then
    node.set_attribute(current,8061,1)
    current = deal_with_inserts(current)
end
\stoptyping

or integrated:

\starttyping
local has_attribute = node.has_attribute
local set_attribute = node.set_attribute

local function migrate_inserts(where)
    local current = tex.lists.contrib_head
    while current do
        local id = current.id
        if id == vlist or id == hlist then
            if has_attribute(current,8061) then
                -- maybe some tracing message
            else
                set_attribute(current,8061,1)
                current = deal_with_inserts(current)
            end
        end
        current = current.next
    end
end

callback.register('buildpage_filter',migrate_inserts)
\stoptyping

\stopsection

\startsection [title={A few remarks}]

Surprisingly, the amount of code needed for insert migration is not that large.
This makes one wonder why \TEX\ does not provide this feature itself as it could
have saved macro writers quite some time and headaches. Performance can be a
reason, unpredictable usage and side effects might be another. Only one person
knows the answer.

In \CONTEXT\ this mechanism is built in and it can be enabled by saying:

\starttyping
\automigrateinserts
\automigratemarks
\stoptyping

As you can see here, we can also migrate marks. Future versions of \CONTEXT\ will
do this automatically and also provide some control over what classes of inserts
are moved around. We will probably overhaul the note handling mechanism a few
more times anyway as \LUATEX\ evolves and the demands from critical editions that
use many kind of notes raise.

\stopsection

\startsection [title={Summary of code}]

The following code should work in plain \TEX:

\starttyping
\directlua 0 {
local hlist         = node.id('hlist')
local vlist         = node.id('vlist')
local ins           = node.id('ins')
local has_attribute = node.has_attribute
local set_attribute = node.set_attribute

local status = 8061

local function locate(head,first,last)
    local current = head
    while current do
        local id = current.id
        if id == vlist or id == hlist then
            current.list, first, last = locate(current.list,first,last)
            current = current.next
        elseif id == ins then
            local insert = current
            head, current = node.remove(head,current)
            insert.next = nil
            if first then
                insert.prev, last.next = last, insert
            else
                insert.prev, first = nil, insert
            end
            last = insert
        else
            current = current.next
        end
    end
    return head, first, last
end

local function migrate_inserts(where)
    local current = tex.lists.contrib_head
    while current do
        local id = current.id
        if id == vlist or id == hlist and
                not has_attribute(current,status) then
            set_attribute(current,status,1)
            local h, first, last = current.list, nil, nil
            while h do
                local id = h.id
                if id == vlist or id == hlist then
                    h, first, last = locate(h,first,last)
                end
                h = h.next
            end
            if first then
                local n = current.next
                if n then
                    last.next, n.prev = n, last
                end
                current.next, first.prev = first, current
                current = last
            end
        end
        current = current.next
    end
end

callback.register('buildpage_filter', migrate_inserts)
}
\stoptyping

Alternatively you can put the code in a file and load that with:

\starttyping
\directlua {require "luatex-inserts.lua"}
\stoptyping

A simple plain test is:

\starttyping
\vbox{a\footnote{1}{1}b}
\hbox{a\footnote{2}{2}b}
\stoptyping

The first footnote only shows up when we have hooked our migrator into the
callback. A not that bad result for 60 lines of \LUA\ code.

\stopsection

\stopchapter

\stopcomponent