Debian Codegraph @

texmf-dist/source/lualatex/luaindex/luaindex.dtx

raw
1 % \CheckSum{421}
2 % \iffalse meta-comment
3 % ==========================================================================
4 % luaindex.dtx
5 % Copyright (c) Markus Kohm, 2011
6 %
7 % This file is part of the luaTeX package `luaindex'.
8 %
9 % This work may be distributed and/or modified under the conditions of
10 % the LaTeX Project Public License, version 1.3c of the license.
11 % The latest version of this license is in
12 % http://www.latex-project.org/lppl.txt
13 % and version 1.3c or later is part of all distributions of LaTeX
14 % version 2005/12/01 or later.
15 %
16 % This work has the LPPL maintenance status "maintained".
17 %
18 % The Current Maintainer and author of this work is Markus Kohm.
19 %
20 % This work consists of the files `luaindex.dtx' and `README'.
21 % ==========================================================================
22 % \fi^^A meta-comment
23 %
24 % \CharacterTable
25 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
26 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
27 % Digits \0\1\2\3\4\5\6\7\8\9
28 % Exclamation \! Double quote \" Hash (number) \#
29 % Dollar \$ Percent \% Ampersand \&
30 % Acute accent \' Left paren \( Right paren \)
31 % Asterisk \* Plus \+ Comma \,
32 % Minus \- Point \. Solidus \/
33 % Colon \: Semicolon \; Less than \<
34 % Equals \= Greater than \> Question mark \?
35 % Commercial at \@ Left bracket \[ Backslash \\
36 % Right bracket \] Circumflex \^ Underscore \_
37 % Grave accent \` Left brace \{ Vertical bar \|
38 % Right brace \} Tilde \~}
39 %
40 % \iffalse meta-comment
41 %<*dtx|ins>
42 \def\batchfile{luaindex.dtx}
43 \input docstrip.tex
44 \ifToplevel{%
45 \Msg{********************************************************************}
46 \Msg{*}
47 \Msg{* Steps of luaindex generation:}
48 \Msg{* =============================}
49 \Msg{*}
50 \Msg{* - Generation of all needed files:}
51 \keepsilent
52 \askforoverwritefalse
53 }
54 {
55 \obeyspaces\obeylines
56 \global\def\preambletext{
57 \space
58 Copyright (c) 2011 by Markus Kohm <komascript(at)gmx.info>
59
60 This file was generated from file(s) of luaindex distribution.
61 --------------------------------------------------------------
62
63 This work may be distributed and/or modified under the conditions of
64 the LaTeX Project Public License, version 1.3c of the license.
65 The latest version of this license is in
66 http://www.latex-project.org/lppl.txt
67 and version 1.3c or later is part of all distributions of LaTeX
68 version 2005/12/01 or later.
69
70 This work has the LPPL maintenance status "maintained".
71
72 The Current Maintainer and author of this work is Markus Kohm.
73
74 This file may only be distributed together with the files listed in
75 `luaindex.dtx'. You may however distribute the files listed in
76 `luaindex.dtx' without this file.
77
78 NOTE: THIS IS AN ALPHA-VERSION!
79 }
80 }
81 \expandafter\preamble
82 \preambletext
83 \endpreamble
84 \def\ThreeHyphens{---}
85 \let\MetaPrefix\ThreeHyphens
86 \expandafter\declarepreamble\expandafter\luapreamble
87 \preambletext
88 \endpreamble
89
90 \generate{%
91 \let\MetaPrefix\DoubleperCent
92 \usepreamble\defaultpreamble
93 \file{luaindex.ltx}{\from{luaindex.dtx}{manual}}%
94 \file{luaindex.sty}{\from{luaindex.dtx}{package}}%
95 \file{luaindex-example.ltx}{\from{luaindex.dtx}{example}}%
96 \let\MetaPrefix\ThreeHyphens
97 \usepreamble\luapreamble
98 \nopostamble
99 \file{luaindex.lua}{\from{luaindex.dtx}{lua}}%
100 }
101
102 \ifToplevel{%
103 \Msg{* \space\space done.}
104 \Msg{* - Generation of examples:}
105 \directlua{%
106 local retval = os.execute( "lualatex --jobname=luaindex-example
107 --interaction=batchmode luaindex-example.ltx" );
108 if ( retval == nil ) then
109 tex.error( "Shell escape not enabled!",
110 { "You should try",
111 " `luatex --shell-escape --jobname=luaindex luaindex.dtx'",
112 "to enable shell escape.",
113 "Otherwise you will get no example." } )
114 elseif ( retval == 0 ) then
115 else
116 tex.error( "Failed to generate the example",
117 { "I've tried to call",
118 " `lualatex --jobname=luaindex luaindex-example.ltx'",
119 "but got unexpected return value:",
120 retval }
121 )
122 end
123 }%
124 }
125
126 \ifToplevel{%
127 \Msg{* \space\space implemented not yet.}
128 \Msg{* - Generation of manual:}
129 \directlua{%
130 local retval = os.execute( "lualatex --jobname=luaindex
131 --interaction=batchmode luaindex.ltx" );
132 if ( retval == 0 ) then
133 retval = os.execute( "lualatex --jobname=luaindex
134 --interaction=batchmode luaindex.ltx" );
135 end
136 if ( retval == 0 ) then
137 retval = os.execute( "makeindex -q -s gind.ist luaindex" );
138 end
139 if ( retval == 0 ) then
140 retval = os.execute( "makeindex -q -s gglo.ist
141 -o luaindex.gls luaindex.glo" );
142 end
143 if ( retval == 0 ) then
144 retval = os.execute( "lualatex --jobname=luaindex
145 --interaction=batchmode luaindex.ltx" );
146 end
147 if ( retval == nil ) then
148 tex.error( "Shell escape not enabled!",
149 { "You should try",
150 " `luatex --shell-escape --jobname=luaindex luaindex.dtx'",
151 "to enable shell escape.",
152 "Otherwise you will get no manual." } )
153 elseif ( retval == 0 ) then
154 else
155 tex.error( "Failed to generate the manual",
156 { "I've tried to call",
157 " `lualatex --jobname=luaindex luaindex.ltx'",
158 "but got unexpected return value:",
159 retval }
160 )
161 end
162 }%
163 }
164
165 \ifToplevel{%
166 \Msg{* \space\space done.}
167 \Msg{*}
168 \Msg{* Now you may install all the files.}
169 \Msg{*}
170 \Msg{********************************************************************}
171 }
172
173 \csname endinput\endcsname
174 %</dtx|ins>
175 %<*dtx>
176 \ProvidesFile{luaindex.dtx}
177 %</dtx>
178 %<package|manuel|example>\NeedsTeXFormat{LaTeX2e}[1995/06/01]
179 %<package>\ProvidesPackage{luaindex}
180 %<manual>\ProvidesFile{luaindex.ltx}
181 %<example>\ProvidesFile{luaindex-example.ltx}
182 %<lua>if luatexbase.provides_module then
183 %<lua> luatexbase.provides_module({
184 %<lua> name = "luaindex",
185 %<lua> date = "2011/02/18",
186 %<lua> version = "0.1b",
187 %<lua> description = "LuaTeX index processor",
188 %<lua> author = "Markus Kohm",
189 %<lua> licence = "LPPL v1.3c or later"
190 %<lua> })
191 %<lua>end
192 %<*package|manual|dtx>
193 [2011/02/18 v0.1b LuaLaTeX Package
194 %<*manual>
195 documentation:
196 %</manual>
197 index processor]%
198 %</package|manual|dtx>
199 %<*manual>
200 \documentclass{scrdoc}
201 \usepackage{tabularx}
202 \usepackage[english]{babel}
203 \usepackage{xcolor}
204 \colorlet{mcode}{teal}
205 \usepackage{fontspec}
206 \providecommand*{\LuaTeX}{Lua\kern-.125em\TeX}
207 \providecommand*{\LuaLaTeX}{Lua\kern-.025em\LaTeX}
208 \CodelineIndex
209 \RecordChanges
210 \GetFileInfo{luaindex.ltx}
211 \title{%
212 Usage of\\
213 \LuaTeX{} module \texttt{luaindex}\\
214 and\\
215 \LuaLaTeX{} Package \texttt{luaindex}\\
216 for Generating Indexes\\
217 }
218 \date{\fileversion}
219 \author{Markus Kohm\thanks{komascript%
220 @%
221 gmx%
222 .%
223 info}}
224 \begin{document}
225 \maketitle
226 \DocInput{luaindex.dtx}
227 \end{document}
228 %</manual>
229 %\fi^^A meta-comment
230 %
231 % \changes{v0.1}{2011/01/26}{start of new package}
232 % \changes{v0.1b}{2011/02/18}{prefix `koma.' removed from Lua module}
233 %
234 % \begin{abstract}
235 % With \LuaTeX{} it would not be a problem to call an index processor like
236 % MakeIndex while running \LuaTeX{}. So the user would not longer require
237 % to call the index processor on his own. But on the other side Lua hat
238 % enough power to process the index itself. Package \texttt{luaindex} was
239 % made to do this. It consists primary of a Lua module:
240 % \texttt{luaindex.lua}. This provides functions to generate a new index
241 % (or several new indexes), add entries to it and print the index. To make
242 % the world easier there's an additional \LaTeX{} package:
243 % \texttt{luaindex.sty}.
244 % \end{abstract}
245 %
246 % \tableofcontents
247 %
248 % \section{Idea}
249 %
250 % We will explain this in a future release.
251 %
252 % \section{General Options}
253 %
254 % See implementation documentation.
255 %
256 % \section{Generating Index Entries}
257 %
258 % See implementation documentation.
259 %
260 % \section{Print an Index}
261 %
262 % See implementation documentation.
263 %
264 % \section{Known Issues}
265 %
266 % Currently the user documentation is not existing. Please use the
267 % implementation documentation and the example instead of. This will be
268 % changed in a future release but maybe not at a near future.
269 %
270 % Currently there are no attributes to give the different indexes different
271 % headings. You may redefine |\indexname| before printing an index to do
272 % so. Future releases will do this simply by option.
273 %
274 % Currently repeated pre-sort-replaces are not supported. Maybe they will in a
275 % future release.
276 %
277 % Currently page ranges are not supported. They will in a future release.
278 %
279 % Note: This is not even a beta version. It's only a proof of concept. Almost
280 % everything my be designed and implemented in a better kind. The author
281 % himself is just learning \LuaTeX{}.
282 %
283 % Nevertheless you may report bugs and patches to komascript^^A
284 % @^^A
285 % gmx^^A
286 % .^^A
287 % info.
288 %
289 %
290 % \StopEventually{%
291 % \PrintIndex
292 % \PrintChanges
293 % }
294 %
295 % \section{Implementation of Lua Module \texttt{luaindex.lua}}
296 %
297 % \iffalse meta-comment
298 %<*lua>
299 % \fi^^A meta-comment
300 %
301 % First of all wie define a new module named \texttt{luaindex}. All
302 % variables and functions will be local to this module.
303 % \begin{macrocode}
304 module("luaindex", package.seeall)
305 % \end{macrocode}
306 %
307 % To handle all indexes we have a variable named \texttt{indexes}. This is a
308 % table of index tables \emph{assoziated by the name of the index
309 % table}\marginpar{\makebox[0pt][l]{\color{mcode}^^A
310 % \begin{tabular}[t]{@{}l@{}}
311 % \texttt{indexes}=\{\\
312 % \enskip\emph{name}=\{\\
313 % \enskip\enskip\texttt{presortreplaces}=\{\\
314 % \enskip\enskip\enskip\{[\emph{pattern}]=\emph{replace}, \dots\kern.1em\},
315 % \dots\\
316 % \enskip\enskip\},\\
317 % \enskip\enskip\texttt{sortorderbychar}=\{\\
318 % \enskip\enskip\enskip[\emph{char}]=\emph{position}, \dots\\
319 % \enskip\enskip\},\\
320 % \enskip\enskip\{\\
321 % \enskip\enskip\enskip\texttt{sort}="\dots\kern.1em",\\
322 % \enskip\enskip\enskip\texttt{value}="\dots\kern.1em",\\
323 % \enskip\enskip\enskip\texttt{pages}=\{\dots\kern.1em\},\\
324 % \enskip\enskip\enskip\texttt{subindex}=\{\dots\kern.1em\}\\
325 % \enskip\enskip\}\\
326 % \enskip\}\\
327 % \}\\
328 % \end{tabular}
329 % }}
330 % \begin{itemize}
331 % \item Each index table has at least \emph{two elements} assoziated to
332 % \texttt{presortreplaces} and
333 % \texttt{sortorderbychar}.
334 % \item There may be additional numericly assoziated elements, the \emph{index
335 % entries}.
336 % \begin{itemize}
337 % \item Each index entry has a least \emph{two elements} assoziated to
338 % \texttt{sort} und \texttt{value}. Element \texttt{sort} is the sort key
339 % of the index entry. Element \texttt{value} is the print value of the
340 % index entry.
341 % \item Each index entry may have an element assoziated to
342 % \texttt{pages}. This is a table of print values, that will be used as
343 % page number of the entry. It need not to be numeric. This table hat
344 % numeric assoziations. Later addeed pages will be appended to the end of
345 % the table.
346 % \item Each index entry may habe an element assoziated to
347 % \texttt{subindex}. This is an index table too, but do not have elements
348 % \texttt{presortreplaces} or \texttt{sortorderbychar}.
349 % \end{itemize}
350 % \end{itemize}
351 % \begin{macrocode}
352 local indexes = {}
353 % \end{macrocode}
354 %
355 % Next we have a function\marginpar{\makebox[0pt][l]{\color{mcode}^^A
356 % \texttt{newindex(\emph{index name})}}} to generate a new \emph{index table}
357 % at \texttt{indexes}:
358 % \begin{macrocode}
359 function newindex( indexname )
360 indexes[indexname]={ presortreplaces = {},
361 sortorderbychar = {} }
362 end
363 % \end{macrocode}
364 % The function parameter is the name of the index. This is not realy a print
365 % name, but a simple assoziation name.
366 %
367 % Don't be impressed because of empty initialization of
368 % \texttt{presortreplaces} and \texttt{sortorderbychar}. We will have
369 % functions to change this.
370 %
371 % First of all, we have a function\marginpar{\makebox[0pt][l]{\color{mcode}^^A
372 % \begin{tabular}[t]{@{}l@{}l@{}}
373 % \texttt{sortorder(} & \texttt{\emph{index name},} \\
374 % & \texttt{\emph{sort-order})}\\
375 % \end{tabular}%
376 % }} to add a new sort order.
377 % \begin{macrocode}
378 function sortorder( indexname, sortorder )
379 local i, value
380 % \end{macrocode}
381 % The first parameter of the function is the name if the index table. If an
382 % index table with the given name does not exist, \TeX{} should release an
383 % error message with some optional help.
384 % \begin{macrocode}
385 local index = indexes[indexname]
386 if index == nil then
387 tex.error( "Unknown index `" .. indexname .. "'",
388 { "You've tried to add a new sortorder to an index, but there's no index with the",
389 "given name.",
390 "You should define the index using lua function ",
391 " `luaindex.newindex(\"" .. indexname .. "\")'",
392 "before."
393 }
394 )
395 else
396 if type(sortorder) == "string" then
397 % \end{macrocode}
398 % The second parameter of the function may be a string. The string simply is
399 % an concatenation of the character in the order that should be used to sort
400 % the index entries of this index. The index table assoziatione
401 % \texttt{sortorderbychar} is a table. The characters are the assoziation and
402 % the wanted sort position is the assoziated value.
403 % \begin{macrocode}
404 local value
405 i = 1
406 repeat
407 value = unicode.utf8.sub( sortorder, i, i )
408 %<debug> print( i, value )
409 if value then
410 index.sortorderbychar[value] = i
411 end
412 i = i + 1
413 until value == ""
414 else -- should be table
415 % \end{macrocode}
416 % The second parameter of the function may also be a table with numerical
417 % assoziations.
418 % \begin{macrocode}
419 for i, value in ipairs( sortorder ) do
420 index.sortorderbychar[value] = i
421 end
422 end
423 end
424 end
425 % \end{macrocode}
426 %
427 % Second manipulation function\marginpar{\makebox[0pt][l]{\color{mcode}^^A
428 % \begin{tabular}[t]{@{}l@{}l@{}}
429 % \texttt{presortreplace(} & \texttt{\emph{index name},} \\
430 % & \texttt{\emph{pass},} \\
431 % & \texttt{\emph{pattern},} \\
432 % & \texttt{\emph{replace})}\\
433 % \end{tabular}%
434 % }} is to add presort entries to a presort pass of
435 % an index. \texttt{\emph{pattern}} and \texttt{\emph{replace}} are
436 % strings. See Lua function \texttt{unicode.utf8.sub} for more information
437 % about these.
438 % \begin{macrocode}
439 function presortreplace( indexname, pass, pattern, replace )
440 local n
441 % \end{macrocode}
442 % The first parameter of the function is the name if the index table. If an
443 % index table with the given name does not exist, \TeX{} should release an
444 % error message with some optional help.
445 % \begin{macrocode}
446 local index = indexes[indexname]
447 if index == nil then
448 tex.error( "Unknown index `" .. indexname .. "'",
449 { "You've tried to add a new presort-replace to an index, but there's no index",
450 "with the given name.",
451 "You should define the index using lua function ",
452 " `luaindex.newindex(\"" .. indexname .. "\")'",
453 "before."
454 }
455 )
456 else
457 % \end{macrocode}
458 % If the index exists, we have to create replace tables for every pass until
459 % the given.
460 % \begin{macrocode}
461 for n = table.maxn(index.presortreplaces), pass, 1 do
462 if ( index.presortreplaces[n] == nil ) then
463 index.presortreplaces[n] = {}
464 end
465 end
466 % \end{macrocode}
467 % Last but not least we have to add a new replace to the pass:
468 % \begin{macrocode}
469 index.presortreplaces[pass][pattern]=replace
470 end
471 end
472 % \end{macrocode}
473 %
474 % Indexes are normally separated into single letters, all numbers and all
475 % other symbols. To do so, we have a new
476 % function\marginpar{\makebox[0pt][l]{\color{mcode}^^A
477 % \begin{tabular}[b]{@{}l@{~}l@{}}
478 % \texttt{local} & \texttt{getclass(} \\
479 % & \enskip\texttt{\emph{utf8-char})}\\
480 % \end{tabular}%
481 % }} that returns 1 for all other symbols, 2 for all numbers and 3 for all
482 % letters. Wether an UTF-8 character is a letter or not depends on the
483 % locale type ``\texttt{collate}''. You may set it using
484 % \texttt{os.setlocale("\emph{locale}", "collate")}.
485 % \begin{macrocode}
486 local function getclass( utfc )
487 local i
488 for i in unicode.utf8.gmatch( utfc, "%n" ) do
489 %<debug> print( utfc .. " is a number" )
490 return 2
491 end
492 for i in unicode.utf8.gmatch( utfc, "%a" ) do
493 %<debug> print( utfc .. " is a letter" )
494 return 3
495 end
496 %<debug> print( utfc .. " is a symbol" )
497 return 1
498 end
499 % \end{macrocode}
500 %
501 % Before printing or sorting we may want to replace\marginpar{%^^A
502 % \makebox[0pt][l]{\color{mcode}^^A
503 % \begin{tabular}[t]{@{}l@{~}l@{}}
504 % \texttt{local} & \texttt{do\_presortreplaces(} \\
505 % & \enskip\texttt{\emph{utf8-string},}\\
506 % & \enskip\texttt{\emph{replace table})}\\
507 % \end{tabular}%
508 % }} some strings. We have a table of those. At the string each occurence of
509 % the assoziation should be replaced by the assoziated value.
510 % \begin{macrocode}
511 local function do_presortreplaces( srcstr, presortreplace )
512 if presortreplace then
513 local pat, rep
514 for pat, rep in pairs( presortreplace ) do
515 srcstr = unicode.utf8.gsub( srcstr, pat, rep )
516 end
517 end
518 return srcstr
519 end
520 % \end{macrocode}
521 %
522 % Now let's print the index.\marginpar{\makebox[0pt][l]{\color{mcode}^^A
523 % \begin{tabular}[t]{@{}l@{~}l@{}}
524 % \texttt{local} & \texttt{printsubindex(} \\
525 % & \enskip\texttt{\emph{level},}\\
526 % & \enskip\texttt{\emph{index},}\\
527 % & \enskip\texttt{\emph{presortreplace\_zero})}\\
528 % \end{tabular}%
529 % }}
530 % There aren't much differences in printing an index or a sub-index to an
531 % index entry. We only need to know the level of the (sub-) index. level 0
532 % is the main index.
533 % \begin{macrocode}
534 local function printsubindex( level, index, presortreplace_zero )
535 local i,t,n,p,l
536 local group=""
537 local class=-1
538 % \end{macrocode}
539 % We build the \TeX{} index item command: \verb|\item|, \verb|\subitem|,
540 % \verb|\subsubitem| etc. depending on the level. So \texttt{level} is simply
541 % the number of \verb|sub| at the index item command.
542 % \begin{macrocode}
543 local item="\\"
544 for l = 1, level, 1 do
545 item = item .. "sub"
546 end
547 item = item .. "item "
548 % \end{macrocode}
549 % Walk through all index items.
550 % \begin{macrocode}
551 for i,t in ipairs( index ) do
552 % \end{macrocode}
553 % If \texttt{level} is 0, we are at the root index. We want to group this
554 % Index into numbers, symbols and single letters. To do so, we detect the
555 % class of the first character at the sort string and add \verb|\indexgroup|
556 % commands if neccessary.
557 % \begin{macrocode}
558 if ( level == 0 ) then
559 local sort=do_presortreplaces( t["sort"], presortreplace_zero )
560 local firstchar=unicode.utf8.upper( unicode.utf8.sub( sort, 1, 1 ) )
561 if ( firstchar ~= group ) then
562 local newclass
563 % \end{macrocode}
564 % The character differ, but we have to print the group only if the groups of
565 % the characters differ.
566 % \begin{macrocode}
567 newclass=getclass( firstchar )
568 if ( newclass == 1 and class ~= newclass ) then
569 tex.print( "\\indexgroup{\\symbolsname}" )
570 elseif ( newclass == 3 ) then
571 tex.print( "\\indexgroup{" .. firstchar .. "}" )
572 elseif ( newclass == 2 and class ~= newclass ) then
573 tex.print( "\\indexgroup{\\numbersname}" )
574 end
575 group=firstchar
576 class=newclass
577 end
578 end
579 % \end{macrocode}
580 % Now we have to print the index item. We use the \texttt{value} to be
581 % printed. If one or more pagenumbers are stored, we print them too. If the
582 % index entry has a sub index, we call \texttt{printsubindex} for this one
583 % with increased level.
584 % \begin{macrocode}
585 tex.sprint( item, t["value"] )
586 if t["pages"] then
587 tex.sprint( "\\indexpagenumbers{" )
588 for n,p in ipairs( t["pages"] ) do
589 tex.sprint( "\\indexpagenumber{", p, "}" )
590 end
591 tex.print( "}" )
592 end
593 if t["subindex"] then
594 printsubindex( level+1, t["subindex"], presortreplaces_zero )
595 end
596 end
597 end
598 % \end{macrocode}
599 % Printing\marginpar{\makebox[0pt][l]{\color{mcode}^^A
600 % \begin{tabular}[t]{@{}l@{}}
601 % \texttt{printindex(\emph{index name})}\\
602 % \end{tabular}%
603 % }} a whole index is simply the same like printing a sub index, but before
604 % printing the index, we have to test, wether the named index exists or
605 % not.
606 % \begin{macrocode}
607 function printindex( indexname )
608 local index=indexes[indexname]
609 if index == nil then
610 tex.error( "Unknown index `" .. indexname .. "'",
611 { "You've tried to print an index, but there's no index with the",
612 "given name.",
613 "You should define the index using lua function ",
614 " `luaindex.newindex(\"" .. indexname .. "\")'",
615 "before."
616 }
617 )
618 else
619 print( "Index: \"" .. indexname .. "\" with " .. table.maxn( index ) .. " level-0-entries" )
620 tex.print( "\\begin{theindex}" )
621 printsubindex(0,indexes[indexname],indexes[indexname].presortreplaces[0])
622 tex.print( "\\end{theindex}" )
623 end
624 end
625 % \end{macrocode}
626 %
627 % To sort the index character classes numbers, letters and other are not
628 % enough. So we build sub-classes\marginpar{\makebox[0pt][l]{\color{mcode}^^A
629 % \begin{tabular}[b]{@{}l@{~}l@{}}
630 % \texttt{local} & \texttt{getsubclass(} \\
631 % & \enskip\texttt{\emph{utf8-char})}\\
632 % \end{tabular}%
633 % }} inside these three classes.
634 % \begin{macrocode}
635 local function getsubclass( utfc )
636 local i
637 % \end{macrocode}
638 % Inside letters we want so sort upper case before lower case.
639 % \begin{macrocode}
640 for i in unicode.utf8.gmatch( utfc, "%l" ) do
641 return 1
642 end
643 for i in unicode.utf8.gmatch( utfc, "%u" ) do
644 return 2
645 end
646 % \end{macrocode}
647 % Inside other symbols we want so sort controls before spaces before
648 % punctuations before numbers before unknown.
649 % \begin{macrocode}
650 for i in unicode.utf8.gmatch( utfc, "%c" ) do
651 return 1
652 end
653 for i in unicode.utf8.gmatch( utfc, "%s" ) do
654 return 2
655 end
656 for i in unicode.utf8.gmatch( utfc, "%p" ) do
657 return 3
658 end
659 for i in unicode.utf8.gmatch( utfc, "%n" ) do
660 return 4
661 end
662 return 10 -- unkown is the biggest sub class
663 end
664 % \end{macrocode}
665 % To compare\marginpar{\makebox[0pt][l]{\color{mcode}^^A
666 % \begin{tabular}[t]{@{}l@{~}l@{}}
667 % \texttt{local} & \texttt{do\_strcmp(} \\
668 % & \enskip\texttt{\emph{first string},} \\
669 % & \enskip\texttt{\emph{second string},} \\
670 % & \enskip\texttt{\emph{sort order table})}\\
671 % \end{tabular}%
672 % }} two UTF8-strings we could simply use the string compare of
673 % Lua. But for our purpose this is not enough. So we've added a configurable
674 % sort order and now have to compare character by character depeding on this
675 % sort order.
676 % \begin{macrocode}
677 local function do_strcmp( first, second, sortorderbychar )
678 local secondtable = string.explode( second, "" )
679 local firstutf
680 local n = 1
681 %<debug> print( first .. ", " .. second );
682 for firstutf in string.utfcharacters( first ) do
683 local secondutf = unicode.utf8.sub( second, n, n )
684 n = n + 1;
685 if firstutf then
686 if secondutf ~= "" then
687 %<debug> print( " " .. firstutf .. ", " .. secondutf )
688 if firstutf ~= secondutf then
689 local firstn, secondn
690 if sortorderbychar then
691 firstn = sortorderbychar[firstutf]
692 secondn = sortorderbychar[secondutf]
693 end
694 % \end{macrocode}
695 % If both characters were in the sort order table with different index we may
696 % return -1, if the index of first was lower than second, and 1, if the index
697 % of first was higher than second.
698 % \begin{macrocode}
699 if firstn and secondn then
700 %<debug> print( " n: " .. firstn .. ", " .. secondn )
701 if firstn < secondn then
702 return -1
703 elseif firstn > secondn then
704 return 1
705 end
706 else
707 % \end{macrocode}
708 % If one character was not in the sort order table, we compare the classes and
709 % if same the sub-classes.
710 % \begin{macrocode}
711 local firstclass = getclass( firstutf )
712 local secondclass = getclass( secondutf )
713 if firstclass < secondclass then
714 return -1
715 elseif firstclass == secondclass then
716 local firstsubclass = getsubclass( firstutf)
717 local secondsubclass = getsubclass( secondutf )
718 if firstsubclass < secondsubclass then
719 return -1
720 elseif firstsubclass == secondsubclass then
721 if firstutf < secondutf then
722 return -1
723 else
724 return 1
725 end
726 else
727 return 1
728 end
729 else
730 return 1
731 end
732 end
733 end
734 else
735 % \end{macrocode}
736 % If the first string was longer than the second, it is greater.
737 % \begin{macrocode}
738 return 1
739 end
740 else
741 % \end{macrocode}
742 % If the first string was shorter than the second, it is lower.
743 % \begin{macrocode}
744 if secondutf ~= "" then
745 return -1
746 else
747 return 0 -- This should never happen!
748 end
749 end
750 end
751 % \end{macrocode}
752 % If the first string was shorter than the second, it is lower. If not they
753 % are same.
754 % \begin{macrocode}
755 if unicode.utf8.sub( second, n, n ) ~= "" then
756 return -1
757 else
758 return 0
759 end
760 end
761 % \end{macrocode}
762 %
763 % Now we are able to compare\marginpar{%^^A
764 % \makebox[0pt][l]{\color{mcode}^^A
765 % \begin{tabular}[t]{@{}l@{~}l@{}}
766 % \texttt{local} & \texttt{do\_indexcmp(} \\
767 % & \enskip\texttt{\emph{first string},}\\
768 % & \enskip\texttt{\emph{second string},}\\
769 % & \enskip\texttt{\emph{replace tables},}\\
770 % & \enskip\texttt{\emph{sort order table})}\\
771 % \end{tabular}%
772 % }} the sort value of two index entries. Before the
773 % first compare we do the first pre-sort replace. All other pre-sort replaces
774 % will be done only, if the sort entries are not same!
775 % \begin{macrocode}
776 local function do_indexcmp( firstsort, secondsort,
777 presortreplaces, sortorderbychar )
778 local pass = 0
779 local ncmp = 0
780 repeat
781 if presortreplaces and presortreplaces[pass] then
782 firstsort = do_presortreplaces( firstsort, presortreplaces[pass] )
783 secondsort = do_presortreplaces( secondsort, presortreplaces[pass] )
784 %<debug> print( "Replace-Pass " .. pass .. ": " .. firstsort .. ", " .. secondsort )
785 end
786 pass = pass + 1
787 ncmp = do_strcmp( firstsort, secondsort, sortorderbychar )
788 until ( ncmp ~= 0 ) or ( pass > table.maxn( presortreplaces ) )
789 %<*debug>
790 if ncmp < 0 then
791 print( firstsort .. "<" .. secondsort )
792 elseif ncmp == 0 then
793 print ( firstsort .. "=" .. secondsort )
794 else
795 print( firstsort .. ">" .. secondsort )
796 end
797 %</debug>
798 return ncmp
799 end
800 % \end{macrocode}
801 %
802 % Inserting\marginpar{%^^A
803 % \makebox[0pt][l]{\color{mcode}^^A
804 % \begin{tabular}[t]{@{}l@{~}l@{}}
805 % \texttt{local} & \texttt{subinsert(} \\
806 % & \enskip\texttt{\emph{index table},}\\
807 % & \enskip\texttt{\emph{replace tables},}\\
808 % & \enskip\texttt{\emph{sort order table},}\\
809 % & \enskip\texttt{\emph{page string},}\\
810 % & \enskip\texttt{\emph{sort value},}\\
811 % & \enskip\texttt{\emph{print value},}\\
812 % & \enskip\dots\kern.1em\texttt{)}\\
813 % \end{tabular}%
814 % }} a new entry to an index is same like inserting a new entry to a
815 % sub-index of an already existing entry. So we have only one local function
816 % for this. A new entry consists of a page string, that should be added to the
817 % page list of the entry, a sort value, that should be used to find the
818 % correct entry and a print value, that should be shown at the index. Entries
819 % are only same, if the compare of the sort value is 0 and the print values
820 % are same. A new entry may be not only a new entry to the top level but to
821 % sub levels. Because of this, there may be several pairs of sort- and print
822 % values. We use bisection search to find the insert position.
823 % \begin{macrocode}
824 local function subinsert( index, presortreplaces, sortorderbychar,
825 pagestring, sortvalue, outputvalue, ... )
826 local min = 1
827 local max = table.maxn(index)
828 local updown = 0
829
830 local n = math.ceil(( min + max ) / 2)
831 while min <= max do
832 updown = do_indexcmp( sortvalue, index[n].sort,
833 presortreplaces, sortorderbychar )
834 if updown == 0 then
835 % \end{macrocode}
836 % The sort values are compared to be same (after serveral replaces). But only
837 % if the print values are (without any replaces) same, we have to use this
838 % entry. In this case we add a new sub-entry to this entry and if no new sub
839 % entry was given the page string to the page table.
840 % \begin{macrocode}
841 if outputvalue == index[n].value then
842 %<debug> print( "The entries are same." )
843 if ( ... ) then
844 %<debug> print( " Adding subentry to already existing entry" )
845 if ( index[n].subindex == nil ) then
846 index[n].subindex = {}
847 end
848 subinsert( index[n].subindex, presortreplaces, sortorderbychar,
849 pagestring, ... )
850 else
851 %<debug> print( " Is the pagestring already at the pages table?" )
852 local i, p
853 for i, p in ipairs( index[n].pages ) do
854 if pagestring == p then
855 %<debug> print( "The pagestring is already at the pages table." )
856 %<debug> print( " We have nothing to do." )
857 return
858 end
859 %<debug> print( pagestring, "!=", p )
860 end
861 %<debug> print( "The pagestring was not at the pages table.",
862 %<debug> "Add the new pagestring to the pages table",
863 %<debug> "and stop processing." )
864 table.insert( index[n].pages, pagestring )
865 end
866 return
867 else
868 % \end{macrocode}
869 % If the print values are not same, we use sequential search for the position
870 % after the last entry with same sort value but different print value. This is
871 % the position to use for the new entry.
872 % \begin{macrocode}
873 %<debug> print( "The entries are not same.",
874 %<debug> "Search for the last entry, with same sort." )
875 repeat
876 n = n + 1
877 if n <= max then
878 updown = do_indexcmp( sortvalue, index[min].sort,
879 presortreplaces, sortorderbychar )
880 end
881 until n > max or updown ~= 0
882 min = n
883 max = n-1
884 end
885 elseif updown > 0 then
886 min = n+1
887 else
888 max = n-1
889 end
890 n = math.ceil(( min + max ) / 2)
891 %<debug> print ( min, max, n )
892 end
893 % \end{macrocode}
894 % if we have a new sub entry we add this to the new position. If not we simply
895 % add the new entry with the page table.
896 % \begin{macrocode}
897 if ( ... ) then
898 %<debug> print( "Generating new entry without page but subindex" )
899 table.insert( index, n,
900 { sort=sortvalue, value=outputvalue, subindex={} } )
901 %<debug> print( "Add subindex to new generated entry" )
902 subinsert( index[n].subindex, presortreplaces, sortorderbychar,
903 pagestring, ... )
904 else
905 %<debug> print( "Generating new entry with page" )
906 table.insert( index, n,
907 { sort=sortvalue, value=outputvalue, pages={pagestring} } )
908 end
909 end
910 % \end{macrocode}
911 % We've explained before, that inserting\marginpar{%^^A
912 % \makebox[0pt][l]{\color{mcode}^^A
913 % \begin{tabular}[t]{@{}l@{}}
914 % \texttt{insert(\emph{index name},}\\
915 % \enskip\texttt{\emph{page string},}\\
916 % \enskip\texttt{\emph{sort value},}\\
917 % \enskip\texttt{\emph{print value},}\\
918 % \enskip\dots\kern.1em\texttt{)}\\
919 % \end{tabular}%
920 % }} a new entry is same like inserting a entry to a sub entry. There's only
921 % one tiny difference: the replace tables and sort order are members of the
922 % index table.
923 % \begin{macrocode}
924 function insert( indexname, pagestring, sortvalue, outputvalue, ... )
925 local index=indexes[indexname]
926 subinsert( index, index.presortreplaces, index.sortorderbychar,
927 pagestring, sortvalue, outputvalue, ... )
928 end
929 % \end{macrocode}
930 %
931 % Last we will need a function,\marginpar{^^A
932 % \makebox[0pt][l]{\color{mcode}^^A
933 % \texttt{removeentries(\emph{index name})}}} that only removes all index
934 % entries but not presortreplaces or sortorderbychar.
935 % \begin{macrocode}
936 function removeentries( indexname )
937 local p = indexes[indexname].presortreplaces
938 local s = indexes[indexname].sortorderbychar
939 indexes[indexname]={ presortreplaces = p,
940 sortorderbychar = s }
941 end
942 % \end{macrocode}
943 %
944 % \iffalse meta-comment
945 %</lua>
946 % \fi^^A meta-comment
947 %
948 % \section{Implementation of \LaTeX{} Package \texttt{luaindex.sty}}
949 %
950 % \iffalse meta-comment
951 %<*package>
952 % \fi^^A meta-comment
953 %
954 % The \LaTeX{} package is user's candy but not necessary. You may use
955 % \texttt{luaindex.lua} directly, but \LaTeX{} users will expect a \LaTeX{}
956 % interface.
957 %
958 % \subsection{Package Startup}
959 %
960 % \LuaLaTeX{} must be used to use the package.
961 % \begin{macrocode}
962 \RequirePackage{ifluatex}
963 \ifluatex\else
964 \PackageError{luaindex}{lualatex needed}{%
965 Package `luaindex' needs LuaTeX.\MessageBreak
966 So you should use `lualatex' to process you document!\MessageBreak
967 See documentation of `luaindex' for further information.}%
968 \expandafter\expandafter\expandafter\csname endinput\endcsname
969 \fi
970 % \end{macrocode}
971 % \changes{v0.1b}{2011/02/18}{Using package \texttt{luatexbase-compat}}
972 % \begin{macrocode}
973 \RequirePackage{luatexbase-compat}[2010/10/10]
974 % \end{macrocode}
975 % \changes{v0.1b}{2011/02/18}{Using package \texttt{luatexbase-modutils}}
976 % \begin{macrocode}
977 \RequirePackage{luatexbase-modutils}[2010/10/10]
978 % \end{macrocode}
979 %
980 % We need some \LuaTeX{} primitives:
981 % \begin{macrocode}
982 \luatexbase@ensure@primitive{luaescapestring}
983 % \end{macrocode}
984 %
985 % We need some Lua functions:
986 % \begin{macrocode}
987 \directlua{%
988 if not tex.error then
989 luatexbase.module_error('luaindex',
990 'undefined function!\string\n%
991 LuaTeX function tex.error() needed but not defined.\string\n%
992 Maybe you are using the wrong version of LuaTeX.')
993 end
994 if not tex.print then
995 luatexbase.module_error('luaindex',
996 'undefined function!\string\n%
997 LuaTeX function tex.print() needed but not defined.\string\n%
998 Maybe you are using the wrong version of LuaTeX.')
999 end
1000 if not tex.sprint then
1001 luatexbase.module_error('luaindex',
1002 'undefined function!\string\n%
1003 LuaTeX function tex.sprint() needed but not defined.\string\n%
1004 Maybe you are using the wrong version of LuaTeX.')
1005 end
1006 }
1007 % \end{macrocode}
1008 %
1009 % Load an initialize lua module. We could do this much later, but it is very,
1010 % very important, so we do is as soon as possible.
1011 % \begin{macrocode}
1012 \RequireLuaModule{luaindex}
1013 % \end{macrocode}
1014 %
1015 % With \texttt{luaindex} we use a temporary index file, too. This is
1016 % necessary, because page numbers are only valid while output routine. So
1017 % usage of a temporary index file is a good solution to have correct page
1018 % numbers. If this file exists, we load it simply while |\begin{document}| and
1019 % then produce an new one. But loading the old one is not simply an
1020 % |\input|. Out temporary index file is a Lua file, so we use Lua function
1021 % \texttt{dofile} to load it.
1022 % \begin{macrocode}
1023 \newwrite\@indexfile
1024 \AtBeginDocument{%
1025 \IfFileExists{\jobname.ldx}{\directlua{dofile('\jobname.ldx')}}{}%
1026 \openout\@indexfile=\jobname.ldx
1027 }
1028 % \end{macrocode}
1029 %
1030 % \subsection{Options}
1031 %
1032 % We use a key-value interface even for options. Because of this we're using
1033 % \KOMAScript{} package scrbase.
1034 % \begin{macrocode}
1035 \RequirePackage{scrbase}
1036 \DefineFamily{luaindex}
1037 \DefineFamilyMember{luaindex}
1038 % \end{macrocode}
1039 %
1040 % \begin{option}{sortorder}
1041 % \begin{macro}{\luaindex@sortorder}
1042 % Support for individual sort order. Sort order is an attribute of the index
1043 % root Lua table. Because of this the option simply saves it and it will be
1044 % setup later while defining new indexes.
1045 % \begin{macrocode}
1046 \newcommand*{\luaindex@sortorder}{}
1047 \DefineFamilyKey{luaindex}{sortorder}{%
1048 \edef\luaindex@sortorder{#1}%
1049 }
1050 % \end{macrocode}
1051 % \end{macro}
1052 % \end{option}
1053 %
1054 % \begin{option}{locale}
1055 % If no individual sort order is given, the \emph{collate} locale would cause
1056 % the sort order. So we add an option make this locale changable. Note, that
1057 % changing this locale may also affect to other Lua functions!
1058 % \begin{macrocode}
1059 \DefineFamilyKey{luaindex}{locale}{%
1060 \if@atdocument
1061 \expandafter\@firstofone
1062 \else
1063 \expandafter\AtBeginDocument
1064 \fi
1065 {%
1066 \protected@write\@indexfile{}{%
1067 os.setlocale('#1','collate')
1068 }%
1069 }%
1070 }
1071 % \end{macrocode}
1072 % \end{option}
1073 %
1074 % \begin{option}{pageformat}
1075 % \begin{macro}{\luaindex@pageformat}
1076 % The page format is an attribute of every index entry. But you may define a
1077 % primary page format to be used, if no individual page format will be given.
1078 % \begin{macrocode}
1079 \newcommand*{\luaindex@pageformat}{}
1080 \DefineFamilyKey{luaindex}{pageformat}{%
1081 \def\luaindex@pageformat{#1}%
1082 }
1083 % \end{macrocode}
1084 % \end{macro}
1085 % \end{option}
1086 %
1087 % \begin{option}{singlepass}
1088 % This option changes the general behavior of |\printindex|. See definition of
1089 % |\printindex| for more information about.
1090 % \begin{macrocode}
1091 \FamilyBoolKey{luaindex}{singlepass}{@luaindexsinglepass}
1092 % \end{macrocode}
1093 % \end{option}
1094 %
1095 % Processing all the options while loading the package.
1096 % \begin{macrocode}
1097 \FamilyProcessOptions{luaindex}\relax
1098 % \end{macrocode}
1099 %
1100 % \begin{macro}{\setupluaindex}
1101 % This is only an convenience command for run time setup of \texttt{luadindex}
1102 % options.
1103 % \begin{macrocode}
1104 \newcommand*{\setupluaindex}{\FamilyOptions{luaindex}}
1105 % \end{macrocode}
1106 % \end{macro}
1107 %
1108 % \subsection{Some Usual Index Commands}
1109 %
1110 % \begin{macro}{\see}
1111 % \begin{macro}{\seealso}
1112 % |\see| and |\seealso| are common commands used at the page number
1113 % format. They are defined for compatibility. ^^A Fixme: Is this a good idea?
1114 % \begin{macro}{\seename}
1115 % \begin{macro}{\alsoname}
1116 % The two terms |\seename| and |\alsoname| are used by |\see| and |\seealso|
1117 % and needed to be defined also.
1118 % \begin{macrocode}
1119 \newcommand*\see[2]{\emph{\seename} #1}
1120 \providecommand*\seealso[2]{\emph{\alsoname} #1}
1121 \providecommand\seename{see}
1122 \providecommand*\alsoname{see also}
1123 % \end{macrocode}
1124 % \end{macro}
1125 % \end{macro}
1126 % \end{macro}
1127 % \end{macro}
1128 %
1129 %
1130 % \subsection{Generation of Indexes and Index Entries}
1131 %
1132 % \begin{macro}{\newindex}
1133 % We can handle not only one index but several indexes. To do so, we have to
1134 % create a new lua index table for each index. Just use
1135 % \begin{quote}|\newindex|\marg{index name}\end{quote} to do so. Additional
1136 % features may be set up using: \begin{quote}|\newindex|\oarg{index
1137 % options}\marg{index name}\end{quote} Currently all global options are
1138 % supported for \meta{index options}, but some will be ignored.
1139 % \begin{macrocode}
1140 \newcommand*{\newindex}[2][]{%
1141 \directlua{luaindex.newindex('\luatexluaescapestring{#2}')}%
1142 \begingroup
1143 \setupluaindex{#1}%
1144 \ifx\luaindex@sortorder\@empty\else
1145 \AtBeginDocument{%
1146 \protected@write\@indexfile{}{%
1147 luaindex.sortorder('\luatexluaescapestring{#2}',
1148 '\luaindex@sortorder')
1149 }}%
1150 \fi
1151 \endgroup
1152 }
1153 % \end{macrocode}
1154 % You may use |\newindex| at the document preamble only.
1155 % \begin{macrocode}
1156 \@onlypreamble\newindex
1157 % \end{macrocode}
1158 % \end{macro}
1159 %
1160 % \begin{macro}{\luaindex}
1161 % This command will be used to add a new root level entry to an index:
1162 % \begin{quote}|\luaindex|\marg{index
1163 % name}\oarg{options}\marg{entry}\end{quote}
1164 % \begin{description}
1165 % \item[\normalfont\meta{index name}] -- the name of the index to be
1166 % used. This has to be the same like you've used to create the new index
1167 % using |\newindex|.
1168 % \item[\normalfont\meta{options}] -- several options for the index
1169 % entry. Currently supported are:
1170 % \begin{description}
1171 % \item[\normalfont\texttt{locale=}\meta{locale specifier}] -- just calls
1172 % |\luaindexsetup|\marg{locale specifier}. Note, that this is a global
1173 % action!
1174 % \item[\normalfont\texttt{pageformat=}\meta{command}] -- is a command with
1175 % at most one argument to format the page number of the index entry. You
1176 % may, e.\,g., use |sort=\see|\marg{reference} or
1177 % |sort=\seealso|{\marg{reference}} to produce a ``see'' or ``see also''
1178 % cross reference to \meta{reference} instead of showing a real page
1179 % number.
1180 % \item[\normalfont\texttt{sort=}\meta{sort entry}] -- destines the sort
1181 % position of the index entry. If it is omitted \meta{entry} will be used
1182 % instead.
1183 % \end{description}
1184 % \item[\normalfont\meta{entry}] -- this will be shown in the index.
1185 % \end{description}
1186 % Note: An index entry is only same, if \meta{sort entry} is same (after
1187 % several presort replaces) and \meta{entry} is same. Index entries
1188 % with same \meta{sort entry} but different \meta{entry} will be placed
1189 % at the current end of the entries with same \meta{sort entry}.
1190 % \begin{macrocode}
1191 \newcommand*{\luaindex}[1]{%
1192 \@bsphack
1193 \begingroup
1194 \edef\luaindex@name{#1}%
1195 \lua@index
1196 }
1197 \newcommand*{\lua@index}[2][]{%
1198 \set@display@protect
1199 \edef\luaindex@sort{#2}%
1200 \define@key{luaindex.setindex}{sort}{\edef\luaindex@sort{##1}}%
1201 \define@key{luaindex.setindex}{pageformat}{\def\luaindex@pageformat{##1}}%
1202 \define@key{luaindex.setindex}{locale}{\luaindexsetup{locale=#1}}%
1203 \setkeys{luaindex.setindex}{#1}%
1204 \protected@write\@indexfile{\let\luatexluaescapestring\relax}{%
1205 luaindex.insert('\luatexluaescapestring{\luaindex@name}',
1206 '{\luatexluaescapestring{\luaindex@pageformat{\thepage}}}',
1207 '\luatexluaescapestring{\luaindex@sort}',
1208 '\luatexluaescapestring{#2}')
1209 }%
1210 \endgroup
1211 \@esphack
1212 }
1213 % \end{macrocode}
1214 % \end{macro}
1215 %
1216 % \begin{macro}{\luasubindex}
1217 % \begin{macro}{\lua@subindex}
1218 % \begin{macro}{\lua@@subindex}
1219 % Same like |\luaindex| but to produce a sub entry:
1220 % \begin{quote}|\luasubindex|\marg{index name}\oarg{options}\marg{entry}^^A
1221 % \linebreak[2]\oarg{options}\marg{sub-entry}\end{quote} Note, that the
1222 % \meta{options} for the \meta{sub-entry} only allows a sub-set of the options
1223 % shown for |\luaindex|. Currently only \texttt{sort=}\meta{sort entry}.
1224 % \begin{macrocode}
1225 \newcommand*{\luasubindex}[1]{%
1226 \@bsphack
1227 \begingroup
1228 \edef\luaindex@name{#1}%
1229 \lua@subindex
1230 }
1231 \newcommand*{\lua@subindex}[2][]{%
1232 \set@display@protect
1233 \edef\luaindex@sort{#2}%
1234 \define@key{luaindex.setindex}{sort}{\edef\luaindex@sort{##1}}%
1235 \define@key{luaindex.setindex}{pageformat}{\def\luaindex@pageformat{##1}}%
1236 \define@key{luaindex.setindex}{locale}{\luaindexsetup{locale=#1}}%
1237 \setkeys{luaindex.setindex}{#1}%
1238 \protected@write\@indexfile{\let\luatexluaescapestring\relax}{%
1239 luaindex.insert('\luatexluaescapestring{\luaindex@name}',
1240 '{\luatexluaescapestring{\luaindex@pageformat{\thepage}}}',
1241 '\luatexluaescapestring{\luaindex@sort}',
1242 '\luatexluaescapestring{#2}',
1243 }%
1244 \aftergroup\lua@@subindex
1245 \endgroup
1246 }
1247 \newcommand*{\lua@@subindex}[2][]{%
1248 \begingroup
1249 \set@display@protect
1250 \edef\luaindex@sort{#2}%
1251 \define@key{luaindex.setindex}{sort}{\edef\luaindex@sort{##1}}%
1252 \setkeys{luaindex.setindex}{#1}%
1253 \protected@write\@indexfile{\let\luatexluaescapestring\relax}{%
1254 \@spaces
1255 '\luatexluaescapestring{\luaindex@sort}',
1256 '\luatexluaescapestring{#2}')
1257 }%
1258 \endgroup
1259 \@esphack
1260 }
1261 % \end{macrocode}
1262 % \end{macro}
1263 % \end{macro}
1264 % \end{macro}
1265 %
1266 % \begin{macro}{\luasubsubindex}
1267 % \begin{macro}{\lua@subsubindex}
1268 % \begin{macro}{\lua@@@subindex}
1269 % Same like |\luaindex| but to produce a sub-sub-entry, that is a sub-entry to
1270 % a sub-entry:
1271 % \begin{quote}|\luasubindex|\marg{index
1272 % name}\oarg{options}\marg{entry}\linebreak[2]^^A
1273 % \oarg{options}\marg{sub-entry}\linebreak[2]^^A
1274 % \oarg{options}\linebreak[2]\marg{sub-sub-entry}\end{quote}
1275 % Note, that the \meta{options} for the \meta{sub-entry} and the
1276 % \meta{sub-sub-entry} only allows a sub-set of the options shown for
1277 % |\luaindex|. Currently only \texttt{sort=}\meta{sort entry}.
1278 % \begin{macrocode}
1279 \newcommand*{\luasubsubindex}[1]{%
1280 \@bsphack
1281 \begingroup
1282 \edef\luaindex@name{#1}%
1283 \lua@subsubindex
1284 }
1285 \newcommand*{\lua@subsubindex}[2][]{%
1286 \set@display@protect
1287 \edef\luaindex@sort{#2}%
1288 \define@key{luaindex.setindex}{sort}{\edef\luaindex@sort{##1}}%
1289 \define@key{luaindex.setindex}{pageformat}{\def\luaindex@pageformat{##1}}%
1290 \define@key{luaindex.setindex}{locale}{%
1291 \luaindexsetup{locale=#1}%
1292 }
1293 \setkeys{luaindex.setindex}{#1}%
1294 \protected@write\@indexfile{\let\luatexluaescapestring\relax}{%
1295 luaindex.insert('\luatexluaescapestring{\luaindex@name}',
1296 '{\luatexluaescapestring{\luaindex@pageformat{\thepage}}}',
1297 '\luatexluaescapestring{\luaindex@sort}',
1298 '\luatexluaescapestring{#2}',
1299 }%
1300 \aftergroup\lua@@@subindex
1301 \endgroup
1302 }
1303 \newcommand*{\lua@@@subindex}[2][]{%
1304 \begingroup
1305 \set@display@protect
1306 \edef\luaindex@sort{#2}%
1307 \define@key{luaindex.setindex}{sort}{\edef\luaindex@sort{##1}}%
1308 \setkeys{luaindex.setindex}{#1}%
1309 \protected@write\@indexfile{\let\luatexluaescapestring\relax}{%
1310 \@spaces
1311 '\luatexluaescapestring{\luaindex@sort}',
1312 '\luatexluaescapestring{#2}',
1313 }%
1314 \aftergroup\lua@@subindex
1315 \endgroup
1316 }
1317 % \end{macrocode}
1318 % \end{macro}
1319 % \end{macro}
1320 % \end{macro}
1321 %
1322 % \begin{macro}{\makeindex}
1323 % \begin{macro}{\index}
1324 % \begin{macro}{\subindex}
1325 % \begin{macro}{\subsubindex}
1326 % These are defined to increase compatibility to old index packages
1327 % only. Command |\makeindex| simply generates the new index named
1328 % \texttt{general} and the other commands to add entries to that index. Note,
1329 % that adding a sub-entry or sub-sub-entry is not yet compatible to other
1330 % index packages. You need to use the command |\subindex| and |\subsubindex|
1331 % instead of something like
1332 % |\index{|\meta{entry}|!|\meta{sub-entry}|!|\meta{sub-sub-entry}|}|. Note
1333 % also, that changing the format of the page number is not compatible with
1334 % other index packages. You have to use |\index[pageformat=|\meta{page
1335 % format}|]{|\dots\kern.1em|}| instead of something like
1336 % |\index{|\meta{entry}\verb+|+\meta{page format}|}|.
1337 % \begin{macrocode}
1338 \renewcommand*{\makeindex}{%
1339 \newindex{general}%
1340 \renewcommand*\index{\luaindex{general}}%
1341 \newcommand*\subindex{\luasubindex{general}}%
1342 \newcommand*\subsubindex{\luasubsubindex{general}}%
1343 }
1344 % \end{macrocode}
1345 % \end{macro}
1346 % \end{macro}
1347 % \end{macro}
1348 % \end{macro}
1349 %
1350 %
1351 % \subsection{Printing an Index}
1352 %
1353 % We do not only want to create an index, we also need to print it.
1354 %
1355 % \begin{macro}{\printindex}
1356 % With
1357 % \begin{quote}|\printindex|\oarg{options}\end{quote}
1358 % you can print an index. The known options are
1359 % \begin{description}
1360 % \item[\normalfont\texttt{index=}\meta{index name}] -- print the index with
1361 % the given name as declared at |\newindex|. If you omit this option, index
1362 % ``\texttt{general}'' will be printed.
1363 % \item[\normalfont\texttt{singlepass=}\meta{boolean value}] -- you may switch
1364 % on and of the single pass feature. For the differences of single pass
1365 % feature on and off, see table~\ref{tab:singlepass}
1366 % \begin{table}
1367 % \centering
1368 % \caption{Implications of option \texttt{singlepass} to
1369 % \texttt{\string\printindex}}
1370 % \label{tab:singlepass}
1371 % \begin{tabularx}{\linewidth}{@{}>{\raggedright}X>{\raggedright}X}
1372 % \firsthline
1373 % \texttt{singlepass=false} & \texttt{singlepass=true} \tabularnewline
1374 % \hline
1375 % index of previous \LuaLaTeX{} run will be printed
1376 % & index of current \LuaLaTeX{} run will be printed
1377 % \tabularnewline[1ex]
1378 % start of index depends on the class
1379 % & start of the index at next page earliest
1380 % \tabularnewline[1ex]
1381 % index entries may be added to an index even after it has been printed
1382 % & no more index entries may be added to the index after it has been
1383 % printed
1384 % \tabularnewline
1385 % \lasthline
1386 % \end{tabularx}
1387 % \end{table}
1388 % \end{description}
1389 % \begin{macrocode}
1390 \newcommand*{\printindex}[1][]{%
1391 \begingroup
1392 \edef\luaindex@name{general}%
1393 \define@key{luaindex.setindex}{index}{\edef\luaindex@name{##1}}%
1394 \define@key{luaindex.setindex}{singlepass}[true]{%
1395 \setupluaindex{singlepass}{##1}%
1396 }%
1397 \setkeys{luaindex.setindex}{#1}%
1398 \if@luaindexsinglepass
1399 \closeout\@indexfile
1400 \clearpage
1401 \directlua{%
1402 luaindex.removeentries('\luatexluaescapestring{\luaindex@name}')
1403 dofile('\jobname.ldx')
1404 }%
1405 \fi
1406 \directlua{%
1407 luaindex.printindex('\luatexluaescapestring{\luaindex@name}')
1408 }%
1409 \endgroup
1410 }
1411 % \end{macrocode}
1412 % \end{macro}
1413 %
1414 % \texttt{luaindex.lua} uses several macros while printing the index. First of
1415 % all it uses the environment \texttt{theindex}. But several additional macros
1416 % will be used:
1417 %
1418 % \begin{macro}{\indexgroup}
1419 % \begin{macro}{\indexspace}
1420 % \begin{macro}{\symbolsname}
1421 % \begin{macro}{\numbersname}
1422 % Each index is grouped. Index groups are symbols, numbers and each first
1423 % letter. Each group starts with |\indexgroup|\marg{group} with group is
1424 % either |\symbolsname|, |\numbersname| or a upper case letter. In difference
1425 % to other index processors no automatic |\indexspace| will be added before
1426 % each group. So we define |\indexgroup| to add it.
1427 % \begin{macrocode}
1428 \providecommand*{\indexgroup}[1]{%
1429 \indexspace\textbf{#1}\nopagebreak
1430 }
1431 \providecommand*{\indexspace}{%
1432 \def\indexspace{\vskip\baselineskip}
1433 }
1434 \providecommand*{\symbolsname}{Symbols}
1435 \providecommand*{\numbersname}{Numbers}
1436 \AtBeginDocument{%
1437 \providecaptionname{english}\symbolsname{Symbols}%
1438 \providecaptionname{english}\numbersname{Numbers}%
1439 \providecaptionname{german}\symbolsname{Symbole}%
1440 \providecaptionname{german}\numbersname{Zahlen}%
1441 \providecaptionname{ngerman}\symbolsname{Symbole}%
1442 \providecaptionname{ngerman}\numbersname{Zahlen}%
1443 \providecaptionname{austrian}\symbolsname{Symbole}%
1444 \providecaptionname{austrian}\numbersname{Zahlen}%
1445 \providecaptionname{naustrian}\symbolsname{Symbole}%
1446 \providecaptionname{naustrian}\numbersname{Zahlen}%
1447 \providecaptionname{french}\symbolsname{Symbole}%
1448 \providecaptionname{french}\numbersname{Chiffres}%
1449 \providecaptionname{spanish}\symbolsname{Simbolos}%
1450 \providecaptionname{spanish}\numbersname{N\'umeros}%
1451 }
1452 % \end{macrocode}
1453 % \end{macro}
1454 % \end{macro}
1455 % \end{macro}
1456 % \end{macro}
1457 %
1458 % \begin{macro}{\indexpagenumbers}
1459 % \begin{macro}{\indexpagenumber}
1460 % \begin{macro}{\indexpagenumbersep}
1461 % \begin{macro}{\index@pagenumbersep}
1462 % The page numbers of an entry are printed all together as argument of
1463 % |\indexpagenumbers|\marg{page number}. Each single page number is printed as
1464 % argument of |\indexpagenumber|\marg{page number}. So separate the single
1465 % page numbers |\indexpagenumber| is predefined to add internal macro
1466 % |\index@pagenumbersep| before the page number. This will add
1467 % |\indexpagenumbersep| before each page number but the first one.
1468 % \begin{macrocode}
1469 \providecommand*{\indexpagenumbers}[1]{%
1470 \def\index@pagenumbersep{\let\index@pagenumbersep\indexpagenumbersep}%
1471 \nobreakspace-- #1}
1472 \providecommand*{\indexpagenumber}[1]{\index@pagenumbersep #1}
1473 \providecommand*{\indexpagenumbersep}{, }
1474 % \end{macrocode}
1475 % \end{macro}
1476 % \end{macro}
1477 % \end{macro}
1478 % \end{macro}
1479 %
1480 % \iffalse meta-comment
1481 %</package>
1482 % \fi^^A meta-comment
1483 %
1484 % \section{Examples}
1485 %
1486 % \iffalse meta-comment
1487 %<*example>
1488 % \fi^^A meta-comment
1489 %
1490 % Currently only one example file will be produced:
1491 % \begin{description}
1492 % \item[\normalfont\texttt{luaindex-example}] -- This should show index
1493 % entries, index sub-entries, index sub-sub-entries.
1494 % \begin{macrocode}
1495 \documentclass{article}
1496 \usepackage[ngerman]{babel}
1497 \usepackage{blindtext}
1498 \usepackage{fontspec}
1499 % \end{macrocode}
1500 % We load package \texttt{luaindex} with option \texttt{locale=de\_DE}. At
1501 % least at Linux this will add \"A, \"O, \"U, \"a, \"o, \"u, and \ss{} to the
1502 % letters and even set a valid sort order for those.
1503 %
1504 % We load package \texttt{luaindex} with option \texttt{singlepass} to produce
1505 % a valid index with one \LuaLaTeX{} run instead of two or more. But with this
1506 % printing of the index will produce a new page.
1507 % \begin{macrocode}
1508 \usepackage[
1509 locale=de_DE,
1510 singlepass % Wenn der Index ohnehin eine neue Seite produziert,
1511 % dann kann er direkt beim ersten Lauf ein korrektes
1512 % Ergebnis liefern.
1513 ]{luaindex}
1514 % \end{macrocode}
1515 %
1516 % We use the compatibility command |\makeindex| to generate the ``general''
1517 % index and the further compatibility commands, e.g., |\index|.
1518 % \begin{macrocode}
1519 \makeindex
1520 % \end{macrocode}
1521 %
1522 % We want |\textbf| to be ignored at the sort:
1523 % \begin{macrocode}
1524 \directlua{luaindex.presortreplace('general',0,
1525 '\luatexluaescapestring{\string\textbf}\space*\string\{([\string^\string\}]*)\string\}','\%1')}
1526 % \end{macrocode}
1527 %
1528 % Now we can start our document. This consist of some text and several index
1529 % entries.
1530 % \begin{macrocode}
1531 \begin{document}
1532
1533 \blindtext[10]
1534 A\index{B ist der zweite Buchstabe}
1535 aber\index{aber ist ein Wort}
1536 D\index{D ist der vierte Buchstabe}
1537 A\index{A ist der erste Buchstabe}
1538 A\index{A ist der erste Buchstabe}
1539 % \end{macrocode}
1540 % Now, let's do something different. Let's show that babel shorthands may be
1541 % used inside index entries:
1542 % \begin{macrocode}
1543 C\index{C ist "`der"' dritte Buchstabe}
1544 X\index{X ist der drittletzte Buchstabe}
1545 % \end{macrocode}
1546 % And macros may also be used but change the sort sequence of the index!
1547 % \begin{macrocode}
1548 D\index{\textbf{D} ist der Buchstabe nach C}
1549 Y\index{Y ist der \textbf{vorletzte} Buchstabe}
1550 Z\index{Z ist der letzte Buchstabe}
1551 A\index{Ä ist auch ein Buchstabe}
1552 % \end{macrocode}
1553 % We may change the sort sequence manually by adding the \texttt{sort}
1554 % option. The page number format may also be changed using the
1555 % \texttt{pageformat} option.
1556 % \begin{macrocode}
1557 Ä\index[sort={Ä ist aber auch ein Buchstabe},%
1558 pageformat=\emph]{Ä ist wirklich auch
1559 ein Buchstabe (und hier stimmt die Sortierung
1560 nicht -- \emph{aber eigentlich doch})}
1561 % \end{macrocode}
1562 % Let's add one more page with some more index entries:
1563 % \begin{macrocode}
1564 \clearpage
1565
1566 A\index{A ist der erste Buchstabe}
1567 Ae\index{Ae ist kein Buchstabe, sondern zwei}
1568
1569 % \end{macrocode}
1570 % And now, let's have some sub-entries and even a sub-sub-entry. One of the
1571 % sub-entries will become a different sort position and will be marked with an
1572 % emphasized page number.
1573 % \begin{macrocode}
1574 Kompliziert\subindex{Diverses}{Untereintrag}
1575 Noch komplizierter\subindex{Diverses}{Obereintrag}
1576 Noch komplizierter\%
1577 subindex{Diverses}[sort=Obereintra,pageformat=\emph]{Untereintrag}
1578 Noch komplizierter%
1579 \subsubindex{Diverses}{Untereintrag}{Unteruntereintrag}
1580
1581 % \end{macrocode}
1582 % That's enough. Time time to print the index. Remember, that this is already
1583 % a valid index, because we are using option \texttt{singlepass}.
1584 % \begin{macrocode}
1585 \printindex
1586 \end{document}
1587 % \end{macrocode}
1588 % \end{description}
1589 %
1590 % \iffalse meta-comment
1591 %</example>
1592 % \fi^^A meta-comment
1593 %
1594 % \Finale
1595 %
1596 \endinput
1597 %% Local Variables:
1598 %% TeX-master: t
1599 %% TeX-PDF-mode: t
1600 %% TeX-engine: luatex
1601 %% flyspell-mode: t
1602 %% End: