| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \makeindex {Makeindex and sorting <2024-12-01>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ----------------------------- |
| 6 |
\^`\makeindex` implements sorting algorithm at \TeX/ macro-language level. |
| 7 |
You need not any external program. The sorting can be used for various |
| 8 |
other applications, see an example in |
| 9 |
\ulink[http://petr.olsak.net/optex/optex-tricks.html\#sort]{\OpTeX/ trick~0068}. |
| 10 |
|
| 11 |
There are two passes in the sorting algorithm. The primary pass does not |
| 12 |
distinguish between a group of letters (typically non-accented and |
| 13 |
accented). If the result of comparing two string is equal in primary pass |
| 14 |
then the secondary pass is started. It distinguishes between variously accented |
| 15 |
letters. Czech rules, for example, says: not accented before dieresis |
| 16 |
before acute before circumflex before ring. At less priority: lowercase |
| 17 |
letters must be before uppercase letters. |
| 18 |
|
| 19 |
The \`\_sortingdatalatin` |
| 20 |
implements these rules for the languages with latin alphabets. |
| 21 |
The groups between commas are not distinguished in the first |
| 22 |
pass. The second pass distinguishes all characters mentioned in the |
| 23 |
\^`\_sortingdatalatin` (commas are ignored). The order of letters |
| 24 |
in the \^`\_sortingdatalatin` macro is significant for the sorting algorithm. |
| 25 |
\_cod ----------------------------- |
| 26 |
|
| 27 |
\_def \_sortingdatalatin {% |
| 28 |
|,<,/,{ },_,-,&,@,% |
| 29 |
aAàÀâÂäÄáÁ,% |
| 30 |
ąĄ,% |
| 31 |
bB,% |
| 32 |
cC,% |
| 33 |
ćĆčČ,% |
| 34 |
dDďĎ,% |
| 35 |
eEèÈéÉëËêÊěĚ,% |
| 36 |
ęĘ,% |
| 37 |
fF,% |
| 38 |
gG,% |
| 39 |
hH,% |
| 40 |
^^T^^U^^V,% ch Ch CH |
| 41 |
iIíÍïÏîÎ,% |
| 42 |
jJ,% |
| 43 |
kK,% |
| 44 |
lLĺĹľĽ,% |
| 45 |
łŁ,% |
| 46 |
mM,% |
| 47 |
nNňŇ,% |
| 48 |
ńŃñÑ,% |
| 49 |
oOöÖóÓôÔ,% |
| 50 |
pP,% |
| 51 |
qQ,% |
| 52 |
rRŕŔ,% |
| 53 |
řŘ,% |
| 54 |
sSß,% |
| 55 |
śŚšŠ,% |
| 56 |
tTťŤ,% |
| 57 |
uUùÙûÛüÜúÚůŮűŰ,% |
| 58 |
vV,% |
| 59 |
wW,% |
| 60 |
xX,% |
| 61 |
yYýÝÿŸ,% |
| 62 |
zZ,% |
| 63 |
žŽ,% |
| 64 |
źŹ,% |
| 65 |
żŻ,% |
| 66 |
^^Z,% Hungarian: cz:c^^Z, etc., see \_compoundcharshu in lang-data.opm |
| 67 |
0,1,2,3,4,5,6,7,8,9,',>% |
| 68 |
} |
| 69 |
|
| 70 |
\_doc ----------------------------- |
| 71 |
Characters to be ignored during sorting are declared in \`\_ignoredcharsgeneric`. |
| 72 |
These characters are ignored in the first pass |
| 73 |
without additional condition. All characters are taken into account in |
| 74 |
the second pass: ASCII characters with code $\string<65$ are sorted first if they |
| 75 |
are not mentioned in the `\_sortingdata...` macro. |
| 76 |
Others not mentioned characters have undefined behavior during sorting. |
| 77 |
\_cod ----------------------------- |
| 78 |
|
| 79 |
\_def \_ignoredcharsgeneric {.,;?!:'"()[]=+-} |
| 80 |
|
| 81 |
\_doc ----------------------------- |
| 82 |
Sorting is always processed by rules of a given language. The macros |
| 83 |
\`\_sortingdata``<lang-tag>`, \`\_ignoredchars``<lang-tag>` |
| 84 |
and \`\_compoundchars``<lang-tag>` declare these rules. |
| 85 |
The `<lang-tag>` is ISO code of the language: en, cs, de, pl, es for example. |
| 86 |
The English language is implemented here. Other languages are implemented |
| 87 |
in the `lang-data.opm` file (see section~\ref[langdata]). |
| 88 |
\_cod ----------------------------- |
| 89 |
|
| 90 |
\_let \_sortingdataen = \_sortingdatalatin % English alphabet is subset of Latin |
| 91 |
\_let \_ignoredcharsen = \_ignoredcharsgeneric |
| 92 |
\_def \_compoundcharsen {} % English doesn't have compound characters like DZ |
| 93 |
|
| 94 |
\_doc ----------------------------- |
| 95 |
The \^`\_compoundchars``<lang-tag>` can declare changes performed before |
| 96 |
sorting. For example Czech language declares: |
| 97 |
\begtt |
| 98 |
\_let \_sortingdatacs = \_sortingdatalatin % Czech alphabet is subset of Latin |
| 99 |
\_def \_compoundcharscs {ch:^^T Ch:^^U CH:^^V} |
| 100 |
\endtt |
| 101 |
It transforms two-letters `ch` to single character `^^T` because ch is |
| 102 |
treated as single compound character by Czech rules and CH is sorted between H and I. |
| 103 |
See \^`\_sortingdatalatin` where `^^T` is used. This declaration makes more |
| 104 |
transformations of Ch and CH too. The declarations of the form `x:y` in the |
| 105 |
\^`\_compoundchars``<lang-tag>` are separated by space. |
| 106 |
|
| 107 |
You can declare a transformation from single letter to more letters too. For |
| 108 |
example German rules sets ß equal to ss during sorting: |
| 109 |
\begtt |
| 110 |
\_let \_sortingdatade = \_sortingdatalatin % German alphabet is subset of Latin |
| 111 |
\_def \_compoundcharsde {ß:ss} |
| 112 |
\endtt |
| 113 |
If there are two words equal after first pass of sorting: Masse (mass) |
| 114 |
and Maße (measures) for example, then second pass must decide about the |
| 115 |
order. DIN 5007, section 6.1 says: ss must be before ß in this case. So, |
| 116 |
we want to switch off the \^`\_compoundchars` declaration for the second |
| 117 |
pass and use the order of s and ß given in `\_sortingdata`. This is |
| 118 |
possible if the \`\_xcompoundchars``<lang-tag>` is defined. |
| 119 |
It has precedence in the second pass of sorting. We declare for German: |
| 120 |
\begtt |
| 121 |
\_def \_xcompoundcharsde {} |
| 122 |
\endtt |
| 123 |
Geman rules mention alternative sorting for phone-books or similar lists of |
| 124 |
names. The letters ä ö ü should be interpreted as ae, oe and ue. So we get |
| 125 |
Mueller $\lt$ Müller $\lt$ Muff. If this rule is not taken into account, we get |
| 126 |
Mueller $\lt$ Muff $\lt$ Müller. The rule can be implemented by: |
| 127 |
\begtt |
| 128 |
\_def \_compoundcharsde {ß:ss Ä:AE Ö:OE Ü:UE ä:ae ö:oe ü:ue} |
| 129 |
\endtt |
| 130 |
Because u $\lt$ ü in `\_sortingdata` and because `\_xcompoundcharsde` is |
| 131 |
empty, we have Mueller $\lt$ Müller after second pass of the sorting. |
| 132 |
|
| 133 |
You can declare these macros for more languages if you wish to use |
| 134 |
`\makeindex` with sorting rules with respect to your language. |
| 135 |
Note: if you need to map compound characters to a character, don't use |
| 136 |
`^^I`, `^^J` or `^^M` because these characters have very specific category codes. |
| 137 |
|
| 138 |
If you created `\_sortingdata` etc. for your language, please, send them |
| 139 |
to me. I am ready to add them to the file `lang-data.opm` in a new \OpTeX/ release. |
| 140 |
See also section~\ref[langdata]. |
| 141 |
|
| 142 |
French sorting rule says: if the words are the same except for accents |
| 143 |
then accented letters are sorted after unaccented leters but |
| 144 |
read the words from their end in the second pass. |
| 145 |
For example correct sorting is: cote $\lt$ côte $\lt$ coté |
| 146 |
$\lt$ côté. This rule can be activated if the control sequence |
| 147 |
\`\_secondpass``<lang-tag>` is set to \^`\_reversewords`. |
| 148 |
For example, `lang-data.opm` declares `\_let\_secondpassfr=\_reversewords`. |
| 149 |
|
| 150 |
\bigskip |
| 151 |
Preparing to primary pass is performed by the \`\_setprimarysorting` macro |
| 152 |
implemented here. |
| 153 |
The <lang-tag> is saved to the \^`\_sortinglang` macro when sorting is |
| 154 |
initialized in \^`\_dosorting` (it is typically derived from current `\language` value). |
| 155 |
The \^`\_setprimarysorting` is called from \^`\_dosorting` macro and all |
| 156 |
processing of sorting is in a group. It sets actual |
| 157 |
\^`\_sortingdata`, \^`\_compoundchars` and \^`\_ignoredchars` if given language |
| 158 |
declares them. If not then warning will be printed using \`\_nold` macro |
| 159 |
and English data are used. The `\lccode` of all characters from |
| 160 |
\^`\_sortingdata` and \^`\_ignoredchars` are set. The sorted words will be converted |
| 161 |
using \^`\_compoundchars` followed by `\lowercase` before first pass is run. |
| 162 |
\_cod ----------------------------- |
| 163 |
|
| 164 |
\_def\_setprimarysorting {% |
| 165 |
\_ea\_let \_ea\_sortingdata \_csname _sortingdata\_sortinglang\_endcsname |
| 166 |
\_ea\_let \_ea\_compoundchars \_csname _compoundchars\_sortinglang\_endcsname |
| 167 |
\_ea\_let \_ea\_ignoredchars \_csname _ignoredchars\_sortinglang\_endcsname |
| 168 |
\_def\_nold{}% |
| 169 |
\_ifx \_sortingdata\_relax \_addto\_nold{ sortingdata}% |
| 170 |
\_let \_sortingdata = \_sortingdataen \_fi |
| 171 |
\_ifx \_compoundchars\_relax \_addto\_nold{ compoundchars}% |
| 172 |
\_let \_compoundchars = \_compoundcharsen \_fi |
| 173 |
\_ifx \_ignoredchars\_relax \_addto\_nold{ ignoredchars}% |
| 174 |
\_let \_ignoredchars = \_ignoredcharsen \_fi |
| 175 |
\_ifx\_nold\_empty\_else \_opwarning{Missing\_nold\_space for language (\_sortinglang)}\_fi |
| 176 |
\_ifx \_compoundchars\_empty \_else |
| 177 |
\_edef \_compoundchars {\_detokenize\_ea{\_compoundchars} }\_fi % all must be catcode 12 |
| 178 |
\_def \_act ##1{\_ifx##1\_relax \_else |
| 179 |
\_ifx##1,\_advance\_tmpnum by1 |
| 180 |
\_else \_lccode`##1=\_tmpnum \_fi |
| 181 |
\_ea\_act \_fi}% |
| 182 |
\_tmpnum=65 \_ea\_act \_sortingdata \_relax |
| 183 |
\_def \_act ##1{\_ifx##1\_relax \_else |
| 184 |
\_lccode`##1=`\^^I |
| 185 |
\_ea\_act \_fi}% |
| 186 |
\_ea\_act \_ignoredchars \_relax |
| 187 |
} |
| 188 |
|
| 189 |
\_doc ----------------------------- |
| 190 |
Preparing to secondary pass is implemented by the \`\_setsecondarysorting` macro. |
| 191 |
\_cod ----------------------------- |
| 192 |
|
| 193 |
\_def\_setsecondarysorting {% |
| 194 |
\_def \_act ##1{\_ifx##1\_relax \_else |
| 195 |
\_ifx##1,\_else \_advance\_tmpnum by1 \_lccode`##1=\_tmpnum \_fi |
| 196 |
\_ea\_act \_fi}% |
| 197 |
\_tmpnum=64 \_ea\_act \_sortingdata \_relax |
| 198 |
} |
| 199 |
|
| 200 |
\_doc ----------------------------- |
| 201 |
Strings to be sorted are prepared in `\,<string>` control sequences |
| 202 |
(to save `\TeX` memory). |
| 203 |
The \`\_preparesorting` `\,<string>` converts `<string>` to `\_tmpb` |
| 204 |
with respect to the data initialized in \^`\_setprimarysorting` or |
| 205 |
\^`\_setsecondarysorting`.\nl |
| 206 |
The part of the string after `^^^` is ignored (you can have the same |
| 207 |
sorting key for different things) and |
| 208 |
the compoud characters are converted by the \`\_docompound` macro. |
| 209 |
\_cod ----------------------------- |
| 210 |
|
| 211 |
\_def \_preparesorting #1{% |
| 212 |
\_edef \_tmpb {\_ea\_ignoreit\_csstring #1}% \,<string> -> <string> |
| 213 |
\_edef\_tmpb{\_ea \_stripfromcaret \_tmpb ^^^\_fin}% <string>^^^<ignore> -> <string> |
| 214 |
\_ea \_docompound \_compoundchars \_relax:{} % replace compound characters |
| 215 |
\_lowercase \_ea{\_ea\_def \_ea\_tmpb \_ea{\_tmpb}}% convert in respect to \_sortingdata |
| 216 |
\_ea\_replstring \_ea\_tmpb \_ea{\_csstring\^^I}{}% remove ignored characters |
| 217 |
} |
| 218 |
\_def \_docompound #1:#2 {% |
| 219 |
\_ifx\_relax#1\_else \_replstring\_tmpb {#1}{#2}\_ea\_docompound \_fi |
| 220 |
} |
| 221 |
\_def\_stripfromcaret #1^^^#2\_fin{#1} |
| 222 |
|
| 223 |
\_doc ----------------------------- |
| 224 |
Macro \`\_isAleB` `\,<string1> \,<string2>` returns the result of comparison |
| 225 |
of given two strings to \`\_ifAleB` control sequence. Usage: |
| 226 |
`\_isAleB \,<string1> \,<string2> \_ifAleB ... \_else ... \_fi` |
| 227 |
The converted strings (in respect of the data prepared for first pass) |
| 228 |
must be saved as values of `\,<string1>` and `\,<string2>` macros. |
| 229 |
The reason is speed: we don't want to convert them repeatedly in each |
| 230 |
comparison. |
| 231 |
\nl |
| 232 |
The macro |
| 233 |
\`\_testAleB` `<converted-string1>&\_relax<converted-string2>&\_relax \,<string1>\,<string2>`\nl |
| 234 |
does the real work. It reads the first character from both converted strings, compares them |
| 235 |
and if it is equal then calls itself recursively else gives the result. |
| 236 |
\_cod ----------------------------- |
| 237 |
|
| 238 |
\_newifi \_ifAleB |
| 239 |
|
| 240 |
\_def\_isAleB #1#2{% |
| 241 |
\_edef\_tmpb {#1&\_relax#2&\_relax}% |
| 242 |
\_ea \_testAleB \_tmpb #1#2% |
| 243 |
} |
| 244 |
\_def\_testAleB #1#2\_relax #3#4\_relax #5#6{% |
| 245 |
\_if #1#3\_if #1&\_testAleBsecondary #5#6% goto to the second pass:: |
| 246 |
\_else \_testAleB #2\_relax #4\_relax #5#6% |
| 247 |
\_fi |
| 248 |
\_else \_ifnum `#1<`#3 \_AleBtrue \_else \_AleBfalse \_fi |
| 249 |
\_fi |
| 250 |
} |
| 251 |
|
| 252 |
\_doc ----------------------------- |
| 253 |
The \`\_testAleBsecondary` `\,<string1> \,<string2>` is run if the |
| 254 |
words are equal in the primary pass. It runs \^`\_setsecondarysorting` if it |
| 255 |
was not initialized already. Then prepares compared words to `\_tmpa` and |
| 256 |
`\_tmpb` and corrects them by \^`\_prepsecondpass` if needed. |
| 257 |
Finally, the test is recursively done by the macro \`\_testAleBsecondaryX` |
| 258 |
`<converted-string1>0\_relax<converted-string2>1\_relax` |
| 259 |
\_cod ----------------------------- |
| 260 |
|
| 261 |
|
| 262 |
\_def\_testAleBsecondary#1#2{% |
| 263 |
\_setsecondarysorting \_let\_setsecondarysorting=\_relax |
| 264 |
\_preparesorting#1\_let\_tmpa=\_tmpb \_preparesorting#2% |
| 265 |
\_prepsecondpass |
| 266 |
\_edef\_tmpb{\_tmpa0\_relax\_tmpb1\_relax}% |
| 267 |
\_ea\_testAleBsecondaryX \_tmpb |
| 268 |
} |
| 269 |
\_def\_testAleBsecondaryX #1#2\_relax #3#4\_relax {% |
| 270 |
\_if #1#3\_testAleBsecondaryX #2\_relax #4\_relax |
| 271 |
\_else \_ifnum `#1<`#3 \_AleBtrue \_else \_AleBfalse \_fi |
| 272 |
\_fi |
| 273 |
} |
| 274 |
|
| 275 |
\_doc ----------------------------- |
| 276 |
Merge sort is very effectively implemented by \TeX/ macros. The following |
| 277 |
code is created by my son Miroslav. |
| 278 |
The \`\_mergesort` macro expects that all items in `\_iilist` are separated |
| 279 |
by a comma when it starts. It ends with sorted items in `\_iilist` without commas. |
| 280 |
So \^`\_dosorting` macro must prepare commas between items. |
| 281 |
\_cod ----------------------------- |
| 282 |
|
| 283 |
\_def\_mergesort #1#2,#3{% by Miroslav Olsak |
| 284 |
\_ifx,#1% % prazdna-skupina,neco, (#2=neco #3=pokracovani) |
| 285 |
\_toksapp\_tmptoks{#2,}% % dvojice skupin vyresena |
| 286 |
\_sortreturn{\_fif\_mergesort#3}% % \mergesort pokracovani |
| 287 |
\_fi |
| 288 |
\_ifx,#3% % neco,prazna-skupina, (#1#2=neco #3=,) |
| 289 |
\_toksapp\_tmptoks{#1#2,}% % dvojice skupin vyresena |
| 290 |
\_sortreturn{\_fif\_mergesort}% % \mergesort dalsi |
| 291 |
\_fi |
| 292 |
\_ifx\_fin#3% % neco,konec (#1#2=neco) |
| 293 |
\_if;\_the\_tmptoks;% % neco=kompletni setrideny seznam |
| 294 |
\_tmptoks{#1#2}% |
| 295 |
\_sortreturn{\_fif\_fif\_gobbletoend}% % koncim |
| 296 |
\_else % neco=posledni skupina nebo \end |
| 297 |
\_sortreturn{\_fif\_fif % spojim \indexbuffer+necoa cele znova |
| 298 |
\_tmptoks\_ea{\_ea}\_ea\_mergesort\_the\_tmptoks#1#2,#3}% |
| 299 |
\_fi\_fi % zatriduji: p1+neco1,p2+neco2, (#1#2=p1+neco1 #3=p2) |
| 300 |
\_isAleB #1#3\_ifAleB % p1<p2 |
| 301 |
\_toksapp\_tmptoks{#1}% % p1 do bufferu |
| 302 |
\_sortreturn{\_fif\_mergesort#2,#3}% % \mergesort neco1,p2+neco2, |
| 303 |
\_else % p1>p2 |
| 304 |
\_toksapp\_tmptoks{#3}% % p2 do bufferu |
| 305 |
\_sortreturn{\_fif\_mergesort#1#2,}% % \mergesort p1+neco1,neco2, |
| 306 |
\_fi |
| 307 |
\_relax % zarazka, na ktere se zastavi \sortreturn |
| 308 |
} |
| 309 |
\_def\_sortreturn#1#2\_fi\_relax{#1} \_def\_fif{\_fi} |
| 310 |
\_def\_gobbletoend #1\_fin{} |
| 311 |
|
| 312 |
\_doc ----------------------------- |
| 313 |
The \`\_dosorting` `\list` macro redefines `\list` as sorted `\list`. |
| 314 |
The `\list` have to include control sequences in the form `\<c><string>`. |
| 315 |
These control sequences will be sorted with respect to <strings> without |
| 316 |
change of meanings of these control sequences. Their meanings are |
| 317 |
irrelevant when sorting. The first character <c> in `\<c><string>` should |
| 318 |
be whatever. It does not influence the sorting. \OpTeX/ uses comma at |
| 319 |
this place for sorting indexes: `\,<word1> \,<word2> \,<word3> ...`. |
| 320 |
|
| 321 |
The current language (chosen for hyphenation patterns) is used for |
| 322 |
sorting data. If the macro \`\_sortinglang` |
| 323 |
is defined as `<lang-tag>` (for example `\def\_sortinglang{de}` for German) |
| 324 |
then this has precedence and current language is not used. |
| 325 |
Moreover, if you specify \`\_asciisortingtrue` then ASCII |
| 326 |
sorting will be processed and all language sorting data will be ignored. |
| 327 |
\_cod ----------------------------- |
| 328 |
|
| 329 |
\_newifi \_ifasciisorting \_asciisortingfalse |
| 330 |
\_def\_dosorting #1{% |
| 331 |
\_begingroup |
| 332 |
\_ifasciisorting \_def\_sortinglang{ASCII}\_fi |
| 333 |
\_ifx\_sortinglang\_undefined \_edef\_sortinglang{\_cs{_lan:\_the\_language}}\_fi |
| 334 |
\_sortmessage{OpTeX: Sorting \_string#1 (\_sortinglang) ...^^J}% |
| 335 |
\_ismacro\_sortinglang{ASCII}\_iftrue |
| 336 |
\_def \_preparesorting##1{\_edef\_tmpb{\_ea\_ignoreit\_csstring##1}}% |
| 337 |
\_let \_setsecondarysorting=\_relax |
| 338 |
\_else |
| 339 |
\_setprimarysorting |
| 340 |
\_fi |
| 341 |
\_def \_act##1{\_preparesorting ##1\_edef##1{\_tmpb}}% |
| 342 |
\_ea\_xargs \_ea\_act #1;% \_preparesorting for first pass of sorting applied |
| 343 |
\_ifcsname _xcompoundchars\_sortinglang\_endcsname |
| 344 |
\_ea\_let \_ea\_compoundchars \_csname _xcompoundchars\_sortinglang\_endcsname |
| 345 |
\_fi % \_compoundchars can differ in the second pass of sorting |
| 346 |
\_csname _secondpass\_sortinglang \_endcsname % activates \_reversewords if needed |
| 347 |
\_def \_act##1{\_toksapp\_tmptoks{##1,}}% |
| 348 |
\_tmptoks{}% |
| 349 |
\_edef #1{\_ea}\_ea\_xargs \_ea\_act #1;% commas between items added, mergesort initialized |
| 350 |
\_tmptoks\_ea{\_ea}\_ea\_mergesort \_the\_tmptoks\_fin,\_fin |
| 351 |
\_ea\_endgroup |
| 352 |
\_ea\_def\_ea#1\_ea{\_the\_tmptoks}% |
| 353 |
} |
| 354 |
\_let\_sortmessage=\_message % User can do \let\_sortmessage=\_ignoreit, for example |
| 355 |
|
| 356 |
\_doc ----------------------------- |
| 357 |
French rules needs reverse reading the words in the second pass. |
| 358 |
The \`\_reversewords` is activated in this case and it adds |
| 359 |
new job to the macro \`\_prepsecondpass`: it reverses the letters in |
| 360 |
the compared words (saved in `\_tmpa` and `\_tmpb`) by the expandable |
| 361 |
\`\_sortrevers` macro. The \^`\_prepsecondpass` macro is used in the |
| 362 |
\^`\_testAleBsecondary` and it is empty by default. |
| 363 |
\_cod ----------------------------- |
| 364 |
|
| 365 |
\_def\_prepsecondpass{} |
| 366 |
\_def\_reversewords{% |
| 367 |
\_addto\_prepsecondpass{\_edef\_tmpa{\_ea\_sortrevers\_tmpa\_relax}% |
| 368 |
\_edef\_tmpb{\_ea\_sortrevers\_tmpb\_relax}}% |
| 369 |
} |
| 370 |
\_def\_sortrevers #1#2\_relax{\_ifx^#2^#1\_else \_sortrevers#2\_relax #1\_fi} |
| 371 |
|
| 372 |
\_doc ----------------------------- |
| 373 |
The \`\makeindex` prints the index. First, it sorts the `\_iilist` |
| 374 |
second, it prints the sorted `\_iilist`, each item is printed |
| 375 |
using \^`\_printindexitem`.\nl |
| 376 |
We set `\leftskip=\iindent` and we suppose that each index entry |
| 377 |
starts by `\noindent\hskip-\iindent` (see the macro \~`\_printii`). |
| 378 |
Then the next lines of the same index |
| 379 |
entry (if the page list is broken to more pages) is indented by |
| 380 |
`\leftskip=\iindent`. |
| 381 |
\_cod ----------------------------- |
| 382 |
|
| 383 |
\_def\_makeindex{\_par |
| 384 |
\_ifx\_iilist\_empty \_opwarning{index data-buffer is empty. TeX me again}% |
| 385 |
\_incr\_unresolvedrefs |
| 386 |
\_else |
| 387 |
\_dosorting \_iilist % sorting \_iilist |
| 388 |
\_bgroup |
| 389 |
\_rightskip=0pt plus1fil \_exhyphenpenalty=10000 \_leftskip=\_iindent |
| 390 |
\_ea\_xargs \_ea\_printindexitem \_iilist ;\_par |
| 391 |
\_egroup |
| 392 |
\_fi |
| 393 |
} |
| 394 |
\_public \makeindex ; |
| 395 |
|
| 396 |
\_doc ----------------------------- |
| 397 |
The \`\_printindexitem` `\,<word>` prints one item to the index. |
| 398 |
If `\_,<word>` is defined then this is used instead real <word> |
| 399 |
(this exception is declared by `\iis` macro). Else <word> is printed by |
| 400 |
\^`\_printii`. Finally, \^`\_printiipages` prints the value of `\,<word>`, |
| 401 |
i.e. the list of pages. |
| 402 |
\_cod ----------------------------- |
| 403 |
|
| 404 |
\_def\_printindexitem #1{% |
| 405 |
\_ifcsname _\_csstring #1\_endcsname |
| 406 |
\_ea\_ea\_ea \_printii \_csname _\_csstring #1\_endcsname &% |
| 407 |
\_else |
| 408 |
\_ea\_ea\_ea\_printii \_ea\_ignoreit \_csstring #1&% |
| 409 |
\_fi |
| 410 |
\_ea\_printiipages #1& |
| 411 |
} |
| 412 |
|
| 413 |
\_doc ----------------------------- |
| 414 |
\`\_printii` `<word>&` does more intelligent work because we are working with |
| 415 |
words in the form `<main-word>/<sub-word>/<sub-sub-word>`. |
| 416 |
The \^`\everyii` tokens register is applied before `\noindent`. User can |
| 417 |
declare something special here. |
| 418 |
|
| 419 |
The \`\_newiiletter``{<letter>}{<word>}` macro is empty by default. It is invoked |
| 420 |
if first letter of index entry is changed. You can declare a design between |
| 421 |
index entries here. You can try, for example: |
| 422 |
\begtt |
| 423 |
\def\_newiiletter#1#2{% |
| 424 |
\bigskip \hbox{\setfontsize{at15pt}\bf #1}\nobreak\medskip} |
| 425 |
\endtt |
| 426 |
\`\_definefirstii` `<word>&` macro defines \`\_firstii` which is used as the |
| 427 |
<letter> parameter of the macro \^`\_newiiletter` and for testing if the |
| 428 |
\"first letter" of the index entry was changed. |
| 429 |
The `\uppercase` of the real first letter is used by default here. |
| 430 |
You can re-implement \^`\_definefirstii` if you want. For example, |
| 431 |
you want to ignore accents above letters for index sub-headers: |
| 432 |
\begtt |
| 433 |
\def\_definefirstii#1#2&{% |
| 434 |
\uppercase{\bgroup \iicodes \uppercase{\egroup\def\_firstii{#1}}}} |
| 435 |
\def\iicodes{} |
| 436 |
\def\setiicodes #1#2,{\_ifx^#1^\_else |
| 437 |
\foreach #2\do{\_addto\iicodes{\uccode`##1=`#1}} |
| 438 |
\_ea\setiicodes \_fi |
| 439 |
} |
| 440 |
\setiicodes AÀÂÄÁ,ĆČ,DĎ,EÈÉËÊĚ,IÍÏÎ,LĹĽ,OÖÓÔ,RŔ,ŚŠ,TŤ,UÙÛÜÚŮŰ,YÝŸ,{}, |
| 441 |
\endtt |
| 442 |
If the first character of the <word> is \string< or > then it isn't printed. |
| 443 |
It can be used as first character of the index entry in order to put the entry |
| 444 |
to a group entries sorted \string<before or >after whole normal entries. |
| 445 |
\_cod ----------------------------- |
| 446 |
|
| 447 |
\_def\_printii #1&{\_definefirstii #1&% |
| 448 |
\_ifx\_firstii\_lastii\_else |
| 449 |
\_ea\_newiiletter\_ea{\_firstii}{#1}\_let\_lastii=\_firstii\_fi |
| 450 |
\_gdef\_currii{#1}\_the\_everyii\_noindent |
| 451 |
\_hskip-\_iindent \_ignorespaces \_isnextchar<{\_ea\_printiiA\_ignoreit}% |
| 452 |
{\_isnextchar>{\_ea\_printiiA\_ignoreit}{\_printiiA}}#1//} |
| 453 |
\_def\_printiiA #1/{\_if^#1^\_let\_previi=\_currii \_else |
| 454 |
\_ea\_scanprevii\_previi/&\_edef\_tmpb{\_detokenize{#1}}% |
| 455 |
\_ifx\_tmpa\_tmpb \_iiemdash \_else#1 \_gdef\_previi{}\_fi |
| 456 |
\_ea\_printiiA\_fi |
| 457 |
} |
| 458 |
\_def\_definefirstii #1#2&{\_uppercase{\_def\_firstii{#1}}} |
| 459 |
\_def\_iiemdash{\_kern.1em---\_space} |
| 460 |
\_def\_lastii{} |
| 461 |
\_def\_newiiletter#1#2{} |
| 462 |
|
| 463 |
\_def\_scanprevii#1/#2&{\_def\_previi{#2}\_edef\_tmpa{\_detokenize{#1}}} |
| 464 |
\_def\_previi{} % previous index item |
| 465 |
|
| 466 |
\_doc ----------------------------- |
| 467 |
\`\_printiipages` `<pglist>&` gets `<pglist>` in the form |
| 468 |
`<pg>:<type>,<pg>:<type>,...<pg>:<type>` and it converts them to |
| 469 |
`<pg>, <pg>, <from>--<to>, <pg>` etc. The same pages must be printed only once |
| 470 |
and continuous consequences of pages must be compressed to the form <from>-<to>. |
| 471 |
Moreover, the consequence is continuous only if all pages have the same <type>. |
| 472 |
Empty <type> is most common, pages with `b` <type> must be printed as bold |
| 473 |
and with `i` `<type>` as italics. |
| 474 |
Moreover, the `<pg>` mentioned here are <gpageno>, but we have to print |
| 475 |
<pageno>. The following macros solve these tasks. |
| 476 |
\_cod ----------------------------- |
| 477 |
|
| 478 |
\_def\_printiipages#1&{\_let\_pgtype=\_undefined \_tmpnum=0 \_printpages #1,:,\_par} |
| 479 |
\_def\_printpages#1:#2,{% state automaton for compriming pages |
| 480 |
\_ifx,#1,\_uselastpgnum |
| 481 |
\_else \_def\_tmpa{#2}% |
| 482 |
\_ifx\_pgtype\_tmpa \_else |
| 483 |
\_let\_pgtype=\_tmpa |
| 484 |
\_uselastpgnum \_usepgcomma \_pgprint#1:{#2}% |
| 485 |
\_tmpnum=#1 \_returnfi \_fi |
| 486 |
\_ifnum\_tmpnum=#1 \_returnfi \_fi |
| 487 |
\_advance\_tmpnum by1 |
| 488 |
\_ifnum\_tmpnum=#1 \_ifx\_lastpgnum\_undefined \_usepgdash\_fi |
| 489 |
\_edef\_lastpgnum{\_the\_tmpnum:{\_pgtype}}% |
| 490 |
\_returnfi \_fi |
| 491 |
\_uselastpgnum \_usepgcomma \_pgprint#1:{#2}% |
| 492 |
\_tmpnum=#1 |
| 493 |
\_relax |
| 494 |
\_ea\_printpages \_fi |
| 495 |
} |
| 496 |
\_def\_returnfi #1\_relax{\_fi} |
| 497 |
\_def\_uselastpgnum{\_ifx\_lastpgnum\_undefined |
| 498 |
\_else \_ea\_pgprint\_lastpgnum \_let\_lastpgnum=\_undefined \_fi |
| 499 |
} |
| 500 |
\_def\_usepgcomma{\_ifnum\_tmpnum>0, \_fi} % comma+space between page numbers |
| 501 |
\_def\_usepgdash{\_hbox{--}} % dash in the <from>--<to> form |
| 502 |
|
| 503 |
\_doc ----------------------------- |
| 504 |
You can re-define \`\_pgprint` `<gpageno>:{<iitype>}` |
| 505 |
if you need to implement more <iitypes>. |
| 506 |
\_cod ----------------------------- |
| 507 |
|
| 508 |
\_def\_pgprint #1:#2{% |
| 509 |
\_ifx ,#2,\_pgprintA{#1}\_returnfi \_fi |
| 510 |
\_ifx b#2{\_bf \_pgprintA{#1}}\_returnfi \_fi |
| 511 |
\_ifx i#2{\_it \_pgprintA{#1}}\_returnfi \_fi |
| 512 |
\_ifx u#2\_pgu{\_pgprintA{#1}}\_returnfi \_fi |
| 513 |
\_pgprintA{#1}\_relax |
| 514 |
} |
| 515 |
\_def\_pgprintA #1{\_ilink[pg:#1]{\_cs{_pgi:#1}}} % \ilink[pg:<gpageno>]{<pageno>} |
| 516 |
\_def\_pgu#1{\_leavevmode\_vtop{\_hbox{#1}\kern.3ex\_hrule}} |
| 517 |
|
| 518 |
\_doc ----------------------------- |
| 519 |
The \`\iindex``{<word>}` puts one <word> to the index. It writes |
| 520 |
\^`\_Xindex``{<word>}{<iitype>}` to the `.ref` file. |
| 521 |
All other variants of indexing macros expand internally to `\iindex`. |
| 522 |
\_cod ----------------------------- |
| 523 |
|
| 524 |
\_def\_iindex#1{\_isempty{#1}\_iffalse |
| 525 |
\_openref{\_def~{ }\_ewref\_Xindex{{#1}{\_iitypesaved}}}\_fi} |
| 526 |
\_public \iindex ; |
| 527 |
|
| 528 |
\_doc ----------------------------- |
| 529 |
The \`\_Xindex``{<word>}{<iitype>}` stores `\,<word>` to the `\_iilist` if |
| 530 |
there is the first occurrence of the <word>. The list of pages where `<word>` |
| 531 |
occurs, is the value of the macro `\,<word>`, so the `<gpageno>:<iitype>` |
| 532 |
is appended to this list. |
| 533 |
Moreover, we need a mapping from <gpageno> to `<pageno>`, because we print |
| 534 |
`<pageno>` in the index, but hyperlinks are implemented by `<gpageno>`. |
| 535 |
So, the macro `\_pgi:<gpageno>` is defined as `<pageno>`. |
| 536 |
\_cod ----------------------------- |
| 537 |
|
| 538 |
\_def \_iilist {} |
| 539 |
\_def \_Xindex #1#2{\_ea\_XindexA \_csname ,#1\_ea\_endcsname \_currpage {#2}} |
| 540 |
\_def \_XindexA #1#2#3#4{% #1=\,<word> #2=<gpageno> #3=<pageno> #4=<iitype> |
| 541 |
\_ifx#1\_relax \_global\_addto \_iilist {#1}% |
| 542 |
\_gdef #1{#2:#4}% |
| 543 |
\_else \_global\_addto #1{,#2:#4}% |
| 544 |
\_fi |
| 545 |
\_sxdef{_pgi:#2}{#3}% |
| 546 |
} |
| 547 |
|
| 548 |
\_doc ----------------------------- |
| 549 |
The implementation of macros \`\ii`, \`\iid`, \`\iis` follows. |
| 550 |
Note that `\ii` works in the horizontal mode in order to the `\write` whatsit |
| 551 |
is not broken from the following word. If you need to keep vertical mode, |
| 552 |
use \^`\iindex``{<word>}` directly. |
| 553 |
\nl |
| 554 |
The \`\iitype` `{<type>}` saves the `<type>` to the \`\_iitypesaved` macro. It is |
| 555 |
used in the \^`\iindex` macro. |
| 556 |
\_cod ----------------------------- |
| 557 |
|
| 558 |
\_def\_ii #1 {\_leavevmode\_def\_tmp{#1}\_iiA #1,,\_def\_iitypesaved{}} |
| 559 |
|
| 560 |
\_def\_iiA #1,{\_if$#1$\_else\_def\_tmpa{#1}% |
| 561 |
\_ifx\_tmpa\_iiatsign \_ea\_iiB\_tmp,,\_else\_iindex{#1}\_fi |
| 562 |
\_ea\_iiA\_fi} |
| 563 |
\_def\_iiatsign{@} |
| 564 |
|
| 565 |
\_def\_iiB #1,{\_if$#1$\_else \_iiC#1/\_relax \_ea\_iiB\_fi} |
| 566 |
\_def\_iiC #1/#2\_relax{\_if$#2$\_else\_iindex{#2#1}\_fi} |
| 567 |
|
| 568 |
\_def\_iid #1 {\_leavevmode\_iindex{#1}\_def\_iitypesaved{}% |
| 569 |
\_isnextchar<{\_ignoreit}{\_isnextchar>{\_ignoreit}{}}#1\_futurelet\_tmp\_iiD} |
| 570 |
\_def\_iiD{\_ifx\_tmp,\_else\_ifx\_tmp.\_else\_space\_fi\_fi} |
| 571 |
|
| 572 |
\_def\_iis #1 #2{{\_def~{ }\_global\_sdef{_,#1}{#2}}\_ignorespaces} |
| 573 |
|
| 574 |
\_def\_iitypesaved{} |
| 575 |
\_def\_iitype #1{\_def\_iitypesaved{#1}\_ignorespaces} |
| 576 |
|
| 577 |
\_public \ii \iid \iis \iitype ; |
| 578 |
|
| 579 |
\_endcode % ------------------------------------- |
| 580 |
|
| 581 |
2024-12-01 < first, > last character for sorting, unprinted by \makindex |
| 582 |
2024-11-24 | set as first in sortingdata: it can be used as item separator |
| 583 |
2024-10-31 \mergesort time-optimized using \toksapp instead \addto |
| 584 |
2023-06-02 \_stripfromcaret introduced |
| 585 |
2023-03-12 \_definefirstii introduced |
| 586 |
2022-06-28 \_reversewords for French sorting introduced |
| 587 |
2022-06-28 \_sortingdatalatin covers more languages |
| 588 |
2022-06-28 \_xcompoundchars introduced, comments upgraded (German sorting mentioned) |
| 589 |
2022-03-18 \iid didn't ignore space, bug fixed |
| 590 |
2022-02-19 \_sotringdataen etc. moved to lang-data.opm file |
| 591 |
2021-02-15 \_expandafter -> \_ea |
| 592 |
2021-02-01 secondary sorting: start from code 65 |
| 593 |
2020-04-21 \isempty \iffalse ... \fi added to \iindex |
| 594 |
2020-03-26 introduced |