1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! The names a kept user definition is written under in the style file and
//! in the recomposed LaTeX: each is renamed under a prefix of its own
//! (`\hello` becomes `\flmUDhello`), so that it never collides with a command
//! that LaTeX, a LaTeX package or the style file relies on.
//!
//! A kept user definition
//! ([`kept`](crate::stdlib::features::userdefs::kept)) is a user definition
//! whose use sites a plan left in the tree (the `flm.userdefs.kept`
//! alternative); the `latex-recomposed` backend
//! writes the definition into the style file and its use sites as calls.
//! Written under the author's own name, such a definition would be made
//! unconditionally (`\long\def`, `\DeclareDocumentEnvironment`) and so
//! replace any definition that LaTeX or a package makes under that name. Some
//! of those replacements break the recomposed LaTeX itself (`\par`, `\label`,
//! the commands a character declaration in the same style file relies on) and
//! are very hard to debug. Rather than keep a list of dangerous names, the
//! library renames **every** definition ([`recomposed_name`]):
//!
//! - a macro whose name is ASCII letters only becomes
//! [`MACRO_PREFIX`]`<name>`: `\hello` is written `\flmUDhello`;
//! - a macro whose name holds anything else (a control symbol such as `~`, a
//! non-ASCII letter, a digit) becomes [`MACRO_ENCODED_PREFIX`] followed by
//! an encoding of the name in ASCII letters (see [`recomposed_name`]), since
//! a LaTeX control word may hold letters only: `\~` is written `\flmUXhE`;
//! - an environment becomes [`ENVIRONMENT_PREFIX`]`<name>`, its name kept
//! exactly as written regardless of the characters it holds:
//! `\begin{note}` is written `\begin{flmUEnote}`. An environment name is
//! only ever looked up by `\begin{…}` and `\end{…}` and never written as a
//! control word, so it needs no encoding.
//!
//! Macros and environments take different prefixes because LaTeX keeps them
//! in one name space: `\newenvironment{foo}` defines `\foo`, so a macro `foo`
//! and an environment `foo` written under one prefix would collide.
//!
//! # Per-mode definitions
//!
//! A document may declare one name twice, once for text mode and once for
//! mathematics mode, with different templates and even different arguments;
//! LaTeX has only one command of that name. These *per-mode definitions*
//! therefore take two names, and the recomposed LaTeX calls the one that the
//! mode of the use site requires. The rule: **the prefix of a
//! per-mode definition is the prefix above with the mode's letter inserted
//! after the `U`**, `T` for text mode and `M` for mathematics mode
//! ([`LatexDefinitionMode`]). So a document declaring `\vec` twice gets
//! `\flmUTDvec` and `\flmUMDvec`; a `\~` declared twice gets `\flmUTXhE` and
//! `\flmUMXhE`; an environment `note` declared twice gets `flmUTEnote` and
//! `flmUMEnote`. A name the document declares once keeps the plain prefix,
//! regardless of the modes it applies in.
//!
//! No two definitions can collide under this rule, because the letter after
//! `flmU` says which of five classes a name belongs to (`D`, `X` and `E` for a
//! name declared once, `T` and `M` for a per-mode definition) and the rest of
//! the name is the definition's own. A document macro named `Tvec` is
//! `\flmUDTvec`, never `\flmUTDvec`. The `latex-recomposed` backend chooses
//! between the two per-mode definitions, never LaTeX: an `\ifmmode` test written into the
//! output would be evaluated when a heading's title is *written to the table
//! of contents*, which is outside mathematics regardless of the title's
//! content, and would give the wrong answer there.
//!
//! # The `flmU` name space belongs to the document
//!
//! **Every name beginning with `flmU` is left to the definitions a document
//! declares, and nothing this library writes may take a name there.** Every
//! prefix above begins with [`USER_DEFINITION_PREFIX`], the four characters
//! `flmU`, and the rule is stated over those four characters rather than over
//! the prefixes: a name beginning with them may be the name of a definition
//! that some document declares (a document that declares a macro `blank` has
//! `\flmUDblank`), so a helper of this library written there would collide
//! with it. Because the rule covers the whole space, the per-mode prefixes,
//! and any prefix added later under `flmU`, fit in without changing it.
//!
//! The LaTeX family's own helpers take names of the form `\flm<Name>`
//! (`\flmHeading`, `\flmRef`, `\flmIfBlankTF`). One class of them belongs to
//! a feature: `\flmXN<kind>` is the command an xnote of the kind `<kind>` is
//! written as ([`xnotes::latex`](crate::stdlib::features::xnotes::latex)).
//! A feature that contributes a definition piece of its own must follow the
//! same rule. One name currently breaks it: the `href` feature's LaTeX hook
//! `\flmUrl` begins with `flmU`
//! ([`href::latex`](crate::stdlib::features::href::latex)). It cannot
//! collide with a recomposed name, because the letter after `flmU` in it
//! (`r`) is none of the five class letters (`D`, `X`, `E`, `T`, `M`).
//!
//! The style file also writes the declarations that the package chunks of
//! untechxt's Unicode-to-LaTeX table need
//! ([`chunk_pieces`](super::preamble::chunk_pieces)), whose commands and font
//! identifiers carry untechxt's own prefix, `UnxT`. That name space belongs to
//! untechxt: this library defines no name there and renames nothing the table
//! declares.
//!
//! # Where the names are used, and how to keep the author's own name
//!
//! The renaming applies to the definition in the style file and to every call
//! the recomposed LaTeX writes, in text and in mathematics alike, so the
//! definition keeps its form and only its name changes. The style file gives
//! the author's own name in the comment line above each definition.
//!
//! An author who *wants* LaTeX's own command replaced lists the name in the
//! render option
//! [`bare_names`](crate::stdlib::backends::latex_recomposed::LatexRecomposedRenderOptions::bare_names):
//! the definition is then written under the name as the document spelled it,
//! and the author is responsible for the consequences for the whole document.
//! Some names cannot be listed there and are refused with the Error
//! [`DefinitionNameRefused`](crate::stdlib::backends::latex_recomposed::DefinitionNameRefused)
//! ([`DefinitionNameRefusal`](crate::stdlib::backends::latex_recomposed::DefinitionNameRefusal)
//! lists the reasons), among them a name beginning with [`FAMILY_PREFIX`],
//! the only name space this library owns, and a name the document declares
//! once per mode, since one name cannot hold both per-mode definitions.
use LatexDefinitionKind;
/// The prefix `flm`, the only name space this library owns: every command
/// and environment that the definition pieces written in [`super::preamble`]
/// and the features' LaTeX hooks define begins with it. A name beginning with
/// it cannot be listed in the render option `bare_names` (see
/// [`is_family_name`]).
pub const FAMILY_PREFIX: &str = "flm";
/// The four characters every prefix of a recomposed user definition begins
/// with, `flmU`: the whole name space this library leaves to the definitions a
/// document declares (see the [module documentation](self)). Nothing this
/// library's own LaTeX defines may take a name beginning with it; the `href`
/// feature's LaTeX hook `\flmUrl` currently breaks this rule (see the
/// [module documentation](self#the-flmu-name-space-belongs-to-the-document)).
pub const USER_DEFINITION_PREFIX: &str = "flmU";
/// The prefix a recomposed macro whose name is ASCII letters only is
/// written under, `flmUD`: the user definition `\hello` becomes
/// `\flmUDhello`.
pub const MACRO_PREFIX: &str = "flmUD";
/// The prefix a recomposed macro whose name holds anything but ASCII
/// letters is written under, `flmUX`, followed by the letters-only encoding
/// of the name that [`recomposed_name`] describes: the user definition
/// `\~` becomes `\flmUXhE`.
pub const MACRO_ENCODED_PREFIX: &str = "flmUX";
/// The prefix a recomposed environment is written under, `flmUE`, followed
/// by the name exactly as the document spelled it: the user environment
/// `note` becomes `flmUEnote`, written `\begin{flmUEnote}`.
pub const ENVIRONMENT_PREFIX: &str = "flmUE";
/// Which of a name's definitions a recomposed name is wanted for.
///
/// A document may declare one name twice, once for text mode and once for
/// mathematics mode ([per-mode definitions](self#per-mode-definitions)); the
/// two are written under two names, and this value says which one is meant.
/// Every other definition (one the document declares once, regardless of the
/// parsing modes it applies in) is [`Single`](LatexDefinitionMode::Single).
/// How a definition of `kind` named `name` is written in `mode`: the prefix it
/// takes — one of the three prefixes above, with the mode's letter inserted
/// after the four characters [`USER_DEFINITION_PREFIX`] for a per-mode
/// definition (see the [module documentation](self)) — and whether the name
/// itself is written encoded as ASCII letters ([`encoded_name`]).
///
/// One function decides both because they are one classification: a
/// macro whose name is not ASCII letters only takes the encoded prefix *and*
/// has its name encoded, and nothing else does. Deciding it in two places
/// could let the two part company, which would give a plain prefix an encoded
/// name or the reverse.
/// The mode letter of a per-mode definition's name: the mode that `name` (a
/// recomposed macro or environment name without a backslash) is written for,
/// or `None` where it is not the name of a per-mode definition at all (the
/// name of a definition the document declares once, or a name outside the
/// `flmU` name space).
///
/// The letter right after [`USER_DEFINITION_PREFIX`] classifies every name in
/// that space. The classification is **closed**: the five letters `D`, `X`
/// and `E` (a definition the document declares once) and `T` and `M` (a
/// per-mode definition), and no other (see the [module
/// documentation](self#per-mode-definitions)). A later class of recomposed
/// name must add a letter of its own rather than reuse one of the five.
///
/// [`Single`](LatexDefinitionMode::Single) is never returned: a name under a
/// plain prefix is not a per-mode name, which is `None`.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::latex::names::{LatexDefinitionMode, mode_of_twin_name};
///
/// assert_eq!(mode_of_twin_name("flmUTDvec"), Some(LatexDefinitionMode::Text));
/// assert_eq!(mode_of_twin_name("flmUMEnote"), Some(LatexDefinitionMode::Mathematics));
/// assert_eq!(mode_of_twin_name("flmUDvec"), None);
/// // A document macro named `Tvec` is `flmUDTvec`, which is not a per-mode name.
/// assert_eq!(mode_of_twin_name("flmUDTvec"), None);
/// assert_eq!(mode_of_twin_name("emph"), None);
/// ```
/// Whether `name` (a macro or environment name without a backslash) is in
/// the name space this library's own LaTeX output owns, that is, whether it
/// begins with [`FAMILY_PREFIX`].
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::latex::names::is_family_name;
///
/// assert!(is_family_name("flmHeading"));
/// assert!(!is_family_name("emph"));
/// ```
/// One character of a name, encoded as ASCII letters: its code point in
/// uppercase hexadecimal without leading zeros, with the decimal digits `0`
/// to `9` written as the letters `a` to `j` and the letters `A` to `F` kept
/// as they are.
/// A whole name encoded as ASCII letters: each character through
/// [`encoded_char`], the characters joined by the letter `x`.
/// The name, without a backslash, under which a user definition of `kind`
/// named `name` is recomposed in `mode`. It is the only implementation of the
/// naming rule, used for the definition in the style file and for every call
/// the `latex-recomposed` output writes.
///
/// `mode` says which of a name's definitions this one is
/// ([`LatexDefinitionMode`]): [`Single`](LatexDefinitionMode::Single) for a
/// name the document declares once, and [`Text`](LatexDefinitionMode::Text) or
/// [`Mathematics`](LatexDefinitionMode::Mathematics) for one of the two
/// per-mode definitions of a name it declares once per mode.
///
/// `bare_names` is the render option
/// [`bare_names`](crate::stdlib::backends::latex_recomposed::LatexRecomposedRenderOptions::bare_names):
/// a name listed there is returned unchanged, so that the definition
/// replaces LaTeX's own command of that name. Two kinds of name are never
/// returned unchanged, regardless of the option: a name beginning with
/// [`FAMILY_PREFIX`], the only name space this library owns, and the name of
/// a per-mode definition, since the document's two definitions of it would
/// then be one LaTeX command. This function renames both; the backend reports
/// the refused entry of the option as an Error of its own
/// ([`DefinitionNameRefused`](crate::stdlib::backends::latex_recomposed::DefinitionNameRefused)).
///
/// Otherwise the name is the prefix of its kind followed by the name, with the
/// mode's letter inserted after [`USER_DEFINITION_PREFIX`] for a per-mode
/// definition (see the [module documentation](self#per-mode-definitions)):
///
/// - an environment is [`ENVIRONMENT_PREFIX`] and `name` exactly as written;
/// - a macro whose name is ASCII letters only is [`MACRO_PREFIX`] and `name`;
/// - any other macro is [`MACRO_ENCODED_PREFIX`] and the name encoded as ASCII
/// letters: each character's
/// code point in uppercase hexadecimal without leading zeros, with the
/// decimal digits `0`–`9` written as the letters `a`–`j` and the letters
/// `A`–`F` kept, the characters joined by the letter `x`. So `~` (U+007E,
/// hexadecimal `7E`) encodes as `hE`, and `foo2` encodes as `ggxgFxgFxdc`.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// use flm_core::stdlib::backends::latex::LatexDefinitionKind;
/// use flm_core::stdlib::backends::latex::names::{LatexDefinitionMode, recomposed_name};
///
/// let none = BTreeSet::new();
/// let one = LatexDefinitionMode::Single;
/// assert_eq!(recomposed_name(LatexDefinitionKind::Macro, "hello", one, &none), "flmUDhello");
/// assert_eq!(recomposed_name(LatexDefinitionKind::Macro, "~", one, &none), "flmUXhE");
/// assert_eq!(
/// recomposed_name(LatexDefinitionKind::Environment, "note", one, &none),
/// "flmUEnote"
/// );
///
/// // A name the document declares twice, once per parsing mode.
/// let text = LatexDefinitionMode::Text;
/// let math = LatexDefinitionMode::Mathematics;
/// assert_eq!(recomposed_name(LatexDefinitionKind::Macro, "vec", text, &none), "flmUTDvec");
/// assert_eq!(recomposed_name(LatexDefinitionKind::Macro, "vec", math, &none), "flmUMDvec");
///
/// let bare: BTreeSet<String> = [String::from("emph")].into_iter().collect();
/// assert_eq!(recomposed_name(LatexDefinitionKind::Macro, "emph", one, &bare), "emph");
/// ```