author | wenzelm |
Wed, 12 May 2010 12:50:00 +0200 | |
changeset 36820 | 0cdfce0bf956 |
parent 36818 | 599466689412 |
child 37374 | d66e6cc47fab |
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 |
||
36683 | 17 |
object Block |
18 |
{ |
|
19 |
def apply(indent: Int, body: List[XML.Tree]): XML.Tree = |
|
20 |
XML.Elem(Markup.BLOCK, List((Markup.INDENT, indent.toString)), body) |
|
21 |
||
22 |
def unapply(tree: XML.Tree): Option[(Int, List[XML.Tree])] = |
|
23 |
tree match { |
|
24 |
case XML.Elem(Markup.BLOCK, List((Markup.INDENT, indent)), body) => |
|
25 |
Markup.parse_int(indent) match { |
|
26 |
case Some(i) => Some((i, body)) |
|
27 |
case None => None |
|
28 |
} |
|
29 |
case _ => None |
|
30 |
} |
|
31 |
} |
|
32 |
||
33 |
object Break |
|
34 |
{ |
|
35 |
def apply(width: Int): XML.Tree = |
|
36763 | 36 |
XML.Elem(Markup.BREAK, List((Markup.WIDTH, width.toString)), |
37 |
List(XML.Text(Symbol.spaces(width)))) |
|
36683 | 38 |
|
39 |
def unapply(tree: XML.Tree): Option[Int] = |
|
40 |
tree match { |
|
41 |
case XML.Elem(Markup.BREAK, List((Markup.WIDTH, width)), _) => Markup.parse_int(width) |
|
42 |
case _ => None |
|
43 |
} |
|
44 |
} |
|
45 |
||
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
|
46 |
val FBreak = XML.Text("\n") |
36687 | 47 |
|
48 |
||
49 |
/* formatted output */ |
|
50 |
||
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:
36689
diff
changeset
|
51 |
private def standard_format(tree: XML.Tree): List[XML.Tree] = |
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
|
52 |
tree match { |
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:
36689
diff
changeset
|
53 |
case XML.Elem(name, atts, body) => List(XML.Elem(name, atts, 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:
36687
diff
changeset
|
54 |
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:
36687
diff
changeset
|
55 |
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:
36687
diff
changeset
|
56 |
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:
36687
diff
changeset
|
57 |
} |
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
|
58 |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
59 |
case class Text(tx: List[XML.Tree] = Nil, val pos: Double = 0.0, val nl: Int = 0) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
60 |
{ |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
61 |
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:
36763
diff
changeset
|
62 |
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:
36763
diff
changeset
|
63 |
def blanks(wd: Int): Text = string(Symbol.spaces(wd), wd.toDouble) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
64 |
def content: List[XML.Tree] = tx.reverse |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
65 |
} |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
66 |
|
36745 | 67 |
private val margin_default = 76 |
36818 | 68 |
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:
36689
diff
changeset
|
69 |
|
36820 | 70 |
def font_metric(metrics: FontMetrics): String => Double = |
71 |
if (metrics == null) ((s: String) => s.length.toDouble) |
|
72 |
else { |
|
73 |
val unit = metrics.charWidth(Symbol.spc).toDouble |
|
74 |
((s: String) => if (s == "\n") 1.0 else metrics.stringWidth(s) / unit) |
|
75 |
} |
|
76 |
||
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
77 |
def formatted(input: List[XML.Tree], margin: Int = margin_default, |
36818 | 78 |
metric: String => Double = metric_default): List[XML.Tree] = |
36687 | 79 |
{ |
80 |
val breakgain = margin / 20 |
|
81 |
val emergencypos = margin / 2 |
|
82 |
||
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
83 |
def content_length(tree: XML.Tree): Double = |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
84 |
tree match { |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
85 |
case XML.Elem(_, _, body) => (0.0 /: body)(_ + content_length(_)) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
86 |
case XML.Text(s) => metric(s) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
87 |
} |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
88 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
89 |
def breakdist(trees: List[XML.Tree], after: Double): Double = |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
90 |
trees match { |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
91 |
case Break(_) :: _ => 0.0 |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
92 |
case FBreak :: _ => 0.0 |
36818 | 93 |
case t :: ts => content_length(t) + breakdist(ts, after) |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
94 |
case Nil => after |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
95 |
} |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
96 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
97 |
def forcenext(trees: List[XML.Tree]): List[XML.Tree] = |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
98 |
trees match { |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
99 |
case Nil => Nil |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
100 |
case FBreak :: _ => trees |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
101 |
case Break(_) :: ts => FBreak :: ts |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
102 |
case t :: ts => t :: forcenext(ts) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
103 |
} |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
104 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
105 |
def format(trees: List[XML.Tree], blockin: Double, after: Double, text: Text): Text = |
36687 | 106 |
trees match { |
107 |
case Nil => text |
|
108 |
||
109 |
case Block(indent, body) :: ts => |
|
110 |
val pos1 = text.pos + indent |
|
111 |
val pos2 = pos1 % emergencypos |
|
112 |
val blockin1 = |
|
113 |
if (pos1 < emergencypos) pos1 |
|
114 |
else pos2 |
|
115 |
val btext = format(body, blockin1, breakdist(ts, after), text) |
|
116 |
val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts |
|
117 |
format(ts1, blockin, after, btext) |
|
118 |
||
119 |
case Break(wd) :: ts => |
|
120 |
if (text.pos + wd <= (margin - breakdist(ts, after)).max(blockin + breakgain)) |
|
121 |
format(ts, blockin, after, text.blanks(wd)) |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
122 |
else format(ts, blockin, after, text.newline.blanks(blockin.toInt)) |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
123 |
case FBreak :: ts => format(ts, blockin, after, text.newline.blanks(blockin.toInt)) |
36687 | 124 |
|
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:
36689
diff
changeset
|
125 |
case XML.Elem(name, atts, body) :: ts => |
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:
36689
diff
changeset
|
126 |
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:
36689
diff
changeset
|
127 |
val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts |
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:
36689
diff
changeset
|
128 |
val btext1 = btext.copy(tx = XML.Elem(name, atts, btext.content) :: text.tx) |
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:
36689
diff
changeset
|
129 |
format(ts1, blockin, after, btext1) |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
130 |
case XML.Text(s) :: ts => format(ts, blockin, after, text.string(s, metric(s))) |
36687 | 131 |
} |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
132 |
format(input.flatMap(standard_format), 0.0, 0.0, Text()).content |
36687 | 133 |
} |
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:
36689
diff
changeset
|
134 |
|
36818 | 135 |
def string_of(input: List[XML.Tree], margin: Int = margin_default, |
136 |
metric: String => Double = metric_default): String = |
|
36736 | 137 |
formatted(input).iterator.flatMap(XML.content).mkString |
138 |
||
139 |
||
140 |
/* unformatted output */ |
|
141 |
||
142 |
def unformatted(input: List[XML.Tree]): List[XML.Tree] = |
|
143 |
{ |
|
144 |
def fmt(tree: XML.Tree): List[XML.Tree] = |
|
145 |
tree match { |
|
146 |
case Block(_, body) => body.flatMap(fmt) |
|
36763 | 147 |
case Break(wd) => List(XML.Text(Symbol.spaces(wd))) |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
148 |
case FBreak => List(XML.Text(Symbol.space)) |
36736 | 149 |
case XML.Elem(name, atts, body) => List(XML.Elem(name, atts, body.flatMap(fmt))) |
150 |
case XML.Text(_) => List(tree) |
|
151 |
} |
|
152 |
input.flatMap(standard_format).flatMap(fmt) |
|
153 |
} |
|
154 |
||
155 |
def str_of(input: List[XML.Tree]): String = |
|
156 |
unformatted(input).iterator.flatMap(XML.content).mkString |
|
36683 | 157 |
} |