src/Pure/Thy/latex.scala
author wenzelm
Mon, 22 Nov 2021 16:49:58 +0100
changeset 74829 f31229171b3b
parent 74826 0e4d8aa61ad7
child 74836 a97ec0954c50
permissions -rw-r--r--
source positions for document markup commands, e.g. to retrieve PIDE markup in presentation;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/latex.scala
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     3
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     4
Support for LaTeX.
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     5
*/
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     6
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     7
package isabelle
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     8
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     9
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    10
import java.io.{File => JFile}
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    11
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    12
import scala.annotation.tailrec
74748
wenzelm
parents: 73783
diff changeset
    13
import scala.collection.mutable
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71164
diff changeset
    14
import scala.util.matching.Regex
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    15
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    16
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    17
object Latex
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    18
{
74826
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    19
  /* output name for LaTeX macros */
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    20
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    21
  private val output_name_map: Map[Char, String] =
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    22
    Map('_' -> "UNDERSCORE",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    23
      '\'' -> "PRIME",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    24
      '0' -> "ZERO",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    25
      '1' -> "ONE",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    26
      '2' -> "TWO",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    27
      '3' -> "THREE",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    28
      '4' -> "FOUR",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    29
      '5' -> "FIVE",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    30
      '6' -> "SIX",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    31
      '7' -> "SEVEN",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    32
      '8' -> "EIGHT",
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    33
      '9' -> "NINE")
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    34
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    35
  def output_name(name: String): String =
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    36
    if (name.exists(output_name_map.keySet)) {
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    37
      val res = new StringBuilder
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    38
      for (c <- name) {
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    39
        output_name_map.get(c) match {
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    40
          case None => res += c
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    41
          case Some(s) => res ++= s
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    42
        }
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    43
      }
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    44
      res.toString
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    45
    }
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    46
    else name
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    47
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
    48
74786
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    49
  /* index entries */
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    50
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    51
  def index_escape(str: String): String =
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    52
  {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    53
    val special1 = "!\"@|"
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    54
    val special2 = "\\{}#"
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    55
    if (str.exists(c => special1.contains(c) || special2.contains(c))) {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    56
      val res = new StringBuilder
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    57
      for (c <- str) {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    58
        if (special1.contains(c)) {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    59
          res ++= "\\char"
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    60
          res ++= Value.Int(c)
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    61
        }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    62
        else {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    63
          if (special2.contains(c)) { res += '"'}
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    64
          res += c
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    65
        }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    66
      }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    67
      res.toString
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    68
    }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    69
    else str
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    70
  }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    71
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    72
  object Index_Item
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    73
  {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    74
    sealed case class Value(text: Text, like: String)
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    75
    def unapply(tree: XML.Tree): Option[Value] =
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    76
      tree match {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    77
        case XML.Wrapped_Elem(Markup.Latex_Index_Item(_), text, like) =>
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    78
          Some(Value(text, XML.content(like)))
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    79
        case _ => None
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    80
      }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    81
  }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    82
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    83
  object Index_Entry
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    84
  {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    85
    sealed case class Value(items: List[Index_Item.Value], kind: String)
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    86
    def unapply(tree: XML.Tree): Option[Value] =
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    87
      tree match {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    88
        case XML.Elem(Markup.Latex_Index_Entry(kind), body) =>
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    89
          val items = body.map(Index_Item.unapply)
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    90
          if (items.forall(_.isDefined)) Some(Value(items.map(_.get), kind)) else None
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    91
        case _ => None
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    92
      }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    93
  }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    94
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
    95
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
    96
  /* output text and positions */
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
    97
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
    98
  type Text = XML.Body
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
    99
74777
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   100
  def position(a: String, b: String): String = "%:%" + a + "=" + b + "%:%\n"
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
   101
74777
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   102
  def init_position(file_pos: String): List[String] =
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   103
    if (file_pos.isEmpty) Nil
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   104
    else List("\\endinput\n", position(Markup.FILE, file_pos))
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
   105
74824
6424f74fd9d4 Latex.Output.latex_heading depends on option document_heading_prefix, e.g. relevant for Dagstuhl LIPIcs which prefers unaliased \section etc.;
wenzelm
parents: 74823
diff changeset
   106
  class Output(options: Options)
74777
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   107
  {
74786
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   108
    def latex_output(latex_text: Text): String = apply(latex_text)
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   109
74790
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   110
    def latex_macro0(name: String): Text =
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   111
      XML.string("\\" + name)
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   112
74789
a5c23da73fca clarified signature;
wenzelm
parents: 74786
diff changeset
   113
    def latex_macro(name: String, body: Text): Text =
a5c23da73fca clarified signature;
wenzelm
parents: 74786
diff changeset
   114
      XML.enclose("\\" + name + "{", "}", body)
74786
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   115
74790
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   116
    def latex_environment(name: String, body: Text): Text =
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   117
      XML.enclose("%\n\\begin{" + name + "}%\n", "%\n\\end{" + name + "}", body)
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   118
74829
f31229171b3b source positions for document markup commands, e.g. to retrieve PIDE markup in presentation;
wenzelm
parents: 74826
diff changeset
   119
    def latex_heading(kind: String, pos: Position.T, body: Text): Text =
74824
6424f74fd9d4 Latex.Output.latex_heading depends on option document_heading_prefix, e.g. relevant for Dagstuhl LIPIcs which prefers unaliased \section etc.;
wenzelm
parents: 74823
diff changeset
   120
      XML.enclose("%\n\\" + options.string("document_heading_prefix") + kind + "{", "%\n}\n", body)
74823
d6ce4ce20422 more symbolic latex_output via XML;
wenzelm
parents: 74790
diff changeset
   121
74829
f31229171b3b source positions for document markup commands, e.g. to retrieve PIDE markup in presentation;
wenzelm
parents: 74826
diff changeset
   122
    def latex_body(kind: String, pos: Position.T, body: Text): Text =
74823
d6ce4ce20422 more symbolic latex_output via XML;
wenzelm
parents: 74790
diff changeset
   123
      latex_environment("isamarkup" + kind, body)
d6ce4ce20422 more symbolic latex_output via XML;
wenzelm
parents: 74790
diff changeset
   124
74826
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   125
    def latex_delim(name: String, body: Text): Text =
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   126
    {
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   127
      val s = output_name(name)
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   128
      XML.enclose("%\n\\isadelim" + s + "\n", "%\n\\endisadelim" + s + "\n", body)
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   129
    }
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   130
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   131
    def latex_tag(name: String, body: Text): Text =
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   132
    {
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   133
      val s = output_name(name)
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   134
      XML.enclose("%\n\\isatag" + s + "\n", "%\n\\endisatag" + s + "\n{\\isafold" + s + "}%\n", body)
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   135
    }
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   136
74786
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   137
    def index_item(item: Index_Item.Value): String =
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   138
    {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   139
      val like = if (item.like.isEmpty) "" else index_escape(item.like) + "@"
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   140
      val text = index_escape(latex_output(item.text))
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   141
      like + text
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   142
    }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   143
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   144
    def index_entry(entry: Index_Entry.Value): Text =
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   145
    {
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   146
      val items = entry.items.map(index_item).mkString("!")
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   147
      val kind = if (entry.kind.isEmpty) "" else "|" + index_escape(entry.kind)
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   148
      latex_macro("index", XML.string(items + kind))
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   149
    }
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   150
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   151
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   152
    /* standard output of text with per-line positions */
74784
d2522bb4db1b symbolic latex_output via XML, interpreted in Isabelle/Scala;
wenzelm
parents: 74783
diff changeset
   153
74777
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   154
    def apply(latex_text: Text, file_pos: String = ""): String =
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
   155
    {
74777
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   156
      var line = 1
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   157
      val result = new mutable.ListBuffer[String]
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   158
      val positions = new mutable.ListBuffer[String] ++= init_position(file_pos)
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   159
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   160
      def traverse(body: XML.Body): Unit =
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   161
      {
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   162
        body.foreach {
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   163
          case XML.Text(s) =>
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   164
            line += s.count(_ == '\n')
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   165
            result += s
74783
47f565849e71 tuned signature;
wenzelm
parents: 74777
diff changeset
   166
          case XML.Elem(Markup.Document_Latex(props), body) =>
47f565849e71 tuned signature;
wenzelm
parents: 74777
diff changeset
   167
            for { l <- Position.Line.unapply(props) if positions.nonEmpty } {
47f565849e71 tuned signature;
wenzelm
parents: 74777
diff changeset
   168
              val s = position(Value.Int(line), Value.Int(l))
47f565849e71 tuned signature;
wenzelm
parents: 74777
diff changeset
   169
              if (positions.last != s) positions += s
47f565849e71 tuned signature;
wenzelm
parents: 74777
diff changeset
   170
            }
47f565849e71 tuned signature;
wenzelm
parents: 74777
diff changeset
   171
            traverse(body)
74786
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   172
          case XML.Elem(Markup.Latex_Output(_), body) =>
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   173
            traverse(XML.string(latex_output(body)))
74790
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   174
          case XML.Elem(Markup.Latex_Macro0(name), Nil) =>
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   175
            traverse(latex_macro0(name))
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   176
          case XML.Elem(Markup.Latex_Macro(name), body) =>
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   177
            traverse(latex_macro(name, body))
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   178
          case XML.Elem(Markup.Latex_Environment(name), body) =>
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   179
            traverse(latex_environment(name, body))
74829
f31229171b3b source positions for document markup commands, e.g. to retrieve PIDE markup in presentation;
wenzelm
parents: 74826
diff changeset
   180
          case XML.Elem(markup @ Markup.Latex_Heading(kind), body) =>
f31229171b3b source positions for document markup commands, e.g. to retrieve PIDE markup in presentation;
wenzelm
parents: 74826
diff changeset
   181
            traverse(latex_heading(kind, markup.position_properties, body))
f31229171b3b source positions for document markup commands, e.g. to retrieve PIDE markup in presentation;
wenzelm
parents: 74826
diff changeset
   182
          case XML.Elem(markup @ Markup.Latex_Body(kind), body) =>
f31229171b3b source positions for document markup commands, e.g. to retrieve PIDE markup in presentation;
wenzelm
parents: 74826
diff changeset
   183
            traverse(latex_body(kind, markup.position_properties, body))
74826
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   184
          case XML.Elem(Markup.Latex_Delim(name), body) =>
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   185
            traverse(latex_delim(name, body))
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   186
          case XML.Elem(Markup.Latex_Tag(name), body) =>
0e4d8aa61ad7 more symbolic latex_output via XML (using YXML within text);
wenzelm
parents: 74824
diff changeset
   187
            traverse(latex_tag(name, body))
74786
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   188
          case Index_Entry(entry) =>
543f932f64b8 more symbolic latex output;
wenzelm
parents: 74784
diff changeset
   189
            traverse(index_entry(entry))
74790
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   190
          case t: XML.Tree =>
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   191
            error("Bad latex markup" +
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   192
              (if (file_pos.isEmpty) "" else Position.here(Position.File(file_pos))) + ":\n" +
3ce6fb9db485 more symbolic latex_output via XML;
wenzelm
parents: 74789
diff changeset
   193
              XML.string_of_tree(t))
74777
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   194
        }
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
   195
      }
74777
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   196
      traverse(latex_text)
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
   197
74777
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   198
      result ++= positions
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   199
      result.mkString
2fd0c33fe440 clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents: 74748
diff changeset
   200
    }
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
   201
  }
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
   202
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73736
diff changeset
   203
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   204
  /* generated .tex file */
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   205
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67183
diff changeset
   206
  private val File_Pattern = """^%:%file=(.+)%:%$""".r
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   207
  private val Line_Pattern = """^*%:%(\d+)=(\d+)%:%$""".r
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   208
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   209
  def read_tex_file(tex_file: Path): Tex_File =
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   210
  {
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   211
    val positions =
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   212
      Line.logical_lines(File.read(tex_file)).reverse.
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   213
        takeWhile(_ != "\\endinput").reverse
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   214
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   215
    val source_file =
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   216
      positions match {
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   217
        case File_Pattern(file) :: _ => Some(file)
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   218
        case _ => None
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   219
      }
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   220
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   221
    val source_lines =
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   222
      if (source_file.isEmpty) Nil
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   223
      else
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   224
        positions.flatMap(line =>
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   225
          line match {
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   226
            case Line_Pattern(Value.Int(line), Value.Int(source_line)) => Some(line -> source_line)
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   227
            case _ => None
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   228
          })
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   229
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   230
    new Tex_File(tex_file, source_file, source_lines)
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   231
  }
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   232
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   233
  final class Tex_File private[Latex](
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   234
    tex_file: Path, source_file: Option[String], source_lines: List[(Int, Int)])
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   235
  {
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   236
    override def toString: String = tex_file.toString
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   237
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   238
    def source_position(l: Int): Option[Position.T] =
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   239
      source_file match {
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   240
        case None => None
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   241
        case Some(file) =>
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   242
          val source_line =
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 71784
diff changeset
   243
            source_lines.iterator.takeWhile({ case (m, _) => m <= l }).
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 71784
diff changeset
   244
              foldLeft(0) { case (_, (_, n)) => n }
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67190
diff changeset
   245
          if (source_line == 0) None else Some(Position.Line_File(source_line, file))
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   246
      }
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   247
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   248
    def position(line: Int): Position.T =
67188
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
   249
      source_position(line) getOrElse Position.Line_File(line, tex_file.implode)
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   250
  }
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   251
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   252
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   253
  /* latex log */
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   254
73782
wenzelm
parents: 73780
diff changeset
   255
  def latex_errors(dir: Path, root_name: String): List[String] =
wenzelm
parents: 73780
diff changeset
   256
  {
wenzelm
parents: 73780
diff changeset
   257
    val root_log_path = dir + Path.explode(root_name).ext("log")
wenzelm
parents: 73780
diff changeset
   258
    if (root_log_path.is_file) {
wenzelm
parents: 73780
diff changeset
   259
      for { (msg, pos) <- filter_errors(dir, File.read(root_log_path)) }
wenzelm
parents: 73780
diff changeset
   260
        yield "Latex error" + Position.here(pos) + ":\n" + Library.indent_lines(2, msg)
wenzelm
parents: 73780
diff changeset
   261
    }
wenzelm
parents: 73780
diff changeset
   262
    else Nil
wenzelm
parents: 73780
diff changeset
   263
  }
wenzelm
parents: 73780
diff changeset
   264
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   265
  def filter_errors(dir: Path, root_log: String): List[(String, Position.T)] =
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   266
  {
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   267
    val seen_files = Synchronized(Map.empty[JFile, Tex_File])
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   268
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   269
    def check_tex_file(path: Path): Option[Tex_File] =
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   270
      seen_files.change_result(seen =>
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   271
        seen.get(path.file) match {
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   272
          case None =>
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   273
            if (path.is_file) {
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   274
              val tex_file = read_tex_file(path)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   275
              (Some(tex_file), seen + (path.file -> tex_file))
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   276
            }
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   277
            else (None, seen)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   278
          case some => (some, seen)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   279
        })
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   280
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   281
    def tex_file_position(path: Path, line: Int): Position.T =
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   282
      check_tex_file(path) match {
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   283
        case Some(tex_file) => tex_file.position(line)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   284
        case None => Position.Line_File(line, path.implode)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   285
      }
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   286
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   287
    object File_Line_Error
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   288
    {
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71164
diff changeset
   289
      val Pattern: Regex = """^(.*?\.\w\w\w):(\d+): (.*)$""".r
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   290
      def unapply(line: String): Option[(Path, Int, String)] =
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   291
        line match {
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   292
          case Pattern(file, Value.Int(line), message) =>
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   293
            val path = File.standard_path(file)
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   294
            if (Path.is_wellformed(path)) {
67183
28227b13a2f1 proper file;
wenzelm
parents: 67182
diff changeset
   295
              val file = (dir + Path.explode(path)).canonical
67423
8bec22c6b0fb tuned messages;
wenzelm
parents: 67418
diff changeset
   296
              val msg = Library.perhaps_unprefix("LaTeX Error: ", message)
8bec22c6b0fb tuned messages;
wenzelm
parents: 67418
diff changeset
   297
              if (file.is_file) Some((file, line, msg)) else None
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   298
            }
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   299
            else None
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   300
          case _ => None
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   301
        }
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   302
    }
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   303
67418
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   304
    val Line_Error = """^l\.\d+ (.*)$""".r
67196
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   305
    val More_Error =
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   306
      List(
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   307
        "<argument>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   308
        "<template>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   309
        "<recently read>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   310
        "<to be read again>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   311
        "<inserted text>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   312
        "<output>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   313
        "<everypar>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   314
        "<everymath>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   315
        "<everydisplay>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   316
        "<everyhbox>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   317
        "<everyvbox>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   318
        "<everyjob>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   319
        "<everycr>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   320
        "<mark>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   321
        "<everyeof>",
eb29f4868d14 more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents: 67194
diff changeset
   322
        "<write>").mkString("^(?:", "|", ") (.*)$").r
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   323
73474
4e12a6caefb3 turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents: 73359
diff changeset
   324
    val Bad_Font = """^LaTeX Font Warning: (Font .*\btxmia\b.* undefined).*$""".r
67483
aae933ca6fbd tuned message: same error may occur in different contexts;
wenzelm
parents: 67482
diff changeset
   325
    val Bad_File = """^! LaTeX Error: (File `.*' not found\.)$""".r
67482
dce667537607 detect more errors;
wenzelm
parents: 67423
diff changeset
   326
67423
8bec22c6b0fb tuned messages;
wenzelm
parents: 67418
diff changeset
   327
    val error_ignore =
8bec22c6b0fb tuned messages;
wenzelm
parents: 67418
diff changeset
   328
      Set(
8bec22c6b0fb tuned messages;
wenzelm
parents: 67418
diff changeset
   329
        "See the LaTeX manual or LaTeX Companion for explanation.",
8bec22c6b0fb tuned messages;
wenzelm
parents: 67418
diff changeset
   330
        "Type  H <return>  for immediate help.")
8bec22c6b0fb tuned messages;
wenzelm
parents: 67418
diff changeset
   331
67418
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   332
    def error_suffix1(lines: List[String]): Option[String] =
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   333
    {
71784
8a5da740e388 tuned -- avoid odd compiler warning;
wenzelm
parents: 71601
diff changeset
   334
      val lines1 =
8a5da740e388 tuned -- avoid odd compiler warning;
wenzelm
parents: 71601
diff changeset
   335
        lines.take(20).takeWhile({ case File_Line_Error((_, _, _)) => false case _ => true })
67418
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   336
      lines1.zipWithIndex.collectFirst({
67423
8bec22c6b0fb tuned messages;
wenzelm
parents: 67418
diff changeset
   337
        case (Line_Error(msg), i) =>
8bec22c6b0fb tuned messages;
wenzelm
parents: 67418
diff changeset
   338
          cat_lines(lines1.take(i).filterNot(error_ignore) ::: List(msg)) })
67418
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   339
    }
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   340
    def error_suffix2(lines: List[String]): Option[String] =
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   341
      lines match {
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   342
        case More_Error(msg) :: _ => Some(msg)
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   343
        case _ => None
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   344
      }
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   345
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   346
    @tailrec def filter(lines: List[String], result: List[(String, Position.T)])
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   347
      : List[(String, Position.T)] =
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   348
    {
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   349
      lines match {
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   350
        case File_Line_Error((file, line, msg1)) :: rest1 =>
67183
28227b13a2f1 proper file;
wenzelm
parents: 67182
diff changeset
   351
          val pos = tex_file_position(file, line)
67418
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   352
          val msg2 = error_suffix1(rest1) orElse error_suffix2(rest1) getOrElse ""
5a6ed2e679fb more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents: 67417
diff changeset
   353
          filter(rest1, (Exn.cat_message(msg1, msg2), pos) :: result)
73474
4e12a6caefb3 turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents: 73359
diff changeset
   354
        case Bad_Font(msg) :: rest =>
4e12a6caefb3 turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents: 73359
diff changeset
   355
          filter(rest, (msg, Position.none) :: result)
67483
aae933ca6fbd tuned message: same error may occur in different contexts;
wenzelm
parents: 67482
diff changeset
   356
        case Bad_File(msg) :: rest =>
67482
dce667537607 detect more errors;
wenzelm
parents: 67423
diff changeset
   357
          filter(rest, (msg, Position.none) :: result)
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   358
        case _ :: rest => filter(rest, result)
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   359
        case Nil => result.reverse
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   360
      }
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   361
    }
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   362
67174
258e1394e76a more robust Windows support;
wenzelm
parents: 67173
diff changeset
   363
    filter(Line.logical_lines(root_log), Nil)
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   364
  }
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   365
}