| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
% |
| 3 |
% Module: ZzTeX Indexing Facilities, version 2 |
| 4 |
% |
| 5 |
% Synopsis: This file contains the facilities for generating index |
| 6 |
% parameter files, raw entry files, and for setting the indexes. |
| 7 |
% |
| 8 |
% Author: Paul C. Anagnostopoulos |
| 9 |
% Created: 13 June 2013 |
| 10 |
% |
| 11 |
% Copyright 1989--2020 by Paul C. Anagnostopoulos |
| 12 |
% under The MIT License (opensource.org/licenses/MIT) |
| 13 |
% |
| 14 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 15 |
|
| 16 |
% Define Index Locators |
| 17 |
% ------ ----- -------- |
| 18 |
|
| 19 |
|
| 20 |
% This list contains the definition of each locator. New ones must be |
| 21 |
% added to the end, and any program looking at the definitions must assume |
| 22 |
% that duplicates may exist and that the last one prevails. |
| 23 |
|
| 24 |
\def \zixloclist {} |
| 25 |
|
| 26 |
|
| 27 |
\def \definelocator #1#2#3{% {type}{attributes}{[format-args]} |
| 28 |
{\setflag \pg = \false |
| 29 |
\setflag \rng = \false |
| 30 |
\setflag \txt = \false |
| 31 |
\def \page {\setflag \pg = \true}% |
| 32 |
\def \nopage {\setflag \pg = \false}% |
| 33 |
\def \range {\setflag \pg = \true \setflag \rng = \true}% |
| 34 |
\def \norange {\setflag \rng = \false}% |
| 35 |
\def \text {\setflag \txt = \true}% |
| 36 |
\def \notext {\setflag \txt = \false}% |
| 37 |
#2% |
| 38 |
{\def \attr {#2}% |
| 39 |
\edef \znext {% |
| 40 |
\noexpand\append{{#1}{\expandafter\zdefof \meaning\attr\zmark}}}% |
| 41 |
\znext{\zixloclist}}% |
| 42 |
\if \andp{\pg}{\notp{\txt}}% |
| 43 |
\withname\gdef{\x#1}##1{\zixindex{#1}{S}{##1}{}}% |
| 44 |
\if \rng |
| 45 |
\withname\gdef{\x#1begin}##1{\zixindex{#1}{B}{##1}{}}% |
| 46 |
\withname\gdef{\x#1end}##1{\zixindex{#1}{E}{##1}{}}% |
| 47 |
\fi |
| 48 |
\else\if \txt |
| 49 |
\withname\gdef{\x#1}##1##2{\zixindex{#1}{S}{##1}{##2}}% |
| 50 |
\if \rng |
| 51 |
\withname\gdef{\x#1begin}##1##2{\zixindex{#1}{B}{##1}{##2}}% |
| 52 |
\withname\gdef{\x#1end}##1##2{\zixindex{#1}{E}{##1}{##2}}% |
| 53 |
\fi |
| 54 |
\else |
| 55 |
\error{locattr}{Invalid index locator attributes}% |
| 56 |
\fi\fi |
| 57 |
\if \notp{\emptyargp{#3}}% |
| 58 |
\definelocatorformat{#1}#3\relax |
| 59 |
\fi}} |
| 60 |
|
| 61 |
% Define Locator Formats |
| 62 |
% ------ ------- ------- |
| 63 |
|
| 64 |
|
| 65 |
% This list contains the definition of each locator format. It works like |
| 66 |
% the locator list above. |
| 67 |
|
| 68 |
\def \zixformatlist {} |
| 69 |
|
| 70 |
\def \definelocatorformat #1#2#3#4{% {name}{rank.order}{level-1-items}{level-2-items} |
| 71 |
{\def \lvli {#3}% |
| 72 |
\def \lvlii {#4}% |
| 73 |
\edef \znext {% |
| 74 |
\noexpand\append{{#1}{#2}% |
| 75 |
{\expandafter\zdefof \meaning\lvli\zmark}% |
| 76 |
{\expandafter\zdefof \meaning\lvlii\zmark}}}% |
| 77 |
\znext{\zixformatlist}}} |
| 78 |
|
| 79 |
% Builtin Locator Types |
| 80 |
% ------- ------- ----- |
| 81 |
|
| 82 |
|
| 83 |
% These locators are built-in for ease of use. Ranks 0, 1, and 5 and higher |
| 84 |
% are reserved for the user. Ranks 10 and higher result in deferred |
| 85 |
% locators. |
| 86 |
|
| 87 |
\definelocator{page}{\page\range} |
| 88 |
{{2.1}{prefix1:, |tmplt:?P|rtmplt:?P--?Q|sep:, }{}} |
| 89 |
|
| 90 |
\definelocator{figure}{\page\range} |
| 91 |
{{2.3}{prefix1:, |tmplt:?P\emph{f}|rtmplt:?P--?Q\emph{f}|sep:, }{}} |
| 92 |
|
| 93 |
\definelocator{note}{\page} |
| 94 |
{{2.5}{prefix1:, |tmplt:?P\emph{n}|sep:, }{}} |
| 95 |
|
| 96 |
\definelocator{table}{\page\range} |
| 97 |
{{2.7}{prefix1:, |tmplt:?P\emph{t}|rtmplt:?P--?Q\emph{t}|sep:, }{}} |
| 98 |
|
| 99 |
\definelocator{see}{\nopage\text} |
| 100 |
{{3}{prefix1:. \emph{See} |tmplt:?T|sep:; }{}} |
| 101 |
|
| 102 |
\definelocator{seealso}{\nopage\text} |
| 103 |
{{4}{prefix1:. \emph{See also} |tmplt:?T|sep:; }{}} |
| 104 |
|
| 105 |
% Generate Indexes |
| 106 |
% -------- ------- |
| 107 |
|
| 108 |
|
| 109 |
\def \zixnamelist {} |
| 110 |
\setflag \zindexing = \false |
| 111 |
|
| 112 |
|
| 113 |
\def \generateindex #1{% {name} |
| 114 |
\append{#1}{\zixnamelist}% |
| 115 |
\global\setflag \zindexing = \true} |
| 116 |
|
| 117 |
% Index File Handling |
| 118 |
% ----- ---- -------- |
| 119 |
|
| 120 |
%\definewrite{\zixrootfile} |
| 121 |
%\definewrite{\zixdivfile} |
| 122 |
\setflag \zixdivopen = \false |
| 123 |
|
| 124 |
|
| 125 |
\def \zindexinit {% |
| 126 |
\if \zindexing |
| 127 |
\maplist{\zixwritezzp{##1}}{\zixnamelist}% |
| 128 |
\immediate\openout \zixrootfile = \jobname.zzi\relax |
| 129 |
\immediate\write \zixrootfile |
| 130 |
{\zcomment Raw index entry file for book `\jobname' root.}% |
| 131 |
\fi} |
| 132 |
|
| 133 |
\def \zindexfinal {% |
| 134 |
\if \zindexing |
| 135 |
\immediate\write \zixrootfile {\zcomment [end]}% |
| 136 |
\immediate\closeout \zixrootfile |
| 137 |
\fi} |
| 138 |
|
| 139 |
\def \zindexdivinit #1{% {division} |
| 140 |
\if \zindexing |
| 141 |
\immediate\openout \zixdivfile = #1.zzi\relax |
| 142 |
\immediate\write \zixdivfile |
| 143 |
{\zcomment Raw index entry file for book `\jobname', division `#1'.}% |
| 144 |
\global\setflag \zixdivopen = \true |
| 145 |
\fi} |
| 146 |
|
| 147 |
\def \zindexdivfinal {% |
| 148 |
\if \zixdivopen |
| 149 |
\immediate\write \zixdivfile {\zcomment [end]}% |
| 150 |
\immediate\closeout \zixdivfile |
| 151 |
\global\setflag \zixdivopen = \false |
| 152 |
\fi} |
| 153 |
|
| 154 |
\def \zixwritezzp #1{% {name} |
| 155 |
\begingroup |
| 156 |
\processdesign{\index}{#1}% |
| 157 |
\immediate\openout \zwritea = #1.zzp\relax |
| 158 |
\immediate\write \zwritea {root \jobname;}% |
| 159 |
\immediate\write \zwritea {index #1;}% |
| 160 |
\immediate\write \zwritea {divisions \the\divisions;}% |
| 161 |
\maplist{\immediate\write \zwritea {define-locator ##1;}}{\zixloclist}% |
| 162 |
\maplist{\immediate\write \zwritea {define-format ##1;}}{\zixformatlist}% |
| 163 |
\immediate\write \zwritea {locators \the\locators;}% |
| 164 |
\immediate\write \zwritea {\the\indexparams}% |
| 165 |
\immediate\closeout \zwritea |
| 166 |
\endgroup} |
| 167 |
|
| 168 |
% Write Raw Index Entry |
| 169 |
% ----- --- ----- ----- |
| 170 |
|
| 171 |
|
| 172 |
% This macro is called from the indexing macros defined by |
| 173 |
% \definelocator. |
| 174 |
|
| 175 |
\def \zixindex #1#2#3#4{% {locator}{page-code}{headings}{[text]} |
| 176 |
\zixindexb {#1}{#2}{#4}#3|||\zmark} |
| 177 |
|
| 178 |
\catcode"B8 = \catother |
| 179 |
|
| 180 |
\def \zixindexb #1#2#3#4|#5|#6|#7\zmark{% {locator}{page-code}{text} |
| 181 |
% head1|head2|head3|...\zmark |
| 182 |
\if \zindexing |
| 183 |
\zbeginhidewrite |
| 184 |
\def \zhdx {#4}% |
| 185 |
\def \zhdy {#5}% |
| 186 |
\def \zhdz {#6}% |
| 187 |
\def \ztxt {#3}% |
| 188 |
\edef \znext {#1^^b8% |
| 189 |
#2^^b8% |
| 190 |
\noexpand\folio^^b8% |
| 191 |
\expandafter\zdefof \meaning\zhdx\zmark^^b8% |
| 192 |
\expandafter\zdefof \meaning\zhdy\zmark^^b8% |
| 193 |
\expandafter\zdefof \meaning\zhdz\zmark^^b8% |
| 194 |
\expandafter\zdefof \meaning\ztxt\zmark}% |
| 195 |
\write \if \zixdivopen \zixdivfile \else \zixrootfile \fi |
| 196 |
\expandafter{\znext}% |
| 197 |
\if \vmodep \repeatpenalty \fi % In case glue follows write. |
| 198 |
\zendhidewrite |
| 199 |
\fi} |
| 200 |
|
| 201 |
\catcode"B8 = \catinvalid |
| 202 |
|
| 203 |
% Discussion of Index Formatting |
| 204 |
% ---------- -- ----- ---------- |
| 205 |
|
| 206 |
|
| 207 |
% An index is formatted inside an index block, which is more or less |
| 208 |
% like a text block. Within the index block are occurences of the |
| 209 |
% \indexentry macro, one for each entry. In addition, the |
| 210 |
% \alphabreak macro can be used to produce breaks preceding the 'A' |
| 211 |
% entries, 'B' entries, etc. |
| 212 |
% |
| 213 |
% In an index block, parts 4 and 5 of the mark are used to remember |
| 214 |
% index headings so they can be carried forward to subsequent columns |
| 215 |
% or pages. The mark is used as follows: |
| 216 |
% |
| 217 |
% Part Subpart Usage |
| 218 |
% 4 1 Boolean, true if in the midst of a first-level entry. |
| 219 |
% 4 2 The first-level entry heading. |
| 220 |
% 5 1 Boolean, true if in the midst of a second-level entry. |
| 221 |
% 5 2 The second-level entry heading. |
| 222 |
|
| 223 |
% Index Block |
| 224 |
% ----- ----- |
| 225 |
|
| 226 |
|
| 227 |
\defineblock{\index}{\endindex}{\false}{redefine} |
| 228 |
|
| 229 |
%~block index Type |
| 230 |
% \def \alphaformat ##1{...} % Alphabetic break formatter. |
| 231 |
% \bodyfont = {\size\style} % Body font. |
| 232 |
% \clubpenalty = penalty % Club penalty is often different. |
| 233 |
% \columngutter = glue % Column gutter. |
| 234 |
% \divisions = {...} % Which divisions to include. |
| 235 |
% \leftindent = glue % Left indentation. |
| 236 |
% \locators = {...} % Which locators to include. |
| 237 |
% \indexparams = {...} % Parameters for index generator. |
| 238 |
% \parhang = glue % Paragraph hang. |
| 239 |
% \parindent = dimen % Paragraph indent. |
| 240 |
% \parrag = dimen % Paragraph raggedness. |
| 241 |
% \parskip = glue % Paragraph skip. |
| 242 |
% \position = {...} % Position and number of columns. |
| 243 |
% \rightindent = glue % Right indentation. |
| 244 |
%~end |
| 245 |
|
| 246 |
%\definetoks{\locators} |
| 247 |
%\definetoks{\indexparams} |
| 248 |
|
| 249 |
%\definecount{\previndexlevel}{0} |
| 250 |
%\definecount{\zixcols}{0} |
| 251 |
|
| 252 |
|
| 253 |
\def \index #1{% {type} |
| 254 |
\blockcantbein{\index}{\index}% |
| 255 |
\beginblockscope{index}% |
| 256 |
\global\increment \indexdepth |
| 257 |
\clubpenalty = \breakallowed %~default soft |
| 258 |
\parskip = 0pt %~default soft |
| 259 |
\processdesign{\index}{#1}% |
| 260 |
\global\increment \indexnumber |
| 261 |
\def \zixname {#1}% |
| 262 |
\zixparsepos |
| 263 |
\indexformat} |
| 264 |
|
| 265 |
\def \indexformat {% |
| 266 |
\the\bodyfont |
| 267 |
\alterindentation{\leftindent}{\rightindent}% |
| 268 |
\setparrag{\parrag}% |
| 269 |
\setpagecolumns{\zixpageuse}{\the\zixcols}{\columngutter}% |
| 270 |
\everycolumn = {\zixcarryover}% |
| 271 |
\begingroup |
| 272 |
\let \zfullcolumnbreak = \fullcolumnbreak |
| 273 |
\let \fullcolumnbreak = \zixcolumnbreak |
| 274 |
\let \shortcolumnbreak = \zixcolumnbreak |
| 275 |
\global\previndexlevel = 0\relax} |
| 276 |
|
| 277 |
\def \endindex {% |
| 278 |
\endgraf % End last entry. |
| 279 |
\vbox{}% % Force start of column if not already. |
| 280 |
\endgroup |
| 281 |
\zclearindexmarks |
| 282 |
\everycolumn = {}% |
| 283 |
\setpagecolumns{\usetextmeasure}{1}{0pt}% |
| 284 |
\global\decrement \indexdepth |
| 285 |
\endblockscope{index}} |
| 286 |
|
| 287 |
\def \zixparsepos {% |
| 288 |
\zixcols = 1 |
| 289 |
\def \zixpageuse {\usetextmeasure}% |
| 290 |
\setflag \zixrunin = \false |
| 291 |
\let \oneup = \relax |
| 292 |
\let \outline = \relax |
| 293 |
\def \runin {\setflag \zixrunin = \true}% |
| 294 |
\def \threeup {\zixcols = 3}% |
| 295 |
\def \twoup {\zixcols = 2}% |
| 296 |
\let \usetextmeasure = \relax |
| 297 |
\let \usetextwidth = \relax |
| 298 |
\def \usetypewidth {\def \zixpageuse {\usetypewidth}}% |
| 299 |
\the\position} |
| 300 |
|
| 301 |
% Format Index Entries |
| 302 |
% ------ ----- ------- |
| 303 |
|
| 304 |
|
| 305 |
\setflag \zixbreak = \false |
| 306 |
|
| 307 |
|
| 308 |
% This macro formats the main portion of an index entry. |
| 309 |
|
| 310 |
\def \indexentry #1#2#3{% level{heading}{locators} |
| 311 |
\ifcase #1% |
| 312 |
\or % Level 1 heading: |
| 313 |
\endgraf |
| 314 |
\zclearindexmarks |
| 315 |
\zixmaybeforcebreak |
| 316 |
\hangindent = \parhang \hangafter = 1 |
| 317 |
{\name{\index\zixname entryiformat}{#2\setmarkinfo{4}{\true}{#2}}{#3}}% |
| 318 |
\or % Level 2 heading: |
| 319 |
\if \notp{\zixrunin}% |
| 320 |
\endgraf |
| 321 |
\if \eqlp{\previndexlevel}{1}\vpenalty{\breaknever}\fi |
| 322 |
\fi |
| 323 |
\setmarkinfo{5}{\false}{}% |
| 324 |
\zixmaybeforcebreak |
| 325 |
\hangindent = \parhang \hangafter = 1 |
| 326 |
{\name{\index\zixname entryiiformat}{#2\setmarkinfo{5}{\true}{#2}}{#3}}% |
| 327 |
\or % Level 3 heading: |
| 328 |
\if \notp{\zixrunin}\endgraf \fi |
| 329 |
\zixmaybeforcebreak |
| 330 |
\hangindent = \parhang \hangafter = 1 |
| 331 |
{\name{\index\zixname entryiiiformat}{#2}{#3}}% |
| 332 |
\fi |
| 333 |
\global\previndexlevel = #1\relax |
| 334 |
\ignorespaces} |
| 335 |
|
| 336 |
% This macro format any deferred locators. |
| 337 |
|
| 338 |
\def \indexdeferred #1#2{% level{locators} |
| 339 |
\ifcase #1% |
| 340 |
\or % Level 1 deferred locators: |
| 341 |
\if \notp{\zixrunin}\endgraf \fi |
| 342 |
\setmarkinfo{5}{\false}{}% |
| 343 |
\zixmaybeforcebreak |
| 344 |
\hangindent = \parhang \hangafter = 1 |
| 345 |
{\name{\index\zixname deferrediformat}{#2}}% |
| 346 |
\or % Level 2 deferred locators: |
| 347 |
\if \notp{\zixrunin}\endgraf \fi |
| 348 |
\zixmaybeforcebreak |
| 349 |
\hangindent = \parhang \hangafter = 1 |
| 350 |
{\name{\index\zixname deferrediiformat}{#2}}% |
| 351 |
\fi |
| 352 |
\ignorespaces} |
| 353 |
|
| 354 |
\def \alphabreak #1{% {text} |
| 355 |
\endgraf |
| 356 |
\zclearindexmarks |
| 357 |
\zixmaybeforcebreak |
| 358 |
\alphaformat{#1}% |
| 359 |
\endgraf} |
| 360 |
|
| 361 |
\def \zclearindexmarks {% |
| 362 |
\setmarkinfo{4}{\false}{}% |
| 363 |
\setmarkinfo{5}{\false}{}} |
| 364 |
|
| 365 |
\def \zixcolumnbreak {% |
| 366 |
\setflag \zixbreak = \true} |
| 367 |
|
| 368 |
\def \zixmaybeforcebreak {% |
| 369 |
\if \zixbreak |
| 370 |
\zfullcolumnbreak |
| 371 |
\setflag \zixbreak = \false |
| 372 |
\fi} |
| 373 |
|
| 374 |
|
| 375 |
% This macro is invoked at the beginning of each column to perform |
| 376 |
% any first- and second-level entry carryovers. It only includes |
| 377 |
% carryovers on the first column of versos. |
| 378 |
|
| 379 |
\def \zixcarryover {% |
| 380 |
\if \andp{\evenpagep}{\firstcolumnp}% |
| 381 |
{\setflag \znext = {\markinfo{\bottommark}{4}{1}}% |
| 382 |
\if \znext |
| 383 |
\hangindent = \parhang \hangafter = 1 |
| 384 |
\name{\index\zixname carryoveriformat}{\markinfo{\bottommark}{4}{2}}% |
| 385 |
\setflag \znext = {\markinfo{\bottommark}{5}{1}}% |
| 386 |
\if \znext |
| 387 |
\hangindent = \parhang \hangafter = 1 |
| 388 |
\name{\index\zixname carryoveriiformat}{\markinfo{\bottommark}{5}{2}}% |
| 389 |
\fi |
| 390 |
\endgraf |
| 391 |
\tdimena = \zoutputcoldepth % Compensate for depth of |
| 392 |
\advance \tdimena by -\prevdepth % previous column v. depth |
| 393 |
\kern \tdimena % of carryover line(s). |
| 394 |
\fi}% |
| 395 |
\fi} |
| 396 |
|
| 397 |
% Built-in Entry Formats |
| 398 |
% -------- ----- ------- |
| 399 |
|
| 400 |
|
| 401 |
% These entry formats and carryovers are for a typical outliine style index. |
| 402 |
|
| 403 |
\def \indexoutlineentryiformat #1#2{% |
| 404 |
\noindent #1#2\par} |
| 405 |
|
| 406 |
\def \indexoutlineentryiiformat #1#2{% |
| 407 |
\indent #1#2\par} |
| 408 |
|
| 409 |
\def \indexoutlineentryiiiformat #1#2{% |
| 410 |
\indent\indent #1#2\par} |
| 411 |
|
| 412 |
\def \indexoutlinedeferrediformat #1{% |
| 413 |
\indent #1\par} |
| 414 |
|
| 415 |
\def \indexoutlinedeferrediiformat #1{% |
| 416 |
\indent\indent #1\par} |
| 417 |
|
| 418 |
\def \indexoutlinecarryoveriformat #1{% |
| 419 |
\noindent #1 \tie{\emph{(continued)}}\par} |
| 420 |
|
| 421 |
\def \indexoutlinecarryoveriiformat #1{% |
| 422 |
\indent #1 \tie{\emph{(continued)}}\par} |
| 423 |
|
| 424 |
% These entry formats and carryovers are for a typical run-in style index. |
| 425 |
|
| 426 |
\setflag \zixplain = \false |
| 427 |
|
| 428 |
\def \indexruninentryiformat #1#2{% |
| 429 |
\noindent #1#2% |
| 430 |
\global\setflag \zixplain = {\emptyargp{#2}}% |
| 431 |
\if \zixplain :\space \fi} |
| 432 |
|
| 433 |
\def \indexruninentryiiformat #1#2{% |
| 434 |
\if \notp{\zixplain};\space \fi |
| 435 |
\global\setflag \zixplain = \false |
| 436 |
#1#2} |
| 437 |
|
| 438 |
\def \indexruninentryiiiformat #1#2{% |
| 439 |
\error{runiniii}{It makes no sense to have level-3 entries in a run-in index}} |
| 440 |
|
| 441 |
\def \indexrunindeferrediformat #1{% |
| 442 |
. #1} |
| 443 |
|
| 444 |
\def \indexrunincarryoveriformat #1{% |
| 445 |
\noindent #1 \tie{\emph{(continued)}}\par} |
| 446 |
|
| 447 |
\def \indexrunincarryoveriiformat #1{% |
| 448 |
\relax} |