Debian Codegraph @

texk/web2c/luatexdir/tex/nesting.c

raw
1 /*
2
3 nesting.w
4
5 Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "ptexlib.h"
25
26 /*tex These are for |show_activities|: */
27
28 #define page_goal page_so_far[0]
29
30 /*tex
31
32 \TeX\ is typically in the midst of building many lists at once. For example, when
33 a math formula is being processed, \TeX\ is in math mode and working on an mlist;
34 this formula has temporarily interrupted \TeX\ from being in horizontal mode and
35 building the hlist of a paragraph; and this paragraph has temporarily interrupted
36 \TeX\ from being in vertical mode and building the vlist for the next page of a
37 document. Similarly, when a \.{\\vbox} occurs inside of an \.{\\hbox}, \TeX\ is
38 temporarily interrupted from working in restricted horizontal mode, and it enters
39 internal vertical mode. The ``semantic nest'' is a stack that keeps track of what
40 lists and modes are currently suspended.
41
42 At each level of processing we are in one of six modes:
43
44 |vmode| stands for vertical mode (the page builder);
45
46 |hmode| stands for horizontal mode (the paragraph builder);
47
48 |mmode| stands for displayed formula mode;
49
50 |-vmode| stands for internal vertical mode (e.g., in a \.{\\vbox});
51
52 |-hmode| stands for restricted horizontal mode (e.g., in an \.{\\hbox});
53
54 |-mmode| stands for math formula mode (not displayed).
55
56 The mode is temporarily set to zero while processing \.{\\write} texts in the
57 |ship_out| routine.
58
59 Numeric values are assigned to |vmode|, |hmode|, and |mmode| so that \TeX's ``big
60 semantic switch'' can select the appropriate thing to do by computing the value
61 |abs(mode)+cur_cmd|, where |mode| is the current mode and |cur_cmd| is the
62 current command code.
63
64 */
65
66 static const char *string_mode(int m)
67 {
68 if (m > 0) {
69 switch (m / (max_command_cmd + 1)) {
70 case 0:
71 return "vertical mode";
72 break;
73 case 1:
74 return "horizontal mode";
75 break;
76 case 2:
77 return "display math mode";
78 break;
79 default:
80 break;
81 }
82 } else if (m == 0) {
83 return "no mode";
84 } else {
85 switch ((-m) / (max_command_cmd + 1)) {
86 case 0:
87 return "internal vertical mode";
88 break;
89 case 1:
90 return "restricted horizontal mode";
91 break;
92 case 2:
93 return "math mode";
94 break;
95 default:
96 break;
97 }
98 }
99 return "unknown mode";
100 }
101
102 void print_mode(int m)
103 {
104 tprint(string_mode(m));
105 }
106
107 void print_in_mode(int m)
108 {
109 tprint("' in ");
110 tprint(string_mode(m));
111 }
112
113 int get_mode_id(void)
114 {
115 int m = cur_list.mode_field;
116 if (m > 0) {
117 switch (m / (max_command_cmd + 1)) {
118 case 0:
119 return 'v';
120 break;
121 case 1:
122 return 'h';
123 break;
124 case 2:
125 return 'm';
126 break;
127 default:
128 return '\0';
129 break;
130 }
131 } else if (m == 0) {
132 return 'n';;
133 } else {
134 switch ((-m) / (max_command_cmd + 1)) {
135 case 0:
136 return 'V';
137 break;
138 case 1:
139 return 'H';
140 break;
141 case 2:
142 return 'M';
143 break;
144 default:
145 return '\0';
146 break;
147 }
148 }
149 }
150
151 /*tex
152
153 The state of affairs at any semantic level can be represented by five values:
154
155 |mode| is the number representing the semantic mode, as just explained.
156
157 |head| is a |pointer| to a list head for the list being built; |link(head)|
158 therefore points to the first element of the list, or to |null| if the list is
159 empty.
160
161 |tail| is a |pointer| to the final node of the list being built; thus,
162 |tail=head| if and only if the list is empty.
163
164 |prev_graf| is the number of lines of the current paragraph that have already
165 been put into the present vertical list.
166
167 |aux| is an auxiliary |memory_word| that gives further information that is needed
168 to characterize the situation.
169
170 In vertical mode, |aux| is also known as |prev_depth|; it is the scaled value
171 representing the depth of the previous box, for use in baseline calculations, or
172 it is |<=-1000|pt if the next box on the vertical list is to be exempt from
173 baseline calculations. In horizontal mode, |aux| is also known as |space_factor|;
174 it holds the current space factor used in spacing calculations. In math mode,
175 |aux| is also known as |incompleat_noad|; if not |null|, it points to a record
176 that represents the numerator of a generalized fraction for which the denominator
177 is currently being formed in the current list.
178
179 There is also a sixth quantity, |mode_line|, which correlates the semantic nest
180 with the user's input; |mode_line| contains the source line number at which the
181 current level of nesting was entered. The negative of this line number is the
182 |mode_line| at the level of the user's output routine.
183
184 A seventh quantity, |eTeX_aux|, is used by the extended features eTeX. In math
185 mode it is known as |delim_ptr| and points to the most recent |fence_noad| of a
186 |math_left_group|.
187
188 In horizontal mode, the |prev_graf| field is used for initial language data.
189
190 The semantic nest is an array called |nest| that holds the |mode|, |head|,
191 |tail|, |prev_graf|, |aux|, and |mode_line| values for all semantic levels below
192 the currently active one. Information about the currently active level is kept in
193 the global quantities |mode|, |head|, |tail|, |prev_graf|, |aux|, and
194 |mode_line|, which live in a struct that is ready to be pushed onto |nest| if
195 necessary.
196
197 The math field is used by various bits and pieces in |texmath.w|
198
199 This implementation of \TeX\ uses two different conventions for representing
200 sequential stacks. @^stack conventions@>@^conventions for representing stacks@>
201
202 1) If there is frequent access to the top entry, and if the stack is essentially
203 never empty, then the top entry is kept in a global variable (even better would
204 be a machine register), and the other entries appear in the array
205 $\\{stack}[0\to(\\{ptr}-1)]$. The semantic stack is handled this way.
206
207 2) If there is infrequent top access, the entire stack contents are in the array
208 $\\{stack}[0\to(\\{ptr}-1)]$. For example, the |save_stack| is treated this way,
209 as we have seen.
210
211 In |nest_ptr| we have the first unused location of |nest|, and |max_nest_stack|
212 has the maximum of |nest_ptr| when pushing. In |shown_mode| we store the most
213 recent mode shown by \.{\\tracingcommands} and with |save_tail| we can examine
214 whether we have an auto kern before a glue.
215
216 */
217
218 list_state_record *nest;
219 int nest_ptr, max_nest_stack, shown_mode;
220 halfword save_tail;
221
222 /*tex
223
224 We will see later that the vertical list at the bottom semantic level is split
225 into two parts; the ``current page'' runs from |page_head| to |page_tail|, and
226 the ``contribution list'' runs from |contrib_head| to |tail| of semantic level
227 zero. The idea is that contributions are first formed in vertical mode, then
228 ``contributed'' to the current page (during which time the page-breaking
229 decisions are made). For now, we don't need to know any more details about the
230 page-building process.
231
232 */
233
234 void initialize_nesting(void)
235 {
236 nest_ptr = 0;
237 max_nest_stack = 0;
238 shown_mode = 0;
239 cur_list.mode_field = vmode;
240 cur_list.head_field = contrib_head;
241 cur_list.tail_field = contrib_head;
242 cur_list.eTeX_aux_field = null;
243 cur_list.prev_depth_field = ignore_depth;
244 cur_list.space_factor_field = 1000;
245 cur_list.incompleat_noad_field = null;
246 cur_list.ml_field = 0;
247 cur_list.pg_field = 0;
248 cur_list.dirs_field = null;
249 init_math_fields();
250 }
251
252 /*tex
253
254 Here is a common way to make the current list grow:
255
256 */
257
258 void tail_append(halfword p)
259 {
260 couple_nodes(cur_list.tail_field, p);
261 cur_list.tail_field = vlink(cur_list.tail_field);
262 }
263
264 halfword pop_tail(void)
265 {
266 halfword n, r;
267 if (cur_list.tail_field != cur_list.head_field) {
268 r = cur_list.tail_field;
269 if (vlink(alink(cur_list.tail_field)) == cur_list.tail_field) {
270 n = alink(cur_list.tail_field);
271 } else {
272 n = cur_list.head_field;
273 while (vlink(n) != cur_list.tail_field)
274 n = vlink(n);
275 }
276 cur_list.tail_field = n;
277 alink(r) = null;
278 vlink(n) = null;
279 return r;
280 } else {
281 return null;
282 }
283 }
284
285 /*tex
286
287 When \TeX's work on one level is interrupted, the state is saved by calling
288 |push_nest|. This routine changes |head| and |tail| so that a new (empty) list is
289 begun; it does not change |mode| or |aux|.
290
291 */
292
293 void push_nest(void)
294 {
295 if (nest_ptr > max_nest_stack) {
296 max_nest_stack = nest_ptr;
297 if (nest_ptr == nest_size)
298 overflow("semantic nest size", (unsigned) nest_size);
299 }
300 incr(nest_ptr);
301 cur_list.mode_field = nest[nest_ptr - 1].mode_field;
302 cur_list.head_field = new_node(temp_node, 0);
303 cur_list.tail_field = cur_list.head_field;
304 cur_list.eTeX_aux_field = null;
305 cur_list.ml_field = line;
306 cur_list.pg_field = 0;
307 cur_list.dirs_field = null;
308 cur_list.prev_depth_field = nest[nest_ptr - 1].prev_depth_field;
309 cur_list.space_factor_field = nest[nest_ptr - 1].space_factor_field;
310 cur_list.incompleat_noad_field = nest[nest_ptr - 1].incompleat_noad_field;
311 init_math_fields();
312 }
313
314 /*tex
315
316 Conversely, when \TeX\ is finished on the current level, the former state is
317 restored by calling |pop_nest|. This routine will never be called at the lowest
318 semantic level, nor will it be called unless |head| is a node that should be
319 returned to free memory.
320
321 */
322
323 void pop_nest(void)
324 {
325 flush_node(cur_list.head_field);
326 decr(nest_ptr);
327 }
328
329 /*tex
330
331 Here is a procedure that displays what \TeX\ is working on, at all levels.
332
333 */
334
335 void show_activities(void)
336 {
337 /*tex Index into |nest|: */
338 int p;
339 /*tex The mode: */
340 int m;
341 /*tex For showing the current page: */
342 halfword q, r;
343 /*tex Ditto: */
344 int t;
345 tprint_nl("");
346 print_ln();
347 for (p = nest_ptr; p >= 0; p--) {
348 m = nest[p].mode_field;
349 tprint_nl("### ");
350 print_mode(m);
351 tprint(" entered at line ");
352 print_int(abs(nest[p].ml_field));
353 if (nest[p].ml_field < 0)
354 tprint(" (\\output routine)");
355 if (p == 0) {
356 /*tex Show the status of the current page */
357 if (page_head != page_tail) {
358 tprint_nl("### current page:");
359 if (output_active)
360 tprint(" (held over for next output)");
361 show_box(vlink(page_head));
362 if (page_contents > empty) {
363 tprint_nl("total height ");
364 print_totals();
365 tprint_nl(" goal height ");
366 print_scaled(page_goal);
367 r = vlink(page_ins_head);
368 while (r != page_ins_head) {
369 print_ln();
370 tprint_esc("insert");
371 t = subtype(r);
372 print_int(t);
373 tprint(" adds ");
374 if (count(t) == 1000)
375 t = height(r);
376 else
377 t = x_over_n(height(r), 1000) * count(t);
378 print_scaled(t);
379 if (type(r) == split_up_node) {
380 q = page_head;
381 t = 0;
382 do {
383 q = vlink(q);
384 if ((type(q) == ins_node)
385 && (subtype(q) == subtype(r)))
386 incr(t);
387 } while (q != broken_ins(r));
388 tprint(", #");
389 print_int(t);
390 tprint(" might split");
391 }
392 r = vlink(r);
393 }
394 }
395 }
396 if (vlink(contrib_head) != null)
397 tprint_nl("### recent contributions:");
398 }
399 show_box(vlink(nest[p].head_field));
400 /*tex Show the auxiliary field, |a|. */
401 switch (abs(m) / (max_command_cmd + 1)) {
402 case 0:
403 tprint_nl("prevdepth ");
404 if (nest[p].prev_depth_field <= ignore_depth)
405 tprint("ignored");
406 else
407 print_scaled(nest[p].prev_depth_field);
408 if (nest[p].pg_field != 0) {
409 tprint(", prevgraf ");
410 print_int(nest[p].pg_field);
411 if (nest[p].pg_field != 1)
412 tprint(" lines");
413 else
414 tprint(" line");
415 }
416 break;
417 case 1:
418 tprint_nl("spacefactor ");
419 print_int(nest[p].space_factor_field);
420 break;
421 case 2:
422 if (nest[p].incompleat_noad_field != null) {
423 tprint("this will be denominator of:");
424 show_box(nest[p].incompleat_noad_field);
425 }
426 break;
427 }
428 }
429 }