retex-engine 0.9.0

The TeX typesetting engine for retex
Documentation
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//! Error reporting and recovery (*TeX: The Program* §71–§98, §311–§320).
//!
//! Errors print `! message.`, the input context and help text exactly as
//! TeX does. Fatal errors end the run by unwinding to the top-level driver
//! with a [`JumpOut`] payload (TeX's `jump_out`). `std::panic::resume_unwind`
//! is used, so no panic hook runs and nothing is printed. This is the only
//! non-local exit in the engine.

use crate::Engine;
use crate::input::{Level, LineSource, TokKind};
use crate::print::Selector;

/// Interaction level (`interaction`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Interaction {
    /// `\batchmode`.
    Batch = 0,
    /// `\nonstopmode`.
    NonStop = 1,
    /// `\scrollmode`.
    Scroll = 2,
    /// `\errorstopmode`.
    ErrorStop = 3,
}

impl Interaction {
    /// The level with numeric value `n` (0–3).
    pub fn from_i32(n: i32) -> Interaction {
        match n {
            0 => Interaction::Batch,
            1 => Interaction::NonStop,
            2 => Interaction::Scroll,
            _ => Interaction::ErrorStop,
        }
    }
}

/// How bad things are (`history`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum History {
    /// No problems.
    Spotless,
    /// `begin_diagnostic` was called with warnings.
    WarningIssued,
    /// An error was reported.
    ErrorMessageIssued,
    /// The run was aborted.
    FatalErrorStop,
}

/// Payload of the unwind that ends a run (TeX's `jump_out`).
#[derive(Debug, Clone, Copy)]
pub struct JumpOut;

/// Error-handling state.
#[derive(Debug)]
pub struct ErrorState {
    /// The interaction level.
    pub interaction: Interaction,
    /// The run's history.
    pub history: History,
    /// Number of errors since the last paragraph end (`error_count`).
    pub error_count: i32,
    /// Help lines for the next error, top line first.
    pub help: Vec<&'static str>,
    /// Owned help lines (used instead of `help` when non-empty).
    pub help_owned: Vec<String>,
    /// Whether `\errhelp` should be shown instead of the help lines.
    pub use_err_help: bool,
    /// Whether deletions are allowed in error recovery.
    pub deletions_allowed: bool,
    /// Whether `\setbox` is allowed here.
    pub set_box_allowed: bool,
    /// `no_new_control_sequence`: lookups must not create new names.
    pub no_new_control_sequence: bool,
    /// The selector saved by `begin_diagnostic`.
    pub old_setting: Option<Selector>,
    /// Whether `file:line:error` style messages are requested.
    pub file_line_error: bool,
    /// Whether a `\par` should not be inserted (`OK_to_interrupt`).
    pub interrupt: bool,
}

impl Default for ErrorState {
    fn default() -> Self {
        ErrorState {
            interaction: Interaction::ErrorStop,
            history: History::FatalErrorStop,
            error_count: 0,
            help: Vec::new(),
            help_owned: Vec::new(),
            use_err_help: false,
            deletions_allowed: true,
            set_box_allowed: true,
            no_new_control_sequence: true,
            old_setting: None,
            file_line_error: false,
            interrupt: false,
        }
    }
}

impl Engine {
    /// Starts an error message (`print_err`).
    pub fn print_err(&mut self, msg: &str) {
        self.printer.begin_line();
        if !(self.err.file_line_error && self.print_file_line()) {
            self.printer.print_str("! ");
        }
        if self.diagnostics.is_some() {
            self.printer.capture = Some(Vec::new());
        }
        self.printer.print_str(msg);
    }

    /// Starts collecting a warning message (default output mode).
    pub fn diag_capture_start(&mut self) {
        if self.diagnostics.is_some() {
            self.printer.capture = Some(Vec::new());
        }
    }

    /// The innermost input file: name, line number, line text and the
    /// reading position in it.
    pub fn source_location(&self) -> Option<(String, u32, String, usize)> {
        let elc = self
            .eqtb
            .int(crate::primitives_gen::assign_int::ENDLINECHAR);
        self.input.stack.iter().rev().find_map(|l| match l {
            crate::input::Level::Text(t) if t.is_input_file => {
                let mut chars: &[u32] = &t.buf;
                if let Some((&last, rest)) = chars.split_last()
                    && i64::from(last) == i64::from(elc)
                {
                    chars = rest;
                }
                let text: String = chars.iter().filter_map(|&c| char::from_u32(c)).collect();
                Some((
                    String::from_utf8_lossy(&t.name).into_owned(),
                    t.line.max(0) as u32,
                    text,
                    t.loc.min(chars.len()),
                ))
            }
            _ => None,
        })
    }

    /// Records the captured warning text as a diagnostic located at `line`
    /// of the current file (or its current line).
    pub fn diag_capture_warning(&mut self, line: Option<u32>) {
        let Some(text) = self.printer.capture.take() else {
            return;
        };
        let message = String::from_utf8_lossy(&text).trim().replace('\n', " ");
        let mut d = crate::diag::Diagnostic::new(crate::diag::Severity::Warning, message);
        if let Some((file, cur, _, _)) = self.source_location() {
            d.file = Some(file);
            d.line = Some(line.unwrap_or(cur));
        }
        if let Some(list) = self.diagnostics.as_mut() {
            list.push(d);
        }
    }

    /// Records the error being reported (default output mode).
    fn record_error(&mut self) {
        let Some(text) = self.printer.capture.take() else {
            return;
        };
        let mut message = String::from_utf8_lossy(&text).trim().replace('\n', " ");
        if message == "Undefined control sequence" && self.cur_cs != crate::hash::NO_CS {
            let name = self.print_to_string(|e| e.print_cs(e.cur_cs));
            message = format!(
                "Undefined control sequence {}",
                String::from_utf8_lossy(&name).trim_end()
            );
        }
        let mut d = crate::diag::Diagnostic::new(crate::diag::Severity::Error, message);
        if let Some((file, line, source, column)) = self.source_location() {
            d.file = Some(file);
            d.line = Some(line);
            d.source = Some(source);
            d.column = Some(column);
        }
        d.help = if self.err.help_owned.is_empty() {
            self.err.help.iter().map(|s| (*s).to_owned()).collect()
        } else {
            self.err.help_owned.clone()
        };
        if let Some(list) = self.diagnostics.as_mut() {
            list.push(d);
        }
    }

    /// Starts an error message given as bytes.
    pub fn print_err_bytes(&mut self, msg: &[u8]) {
        self.print_err("");
        self.printer.print_bytes(msg);
    }

    /// Prints `file:line: ` for `-file-line-error`; returns `false` if no
    /// file is open.
    fn print_file_line(&mut self) -> bool {
        let found = self.input.stack.iter().rev().find_map(|l| match l {
            Level::Text(t) if t.is_input_file => Some((t.name.clone(), t.line)),
            _ => None,
        });
        match found {
            Some((name, line)) => {
                self.printer.print_bytes(&name);
                self.printer.print_char(':' as u32);
                self.printer.print_int(line.into());
                self.printer.print_str(": ");
                true
            }
            None => false,
        }
    }

    /// Sets the help lines for the next error (top line first).
    pub fn help(&mut self, lines: &[&'static str]) {
        self.err.help.clear();
        self.err.help.extend_from_slice(lines);
        self.err.help_owned.clear();
    }

    /// Sets owned help lines.
    pub fn help_owned(&mut self, lines: Vec<String>) {
        self.err.help.clear();
        self.err.help_owned = lines;
    }

    /// Completes an error message: prints the context and help, and
    /// interacts with the user if appropriate (`error`, §82).
    pub fn error(&mut self) {
        self.record_error();
        if self.err.history < History::ErrorMessageIssued {
            self.err.history = History::ErrorMessageIssued;
        }
        self.printer.print_char('.' as u32);
        self.show_context();
        if self.err.interaction == Interaction::ErrorStop {
            // No interactive terminal support: behave like scroll mode, but
            // a real terminal session would ask the user here.
            self.terminal_error_stop();
        }
        self.err.error_count += 1;
        if self.err.error_count == 100 {
            self.printer
                .print_nl("(That makes 100 errors; please try again.)");
            self.err.history = History::FatalErrorStop;
            self.jump_out();
        }
        // Put the help message on the transcript file.
        let sel = self.printer.selector;
        if self.err.interaction > Interaction::Batch {
            self.printer.selector = match sel {
                Selector::TermAndLog => Selector::LogOnly,
                Selector::TermOnly => Selector::NoPrint,
                s => s,
            };
        }
        if self.err.use_err_help {
            self.printer.print_ln();
            self.give_err_help();
        } else {
            let lines: Vec<String> = if self.err.help_owned.is_empty() {
                self.err.help.iter().map(|s| s.to_string()).collect()
            } else {
                self.err.help_owned.clone()
            };
            let empty = lines.is_empty();
            for l in lines {
                self.printer.print_nl(&l);
            }
            // LuaTeX does not add an empty line when there is no help and
            // the context line was wrapped at max_print_line.
            if empty {
                self.printer.begin_line();
            } else {
                self.printer.print_ln();
            }
        }
        if self.err.use_err_help {
            self.printer.print_ln();
        }
        self.err.help.clear();
        self.err.help_owned.clear();
        if self.err.interaction > Interaction::Batch {
            self.printer.selector = sel;
        }
        self.printer.print_ln();
    }

    /// Reads a line from the terminal, echoing it to the log
    /// (`term_input`, §71). End of input is fatal, as in TeX.
    pub fn term_input(&mut self) -> Vec<u32> {
        self.printer.update_terminal();
        let mut line = String::new();
        match std::io::stdin().read_line(&mut line) {
            Ok(0) | Err(_) => self.fatal_error("End of file on the terminal!"),
            Ok(_) => {}
        }
        let chars = crate::input::decode_line(line.trim_end_matches(['\n', '\r']).as_bytes());
        self.printer.term_offset = 0;
        let old = self.printer.selector;
        self.printer.selector = match old {
            Selector::TermAndLog => Selector::LogOnly,
            Selector::TermOnly => Selector::NoPrint,
            s => s,
        };
        for &c in &chars {
            self.printer.print_char(c);
        }
        self.printer.print_ln();
        self.printer.selector = old;
        chars
    }

    /// Prints a prompt and reads a line (`prompt_input`).
    pub fn prompt_input(&mut self, prompt: &str) -> Vec<u32> {
        self.printer.print_str(prompt);
        self.term_input()
    }

    /// Asks the user what to do about an error (§84–§93).
    fn terminal_error_stop(&mut self) {
        loop {
            if self.err.interaction != Interaction::ErrorStop {
                return;
            }
            self.clear_for_error_prompt();
            let line = self.prompt_input("? ");
            let Some(&c0) = line.first() else {
                return;
            };
            let c = char::from_u32(c0).unwrap_or(' ').to_ascii_uppercase();
            match c {
                '0'..='9' if self.err.deletions_allowed => {
                    let mut n = c as u32 - '0' as u32;
                    if let Some(&d) = line.get(1)
                        && (0x30..=0x39).contains(&d)
                    {
                        n = n * 10 + (d - 0x30);
                    }
                    let (s1, s2, s3, s4) = (
                        self.cur_tok,
                        self.cur_cmd,
                        self.cur_chr,
                        self.input.align_state,
                    );
                    self.input.align_state = 1_000_000;
                    for _ in 0..n {
                        self.get_token();
                    }
                    self.cur_tok = s1;
                    self.cur_cmd = s2;
                    self.cur_chr = s3;
                    self.input.align_state = s4;
                    self.help(&[
                        "I have just deleted some text, as you asked.",
                        "You can now delete more, or insert, or whatever.",
                    ]);
                    self.show_context();
                    continue;
                }
                'E' => {
                    let file = self.input.stack.iter().rev().find_map(|l| match l {
                        Level::Text(t) if t.is_input_file => Some((t.name.clone(), t.line)),
                        _ => None,
                    });
                    if let Some((name, line)) = file {
                        self.printer.print_nl("You want to edit file ");
                        self.printer.print_bytes(&name);
                        self.printer.print_str(" at line ");
                        self.printer.print_int(line.into());
                        self.err.interaction = Interaction::Scroll;
                        self.jump_out();
                    }
                }
                'H' => {
                    if self.err.use_err_help {
                        self.give_err_help();
                        self.err.use_err_help = false;
                    } else {
                        let lines: Vec<String> = if !self.err.help_owned.is_empty() {
                            self.err.help_owned.clone()
                        } else if !self.err.help.is_empty() {
                            self.err.help.iter().map(|s| s.to_string()).collect()
                        } else {
                            vec![
                                "Sorry, I don't know how to help in this situation.".into(),
                                "Maybe you should try asking a human?".into(),
                            ]
                        };
                        for l in lines {
                            self.printer.print_str(&l);
                            self.printer.print_ln();
                        }
                    }
                    self.help(&[
                        "Sorry, I already gave what help I could...",
                        "Maybe you should try asking a human?",
                        "An error might have occurred before I noticed any problems.",
                        "``If all else fails, read the instructions.''",
                    ]);
                    continue;
                }
                'I' => {
                    // Insert material from the terminal.
                    let text: Vec<u32> = if line.len() > 1 {
                        let mut v = vec![' ' as u32];
                        v.extend_from_slice(&line[1..]);
                        v
                    } else {
                        self.prompt_input("insert>")
                    };
                    self.begin_file_reading(crate::input::LineSource::Terminal, Vec::new(), false);
                    if let Some(Level::Text(t)) = self.input.stack.last_mut() {
                        t.buf = text;
                        t.loc = 0;
                        t.state = crate::input::LexState::MidLine;
                    }
                    return;
                }
                'Q' | 'R' | 'S' => {
                    self.err.error_count = 0;
                    self.printer.print_str("OK, entering ");
                    match c {
                        'Q' => {
                            self.err.interaction = Interaction::Batch;
                            self.print_esc_str("batchmode");
                            self.printer.selector = match self.printer.selector {
                                Selector::TermAndLog => Selector::LogOnly,
                                _ => Selector::NoPrint,
                            };
                        }
                        'R' => {
                            self.err.interaction = Interaction::NonStop;
                            self.print_esc_str("nonstopmode");
                        }
                        _ => {
                            self.err.interaction = Interaction::Scroll;
                            self.print_esc_str("scrollmode");
                        }
                    }
                    self.printer.print_str("...");
                    self.printer.print_ln();
                    self.printer.update_terminal();
                    return;
                }
                'X' => {
                    self.err.interaction = Interaction::Scroll;
                    self.jump_out();
                }
                _ => {}
            }
            // Print the menu of available options.
            self.printer
                .print_str("Type <return> to proceed, S to scroll future error messages,");
            self.printer
                .print_nl("R to run without stopping, Q to run quietly,");
            self.printer.print_nl("I to insert something, ");
            if self
                .input
                .stack
                .iter()
                .any(|l| matches!(l, Level::Text(t) if t.is_input_file))
            {
                self.printer.print_str("E to edit your file,");
            }
            if self.err.deletions_allowed {
                self.printer
                    .print_nl("1 or ... or 9 to ignore the next 1 to 9 tokens of input,");
            }
            self.printer.print_nl("H for help, X to quit.");
        }
    }

    /// Pops finished terminal levels before prompting
    /// (`clear_for_error_prompt`, §330).
    fn clear_for_error_prompt(&mut self) {
        while self.input.stack.len() > 1 {
            match self.input.stack.last() {
                Some(Level::Text(t))
                    if matches!(t.source, LineSource::Terminal) && t.loc >= t.buf.len() =>
                {
                    self.end_file_reading();
                }
                _ => break,
            }
        }
        self.printer.print_ln();
    }

    /// Prints `\errhelp` (`give_err_help`).
    pub fn give_err_help(&mut self) {
        let list = self
            .eqtb
            .toks_param(crate::primitives_gen::assign_toks::ERRHELP)
            .clone();
        self.token_show_list(&list, 0, list.len(), 10_000_000);
    }

    /// Reports a fatal error and ends the run (`fatal_error`).
    pub fn fatal_error(&mut self, s: &str) -> ! {
        self.normalize_selector();
        self.print_err("Emergency stop");
        self.help_owned(vec![s.to_owned()]);
        self.succumb();
    }

    /// A LuaTeX fatal error of category `category` (as LuaTeX prints them:
    /// `! error:  (category): message`, then `!  ==> Fatal error
    /// occurred, …`); ends the run without the transcript line.
    pub fn luatex_fatal(&mut self, category: &str, msg: &str) -> ! {
        self.normalize_selector();
        self.printer.print_nl("! error:  (");
        self.printer.print_str(category);
        self.printer.print_str("): ");
        self.printer.print_str(msg);
        let pdf = self.eqtb.int(crate::primitives_gen::assign_int::OUTPUTMODE) > 0;
        self.printer.print_nl(if pdf {
            "!  ==> Fatal error occurred, bad output PDF file produced!"
        } else {
            "!  ==> Fatal error occurred, bad output DVI file produced!"
        });
        self.printer.print_ln();
        self.err.history = History::FatalErrorStop;
        for n in 0..128u32 {
            if self.printer.write_open(n) {
                self.printer.close_write(n);
            }
        }
        if self.total_pages == 0 {
            self.printer.print_str("No pages of output.");
            self.printer.print_ln();
        }
        if self.printer.log_opened() {
            self.printer.close_log();
        }
        self.printer.selector = Selector::TermOnly;
        self.printer.flush();
        std::panic::resume_unwind(Box::new(JumpOut));
    }

    /// Reports that a capacity was exceeded and ends the run (`overflow`).
    pub fn overflow(&mut self, s: &str, n: i64) -> ! {
        self.normalize_selector();
        self.print_err("TeX capacity exceeded, sorry [");
        self.printer.print_str(s);
        self.printer.print_char('=' as u32);
        self.printer.print_int(n);
        self.printer.print_char(']' as u32);
        self.help(&[
            "If you really absolutely need more capacity,",
            "you can ask a wizard to enlarge me.",
        ]);
        self.succumb();
    }

    /// Reports an internal inconsistency (`confusion`).
    pub fn confusion(&mut self, s: &str) -> ! {
        self.normalize_selector();
        if self.err.history < History::ErrorMessageIssued {
            self.print_err("This can't happen (");
            self.printer.print_str(s);
            self.printer.print_char(')' as u32);
            self.help(&["I'm broken. Please show this to someone who can fix can fix"]);
        } else {
            self.print_err("I can't go on meeting you like this");
            self.help(&[
                "One of your faux pas seems to have wounded me deeply...",
                "in fact, I'm barely conscious. Please fix it and try again.",
            ]);
        }
        self.succumb();
    }

    fn succumb(&mut self) -> ! {
        if self.err.interaction == Interaction::ErrorStop {
            self.err.interaction = Interaction::Scroll;
        }
        if self.printer.log_opened() {
            self.error();
        }
        self.err.history = History::FatalErrorStop;
        self.jump_out();
    }

    /// Ends the run (`jump_out`): closes files and unwinds to the driver.
    pub fn jump_out(&mut self) -> ! {
        self.close_files_and_terminate();
        std::panic::resume_unwind(Box::new(JumpOut));
    }

    /// Makes sure output goes somewhere sensible (`normalize_selector`).
    pub fn normalize_selector(&mut self) {
        self.printer.selector = if self.printer.log_opened() {
            Selector::TermAndLog
        } else {
            Selector::TermOnly
        };
        if self.job_name.is_none() {
            self.open_log_file();
        }
        if self.err.interaction == Interaction::Batch {
            self.printer.selector = match self.printer.selector {
                Selector::TermAndLog => Selector::LogOnly,
                _ => Selector::NoPrint,
            };
        }
    }

    /// Hook for user interrupts; retex does not support them.
    pub fn check_interrupt(&mut self) {}

    /// `back_error`: backs up the current token and reports an error.
    pub fn back_error(&mut self) {
        self.err.interrupt = false;
        self.back_input();
        self.err.interrupt = true;
        self.error();
    }

    /// `ins_error`: like `back_error` but the token is marked inserted.
    pub fn ins_error(&mut self) {
        self.err.interrupt = false;
        self.back_input_inserted();
        self.err.interrupt = true;
        self.error();
    }

    /// A LuaTeX warning (`warning  (category): message` on a line of its
    /// own); it counts as a warning for the final "see the transcript"
    /// note.
    pub fn luatex_warning(&mut self, category: &str, msg: &str) {
        self.printer.luatex_warning(category, msg);
        if self.err.history == History::Spotless {
            self.err.history = History::WarningIssued;
        }
    }

    /// Starts diagnostic output (`begin_diagnostic`): goes to the log only
    /// unless `\tracingonline > 0`.
    pub fn begin_diagnostic(&mut self) {
        self.err.old_setting = Some(self.printer.selector);
        let online = self
            .eqtb
            .int(crate::primitives_gen::assign_int::TRACINGONLINE);
        if online <= 0 && self.printer.selector == Selector::TermAndLog {
            self.printer.selector = Selector::LogOnly;
            if self.err.history == History::Spotless {
                self.err.history = History::WarningIssued;
            }
        }
    }

    /// Ends diagnostic output (`end_diagnostic`).
    pub fn end_diagnostic(&mut self, blank_line: bool) {
        self.printer.print_nl("");
        if blank_line {
            self.printer.print_ln();
        }
        if let Some(s) = self.err.old_setting.take() {
            self.printer.selector = s;
        }
    }

    /// Shows where the scanner is (`show_context`, §311).
    pub fn show_context(&mut self) {
        let error_context_lines = self
            .eqtb
            .int(crate::primitives_gen::assign_int::ERRORCONTEXTLINES);
        let input_ptr = self.input.stack.len().saturating_sub(1);
        let mut base_ptr = input_ptr;
        let mut nn: i32 = -1;
        loop {
            let (is_text, bottom) = match &self.input.stack.get(base_ptr) {
                Some(Level::Text(t)) => (
                    true,
                    t.is_input_file || matches!(t.source, LineSource::File { .. }) || base_ptr == 0,
                ),
                Some(Level::Tokens(_)) => (false, false),
                None => (true, true),
            };
            let bottom_line = is_text && bottom;
            if base_ptr == input_ptr || bottom_line || nn < error_context_lines {
                // Display the current context, omitting backed-up token
                // lists that have already been read.
                let show = match self.input.stack.get(base_ptr) {
                    Some(Level::Tokens(t)) => {
                        base_ptr == input_ptr
                            || !matches!(t.kind, TokKind::BackedUp | TokKind::BackedUpChar)
                            || t.loc < t.list.len()
                    }
                    _ => true,
                };
                if show {
                    self.printer.term_just_wrapped = false;
                    self.display_context(base_ptr);
                    nn += 1;
                    // LuaTeX's terminal shows an empty line after a context
                    // line that filled the terminal line exactly, when more
                    // context follows (the log does not).
                    if self.printer.term_just_wrapped
                        && !(bottom_line || base_ptr == 0)
                        && matches!(
                            self.printer.selector,
                            crate::print::Selector::TermAndLog | crate::print::Selector::TermOnly
                        )
                    {
                        self.printer.term_raw(b"\n");
                    }
                }
            } else if nn == error_context_lines {
                self.printer.print_nl("...");
                nn += 1;
            }
            if bottom_line || base_ptr == 0 {
                break;
            }
            base_ptr -= 1;
        }
    }

    fn display_context(&mut self, base_ptr: usize) {
        let old_setting = self.printer.selector;
        self.printer.tally = 0;
        let level = self.input.stack[base_ptr].clone();
        let l: usize;
        match &level {
            Level::Text(t) => {
                match &t.source {
                    LineSource::Terminal => {
                        if base_ptr == 0 {
                            self.printer.print_nl("<*>");
                        } else {
                            self.printer.print_nl("<insert> ");
                        }
                    }
                    LineSource::Read(n) => {
                        self.printer.print_nl("<read ");
                        if *n == 16 {
                            self.printer.print_char('*' as u32);
                        } else {
                            self.printer.print_int(i64::from(*n));
                        }
                        self.printer.print_char('>' as u32);
                    }
                    _ => {
                        self.printer.print_nl("l.");
                        self.printer.print_int(t.line.into());
                    }
                }
                self.printer.print_char(' ' as u32);
                // Pseudoprint the line.
                l = self.begin_pseudoprint();
                let end_line_char = self
                    .eqtb
                    .int(crate::primitives_gen::assign_int::ENDLINECHAR);
                let mut j = t.buf.len();
                if j > 0 && t.buf[j - 1] as i32 == end_line_char {
                    j -= 1;
                }
                for i in 0..j {
                    if i == t.loc {
                        self.set_trick_count();
                    }
                    self.printer.print_char(t.buf[i]);
                }
            }
            Level::Tokens(t) => {
                match t.kind {
                    TokKind::Parameter => self.printer.print_nl("<argument> "),
                    TokKind::UTemplate | TokKind::VTemplate => self.printer.print_nl("<template> "),
                    TokKind::BackedUp | TokKind::BackedUpChar => {
                        if t.loc >= t.list.len() {
                            self.printer.print_nl("<recently read> ")
                        } else {
                            self.printer.print_nl("<to be read again> ")
                        }
                    }
                    TokKind::Inserted => self.printer.print_nl("<inserted text> "),
                    TokKind::Macro => {
                        self.printer.print_ln();
                        self.print_cs(t.name);
                    }
                    TokKind::OutputText => self.printer.print_nl("<output> "),
                    TokKind::EveryParText => self.printer.print_nl("<everypar> "),
                    TokKind::EveryMathText => self.printer.print_nl("<everymath> "),
                    TokKind::EveryDisplayText => self.printer.print_nl("<everydisplay> "),
                    TokKind::EveryHboxText => self.printer.print_nl("<everyhbox> "),
                    TokKind::EveryVboxText => self.printer.print_nl("<everyvbox> "),
                    TokKind::EveryJobText => self.printer.print_nl("<everyjob> "),
                    TokKind::EveryCrText => self.printer.print_nl("<everycr> "),
                    TokKind::MarkText => self.printer.print_nl("<mark> "),
                    TokKind::EveryEofText => self.printer.print_nl("<everyeof> "),
                    TokKind::WriteText => self.printer.print_nl("<write> "),
                    TokKind::LocalText => self.printer.print_nl("<local> "),
                }
                l = self.begin_pseudoprint();
                // For macros the list starts with the parameter text; TeX
                // shows the whole definition (parameters and body).
                self.show_token_list_ctx(&t.list, t.loc, 100_000);
            }
        }
        self.printer.selector = old_setting;
        self.print_two_lines(l);
    }

    fn begin_pseudoprint(&mut self) -> usize {
        let l = self.printer.tally;
        self.printer.tally = 0;
        self.printer.selector = Selector::Pseudo;
        self.printer.trick_count = 1_000_000;
        self.printer.trick_buf.clear();
        self.printer.trick_buf.resize(self.printer.error_line, b' ');
        l
    }

    /// TeX's `set_trick_count`.
    pub fn set_trick_count(&mut self) {
        let p = &mut self.printer;
        p.first_count = p.tally;
        p.trick_count = p.tally + 1 + p.error_line - p.half_error_line;
        if p.trick_count < p.error_line {
            p.trick_count = p.error_line;
        }
    }

    fn print_two_lines(&mut self, l: usize) {
        if self.printer.trick_count == 1_000_000 {
            self.set_trick_count();
        }
        let p = &self.printer;
        let (tally, first_count, trick_count, half, error_line) = (
            p.tally,
            p.first_count,
            p.trick_count,
            p.half_error_line,
            p.error_line,
        );
        let m = if tally < trick_count {
            tally - first_count
        } else {
            trick_count - first_count
        };
        let (start, n) = if l + first_count <= half {
            (0, l + first_count)
        } else {
            self.printer.print_plain("...");
            (l + first_count - half + 3, half)
        };
        let buf = self.printer.trick_buf.clone();
        let mut bytes = Vec::new();
        for q in start..first_count {
            bytes.push(buf[q % error_line]);
        }
        self.print_raw_bytes(&bytes);
        self.printer.print_ln();
        for _ in 0..n {
            self.printer.print_char(' ' as u32);
        }
        let p_end = if m + n <= error_line {
            first_count + m
        } else {
            first_count + (error_line - n - 3)
        };
        bytes.clear();
        for q in first_count..p_end {
            bytes.push(buf[q % error_line]);
        }
        self.print_raw_bytes(&bytes);
        if m + n > error_line {
            self.printer.print_plain("...");
        }
    }

    /// Prints already-converted bytes from the pseudo buffer, decoding
    /// complete UTF-8 sequences so that line lengths stay correct.
    fn print_raw_bytes(&mut self, bytes: &[u8]) {
        let mut i = 0;
        while i < bytes.len() {
            let (c, n) = crate::utf8x::decode(bytes, i);
            if c < 32 && c != 9 && c != 10 && c != 13 {
                // A raw control byte inside the buffer: print as is.
                self.printer.print_char(c);
            } else {
                self.printer.print_char(c);
            }
            i += n;
        }
    }
}