src/Pure/General/pretty.scala
author wenzelm
Sat, 19 Dec 2015 14:47:52 +0100
changeset 61864 3a5992c3410c
parent 61862 e2a9e46ac0fb
child 61865 6dcc9e4f1aa6
permissions -rw-r--r--
support for blocks with consistent breaks; 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
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    10
object Pretty
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    11
{
48704
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    12
  /* spaces */
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    13
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    14
  val space = " "
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    15
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    16
  private val static_spaces = space * 4000
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    17
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    18
  def spaces(k: Int): String =
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    19
  {
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    20
    require(k >= 0)
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    21
    if (k < static_spaces.length) static_spaces.substring(0, k)
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    22
    else space * k
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    23
  }
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    24
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    25
  def spaces_body(k: Int): XML.Body =
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    26
    if (k == 0) Nil
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    27
    else List(XML.Text(spaces(k)))
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    28
48704
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    29
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
    30
  /* 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
    31
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
  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
    33
  {
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
    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
    35
    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
    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
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
  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
    39
  {
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
    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
    41
    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
    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
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
    44
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    45
  /* markup trees with physical blocks and breaks */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    46
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    47
  def block(body: XML.Body): XML.Tree = Block(false, 2, body)
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    48
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    49
  object Block
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    50
  {
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    51
    def apply(consistent: Boolean, indent: Int, body: XML.Body): XML.Tree =
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    52
      XML.Elem(Markup.Block(consistent, indent), body)
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    53
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    54
    def unapply(tree: XML.Tree): Option[(Boolean, Int, XML.Body)] =
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    55
      tree match {
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    56
        case XML.Elem(Markup.Block(consistent, indent), body) => Some((consistent, indent, 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
  {
61862
e2a9e46ac0fb support pretty break indent, like underlying ML systems;
wenzelm
parents: 55551
diff changeset
    63
    def apply(w: Int, i: Int = 0): XML.Tree =
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    64
      XML.Elem(Markup.Break(w, i), spaces_body(w))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    65
61862
e2a9e46ac0fb support pretty break indent, like underlying ML systems;
wenzelm
parents: 55551
diff changeset
    66
    def unapply(tree: XML.Tree): Option[(Int, Int)] =
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    67
      tree match {
61862
e2a9e46ac0fb support pretty break indent, like underlying ML systems;
wenzelm
parents: 55551
diff changeset
    68
        case XML.Elem(Markup.Break(w, i), _) => Some((w, i))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    69
        case _ => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    70
      }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    71
  }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    72
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
    73
  val FBreak = XML.Text("\n")
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    74
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: 51569
diff changeset
    75
  def item(body: XML.Body): XML.Tree =
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    76
    Block(false, 2, XML.elem(Markup.BULLET, List(XML.Text(space))) :: XML.Text(space) :: body)
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: 51569
diff changeset
    77
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49843
diff changeset
    78
  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
    79
  def separate(ts: List[XML.Tree]): XML.Body = Library.separate(Separator, ts.map(List(_))).flatten
49473
ca7e2c21b104 tuned rendering;
wenzelm
parents: 49468
diff changeset
    80
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    81
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: 51569
diff changeset
    82
  /* standard form */
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    83
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: 51569
diff changeset
    84
  def standard_form(body: XML.Body): XML.Body =
49468
8b06b99fb85c tuned signature;
wenzelm
parents: 49416
diff changeset
    85
    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
    86
      case XML.Wrapped_Elem(markup, body1, body2) =>
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: 51569
diff changeset
    87
        List(XML.Wrapped_Elem(markup, body1, standard_form(body2)))
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: 51569
diff changeset
    88
      case XML.Elem(markup, body) =>
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: 51569
diff changeset
    89
        if (markup.name == Markup.ITEM) List(item(standard_form(body)))
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: 51569
diff changeset
    90
        else List(XML.Elem(markup, standard_form(body)))
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48704
diff changeset
    91
      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
    92
    }
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
    93
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: 51569
diff changeset
    94
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: 51569
diff changeset
    95
  /* formatted output */
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: 51569
diff changeset
    96
51493
59d8a1031c00 allow fractional pretty margin -- avoid premature rounding;
wenzelm
parents: 51492
diff changeset
    97
  private val margin_default = 76.0
36820
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    98
51493
59d8a1031c00 allow fractional pretty margin -- avoid premature rounding;
wenzelm
parents: 51492
diff changeset
    99
  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
   100
    metric: Metric = Metric_Default): XML.Body =
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   101
  {
51568
wenzelm
parents: 51507
diff changeset
   102
    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
   103
    {
a981a5c8a505 proper metric for blanks -- NB: 70f7483df9cb discontinues coincidence of char_width with space width;
wenzelm
parents: 51469
diff changeset
   104
      def newline: Text = copy(tx = FBreak :: tx, pos = 0.0, nl = nl + 1)
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   105
      def string(s: String): Text =
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   106
        if (s == "") this
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   107
        else copy(tx = XML.Text(s) :: tx, pos = pos + metric(s))
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
   108
      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
   109
      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
   110
    }
a981a5c8a505 proper metric for blanks -- NB: 70f7483df9cb discontinues coincidence of char_width with space width;
wenzelm
parents: 51469
diff changeset
   111
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   112
    val breakgain = margin / 20
51569
4e084727faae maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
wenzelm
parents: 51568
diff changeset
   113
    val emergencypos = (margin / 2).round.toInt
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   114
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   115
    def content_length(body: XML.Body): Double =
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   116
      XML.traverse_text(body)(0.0)(_ + metric(_))
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   117
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   118
    def break_dist(trees: XML.Body, after: Double): Double =
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   119
      trees match {
61862
e2a9e46ac0fb support pretty break indent, like underlying ML systems;
wenzelm
parents: 55551
diff changeset
   120
        case Break(_, _) :: _ => 0.0
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   121
        case FBreak :: _ => 0.0
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   122
        case t :: ts => content_length(List(t)) + break_dist(ts, after)
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   123
        case Nil => after
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   124
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   125
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   126
    def force_all(trees: XML.Body): XML.Body =
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   127
      trees flatMap {
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   128
        case Break(_, ind) => FBreak :: spaces_body(ind)
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   129
        case tree => List(tree)
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   130
      }
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   131
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   132
    def force_next(trees: XML.Body): XML.Body =
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   133
      trees match {
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   134
        case Nil => Nil
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   135
        case FBreak :: _ => trees
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   136
        case Break(_, ind) :: ts => FBreak :: spaces_body(ind) ::: ts
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   137
        case t :: ts => t :: force_next(ts)
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   138
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   139
51569
4e084727faae maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
wenzelm
parents: 51568
diff changeset
   140
    def format(trees: XML.Body, blockin: Int, after: Double, text: Text): Text =
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   141
      trees match {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   142
        case Nil => text
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   143
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   144
        case Block(consistent, indent, body) :: ts =>
51569
4e084727faae maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
wenzelm
parents: 51568
diff changeset
   145
          val pos1 = (text.pos + indent).ceil.toInt
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   146
          val pos2 = pos1 % emergencypos
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   147
          val blockin1 =
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   148
            if (pos1 < emergencypos) pos1
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   149
            else pos2
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   150
          val blen = content_length(body)
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   151
          val d = break_dist(ts, after)
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   152
          val body1 = if (consistent && text.pos + blen > margin - d) force_all(body) else body
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   153
          val btext = format(body1, blockin1, d, text)
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   154
          val ts1 = if (text.nl < btext.nl) force_next(ts) else ts
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   155
          format(ts1, blockin, after, btext)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   156
61862
e2a9e46ac0fb support pretty break indent, like underlying ML systems;
wenzelm
parents: 55551
diff changeset
   157
        case Break(wd, ind) :: ts =>
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   158
          if (text.pos + wd <= ((margin - break_dist(ts, after)) max (blockin + breakgain)))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   159
            format(ts, blockin, after, text.blanks(wd))
61862
e2a9e46ac0fb support pretty break indent, like underlying ML systems;
wenzelm
parents: 55551
diff changeset
   160
          else format(ts, blockin, after, text.newline.blanks(blockin + ind))
51569
4e084727faae maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
wenzelm
parents: 51568
diff changeset
   161
        case FBreak :: ts => format(ts, blockin, after, text.newline.blanks(blockin))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   162
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
   163
        case XML.Wrapped_Elem(markup, body1, body2) :: ts =>
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   164
          val btext = format(body2, blockin, break_dist(ts, after), text.copy(tx = Nil))
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   165
          val ts1 = if (text.nl < btext.nl) force_next(ts) else ts
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
   166
          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
   167
          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
   168
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
   169
        case XML.Elem(markup, body) :: ts =>
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   170
          val btext = format(body, blockin, break_dist(ts, after), text.copy(tx = Nil))
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   171
          val ts1 = if (text.nl < btext.nl) force_next(ts) else ts
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
   172
          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
   173
          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
   174
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
   175
        case XML.Text(s) :: ts => format(ts, blockin, after, text.string(s))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   176
      }
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
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: 51569
diff changeset
   178
    format(standard_form(input), 0, 0.0, Text()).content
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   179
  }
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
   180
51493
59d8a1031c00 allow fractional pretty margin -- avoid premature rounding;
wenzelm
parents: 51492
diff changeset
   181
  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
   182
      metric: Metric = Metric_Default): String =
49416
1053a564dd25 some actual rich text markup via XML.content_markup;
wenzelm
parents: 49414
diff changeset
   183
    XML.content(formatted(input, margin, metric))
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   184
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   185
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   186
  /* unformatted output */
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   187
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   188
  def unformatted(input: XML.Body): XML.Body =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   189
  {
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   190
    def fmt(tree: XML.Tree): XML.Body =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   191
      tree match {
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   192
        case Block(_, _, body) => body.flatMap(fmt)
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
   193
        case Break(wd, _) => spaces_body(wd)
48704
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
   194
        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
   195
        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
   196
          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
   197
        case XML.Elem(markup, body) => List(XML.Elem(markup, body.flatMap(fmt)))
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   198
        case XML.Text(_) => List(tree)
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   199
      }
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: 51569
diff changeset
   200
    standard_form(input).flatMap(fmt)
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   201
  }
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   202
49416
1053a564dd25 some actual rich text markup via XML.content_markup;
wenzelm
parents: 49414
diff changeset
   203
  def str_of(input: XML.Body): String = XML.content(unformatted(input))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
   204
}