author | wenzelm |
Fri, 07 May 2021 16:49:08 +0200 | |
changeset 73647 | a037f01aedab |
parent 73474 | 4e12a6caefb3 |
child 73736 | a8ff6e4ee661 |
permissions | -rw-r--r-- |
67172 | 1 |
/* Title: Pure/Thy/latex.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Support for LaTeX. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
67173 | 10 |
import java.io.{File => JFile} |
11 |
||
67172 | 12 |
import scala.annotation.tailrec |
71601 | 13 |
import scala.util.matching.Regex |
67172 | 14 |
|
15 |
||
16 |
object Latex |
|
17 |
{ |
|
67173 | 18 |
/** latex errors **/ |
19 |
||
67176
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
20 |
def latex_errors(dir: Path, root_name: String): List[String] = |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
21 |
{ |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
22 |
val root_log_path = dir + Path.explode(root_name).ext("log") |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
23 |
if (root_log_path.is_file) { |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
24 |
for { (msg, pos) <- filter_errors(dir, File.read(root_log_path)) } |
71164 | 25 |
yield "Latex error" + Position.here(pos) + ":\n" + Library.prefix_lines(" ", msg) |
67176
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
26 |
} |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
27 |
else Nil |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
28 |
} |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
29 |
|
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
30 |
|
67173 | 31 |
/* generated .tex file */ |
32 |
||
67184 | 33 |
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
|
34 |
private val Line_Pattern = """^*%:%(\d+)=(\d+)%:%$""".r |
67173 | 35 |
|
36 |
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
|
37 |
{ |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
38 |
val positions = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
39 |
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
|
40 |
takeWhile(_ != "\\endinput").reverse |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
41 |
|
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
42 |
val source_file = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
43 |
positions match { |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
44 |
case File_Pattern(file) :: _ => Some(file) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
45 |
case _ => None |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
46 |
} |
67173 | 47 |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
48 |
val source_lines = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
49 |
if (source_file.isEmpty) Nil |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
50 |
else |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
51 |
positions.flatMap(line => |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
52 |
line match { |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
53 |
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
|
54 |
case _ => None |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
55 |
}) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
56 |
|
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
57 |
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
|
58 |
} |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
59 |
|
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
60 |
final class Tex_File private[Latex]( |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
61 |
tex_file: Path, source_file: Option[String], source_lines: List[(Int, Int)]) |
67173 | 62 |
{ |
63 |
override def toString: String = tex_file.toString |
|
64 |
||
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
65 |
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
|
66 |
source_file match { |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
67 |
case None => None |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
68 |
case Some(file) => |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
69 |
val source_line = |
73359 | 70 |
source_lines.iterator.takeWhile({ case (m, _) => m <= l }). |
71 |
foldLeft(0) { case (_, (_, n)) => n } |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
72 |
if (source_line == 0) None else Some(Position.Line_File(source_line, file)) |
67173 | 73 |
} |
74 |
||
75 |
def position(line: Int): Position.T = |
|
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
76 |
source_position(line) getOrElse Position.Line_File(line, tex_file.implode) |
67173 | 77 |
} |
78 |
||
79 |
||
80 |
/* latex log */ |
|
81 |
||
82 |
def filter_errors(dir: Path, root_log: String): List[(String, Position.T)] = |
|
67172 | 83 |
{ |
67173 | 84 |
val seen_files = Synchronized(Map.empty[JFile, Tex_File]) |
85 |
||
86 |
def check_tex_file(path: Path): Option[Tex_File] = |
|
87 |
seen_files.change_result(seen => |
|
88 |
seen.get(path.file) match { |
|
89 |
case None => |
|
90 |
if (path.is_file) { |
|
91 |
val tex_file = read_tex_file(path) |
|
92 |
(Some(tex_file), seen + (path.file -> tex_file)) |
|
93 |
} |
|
94 |
else (None, seen) |
|
95 |
case some => (some, seen) |
|
96 |
}) |
|
97 |
||
98 |
def tex_file_position(path: Path, line: Int): Position.T = |
|
99 |
check_tex_file(path) match { |
|
100 |
case Some(tex_file) => tex_file.position(line) |
|
101 |
case None => Position.Line_File(line, path.implode) |
|
102 |
} |
|
103 |
||
67172 | 104 |
object File_Line_Error |
105 |
{ |
|
71601 | 106 |
val Pattern: Regex = """^(.*?\.\w\w\w):(\d+): (.*)$""".r |
67173 | 107 |
def unapply(line: String): Option[(Path, Int, String)] = |
67172 | 108 |
line match { |
109 |
case Pattern(file, Value.Int(line), message) => |
|
110 |
val path = File.standard_path(file) |
|
111 |
if (Path.is_wellformed(path)) { |
|
67183 | 112 |
val file = (dir + Path.explode(path)).canonical |
67423 | 113 |
val msg = Library.perhaps_unprefix("LaTeX Error: ", message) |
114 |
if (file.is_file) Some((file, line, msg)) else None |
|
67172 | 115 |
} |
116 |
else None |
|
117 |
case _ => None |
|
118 |
} |
|
119 |
} |
|
120 |
||
67418
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
121 |
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
|
122 |
val More_Error = |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
123 |
List( |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
124 |
"<argument>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
125 |
"<template>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
126 |
"<recently read>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
127 |
"<to be read again>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
128 |
"<inserted text>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
129 |
"<output>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
130 |
"<everypar>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
131 |
"<everymath>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
132 |
"<everydisplay>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
133 |
"<everyhbox>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
134 |
"<everyvbox>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
135 |
"<everyjob>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
136 |
"<everycr>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
137 |
"<mark>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
138 |
"<everyeof>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
139 |
"<write>").mkString("^(?:", "|", ") (.*)$").r |
67172 | 140 |
|
73474
4e12a6caefb3
turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents:
73359
diff
changeset
|
141 |
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
|
142 |
val Bad_File = """^! LaTeX Error: (File `.*' not found\.)$""".r |
67482 | 143 |
|
67423 | 144 |
val error_ignore = |
145 |
Set( |
|
146 |
"See the LaTeX manual or LaTeX Companion for explanation.", |
|
147 |
"Type H <return> for immediate help.") |
|
148 |
||
67418
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
149 |
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
|
150 |
{ |
71784 | 151 |
val lines1 = |
152 |
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
|
153 |
lines1.zipWithIndex.collectFirst({ |
67423 | 154 |
case (Line_Error(msg), i) => |
155 |
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
|
156 |
} |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
157 |
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
|
158 |
lines match { |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
159 |
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
|
160 |
case _ => None |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
161 |
} |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
162 |
|
67173 | 163 |
@tailrec def filter(lines: List[String], result: List[(String, Position.T)]) |
164 |
: List[(String, Position.T)] = |
|
165 |
{ |
|
67172 | 166 |
lines match { |
67173 | 167 |
case File_Line_Error((file, line, msg1)) :: rest1 => |
67183 | 168 |
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
|
169 |
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
|
170 |
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
|
171 |
case Bad_Font(msg) :: rest => |
4e12a6caefb3
turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents:
73359
diff
changeset
|
172 |
filter(rest, (msg, Position.none) :: result) |
67483
aae933ca6fbd
tuned message: same error may occur in different contexts;
wenzelm
parents:
67482
diff
changeset
|
173 |
case Bad_File(msg) :: rest => |
67482 | 174 |
filter(rest, (msg, Position.none) :: result) |
67172 | 175 |
case _ :: rest => filter(rest, result) |
176 |
case Nil => result.reverse |
|
177 |
} |
|
67173 | 178 |
} |
67172 | 179 |
|
67174 | 180 |
filter(Line.logical_lines(root_log), Nil) |
67173 | 181 |
} |
67172 | 182 |
} |