Debian Codegraph @

texmf-dist/source/latex/forest/forest-index.dtx

raw
1 % \CheckSum{755}
2 % \iffalse meta-comment
3 % forest-index.dtx
4 %% `forest-index' is an indexing system for the documentation of package
5 %% `forest', using the package itself. (The derived `forest-index.sty' is not
6 %% needed to use the package.)
7 %%
8 %% Copyright (c) 2012-2017 Saso Zivanovic
9 %% (Sa\v{s}o \v{Z}ivanovi\'{c})
10 %% [email protected]
11 %%
12 %% This work may be distributed and/or modified under the
13 %% conditions of the LaTeX Project Public License, either version 1.3
14 %% of this license or (at your option) any later version.
15 %% The latest version of this license is in
16 %%
17 %% http://www.latex-project.org/lppl.txt
18 %%
19 %% and version 1.3 or later is part of all distributions of LaTeX
20 %% version 2005/12/01 or later.
21 %%
22 %% This work has the LPPL maintenance status `author-maintained'.
23 %%
24 %% This file is a part of package `forest'. For the list of files
25 %% constituting the package see main source file of the package,
26 %% `forest.dtx', or the derived `forest.sty'.
27 %%
28 % \fi
29 %
30 % \newcommand\entry[1]{{``#1''}}
31 %
32 % \subsection{\protect{\texttt{forest-index}}}
33 % \label{sec:forest-index}
34 %
35 % The indexing system used to document the \foRest; package uses the
36 % package itself quite heavily. While this might be a bit surprising
37 % at first sight, as indexing draws no trees, the indexing package
38 % illustrates the usage of some of the more exotic features and
39 % usage-cases of the \foRest; package, which is why its source is
40 % included in this documentation.\footnote{Indexing with this package
41 % makes the compilation very slow, so I cannot whole-heartedly
42 % recommend it, but I still hope that it will make a useful example.}
43 %
44 % This package has three main functions:
45 % \begin{itemize}
46 % \item It is possible to index subentries using a \emph{short form}
47 % of their index key, i.e.\ without referring to their ancestor
48 % entries. For example, instead of writing |\index{option>content}|
49 % one can simply write |\index{content}|. (Obviously, the subentry
50 % must \entry{content} be defined as belonging to entry
51 % \entry{option} first. This is done using
52 % |\indexdef{option>content}|.) This works for all keys which are a
53 % subentry of a single entry.
54 % \item All subentries are automatically entered as main entries as
55 % well, with a qualificator of which entry they belong to. So,
56 % |\index{option>content}| produces two index entries: entry
57 % \entry{option} with subentry \entry{content} and entry
58 % \entry{content \scriptsize option}. This works for an arbitrary
59 % number of subentry levels.
60 % \item Entries can be given options that format the appearance of the
61 % entry and/or its descendants in both text and index. (Entries that
62 % format the appearance of their descendants are called categories
63 % below.)
64 % \item If |hyperref| package is loaded, the following hyperlinks are
65 % created besides the standard ones linking the page numbers in
66 % index to text:
67 % \begin{enumerate*}[(i)]
68 % \item entries in text link to the definition in text,
69 % \item definitions in the text link to the index entry,
70 % \item categories in index are cross-linked.
71 % \end{enumerate*}
72 %
73 % \end{itemize}
74 %
75 % The \foRest; package mainly enters the picture with respect to the
76 % entry formatting. A simple (narrow) tree is built containing an entry
77 % and all its ancestors. Formatting instructions are then processed
78 % using \foRest;'s option processing mechanisms.
79 %
80 % Finally, note that this package might change without retaining
81 % backwards compatibility, and that changes of this package will not
82 % be entered into the changelog.
83 %
84 % Identification.
85 % \begin{macrocode}
86 \ProvidesPackage{forest-index}
87 \RequirePackage{forest}
88 % \end{macrocode}
89 % Remember the original \LaTeX's |\index| command.
90 % \begin{macrocode}
91 \let\forestindex@LaTeX@index\index
92 % \end{macrocode}
93 %
94 % \subsubsection*{The user interface macros}
95 %
96 % \DescribeMacro{\index} is the general purpose macro.
97 % \DescribeMacro{\indexdef} and \DescribeMacro{\indexex} are
98 % shorthands for indexing definitions and
99 % examples. \DescribeMacro{\indexitem} is a combination of |\indexdef|
100 % and the |\item| of the |lstdoc| package. It automatically indexes
101 % the command being documented. \DescribeMacro{\indexset} neither
102 % typesets or indexes the entry, but only configures it; it is usually
103 % used to configure categories. All these macros parse their
104 % arguments using |xparse|. The arguments, listed in the reverse
105 % order:
106 % \begin{itemize}
107 % \item The final argument, which is the only mandatory argument, is a
108 % comma-separated list of index keys.
109 % \item The boolean switch |>| just before the mandatory argument
110 % signals that the keys are given in the full form. Otherwise, keys
111 % without a level separator are considered short.
112 % \item Indexing options are given by the |[|optional |]| argument.
113 % \item The first |(|optional|)| argument of:
114 % \begin{compactitem}
115 % \item |\indexitem|: specifies the default value of the command.
116 % \item |\index|: is used to
117 % provide ``early'' options.
118 % \end{compactitem}
119 % \end{itemize}
120 %
121 % Among the options of these commands, three keylists are of special
122 % importance: |index_key_format|, |index_form_format| and
123 % |print_format|. These hold instructions on how to format the index
124 % key, the form of the entry in the index and the form of the entry in
125 % the main text. They work by modifying the contents of an
126 % \indexex{autowrapped toks} register |result|.
127 %
128 % An example: how macros are indexed in this documentation. Style
129 % |macro| defined below does everything needed to format a macro name:
130 % it detokenizes the given name (in case the name contains some funny
131 % characters), prefixes the backslash, wraps in in the typewriter
132 % font, adds color and hyperlink (the final two styles are defined in
133 % below this package). Note the usage of |\protect|: it is needed
134 % because we want to use these styles to format entries not just in
135 % the main next, but also in the index.
136 % \lstinputregion{forest-doc.tex}{index_macro_style} Then, we
137 % configure the main level entry \entry{macro}: the child of this
138 % entry will be formatted (both in index and in the main text) using
139 % the previously defined style.
140 % \lstinputregion{forest-doc.tex}{index_macro_category} Usage is then
141 % simple: we write |\indexex{macro>forestoption}| (or simply
142 % |\indexex{forestoption}| to get \indexex{forestoption}.
143 %
144 % \begin{macrocode}
145 \DeclareDocumentCommand\indexdef{O{} t> m}{%
146 \IfBooleanTF{#2}
147 {\let\forestindex@resolvekey\forestindex@resolvekey@long}
148 {\let\forestindex@resolvekey\forestindex@resolvekey@shortorlong}%
149 \forestindex@index{definition}{#1}{#3}}
150 \DeclareDocumentCommand\indexex{O{} t> m}{%
151 \IfBooleanTF{#2}
152 {\let\forestindex@resolvekey\forestindex@resolvekey@long}
153 {\let\forestindex@resolvekey\forestindex@resolvekey@shortorlong}%
154 \forestindex@index{example}{#1}{#3}}
155 % \DeclareDocumentCommand\indexitem{D(){} O{} t> m}{%
156 % \IfBooleanTF{#3}
157 % {\let\forestindex@resolvekey\forestindex@resolvekey@long}
158 % {\let\forestindex@resolvekey\forestindex@resolvekey@shortorlong}%
159 % \forestindex@index{definition}{default={#1},print format=item,#2}{#4}}
160 \DeclareDocumentCommand\indexitem{D(){} O{} t> m}{%
161 \let\forestindex@resolvekey\forestindex@resolvekey@long
162 \forestindex@index{definition}{default={#1},#2,print format+=item}{#4}}
163 \DeclareDocumentCommand\indexset{O{} t> m}{%
164 \IfBooleanTF{#2}
165 {\let\forestindex@resolvekey\forestindex@resolvekey@long}
166 {\let\forestindex@resolvekey\forestindex@resolvekey@shortorlong}%
167 \forestindex@index{not print,not index,definition}{set={#1}}{#3}}
168 \DeclareDocumentCommand\index{D(){} O{} t> m}{%
169 \IfBooleanTF{#3}
170 {\let\forestindex@resolvekey\forestindex@resolvekey@long}
171 {\let\forestindex@resolvekey\forestindex@resolvekey@shortorlong}%
172 \forestindex@index{#1}{#2}{#4}%
173 }
174 % \end{macrocode}
175 % All UI macros call this macro.
176 % \begin{arguments}
177 % \item early option keylist
178 % \item late option keylist
179 % \item a comma-sep list of forest index key (full or short form). A
180 % key can be given an argument using |key=argument| syntax. How the
181 % argument is used is up to the user. For example, the
182 % \entry{environment} entry of the \foRest; documentation uses it to
183 % typeset the contents of the environment:
184 % \lstinputregion{forest-doc.tex}{forest_environment_doc}
185 % \end{arguments}
186 % \begin{macrocode}
187 \def\forestindex@index#1#2#3{%
188 % \end{macrocode}
189 % Partition the index keylist into single keys. And put it all in a group: the
190 % persistent stuff is saved globally.
191 % \begin{macrocode}
192 {\forcsvlist{\forestindex@forkey{#1}{#2}}{#3}}%
193 }
194 \def\forestindex@forkey#1#2#3{%
195 % \end{macrocode}
196 % Short-key resolution. The result is stored into |\forestindex@fullkey|.
197 % \begin{macrocode}
198 \forestindex@resolvekey{#3}%
199 % \end{macrocode}
200 % Manipulate arguments a bit, so that we can use our quick-and-dirty one-arg
201 % memoization.
202 % \begin{macrocode}
203 %\forestset{@index/.process={__o}{#1}{#2}{\forestindex@fullkey}}
204 \edef\forest@marshal{%
205 \noexpand\forestindex@index@{%
206 {\unexpanded{#1}}%
207 {\unexpanded{#2}}%
208 {\expandonce{\forestindex@fullkey}}%
209 }%
210 }\forest@marshal
211 }
212 % \end{macrocode}
213 % Call the central processing command, style |@index|. See how
214 % \indexex{handler>process} is used to expand (once) the last argument.
215 % \begin{macrocode}
216 \def\forestindex@index@#1{\forestset{@index/.process={__o}#1}}
217 \forestset{
218 % \end{macrocode}
219 % \subsubsection*{Declarations}
220 %
221 % Should we print and/or index the entry? For example,
222 % |\index[not_print]{...}| will index silently (as \LaTeX's |\index|
223 % command does).
224 % \begin{macrocode}
225 declare boolean register=print,
226 declare boolean register=index,
227 declare boolean register=short,
228 % \end{macrocode}
229 % Options |name|, |content|, |key| and |argument| hold info about the
230 % current entry. We need to declare only the latter two, the former
231 % two we steal from \foRest;.
232 % \begin{macrocode}
233 declare toks={key}{},
234 declare toks={argument}{},
235 % \end{macrocode}
236 % These options will hold first the initial, and then the calculated
237 % values of the index key, index form and the form in text. When
238 % (late) options are executed, these options are initialized to the
239 % value of option |key|; it is safe to modify them at this point.
240 % Afterwards, they will be further processed by keylists
241 % |index_key_format|, |index_form_format| and |print_format|,
242 % respectively.
243 % \begin{macrocode}
244 declare toks={index key}{},
245 declare toks={index form}{},
246 declare toks={print form}{},
247 % \end{macrocode}
248 % The customization of entries' appearance is done by specifying the
249 % following three keylists. The keylists work by modifying register
250 % |result|.
251 % \begin{macrocode}
252 declare keylist={index key format}{},
253 declare keylist={index format}{},
254 declare keylist={print format}{},
255 declare autowrapped toks register=result,
256 % \end{macrocode}
257 % Some shorthands.
258 % \begin{macrocode}
259 format'/.style={print format'={#1}, index format'={#1}},
260 format/.style={print format={#1}, index format={#1}},
261 format+/.style={print format+={#1}, index format+={#1}},
262 +format/.style={+print format={#1}, +index format={#1}},
263 form/.style={print form={#1},index form={#1}},
264 form+/.style={print form+={#1},index form+={#1}},
265 +form/.style={+print form={#1},+index form={#1}},
266 % \end{macrocode}
267 % Entry types are normal (default), definition, example. Only
268 % definitions are special, as their options are automatically saved.
269 % \begin{macrocode}
270 declare toks register=index entry type,
271 definition/.style={index entry type=definition},
272 normal/.style={index entry type=normal},
273 example/.style={index entry type=example},
274 normal,
275 % \end{macrocode}
276 % This option is used internally to store the hyper ids.
277 % \begin{macrocode}
278 declare toks={index@hypertarget}{},
279 every index begin/.style={},
280 every index end/.style={},
281 % \end{macrocode}
282 % Some formatting tools need to know whether we're typesetting text or
283 % index: this info is stored in the |stage| register.\indexex[margin]{declare toks register}
284 % \begin{macrocode}
285 declare toks register=stage,
286 % \end{macrocode}
287 %
288 % \subsubsection*{The central processing command}
289 % \begin{arguments}
290 % \item early option keylist (these are only used to define category
291 % ``@unknown'' at the end of this package)
292 % \item late option keylist
293 % \item index key (full form)
294 % \end{arguments}
295 % \begin{macrocode}
296 @index/.style n args={3}{
297 % \end{macrocode}
298 % Set the defaults.
299 % \begin{macrocode}
300 print, index, index entry type=normal, set'={}, short,
301 % \end{macrocode}
302 % Create the tree structure:
303 % |[entry[subentry[subsubentry...]]]|. Three options of every node
304 % created:
305 % \begin{compactitem}
306 % \item |key| contains the key of the (sub)entry
307 % \item |name| contains the full path to the (sub)entry
308 % \item |arguments| contains the arguments given to the (sub)entry's key
309 % \item |content| contains the full key, with arguments for all but
310 % the most deeply embedded subentry
311 % \end{compactitem}
312 % \indexex{for step=\indexex{nodewalk}} is used because
313 % |create@subentry@node| walks down the created tree. At |if n=0|
314 % below, we're thus positioned at the lowest node.\indexex[margin]{step>nodewalk}
315 % \begin{macrocode}
316 for nodewalk={
317 % \end{macrocode}
318 % The components of the full key are separated using \indexex{split},
319 % with different keys being executed for the first component and the
320 % rest.
321 % \begin{macrocode}
322 split={#3}{>}{create@main@entry@node, create@subentry@node},
323 % \end{macrocode}
324 % Remove the argument from the most deeply embedded subentry.
325 % \begin{macrocode}
326 if n=0{
327 content/.option=key,
328 }{
329 content/.process={OOw2} {!parent.content} {key} {##1>##2},
330 }
331 }{},
332 for root'={
333 % \end{macrocode}
334 % Don't memoize if the key is of an unknown category.
335 % \begin{macrocode}
336 if strequal/.process={O}{!root.name}{@unknown}{TeX=\global\forest@memoizing@ok@false}{},
337 % \end{macrocode}
338 % Option |print_form| is what will be typeset in the text. Option
339 % |index_key| is the key that will be used for sorting the
340 % index. Option |index_form| is what will be typeset in the index. All
341 % these are initialized to the |key|. See how
342 % \indexex{handler>option} is used to assign an option value to
343 % another option.
344 % \begin{macrocode}
345 for tree={
346 print form/.option=key,
347 index key/.option=key,
348 index form/.option=key,
349 },
350 % \end{macrocode}
351 % Below, \indexex{node key>on invalid} is set to \indexex{value of=on invalid>fake} at four points.
352 % This is so we won't get in trouble when |\indexset|ting the
353 % categories: when the category formatting code will try to step into
354 % the child, it will fail as the child does not exist when |\indexset|
355 % is called for the category; but we ignore the failure.
356 %
357 % Go to the the most deeply embedded subentry.\indexex[margin]{first leaf'}
358 % \begin{macrocode}
359 for first leaf'={
360 % \end{macrocode}
361 % Execute every index options and the given early options.
362 % \begin{macrocode}
363 on invalid={fake}{
364 every index begin,
365 #1,
366 },
367 % \end{macrocode}
368 % Ancestors are walked in the \indexex{reverse} order (top down). At
369 % every node, the saved configuration is executed (if it exists).
370 % \begin{macrocode}
371 for reverse={current and ancestors}{on invalid={fake}{@@index/\forestoption{name}/.try}},
372 % \end{macrocode}
373 % We don't execute the saved configuration for definitions, as
374 % definitions are where the configuration is set.
375 % \begin{macrocode}
376 if index entry type={definition}{}{%
377 on invalid={fake}{@@index/\forestoption{name}/.try},
378 },
379 % \end{macrocode}
380 % Execute late (well, normal) options. See the discussion about early
381 % options above.
382 % \begin{macrocode}
383 on invalid={fake}{
384 #2,
385 every index end
386 },
387 % \end{macrocode}
388 % Remember the given config for the rest of the document.
389 % \begin{macrocode}
390 if set={}{}{save@session},
391 % \end{macrocode}
392 % If we're at a definition, save the config into the auxiliary file.
393 % \begin{macrocode}
394 if index entry type={definition}{save@foridx}{},
395 },
396 stage={},
397 % \end{macrocode}
398 % Create hyperlink targets of the form |.entry.subentry.subsubentry...|.
399 %
400 % \FoRest; points:
401 % \begin{enumerate*}[(i)]
402 % \item the generic conditional \indexex{if},
403 % \item handler \index{handler>process},
404 % \end{enumerate*}
405 % \begin{macrocode}
406 if index={
407 index@hypertarget/.process={OS_= ? l_ w2}
408 {index key}
409 {}
410 {}{.}
411 {##2##1},
412 for descendants={
413 index@hypertarget/.process={OO S _l1= ? w2 }
414 {!parent.index@hypertarget}{index key}
415 {}
416 {##1} % empty index key
417 {##1.##2} % otherwise
418 },
419 }{},
420 % \end{macrocode}
421 % Index.
422 % \begin{macrocode}
423 if index={
424 begingroup,
425 stage=index,
426 % \end{macrocode}
427 % For each (sub)entry, format the |index_key| using the instructions
428 % in |index_key_format|.
429 % \begin{macrocode}
430 for tree={
431 result/.option=index key,
432 process keylist'={index key format}{current},
433 index key/.register=result,
434 },
435 % \end{macrocode}
436 % For each (sub)entry, format the |index_form| using the instructions
437 % in |index_form_format|.
438 % \begin{macrocode}
439 for tree={
440 result/.option=index form,
441 process keylist'={index format}{current},
442 index form/.register=result,
443 },
444 % \end{macrocode}
445 % Create an index entry for all nodes where |index_form| is non-empty.
446 % \begin{macrocode}
447 where index form={}{}{
448 % \end{macrocode}
449 % All the ancestor nodes with an non-empty |index_form| will be
450 % appended (in script size, as a hyperlink) to the |index_form| of the
451 % current node.
452 % \begin{macrocode}
453 if n=0{
454 temptoksb={},
455 }{
456 temptoksc={},
457 for ancestors={
458 if index form={}{}{
459 temptoksb+/.expanded={\forestregister{temptoksc}%
460 \noexpand\protect\noexpand\hyperlinknocolor{%
461 \forestoption{index@hypertarget}}{\forestoption{index form}}},
462 temptoksc={,\space},
463 },
464 },
465 if temptoksb={}{}{
466 +temptoksb={\protect\space\begingroup\protect\scriptsize},
467 temptoksb+={\endgroup},
468 },
469 },
470 temptoksa={},
471 result'={},
472 if n children=0{tempboola}{not tempboola},
473 where index form={}{}{
474 % \end{macrocode}
475 % Create the hypertarget that the definitions in text and other index entries
476 % will point to.
477 % \begin{macrocode}
478 temptoksd/.expanded={\noexpand\protect\noexpand\hypertarget{%
479 \forestoption{index@hypertarget}}{}},
480 % \end{macrocode}
481 % Add the (inner) current node to the index entry of the (outer) current node.
482 % \begin{macrocode}
483 result+/.expanded={%
484 \forestregister{temptoksa}%
485 \forestoption{index key}%
486 =\forestoption{index form}%
487 \forestregister{temptoksd}%
488 \forestregister{temptoksb}%
489 },
490 temptoksa={>},
491 temptoksb={},
492 },
493 % \end{macrocode}
494 % Do the actual indexing.
495 % \begin{macrocode}
496 result+/.expanded={|indexpagenumber\forestregister{index entry type}},
497 TeX and memoize/.expanded={\noexpand\forestindex@LaTeX@index{\forestregister{result}}},
498 },
499 endgroup
500 }{},
501 if print={
502 begingroup,
503 stage=print,
504 % \end{macrocode}
505 % For each (sub)entry, format the |print_form| using the instructions
506 % in |print_form_format|.
507 % \begin{macrocode}
508 for tree={
509 result/.option=print form,
510 process keylist'={print format}{current},
511 print form/.register=result,
512 },
513 % \end{macrocode}
514 % Typeset the entry in the text.
515 % \begin{macrocode}
516 for first leaf'={TeX and memoize/.expanded={\forestoption{print form}}},
517 endgroup,
518 }{},
519 }
520 },
521 % \end{macrocode}
522 % Create the main entry node and set to be the root.
523 % \begin{macrocode}
524 create@main@entry@node/.style={% #1 = subentry
525 set root={[]},
526 do dynamics, for root'={process delayed=tree},
527 root',
528 setup@entry@node={#1}
529 },
530 % \end{macrocode}
531 % Create a subentry node and move into it.
532 % \begin{macrocode}
533 create@subentry@node/.style={
534 append={[]},
535 do dynamics, for root'={process delayed=tree},
536 n=1,
537 setup@entry@node={#1}
538 },
539 % \end{macrocode}
540 % Parse \#1 into |key| and |argument|, and assign |name| and |content|.
541 % \begin{macrocode}
542 setup@entry@node/.style={
543 options={
544 split={#1}{=}{key,argument},
545 if n=0{
546 name'/.option=key,
547 content={#1},
548 }{
549 name'/.process={OOw2} {!parent.name} {key} {##1>##2},
550 content/.process={Ow1} {!parent.content} {##1>#1},
551 },
552 }
553 },
554 }
555 % \end{macrocode}
556 % \subsubsection*{Saving and loading the options}
557 % \begin{macrocode}
558 \forestset{
559 % \end{macrocode}
560 % This register holds whatever we need to remember.
561 % \begin{macrocode}
562 declare keylist register=set,
563 % \end{macrocode}
564 % Besides storing the keylist in the register, also immediately
565 % execute it.\indexex[margin]{Autoforward register}.
566 % \begin{macrocode}
567 Autoforward register={set}{##1},
568 % \end{macrocode}
569 % Remember things by saving them in a global style.
570 % \begin{macrocode}
571 save@session/.style={@@index/\forestoption{name}/.global style/.register=set},
572 % \end{macrocode}
573 % Save thinks to the auxiliary file.
574 % \begin{macrocode}
575 save@foridx/.style={
576 % \end{macrocode}
577 % Don't save entries of unknown category.
578 % \begin{macrocode}
579 if strequal/.process={O}{!root.name}{@unknown}{}{
580 % \end{macrocode}
581 % Don't save if nothing is set.
582 % \begin{macrocode}
583 if set={}{}{
584 TeX and memoize/.expanded={%
585 \noexpand\immediate\noexpand\write\noexpand\forestindex@out{%
586 \noexpand\string\noexpand\indexloadsettings\noexpand\unexpanded{{\forestoption{name}}{\forestregister{set}}}%
587 }%
588 },
589 },
590 },
591 % \end{macrocode}
592 % Save the full form of the key in the auxiliary file. Obviously, do
593 % it only for subentries. The full form contains whatever arguments
594 % were given to the non-last component.
595 % \begin{macrocode}
596 if key/.process={O}{content} {} {%
597 if short={
598 TeX and memoize/.expanded={%
599 \noexpand\immediate\noexpand\write\noexpand\forestindex@out{%
600 \noexpand\string\noexpand\indexdefineshortkey\noexpand\unexpanded{{\forestoption{key}}{\forestoption{content}}}%
601 }%
602 }%
603 }{}
604 }
605 }
606 }
607 % \end{macrocode}
608 % Load settings from the auxiliary file into the global style. Warn
609 % if anything was configured more than once (e.g.\ by |\indexdef|ing
610 % the same key twice).
611 % \begin{macrocode}
612 \def\indexloadsettings#1#2{%
613 \pgfkeysifdefined{/forest/@@index/#1/.@cmd}{%
614 \forestindex@loadsettings@warning{#1}%
615 }{}%
616 % #s in #2 are doubled; the following \def removes one layer of doubling
617 \def\forest@temp{#2}%
618 \forestset{@@index/#1/.global style/.expand once=\forest@temp}%
619 }
620 \def\forestindex@loadsettings@warning#1{%
621 \PackageWarning{forest-index}{Forest index key "#1" was configured more than once!
622 I'm using the last configuration.}%
623 }
624 % \end{macrocode}
625 % Load the full form of a short key from the auxiliary file. Out of
626 % kindness for the end user, remember all the full keys corresponding
627 % to a short key: this will make a more informative warning below.
628 % \begin{macrocode}
629 \def\indexdefineshortkey#1#2{%
630 \def\forestindex@temp@short{#1}%
631 \def\forestindex@temp@long{#2}%
632 \ifx\forestindex@temp@short\forestindex@temp@long
633 \else
634 \ifcsdef{index@long@#1}{%
635 \global\cslet{index@long@#1}\relax
636 \csgappto{index@alllong@#1}{,#2}%
637 }{%
638 \global\csgdef{index@long@#1}{#2}%
639 \global\csgdef{index@alllong@#1}{#2}%
640 }%
641 \fi
642 }
643 % \end{macrocode}
644 % \subsubsection*{Short key resolution}
645 %
646 % Nothing to do for a long key.
647 % \begin{macrocode}
648 \def\forestindex@resolvekey@long#1{\def\forestindex@fullkey{#1}}
649 % \end{macrocode}
650 % Decide whether a key is short or long based on the absence or
651 % presence of the level separator |>|.
652 % \begin{macrocode}
653 \def\forestindex@resolvekey@shortorlong#1{%
654 \pgfutil@in@>{#1}%
655 \ifpgfutil@in@
656 \expandafter\def\expandafter\forestindex@fullkey
657 \else
658 \expandafter\forestindex@resolvekey@short
659 \fi
660 {#1}%
661 }
662 % \end{macrocode}
663 % Before resolving the short key, we need to split the user input into
664 % the key and the argument. The latter is then appended to the full
665 % key (which can, in principle, contain arguments for other components
666 % as well).
667 % \begin{macrocode}
668 \def\forestindex@resolvekey@short#1{%
669 \forestset{split={#1}{=}{index@resolveshortkey@key,index@resolveshortkey@arg}}%
670 }
671 \forestset{
672 index@resolveshortkey@key/.code={%
673 \ifcsvoid{index@long@#1}{%
674 \forestindex@resolveshortkey@warning{#1}%
675 \def\forestindex@fullkey{@unknown>#1}%
676 }{%
677 \letcs\forestindex@fullkey{index@long@#1}%
678 }%
679 },
680 index@resolveshortkey@arg/.code={%
681 \appto\forestindex@fullkey{=#1}%
682 },
683 }
684 \def\forestindex@resolveshortkey@warning#1{%
685 \PackageWarning{forest-index}{Cannot resolve short forest index key "#1".
686 These are the definitions I found (from the previous run):
687 "\csuse{index@alllong@#1}"}%
688 }
689 % \end{macrocode}
690 % \subsubsection*{Formatting styles}
691 %
692 % Define default colors for index entry types and provide a style that
693 % typesets the entry in text (but not index) in the desired color.
694 % \begin{macrocode}
695 \forestset{
696 normal color/.initial=blue,
697 definition color/.initial=red,
698 example color/.initial=darkgreen,
699 print in color/.style={if stage={print}{result/.expanded=\noexpand\protect\noexpand\textcolor{%
700 \pgfkeysvalueof{/forest/#1 color}}{\unexpanded{##1}}}{}},
701 print in color/.default=\forestregister{index entry type},
702 % \end{macrocode}
703 % Use this style in |..._format| keylists if you want the index
704 % entries to be hyperlinks to the definition, and the definition to be
705 % a hyperlink to the index.
706 % \begin{macrocode}
707 hyper/.style={
708 if stage={index}{}{
709 if index entry type={definition}{
710 result/.expanded={\noexpand\hypertarget{\forestoption{name}}%
711 {\noexpand\hyperlink{\forestoption{index@hypertarget}}{\forestregister{result}}}}
712 }{
713 result/.expanded=\noexpand\hyperlink{\forestoption{name}}{\forestregister{result}}
714 }
715 }
716 },
717 }
718 % \end{macrocode}
719 % Color page numbers in the index, with or without |hyperref| package.
720 % \begin{macrocode}
721 \ifdef\hyperpage{%
722 \newcommand\indexpagenumbernormal[1]{{%
723 \hypersetup{linkcolor=\pgfkeysvalueof{/forest/normal color}}\hyperpage{#1}}}
724 \newcommand\indexpagenumberdefinition[1]{{%
725 \hypersetup{linkcolor=\pgfkeysvalueof{/forest/definition color}}\hyperpage{#1}}}
726 \newcommand\indexpagenumberexample[1]{{%
727 \hypersetup{linkcolor=\pgfkeysvalueof{/forest/example color}}\hyperpage{#1}}}
728 }{
729 \newcommand\indexpagenumbernormal[1]{%
730 \textcolor{\pgfkeysvalueof{/forest/normal color}}{#1}}
731 \newcommand\indexpagenumberdefinition[1]{%
732 \textcolor{\pgfkeysvalueof{/forest/definition color}}{#1}}
733 \newcommand\indexpagenumberexample[1]{%
734 \textcolor{\pgfkeysvalueof{/forest/example color}}{#1}}
735 }
736 % \end{macrocode}
737 % Provide dummy |\hyper...| commands if |hyperref| is not loaded.
738 % \begin{macrocode}
739 \providecommand\hyperlink[2]{#2}
740 \providecommand\hypertarget[2]{#2}
741 \providecommand\hypersetup[1]{}
742 % \end{macrocode}
743 % This is used by entry qualifiers: we want them to be hyperlinks, but black.
744 % \begin{macrocode}
745 \newcommand\hyperlinknocolor[2]{{\hypersetup{linkcolor=black}\hyperlink{#1}{#2}}}
746 % \end{macrocode}
747 %
748 % Use style |item| to have the index entry (in text) function as the
749 % |\item| of a |lstdoc|'s |syntax| environment.
750 % \begin{macrocode}
751 \forestset{
752 declare toks register=default,
753 default={},
754 item/.style={
755 result/.process= {_RORw4}
756 {} {default} {!parent.print form} {result}
757 {\item[,##2,##3]{##4}},
758 },
759 }
760 % \end{macrocode}
761 %
762 % \subsubsection*{Utilities}
763 %
764 % We will need a global version of several |pgfkeys| commands.
765 % \begin{macrocode}
766 \pgfkeys{/handlers/.global style/.code=\pgfkeys{\pgfkeyscurrentpath/.global code=\pgfkeysalso{#1}}}
767 \pgfkeysdef{/handlers/.global code}{\pgfkeysglobaldef{\pgfkeyscurrentpath}{#1}}
768 \long\def\pgfkeysglobaldef#1#2{%
769 \long\def\pgfkeys@temp##1\pgfeov{#2}%
770 \pgfkeysgloballet{#1/.@cmd}{\pgfkeys@temp}%
771 \pgfkeysglobalsetvalue{#1/.@body}{#2}%
772 }
773 \def\pgfkeysgloballet#1#2{%
774 \expandafter\global\expandafter\let\csname pgfk@#1\endcsname#2%
775 }
776 \long\def\pgfkeysglobalsetvalue#1#2{%
777 \pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}%
778 }
779 \forestset{
780 % unlike pgfmath function strequal, |if strequal| does not expand the compared args!
781 if strequal/.code n args={4}{\ifstrequal{#1}{#2}{\pgfkeysalso{#3}}{\pgfkeysalso{#4}}},
782 }
783 % \end{macrocode}
784 % Begin and end group, \foRest;-style:
785 % \begin{macrocode}
786 \forestset{
787 begingroup/.code={\begingroup},
788 endgroup/.code={\endgroup},
789 }
790 % \end{macrocode}
791 %
792 % \subsubsection{Memoize}
793 %
794 % Quick and dirty memoization. Single argument macros only. Does not support nesting.
795 %
796 % \begin{macrocode}
797 \newtoks\forest@memo@key
798 \newtoks\forest@memo
799 \newif\ifforest@memoizing@now@
800 \newif\ifforest@memoizing@ok@
801 \newif\ifforest@execandmemoize@
802 \def\forest@memoize#1{% #1 = \cs
803 \cslet{forest@memo@orig@\string#1}#1%
804 \def#1##1{%
805 \ifforest@memoizing@now@
806 \forest@globalsaveandrestoreifcs{forest@execandmemoize@}{%
807 \global\forest@execandmemoize@false
808 \csname forest@memo@orig@\string#1\endcsname{##1}%
809 }%
810 \else
811 \expandafter\global\expandafter\forest@memo@key\expandafter{\detokenize{forest@memo@#1{##1}}}%
812 \ifcsname\the\forest@memo@key\endcsname
813 \@escapeifif{\csname\the\forest@memo@key\endcsname}%
814 \else
815 \@escapeifif{%
816 \global\forest@memo{}%
817 \global\forest@memoizing@ok@true
818 \global\forest@memoizing@now@true
819 \global\forest@execandmemoize@true
820 \csname forest@memo@orig@\string#1\endcsname{##1}%
821 \global\forest@execandmemoize@false
822 \global\forest@memoizing@now@false
823 \ifforest@memoizing@ok@
824 \csxdef{\the\forest@memo@key}{\the\forest@memo}%
825 \immediate\write\forest@memo@out{%
826 \noexpand\forest@memo@load{\the\forest@memo@key}{\the\forest@memo}%
827 }%
828 \fi
829 }%
830 \fi
831 \fi
832 }%
833 }
834 \def\forest@memo@load#1#2{%
835 % \end{macrocode}
836 % The following two |\def|s remove one level of hash-doubling from the
837 % arguments, introduced by |\write|.
838 % \begin{macrocode}
839 \def\forest@temp@key{#1}%
840 \def\forest@temp@value{#2}%
841 \csxdef{\detokenize\expandafter{\forest@temp@key}}{\expandonce\forest@temp@value}%
842 \immediate\write\forest@memo@out{%
843 \noexpand\forest@memo@load{\detokenize\expandafter{\forest@temp@key}}{\detokenize\expandafter{\forest@temp@value}}%
844 }%
845 }
846 \forestset{
847 TeX and memoize/.code={\forest@execandmemoize{#1}},
848 }
849 \def\forest@execandmemoize#1{%
850 \ifforest@execandmemoize@
851 \let\forest@memo@next\forest@execandmemoize@
852 \else
853 \let\forest@memo@next\@gobble
854 \fi
855 \forest@memo@next{#1}%
856 #1%
857 }
858 \def\forest@execandmemoize@#1{%
859 \gapptotoks\forest@memo{#1}%
860 }
861 \def\forest@memo@filename{\jobname.memo}
862 \newwrite\forest@memo@out
863 \immediate\openout\forest@memo@out=\forest@[email protected]
864 \IfFileExists{\forest@memo@filename}{%
865 \input\forest@memo@filename\relax
866 }{}%
867 \AtEndDocument{%
868 \immediate\closeout\forest@memo@out
869 \forest@file@copy{\forest@[email protected]}{\forest@memo@filename}%
870 }
871 % \end{macrocode}
872 % Commenting the following line turns off memoization.
873 % \begin{macrocode}
874 \forest@memoize\forestindex@index@
875 % \end{macrocode}
876 %
877 % \subsubsection*{Initialize}
878 %
879 % Declare category ``@unknown''.
880 % \begin{macrocode}
881 \index(not print,not index)[%
882 set={
883 index key=unknown,
884 form={\textbf{unknown!!}},
885 for first={format={result/.expanded=\noexpand\textbf{\forestregister{result}??}}}
886 },
887 ]>{@unknown}
888 % \end{macrocode}
889 % Load the auxiliary file made in the previous compilation, and open
890 % it for writing to save data from this compilation.
891 % \begin{macrocode}
892 \def\forestindex@filename{\jobname.foridx}
893 \IfFileExists{\forestindex@filename}{%
894 \input\forestindex@filename\relax
895 }{}%
896 \newwrite\forestindex@out
897 \immediate\openout\forestindex@out=\[email protected]
898 \AtEndDocument{%
899 \immediate\closeout\forestindex@out
900 \forest@file@copy{\[email protected]}{\forestindex@filename}%
901 }
902 \endinput
903 % \end{macrocode}
904 %
905 %%% \iffalse
906 %%% Local Variables:
907 %%% mode: doctex
908 %%% TeX-master: "forest-doc"
909 %%% TeX-command-default: "sty"
910 %%% End:
911 %%% \fi