author | wenzelm |
Tue, 16 Jan 2018 11:27:52 +0100 | |
changeset 67446 | 1f4d167b6ac9 |
parent 67439 | 78759a7bd874 |
child 67461 | aad5c0982c3d |
permissions | -rw-r--r-- |
22124 | 1 |
(* Title: Pure/Thy/thy_output.ML |
67386 | 2 |
Author: Makarius |
22124 | 3 |
|
67386 | 4 |
Theory document output. |
22124 | 5 |
*) |
6 |
||
7 |
signature THY_OUTPUT = |
|
8 |
sig |
|
67381 | 9 |
val output_text: Proof.context -> {markdown: bool} -> Input.source -> Latex.text list |
10 |
val check_comments: Proof.context -> Symbol_Pos.T list -> unit |
|
11 |
val check_token_comments: Proof.context -> Token.T -> unit |
|
12 |
val output_token: Proof.context -> Token.T -> Latex.text list |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
13 |
val present_thy: theory -> (Toplevel.transition * Toplevel.state) list -> |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
14 |
Token.T list -> Latex.text list |
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
15 |
val pretty_text: Proof.context -> string -> Pretty.T |
28644 | 16 |
val pretty_term: Proof.context -> term -> Pretty.T |
17 |
val pretty_thm: Proof.context -> thm -> Pretty.T |
|
58011
bc6bced136e5
tuned signature -- moved type src to Token, without aliases;
wenzelm
parents:
57080
diff
changeset
|
18 |
val str_of_source: Token.src -> string |
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
19 |
val maybe_pretty_source: (Proof.context -> 'a -> Pretty.T) -> Proof.context -> |
58011
bc6bced136e5
tuned signature -- moved type src to Token, without aliases;
wenzelm
parents:
57080
diff
changeset
|
20 |
Token.src -> 'a list -> Pretty.T list |
59175
bf465f335e85
system option "pretty_margin" is superseded by "thy_output_margin";
wenzelm
parents:
59067
diff
changeset
|
21 |
val string_of_margin: Proof.context -> Pretty.T -> string |
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
22 |
val output: Proof.context -> Pretty.T list -> string |
58716
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
23 |
val verbatim_text: Proof.context -> string -> string |
22124 | 24 |
end; |
25 |
||
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
26 |
structure Thy_Output: THY_OUTPUT = |
22124 | 27 |
struct |
28 |
||
61457 | 29 |
(* output text *) |
56548 | 30 |
|
67381 | 31 |
fun output_text ctxt {markdown} source = |
61457 | 32 |
let |
33 |
val pos = Input.pos_of source; |
|
34 |
val syms = Input.source_explode source; |
|
35 |
||
67381 | 36 |
val _ = Context_Position.report ctxt pos (Markup.language_document (Input.is_delimited source)); |
62749 | 37 |
|
67184 | 38 |
val output_antiquotes = |
67386 | 39 |
map (fn ant => |
40 |
Latex.text (Document_Antiquotation.evaluate ctxt ant, #1 (Antiquote.range [ant]))); |
|
61036
f6f2959bed67
clarified language context, e.g. relevant for symbols;
wenzelm
parents:
60100
diff
changeset
|
41 |
|
61461 | 42 |
fun output_line line = |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
43 |
(if Markdown.line_is_item line then [Latex.string "\\item "] else []) @ |
61461 | 44 |
output_antiquotes (Markdown.line_content line); |
45 |
||
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
46 |
fun output_block (Markdown.Par lines) = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
47 |
Latex.block (separate (Latex.string "\n") (map (Latex.block o output_line) lines)) |
61459
5f2ddeb15c06
clarified nesting of paragraphs: indentation is taken into account more uniformly;
wenzelm
parents:
61458
diff
changeset
|
48 |
| output_block (Markdown.List {kind, body, ...}) = |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
49 |
Latex.environment_block (Markdown.print_kind kind) (output_blocks body) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
50 |
and output_blocks blocks = separate (Latex.string "\n\n") (map output_block blocks); |
67184 | 51 |
in |
67381 | 52 |
if Toplevel.is_skipped_proof (Toplevel.presentation_state ctxt) then [] |
67184 | 53 |
else if markdown andalso exists (Markdown.is_control o Symbol_Pos.symbol) syms |
54 |
then |
|
55 |
let |
|
56 |
val ants = Antiquote.parse pos syms; |
|
57 |
val reports = Antiquote.antiq_reports ants; |
|
58 |
val blocks = Markdown.read_antiquotes ants; |
|
67381 | 59 |
val _ = Context_Position.reports ctxt (reports @ Markdown.reports blocks); |
67186
a58bbe66ac81
avoid excessive whitespace between antiquotations and text;
wenzelm
parents:
67184
diff
changeset
|
60 |
in output_blocks blocks end |
67184 | 61 |
else |
62 |
let |
|
63 |
val ants = Antiquote.parse pos (Symbol_Pos.trim_blanks syms); |
|
64 |
val reports = Antiquote.antiq_reports ants; |
|
67381 | 65 |
val _ = Context_Position.reports ctxt (reports @ Markdown.text_reports ants); |
67186
a58bbe66ac81
avoid excessive whitespace between antiquotations and text;
wenzelm
parents:
67184
diff
changeset
|
66 |
in output_antiquotes ants end |
67184 | 67 |
end; |
48918
6e5fd4585512
check side-comments of command spans (normally filtered out in Outer_Syntax.toplevel_source);
wenzelm
parents:
47005
diff
changeset
|
68 |
|
6e5fd4585512
check side-comments of command spans (normally filtered out in Outer_Syntax.toplevel_source);
wenzelm
parents:
47005
diff
changeset
|
69 |
|
67425
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
70 |
(* output tokens with formal comments *) |
22124 | 71 |
|
67353 | 72 |
local |
73 |
||
67429 | 74 |
fun output_latex syms = |
75 |
[Latex.text (Symbol_Pos.content syms, #1 (Symbol_Pos.range syms))]; |
|
76 |
||
67356
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
77 |
fun output_symbols syms = |
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
78 |
[Latex.text (Latex.output_symbols (map Symbol_Pos.symbol syms), #1 (Symbol_Pos.range syms))]; |
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
79 |
|
67358 | 80 |
val output_symbols_antiq = |
81 |
(fn Antiquote.Text syms => output_symbols syms |
|
67353 | 82 |
| Antiquote.Control {name = (name, _), body, ...} => |
67374
5a049cf98438
clarified output (see also 909dcdec2122, 34d1913f0b20);
wenzelm
parents:
67372
diff
changeset
|
83 |
Latex.string (Latex.output_symbols [Symbol.encode (Symbol.Control name)]) :: |
67358 | 84 |
output_symbols body |
67353 | 85 |
| Antiquote.Antiq {body, ...} => |
67358 | 86 |
Latex.enclose_body "%\n\\isaantiq\n" "{}%\n\\endisaantiq\n" (output_symbols body)); |
67353 | 87 |
|
67425
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
88 |
fun output_symbols_comment ctxt {antiq} (comment, syms) = |
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
89 |
(case (comment, antiq) of |
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
90 |
(NONE, false) => output_symbols syms |
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
91 |
| (NONE, true) => |
67413 | 92 |
Antiquote.parse (#1 (Symbol_Pos.range syms)) syms |
93 |
|> maps output_symbols_antiq |
|
67425
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
94 |
| (SOME Comment.Comment, _) => |
67413 | 95 |
let |
96 |
val body = Symbol_Pos.cartouche_content syms; |
|
97 |
val source = Input.source true (Symbol_Pos.implode body) (Symbol_Pos.range body); |
|
98 |
in |
|
99 |
output_text ctxt {markdown = false} source |
|
67421
c4a10621c72e
allow TeX comment % in formal comment body, but avoid extra space (cf. d7c6054b2ab1);
wenzelm
parents:
67413
diff
changeset
|
100 |
|> Latex.enclose_body "%\n\\isamarkupcmt{" "%\n}" |
67425
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
101 |
end |
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
102 |
| (SOME Comment.Cancel, _) => |
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
103 |
output_symbols (Symbol_Pos.cartouche_content syms) |
67429 | 104 |
|> Latex.enclose_body "%\n\\isamarkupcancel{" "}" |
105 |
| (SOME Comment.Latex, _) => |
|
106 |
output_latex (Symbol_Pos.cartouche_content syms)); |
|
67372 | 107 |
|
67353 | 108 |
in |
109 |
||
67381 | 110 |
fun output_body ctxt antiq bg en syms = |
67425
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
111 |
(case Comment.read_body syms of |
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
112 |
SOME res => maps (output_symbols_comment ctxt {antiq = antiq}) res |
67375 | 113 |
| NONE => output_symbols syms) |> Latex.enclose_body bg en |
67381 | 114 |
and output_token ctxt tok = |
67353 | 115 |
let |
67378 | 116 |
fun output antiq bg en = |
67381 | 117 |
output_body ctxt antiq bg en (Input.source_explode (Token.input_of tok)); |
67378 | 118 |
in |
119 |
(case Token.kind_of tok of |
|
67439
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
120 |
Token.Comment NONE => [] |
67378 | 121 |
| Token.Command => output false "\\isacommand{" "}" |
122 |
| Token.Keyword => |
|
123 |
if Symbol.is_ascii_identifier (Token.content_of tok) |
|
124 |
then output false "\\isakeyword{" "}" |
|
125 |
else output false "" "" |
|
126 |
| Token.String => output false "{\\isachardoublequoteopen}" "{\\isachardoublequoteclose}" |
|
127 |
| Token.Alt_String => output false "{\\isacharbackquoteopen}" "{\\isacharbackquoteclose}" |
|
128 |
| Token.Verbatim => output true "{\\isacharverbatimopen}" "{\\isacharverbatimclose}" |
|
129 |
| Token.Cartouche => output false "{\\isacartoucheopen}" "{\\isacartoucheclose}" |
|
130 |
| _ => output false "" "") |
|
131 |
end handle ERROR msg => error (msg ^ Position.here (Token.pos_of tok)); |
|
67353 | 132 |
|
67381 | 133 |
fun check_comments ctxt = |
67425
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
134 |
Comment.read_body #> (Option.app o List.app) (fn (comment, syms) => |
67388
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
135 |
let |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
136 |
val pos = #1 (Symbol_Pos.range syms); |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
137 |
val _ = |
67429 | 138 |
comment |> Option.app (fn kind => |
139 |
Context_Position.reports ctxt [(pos, Comment.markup kind), (pos, Markup.cartouche)]); |
|
67425
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
140 |
val _ = output_symbols_comment ctxt {antiq = false} (comment, syms); |
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67421
diff
changeset
|
141 |
in if comment = SOME Comment.Comment then check_comments ctxt syms else () end); |
67377
143665524d8e
check formal comments recursively, within arbitrary cartouches (unknown sublanguages);
wenzelm
parents:
67375
diff
changeset
|
142 |
|
67381 | 143 |
fun check_token_comments ctxt tok = |
144 |
check_comments ctxt (Input.source_explode (Token.input_of tok)); |
|
67377
143665524d8e
check formal comments recursively, within arbitrary cartouches (unknown sublanguages);
wenzelm
parents:
67375
diff
changeset
|
145 |
|
67353 | 146 |
end; |
147 |
||
148 |
||
67356
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
149 |
|
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
150 |
(** present theory source **) |
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
151 |
|
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
152 |
(*NB: arranging white space around command spans is a black art*) |
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
153 |
|
67439
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
154 |
val is_white = Token.is_space orf Token.is_informal_comment; |
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
155 |
val is_black = not o is_white; |
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
156 |
|
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
157 |
val is_white_comment = Token.is_informal_comment; |
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
158 |
val is_black_comment = Token.is_formal_comment; |
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
159 |
|
67356
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
160 |
|
22124 | 161 |
(* presentation tokens *) |
162 |
||
163 |
datatype token = |
|
67360 | 164 |
Ignore_Token |
59065 | 165 |
| Basic_Token of Token.T |
166 |
| Markup_Token of string * Input.source |
|
167 |
| Markup_Env_Token of string * Input.source |
|
61456 | 168 |
| Raw_Token of Input.source; |
22124 | 169 |
|
59065 | 170 |
fun basic_token pred (Basic_Token tok) = pred tok |
22124 | 171 |
| basic_token _ _ = false; |
172 |
||
67439
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
173 |
val white_token = basic_token is_white; |
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
174 |
val white_comment_token = basic_token is_white_comment; |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
175 |
val blank_token = basic_token Token.is_blank; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
176 |
val newline_token = basic_token Token.is_newline; |
22124 | 177 |
|
67381 | 178 |
fun present_token ctxt tok = |
61457 | 179 |
(case tok of |
67360 | 180 |
Ignore_Token => [] |
67381 | 181 |
| Basic_Token tok => output_token ctxt tok |
67357 | 182 |
| Markup_Token (cmd, source) => |
183 |
Latex.enclose_body ("%\n\\isamarkup" ^ cmd ^ "{") "%\n}\n" |
|
67381 | 184 |
(output_text ctxt {markdown = false} source) |
61457 | 185 |
| Markup_Env_Token (cmd, source) => |
67381 | 186 |
[Latex.environment_block ("isamarkup" ^ cmd) (output_text ctxt {markdown = true} source)] |
67186
a58bbe66ac81
avoid excessive whitespace between antiquotations and text;
wenzelm
parents:
67184
diff
changeset
|
187 |
| Raw_Token source => |
67381 | 188 |
Latex.string "%\n" :: output_text ctxt {markdown = true} source @ [Latex.string "\n"]); |
61455 | 189 |
|
190 |
||
22124 | 191 |
(* command spans *) |
192 |
||
193 |
type command = string * Position.T * string list; (*name, position, tags*) |
|
194 |
type source = (token * (string * int)) list; (*token, markup flag, meta-comment depth*) |
|
195 |
||
196 |
datatype span = Span of command * (source * source * source * source) * bool; |
|
197 |
||
198 |
fun make_span cmd src = |
|
199 |
let |
|
200 |
fun take_newline (tok :: toks) = |
|
201 |
if newline_token (fst tok) then ([tok], toks, true) |
|
202 |
else ([], tok :: toks, false) |
|
203 |
| take_newline [] = ([], [], false); |
|
204 |
val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) = |
|
205 |
src |
|
67439
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
206 |
|> take_prefix (white_token o fst) |
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
207 |
||>> take_suffix (white_token o fst) |
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
208 |
||>> take_prefix (white_comment_token o fst) |
22124 | 209 |
||> take_newline; |
210 |
in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end; |
|
211 |
||
212 |
||
213 |
(* present spans *) |
|
214 |
||
215 |
local |
|
216 |
||
217 |
fun err_bad_nesting pos = |
|
218 |
error ("Bad nesting of commands in presentation" ^ pos); |
|
219 |
||
220 |
fun edge which f (x: string option, y) = |
|
221 |
if x = y then I |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
222 |
else (case which (x, y) of NONE => I | SOME txt => cons (Latex.string (f txt))); |
22124 | 223 |
|
224 |
val begin_tag = edge #2 Latex.begin_tag; |
|
225 |
val end_tag = edge #1 Latex.end_tag; |
|
226 |
fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e; |
|
227 |
fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e; |
|
228 |
||
229 |
in |
|
230 |
||
67381 | 231 |
fun present_span thy keywords document_tags span state state' |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
232 |
(tag_stack, active_tag, newline, latex, present_cont) = |
22124 | 233 |
let |
67381 | 234 |
val ctxt' = |
235 |
Toplevel.presentation_context state' |
|
236 |
handle Toplevel.UNDEF => Proof_Context.init_global (Context.get_theory thy Context.PureN); |
|
22124 | 237 |
val present = fold (fn (tok, (flag, 0)) => |
67381 | 238 |
fold cons (present_token ctxt' tok) |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
239 |
#> cons (Latex.string flag) |
22124 | 240 |
| _ => I); |
241 |
||
242 |
val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span; |
|
243 |
||
244 |
val (tag, tags) = tag_stack; |
|
46924 | 245 |
val tag' = try hd (fold (update (op =)) cmd_tags (the_list tag)); |
22124 | 246 |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
247 |
val nesting = Toplevel.level state' - Toplevel.level state; |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
248 |
|
22124 | 249 |
val active_tag' = |
250 |
if is_some tag' then tag' |
|
251 |
else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
252 |
else |
67138 | 253 |
(case Keyword.command_tags keywords cmd_name @ document_tags of |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
254 |
default_tag :: _ => SOME default_tag |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
255 |
| [] => |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
256 |
if Keyword.is_vacuous keywords cmd_name andalso Toplevel.is_proof state |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
257 |
then active_tag |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
258 |
else NONE); |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
259 |
|
22124 | 260 |
val edge = (active_tag, active_tag'); |
261 |
||
262 |
val newline' = |
|
263 |
if is_none active_tag' then span_newline else newline; |
|
264 |
||
265 |
val tag_stack' = |
|
266 |
if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack |
|
267 |
else if nesting >= 0 then (tag', replicate nesting tag @ tags) |
|
268 |
else |
|
33957 | 269 |
(case drop (~ nesting - 1) tags of |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
270 |
tg :: tgs => (tg, tgs) |
48992 | 271 |
| [] => err_bad_nesting (Position.here cmd_pos)); |
22124 | 272 |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
273 |
val latex' = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
274 |
latex |
22124 | 275 |
|> end_tag edge |
276 |
|> close_delim (fst present_cont) edge |
|
277 |
|> snd present_cont |
|
278 |
|> open_delim (present (#1 srcs)) edge |
|
279 |
|> begin_tag edge |
|
280 |
|> present (#2 srcs); |
|
281 |
val present_cont' = |
|
282 |
if newline then (present (#3 srcs), present (#4 srcs)) |
|
283 |
else (I, present (#3 srcs) #> present (#4 srcs)); |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
284 |
in (tag_stack', active_tag', newline', latex', present_cont') end; |
22124 | 285 |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
286 |
fun present_trailer ((_, tags), active_tag, _, latex, present_cont) = |
22124 | 287 |
if not (null tags) then err_bad_nesting " at end of theory" |
288 |
else |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
289 |
latex |
22124 | 290 |
|> end_tag (active_tag, NONE) |
291 |
|> close_delim (fst present_cont) (active_tag, NONE) |
|
292 |
|> snd present_cont; |
|
293 |
||
294 |
end; |
|
295 |
||
296 |
||
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
297 |
(* present_thy *) |
22124 | 298 |
|
299 |
local |
|
300 |
||
61455 | 301 |
val markup_true = "\\isamarkuptrue%\n"; |
302 |
val markup_false = "\\isamarkupfalse%\n"; |
|
303 |
||
22124 | 304 |
val space_proper = |
67439
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
305 |
Scan.one Token.is_blank -- Scan.many is_white_comment -- Scan.one is_black; |
22124 | 306 |
|
67439
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
307 |
val is_improper = not o (is_black orf Token.is_begin_ignore orf Token.is_end_ignore); |
22124 | 308 |
val improper = Scan.many is_improper; |
309 |
val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper)); |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
310 |
val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one Token.is_blank)); |
22124 | 311 |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
312 |
val opt_newline = Scan.option (Scan.one Token.is_newline); |
22124 | 313 |
|
314 |
val ignore = |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
315 |
Scan.depend (fn d => opt_newline |-- Scan.one Token.is_begin_ignore |
22124 | 316 |
>> pair (d + 1)) || |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
317 |
Scan.depend (fn d => Scan.one Token.is_end_ignore --| |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43564
diff
changeset
|
318 |
(if d = 0 then Scan.fail_with (K (fn () => "Bad nesting of meta-comments")) else opt_newline) |
22124 | 319 |
>> pair (d - 1)); |
320 |
||
36950 | 321 |
val tag = (improper -- Parse.$$$ "%" -- improper) |-- Parse.!!! (Parse.tag_name --| blank_end); |
22124 | 322 |
|
323 |
val locale = |
|
36950 | 324 |
Scan.option ((Parse.$$$ "(" -- improper -- Parse.$$$ "in") |-- |
62969 | 325 |
Parse.!!! (improper |-- Parse.name --| (improper -- Parse.$$$ ")"))); |
22124 | 326 |
|
327 |
in |
|
328 |
||
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58923
diff
changeset
|
329 |
fun present_thy thy command_results toks = |
22124 | 330 |
let |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58923
diff
changeset
|
331 |
val keywords = Thy_Header.get_keywords thy; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58923
diff
changeset
|
332 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58923
diff
changeset
|
333 |
|
22124 | 334 |
(* tokens *) |
335 |
||
336 |
val ignored = Scan.state --| ignore |
|
67360 | 337 |
>> (fn d => (NONE, (Ignore_Token, ("", d)))); |
22124 | 338 |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
339 |
fun markup pred mk flag = Scan.peek (fn d => |
36950 | 340 |
improper |-- |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
341 |
Parse.position (Scan.one (fn tok => |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
342 |
Token.is_command tok andalso pred keywords (Token.content_of tok))) -- |
22124 | 343 |
Scan.repeat tag -- |
51627
589daaf48dba
tuned signature -- agree with markup terminology;
wenzelm
parents:
51626
diff
changeset
|
344 |
Parse.!!!! ((improper -- locale -- improper) |-- Parse.document_source --| improper_end) |
59064 | 345 |
>> (fn (((tok, pos'), tags), source) => |
59065 | 346 |
let val name = Token.content_of tok |
347 |
in (SOME (name, pos', tags), (mk (name, source), (flag, d))) end)); |
|
22124 | 348 |
|
349 |
val command = Scan.peek (fn d => |
|
61814
1ca1142e1711
clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents:
61748
diff
changeset
|
350 |
Scan.optional (Scan.one Token.is_command_modifier ::: improper) [] -- |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
351 |
Scan.one Token.is_command -- Scan.repeat tag |
59939
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents:
59924
diff
changeset
|
352 |
>> (fn ((cmd_mod, cmd), tags) => |
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents:
59924
diff
changeset
|
353 |
map (fn tok => (NONE, (Basic_Token tok, ("", d)))) cmd_mod @ |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
354 |
[(SOME (Token.content_of cmd, Token.pos_of cmd, tags), |
61455 | 355 |
(Basic_Token cmd, (markup_false, d)))])); |
22124 | 356 |
|
357 |
val cmt = Scan.peek (fn d => |
|
67446 | 358 |
Scan.one is_black_comment >> (fn tok => (NONE, (Basic_Token tok, ("", d))))); |
22124 | 359 |
|
360 |
val other = Scan.peek (fn d => |
|
59065 | 361 |
Parse.not_eof >> (fn tok => (NONE, (Basic_Token tok, ("", d))))); |
22124 | 362 |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
363 |
val tokens = |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
364 |
(ignored || |
61455 | 365 |
markup Keyword.is_document_heading Markup_Token markup_true || |
366 |
markup Keyword.is_document_body Markup_Env_Token markup_true || |
|
61456 | 367 |
markup Keyword.is_document_raw (Raw_Token o #2) "") >> single || |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
368 |
command || |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
369 |
(cmt || other) >> single; |
22124 | 370 |
|
371 |
||
372 |
(* spans *) |
|
373 |
||
59065 | 374 |
val is_eof = fn (_, (Basic_Token x, _)) => Token.is_eof x | _ => false; |
375 |
val stopper = Scan.stopper (K (NONE, (Basic_Token Token.eof, ("", 0)))) is_eof; |
|
22124 | 376 |
|
377 |
val cmd = Scan.one (is_some o fst); |
|
27732 | 378 |
val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2; |
22124 | 379 |
|
67439
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
380 |
val white_comments = Scan.many (white_comment_token o fst o snd); |
22124 | 381 |
val blank = Scan.one (blank_token o fst o snd); |
382 |
val newline = Scan.one (newline_token o fst o snd); |
|
383 |
val before_cmd = |
|
67439
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
384 |
Scan.option (newline -- white_comments) -- |
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
385 |
Scan.option (newline -- white_comments) -- |
78759a7bd874
more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents:
67429
diff
changeset
|
386 |
Scan.option (blank -- white_comments) -- cmd; |
22124 | 387 |
|
388 |
val span = |
|
389 |
Scan.repeat non_cmd -- cmd -- |
|
390 |
Scan.repeat (Scan.unless before_cmd non_cmd) -- |
|
391 |
Scan.option (newline >> (single o snd)) |
|
392 |
>> (fn (((toks1, (cmd, tok2)), toks3), tok4) => |
|
393 |
make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4)))); |
|
394 |
||
57080
0e5fa27d3293
strip trailing white space, to avoid notorious problems of jEdit with last line;
wenzelm
parents:
56548
diff
changeset
|
395 |
val spans = toks |
0e5fa27d3293
strip trailing white space, to avoid notorious problems of jEdit with last line;
wenzelm
parents:
56548
diff
changeset
|
396 |
|> take_suffix Token.is_space |> #1 |
0e5fa27d3293
strip trailing white space, to avoid notorious problems of jEdit with last line;
wenzelm
parents:
56548
diff
changeset
|
397 |
|> Source.of_list |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
398 |
|> Source.source' 0 Token.stopper (Scan.error (Scan.bulk tokens >> flat)) |
58864 | 399 |
|> Source.source stopper (Scan.error (Scan.bulk span)) |
22124 | 400 |
|> Source.exhaust; |
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
401 |
|
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
402 |
|
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
403 |
(* present commands *) |
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
404 |
|
67147 | 405 |
val document_tags = space_explode "," (Options.default_string \<^system_option>\<open>document_tags\<close>); |
67138 | 406 |
|
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
407 |
fun present_command tr span st st' = |
67191 | 408 |
Toplevel.setmp_thread_position tr |
67381 | 409 |
(present_span thy keywords document_tags span st st'); |
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
410 |
|
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
411 |
fun present _ [] = I |
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
412 |
| present st (((tr, st'), span) :: rest) = present_command tr span st st' #> present st' rest; |
22124 | 413 |
in |
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
414 |
if length command_results = length spans then |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
415 |
((NONE, []), NONE, true, [], (I, I)) |
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
416 |
|> present Toplevel.toplevel (command_results ~~ spans) |
22124 | 417 |
|> present_trailer |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
418 |
|> rev |
22124 | 419 |
else error "Messed-up outer syntax for presentation" |
420 |
end; |
|
421 |
||
422 |
end; |
|
423 |
||
424 |
||
425 |
||
67386 | 426 |
(** standard output operations **) |
22124 | 427 |
|
428 |
(* basic pretty printing *) |
|
429 |
||
61435 | 430 |
fun perhaps_trim ctxt = |
67386 | 431 |
not (Config.get ctxt Document_Antiquotation.thy_output_display) ? Symbol.trim_blanks; |
22124 | 432 |
|
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
433 |
fun pretty_text ctxt = |
61435 | 434 |
Pretty.chunks o map Pretty.str o map (perhaps_trim ctxt) o split_lines; |
22124 | 435 |
|
26710
f79aa228c582
pretty_term: no revert_skolems here, but auto_fixes (token translations will do the rest);
wenzelm
parents:
26455
diff
changeset
|
436 |
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t; |
24920 | 437 |
|
32898
e871d897969c
term styles also cover antiquotations term_type and typeof
haftmann
parents:
32890
diff
changeset
|
438 |
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of; |
e871d897969c
term styles also cover antiquotations term_type and typeof
haftmann
parents:
32890
diff
changeset
|
439 |
|
22124 | 440 |
|
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
441 |
(* default output *) |
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
442 |
|
61814
1ca1142e1711
clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents:
61748
diff
changeset
|
443 |
val str_of_source = space_implode " " o map Token.unparse o Token.args_of_src; |
22124 | 444 |
|
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
445 |
fun maybe_pretty_source pretty ctxt src xs = |
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
446 |
map (pretty ctxt) xs (*always pretty in order to exhibit errors!*) |
67386 | 447 |
|> (if Config.get ctxt Document_Antiquotation.thy_output_source |
448 |
then K [pretty_text ctxt (str_of_source src)] else I); |
|
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
449 |
|
67386 | 450 |
fun string_of_margin ctxt = |
451 |
Pretty.string_of_margin (Config.get ctxt Document_Antiquotation.thy_output_margin); |
|
59175
bf465f335e85
system option "pretty_margin" is superseded by "thy_output_margin";
wenzelm
parents:
59067
diff
changeset
|
452 |
|
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
453 |
fun output ctxt prts = |
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
454 |
prts |
67386 | 455 |
|> Config.get ctxt Document_Antiquotation.thy_output_quotes ? map Pretty.quote |
456 |
|> (if Config.get ctxt Document_Antiquotation.thy_output_display then |
|
457 |
map (Pretty.indent (Config.get ctxt Document_Antiquotation.thy_output_indent) |
|
458 |
#> string_of_margin ctxt #> Output.output) |
|
58716
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
459 |
#> space_implode "\\isasep\\isanewline%\n" |
61462 | 460 |
#> Latex.environment "isabelle" |
58716
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
461 |
else |
61877
276ad4354069
renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
wenzelm
parents:
61865
diff
changeset
|
462 |
map |
67386 | 463 |
((if Config.get ctxt Document_Antiquotation.thy_output_break |
464 |
then string_of_margin ctxt else Pretty.unformatted_string_of) #> Output.output) |
|
58716
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
465 |
#> space_implode "\\isasep\\isanewline%\n" |
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
466 |
#> enclose "\\isa{" "}"); |
22124 | 467 |
|
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
468 |
|
61619 | 469 |
(* verbatim text *) |
61473
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
wenzelm
parents:
61471
diff
changeset
|
470 |
|
61619 | 471 |
fun verbatim_text ctxt = |
67386 | 472 |
if Config.get ctxt Document_Antiquotation.thy_output_display then |
473 |
split_lines #> |
|
474 |
map (prefix (Symbol.spaces (Config.get ctxt Document_Antiquotation.thy_output_indent))) #> |
|
475 |
cat_lines #> |
|
61619 | 476 |
Latex.output_ascii #> Latex.environment "isabellett" |
477 |
else |
|
478 |
split_lines #> |
|
479 |
map (Latex.output_ascii #> enclose "\\isatt{" "}") #> |
|
480 |
space_implode "\\isasep\\isanewline%\n"; |
|
61491
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents:
61473
diff
changeset
|
481 |
|
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
482 |
end; |