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