src/Pure/General/pretty.scala
author wenzelm
Thu, 28 Mar 2013 14:47:37 +0100
changeset 51568 cdb4b7dc76cb
parent 51507 ebd5366e7a42
child 51569 4e084727faae
permissions -rw-r--r--
tuned;
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
{
48704
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    15
  /* spaces */
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    16
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    17
  val space = " "
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    18
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    19
  private val static_spaces = space * 4000
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    20
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    21
  def spaces(k: Int): String =
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    22
  {
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    23
    require(k >= 0)
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    24
    if (k < static_spaces.length) static_spaces.substring(0, k)
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    25
    else space * k
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    26
  }
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    27
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    28
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: 51470
diff changeset
    29
  /* 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: 51470
diff changeset
    30
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: 51470
diff changeset
    31
  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: 51470
diff changeset
    32
  {
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: 51470
diff changeset
    33
    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: 51470
diff changeset
    34
    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: 51470
diff 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: 51470
diff changeset
    36
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: 51470
diff changeset
    37
  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: 51470
diff changeset
    38
  {
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: 51470
diff changeset
    39
    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: 51470
diff changeset
    40
    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: 51470
diff 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: 51470
diff changeset
    42
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: 51470
diff changeset
    43
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    44
  /* markup trees with physical blocks and breaks */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    45
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    46
  def block(body: XML.Body): XML.Tree = Block(2, body)
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    47
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    48
  object Block
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    49
  {
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    50
    def apply(i: Int, body: XML.Body): XML.Tree =
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49843
diff changeset
    51
      XML.Elem(Markup(Markup.BLOCK, Markup.Indent(i)), body)
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    52
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    53
    def unapply(tree: XML.Tree): Option[(Int, XML.Body)] =
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    54
      tree match {
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49843
diff changeset
    55
        case XML.Elem(Markup(Markup.BLOCK, Markup.Indent(i)), body) =>
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 45251
diff changeset
    56
          Some((i, body))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    57
        case _ => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    58
      }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    59
  }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    60
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    61
  object Break
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    62
  {
38414
49f1f657adc2 more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents: 38230
diff changeset
    63
    def apply(w: Int): XML.Tree =
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49843
diff changeset
    64
      XML.Elem(Markup(Markup.BREAK, Markup.Width(w)),
48704
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    65
        List(XML.Text(spaces(w))))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    66
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    67
    def unapply(tree: XML.Tree): Option[Int] =
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    68
      tree match {
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49843
diff changeset
    69
        case XML.Elem(Markup(Markup.BREAK, Markup.Width(w)), _) => Some(w)
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    70
        case _ => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    71
      }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    72
  }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    73
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
    74
  val FBreak = XML.Text("\n")
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    75
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49843
diff changeset
    76
  val Separator = List(XML.elem(Markup.SEPARATOR, List(XML.Text(space))), FBreak)
49843
afddf4e26fac further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents: 49827
diff changeset
    77
  def separate(ts: List[XML.Tree]): XML.Body = Library.separate(Separator, ts.map(List(_))).flatten
49473
ca7e2c21b104 tuned rendering;
wenzelm
parents: 49468
diff changeset
    78
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    79
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    80
  /* formatted output */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    81
49468
8b06b99fb85c tuned signature;
wenzelm
parents: 49416
diff changeset
    82
  def standard_format(body: XML.Body): XML.Body =
8b06b99fb85c tuned signature;
wenzelm
parents: 49416
diff changeset
    83
    body flatMap {
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
    84
      case XML.Wrapped_Elem(markup, body1, body2) =>
49657
40e4feac2921 turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents: 49651
diff changeset
    85
        List(XML.Wrapped_Elem(markup, body1, standard_format(body2)))
49468
8b06b99fb85c tuned signature;
wenzelm
parents: 49416
diff changeset
    86
      case XML.Elem(markup, body) => List(XML.Elem(markup, standard_format(body)))
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48704
diff changeset
    87
      case XML.Text(text) => Library.separate(FBreak, split_lines(text).map(XML.Text))
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
    88
    }
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
    89
51493
59d8a1031c00 allow fractional pretty margin -- avoid premature rounding;
wenzelm
parents: 51492
diff changeset
    90
  private val margin_default = 76.0
36820
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    91
51493
59d8a1031c00 allow fractional pretty margin -- avoid premature rounding;
wenzelm
parents: 51492
diff changeset
    92
  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: 51470
diff changeset
    93
    metric: Metric = Metric_Default): XML.Body =
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    94
  {
51568
wenzelm
parents: 51507
diff changeset
    95
    sealed case class Text(tx: XML.Body = Nil, pos: Double = 0.0, nl: Int = 0)
51470
a981a5c8a505 proper metric for blanks -- NB: 70f7483df9cb discontinues coincidence of char_width with space width;
wenzelm
parents: 51469
diff changeset
    96
    {
a981a5c8a505 proper metric for blanks -- NB: 70f7483df9cb discontinues coincidence of char_width with space width;
wenzelm
parents: 51469
diff changeset
    97
      def newline: Text = copy(tx = FBreak :: tx, pos = 0.0, nl = nl + 1)
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: 51470
diff changeset
    98
      def string(s: String): Text = copy(tx = XML.Text(s) :: tx, pos = pos + metric(s))
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: 51470
diff changeset
    99
      def blanks(wd: Int): Text = string(spaces(wd))
51470
a981a5c8a505 proper metric for blanks -- NB: 70f7483df9cb discontinues coincidence of char_width with space width;
wenzelm
parents: 51469
diff changeset
   100
      def content: XML.Body = tx.reverse
a981a5c8a505 proper metric for blanks -- NB: 70f7483df9cb discontinues coincidence of char_width with space width;
wenzelm
parents: 51469
diff changeset
   101
    }
a981a5c8a505 proper metric for blanks -- NB: 70f7483df9cb discontinues coincidence of char_width with space width;
wenzelm
parents: 51469
diff changeset
   102
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   103
    val breakgain = margin / 20
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   104
    val emergencypos = margin / 2
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   105
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   106
    def content_length(tree: XML.Tree): Double =
49651
wenzelm
parents: 49650
diff changeset
   107
      XML.traverse_text(List(tree))(0.0)(_ + metric(_))
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   108
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   109
    def breakdist(trees: XML.Body, after: Double): Double =
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   110
      trees match {
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   111
        case Break(_) :: _ => 0.0
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   112
        case FBreak :: _ => 0.0
36818
wenzelm
parents: 36817
diff changeset
   113
        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
   114
        case Nil => after
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   115
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   116
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   117
    def forcenext(trees: XML.Body): XML.Body =
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   118
      trees match {
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   119
        case Nil => Nil
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   120
        case FBreak :: _ => trees
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   121
        case Break(_) :: ts => FBreak :: ts
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   122
        case t :: ts => t :: forcenext(ts)
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   123
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   124
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   125
    def format(trees: XML.Body, blockin: Double, after: Double, text: Text): Text =
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   126
      trees match {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   127
        case Nil => text
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   128
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   129
        case Block(indent, body) :: ts =>
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   130
          val pos1 = text.pos + indent
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   131
          val pos2 = pos1 % emergencypos
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   132
          val blockin1 =
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   133
            if (pos1 < emergencypos) pos1
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   134
            else pos2
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   135
          val btext = format(body, blockin1, breakdist(ts, after), text)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   136
          val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   137
          format(ts1, blockin, after, btext)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   138
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   139
        case Break(wd) :: ts =>
51568
wenzelm
parents: 51507
diff changeset
   140
          if (text.pos + wd <= ((margin - breakdist(ts, after)) max (blockin + breakgain)))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   141
            format(ts, blockin, after, text.blanks(wd))
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   142
          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
   143
        case FBreak :: ts => format(ts, blockin, after, text.newline.blanks(blockin.toInt))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   144
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   145
        case XML.Wrapped_Elem(markup, body1, body2) :: ts =>
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   146
          val btext = format(body2, blockin, breakdist(ts, after), text.copy(tx = Nil))
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   147
          val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   148
          val btext1 = btext.copy(tx = XML.Wrapped_Elem(markup, body1, btext.content) :: text.tx)
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   149
          format(ts1, blockin, after, btext1)
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   150
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
   151
        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
   152
          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
   153
          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
   154
          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
   155
          format(ts1, blockin, after, btext1)
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   156
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: 51470
diff changeset
   157
        case XML.Text(s) :: ts => format(ts, blockin, after, text.string(s))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   158
      }
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   159
49468
8b06b99fb85c tuned signature;
wenzelm
parents: 49416
diff changeset
   160
    format(standard_format(input), 0.0, 0.0, Text()).content
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   161
  }
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
   162
51493
59d8a1031c00 allow fractional pretty margin -- avoid premature rounding;
wenzelm
parents: 51492
diff changeset
   163
  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: 51470
diff changeset
   164
      metric: Metric = Metric_Default): String =
49416
1053a564dd25 some actual rich text markup via XML.content_markup;
wenzelm
parents: 49414
diff changeset
   165
    XML.content(formatted(input, margin, metric))
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   166
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   167
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   168
  /* unformatted output */
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   169
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   170
  def unformatted(input: XML.Body): XML.Body =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   171
  {
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   172
    def fmt(tree: XML.Tree): XML.Body =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   173
      tree match {
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   174
        case Block(_, body) => body.flatMap(fmt)
48704
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
   175
        case Break(wd) => List(XML.Text(spaces(wd)))
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
   176
        case FBreak => List(XML.Text(space))
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   177
        case XML.Wrapped_Elem(markup, body1, body2) =>
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49473
diff changeset
   178
          List(XML.Wrapped_Elem(markup, body1, body2.flatMap(fmt)))
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
   179
        case XML.Elem(markup, body) => List(XML.Elem(markup, body.flatMap(fmt)))
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   180
        case XML.Text(_) => List(tree)
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   181
      }
49468
8b06b99fb85c tuned signature;
wenzelm
parents: 49416
diff changeset
   182
    standard_format(input).flatMap(fmt)
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   183
  }
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   184
49416
1053a564dd25 some actual rich text markup via XML.content_markup;
wenzelm
parents: 49414
diff changeset
   185
  def str_of(input: XML.Body): String = XML.content(unformatted(input))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
   186
}