author | wenzelm |
Wed, 26 Feb 2014 10:53:19 +0100 | |
changeset 55763 | 4b3907cb5654 |
parent 55033 | 8e8243975860 |
child 55918 | 41e06ec17604 |
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 |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
15 |
is broken then all enclosing blocks will also be broken. Only "inconsistent |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
16 |
breaks" are provided. |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
17 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
18 |
The stored length of a block is used in breakdist (to treat each inner block as |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
19 |
a unit for breaking). |
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 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
22 |
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
|
23 |
sig |
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
24 |
val spaces: int -> string |
40131
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
25 |
val default_indent: string -> int -> Output.output |
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
26 |
val add_mode: string -> (string -> int -> Output.output) -> unit |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
27 |
type T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
28 |
val str: string -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
29 |
val brk: int -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
30 |
val fbrk: T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
31 |
val breaks: T list -> T list |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
32 |
val fbreaks: T list -> T list |
23645 | 33 |
val blk: int * T list -> T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
34 |
val block: T list -> T |
23645 | 35 |
val strs: string list -> T |
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
36 |
val raw_markup: Output.output * Output.output -> int * T list -> T |
23617 | 37 |
val markup: Markup.T -> T list -> T |
26703 | 38 |
val mark: Markup.T -> T -> T |
42266 | 39 |
val mark_str: Markup.T * string -> T |
40 |
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
|
41 |
val item: T list -> T |
52693 | 42 |
val text_fold: T list -> T |
55763 | 43 |
val keyword1: string -> T |
44 |
val keyword2: string -> T |
|
45 |
val keyword3: string -> T |
|
50162 | 46 |
val text: string -> T list |
47 |
val paragraph: T list -> T |
|
48 |
val para: string -> T |
|
23638 | 49 |
val markup_chunks: Markup.T -> T list -> T |
18802 | 50 |
val chunks: T list -> T |
19266 | 51 |
val chunks2: T list -> T |
23617 | 52 |
val block_enclose: T * T -> T list -> T |
18802 | 53 |
val quote: T -> T |
54 |
val backquote: T -> T |
|
55033 | 55 |
val cartouche: T -> T |
18802 | 56 |
val separate: string -> T list -> T list |
57 |
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
|
58 |
val enclose: string -> string -> T list -> T |
18802 | 59 |
val enum: string -> string -> string -> T list -> T |
30620 | 60 |
val position: Position.T -> T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
val big_list: string -> T list -> T |
9730 | 64 |
val indent: int -> T -> T |
23645 | 65 |
val unbreakable: T -> T |
36745 | 66 |
val margin_default: int Unsynchronized.ref |
23645 | 67 |
val symbolicN: string |
36745 | 68 |
val output_buffer: int option -> T -> Buffer.T |
40131
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
69 |
val output: int option -> T -> Output.output |
36745 | 70 |
val string_of_margin: int -> T -> string |
23645 | 71 |
val string_of: T -> string |
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
72 |
val writeln: T -> unit |
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
73 |
val symbolic_output: T -> Output.output |
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49554
diff
changeset
|
74 |
val symbolic_string_of: T -> string |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
75 |
val str_of: T -> string |
36748 | 76 |
val to_ML: T -> ML_Pretty.pretty |
77 |
val from_ML: ML_Pretty.pretty -> T |
|
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
78 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
79 |
|
23617 | 80 |
structure Pretty: PRETTY = |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
81 |
struct |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
82 |
|
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
83 |
(** spaces **) |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
84 |
|
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
85 |
local |
51491 | 86 |
val small_spaces = Vector.tabulate (65, fn i => replicate_string i Symbol.space); |
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
87 |
in |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
88 |
fun spaces k = |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
89 |
if k < 64 then Vector.sub (small_spaces, k) |
51491 | 90 |
else |
91 |
replicate_string (k div 64) (Vector.sub (small_spaces, 64)) ^ |
|
92 |
Vector.sub (small_spaces, k mod 64); |
|
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
93 |
end; |
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
94 |
|
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
95 |
|
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
96 |
|
23617 | 97 |
(** print mode operations **) |
98 |
||
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
99 |
fun default_indent (_: string) = spaces; |
23617 | 100 |
|
101 |
local |
|
23698 | 102 |
val default = {indent = default_indent}; |
43684 | 103 |
val modes = Synchronized.var "Pretty.modes" (Symtab.make [("", default)]); |
23617 | 104 |
in |
43684 | 105 |
fun add_mode name indent = |
46894
e2ad717ec889
allow redefining pretty/markup modes (not output due to bootstrap issues) -- to support reloading of theory src/HOL/src/Tools/Code_Generator;
wenzelm
parents:
45666
diff
changeset
|
106 |
Synchronized.change modes (fn tab => |
e2ad717ec889
allow redefining pretty/markup modes (not output due to bootstrap issues) -- to support reloading of theory src/HOL/src/Tools/Code_Generator;
wenzelm
parents:
45666
diff
changeset
|
107 |
(if not (Symtab.defined tab name) then () |
e2ad717ec889
allow redefining pretty/markup modes (not output due to bootstrap issues) -- to support reloading of theory src/HOL/src/Tools/Code_Generator;
wenzelm
parents:
45666
diff
changeset
|
108 |
else warning ("Redefining pretty mode " ^ quote name); |
e2ad717ec889
allow redefining pretty/markup modes (not output due to bootstrap issues) -- to support reloading of theory src/HOL/src/Tools/Code_Generator;
wenzelm
parents:
45666
diff
changeset
|
109 |
Symtab.update (name, {indent = indent}) tab)); |
23617 | 110 |
fun get_mode () = |
43684 | 111 |
the_default default |
112 |
(Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ())); |
|
23617 | 113 |
end; |
114 |
||
115 |
fun mode_indent x y = #indent (get_mode ()) x y; |
|
23645 | 116 |
|
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
117 |
val output_spaces = Output.output o spaces; |
23645 | 118 |
val add_indent = Buffer.add o output_spaces; |
23617 | 119 |
|
120 |
||
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
121 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
122 |
(** printing items: compound phrases, strings, and breaks **) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
123 |
|
37529 | 124 |
abstype T = |
40131
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
125 |
Block of (Output.output * Output.output) * T list * int * int |
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
126 |
(*markup output, body, indentation, length*) |
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
127 |
| String of Output.output * int (*text, length*) |
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
128 |
| Break of bool * int (*mandatory flag, width if not taken*) |
37529 | 129 |
with |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
130 |
|
23645 | 131 |
fun length (Block (_, _, _, len)) = len |
132 |
| length (String (_, len)) = len |
|
133 |
| length (Break (_, wd)) = wd; |
|
134 |
||
9730 | 135 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
136 |
|
23645 | 137 |
(** derived operations to create formatting expressions **) |
138 |
||
139 |
val str = String o Output.output_width; |
|
140 |
||
141 |
fun brk wd = Break (false, wd); |
|
36690
97d2780ad6f0
uniform treatment of length = 1 for forced breaks, also makes ML/Pretty.length coincide with Scala/XML.content_length;
wenzelm
parents:
36689
diff
changeset
|
142 |
val fbrk = Break (true, 1); |
23645 | 143 |
|
144 |
fun breaks prts = Library.separate (brk 1) prts; |
|
145 |
fun fbreaks prts = Library.separate fbrk prts; |
|
146 |
||
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
147 |
fun raw_markup m (indent, es) = |
23645 | 148 |
let |
149 |
fun sum [] k = k |
|
150 |
| sum (e :: es) k = sum es (length e + k); |
|
151 |
in Block (m, es, indent, sum es 0) end; |
|
152 |
||
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
153 |
fun markup_block m arg = raw_markup (Markup.output m) arg; |
30667
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
154 |
|
38474
e498dc2eb576
uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents:
37529
diff
changeset
|
155 |
val blk = markup_block Markup.empty; |
23645 | 156 |
fun block prts = blk (2, prts); |
157 |
val strs = block o breaks o map str; |
|
158 |
||
159 |
fun markup m prts = markup_block m (0, prts); |
|
42266 | 160 |
fun mark m prt = if m = Markup.empty then prt else markup m [prt]; |
161 |
fun mark_str (m, s) = mark m (str s); |
|
162 |
fun marks_str (ms, s) = fold_rev mark ms (str s); |
|
163 |
||
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
|
164 |
val item = markup Markup.item; |
52693 | 165 |
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
|
166 |
|
55763 | 167 |
fun keyword1 name = mark_str (Markup.keyword1, name); |
168 |
fun keyword2 name = mark_str (Markup.keyword2, name); |
|
169 |
fun keyword3 name = mark_str (Markup.keyword3, name); |
|
23645 | 170 |
|
50162 | 171 |
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
|
172 |
val paragraph = markup Markup.paragraph; |
50162 | 173 |
val para = paragraph o text; |
174 |
||
52693 | 175 |
fun markup_chunks m prts = markup m (fbreaks (map (text_fold o single) prts)); |
38474
e498dc2eb576
uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents:
37529
diff
changeset
|
176 |
val chunks = markup_chunks Markup.empty; |
50545
00bdc48c5f71
explicit text_fold markup, which is used by default in Pretty.chunks/chunks2;
wenzelm
parents:
50201
diff
changeset
|
177 |
|
00bdc48c5f71
explicit text_fold markup, which is used by default in Pretty.chunks/chunks2;
wenzelm
parents:
50201
diff
changeset
|
178 |
fun chunks2 prts = |
51511 | 179 |
(case try split_last prts of |
180 |
NONE => blk (0, []) |
|
181 |
| SOME (prefix, last) => |
|
52693 | 182 |
blk (0, maps (fn prt => [text_fold [prt, fbrk], fbrk]) prefix @ [text_fold [last]])); |
23645 | 183 |
|
36733 | 184 |
fun block_enclose (prt1, prt2) prts = chunks [block (fbreaks (prt1 :: prts)), prt2]; |
23645 | 185 |
|
186 |
fun quote prt = blk (1, [str "\"", prt, str "\""]); |
|
187 |
fun backquote prt = blk (1, [str "`", prt, str "`"]); |
|
55033 | 188 |
fun cartouche prt = blk (1, [str "\\<open>", prt, str "\\<close>"]); |
23645 | 189 |
|
190 |
fun separate sep prts = |
|
191 |
flat (Library.separate [str sep, brk 1] (map single prts)); |
|
192 |
||
193 |
val commas = separate ","; |
|
194 |
||
195 |
fun enclose lpar rpar prts = |
|
196 |
block (str lpar :: (prts @ [str rpar])); |
|
197 |
||
198 |
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
|
199 |
|
30620 | 200 |
val position = |
201 |
enum "," "{" "}" o map (fn (x, y) => str (x ^ "=" ^ y)) o Position.properties_of; |
|
202 |
||
23645 | 203 |
val list = enum ","; |
204 |
fun str_list lpar rpar strs = list lpar rpar (map str strs); |
|
205 |
||
206 |
fun big_list name prts = block (fbreaks (str name :: prts)); |
|
207 |
||
208 |
fun indent 0 prt = prt |
|
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
46894
diff
changeset
|
209 |
| indent n prt = blk (0, [str (spaces n), prt]); |
23645 | 210 |
|
211 |
fun unbreakable (Break (_, wd)) = String (output_spaces wd, wd) |
|
212 |
| unbreakable (Block (m, es, indent, wd)) = Block (m, map unbreakable es, indent, wd) |
|
213 |
| unbreakable (e as String _) = e; |
|
214 |
||
215 |
||
216 |
||
217 |
(** formatting **) |
|
218 |
||
219 |
(* formatted output *) |
|
220 |
||
221 |
local |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
222 |
|
17756 | 223 |
type text = {tx: Buffer.T, ind: Buffer.T, pos: int, nl: int}; |
224 |
||
225 |
val empty: text = |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
226 |
{tx = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
227 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
228 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
229 |
nl = 0}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
230 |
|
32784 | 231 |
fun newline {tx, ind = _, pos = _, nl} : text = |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
232 |
{tx = Buffer.add (Output.output "\n") tx, |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
233 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
234 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
235 |
nl = nl + 1}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
236 |
|
23628
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
237 |
fun control s {tx, ind, pos: int, nl} : text = |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
238 |
{tx = Buffer.add s tx, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
239 |
ind = ind, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
240 |
pos = pos, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
241 |
nl = nl}; |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
242 |
|
17756 | 243 |
fun string (s, len) {tx, ind, pos: int, nl} : text = |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
244 |
{tx = Buffer.add s tx, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
245 |
ind = Buffer.add s ind, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
246 |
pos = pos + len, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
247 |
nl = nl}; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
248 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
249 |
fun blanks wd = string (output_spaces wd, wd); |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
250 |
|
17756 | 251 |
fun indentation (buf, len) {tx, ind, pos, nl} : text = |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
252 |
let val s = Buffer.content buf in |
23617 | 253 |
{tx = Buffer.add (mode_indent s len) tx, |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
254 |
ind = Buffer.add s ind, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
255 |
pos = pos + len, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
256 |
nl = nl} |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
257 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
258 |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
259 |
(*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
|
260 |
include "after", to account for text following this block.*) |
36687 | 261 |
fun breakdist (Break _ :: _, _) = 0 |
262 |
| breakdist (Block (_, _, _, len) :: es, after) = len + breakdist (es, after) |
|
32784 | 263 |
| breakdist (String (_, len) :: es, after) = len + breakdist (es, after) |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
264 |
| breakdist ([], after) = after; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
265 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
266 |
(*Search for the next break (at this or higher levels) and force it to occur.*) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
267 |
fun forcenext [] = [] |
36690
97d2780ad6f0
uniform treatment of length = 1 for forced breaks, also makes ML/Pretty.length coincide with Scala/XML.content_length;
wenzelm
parents:
36689
diff
changeset
|
268 |
| forcenext (Break _ :: es) = fbrk :: es |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
269 |
| forcenext (e :: es) = e :: forcenext es; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
270 |
|
23645 | 271 |
in |
19266 | 272 |
|
36745 | 273 |
fun formatted margin input = |
274 |
let |
|
275 |
val breakgain = margin div 20; (*minimum added space required of a break*) |
|
276 |
val emergencypos = margin div 2; (*position too far to right*) |
|
277 |
||
278 |
(*es is list of expressions to print; |
|
279 |
blockin is the indentation of the current block; |
|
280 |
after is the width of the following context until next break.*) |
|
281 |
fun format ([], _, _) text = text |
|
282 |
| format (e :: es, block as (_, blockin), after) (text as {ind, pos, nl, ...}) = |
|
283 |
(case e of |
|
284 |
Block ((bg, en), bes, indent, _) => |
|
285 |
let |
|
286 |
val pos' = pos + indent; |
|
287 |
val pos'' = pos' mod emergencypos; |
|
288 |
val block' = |
|
289 |
if pos' < emergencypos then (ind |> add_indent indent, pos') |
|
290 |
else (add_indent pos'' Buffer.empty, pos''); |
|
291 |
val btext: text = text |
|
292 |
|> control bg |
|
293 |
|> format (bes, block', breakdist (es, after)) |
|
294 |
|> control en; |
|
295 |
(*if this block was broken then force the next break*) |
|
296 |
val es' = if nl < #nl btext then forcenext es else es; |
|
297 |
in format (es', block, after) btext end |
|
298 |
| Break (force, wd) => |
|
299 |
(*no break if text to next break fits on this line |
|
300 |
or if breaking would add only breakgain to space*) |
|
301 |
format (es, block, after) |
|
302 |
(if not force andalso |
|
303 |
pos + wd <= Int.max (margin - breakdist (es, after), blockin + breakgain) |
|
304 |
then text |> blanks wd (*just insert wd blanks*) |
|
305 |
else text |> newline |> indentation block) |
|
306 |
| String str => format (es, block, after) (string str text)); |
|
307 |
in |
|
36747
7361d5dde9ce
discontinued Pretty.setdepth, which appears to be largely unused, but can disrupt important markup if enabled accidentally;
wenzelm
parents:
36745
diff
changeset
|
308 |
#tx (format ([input], (Buffer.empty, 0), 0) empty) |
36745 | 309 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
310 |
|
23645 | 311 |
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
|
312 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
313 |
|
23645 | 314 |
(* special output *) |
18802 | 315 |
|
23645 | 316 |
(*symbolic markup -- no formatting*) |
317 |
fun symbolic prt = |
|
318 |
let |
|
30667
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
319 |
fun out (Block ((bg, en), [], _, _)) = Buffer.add bg #> Buffer.add en |
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
320 |
| out (Block ((bg, en), prts, indent, _)) = |
45666 | 321 |
Buffer.add bg #> |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
50162
diff
changeset
|
322 |
Buffer.markup (Markup.block indent) (fold out prts) #> |
45666 | 323 |
Buffer.add en |
23645 | 324 |
| out (String (s, _)) = Buffer.add s |
45666 | 325 |
| out (Break (false, wd)) = |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
50162
diff
changeset
|
326 |
Buffer.markup (Markup.break wd) (Buffer.add (output_spaces wd)) |
36689
379f5b1e7f91
replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents:
36687
diff
changeset
|
327 |
| out (Break (true, _)) = Buffer.add (Output.output "\n"); |
23645 | 328 |
in out prt Buffer.empty end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
329 |
|
23645 | 330 |
(*unformatted output*) |
331 |
fun unformatted prt = |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
332 |
let |
30667
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
333 |
fun fmt (Block ((bg, en), prts, _, _)) = Buffer.add bg #> fold fmt prts #> Buffer.add en |
23617 | 334 |
| fmt (String (s, _)) = Buffer.add s |
36690
97d2780ad6f0
uniform treatment of length = 1 for forced breaks, also makes ML/Pretty.length coincide with Scala/XML.content_length;
wenzelm
parents:
36689
diff
changeset
|
335 |
| fmt (Break (_, wd)) = Buffer.add (output_spaces wd); |
36747
7361d5dde9ce
discontinued Pretty.setdepth, which appears to be largely unused, but can disrupt important markup if enabled accidentally;
wenzelm
parents:
36745
diff
changeset
|
336 |
in fmt prt Buffer.empty end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
337 |
|
30624
e755b8b76365
simplified datatype ML_Pretty.pretty: model Isabelle not Poly/ML;
wenzelm
parents:
30620
diff
changeset
|
338 |
|
36748 | 339 |
(* output interfaces *) |
30620 | 340 |
|
36748 | 341 |
val margin_default = Unsynchronized.ref 76; (*right margin, or page width*) |
23645 | 342 |
|
343 |
val symbolicN = "pretty_symbolic"; |
|
344 |
||
36745 | 345 |
fun output_buffer margin prt = |
23645 | 346 |
if print_mode_active symbolicN then symbolic prt |
36745 | 347 |
else formatted (the_default (! margin_default) margin) prt; |
23645 | 348 |
|
36745 | 349 |
val output = Buffer.content oo output_buffer; |
350 |
fun string_of_margin margin = Output.escape o output (SOME margin); |
|
351 |
val string_of = Output.escape o output NONE; |
|
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
352 |
val writeln = Output.writeln o string_of; |
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
353 |
|
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
354 |
val symbolic_output = Buffer.content o symbolic; |
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
355 |
val symbolic_string_of = Output.escape o symbolic_output; |
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
356 |
|
23645 | 357 |
val str_of = Output.escape o Buffer.content o unformatted; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
358 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
359 |
|
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
360 |
|
36748 | 361 |
(** ML toplevel pretty printing **) |
362 |
||
363 |
fun to_ML (Block (m, prts, ind, _)) = ML_Pretty.Block (m, map to_ML prts, ind) |
|
364 |
| to_ML (String s) = ML_Pretty.String s |
|
365 |
| to_ML (Break b) = ML_Pretty.Break b; |
|
366 |
||
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
49565
diff
changeset
|
367 |
fun from_ML (ML_Pretty.Block (m, prts, ind)) = raw_markup m (ind, map from_ML prts) |
36748 | 368 |
| from_ML (ML_Pretty.String s) = String s |
369 |
| from_ML (ML_Pretty.Break b) = Break b; |
|
370 |
||
37529 | 371 |
end; |
372 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
373 |
end; |