author | wenzelm |
Tue, 13 Mar 2012 11:21:26 +0100 | |
changeset 46894 | e2ad717ec889 |
parent 45666 | d83797ef0d2d |
child 48704 | 85a3de10567d |
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 |
40131
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
24 |
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
|
25 |
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
|
26 |
type T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
27 |
val str: string -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
28 |
val brk: int -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
29 |
val fbrk: T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
30 |
val breaks: T list -> T list |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
31 |
val fbreaks: T list -> T list |
23645 | 32 |
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
|
33 |
val block: T list -> T |
23645 | 34 |
val strs: string list -> T |
23617 | 35 |
val markup: Markup.T -> T list -> T |
26703 | 36 |
val mark: Markup.T -> T -> T |
42266 | 37 |
val mark_str: Markup.T * string -> T |
38 |
val marks_str: Markup.T list * string -> T |
|
23617 | 39 |
val keyword: string -> T |
40 |
val command: string -> T |
|
23638 | 41 |
val markup_chunks: Markup.T -> T list -> T |
18802 | 42 |
val chunks: T list -> T |
19266 | 43 |
val chunks2: T list -> T |
23617 | 44 |
val block_enclose: T * T -> T list -> T |
18802 | 45 |
val quote: T -> T |
46 |
val backquote: T -> T |
|
47 |
val separate: string -> T list -> T list |
|
48 |
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
|
49 |
val enclose: string -> string -> T list -> T |
18802 | 50 |
val enum: string -> string -> string -> T list -> T |
30620 | 51 |
val position: Position.T -> T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
52 |
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
|
53 |
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
|
54 |
val big_list: string -> T list -> T |
9730 | 55 |
val indent: int -> T -> T |
23645 | 56 |
val unbreakable: T -> T |
36745 | 57 |
val margin_default: int Unsynchronized.ref |
23645 | 58 |
val symbolicN: string |
36745 | 59 |
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
|
60 |
val output: int option -> T -> Output.output |
36745 | 61 |
val string_of_margin: int -> T -> string |
23645 | 62 |
val string_of: T -> string |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
63 |
val str_of: T -> string |
23645 | 64 |
val writeln: T -> unit |
36748 | 65 |
val to_ML: T -> ML_Pretty.pretty |
66 |
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
|
67 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
68 |
|
23617 | 69 |
structure Pretty: PRETTY = |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
70 |
struct |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
71 |
|
23617 | 72 |
(** print mode operations **) |
73 |
||
74 |
fun default_indent (_: string) = Symbol.spaces; |
|
75 |
||
76 |
local |
|
23698 | 77 |
val default = {indent = default_indent}; |
43684 | 78 |
val modes = Synchronized.var "Pretty.modes" (Symtab.make [("", default)]); |
23617 | 79 |
in |
43684 | 80 |
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
|
81 |
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
|
82 |
(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
|
83 |
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
|
84 |
Symtab.update (name, {indent = indent}) tab)); |
23617 | 85 |
fun get_mode () = |
43684 | 86 |
the_default default |
87 |
(Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ())); |
|
23617 | 88 |
end; |
89 |
||
90 |
fun mode_indent x y = #indent (get_mode ()) x y; |
|
23645 | 91 |
|
92 |
val output_spaces = Output.output o Symbol.spaces; |
|
93 |
val add_indent = Buffer.add o output_spaces; |
|
23617 | 94 |
|
95 |
||
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
96 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
97 |
(** printing items: compound phrases, strings, and breaks **) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
98 |
|
37529 | 99 |
abstype T = |
40131
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
100 |
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
|
101 |
(*markup output, body, indentation, length*) |
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
102 |
| 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
|
103 |
| Break of bool * int (*mandatory flag, width if not taken*) |
37529 | 104 |
with |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
105 |
|
23645 | 106 |
fun length (Block (_, _, _, len)) = len |
107 |
| length (String (_, len)) = len |
|
108 |
| length (Break (_, wd)) = wd; |
|
109 |
||
9730 | 110 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
111 |
|
23645 | 112 |
(** derived operations to create formatting expressions **) |
113 |
||
114 |
val str = String o Output.output_width; |
|
115 |
||
116 |
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
|
117 |
val fbrk = Break (true, 1); |
23645 | 118 |
|
119 |
fun breaks prts = Library.separate (brk 1) prts; |
|
120 |
fun fbreaks prts = Library.separate fbrk prts; |
|
121 |
||
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
|
122 |
fun block_markup m (indent, es) = |
23645 | 123 |
let |
124 |
fun sum [] k = k |
|
125 |
| sum (e :: es) k = sum es (length e + k); |
|
126 |
in Block (m, es, indent, sum es 0) end; |
|
127 |
||
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
|
128 |
fun markup_block m arg = block_markup (Markup.output m) arg; |
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
129 |
|
38474
e498dc2eb576
uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents:
37529
diff
changeset
|
130 |
val blk = markup_block Markup.empty; |
23645 | 131 |
fun block prts = blk (2, prts); |
132 |
val strs = block o breaks o map str; |
|
133 |
||
134 |
fun markup m prts = markup_block m (0, prts); |
|
42266 | 135 |
fun mark m prt = if m = Markup.empty then prt else markup m [prt]; |
136 |
fun mark_str (m, s) = mark m (str s); |
|
137 |
fun marks_str (ms, s) = fold_rev mark ms (str s); |
|
138 |
||
45666 | 139 |
fun keyword name = mark_str (Isabelle_Markup.keyword, name); |
140 |
fun command name = mark_str (Isabelle_Markup.command, name); |
|
23645 | 141 |
|
142 |
fun markup_chunks m prts = markup m (fbreaks prts); |
|
38474
e498dc2eb576
uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents:
37529
diff
changeset
|
143 |
val chunks = markup_chunks Markup.empty; |
23645 | 144 |
fun chunks2 prts = blk (0, flat (Library.separate [fbrk, fbrk] (map single prts))); |
145 |
||
36733 | 146 |
fun block_enclose (prt1, prt2) prts = chunks [block (fbreaks (prt1 :: prts)), prt2]; |
23645 | 147 |
|
148 |
fun quote prt = blk (1, [str "\"", prt, str "\""]); |
|
149 |
fun backquote prt = blk (1, [str "`", prt, str "`"]); |
|
150 |
||
151 |
fun separate sep prts = |
|
152 |
flat (Library.separate [str sep, brk 1] (map single prts)); |
|
153 |
||
154 |
val commas = separate ","; |
|
155 |
||
156 |
fun enclose lpar rpar prts = |
|
157 |
block (str lpar :: (prts @ [str rpar])); |
|
158 |
||
159 |
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
|
160 |
|
30620 | 161 |
val position = |
162 |
enum "," "{" "}" o map (fn (x, y) => str (x ^ "=" ^ y)) o Position.properties_of; |
|
163 |
||
23645 | 164 |
val list = enum ","; |
165 |
fun str_list lpar rpar strs = list lpar rpar (map str strs); |
|
166 |
||
167 |
fun big_list name prts = block (fbreaks (str name :: prts)); |
|
168 |
||
169 |
fun indent 0 prt = prt |
|
170 |
| indent n prt = blk (0, [str (Symbol.spaces n), prt]); |
|
171 |
||
172 |
fun unbreakable (Break (_, wd)) = String (output_spaces wd, wd) |
|
173 |
| unbreakable (Block (m, es, indent, wd)) = Block (m, map unbreakable es, indent, wd) |
|
174 |
| unbreakable (e as String _) = e; |
|
175 |
||
176 |
||
177 |
||
178 |
(** formatting **) |
|
179 |
||
180 |
(* formatted output *) |
|
181 |
||
182 |
local |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
183 |
|
17756 | 184 |
type text = {tx: Buffer.T, ind: Buffer.T, pos: int, nl: int}; |
185 |
||
186 |
val empty: text = |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
187 |
{tx = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
188 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
189 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
190 |
nl = 0}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
191 |
|
32784 | 192 |
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
|
193 |
{tx = Buffer.add (Output.output "\n") tx, |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
194 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
195 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
196 |
nl = nl + 1}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
197 |
|
23628
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
198 |
fun control s {tx, ind, pos: int, nl} : text = |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
199 |
{tx = Buffer.add s tx, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
200 |
ind = ind, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
201 |
pos = pos, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
202 |
nl = nl}; |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
203 |
|
17756 | 204 |
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
|
205 |
{tx = Buffer.add s tx, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
206 |
ind = Buffer.add s ind, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
207 |
pos = pos + len, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
208 |
nl = nl}; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
209 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
210 |
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
|
211 |
|
17756 | 212 |
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
|
213 |
let val s = Buffer.content buf in |
23617 | 214 |
{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
|
215 |
ind = Buffer.add s ind, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
216 |
pos = pos + len, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
217 |
nl = nl} |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
218 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
219 |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
220 |
(*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
|
221 |
include "after", to account for text following this block.*) |
36687 | 222 |
fun breakdist (Break _ :: _, _) = 0 |
223 |
| breakdist (Block (_, _, _, len) :: es, after) = len + breakdist (es, after) |
|
32784 | 224 |
| 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
|
225 |
| breakdist ([], after) = after; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
226 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
227 |
(*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
|
228 |
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
|
229 |
| forcenext (Break _ :: es) = fbrk :: es |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
230 |
| forcenext (e :: es) = e :: forcenext es; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
231 |
|
23645 | 232 |
in |
19266 | 233 |
|
36745 | 234 |
fun formatted margin input = |
235 |
let |
|
236 |
val breakgain = margin div 20; (*minimum added space required of a break*) |
|
237 |
val emergencypos = margin div 2; (*position too far to right*) |
|
238 |
||
239 |
(*es is list of expressions to print; |
|
240 |
blockin is the indentation of the current block; |
|
241 |
after is the width of the following context until next break.*) |
|
242 |
fun format ([], _, _) text = text |
|
243 |
| format (e :: es, block as (_, blockin), after) (text as {ind, pos, nl, ...}) = |
|
244 |
(case e of |
|
245 |
Block ((bg, en), bes, indent, _) => |
|
246 |
let |
|
247 |
val pos' = pos + indent; |
|
248 |
val pos'' = pos' mod emergencypos; |
|
249 |
val block' = |
|
250 |
if pos' < emergencypos then (ind |> add_indent indent, pos') |
|
251 |
else (add_indent pos'' Buffer.empty, pos''); |
|
252 |
val btext: text = text |
|
253 |
|> control bg |
|
254 |
|> format (bes, block', breakdist (es, after)) |
|
255 |
|> control en; |
|
256 |
(*if this block was broken then force the next break*) |
|
257 |
val es' = if nl < #nl btext then forcenext es else es; |
|
258 |
in format (es', block, after) btext end |
|
259 |
| Break (force, wd) => |
|
260 |
(*no break if text to next break fits on this line |
|
261 |
or if breaking would add only breakgain to space*) |
|
262 |
format (es, block, after) |
|
263 |
(if not force andalso |
|
264 |
pos + wd <= Int.max (margin - breakdist (es, after), blockin + breakgain) |
|
265 |
then text |> blanks wd (*just insert wd blanks*) |
|
266 |
else text |> newline |> indentation block) |
|
267 |
| String str => format (es, block, after) (string str text)); |
|
268 |
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
|
269 |
#tx (format ([input], (Buffer.empty, 0), 0) empty) |
36745 | 270 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
271 |
|
23645 | 272 |
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
|
273 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
274 |
|
23645 | 275 |
(* special output *) |
18802 | 276 |
|
23645 | 277 |
(*symbolic markup -- no formatting*) |
278 |
fun symbolic prt = |
|
279 |
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
|
280 |
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
|
281 |
| out (Block ((bg, en), prts, indent, _)) = |
45666 | 282 |
Buffer.add bg #> |
283 |
Buffer.markup (Isabelle_Markup.block indent) (fold out prts) #> |
|
284 |
Buffer.add en |
|
23645 | 285 |
| out (String (s, _)) = Buffer.add s |
45666 | 286 |
| out (Break (false, wd)) = |
287 |
Buffer.markup (Isabelle_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
|
288 |
| out (Break (true, _)) = Buffer.add (Output.output "\n"); |
23645 | 289 |
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
|
290 |
|
23645 | 291 |
(*unformatted output*) |
292 |
fun unformatted prt = |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
293 |
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
|
294 |
fun fmt (Block ((bg, en), prts, _, _)) = Buffer.add bg #> fold fmt prts #> Buffer.add en |
23617 | 295 |
| 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
|
296 |
| 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
|
297 |
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
|
298 |
|
30624
e755b8b76365
simplified datatype ML_Pretty.pretty: model Isabelle not Poly/ML;
wenzelm
parents:
30620
diff
changeset
|
299 |
|
36748 | 300 |
(* output interfaces *) |
30620 | 301 |
|
36748 | 302 |
val margin_default = Unsynchronized.ref 76; (*right margin, or page width*) |
23645 | 303 |
|
304 |
val symbolicN = "pretty_symbolic"; |
|
305 |
||
36745 | 306 |
fun output_buffer margin prt = |
23645 | 307 |
if print_mode_active symbolicN then symbolic prt |
36745 | 308 |
else formatted (the_default (! margin_default) margin) prt; |
23645 | 309 |
|
36745 | 310 |
val output = Buffer.content oo output_buffer; |
311 |
fun string_of_margin margin = Output.escape o output (SOME margin); |
|
312 |
val string_of = Output.escape o output NONE; |
|
23645 | 313 |
val str_of = Output.escape o Buffer.content o unformatted; |
314 |
val writeln = Output.writeln o string_of; |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
315 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
316 |
|
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
317 |
|
36748 | 318 |
(** ML toplevel pretty printing **) |
319 |
||
320 |
fun to_ML (Block (m, prts, ind, _)) = ML_Pretty.Block (m, map to_ML prts, ind) |
|
321 |
| to_ML (String s) = ML_Pretty.String s |
|
322 |
| to_ML (Break b) = ML_Pretty.Break b; |
|
323 |
||
324 |
fun from_ML (ML_Pretty.Block (m, prts, ind)) = block_markup m (ind, map from_ML prts) |
|
325 |
| from_ML (ML_Pretty.String s) = String s |
|
326 |
| from_ML (ML_Pretty.Break b) = Break b; |
|
327 |
||
37529 | 328 |
end; |
329 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
330 |
end; |