src/Pure/General/pretty.scala
author wenzelm
Fri, 24 Feb 2012 22:58:13 +0100
changeset 46661 d2ac78ba805e
parent 45666 d83797ef0d2d
child 48704 85a3de10567d
permissions -rw-r--r--
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty; discontinued map_nodes, del_nodes conveniences -- avoid inefficient mapValues wrapper (this is not Table.map from ML); tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/pretty.scala
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     3
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
     4
Generic pretty printing module.
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     5
*/
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     6
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     7
package isabelle
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     8
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     9
36820
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    10
import java.awt.FontMetrics
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    11
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    12
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    13
object Pretty
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    14
{
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    15
  /* markup trees with physical blocks and breaks */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    16
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    17
  def block(body: XML.Body): XML.Tree = Block(2, body)
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    18
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    19
  object Block
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    20
  {
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    21
    def apply(i: Int, body: XML.Body): XML.Tree =
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 45251
diff changeset
    22
      XML.Elem(Markup(Isabelle_Markup.BLOCK, Isabelle_Markup.Indent(i)), body)
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    23
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    24
    def unapply(tree: XML.Tree): Option[(Int, XML.Body)] =
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    25
      tree match {
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 45251
diff changeset
    26
        case XML.Elem(Markup(Isabelle_Markup.BLOCK, Isabelle_Markup.Indent(i)), body) =>
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 45251
diff changeset
    27
          Some((i, body))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    28
        case _ => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    29
      }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    30
  }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    31
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    32
  object Break
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    33
  {
38414
49f1f657adc2 more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents: 38230
diff changeset
    34
    def apply(w: Int): XML.Tree =
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 45251
diff changeset
    35
      XML.Elem(Markup(Isabelle_Markup.BREAK, Isabelle_Markup.Width(w)),
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 45251
diff changeset
    36
        List(XML.Text(Symbol.spaces(w))))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    37
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    38
    def unapply(tree: XML.Tree): Option[Int] =
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    39
      tree match {
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 45251
diff changeset
    40
        case XML.Elem(Markup(Isabelle_Markup.BREAK, Isabelle_Markup.Width(w)), _) => Some(w)
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    41
        case _ => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    42
      }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    43
  }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    44
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
    45
  val FBreak = XML.Text("\n")
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    46
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    47
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    48
  /* formatted output */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    49
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    50
  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: 36687
diff changeset
    51
    tree match {
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
    52
      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: 36687
diff changeset
    53
      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
    54
        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
    55
          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
    56
    }
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
45251
12913296be79 more private stuff;
wenzelm
parents: 43714
diff changeset
    58
  private sealed 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: 36763
diff changeset
    59
  {
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    60
    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
    61
    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
    62
    def blanks(wd: Int): Text = string(Symbol.spaces(wd), wd.toDouble)
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    63
    def content: XML.Body = tx.reverse
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    64
  }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    65
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36736
diff changeset
    66
  private val margin_default = 76
36818
wenzelm
parents: 36817
diff changeset
    67
  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
    68
36820
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    69
  def font_metric(metrics: FontMetrics): String => Double =
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    70
    if (metrics == null) ((s: String) => s.length.toDouble)
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    71
    else {
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    72
      val unit = metrics.charWidth(Symbol.spc).toDouble
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    73
      ((s: String) => if (s == "\n") 1.0 else metrics.stringWidth(s) / unit)
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    74
    }
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    75
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    76
  def formatted(input: XML.Body, margin: Int = margin_default,
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    77
    metric: String => Double = metric_default): XML.Body =
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    78
  {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    79
    val breakgain = margin / 20
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    80
    val emergencypos = margin / 2
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    81
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    82
    def content_length(tree: XML.Tree): Double =
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    83
      tree match {
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
    84
        case XML.Elem(_, body) => (0.0 /: body)(_ + content_length(_))
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    85
        case XML.Text(s) => metric(s)
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    86
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    87
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    88
    def breakdist(trees: XML.Body, after: Double): Double =
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    89
      trees match {
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    90
        case Break(_) :: _ => 0.0
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    91
        case FBreak :: _ => 0.0
36818
wenzelm
parents: 36817
diff changeset
    92
        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
    93
        case Nil => after
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    94
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    95
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    96
    def forcenext(trees: XML.Body): XML.Body =
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    97
      trees match {
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    98
        case Nil => Nil
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    99
        case FBreak :: _ => trees
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   100
        case Break(_) :: ts => FBreak :: ts
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   101
        case t :: ts => t :: forcenext(ts)
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   102
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   103
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   104
    def format(trees: XML.Body, blockin: Double, after: Double, text: Text): Text =
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   105
      trees match {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   106
        case Nil => text
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   107
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   108
        case Block(indent, body) :: ts =>
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   109
          val pos1 = text.pos + indent
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   110
          val pos2 = pos1 % emergencypos
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   111
          val blockin1 =
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   112
            if (pos1 < emergencypos) pos1
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   113
            else pos2
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   114
          val btext = format(body, blockin1, breakdist(ts, after), text)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   115
          val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   116
          format(ts1, blockin, after, btext)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   117
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   118
        case Break(wd) :: ts =>
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   119
          if (text.pos + wd <= (margin - breakdist(ts, after)).max(blockin + breakgain))
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   120
            format(ts, blockin, after, text.blanks(wd))
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   121
          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
   122
        case FBreak :: ts => format(ts, blockin, after, text.newline.blanks(blockin.toInt))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   123
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
   124
        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: 36689
diff changeset
   125
          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
   126
          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: 37374
diff changeset
   127
          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: 36689
diff changeset
   128
          format(ts1, blockin, after, btext1)
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   129
        case XML.Text(s) :: ts => format(ts, blockin, after, text.string(s, metric(s)))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   130
      }
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   131
    format(input.flatMap(standard_format), 0.0, 0.0, Text()).content
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   132
  }
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
   133
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   134
  def string_of(input: XML.Body, margin: Int = margin_default,
36818
wenzelm
parents: 36817
diff changeset
   135
      metric: String => Double = metric_default): String =
37374
d66e6cc47fab Pretty.string_of (in Scala): actually observe margin/metric;
wenzelm
parents: 36820
diff changeset
   136
    formatted(input, margin, metric).iterator.flatMap(XML.content).mkString
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   137
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   138
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   139
  /* unformatted output */
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   140
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   141
  def unformatted(input: XML.Body): XML.Body =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   142
  {
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   143
    def fmt(tree: XML.Tree): XML.Body =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   144
      tree match {
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   145
        case Block(_, body) => body.flatMap(fmt)
36763
096ebe74aeaf static Symbol.spaces;
wenzelm
parents: 36745
diff changeset
   146
        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
   147
        case FBreak => List(XML.Text(Symbol.space))
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
   148
        case XML.Elem(markup, body) => List(XML.Elem(markup, body.flatMap(fmt)))
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   149
        case XML.Text(_) => List(tree)
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   150
      }
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   151
    input.flatMap(standard_format).flatMap(fmt)
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   152
  }
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   153
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   154
  def str_of(input: XML.Body): String =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   155
    unformatted(input).iterator.flatMap(XML.content).mkString
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
   156
}