| author | nipkow | 
| Tue, 23 Feb 2016 18:04:31 +0100 | |
| changeset 62393 | a620a8756b7c | 
| parent 61883 | c0f34fe6aa61 | 
| child 62785 | 70b9c7d4ed7f | 
| permissions | -rw-r--r-- | 
| 36683 | 1 | /* Title: Pure/General/pretty.scala | 
| 2 | Author: Makarius | |
| 3 | ||
| 36687 | 4 | Generic pretty printing module. | 
| 36683 | 5 | */ | 
| 6 | ||
| 7 | package isabelle | |
| 8 | ||
| 9 | ||
| 10 | object Pretty | |
| 11 | {
 | |
| 61871 | 12 | /* XML constructors */ | 
| 13 | ||
| 61874 | 14 | val space: XML.Body = List(XML.Text(Symbol.space)) | 
| 15 | def spaces(n: Int): XML.Body = | |
| 16 | if (n == 0) Nil | |
| 17 | else if (n == 1) space | |
| 18 | else List(XML.Text(Symbol.spaces(n))) | |
| 48704 
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
 wenzelm parents: 
45666diff
changeset | 19 | |
| 61871 | 20 | def block(consistent: Boolean, indent: Int, body: XML.Body): XML.Tree = | 
| 21 | XML.Elem(Markup.Block(consistent, indent), body) | |
| 22 | def block(indent: Int, body: XML.Body): XML.Tree = block(false, indent, body) | |
| 23 | def block(body: XML.Body): XML.Tree = block(2, body) | |
| 48704 
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
 wenzelm parents: 
45666diff
changeset | 24 | |
| 61871 | 25 | def brk(width: Int, indent: Int = 0): XML.Tree = | 
| 26 | XML.Elem(Markup.Break(width, indent), spaces(width)) | |
| 27 | ||
| 28 |   val fbrk: XML.Tree = XML.Text("\n")
 | |
| 29 | ||
| 30 | val Separator: XML.Body = List(XML.elem(Markup.SEPARATOR, space), fbrk) | |
| 31 | def separate(ts: List[XML.Tree]): XML.Body = Library.separate(Separator, ts.map(List(_))).flatten | |
| 61864 | 32 | |
| 48704 
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
 wenzelm parents: 
45666diff
changeset | 33 | |
| 51492 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 34 | /* text metric -- standardized to width of space */ | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 35 | |
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 36 | abstract class Metric | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 37 |   {
 | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 38 | val unit: Double | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 39 | def apply(s: String): Double | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 40 | } | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 41 | |
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 42 | object Metric_Default extends Metric | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 43 |   {
 | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 44 | val unit = 1.0 | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 45 | def apply(s: String): Double = s.length.toDouble | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 46 | } | 
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 47 | |
| 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 48 | |
| 36687 | 49 | /* markup trees with physical blocks and breaks */ | 
| 50 | ||
| 61874 | 51 |   private sealed abstract class Tree { def length: Double }
 | 
| 52 | private case class Block( | |
| 61871 | 53 | markup: Option[(Markup, Option[XML.Body])], | 
| 54 | consistent: Boolean, indent: Int, body: List[Tree], length: Double) extends Tree | |
| 61874 | 55 | private case class Break(force: Boolean, width: Int, indent: Int) extends Tree | 
| 61871 | 56 |   { def length: Double = width.toDouble }
 | 
| 61874 | 57 | private case class Str(string: String, length: Double) extends Tree | 
| 36683 | 58 | |
| 61874 | 59 | private val FBreak = Break(true, 1, 0) | 
| 36683 | 60 | |
| 61874 | 61 | private def make_block( | 
| 61871 | 62 | markup: Option[(Markup, Option[XML.Body])], | 
| 63 | consistent: Boolean, | |
| 64 | indent: Int, | |
| 65 | body: List[Tree]): Tree = | |
| 61883 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 66 |   {
 | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 67 | def body_length(prts: List[Tree], len: Double): Double = | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 68 |     {
 | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 69 | val (line, rest) = | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 70 |         Library.take_prefix[Tree]({ case Break(true, _, _) => false case _ => true }, prts)
 | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 71 |       val len1 = ((0.0 /: line) { case (l, t) => l + t.length }) max len
 | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 72 |       rest match {
 | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 73 | case Break(true, _, ind) :: rest1 => | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 74 | body_length(Break(false, indent + ind, 0) :: rest1, len1) | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 75 | case Nil => len1 | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 76 | } | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 77 | } | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 78 | Block(markup, consistent, indent, body, body_length(body, 0.0)) | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 79 | } | 
| 36683 | 80 | |
| 61874 | 81 | |
| 82 | /* formatted output */ | |
| 83 | ||
| 84 | private sealed case class Text(tx: XML.Body = Nil, pos: Double = 0.0, nl: Int = 0) | |
| 85 |   {
 | |
| 86 | def newline: Text = copy(tx = fbrk :: tx, pos = 0.0, nl = nl + 1) | |
| 87 | def string(s: String, len: Double): Text = | |
| 88 | copy(tx = if (s == "") tx else XML.Text(s) :: tx, pos = pos + len) | |
| 89 | def blanks(wd: Int): Text = string(Symbol.spaces(wd), wd.toDouble) | |
| 90 | def content: XML.Body = tx.reverse | |
| 91 | } | |
| 92 | ||
| 93 | private def break_dist(trees: List[Tree], after: Double): Double = | |
| 94 |     trees match {
 | |
| 95 | case (_: Break) :: _ => 0.0 | |
| 96 | case t :: ts => t.length + break_dist(ts, after) | |
| 97 | case Nil => after | |
| 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 | 98 | } | 
| 
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 | 99 | |
| 61874 | 100 | private def force_break(tree: Tree): Tree = | 
| 101 |     tree match { case Break(false, wd, ind) => Break(true, wd, ind) case _ => tree }
 | |
| 102 | private def force_all(trees: List[Tree]): List[Tree] = trees.map(force_break(_)) | |
| 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: 
51569diff
changeset | 103 | |
| 61874 | 104 | private def force_next(trees: List[Tree]): List[Tree] = | 
| 105 |     trees match {
 | |
| 106 | case Nil => Nil | |
| 107 | case (t: Break) :: ts => force_break(t) :: ts | |
| 108 | case t :: ts => t :: force_next(ts) | |
| 109 | } | |
| 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: 
51569diff
changeset | 110 | |
| 51493 
59d8a1031c00
allow fractional pretty margin -- avoid premature rounding;
 wenzelm parents: 
51492diff
changeset | 111 | private val margin_default = 76.0 | 
| 36820 | 112 | |
| 51493 
59d8a1031c00
allow fractional pretty margin -- avoid premature rounding;
 wenzelm parents: 
51492diff
changeset | 113 | def formatted(input: XML.Body, margin: Double = margin_default, | 
| 51492 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 114 | metric: Metric = Metric_Default): XML.Body = | 
| 36687 | 115 |   {
 | 
| 116 | val breakgain = margin / 20 | |
| 51569 
4e084727faae
maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
 wenzelm parents: 
51568diff
changeset | 117 | val emergencypos = (margin / 2).round.toInt | 
| 36687 | 118 | |
| 61874 | 119 | def make_tree(inp: XML.Body): List[Tree] = | 
| 120 |       inp flatMap {
 | |
| 121 | case XML.Wrapped_Elem(markup, body1, body2) => | |
| 122 | List(make_block(Some(markup, Some(body1)), false, 0, make_tree(body2))) | |
| 123 | case XML.Elem(markup, body) => | |
| 124 |           markup match {
 | |
| 125 | case Markup.Block(consistent, indent) => | |
| 126 | List(make_block(None, consistent, indent, make_tree(body))) | |
| 127 | case Markup.Break(width, indent) => | |
| 128 | List(Break(false, width, indent)) | |
| 129 | case Markup(Markup.ITEM, _) => | |
| 130 | List(make_block(None, false, 2, | |
| 131 | make_tree(XML.elem(Markup.BULLET, space) :: space ::: body))) | |
| 132 | case _ => | |
| 133 | List(make_block(Some((markup, None)), false, 0, make_tree(body))) | |
| 134 | } | |
| 135 | case XML.Text(text) => | |
| 136 | Library.separate(FBreak, split_lines(text).map(s => Str(s, metric(s)))) | |
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 137 | } | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 138 | |
| 61871 | 139 | def format(trees: List[Tree], blockin: Int, after: Double, text: Text): Text = | 
| 36687 | 140 |       trees match {
 | 
| 141 | case Nil => text | |
| 142 | ||
| 61871 | 143 | case Block(markup, consistent, indent, body, blen) :: ts => | 
| 51569 
4e084727faae
maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
 wenzelm parents: 
51568diff
changeset | 144 | val pos1 = (text.pos + indent).ceil.toInt | 
| 36687 | 145 | val pos2 = pos1 % emergencypos | 
| 61868 | 146 | val blockin1 = if (pos1 < emergencypos) pos1 else pos2 | 
| 61864 | 147 | val d = break_dist(ts, after) | 
| 148 | val body1 = if (consistent && text.pos + blen > margin - d) force_all(body) else body | |
| 61871 | 149 | val btext = | 
| 150 |             markup match {
 | |
| 151 | case None => format(body1, blockin1, d, text) | |
| 152 | case Some((m, markup_body)) => | |
| 153 | val btext0 = format(body1, blockin1, d, text.copy(tx = Nil)) | |
| 154 | val elem = | |
| 155 |                   markup_body match {
 | |
| 156 | case None => XML.Elem(m, btext0.content) | |
| 157 | case Some(b) => XML.Wrapped_Elem(m, b, btext0.content) | |
| 158 | } | |
| 159 | btext0.copy(tx = elem :: text.tx) | |
| 160 | } | |
| 61864 | 161 | val ts1 = if (text.nl < btext.nl) force_next(ts) else ts | 
| 36687 | 162 | format(ts1, blockin, after, btext) | 
| 163 | ||
| 61871 | 164 | case Break(force, wd, ind) :: ts => | 
| 165 | if (!force && | |
| 166 | text.pos + wd <= ((margin - break_dist(ts, after)) max (blockin + breakgain))) | |
| 36687 | 167 | format(ts, blockin, after, text.blanks(wd)) | 
| 61862 
e2a9e46ac0fb
support pretty break indent, like underlying ML systems;
 wenzelm parents: 
55551diff
changeset | 168 | else format(ts, blockin, after, text.newline.blanks(blockin + ind)) | 
| 36687 | 169 | |
| 61871 | 170 | case Str(s, len) :: ts => format(ts, blockin, after, text.string(s, len)) | 
| 36687 | 171 | } | 
| 61874 | 172 | format(make_tree(input), 0, 0.0, Text()).content | 
| 36687 | 173 | } | 
| 36734 
d9b10c173330
Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
 wenzelm parents: 
36689diff
changeset | 174 | |
| 51493 
59d8a1031c00
allow fractional pretty margin -- avoid premature rounding;
 wenzelm parents: 
51492diff
changeset | 175 | def string_of(input: XML.Body, margin: Double = margin_default, | 
| 51492 
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
 wenzelm parents: 
51470diff
changeset | 176 | metric: Metric = Metric_Default): String = | 
| 49416 
1053a564dd25
some actual rich text markup via XML.content_markup;
 wenzelm parents: 
49414diff
changeset | 177 | XML.content(formatted(input, margin, metric)) | 
| 36683 | 178 | } |