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