author | wenzelm |
Sun, 06 Oct 2024 13:02:33 +0200 | |
changeset 81120 | 080beab27264 |
parent 80942 | 501ebf1fc308 |
child 81121 | 7cacedbddba7 |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/pretty.ML |
8806 | 2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
80892 | 3 |
Author: Makarius |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
4 |
|
80892 | 5 |
Generic pretty printing. |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
6 |
|
80892 | 7 |
The object to be printed is given as a tree with blocks and breaks. A block |
8 |
causes alignment and may specify additional indentation and markup. A break |
|
9 |
inserts a newline if the text until the next break is too long to fit on the |
|
10 |
current line. After the newline, text is indented to the level of the |
|
11 |
enclosing block. Normally, if a block is broken then all enclosing blocks will |
|
12 |
also be broken. |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
13 |
|
80892 | 14 |
References: |
15 |
||
16 |
* L. C. Paulson, "ML for the Working Programmer" (1996), |
|
17 |
2nd edition, Cambridge University Press. |
|
18 |
Section 8.10 "A pretty printer" |
|
19 |
https://www.cl.cam.ac.uk/~lp15/MLbook/PDF/chapter8.pdf |
|
20 |
||
21 |
* D. C. Oppen, "Pretty Printing", |
|
22 |
ACM Transactions on Programming Languages and Systems (1980), 465-483. |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
23 |
*) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
24 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
25 |
signature PRETTY = |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
26 |
sig |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
27 |
type T |
80810
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
28 |
val to_ML: T -> ML_Pretty.pretty |
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
29 |
val from_ML: ML_Pretty.pretty -> T |
80878 | 30 |
val blk: int * T list -> T |
31 |
val block0: T list -> T |
|
32 |
val block1: T list -> T |
|
33 |
val block: T list -> T |
|
80938
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
34 |
type 'a block = {markup: 'a, consistent: bool, indent: int} |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
35 |
val make_block: Markup.output block -> T list -> T |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
36 |
val markup_block: Markup.T block -> T list -> T |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
37 |
val markup: Markup.T -> T list -> T |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
38 |
val mark: Markup.T -> T -> T |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
39 |
val markup_blocks: Markup.T list block -> T list -> T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
40 |
val str: string -> T |
80813
9dd4dcb08d37
clarified signature, following 1f718be3608b: Pretty.str is now value-oriented;
wenzelm
parents:
80812
diff
changeset
|
41 |
val dots: T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
42 |
val brk: int -> T |
61862
e2a9e46ac0fb
support pretty break indent, like underlying ML systems;
wenzelm
parents:
56334
diff
changeset
|
43 |
val brk_indent: int -> int -> T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
44 |
val fbrk: T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
45 |
val breaks: T list -> T list |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
46 |
val fbreaks: T list -> T list |
23645 | 47 |
val strs: string list -> T |
42266 | 48 |
val mark_str: Markup.T * string -> T |
49 |
val marks_str: Markup.T list * string -> T |
|
80873 | 50 |
val mark_position: Position.T -> T -> T |
51 |
val mark_str_position: Position.T * string -> T |
|
51570
3633828d80fc
basic support for Pretty.item, which is considered as logical markup and interpreted in Isabelle/Scala, but ignored elsewhere (TTY, latex etc.);
wenzelm
parents:
51511
diff
changeset
|
52 |
val item: T list -> T |
52693 | 53 |
val text_fold: T list -> T |
55763 | 54 |
val keyword1: string -> T |
55 |
val keyword2: string -> T |
|
50162 | 56 |
val text: string -> T list |
57 |
val paragraph: T list -> T |
|
58 |
val para: string -> T |
|
18802 | 59 |
val quote: T -> T |
55033 | 60 |
val cartouche: T -> T |
18802 | 61 |
val separate: string -> T list -> T list |
62 |
val commas: T list -> T list |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
63 |
val enclose: string -> string -> T list -> T |
18802 | 64 |
val enum: string -> string -> string -> T list -> T |
30620 | 65 |
val position: Position.T -> T |
71465
910a081cca74
more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents:
69345
diff
changeset
|
66 |
val here: Position.T -> T list |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
67 |
val list: string -> string -> T list -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
68 |
val str_list: string -> string -> string list -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
69 |
val big_list: string -> T list -> T |
9730 | 70 |
val indent: int -> T -> T |
23645 | 71 |
val unbreakable: T -> T |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
72 |
type output_ops = |
80855
301612847ea3
further clarification of print_mode: PIDE markup depends on "isabelle_process" alone, Latex is stateless;
wenzelm
parents:
80854
diff
changeset
|
73 |
{symbolic: bool, |
301612847ea3
further clarification of print_mode: PIDE markup depends on "isabelle_process" alone, Latex is stateless;
wenzelm
parents:
80854
diff
changeset
|
74 |
output: string -> Output.output * int, |
80829
bdae6195a287
clarified Pretty.markup_block: use value-oriented YXML.output_markup, with final re-interpretation via print_mode in output_tree;
wenzelm
parents:
80825
diff
changeset
|
75 |
markup: Markup.output -> Markup.output, |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
76 |
indent: string -> int -> Output.output, |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
77 |
margin: int} |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
78 |
val output_ops: int option -> output_ops |
80854 | 79 |
val pure_output_ops: int option -> output_ops |
80830 | 80 |
val markup_output_ops: int option -> output_ops |
80855
301612847ea3
further clarification of print_mode: PIDE markup depends on "isabelle_process" alone, Latex is stateless;
wenzelm
parents:
80854
diff
changeset
|
81 |
val symbolic_output_ops: output_ops |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
82 |
val symbolic_output: T -> Bytes.T |
80844 | 83 |
val symbolic_string_of: T -> string |
84 |
val unformatted_string_of: T -> string |
|
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
85 |
val output: output_ops -> T -> Bytes.T |
80841 | 86 |
val string_of_ops: output_ops -> T -> string |
23645 | 87 |
val string_of: T -> string |
80863 | 88 |
val pure_string_of: T -> string |
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
89 |
val writeln: T -> unit |
56334
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
90 |
val markup_chunks: Markup.T -> T list -> T |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
91 |
val chunks: T list -> T |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
92 |
val chunks2: T list -> T |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
93 |
val block_enclose: T * T -> T list -> T |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
94 |
val writeln_chunks: T list -> unit |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
95 |
val writeln_chunks2: T list -> unit |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
96 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
97 |
|
23617 | 98 |
structure Pretty: PRETTY = |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
99 |
struct |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
100 |
|
80844 | 101 |
(** Pretty.T **) |
102 |
||
103 |
(* abstype: ML_Pretty.pretty without (op =) *) |
|
104 |
||
105 |
abstype T = T of ML_Pretty.pretty |
|
106 |
with |
|
107 |
fun to_ML (T prt) = prt; |
|
108 |
val from_ML = T; |
|
109 |
end; |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
110 |
|
62785
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
wenzelm
parents:
62784
diff
changeset
|
111 |
val force_nat = Integer.max 0; |
80810
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
112 |
val short_nat = FixedInt.fromInt o force_nat; |
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
113 |
val long_nat = force_nat o FixedInt.toInt; |
62785
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
wenzelm
parents:
62784
diff
changeset
|
114 |
|
80810
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
115 |
|
80878 | 116 |
(* blocks *) |
23645 | 117 |
|
80938
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
118 |
fun blk (indent, body) = |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
119 |
from_ML (ML_Pretty.PrettyBlock (short_nat indent, false, [], map to_ML body)); |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
120 |
|
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
121 |
fun block0 body = blk (0, body); |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
122 |
fun block1 body = blk (1, body); |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
123 |
fun block body = blk (2, body); |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
124 |
|
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
125 |
|
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
126 |
(* blocks with markup *) |
a119154a5f27
minor performance tuning: more direct blocks without markup;
wenzelm
parents:
80937
diff
changeset
|
127 |
|
80878 | 128 |
type 'a block = {markup: 'a, consistent: bool, indent: int} |
129 |
||
130 |
fun make_block ({markup, consistent, indent}: Markup.output block) body = |
|
80937 | 131 |
let val context = ML_Pretty.markup_context markup |
132 |
in from_ML (ML_Pretty.PrettyBlock (short_nat indent, consistent, context, map to_ML body)) end; |
|
61864 | 133 |
|
80878 | 134 |
fun markup_block ({markup, consistent, indent}: Markup.T block) body = |
80829
bdae6195a287
clarified Pretty.markup_block: use value-oriented YXML.output_markup, with final re-interpretation via print_mode in output_tree;
wenzelm
parents:
80825
diff
changeset
|
135 |
make_block {markup = YXML.output_markup markup, consistent = consistent, indent = indent} body; |
61864 | 136 |
|
80936 | 137 |
fun markup m = markup_block {markup = m, consistent = false, indent = 0}; |
138 |
||
139 |
fun mark m prt = if m = Markup.empty then prt else markup m [prt]; |
|
23645 | 140 |
|
80937 | 141 |
fun markup_blocks ({markup, consistent, indent}: Markup.T list block) body = |
80942
501ebf1fc308
minor performance tuning for blocks without markup;
wenzelm
parents:
80938
diff
changeset
|
142 |
if forall Markup.is_empty markup andalso not consistent then blk (indent, body) |
501ebf1fc308
minor performance tuning for blocks without markup;
wenzelm
parents:
80938
diff
changeset
|
143 |
else |
501ebf1fc308
minor performance tuning for blocks without markup;
wenzelm
parents:
80938
diff
changeset
|
144 |
let |
501ebf1fc308
minor performance tuning for blocks without markup;
wenzelm
parents:
80938
diff
changeset
|
145 |
val markups = filter_out Markup.is_empty markup; |
501ebf1fc308
minor performance tuning for blocks without markup;
wenzelm
parents:
80938
diff
changeset
|
146 |
val (ms, m) = if null markups then ([], Markup.empty) else split_last markups; |
501ebf1fc308
minor performance tuning for blocks without markup;
wenzelm
parents:
80938
diff
changeset
|
147 |
in |
501ebf1fc308
minor performance tuning for blocks without markup;
wenzelm
parents:
80938
diff
changeset
|
148 |
fold_rev mark ms |
501ebf1fc308
minor performance tuning for blocks without markup;
wenzelm
parents:
80938
diff
changeset
|
149 |
(markup_block {markup = m, consistent = consistent, indent = indent} body) |
501ebf1fc308
minor performance tuning for blocks without markup;
wenzelm
parents:
80938
diff
changeset
|
150 |
end; |
80937 | 151 |
|
80878 | 152 |
|
153 |
(* breaks *) |
|
154 |
||
155 |
fun brk_indent wd ind = from_ML (ML_Pretty.PrettyBreak (short_nat wd, short_nat ind)); |
|
156 |
fun brk wd = brk_indent wd 0; |
|
157 |
val fbrk = from_ML ML_Pretty.PrettyLineBreak; |
|
158 |
||
159 |
fun breaks prts = Library.separate (brk 1) prts; |
|
160 |
fun fbreaks prts = Library.separate fbrk prts; |
|
161 |
||
162 |
||
163 |
(* derived operations to create formatting expressions *) |
|
164 |
||
165 |
val str = from_ML o ML_Pretty.str; |
|
166 |
val dots = from_ML ML_Pretty.dots; |
|
167 |
||
23645 | 168 |
val strs = block o breaks o map str; |
169 |
||
42266 | 170 |
fun mark_str (m, s) = mark m (str s); |
171 |
fun marks_str (ms, s) = fold_rev mark ms (str s); |
|
172 |
||
80875 | 173 |
val mark_position = mark o Position.markup; |
174 |
fun mark_str_position (pos, s) = mark_str (Position.markup pos, s); |
|
80873 | 175 |
|
51570
3633828d80fc
basic support for Pretty.item, which is considered as logical markup and interpreted in Isabelle/Scala, but ignored elsewhere (TTY, latex etc.);
wenzelm
parents:
51511
diff
changeset
|
176 |
val item = markup Markup.item; |
52693 | 177 |
val text_fold = markup Markup.text_fold; |
51570
3633828d80fc
basic support for Pretty.item, which is considered as logical markup and interpreted in Isabelle/Scala, but ignored elsewhere (TTY, latex etc.);
wenzelm
parents:
51511
diff
changeset
|
178 |
|
55763 | 179 |
fun keyword1 name = mark_str (Markup.keyword1, name); |
180 |
fun keyword2 name = mark_str (Markup.keyword2, name); |
|
23645 | 181 |
|
50162 | 182 |
val text = breaks o map str o Symbol.explode_words; |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
50162
diff
changeset
|
183 |
val paragraph = markup Markup.paragraph; |
50162 | 184 |
val para = paragraph o text; |
185 |
||
80328 | 186 |
fun quote prt = block1 [str "\"", prt, str "\""]; |
187 |
fun cartouche prt = block1 [str Symbol.open_, prt, str Symbol.close]; |
|
23645 | 188 |
|
189 |
fun separate sep prts = |
|
190 |
flat (Library.separate [str sep, brk 1] (map single prts)); |
|
191 |
||
192 |
val commas = separate ","; |
|
193 |
||
194 |
fun enclose lpar rpar prts = |
|
195 |
block (str lpar :: (prts @ [str rpar])); |
|
196 |
||
197 |
fun enum sep lpar rpar prts = enclose lpar rpar (separate sep prts); |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
198 |
|
30620 | 199 |
val position = |
80504 | 200 |
enum "," "{" "}" o map (str o Properties.print_eq) o Position.properties_of; |
30620 | 201 |
|
71465
910a081cca74
more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents:
69345
diff
changeset
|
202 |
fun here pos = |
80873 | 203 |
let val (s1, s2) = Position.here_strs pos |
204 |
in if s2 = "" then [] else [str s1, mark_str_position (pos, s2)] end; |
|
71465
910a081cca74
more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents:
69345
diff
changeset
|
205 |
|
23645 | 206 |
val list = enum ","; |
207 |
fun str_list lpar rpar strs = list lpar rpar (map str strs); |
|
208 |
||
209 |
fun big_list name prts = block (fbreaks (str name :: prts)); |
|
210 |
||
211 |
fun indent 0 prt = prt |
|
80328 | 212 |
| indent n prt = block0 [str (Symbol.spaces n), prt]; |
23645 | 213 |
|
80810
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
214 |
val unbreakable = |
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
215 |
let |
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
216 |
fun unbreak (ML_Pretty.PrettyBlock (ind, consistent, context, body)) = |
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
217 |
ML_Pretty.PrettyBlock (ind, consistent, context, map unbreak body) |
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
218 |
| unbreak (ML_Pretty.PrettyBreak (wd, _)) = |
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
219 |
ML_Pretty.str (Symbol.spaces (long_nat wd)) |
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
220 |
| unbreak prt = prt; |
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
221 |
in to_ML #> unbreak #> from_ML end; |
23645 | 222 |
|
223 |
||
224 |
||
225 |
(** formatting **) |
|
226 |
||
80861
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
wenzelm
parents:
80856
diff
changeset
|
227 |
(* output operations *) |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
228 |
|
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
229 |
type output_ops = |
80855
301612847ea3
further clarification of print_mode: PIDE markup depends on "isabelle_process" alone, Latex is stateless;
wenzelm
parents:
80854
diff
changeset
|
230 |
{symbolic: bool, |
301612847ea3
further clarification of print_mode: PIDE markup depends on "isabelle_process" alone, Latex is stateless;
wenzelm
parents:
80854
diff
changeset
|
231 |
output: string -> Output.output * int, |
80829
bdae6195a287
clarified Pretty.markup_block: use value-oriented YXML.output_markup, with final re-interpretation via print_mode in output_tree;
wenzelm
parents:
80825
diff
changeset
|
232 |
markup: Markup.output -> Markup.output, |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
233 |
indent: string -> int -> Output.output, |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
234 |
margin: int}; |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
235 |
|
80861
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
wenzelm
parents:
80856
diff
changeset
|
236 |
fun make_output_ops {symbolic, markup} opt_margin : output_ops = |
80855
301612847ea3
further clarification of print_mode: PIDE markup depends on "isabelle_process" alone, Latex is stateless;
wenzelm
parents:
80854
diff
changeset
|
237 |
{symbolic = symbolic, |
301612847ea3
further clarification of print_mode: PIDE markup depends on "isabelle_process" alone, Latex is stateless;
wenzelm
parents:
80854
diff
changeset
|
238 |
output = fn s => (s, size s), |
80854 | 239 |
markup = markup, |
80861
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
wenzelm
parents:
80856
diff
changeset
|
240 |
indent = fn _ => Symbol.spaces, |
80854 | 241 |
margin = ML_Pretty.get_margin opt_margin}; |
80849
e3a419073736
drop pointless print_mode operations Output.output / Output.escape;
wenzelm
parents:
80848
diff
changeset
|
242 |
|
80861
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
wenzelm
parents:
80856
diff
changeset
|
243 |
val pure_output_ops = make_output_ops {symbolic = false, markup = K Markup.no_output}; |
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
wenzelm
parents:
80856
diff
changeset
|
244 |
val markup_output_ops = make_output_ops {symbolic = false, markup = I}; |
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
wenzelm
parents:
80856
diff
changeset
|
245 |
val symbolic_output_ops = make_output_ops {symbolic = true, markup = I} NONE; |
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
wenzelm
parents:
80856
diff
changeset
|
246 |
|
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
wenzelm
parents:
80856
diff
changeset
|
247 |
fun output_ops opt_margin = |
80867 | 248 |
if Print_Mode.PIDE_enabled () then symbolic_output_ops |
80861
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
wenzelm
parents:
80856
diff
changeset
|
249 |
else pure_output_ops opt_margin; |
80829
bdae6195a287
clarified Pretty.markup_block: use value-oriented YXML.output_markup, with final re-interpretation via print_mode in output_tree;
wenzelm
parents:
80825
diff
changeset
|
250 |
|
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
251 |
fun output_newline (ops: output_ops) = #1 (#output ops "\n"); |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
252 |
|
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
253 |
fun output_spaces (ops: output_ops) = #output ops o Symbol.spaces; |
80843 | 254 |
fun output_spaces_buffer ops = Buffer.add o #1 o output_spaces ops; |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
255 |
fun output_spaces_bytes ops = Bytes.add o #1 o output_spaces ops; |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
256 |
|
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
257 |
|
80844 | 258 |
(* output tree *) |
259 |
||
260 |
datatype tree = |
|
261 |
Block of Markup.output * bool * int * tree list * int |
|
262 |
(*markup output, consistent, indentation, body, length*) |
|
263 |
| Break of bool * int * int (*mandatory flag, width if not taken, extra indentation if taken*) |
|
264 |
| Str of Output.output * int; (*string output, length*) |
|
265 |
||
266 |
fun tree_length (Block (_, _, _, _, len)) = len |
|
267 |
| tree_length (Break (_, wd, _)) = wd |
|
268 |
| tree_length (Str (_, len)) = len; |
|
269 |
||
270 |
local |
|
271 |
||
272 |
fun block_length indent = |
|
273 |
let |
|
80936 | 274 |
fun block_len len body = |
80844 | 275 |
let |
80936 | 276 |
val (line, rest) = chop_prefix (fn Break (true, _, _) => false | _ => true) body; |
80844 | 277 |
val len' = Int.max (fold (Integer.add o tree_length) line 0, len); |
278 |
in |
|
279 |
(case rest of |
|
280 |
Break (true, _, ind) :: rest' => |
|
281 |
block_len len' (Break (false, indent + ind, 0) :: rest') |
|
282 |
| [] => len') |
|
283 |
end; |
|
284 |
in block_len 0 end; |
|
285 |
||
286 |
val fbreak = Break (true, 1, 0); |
|
287 |
||
288 |
in |
|
289 |
||
290 |
fun output_tree (ops: output_ops) make_length = |
|
291 |
let |
|
292 |
fun out (ML_Pretty.PrettyBlock (ind, consistent, context, body)) = |
|
293 |
let |
|
80856 | 294 |
val markup = #markup ops (ML_Pretty.context_markup context); |
80844 | 295 |
val indent = long_nat ind; |
296 |
val body' = map out body; |
|
297 |
val len = if make_length then block_length indent body' else ~1; |
|
80856 | 298 |
in Block (markup, consistent, indent, body', len) end |
80844 | 299 |
| out (ML_Pretty.PrettyBreak (wd, ind)) = Break (false, long_nat wd, long_nat ind) |
300 |
| out ML_Pretty.PrettyLineBreak = fbreak |
|
301 |
| out (ML_Pretty.PrettyString s) = Str (#output ops s ||> force_nat) |
|
302 |
| out (ML_Pretty.PrettyStringWithWidth (s, n)) = Str (s, long_nat n); |
|
303 |
in out o to_ML end; |
|
304 |
||
305 |
end; |
|
306 |
||
307 |
||
23645 | 308 |
(* formatted output *) |
309 |
||
310 |
local |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
311 |
|
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
312 |
type text = {tx: Bytes.T, ind: Buffer.T, pos: int, nl: int}; |
17756 | 313 |
|
314 |
val empty: text = |
|
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
315 |
{tx = Bytes.empty, |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
316 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
317 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
318 |
nl = 0}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
319 |
|
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
320 |
fun newline s {tx, ind = _, pos = _, nl} : text = |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
321 |
{tx = Bytes.add s tx, |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
322 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
323 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
324 |
nl = nl + 1}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
325 |
|
23628
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
326 |
fun control s {tx, ind, pos: int, nl} : text = |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
327 |
{tx = Bytes.add s tx, |
23628
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
328 |
ind = ind, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
329 |
pos = pos, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
330 |
nl = nl}; |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
331 |
|
17756 | 332 |
fun string (s, len) {tx, ind, pos: int, nl} : text = |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
333 |
{tx = Bytes.add s tx, |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
334 |
ind = Buffer.add s ind, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
335 |
pos = pos + len, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
336 |
nl = nl}; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
337 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
338 |
(*Add the lengths of the expressions until the next Break; if no Break then |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
339 |
include "after", to account for text following this block.*) |
61864 | 340 |
fun break_dist (Break _ :: _, _) = 0 |
80936 | 341 |
| break_dist (prt :: prts, after) = tree_length prt + break_dist (prts, after) |
61864 | 342 |
| break_dist ([], after) = after; |
343 |
||
80936 | 344 |
val force_break = fn Break (false, wd, ind) => Break (true, wd, ind) | prt => prt; |
61864 | 345 |
val force_all = map force_break; |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
346 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
347 |
(*Search for the next break (at this or higher levels) and force it to occur.*) |
61864 | 348 |
fun force_next [] = [] |
80936 | 349 |
| force_next ((prt as Break _) :: prts) = force_break prt :: prts |
350 |
| force_next (prt :: prts) = prt :: force_next prts; |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
351 |
|
23645 | 352 |
in |
19266 | 353 |
|
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
354 |
fun format_tree (ops: output_ops) input = |
36745 | 355 |
let |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
356 |
val margin = #margin ops; |
36745 | 357 |
val breakgain = margin div 20; (*minimum added space required of a break*) |
358 |
val emergencypos = margin div 2; (*position too far to right*) |
|
359 |
||
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
360 |
val linebreak = newline (output_newline ops); |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
361 |
val blanks = string o output_spaces ops; |
80843 | 362 |
val blanks_buffer = output_spaces_buffer ops; |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
363 |
|
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
364 |
fun indentation (buf, len) {tx, ind, pos, nl} : text = |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
365 |
let val s = Buffer.content buf in |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
366 |
{tx = Bytes.add (#indent ops s len) tx, |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
367 |
ind = Buffer.add s ind, |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
368 |
pos = pos + len, |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
369 |
nl = nl} |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
370 |
end; |
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
371 |
|
80936 | 372 |
(*blockin is the indentation of the current block; |
36745 | 373 |
after is the width of the following context until next break.*) |
374 |
fun format ([], _, _) text = text |
|
80936 | 375 |
| format (prt :: prts, block as (_, blockin), after) (text as {ind, pos, nl, ...}) = |
376 |
(case prt of |
|
61869 | 377 |
Block ((bg, en), consistent, indent, bes, blen) => |
36745 | 378 |
let |
379 |
val pos' = pos + indent; |
|
380 |
val pos'' = pos' mod emergencypos; |
|
381 |
val block' = |
|
80843 | 382 |
if pos' < emergencypos then (ind |> blanks_buffer indent, pos') |
383 |
else (blanks_buffer pos'' Buffer.empty, pos''); |
|
80936 | 384 |
val d = break_dist (prts, after) |
61864 | 385 |
val bes' = if consistent andalso pos + blen > margin - d then force_all bes else bes; |
36745 | 386 |
val btext: text = text |
387 |
|> control bg |
|
61864 | 388 |
|> format (bes', block', d) |
36745 | 389 |
|> control en; |
390 |
(*if this block was broken then force the next break*) |
|
80936 | 391 |
val prts' = if nl < #nl btext then force_next prts else prts; |
392 |
in format (prts', block, after) btext end |
|
61862
e2a9e46ac0fb
support pretty break indent, like underlying ML systems;
wenzelm
parents:
56334
diff
changeset
|
393 |
| Break (force, wd, ind) => |
36745 | 394 |
(*no break if text to next break fits on this line |
395 |
or if breaking would add only breakgain to space*) |
|
80936 | 396 |
format (prts, block, after) |
36745 | 397 |
(if not force andalso |
80936 | 398 |
pos + wd <= Int.max (margin - break_dist (prts, after), blockin + breakgain) |
61864 | 399 |
then text |> blanks wd (*just insert wd blanks*) |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
400 |
else text |> linebreak |> indentation block |> blanks ind) |
80936 | 401 |
| Str str => format (prts, block, after) (string str text)); |
36745 | 402 |
in |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
403 |
#tx (format ([output_tree ops true input], (Buffer.empty, 0), 0) empty) |
36745 | 404 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
405 |
|
23645 | 406 |
end; |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
407 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
408 |
|
80844 | 409 |
|
410 |
(** no formatting **) |
|
411 |
||
412 |
(* symbolic output: XML markup for blocks/breaks + other markup *) |
|
413 |
||
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
414 |
val symbolic_output = |
80829
bdae6195a287
clarified Pretty.markup_block: use value-oriented YXML.output_markup, with final re-interpretation via print_mode in output_tree;
wenzelm
parents:
80825
diff
changeset
|
415 |
let |
80855
301612847ea3
further clarification of print_mode: PIDE markup depends on "isabelle_process" alone, Latex is stateless;
wenzelm
parents:
80854
diff
changeset
|
416 |
val ops = symbolic_output_ops; |
18802 | 417 |
|
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
418 |
fun markup_bytes m body = |
80829
bdae6195a287
clarified Pretty.markup_block: use value-oriented YXML.output_markup, with final re-interpretation via print_mode in output_tree;
wenzelm
parents:
80825
diff
changeset
|
419 |
let val (bg, en) = #markup ops (YXML.output_markup m) |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
420 |
in Bytes.add bg #> body #> Bytes.add en end; |
80822 | 421 |
|
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
422 |
fun out (Block ((bg, en), _, _, [], _)) = Bytes.add bg #> Bytes.add en |
61869 | 423 |
| out (Block ((bg, en), consistent, indent, prts, _)) = |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
424 |
Bytes.add bg #> |
81120 | 425 |
markup_bytes (Markup.block {consistent = consistent, indent = indent}) (fold out prts) #> |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
426 |
Bytes.add en |
80843 | 427 |
| out (Break (false, wd, ind)) = |
81120 | 428 |
markup_bytes (Markup.break {width = wd, indent = ind}) (output_spaces_bytes ops wd) |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
429 |
| out (Break (true, _, _)) = Bytes.add (output_newline ops) |
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
430 |
| out (Str (s, _)) = Bytes.add s; |
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
431 |
in Bytes.build o out o output_tree ops false end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
432 |
|
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
433 |
val symbolic_string_of = Bytes.content o symbolic_output; |
80844 | 434 |
|
435 |
||
436 |
(* unformatted output: other markup only *) |
|
437 |
||
80834 | 438 |
fun unformatted ops = |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
439 |
let |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
440 |
fun out (Block ((bg, en), _, _, prts, _)) = Bytes.add bg #> fold out prts #> Bytes.add en |
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
441 |
| out (Break (_, wd, _)) = output_spaces_bytes ops wd |
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
442 |
| out (Str (s, _)) = Bytes.add s; |
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
443 |
in Bytes.build o out o output_tree ops false end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
444 |
|
80844 | 445 |
fun unformatted_string_of prt = |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
446 |
Bytes.content (unformatted (output_ops NONE) prt); |
80844 | 447 |
|
30624
e755b8b76365
simplified datatype ML_Pretty.pretty: model Isabelle not Poly/ML;
wenzelm
parents:
30620
diff
changeset
|
448 |
|
36748 | 449 |
(* output interfaces *) |
30620 | 450 |
|
80855
301612847ea3
further clarification of print_mode: PIDE markup depends on "isabelle_process" alone, Latex is stateless;
wenzelm
parents:
80854
diff
changeset
|
451 |
fun output ops = if #symbolic ops then symbolic_output else format_tree ops; |
23645 | 452 |
|
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
453 |
fun string_of_ops ops = Bytes.content o output ops; |
80841 | 454 |
fun string_of prt = string_of_ops (output_ops NONE) prt; |
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
455 |
|
80863 | 456 |
val pure_string_of = string_of_ops (pure_output_ops NONE); |
457 |
||
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
458 |
fun writeln prt = |
80848
df85df6315af
clarified signature: prefer explicit type Bytes.T;
wenzelm
parents:
80846
diff
changeset
|
459 |
Output.writelns (Bytes.contents (output (output_ops NONE) prt)); |
80825
b866d1510bd0
clarified signature: more explicit type output_ops: default via print_mode;
wenzelm
parents:
80824
diff
changeset
|
460 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
461 |
|
56334
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
462 |
(* chunks *) |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
463 |
|
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
464 |
fun markup_chunks m prts = markup m (fbreaks (map (text_fold o single) prts)); |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
465 |
val chunks = markup_chunks Markup.empty; |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
466 |
|
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
467 |
fun chunks2 prts = |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
468 |
(case try split_last prts of |
80328 | 469 |
NONE => block0 [] |
56334
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
470 |
| SOME (prefix, last) => |
80328 | 471 |
block0 (maps (fn prt => [text_fold [prt, fbrk], fbrk]) prefix @ [text_fold [last]])); |
56334
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
472 |
|
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
473 |
fun block_enclose (prt1, prt2) prts = chunks [block (fbreaks (prt1 :: prts)), prt2]; |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
474 |
|
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
475 |
fun string_of_text_fold prt = string_of prt |> Markup.markup Markup.text_fold; |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
476 |
|
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
477 |
fun writeln_chunks prts = |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
478 |
Output.writelns (Library.separate "\n" (map string_of_text_fold prts)); |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
479 |
|
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
480 |
fun writeln_chunks2 prts = |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
481 |
(case try split_last prts of |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
482 |
NONE => () |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
483 |
| SOME (prefix, last) => |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
484 |
(map (fn prt => Markup.markup Markup.text_fold (string_of prt ^ "\n") ^ "\n") prefix @ |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
485 |
[string_of_text_fold last]) |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
486 |
|> Output.writelns); |
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
487 |
|
80810
1f718be3608b
clarified Pretty.T vs. output tree (following Isabelle/Scala): Output.output_width (via print_mode) happens during formatting, instead of construction;
wenzelm
parents:
80809
diff
changeset
|
488 |
end; |
56334
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents:
55918
diff
changeset
|
489 |
|
80844 | 490 |
|
491 |
||
492 |
(** back-patching **) |
|
62899
845ed4584e21
clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents:
62823
diff
changeset
|
493 |
|
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80805
diff
changeset
|
494 |
structure ML_Pretty: ML_PRETTY = |
62899
845ed4584e21
clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents:
62823
diff
changeset
|
495 |
struct |
845ed4584e21
clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents:
62823
diff
changeset
|
496 |
open ML_Pretty; |
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80805
diff
changeset
|
497 |
val string_of = Pretty.string_of o Pretty.from_ML; |
62899
845ed4584e21
clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents:
62823
diff
changeset
|
498 |
end; |
80812 | 499 |
|
500 |
||
501 |
||
502 |
(** toplevel pretty printing **) |
|
503 |
||
504 |
val _ = ML_system_pp (fn d => fn _ => ML_Pretty.prune (d + 1) o Pretty.to_ML o Pretty.quote); |
|
505 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_ML o Pretty.position); |
|
506 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_ML o Pretty.mark_str o Path.print_markup); |
|
507 |
||
508 |
val _ = |
|
509 |
ML_system_pp (fn _ => fn _ => fn t => |
|
510 |
ML_Pretty.str ("<thread " ^ quote (Isabelle_Thread.print t) ^ |
|
511 |
(if Isabelle_Thread.is_active t then "" else " (inactive)") ^ ">")); |
|
512 |
||
513 |
val _ = |
|
514 |
ML_system_pp (fn _ => fn _ => fn bytes => |
|
515 |
ML_Pretty.str |
|
516 |
(if Bytes.is_empty bytes then "Bytes.empty" |
|
517 |
else "Bytes {size = " ^ string_of_int (Bytes.size bytes) ^ "}")); |