Debian Codegraph @

texmf-dist/source/latex/base/ltpara.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: ltpara.dtx
4 %% Copyright (C) 2020-2025
5 %% Frank Mittelbach, The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 %
15 %%% From File: ltpara.dtx
16 %
17 %<*driver>
18 % \fi
19 \ProvidesFile{ltpara.dtx}
20 [2025/02/07 v1.0n LaTeX Kernel (paragraph hooks)]
21 % \iffalse
22 %
23 \documentclass{l3doc}
24 \GetFileInfo{ltpara.dtx}
25
26 \EnableCrossrefs
27 \CodelineIndex
28 \begin{document}
29 \DocInput{ltpara.dtx}
30 \end{document}
31 %</driver>
32 %
33 % \fi
34 %
35 % \providecommand\hook[1]{\texttt{#1}}
36 % \providecommand\env[1]{\texttt{#1}}
37 %
38 %
39 %
40 % \title{The \texttt{ltpara.dtx} code\thanks{This file has version
41 % \fileversion\ dated \filedate, \copyright\ \LaTeX\
42 % Project.}}
43 % \author{Frank Mittelbach}
44 %
45 % \maketitle
46 %
47 %
48 % \begin{abstract}
49 % This code defines four special kernel hooks to support paragraph
50 % tagging as well as four public hooks which can be occasionally
51 % useful.
52 % \end{abstract}
53 %
54 % \tableofcontents
55 %
56 %
57 % \section{Introduction}
58 %
59 %
60 % The building of paragraphs in the \TeX{} engine(s) has a number
61 % of peculiarities that makes it on one hand fairly flexible but on
62 % the other hand somewhat awkward to control or reliably to extend.
63 % Thus to better understand the code below we start with a brief introduction
64 % of the mechanism; for more details refer to the
65 % \TeX{}book~\cite[chap.~14]{texbook} (for the full truth you may
66 % even have to study the program code).
67 %
68 % \subsection{The default processing done by the engine}
69 %
70 % \TeX{} automatically starts building a paragraph when it is
71 % currently in vertical mode and encounters anything that can only
72 % live in horizontal mode. Most often this is a character, but
73 % there are also many commands that can be used only in horizontal mode.
74 % If any of them is encountered, \TeX{} will immediately back up
75 % (i.e., the character or command is read later again), adds a
76 % \cs{parskip} glue to the current vertical list unless the list is
77 % empty, switches to
78 % horizontal mode, starts its special \enquote{start of paragraph
79 % processing} and only then rereads the character or command that
80 % caused the mode change.\footnote{Already not quite true: the
81 % command \cs{noindent} starts the paragraph but influences the
82 % special processing by suppressing the paragraph indentation box
83 % normally inserted by it.}
84 %
85 % This \enquote{start of paragraph
86 % processing} first adds an empty box at the start of the
87 % horizontal list of width \cs{parindent} (which represents the
88 % paragraph indentation) unless the paragraph was started with
89 % \cs{noindent} in which case no such box is
90 % added\footnote{That's a bit different from placing a zero-sized
91 % box!}. It then reads and processes all tokens stored in the
92 % special engine token register \cs{everypar}. After that it reads
93 % and processes whatever has caused the paragraph to start.
94 %
95 % Thus out of the box, \TeX{} offers the possibility to put some
96 % special code into \cs{everypar} to gain control at (more or less)
97 % the start of the paragraph. For example, in LaTeX{} and a number
98 % of packages, special code like the following is sometimes used:
99 %\begin{verbatim}
100 % \everypar{{\setbox\z@\lastbox}\everypar{} ...}
101 %\end{verbatim}
102 % This removes the paragraph indentation box again (that was already
103 % placed by \TeX), then resets \cs{everypar} so that it doesn't do
104 % anything on the next paragraph start and then does whatever it
105 % wants to do, e.g., in an \cs{item} of a list it will typeset the label in
106 % front of the paragraph text.
107 % However, there is only one such \cs{everypar} token register and
108 % if different packages and/or the kernel all attempt to add their
109 % own code here, coordination is very difficult if not impossible.
110 %
111 % The process when the paragraph ends has different mechanisms and interfaces.
112 % A paragraph ends when the engine primitive \cs{par} is called
113 % while \TeX{} is in unrestricted horizontal mode, i.e., is
114 % building a paragraph. At other times this primitive does nothing
115 % or generates as an error depending on the mode \TeX{} is in,
116 % e.g., the \cs{par} in
117 % \verb=\hbox{a\par b}= is ignored, but \verb=$a\par b$= would complain.
118 %
119 % If this primitive ends the paragraph it does some special
120 % \enquote{end of horizontal list} processing, then calls \TeX{}'s paragraph
121 % builder; this breaks the horizontal list into lines and then these
122 % lines are added as boxes to the enclosing vertical list and
123 % \TeX{} returns to vertical mode.
124 %
125 % This \cs{par} command can be given explicitly, but there are also
126 % situations in which \TeX{} is generating it on the fly. Most
127 % often this happens when \TeX\ encounters a blank line which is
128 % automatically changed to a \cs{par} command which is then
129 % executed. The other possibility is that \TeX{} encounters a
130 % command which is incompatible with horizontal processing, e.g.,
131 % \cs{vskip} (a request for adding vertical space). In such cases it
132 % silently backs up, and inserts a \cs{par} in the hope that this
133 % gets it out of horizontal mode and makes the vertical command
134 % acceptable.
135
136 % The important point to note here is that \TeX{} really inserts
137 % the command with the name \cs{par}, which can be redefined.
138 % Thus, it may not have
139 % its original \enquote{primitive} meaning and therefore may not end the
140 % horizontal list and call the paragraph builder. This approach
141 % offers some flexibility but also allows you to easily produce a
142 % \TeX{} document that loops forever, for example, the simple line
143 %\begin{verbatim}
144 % A \let\par\relax \vskip
145 %\end{verbatim}
146 % will start a horizontal list at \texttt{A}, redefines \cs{par},
147 % then sees \cs{vskip} and inserts \cs{par} to end the
148 % paragraph. But this now only runs \cs{relax} so nothing changes
149 % and \cs{vskip} is read again, issues a \cs{par} which \ldots. In
150 % short, it only takes a plain \TeX{} document with five tokens to run
151 % forever (since no memory is consumed and therefore eventually
152 % exhausted).
153 %
154 % There is no way other than changing \cs{par} to gain control at
155 % the end of a paragraph, i.e., there is no token list like
156 % \cs{everypar} that is inserted. Hence the only way to change the
157 % default behavior is to modify the action that \cs{par} executes,
158 % with similar issues as outlined before: different processes need
159 % to ensure that they do not overwrite their modifications or
160 % worse, think that the \cs{par} in front of them is the engine
161 % primitive while in fact it has already been changed by other
162 % code.
163 %
164 % To make matters slightly worse there are a few places where
165 % \TeX{} handles the situation differently (most likely for speed
166 % reasons back when computers were much slower). If \TeX{} finds
167 % itself in unrestricted horizontal mode at the end of building a
168 % vertical box (for an \cs{insert}, \cs{vadjust} or
169 % executing the output routine code), it will finish the horizontal
170 % list not by issuing a \cs{par} command (which would be consistent
171 % with all other places) but by simply executing the primitive meaning
172 % of \cs{par}, regardless of the actual definition that \cs{par}
173 % has at the time.
174 %
175 % Thus, if you have carefully crafted a redefined \cs{par} to execute
176 % some special actions at the end of a paragraph and you write
177 % something like
178 %\begin{verbatim}
179 % \vbox{Some paragraph ... text.}
180 %\end{verbatim}
181 % you will find that your code does not get run for the last paragraph
182 % in that box. \LaTeX{} avoids this problem, by making sure that
183 % its boxes (such as \cs{parbox} or the \env{minipage}
184 % environment, etc.) all internally add an explicit \cs{par} at the
185 % end so that such code is run and \TeX{} finds itself in vertical
186 % mode already without the need to start up the paragraph builder
187 % internally. But, of course, this only works for boxes under direct
188 % control of the \LaTeX{} kernel; if some package uses low-level
189 % \cs{vbox}es without adding this precaution the \TeX{}
190 % optimization kicks in and no special \cs{par} code is executed.
191 %
192 % And there is another optimization that is painful: if a paragraph
193 % is interrupted by a mathematical display, e.g., \verb=\[...\]= in
194 % \LaTeX{} or \verb=$$...$$= in plain \TeX{}, then \TeX{} will
195 % resume horizontal mode afterward, i.e., it will start to build
196 % a new horizontal
197 % list without inserting an indentation box or \cs{everypar} at
198 % that point. However, if that list immediately ends with an
199 % explicit or implicit \cs{par} then \TeX{} will simply throw away
200 % this \enquote{null} paragraph and not do its usual \enquote{end
201 % of horizontal list} processing, so this special case also needs to be
202 % accounted for when introducing any extended processing.
203 %
204 %
205 %
206 % \section{The new mechanism implemented for \LaTeX{}}
207 %
208 % To improve the situation (and also to support automatic tagging
209 % of PDF documents) we now offer public as well as private hooks at
210 % the start and end of the paragraph processing. The public hooks
211 % can be used by packages (or by the user in the preamble or
212 % within the document) and using the hook mechanisms it is possible
213 % to reorder or arrange code from different packages in such a way that
214 % these can safely coexist.
215 %
216 % To make that happen we have to make use of the basic
217 % functionality that is offered by \TeX{}, e.g., we install
218 % special code inside \cs{everypar} to provide hooks at the
219 % beginning and we redefine \cs{par} to do some special processing
220 % when appropriate to install hooks at the end of the paragraph.
221 %
222 % In order to make this work, we have to ensure that package use of
223 % \cs{everypar} is not overwriting our code. This is done through a
224 % trick: we basically hide the real \cs{everypar} from the packages
225 % and offer them a new token register (with the same name). So if
226 % they install their own code it doesn't overwrite ours. Our code
227 % then inserts the new \cs{everypar} at the right place inside the
228 % process so that it looks as if it was the primitive
229 % \cs{everypar}.\footnote{Ideally, \cs{everypar} wouldn't be used
230 % at all by packages and instead they would simply write their code
231 % into the hooks now offered by the kernel. However, while this is
232 % the longterm goal and clearly an improvement (because then the
233 % packages do no longer need to worry about getting their code
234 % overwritten or needing to account for already existing code in
235 % \cs{everypar}), this will not happen overnight. For that reason
236 % support for this legacy method is retained.}
237 %
238 % At the end of the paragraph it would be great if we could use a
239 % similar trick. However, due to the fact that \TeX{} inserts the
240 % token \cs{par} (that doesn't have a defined meaning) we can't hide
241 % \enquote{the real thing\textsuperscript{TM}} and offer the
242 % package an indistinguishable alternate.
243 %
244 % Fortunately, \LaTeX{} has already redefined \cs{par} for its own
245 % purposes. As a result there aren't many packages that attempt to
246 % change \cs{par}, because without a lot of extra care that would
247 % fail miserably. But the bottom line is that, if you load a package that
248 % alters \cs{par} then the end of paragraph hooks are most likely
249 % not executing while that redefinition is
250 % active.\footnote{Similarly to the \cs{everypar} situation, the
251 % remedy is that such packages stop doing this and instead add
252 % their alterations into the paragraph hooks now provided.}
253 %
254 %
255 %
256 % \subsection{The provided hooks}
257 %
258 %
259 % \begin{variable}{para/before,
260 % para/begin,
261 % para/end,
262 % para/after
263 % }
264 % The following four public hooks are defined and executed for
265 % each paragraph:
266 % \begin{description}
267 %
268 % \item[\hook{para/before}]
269 %
270 % This hook is executed after the kernel hook
271 % \cs{@kernel@before@para@before} (discussed below) in vertical
272 % mode immediately after \TeX{} has contributed \cs{parskip} to
273 % the vertical list and before the actual paragraph processing in
274 % horizontal mode starts.
275 %
276 % This hook should either not produce any typeset material or add
277 % only vertical material. If it starts a paragraph an error is
278 % generated. The reason is that we are in the starting process of
279 % processing a paragraph and so this would lead to endless
280 % recursion.\footnote{One could allow it but only if the newly
281 % started paragraph is processed without any hooks. Furthermore
282 % correct spacing would be a bit of a nightmare so for now this
283 % is forbidden.}
284 %
285 % \end{description}
286 % \end{variable}
287 %
288 % \vspace{-\bigskipamount}
289 %
290 % \begin{description}
291 %
292 % \item[\hook{para/begin}]
293 %
294 % This hook is executed after the kernel hook
295 % \cs{@kernel@before@para@begin} (discussed below) in horizontal
296 % mode immediately before the indentation box is placed (if there
297 % is any, i.e., if the paragraph hasn't been started with
298 % \cs{noindent}).
299 %
300 % The indentation box to be typeset is available to the hook as
301 % \cs{IndentBox} and its automatic placement (after the hook is
302 % executed) can be prevented through \cs{OmitIndent}.
303 % More precisely \cs{OmitIndent} voids the box.
304 %
305 % The indentation box is then typeset directly
306 % after the hook execution by something equivalent to
307 % \cs{box}\cs{IndentBox} followed by the current content of
308 % the token register \cs{everypar} that it is available to the
309 % kernel or to packages (that run some legacy code).
310 %
311 % One has to be careful not to add any code to the hook that
312 % starts its own paragraph (e.g., by adding a \cs{parbox} or a
313 % \cs{marginpar} inside) because that would call the hook inside
314 % again (as a new paragraph is started there) and thus lead to an
315 % endless recursion ending only after exhausting the available
316 % memory. This can only be done by making sure that is not
317 % executed for the inner paragraphs (or at least not recursively
318 % forever).
319 %
320 %
321 % \item[\hook{para/end}]
322 %
323 % This hook is executed at the end of a paragraph when \TeX{} is
324 % ready to return to vertical mode and after it has removed the
325 % last horizontal glue (but not any kerns) placed on the horizontal
326 % list. The code is still executed in horizontal mode so it is
327 % possible to add further horizontal material at this point, but
328 % it should not alter the mode (even a temporary exit from
329 % horizontal mode would create chaos---any attempt will cause an
330 % error message)! After the hook has ended the kernel hook
331 % \cs{@kernel@after@para@end} is executed and then \TeX{} returns to
332 % vertical mode.
333 %
334 % The hook is offered as public hook, but because of the
335 % requirement to stay within horizontal mode one needs to be
336 % careful in what is placed into the hook.\footnote{Maybe we
337 % should guard against that, but it would be rather tricky to
338 % implement as mode changes can happen across group boundaries so
339 % one would need to keep a private stack just for that. Well,
340 % something to ponder.}
341 %
342 % This hook is implemented as a reversed hook.
343 %
344 % \item[\hook{para/after}]
345 %
346 % This hook is executed directly after \TeX{} has returned to
347 % vertical mode and after any material that migrated out of the
348 % horizontal list (e.g., from a \cs{vadjust}) has processed.
349 %
350 % This hook should either not produce any typeset material or add
351 % only vertical material.
352 % However, for this hook starting a new paragraph is not a
353 % disaster so that it isn't prevented.
354 %
355 % This hook is implemented as a reversed hook.
356 %
357 % Once that hook code has been processed the kernel hook
358 % \cs{@kernel@after@para@after} is executed as the final action
359 % of the paragraph processing.
360 %
361 % \end{description}
362 %
363 % \begin{variable}{\@kernel@before@para@before,
364 % \@kernel@after@para@after,
365 % \@kernel@before@para@begin,
366 % \@kernel@after@para@end,
367 % }
368 % As already mentioned above there are also four kernel hooks that
369 % are executed at the start and end of the processing.
370 % \begin{description}
371 %
372 % \item[\cs{@kernel@before@para@before}]
373 % For future extensions, not currently used by the kernel.
374 %
375 %
376 % \item[\cs{@kernel@after@para@after}]
377 % For future extensions, not currently used by the kernel.
378 %
379 %
380 % \item[\cs{@kernel@before@para@begin}]
381 %
382 % Used by the kernel to implement tagging. This hook is executed
383 % at the very beginning of a paragraph after \TeX{} has switched to
384 % horizontal mode but before any indentation box got added or any
385 % \cs{everypar} was run.
386 %
387 % It should not generate typeset material that could alter the
388 % position. Note that it should never leave hmode, otherwise you
389 % will end with a loop! We could guard against this, but since it
390 % is an internal kernel hook that shouldn't be touched this isn't
391 % checked.
392 % \end{description}
393 % \end{variable}
394 %
395 % \vspace{-\bigskipamount}
396 %
397 % \begin{description}
398 %
399 % \item[\cs{@kernel@after@para@end}]
400 %
401 % Used by the kernel to implement tagging. It is executed
402 % directly after the public \hook{para/end} hook. After it there
403 % is a quick check that we are still in horizontal mode, i.e.,
404 % that the public hook has not mistakenly ended horizontal mode
405 % prematurely (this is an incomplete check just testing the mode
406 % and could perhaps be improved (at the cost of speed)).
407 %
408 % \end{description}
409 %
410 %
411 % \subsection{Altered and newly provided commands}
412 %
413 % \begin{function}{\par,\endgraf,\para_end:}
414 % An explicit request for ending a paragraph is provided in plain
415 % \TeX{} under the name \cs{endgraf}, which simply uses the
416 % primitive meaning (regardless of what \cs{par} may have as its
417 % current definition). In \LaTeX{} \cs{endgraf} (with that behavior)
418 % was originally also available.
419 %
420 % With the new paragraph handling in \LaTeX{}, ending a paragraph
421 % means a bit more than just calling the engine's paragraph
422 % builder: the process also has to add any hook code for the end of
423 % a paragraph. Thus
424 % \cs{endgraf} was changed to provide this additional functionality
425 % (along with \cs{par} remaining subject to its current meaning).
426 %
427 % The \pkg{expl3} name for this functionality is \cs{para_end:}.
428 % \end{function}
429 %
430 % \begin{quote}
431 % \textbf{Note:} \em The next two commands are still under
432 % discussion and may slightly change their semantics (as described
433 % in the document) and/or their names between now and the 2021
434 % Spring release!
435 % \end{quote}
436 %
437 % \begin{function}{\OmitIndent,\para_omit_indent:}
438 % Inside the \hook{para/begin} hook one can use this command to
439 % suppress the indentation box at the start of the
440 % paragraph. (Technically it is possible to use this command
441 % outside the hook as well, but this should not be relied upon.)
442 % The box itself remains available for use.
443 %
444 % The \pkg{expl3} name for the function is \cs{para_omit_indent:}.
445 % \end{function}
446 %
447 % \begin{variable}{\IndentBox,\g_para_indent_box}
448 % The box register holding the indentation box for the paragraph is
449 % available for inspection (or changes) inside hooks. It remains
450 % available even if the \cs{OmitIndent} command was
451 % used; in that case it will just not be automatically placed.
452 %
453 % The \pkg{expl3} name for the box register is \cs{g_para_indent_box}.
454 % \end{variable}
455 %
456 %
457 % \begin{function}{\RawIndent,\para_raw_indent:,
458 % \RawNoindent,\para_raw_noindent:,
459 % \RawParEnd,\para_raw_end:}
460 % \begin{syntax}
461 % \cs{RawIndent} \textit{hmode material} \cs{RawParEnd}
462 % \cs{RawNoindent} \textit{hmode material} \cs{RawParEnd}
463 % \end{syntax}
464 %
465 % The commands \cs{RawIndent} and \cs{RawNoindent} are not meant
466 % for normal paragraph building (where the result is a textual
467 % paragraph in the traditional meaning of the word), but for
468 % special cases where \TeX{}'s low-level algorithm is used to
469 % achieve special effects, but where the result is not a
470 % \enquote{paragraph}.
471 %
472 % They are called \enquote{raw}, because they bypass \LaTeX{}'s
473 % hook mechanism for paragraphs and simply invoke the low-level
474 % \TeX{} algorithm. I.e., they are like the original \TeX{}
475 % primitives \cs{indent} and \cs{noindent} (that is they execute no
476 % hooks other than \cs{everypar}) except that they can only be used
477 % in vertical mode and generate an error if found elsewhere.
478 %
479 % To avoid issues a paragraph started by them should always be
480 % ended by \cs{RawParEnd}\footnote{Technical note for those who
481 % know their \textit{\TeX book\/}: the \cs{RawParEnd} command
482 % invokes the original \TeX{} engine definition of \cs{par} that
483 % (solely) triggers the paragraph builder in \TeX{} when found
484 % inside unrestricted horizontal mode and does nothing in other
485 % processing modes.}
486 % and not by \cs{par} (or a blank line), because the latter will execute
487 % hooks which then have no counterpart at the beginning of the
488 % paragraph. It is the responsibility of the programmer to make
489 % sure that they are properly paired. This also means that one
490 % should not put arbitrary user content between these commands if
491 % that content could contain stray \cs{par}s.
492 %
493 % The \pkg{expl3} names for the functions are
494 % \cs{para_raw_indent:}, \cs{para_raw_indent:} and
495 % \cs{para_raw_end:}.
496 % \end{function}
497 %
498 %
499 %
500 % \subsection{Examples}
501 %
502 % None of the examples in this section are meant for real use as
503 % they are far too simpleminded but they should give some ideas of
504 % what could be possible if a bit more care is applied.
505 %
506 % \subsubsection{Testing the mechanism}
507 %
508 % The idea is to output for each paragraph encountered some
509 % information: a paragraph sequence number, a level number in roman
510 % numerals, the environment in which this paragraph appears, and
511 % the line number where the start or end of the paragraph is, e.g.,
512 % something like
513 %\begin{verbatim}
514 % PARA: 1-i start (document env. on input line 38)
515 % PARA: 1-i end (document env. on input line 38)
516 % PARA: 2-i start (document env. on input line 40)
517 % PARA: 3-ii start (minipage env. on input line 40)
518 % PARA: 3-ii end (minipage env. on input line 40)
519 % PARA: 2-i end (document env. on input line 41)
520 %\end{verbatim}
521 % As you can see paragraph 2 starts on line 40 and ends on 41 and
522 % inside a minipage started paragraph 3 (start and end on line 40).
523 % If you run this on some document you will find that \LaTeX{}
524 % considers more things \enquote{a paragraph} than you have
525 % probably thought.
526 %
527 % This was generated by the following hook code:
528 %\begin{verbatim}
529 % \newcounter{paracnt} % sequence counter
530 % \newcounter{paralevel} % level counter
531 %\end{verbatim}
532 %
533 % To support paragraph nesting we need to maintain a stack of the
534 % sequence numbers. This is most easily done using \pkg{expl3}
535 % functions, so we switch over. This is not a very general
536 % implementation, just enough for what we need and a bit of
537 % \LaTeXe{} thrown in as well. When popping, the result gets stored
538 % in \cs{paracntvalue} and the \cs{ERROR} should never happen
539 % because it means we have tried to pop from an empty stack.
540 %\begin{verbatim}
541 % \ExplSyntaxOn
542 % \seq_new:N \g_para_seq
543 % \cs_new:Npn \ParaPush
544 % {\seq_gpush:No \g_para_seq {\the\value{paracnt}}}
545 % \cs_new:Npn \ParaPop {\seq_gpop:NNF \g_para_seq \paracntvalue \ERROR }
546 % \ExplSyntaxOff
547 %\end{verbatim}
548 % At the start of the paragraph increment both sequence counter and
549 % level and also save the then current sequence number on our stack.
550 %\begin{verbatim}
551 % \makeatletter % because we use a few internal 2e commands
552 % \AddToHook{para/begin}{%
553 % \stepcounter{paracnt}\stepcounter{paralevel}%
554 % \ParaPush
555 %\end{verbatim}
556 % To display the sequence number we \cs{typeout} the current
557 % sequence and level number. The command \cs{@currenvir} gives us
558 % the current environment and \cs{on@line} produces a space and
559 % the current input line number.
560 %\begin{verbatim}
561 % \typeout{PARA: \arabic{paracnt}-\roman{paralevel} start
562 % (\@currenvir\space env.\on@line)}%
563 %\end{verbatim}
564 % We also typeset the sequence number as a tiny red number in a box
565 % that takes up no horizontal space. This helps us seeing where
566 % \LaTeX{} sees the start and end of the paragraphs in the
567 % document.
568 %\begin{verbatim}
569 % \llap{\color{red}\tiny\arabic{paracnt}\ }%
570 % }
571 %\end{verbatim}
572 %
573 % At the end of the paragraph we display sequence number and
574 % level again. The level counter has the correct value but we need
575 % to retrieve the right sequence value by popping it off the stack
576 % after which it is available in \cs{paracntvalue} the way we have
577 % set this up above.
578 %\begin{verbatim}
579 % \AddToHook{para/end}{%
580 % \ParaPop
581 % \typeout{PARA: \paracntvalue-\roman{paralevel} end \space\space
582 % (\@currenvir\space env.\on@line)}%
583 %\end{verbatim}
584 % We also typeset again a tiny red number with that value, this
585 % time sticking out to the right.\footnote{Note that this can alter
586 % the document pagination, because a paragraph ending in a display
587 % (e.g., an equation) will get an extra line---in that case our tiny number
588 % has an effect even though it doesn't take up any space, because
589 % it paragraph is no longer empty and thus isn't dropped!}
590 % We also decrement the level counter since our level has finished.
591 %\begin{verbatim}
592 % \rlap{\color{red}\tiny\ \paracntvalue}%
593 % \addtocounter{paralevel}{-1}%
594 % }
595 % \makeatother
596 %\end{verbatim}
597 %
598 %
599 %
600 % \subsubsection{Mark the first paragraph of each \env{itemize}}
601 %
602 % The code for this is rather simple. We supply some code that is
603 % executed only once inside a hook at the start of
604 % each \env{itemize}. We explicitly change the color back and
605 % forth so that we don't introduce grouping around the paragraph.
606 %\begin{verbatim}
607 % \AddToHook{env/itemize/begin}{%
608 % \AddToHookNext{para/begin}{\color{blue}}%
609 % \AddToHookNext{para/end}{\color{black}}%
610 % }
611 %\end{verbatim}
612 % As a result the first paragraph of each \env{itemize} will appear
613 % in blue.
614 %
615 %
616 %
617 % \subsection{Some technical notes}
618 %
619 % The code tries hard to be transparent for package code, but of
620 % course any change means that there is a potential for breaking
621 % other code. So in section we collect a few cases that may be of
622 % importance if low-level code is dealing with paragraphs that are
623 % now behaving slightly differently. The notes are from issues we
624 % observed and will probably grow over time.
625 %
626 % \subsubsection{Glue items between paragraphs (found with \pkg{fancypar})}
627 %
628 % In the past \LaTeX{} placed two glue items between two
629 % consecutive paragraphs, e.g.,
630 %\begin{verbatim}
631 % text1 \par text2 \par
632 %\end{verbatim}
633 % would show something like
634 %\begin{verbatim}
635 % \glue(\parskip) 0.0 plus 1.0
636 % \glue(\baselineskip) 5.16669
637 %\end{verbatim}
638 % but now there is another \cs{parskip} glue (that is always 0pt):
639 %\begin{verbatim}
640 % \glue(\parskip) 0.0 plus 1.0
641 % \glue(\parskip) 0.0
642 % \glue(\baselineskip) 5.16669
643 %\end{verbatim}
644 % The reason is that we generate a \enquote{fake} paragraph to
645 % gain control and safely add the early hooks, but this generates
646 % an additional glue item. That item doesn't contribute anything
647 % vertically but if somebody writes code that unravels a constructed
648 % list using \cs{lastbox}, \cs{unskip} and \cs{unpenalty} then the
649 % code has to remove one additional glue item or else it will fail.
650
651 %
652 % ^^A \subsubsection{}
653 %
654 %
655 %
656 % ^^A \subsubsection{}
657 %
658 %
659 %
660 %
661 %
662 %
663 % \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex }
664 %
665 %
666 % \section{The Implementation}
667 %
668 % \begin{macrocode}
669 %<@@=para>
670 % \end{macrocode}
671 %
672 % \changes{v1.0g}{2021/05/24}{Use \cs{msg_...} instead of \cs{__kernel_msg...}}
673 %
674 %
675 % \begin{macrocode}
676 %<*2ekernel|latexrelease>
677 \ExplSyntaxOn
678 %<latexrelease>\NewModuleRelease{2021/06/01}{ltpara}
679 %<latexrelease> {Paragraph~handling~and~hooks}
680 % \end{macrocode}
681 %
682 %
683 %
684 % \subsection{Providing hooks for paragraphs}
685 %
686 %
687 % \begin{macro}{para/before,para/after,para/begin,para/end}
688 % The public hooks. They are implemented as a paired set of hooks.
689 % \begin{macrocode}
690 \hook_new_pair:nn{para/before}{para/after}
691 \hook_new_pair:nn{para/begin}{para/end}
692 % \end{macrocode}
693 % \end{macro}
694 %
695 %
696 %
697 % \begin{macro}{\@kernel@before@para@before,
698 % \@kernel@after@para@after,
699 % \@kernel@before@para@begin,
700 % \@kernel@after@para@end}
701 % The corresponding kernel hooks (for tagging and future extensions).
702 % \begin{macrocode}
703 \let \@kernel@before@para@before \@empty
704 \let \@kernel@before@para@begin \@empty
705 \let \@kernel@after@para@end \@empty
706 \let \@kernel@after@para@after \@empty
707 % \end{macrocode}
708 % \end{macro}
709 %
710 %
711 %
712 %
713 %
714 % \begin{macro}{\g_@@_standard_everypar_tl}
715 % Whenever \TeX{} starts a paragraph it inserts first an
716 % indentation box and then executes the tokens stored in
717 % \cs{tex_everypar:D} (known to \LaTeX{} as \cs{everypar}). We
718 % alter this behavior slightly here, so that hooks are added
719 % into the right place. Otherwise the process change remains
720 % transparent to any legacy code for this space.
721 %
722 % We keep the standard code to be used by \cs{tex_everypar:D} in a
723 % separate token list because we have to switch back and forth
724 % for error recovery and so altering \cs{tex_everypar:D} all the
725 % time should be a tiny bit faster.
726 % \begin{macrocode}
727 %<latexrelease>\IncludeInRelease{2023/06/01}
728 %<latexrelease> {\g_@@_standard_everypar_tl}{minipage~ fix}
729 \tl_new:N \g_@@_standard_everypar_tl
730 % \end{macrocode}
731 % Here is now its definition:
732 % \begin{macrocode}
733 \tl_gset:Nn \g_@@_standard_everypar_tl {
734 % \end{macrocode}
735 % First we remove the indentation box and store it in
736 % \cs{g_para_indent_box}. If there was none because the paragraph
737 % was started by \cs{noindent} the box register will be void.
738 % \begin{macrocode}
739 \box_gset_to_last:N \g_para_indent_box
740 % \end{macrocode}
741 %
742 % This will make the newly started horizontal list empty, so if we
743 % stop it now and return to vertical mode it will be dropped by
744 % \TeX{}. We do that but inside a group so that any \cs{parshape}
745 % settings will not get lost as we need them for later.
746 % \begin{macrocode}
747 \group_begin:
748 \tex_par:D
749 \group_end:
750 % \end{macrocode}
751 % We then change \cs{tex_everypar:D} to generate an error so that
752 % we can detect and report if the \hook{para/before} hook illegally
753 % changed out of vmode.
754 % \begin{macrocode}
755 \tex_everypar:D { \msg_error:nnnn { hooks }{ para-mode }{before}{vertical} }
756 \@kernel@before@para@before
757 \hook_use:n {para/before}
758 % \end{macrocode}
759 % Assuming the hooks have been well behaved it is time to return to
760 % horizontal mode and start the paragraph in earnest. We already
761 % have the indentation box saved away so we now have to restart the
762 % paragraph with an empty \cs{tex_everypar:D} and with
763 % \cs{tex_noindent:D}. And we need to make sure not to get another
764 % \cs{parskip} or rather (since we can't prevent that) that it is
765 % of zero size.
766 % \begin{macrocode}
767 \group_begin:
768 \tex_everypar:D {}
769 % \end{macrocode}
770 % There has been a long-standing problem with \LaTeX's minipages in
771 % that invisible material at the beginning of a minipage (such as a
772 % \cs{color} setting) would result in \cs{parskip} being added in
773 % front of the first paragraph---something that is not done by
774 % \TeX{} if a vertical list is completely empty. As this is
775 % happening on a very low-level in the engine it wasn't really
776 % possible to find out if this \cs{parskip} was added or if a space
777 % we see in front of the current point is legitimate. However, with
778 % the new paragraph handling we are in a better position: while we
779 % still don't know if there is such a space or not, we do know
780 % if we have just created an empty paragraph. Thus, if we now set
781 % \cs{parskip} to \texttt{-}\cs{parskip} the two will cancel each other
782 % if present and if the first was ignored because the vertical list
783 % was empty, then the second will be ignored too because it is
784 % still empty. Of course, we don't want to cancel always but only
785 % at the start of a minipage and that is signaled with the
786 % \texttt{@minipage} switch.
787 % \changes{v1.0l}{2023/01/30}{Backout \cs{parskip} at top of minipage (gh/989)}
788 % \begin{macrocode}
789 \skip_set:Nn \tex_parskip:D
790 { \if@minipage -\tex_parskip:D \else: \c_zero_skip \fi: }
791 \tex_noindent:D
792 \group_end:
793 % \end{macrocode}
794 % That brings us back to the start of the horizontal list but we
795 % need to change \cs{tex_everypar:D} back to its normal content in
796 % case there are nested paragraphs coming up.
797 % \begin{macrocode}
798 \tex_everypar:D{\g_@@_standard_everypar_tl}
799 % \end{macrocode}
800 %
801 % This is followed by executing the kernel and the public hook. The
802 % kernel hook is there to enable tagging.
803 % \begin{macrocode}
804 \@kernel@before@para@begin
805 \hook_use:n {para/begin}
806 % \end{macrocode}
807 % If we aren't in horizontal mode any longer the hooks above misbehaved.
808 % \changes{v1.0m}{2023/11/16}{Correct error message: hook
809 % left horizontal not vertical mode (gh/1182)}
810 % \begin{macrocode}
811 \if_mode_horizontal: \else:
812 \msg_error:nnnn { hooks }{ para-mode }{begin}{horizontal} \fi:
813 % \end{macrocode}
814 % Finally we reinsert the indentation box (unless suppressed) and
815 % then call \cs{everypar} the way legacy \LaTeX\ code expects it.
816 %
817 % However, adding the public \cs{everypar} is a bit tricky (see below) so
818 % we add that later, and indirectly.
819 % \begin{macrocode}
820 \@@_handle_indent:
821 % \the \everypar % <--- done differently below
822 }
823 % \end{macrocode}
824 % \changes{v1.0n}{2024/06/23}
825 % {Append \cs{everypar} toks to \cs{g__para_standard_everypar_tl},
826 % rollback 2023/06/01 (gh/1386)}
827 % \begin{macrocode}
828 %<latexrelease>\cs_set:Npn \@@_tmp:w #1#2#3#4#5 { }
829 %<latexrelease>\tl_gput_right:Nx \g_@@_standard_everypar_tl {
830 %<latexrelease> \exp_not:N \the
831 %<latexrelease> \exp_not:N \toks
832 %<latexrelease> \exp_after:wN \@@_tmp:w \token_to_meaning:N \everypar
833 %<latexrelease> \c_space_tl
834 %<latexrelease>}
835 %<latexrelease>\EndIncludeInRelease
836 %<latexrelease>\IncludeInRelease{2021/06/01}
837 %<latexrelease> {\g_@@_standard_everypar_tl}{minipage~ fix}
838 %<latexrelease>
839 %<latexrelease>\tl_gset:Nn \g_@@_standard_everypar_tl {
840 %<latexrelease> \box_gset_to_last:N \g_para_indent_box
841 %<latexrelease> \group_begin:
842 %<latexrelease> \tex_par:D
843 %<latexrelease> \group_end:
844 %<latexrelease> \tex_everypar:D { \msg_error:nnnn { hooks }{ para-mode }{before}{vertical} }
845 %<latexrelease> \@kernel@before@para@before
846 %<latexrelease> \hook_use:n {para/before}
847 %<latexrelease> \group_begin:
848 %<latexrelease> \tex_everypar:D {}
849 %<latexrelease> \skip_zero:N \tex_parskip:D
850 %<latexrelease> \tex_noindent:D
851 %<latexrelease> \group_end:
852 %<latexrelease> \tex_everypar:D{\g_@@_standard_everypar_tl}
853 %<latexrelease> \@kernel@before@para@begin
854 %<latexrelease> \hook_use:n {para/begin}
855 %<latexrelease> \if_mode_horizontal: \else:
856 %<latexrelease> \msg_error:nnnn { hooks }{ para-mode }{begin}{horizontal} \fi:
857 %<latexrelease> \@@_handle_indent:
858 %<latexrelease>}
859 % \end{macrocode}
860 % We also have to add the \cs{everypar} toks register at the
861 % end. In case of rollback this is already allocated and we have to
862 % find out the correct number (hope this is correctly done)
863 % \begin{macrocode}
864 %<latexrelease>\cs_set:Npn \@@_tmp:w #1#2#3#4#5 { }
865 %<latexrelease>\tl_gput_right:Nx \g_@@_standard_everypar_tl {
866 %<latexrelease> \exp_not:N \the
867 %<latexrelease> \exp_not:N \toks
868 %<latexrelease> \exp_after:wN \@@_tmp:w \token_to_meaning:N \everypar
869 %<latexrelease> \c_space_tl
870 %<latexrelease>}
871 %<latexrelease>\EndIncludeInRelease
872 % \end{macrocode}
873 % \end{macro}
874 %
875 % \begin{macro}[int]{\tex_everypar:D}
876 % \cs{tex_everypar:D} then only has to execute
877 % \cs{g_@@_standard_everypar_tl} by default.
878 % \begin{macrocode}
879 \tex_everypar:D{\g_@@_standard_everypar_tl}
880 % \end{macrocode}
881 % \end{macro}
882 %
883 %
884 %
885 % \begin{macro}[int]{\everypar}
886 %
887 % Tokens inserted at the beginning of the paragraph are placed into
888 % \cs{everypar} inside legacy \LaTeX{} code, e.g., by the list
889 % environments or by headings to handle \cs{clubpenalty}, etc. Now
890 % this isn't any longer the primitive but simply a toks register
891 % used in the code above but to legacy \LaTeX{} code that is
892 % transparent.
893 %
894 % There is, however, a problem: a handful packages use exactly the
895 % same trick and replace the primitive with a token register and
896 % call the token register inside the renamed primitive. That is
897 % they assume that \cs{everypar} is the primitive and that it will
898 % still be called at the start of the paragraph even if renamed.
899 %
900 % But if we have already replaced it by a token register then all
901 % they do is to give that token register a new name. Thus our code
902 % in \cs{tex_everypar:D} would call \cs{everypar} (which is now their
903 % token register) and the code that they added ends up in our
904 % token register which is then never used at all. A bit mind
905 % boggling I guess.
906 %
907 % So what we have to do is not to call the token register
908 % \cs{everypar} by its name inside \cs{tex_everypar:D} but by using
909 % its actual register number.
910 % \begin{macrocode}
911 \newtoks \everypar
912 % \end{macrocode}
913 %
914 % After we have allocated a new toks register with the name
915 % \cs{everypar} the actual register number is available (briefly)
916 % inside \cs{allocationnumber}. So instead of \cs{the}\cs{everypar}
917 % we have to put \cs{the}\cs{toks}\meta{allocated number} at the end of
918 % \cs{tex_everypar:D}.
919 %
920 % So what remains doing is to append a few tokens to the token list
921 % \cs{g_@@_standard_everypar_tl} which we do now. We use \texttt{x}
922 % expansion here to get the value of \cs{allocationnumber} in, all
923 % the other tokens should not be expanded at this point.
924
925 % One important point here is to terminate the register allocation
926 % number with a real space. This space will get swallowed up when
927 % the number is read. Anything else, such as \cs{scan_stop:} would
928 % remain in the input and that would mean that it would interfere
929 % with \cs{everypar} code that attempts to scan ahead to see how
930 % the paragraph text starts.
931 % \begin{macrocode}
932 \tl_gput_right:Nx \g_@@_standard_everypar_tl {
933 \exp_not:N \the
934 \exp_not:N \toks
935 \the \allocationnumber
936 \c_space_tl
937 }
938 % \end{macrocode}
939 % \end{macro}
940 %
941 %
942 % \begin{macro}{\g_para_indent_box}
943 % For managing the indentation we need to provide a public
944 % accessible box register
945 % \begin{macrocode}
946 \box_new:N \g_para_indent_box
947 % \end{macrocode}
948 % \end{macro}
949
950 % \begin{macro}{\@@_handle_indent:}
951 % Adding (typesetting) the indent box is straight forward.
952 % If it was emptied before it does nothing.
953 % \begin{macrocode}
954 \cs_new:Npn \@@_handle_indent: {
955 \box_use_drop:N \g_para_indent_box
956 }
957 % \end{macrocode}
958 % The declaration \cs{para_omit_indent:} (or
959 % \cs{OmitIndent}) changes that to do nothing.
960 % \begin{macrocode}
961 \cs_new:Npn \para_omit_indent: {
962 \box_gclear:N \g_para_indent_box
963 }
964 % \end{macrocode}
965 % \end{macro}
966 %
967 %
968 % \begin{macro}{\IndentBox,\OmitIndent}
969 % The \LaTeXe{} names for the indentation box and for suppressing it
970 % for use in the \hook{para/begin} hook.
971 % \begin{macrocode}
972 \cs_set_eq:NN \IndentBox \g_para_indent_box
973 \cs_set_eq:NN \OmitIndent \para_omit_indent:
974 % \end{macrocode}
975 % \end{macro}
976 %
977 %
978 %
979 %
980 %
981 %
982 % \begin{macro}{\para_end:}
983 % Adding hooks to the end of a paragraph is similar but here we
984 % need to alter the command that is used by \TeX{} to end horizontal
985 % mode and return to vertical mode, i.e., \cs{par}.
986 %
987 % This is a bit more complicated as this command can appear anywhere
988 % either explicitly or implicitly added by \TeX{} in certain
989 % situations:
990 % \begin{itemize}
991 % \item
992 % when using \cs{par} in the code or the document
993 % \item
994 % when using a blank line (which is converted to \cs{par})
995 % \item
996 % when \TeX{} finds any commands incompatible with horizontal
997 % mode it issues a \cs{par} and then rereads the command.
998 % \end{itemize}
999 %
1000 % Unfortunately, \TeX{} has some (these days) unnecessary
1001 % optimizations: if a \cs{vbox} ends and \TeX{} is still in
1002 % horizontal mode it simply exercises the paragraph builder instead
1003 % of issuing a \cs{par}. It is therefore necessary for \LaTeX{} to
1004 % ensure that this case doesn't happen and all boxes internally
1005 % have a \cs{par} command at their end.
1006 %
1007 % This \cs{par} may or may not run the ``par primitive'' (which is
1008 % always available as \cs{tex_par:D} in \pkg{expl3}); it is
1009 % permissible to have a changed meaning and it is in fact changed
1010 % by \LaTeX{} in various ways at various points inside
1011 % \texttt{latex.ltx}. For this \LaTeXe{} code has the following
1012 % conventions: \cs{@@@@par} and \cs{endgraf} both refer to the
1013 % default meaning (in the past this was the initex primitive) while
1014 % \cs{par} is the current meaning which maybe does something else.
1015 %
1016 %
1017 % We are now going to change this default meaning to instead run
1018 % \cs{para_end:}, which ultimately executes the initex
1019 % primitive but additionally adds our hooks when appropriate.
1020 % This way the change is again transparent to the legacy \LaTeXe{}
1021 % code.
1022 %
1023 % In most cases \cs{para_end:} should behave exactly like the
1024 % primitive and we achieve this by simply expanding it to the
1025 % primitive which is available to us as \cs{tex_par:D}. This way we
1026 % don't have to care about whether \TeX{} just does nothing (e.g.,
1027 % if in vertical mode already) or generates an error, etc.
1028 % \begin{macrocode}
1029 \cs_new_protected:Npn \para_end: {
1030 % \end{macrocode}
1031 %
1032 % CCC Maybe needs more explanation.
1033 % TEMP NOTE: What should happen if in outer hmode with an empty hlist?
1034 %
1035 % The only case we care about is when we are in horizontal mode
1036 % (i.e., doing typesetting) and not also in inner mode (i.e.,
1037 % making paragraphs and not building an \cs{hbox}.
1038 %\begin{verbatim}
1039 % \bool_lazy_and:nnT
1040 % { \mode_if_horizontal_p: }
1041 % { \bool_not_p:n { \mode_if_inner_p: } }
1042 % { ...
1043 %\end{verbatim}
1044 % Since this is executed for each and every paragraph in a document
1045 % we try to stay as fast as possible, so we do not use the
1046 % above construct but two conditionals instead. Using low-level
1047 % \cs{if_mode...} conditions would be even faster but has the
1048 % danger to conflict with conditionals in the user hooks.
1049 %
1050 % If \cs{para_end:} is executed while \TeX{} is currently doing a
1051 % low-level assignment the test for horizontal mode may get
1052 % executed as part of the assignment. That is normally not an issue
1053 % but we just found one case where it is:
1054 %\begin{verbatim}
1055 % \afterassignment\lst@vskip\@tempskipa \z@ \par
1056 %\end{verbatim}
1057 % If \TeX{} is in hmode while that assignment happens then the
1058 % \cs{par} is seen in hmode because in the above case the
1059 % assignment may not be finished (one should have used \cs{z@skip}) and
1060 % the \cs{lst@vskip} will get inserted into the middle of the
1061 % conditional. The \cs{lst@vskip} then changes to vmode and you get
1062 % a surprising error about the \texttt{para/end} hook having
1063 % changed modes even if you don't have any hook code(!): it is
1064 % the inserted \cs{lst@vskip} that is actually causing the change of
1065 % mode.This is what happened
1066 % when the output routines got started while a \texttt{lstlisting}
1067 % environment (that redefines \cs{vskip} in this way) was
1068 % active. This is really faulty coding, but we try to be proactive
1069 % and guard the conditional so that any scanning is first stopped, thus:
1070 % \begin{macrocode}
1071 \scan_stop:
1072 % \end{macrocode}
1073 %
1074 % \begin{macrocode}
1075 \mode_if_horizontal:TF {
1076 \mode_if_inner:F {
1077 % \end{macrocode}
1078 % In that case the action of the primitive would be to remove the
1079 % last glue (but no kerns) from the horizontal list (constructed to form
1080 % a paragraph) and then to append a penalty of 10000 and the
1081 % \cs{parfillskip}; it then passes the whole list to the
1082 % paragraph builder, which breaks it into lines and \TeX{} then
1083 % returns to vertical mode.
1084 %
1085 % What we want to do is to add this hook code at the end of
1086 % the horizontal list before any of the above happens.
1087 % If there was a glue item at the end of the list then
1088 % it should get removed before the hook code gets added so we have
1089 % to arrange for this removal.
1090 %
1091 % As in other similar cases, it may be best to add here
1092 % a \cs{nobreak} in case the hook itself adds glue and thus
1093 % creates a non-explicit and unwanted potential breakpoont.
1094 % On the other hand (as has been argued) the code in the hook
1095 % should perhaps have the responsibility for adding such a
1096 % guard penalty in this casse.
1097 % This needs further analysis and decisions (as in emails).
1098 %
1099 % In either case, good documentation of these hooks is essential,
1100 % covering what the hook may or should provide and all
1101 % such related considerations convernimg the content.
1102 %
1103 % There is not much point in checking if there was really a glue
1104 % item at the end of the horizontal list, instead we simply try to
1105 % remove one using \cs{tex_unskip:D}: if there wasn't one this will
1106 % do nothing.
1107 % \begin{macrocode}
1108 \tex_unskip:D
1109 % \end{macrocode}
1110 % We then execute the public hook (which may add some final typeset
1111 % material) followed by the kernel hook that we need for adding tagging
1112 % support. None of this is supposed to change the mode---at the
1113 % moment we make only a very simple test for this, more devious
1114 % changes go unnoticed, but too bad as they will then probably
1115 % backfire badly.
1116 % \begin{macrocode}
1117 \hook_use:n{para/end}
1118 \@kernel@after@para@end
1119 \mode_if_horizontal:TF {
1120 % \end{macrocode}
1121 % The final action (before getting to the point where
1122 % \cs{tex_par:D} is called) is to add an extra glue item so that the
1123 % primitive is prevented from removing intended glue
1124 % (if there was some). If we don't do this and the
1125 % horizontal list ends in several glue items we would end up removing
1126 % two glue items instead of just the last one, which would be wrong.
1127 % We use glue (rather than a kern) as that will be removed by the
1128 % primitive.
1129 %
1130 % There is however one other \TeX{} optimization that hurts: in a
1131 % sequence like this \verb=$$ ... $$ \par= (with \cs{par} being the primitive)
1132 % \TeX{} will be in
1133 % horizontal mode after the display, ready to receive further
1134 % paragraph text, but since the \cs{par} follows immediately there
1135 % is a ``null'' paragraph at the end and \TeX{} simply throws that
1136 % away.
1137 % The space between \verb=$$= and \cs{par} got already
1138 % dropped during the display processing so the \cs{par} is not
1139 % removing any space and appending \cs{parfillskip}, instead it
1140 % simply goes silently to vmode.
1141 %
1142 % Now if we would have added something (to
1143 % prevent glue removal) that would look to \TeX{} like material
1144 % after the display and so we would end up with an empty paragraph
1145 % just containing a penalty and \cs{parfillskip}.
1146 %
1147 % We therefore check if the current hlist does end in glue
1148 % (\cs{tex_lastnodetype:D} has the value \texttt{11}) and
1149 % if so we add a zero-length guard skip which will be removed by the
1150 % following \cs{tex_par:D}.
1151 % \changes{v1.0i}{2021/09/18}{Use skip rather than kern as guard.}
1152 % \begin{macrocode}
1153 \if_int_compare:w 11 = \tex_lastnodetype:D
1154 \tex_hskip:D \c_zero_dim
1155 \fi:
1156 % \end{macrocode}
1157 % To run the \hook{para/after} hook we first end the
1158 % paragraph. This means that the \cs{tex_par:D} at the very end is
1159 % unnecessary but executing it there unnecessarily is better than
1160 % having code that tests for all the different mode possibilities.
1161 % \begin{macrocode}
1162 \tex_par:D
1163 \hook_use:n{para/after}
1164 \@kernel@after@para@after
1165 }
1166 % \end{macrocode}
1167 % If we were not horizontal mode (the F case from above)
1168 % then the earlier hook
1169 % \hook{para/end} must have been at fault, so we report that.
1170 % \begin{macrocode}
1171 { \msg_error:nnnn { hooks }{ para-mode }{end}{horizontal} }
1172 % \end{macrocode}
1173 % Finally close out the nested conditionals.
1174 % \begin{macrocode}
1175 }
1176 }
1177 % \end{macrocode}
1178 % And then we can use the primitive to truly end the paragraph.
1179 % \begin{macrocode}
1180 \tex_par:D
1181 }
1182 % \end{macrocode}
1183 % \end{macro}
1184 %
1185 %
1186 %
1187 %
1188 % \begin{macro}{\para_raw_indent:,
1189 % \para_raw_noindent:,
1190 % \para_raw_end:}
1191 %
1192 % The commands \cs{para_raw_indent:} and \cs{para_raw_noindent:}
1193 % are like the primitives \cs{indent} and \cs{noindent} except that
1194 % they can only be used in vertical mode.
1195 %
1196 % To avoid issues a paragraph started by them should always be
1197 % ended by \cs{para_raw_end:} and not by \cs{para_end:} or
1198 % \cs{par} as the latter will execute hooks which then have no
1199 % counterpart at the beginning of the paragraph. It is the
1200 % responsibility of the programmer to make sure that they are
1201 % properly paired.
1202 % \begin{macrocode}
1203 \cs_new:Npn \para_raw_indent: {
1204 \mode_if_vertical:TF
1205 {
1206 \tex_everypar:D {
1207 \box_gset_to_last:N \g_para_indent_box
1208 \tex_everypar:D { \g_@@_standard_everypar_tl }
1209 \@@_handle_indent:
1210 \the\everypar }
1211 }
1212 { \msg_error:nn { latex2e }{ raw-para } }
1213 \tex_indent:D
1214 }
1215 % \end{macrocode}
1216 %
1217 % \begin{macrocode}
1218 \cs_new:Npn \para_raw_noindent: {
1219 \mode_if_vertical:TF
1220 {
1221 \tex_everypar:D {
1222 \tex_everypar:D { \g_@@_standard_everypar_tl }
1223 \the\everypar }
1224 }
1225 { \msg_error:nn { latex2e }{ raw-para } }
1226 \tex_noindent:D
1227 }
1228 % \end{macrocode}
1229 %
1230 % \begin{macrocode}
1231 \cs_new_eq:NN \para_raw_end: \tex_par:D
1232 % \end{macrocode}
1233 % \end{macro}
1234 %
1235 %
1236 % \begin{macro}{\RawIndent,\RawNoIndent,\RawParEnd}
1237 % The \LaTeXe{} names for starting and ending a paragraph without adding any hooks.
1238 % \begin{macrocode}
1239 \cs_set_eq:NN \RawIndent \para_raw_indent:
1240 \cs_set_eq:NN \RawNoindent \para_raw_noindent:
1241 \cs_set_eq:NN \RawParEnd \para_raw_end:
1242 % \end{macrocode}
1243 % \end{macro}
1244 %
1245 %
1246 % This ends the \texttt{para} module code.
1247 % \begin{macrocode}
1248 %<@@=>
1249 % \end{macrocode}
1250 %
1251 % \begin{macro}{\par,\endgraf}
1252 % \begin{macro}[int]{\@@par}
1253 %
1254 % Having the new default definition for \cs{par} we also have to
1255 % set it up so that it gets used. This involves three commands:
1256 % \cs{par}, \cs{@@par} (to which \LaTeX{} resets \cs{par}
1257 % occasionally) and \cs{endgraf}, which is another name for the
1258 % ``default'' action of \cs{par}.
1259 %
1260 % \begin{macrocode}
1261 \cs_set_eq:NN \par \para_end:
1262 \cs_set_eq:NN \@@par \para_end:
1263 \cs_set_eq:NN \endgraf \para_end:
1264 % \end{macrocode}
1265 % \end{macro}
1266 % \end{macro}
1267 %
1268 % While this is not integrated properly into the format we have to
1269 % redo the \cs{everypar} setting from the kernel, otherwise that
1270 % gets lost (as it happens before that file is loaded).
1271 % \begin{macrocode}
1272 \everypar{\@nodocument} %% To get an error if text appears before the \document
1273 % \end{macrocode}
1274 %
1275 %
1276 % \subsection{The error messages}
1277 %
1278 % This one is used when we detect that some hook code has changed
1279 % the mode where it shouldn't, e.g., by starting or ending a
1280 % paragraph.
1281 % The first argument is the hook name second the mode
1282 % it should have stayed in but didn't.
1283 %
1284 % \begin{macrocode}
1285 \msg_new:nnnn { hooks } { para-mode }
1286 {
1287 Illegal~mode~ change~ in~ hook~ 'para/#1'.\\
1288 Hook~ code~ did~ not~ remain~ in~ #2~ mode.
1289 }
1290 {
1291 Paragraph~ hooks~ cannot~ change~ the~ TeX~ mode~ without~ causing~
1292 endless~ recursion.~ The~ hook~ code~ in~ 'para/#1'~ needs~ to~ stay~
1293 in~ #2~ mode,~ but~ it~ didn't.~ Examine~ the~ hook~
1294 code~ with~ \iow_char:N \\ShowHook~ to~ find~ the~ issue.
1295 }
1296 % \end{macrocode}
1297 %
1298 % \changes{v1.0i}{2021/08/27}{Internal message name changes}
1299 % And here is one used in the \enquote{raw} commands when they are
1300 % used outside of vertical mode.
1301 % \begin{macrocode}
1302 \msg_new:nnnn { latex2e } { raw-para }
1303 {
1304 Not~ in~ vertical~ mode.
1305 }
1306 {
1307 Starting~ a~ paragraph~ with~ \iow_char:N \\RawIndent~ or~
1308 \iow_char:N \\RawNoindent \\
1309 (or~ \iow_char:N \\para_raw_indent:~ or~
1310 \iow_char:N \\para_raw_noindent:)~ is~ only~ allowed \\
1311 if~ LaTeX~ is~ in~ vertical~ mode.
1312 }
1313 % \end{macrocode}
1314
1315
1316 % \begin{macrocode}
1317 %
1318 %<latexrelease>\IncludeInRelease{0000/00/00}%
1319 %<latexrelease> {ltpara}{Undo~hooks~for~paragraphs}
1320 %<latexrelease>
1321 %<latexrelease>\let \OmitIndent \@undefined
1322 %<latexrelease>\let \IndentBox \@undefined
1323 %<latexrelease>\let \RawIndent \@undefined
1324 %<latexrelease>\let \RawNoindent \@undefined
1325 %<latexrelease>\let \RawParEnd \@undefined
1326 %<latexrelease>
1327 %<latexrelease>\cs_set_eq:NN \par \tex_par:D
1328 %<latexrelease>\cs_set_eq:NN \@@par \tex_par:D
1329 %<latexrelease>\cs_set_eq:NN \endgraf \tex_par:D
1330 %<latexrelease>
1331 % \end{macrocode}
1332 % We also need to clean up the primitive ``everypar'' as that
1333 % should no longer execute any code by default. And, of course,
1334 % make \cs{everypar} become the primitive again.
1335 % \changes{v1.0k}{2021/10/19}{Remove content from \cs{tex_everypar:D}
1336 % on rollback}
1337 % \begin{macrocode}
1338 %<latexrelease>\tex_everypar:D {}
1339 %<latexrelease>\cs_set_eq:NN \everypar \tex_everypar:D
1340 %<latexrelease>
1341 %<latexrelease>\EndModuleRelease
1342 \ExplSyntaxOff
1343 %</2ekernel|latexrelease>
1344 % \end{macrocode}
1345 %
1346 %
1347 %
1348 %
1349 %
1350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1351 \endinput
1352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1353 %