| author | blanchet | 
| Mon, 06 Sep 2010 16:50:29 +0200 | |
| changeset 39220 | 8420a873f534 | 
| parent 38724 | d1feec02cf02 | 
| child 43714 | 3749d1e6dde9 | 
| 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 | ||
| 36820 | 10 | import java.awt.FontMetrics | 
| 11 | ||
| 12 | ||
| 36683 | 13 | object Pretty | 
| 14 | {
 | |
| 36687 | 15 | /* markup trees with physical blocks and breaks */ | 
| 16 | ||
| 38573 | 17 | def block(body: XML.Body): XML.Tree = Block(2, body) | 
| 18 | ||
| 36683 | 19 | object Block | 
| 20 |   {
 | |
| 38573 | 21 | def apply(i: Int, body: XML.Body): XML.Tree = | 
| 38724 | 22 | XML.Elem(Markup(Markup.BLOCK, Markup.Indent(i)), body) | 
| 36683 | 23 | |
| 38573 | 24 | def unapply(tree: XML.Tree): Option[(Int, XML.Body)] = | 
| 36683 | 25 |       tree match {
 | 
| 38724 | 26 | case XML.Elem(Markup(Markup.BLOCK, Markup.Indent(i)), body) => Some((i, body)) | 
| 36683 | 27 | case _ => None | 
| 28 | } | |
| 29 | } | |
| 30 | ||
| 31 | object Break | |
| 32 |   {
 | |
| 38414 
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
 wenzelm parents: 
38230diff
changeset | 33 | def apply(w: Int): XML.Tree = | 
| 38724 | 34 | XML.Elem(Markup(Markup.BREAK, Markup.Width(w)), List(XML.Text(Symbol.spaces(w)))) | 
| 36683 | 35 | |
| 36 | def unapply(tree: XML.Tree): Option[Int] = | |
| 37 |       tree match {
 | |
| 38724 | 38 | case XML.Elem(Markup(Markup.BREAK, Markup.Width(w)), _) => Some(w) | 
| 36683 | 39 | case _ => None | 
| 40 | } | |
| 41 | } | |
| 42 | ||
| 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 | 43 |   val FBreak = XML.Text("\n")
 | 
| 36687 | 44 | |
| 45 | ||
| 46 | /* formatted output */ | |
| 47 | ||
| 38573 | 48 | private def standard_format(tree: XML.Tree): XML.Body = | 
| 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 | 49 |     tree match {
 | 
| 38230 
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
 wenzelm parents: 
37374diff
changeset | 50 | case XML.Elem(markup, body) => List(XML.Elem(markup, body.flatMap(standard_format))) | 
| 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 | 51 | case XML.Text(text) => | 
| 
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 | 52 | Library.separate(FBreak, | 
| 
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 | 53 | Library.chunks(text).toList.map((s: CharSequence) => XML.Text(s.toString))) | 
| 
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 | 54 | } | 
| 
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 | 55 | |
| 38573 | 56 | case class Text(tx: XML.Body = Nil, val pos: Double = 0.0, val nl: Int = 0) | 
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 57 |   {
 | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 58 | def newline: Text = copy(tx = FBreak :: tx, pos = 0.0, nl = nl + 1) | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 59 | def string(s: String, len: Double): Text = copy(tx = XML.Text(s) :: tx, pos = pos + len) | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 60 | def blanks(wd: Int): Text = string(Symbol.spaces(wd), wd.toDouble) | 
| 38573 | 61 | def content: XML.Body = tx.reverse | 
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 62 | } | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 63 | |
| 36745 | 64 | private val margin_default = 76 | 
| 36818 | 65 | private def metric_default(s: String) = s.length.toDouble | 
| 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 | 66 | |
| 36820 | 67 | def font_metric(metrics: FontMetrics): String => Double = | 
| 68 | if (metrics == null) ((s: String) => s.length.toDouble) | |
| 69 |     else {
 | |
| 70 | val unit = metrics.charWidth(Symbol.spc).toDouble | |
| 71 | ((s: String) => if (s == "\n") 1.0 else metrics.stringWidth(s) / unit) | |
| 72 | } | |
| 73 | ||
| 38573 | 74 | def formatted(input: XML.Body, margin: Int = margin_default, | 
| 75 | metric: String => Double = metric_default): XML.Body = | |
| 36687 | 76 |   {
 | 
| 77 | val breakgain = margin / 20 | |
| 78 | val emergencypos = margin / 2 | |
| 79 | ||
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 80 | def content_length(tree: XML.Tree): Double = | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 81 |       tree match {
 | 
| 38230 
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
 wenzelm parents: 
37374diff
changeset | 82 | case XML.Elem(_, body) => (0.0 /: body)(_ + content_length(_)) | 
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 83 | case XML.Text(s) => metric(s) | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 84 | } | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 85 | |
| 38573 | 86 | def breakdist(trees: XML.Body, after: Double): Double = | 
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 87 |       trees match {
 | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 88 | case Break(_) :: _ => 0.0 | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 89 | case FBreak :: _ => 0.0 | 
| 36818 | 90 | case t :: ts => content_length(t) + breakdist(ts, after) | 
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 91 | case Nil => after | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 92 | } | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 93 | |
| 38573 | 94 | def forcenext(trees: XML.Body): XML.Body = | 
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 95 |       trees match {
 | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 96 | case Nil => Nil | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 97 | case FBreak :: _ => trees | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 98 | case Break(_) :: ts => FBreak :: ts | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 99 | case t :: ts => t :: forcenext(ts) | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 100 | } | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 101 | |
| 38573 | 102 | def format(trees: XML.Body, blockin: Double, after: Double, text: Text): Text = | 
| 36687 | 103 |       trees match {
 | 
| 104 | case Nil => text | |
| 105 | ||
| 106 | case Block(indent, body) :: ts => | |
| 107 | val pos1 = text.pos + indent | |
| 108 | val pos2 = pos1 % emergencypos | |
| 109 | val blockin1 = | |
| 110 | if (pos1 < emergencypos) pos1 | |
| 111 | else pos2 | |
| 112 | val btext = format(body, blockin1, breakdist(ts, after), text) | |
| 113 | val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts | |
| 114 | format(ts1, blockin, after, btext) | |
| 115 | ||
| 116 | case Break(wd) :: ts => | |
| 117 | if (text.pos + wd <= (margin - breakdist(ts, after)).max(blockin + breakgain)) | |
| 118 | format(ts, blockin, after, text.blanks(wd)) | |
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 119 | else format(ts, blockin, after, text.newline.blanks(blockin.toInt)) | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 120 | case FBreak :: ts => format(ts, blockin, after, text.newline.blanks(blockin.toInt)) | 
| 36687 | 121 | |
| 38230 
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
 wenzelm parents: 
37374diff
changeset | 122 | case XML.Elem(markup, body) :: ts => | 
| 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 | 123 | val btext = format(body, blockin, breakdist(ts, after), text.copy(tx = Nil)) | 
| 
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 | 124 | val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts | 
| 38230 
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
 wenzelm parents: 
37374diff
changeset | 125 | val btext1 = btext.copy(tx = XML.Elem(markup, btext.content) :: text.tx) | 
| 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 | 126 | format(ts1, blockin, after, btext1) | 
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 127 | case XML.Text(s) :: ts => format(ts, blockin, after, text.string(s, metric(s))) | 
| 36687 | 128 | } | 
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 129 | format(input.flatMap(standard_format), 0.0, 0.0, Text()).content | 
| 36687 | 130 | } | 
| 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 | 131 | |
| 38573 | 132 | def string_of(input: XML.Body, margin: Int = margin_default, | 
| 36818 | 133 | metric: String => Double = metric_default): String = | 
| 37374 
d66e6cc47fab
Pretty.string_of (in Scala): actually observe margin/metric;
 wenzelm parents: 
36820diff
changeset | 134 | formatted(input, margin, metric).iterator.flatMap(XML.content).mkString | 
| 36736 | 135 | |
| 136 | ||
| 137 | /* unformatted output */ | |
| 138 | ||
| 38573 | 139 | def unformatted(input: XML.Body): XML.Body = | 
| 36736 | 140 |   {
 | 
| 38573 | 141 | def fmt(tree: XML.Tree): XML.Body = | 
| 36736 | 142 |       tree match {
 | 
| 143 | case Block(_, body) => body.flatMap(fmt) | |
| 36763 | 144 | case Break(wd) => List(XML.Text(Symbol.spaces(wd))) | 
| 36817 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 145 | case FBreak => List(XML.Text(Symbol.space)) | 
| 38230 
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
 wenzelm parents: 
37374diff
changeset | 146 | case XML.Elem(markup, body) => List(XML.Elem(markup, body.flatMap(fmt))) | 
| 36736 | 147 | case XML.Text(_) => List(tree) | 
| 148 | } | |
| 149 | input.flatMap(standard_format).flatMap(fmt) | |
| 150 | } | |
| 151 | ||
| 38573 | 152 | def str_of(input: XML.Body): String = | 
| 36736 | 153 | unformatted(input).iterator.flatMap(XML.content).mkString | 
| 36683 | 154 | } |