| author | wenzelm | 
| Wed, 02 Apr 2025 13:39:12 +0200 | |
| changeset 82413 | a6046b6d23b4 | 
| parent 81718 | 289ded3c342f | 
| child 82988 | 71ffc2c22348 | 
| 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  | 
||
| 71601 | 9  | 
import scala.annotation.tailrec  | 
10  | 
||
| 36683 | 11  | 
|
| 75393 | 12  | 
object Pretty {
 | 
| 61871 | 13  | 
/* XML constructors */  | 
14  | 
||
| 61874 | 15  | 
val space: XML.Body = List(XML.Text(Symbol.space))  | 
16  | 
def spaces(n: Int): XML.Body =  | 
|
17  | 
if (n == 0) Nil  | 
|
18  | 
else if (n == 1) space  | 
|
19  | 
else List(XML.Text(Symbol.spaces(n)))  | 
|
| 
48704
 
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
 
wenzelm 
parents: 
45666 
diff
changeset
 | 
20  | 
|
| 80940 | 21  | 
val bullet: XML.Body = XML.elem(Markup.BULLET, space) :: space  | 
22  | 
||
| 
81121
 
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
 
wenzelm 
parents: 
81120 
diff
changeset
 | 
23  | 
def block(body: XML.Body,  | 
| 
 
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
 
wenzelm 
parents: 
81120 
diff
changeset
 | 
24  | 
consistent: Boolean = false,  | 
| 
 
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
 
wenzelm 
parents: 
81120 
diff
changeset
 | 
25  | 
indent: Int = 2  | 
| 81395 | 26  | 
): XML.Elem = XML.Elem(Markup.Block(consistent = consistent, indent = indent), body)  | 
| 
48704
 
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
 
wenzelm 
parents: 
45666 
diff
changeset
 | 
27  | 
|
| 81395 | 28  | 
def string(s: String): XML.Elem = block(XML.string(s), indent = 0)  | 
29  | 
||
30  | 
def brk(width: Int, indent: Int = 0): XML.Elem =  | 
|
| 81120 | 31  | 
XML.Elem(Markup.Break(width = width, indent = indent), spaces(width))  | 
| 61871 | 32  | 
|
| 69867 | 33  | 
val fbrk: XML.Tree = XML.newline  | 
| 65130 | 34  | 
def fbreaks(ts: List[XML.Tree]): XML.Body = Library.separate(fbrk, ts)  | 
| 61871 | 35  | 
|
36  | 
val Separator: XML.Body = List(XML.elem(Markup.SEPARATOR, space), fbrk)  | 
|
| 75958 | 37  | 
def separate(ts: List[XML.Tree], sep: XML.Body = Separator): XML.Body =  | 
38  | 
Library.separate(sep, ts.map(List(_))).flatten  | 
|
39  | 
||
40  | 
  val comma: XML.Body = List(XML.Text(","), brk(1))
 | 
|
41  | 
def commas(ts: List[XML.Tree]): XML.Body = separate(ts, sep = comma)  | 
|
42  | 
||
43  | 
def `enum`(ts: List[XML.Tree],  | 
|
44  | 
    bg: String = "(",
 | 
|
45  | 
en: String = ")",  | 
|
46  | 
sep: XML.Body = comma,  | 
|
47  | 
indent: Int = 2  | 
|
| 81395 | 48  | 
): XML.Elem = Pretty.block(XML.enclose(bg, en, separate(ts, sep = sep)), indent = indent)  | 
| 61864 | 49  | 
|
| 
48704
 
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
 
wenzelm 
parents: 
45666 
diff
changeset
 | 
50  | 
|
| 
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
 | 
51  | 
/* 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
 | 
52  | 
|
| 75393 | 53  | 
  abstract class Metric {
 | 
| 81340 | 54  | 
def unit: Double  | 
| 
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
 | 
55  | 
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
 | 
56  | 
}  | 
| 
 
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
 | 
57  | 
|
| 
 
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
 | 
58  | 
|
| 36687 | 59  | 
/* markup trees with physical blocks and breaks */  | 
60  | 
||
| 62820 | 61  | 
private def force_nat(i: Int): Int = i max 0  | 
62  | 
||
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
63  | 
type Markup_Body = (Markup, Option[XML.Body])  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
64  | 
val no_markup: Markup_Body = (Markup.Empty, None)  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
65  | 
val item_markup: Markup_Body = (Markup.Expression.item, None)  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
66  | 
|
| 61874 | 67  | 
  private sealed abstract class Tree { def length: Double }
 | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
68  | 
  private case object End extends Tree {
 | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
69  | 
override def length: Double = 0.0  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
70  | 
}  | 
| 61874 | 71  | 
private case class Block(  | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
72  | 
markup: Markup_Body,  | 
| 
81121
 
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
 
wenzelm 
parents: 
81120 
diff
changeset
 | 
73  | 
open_block: Boolean,  | 
| 80798 | 74  | 
consistent: Boolean,  | 
75  | 
indent: Int,  | 
|
76  | 
body: List[Tree],  | 
|
77  | 
length: Double  | 
|
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
78  | 
) extends Tree  | 
| 75393 | 79  | 
  private case class Break(force: Boolean, width: Int, indent: Int) extends Tree {
 | 
80  | 
def length: Double = width.toDouble  | 
|
81  | 
}  | 
|
| 61874 | 82  | 
private case class Str(string: String, length: Double) extends Tree  | 
| 36683 | 83  | 
|
| 61874 | 84  | 
private val FBreak = Break(true, 1, 0)  | 
| 36683 | 85  | 
|
| 80940 | 86  | 
private def make_block(body: List[Tree],  | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
87  | 
markup: Markup_Body = no_markup,  | 
| 
81121
 
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
 
wenzelm 
parents: 
81120 
diff
changeset
 | 
88  | 
open_block: Boolean = false,  | 
| 80940 | 89  | 
consistent: Boolean = false,  | 
90  | 
indent: Int = 0  | 
|
| 75393 | 91  | 
  ): Tree = {
 | 
| 
62785
 
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
 
wenzelm 
parents: 
61883 
diff
changeset
 | 
92  | 
val indent1 = force_nat(indent)  | 
| 
 
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
 
wenzelm 
parents: 
61883 
diff
changeset
 | 
93  | 
|
| 75393 | 94  | 
    @tailrec def body_length(prts: List[Tree], len: Double): Double = {
 | 
| 
61883
 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 
wenzelm 
parents: 
61875 
diff
changeset
 | 
95  | 
val (line, rest) =  | 
| 
 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 
wenzelm 
parents: 
61875 
diff
changeset
 | 
96  | 
        Library.take_prefix[Tree]({ case Break(true, _, _) => false case _ => true }, prts)
 | 
| 73359 | 97  | 
      val len1 = (line.foldLeft(0.0) { case (l, t) => l + t.length }) max len
 | 
| 71781 | 98  | 
      (rest: @unchecked) match {
 | 
| 
61883
 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 
wenzelm 
parents: 
61875 
diff
changeset
 | 
99  | 
case Break(true, _, ind) :: rest1 =>  | 
| 
62785
 
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
 
wenzelm 
parents: 
61883 
diff
changeset
 | 
100  | 
body_length(Break(false, indent1 + ind, 0) :: rest1, len1)  | 
| 
61883
 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 
wenzelm 
parents: 
61875 
diff
changeset
 | 
101  | 
case Nil => len1  | 
| 
 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 
wenzelm 
parents: 
61875 
diff
changeset
 | 
102  | 
}  | 
| 
 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 
wenzelm 
parents: 
61875 
diff
changeset
 | 
103  | 
}  | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
104  | 
Block(markup, open_block, consistent, indent1, body, body_length(body, 0.0))  | 
| 
61883
 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 
wenzelm 
parents: 
61875 
diff
changeset
 | 
105  | 
}  | 
| 36683 | 106  | 
|
| 61874 | 107  | 
|
| 80845 | 108  | 
/* no formatting */  | 
| 
76086
 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 
wenzelm 
parents: 
75958 
diff
changeset
 | 
109  | 
|
| 80871 | 110  | 
def output_content(pure: Boolean, output: XML.Body): String =  | 
111  | 
XML.content(if (pure) Protocol_Message.clean_output(output) else output)  | 
|
112  | 
||
| 80807 | 113  | 
def unbreakable(input: XML.Body): XML.Body =  | 
| 
76086
 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 
wenzelm 
parents: 
75958 
diff
changeset
 | 
114  | 
    input flatMap {
 | 
| 81695 | 115  | 
case XML.Wrapped_Elem(markup1, markup2, body) =>  | 
116  | 
List(XML.Wrapped_Elem(markup1, markup2, unbreakable(body)))  | 
|
| 80807 | 117  | 
case XML.Elem(Markup.Break(width, _), _) => spaces(width)  | 
118  | 
case XML.Elem(markup, body) => List(XML.Elem(markup, unbreakable(body)))  | 
|
| 
76086
 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 
wenzelm 
parents: 
75958 
diff
changeset
 | 
119  | 
case XML.Text(text) => XML.string(split_lines(text).mkString(Symbol.space))  | 
| 
 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 
wenzelm 
parents: 
75958 
diff
changeset
 | 
120  | 
}  | 
| 80807 | 121  | 
|
| 80871 | 122  | 
def unformatted_string_of(input: XML.Body, pure: Boolean = false): String =  | 
123  | 
output_content(pure, unbreakable(input))  | 
|
| 
76086
 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 
wenzelm 
parents: 
75958 
diff
changeset
 | 
124  | 
|
| 
 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 
wenzelm 
parents: 
75958 
diff
changeset
 | 
125  | 
|
| 80845 | 126  | 
/* formatting */  | 
| 61874 | 127  | 
|
| 
81710
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
128  | 
  private sealed abstract class Tree_Buffer { def result: XML.Tree }
 | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
129  | 
  private case class Elem_Buffer(markup: Markup_Body, body: XML.Body) extends Tree_Buffer {
 | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
130  | 
def result: XML.Elem =  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
131  | 
      markup match {
 | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
132  | 
case (m, None) => XML.Elem(m, body)  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
133  | 
case (m1, Some(m2)) => XML.Wrapped_Elem(m1, m2, body)  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
134  | 
}  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
135  | 
}  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
136  | 
  private case class Text_Buffer(buffer: List[String]) extends Tree_Buffer {
 | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
137  | 
def result: XML.Text = XML.Text(buffer.reverse.mkString)  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
138  | 
|
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
139  | 
def add(s: String): Text_Buffer =  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
140  | 
if (s.isEmpty) this else copy(buffer = s :: buffer)  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
141  | 
}  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
142  | 
|
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
143  | 
private case class Result_Buffer(  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
144  | 
markup: Markup_Body = no_markup,  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
145  | 
buffer: List[Tree_Buffer] = Nil  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
146  | 
  ) {
 | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
147  | 
def result_body: XML.Body = buffer.foldLeft[XML.Body](Nil)((res, t) => t.result :: res)  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
148  | 
def result_elem: Elem_Buffer = Elem_Buffer(markup, result_body)  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
149  | 
|
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
150  | 
def add(elem: Elem_Buffer): Result_Buffer = copy(buffer = elem :: buffer)  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
151  | 
|
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
152  | 
def string(s: String): Result_Buffer =  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
153  | 
if (s.isEmpty) this  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
154  | 
      else {
 | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
155  | 
        buffer match {
 | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
156  | 
case (text: Text_Buffer) :: ts => copy(buffer = text.add(s) :: ts)  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
157  | 
case _ => copy(buffer = Text_Buffer(List(s)) :: buffer)  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
158  | 
}  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
159  | 
}  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
160  | 
}  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
161  | 
|
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
162  | 
private type State = List[Result_Buffer]  | 
| 
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
163  | 
private val init_state: State = List(Result_Buffer())  | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
164  | 
|
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
165  | 
private sealed case class Text(  | 
| 81718 | 166  | 
main: State = init_state,  | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
167  | 
pos: Double = 0.0,  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
168  | 
nl: Int = 0  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
169  | 
  ) {
 | 
| 
81710
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
170  | 
def add(elem: Elem_Buffer): Text =  | 
| 81718 | 171  | 
      (main: @unchecked) match {
 | 
172  | 
case res :: rest => copy(main = res.add(elem) :: rest)  | 
|
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
173  | 
}  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
174  | 
|
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
175  | 
def push(m: Markup_Body): Text =  | 
| 81718 | 176  | 
copy(main = Result_Buffer(markup = m) :: main)  | 
| 81685 | 177  | 
|
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
178  | 
def pop: Text =  | 
| 81718 | 179  | 
      (main: @unchecked) match {
 | 
180  | 
case res1 :: res2 :: rest => copy(main = res2.add(res1.result_elem) :: rest)  | 
|
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
181  | 
}  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
182  | 
|
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
183  | 
def result: XML.Body =  | 
| 81718 | 184  | 
      (main: @unchecked) match {
 | 
| 
81710
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
185  | 
case List(res) if res.markup == no_markup => res.result_body  | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
186  | 
}  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
187  | 
|
| 81718 | 188  | 
def reset: Text = copy(main = init_state)  | 
189  | 
def restore(other: Text): Text = copy(main = other.main)  | 
|
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
190  | 
|
| 61874 | 191  | 
def string(s: String, len: Double): Text =  | 
| 81718 | 192  | 
      (main: @unchecked) match {
 | 
193  | 
case res :: rest => copy(main = res.string(s) :: rest, pos = pos + len)  | 
|
| 
81710
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
194  | 
}  | 
| 61874 | 195  | 
def blanks(wd: Int): Text = string(Symbol.spaces(wd), wd.toDouble)  | 
| 
81710
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
196  | 
    def newline: Text = string("\n", 0.0).copy(pos = 0.0, nl = nl + 1)
 | 
| 61874 | 197  | 
}  | 
198  | 
||
199  | 
private def break_dist(trees: List[Tree], after: Double): Double =  | 
|
200  | 
    trees match {
 | 
|
201  | 
case (_: Break) :: _ => 0.0  | 
|
202  | 
case t :: ts => t.length + break_dist(ts, after)  | 
|
203  | 
case Nil => after  | 
|
| 
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
 | 
204  | 
}  | 
| 
 
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
 | 
205  | 
|
| 61874 | 206  | 
private def force_break(tree: Tree): Tree =  | 
207  | 
    tree match { case Break(false, wd, ind) => Break(true, wd, ind) case _ => tree }
 | 
|
| 71601 | 208  | 
private def force_all(trees: List[Tree]): List[Tree] = trees.map(force_break)  | 
| 
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
 | 
209  | 
|
| 61874 | 210  | 
private def force_next(trees: List[Tree]): List[Tree] =  | 
211  | 
    trees match {
 | 
|
212  | 
case Nil => Nil  | 
|
213  | 
case (t: Break) :: ts => force_break(t) :: ts  | 
|
214  | 
case t :: ts => t :: force_next(ts)  | 
|
215  | 
}  | 
|
| 
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
 | 
216  | 
|
| 71601 | 217  | 
val default_margin: Double = 76.0  | 
218  | 
val default_breakgain: Double = default_margin / 20  | 
|
| 36820 | 219  | 
|
| 
67547
 
aefe7a7b330a
clarified breakgain: keeping it constant avoids margin fluctuation in Pretty_Tooltip vs. Pretty_Text_Area;
 
wenzelm 
parents: 
65130 
diff
changeset
 | 
220  | 
def formatted(input: XML.Body,  | 
| 67896 | 221  | 
margin: Double = default_margin,  | 
222  | 
breakgain: Double = default_breakgain,  | 
|
| 81346 | 223  | 
metric: Metric = Codepoint.Metric  | 
| 75393 | 224  | 
  ): XML.Body = {
 | 
| 
51569
 
4e084727faae
maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
 
wenzelm 
parents: 
51568 
diff
changeset
 | 
225  | 
val emergencypos = (margin / 2).round.toInt  | 
| 36687 | 226  | 
|
| 61874 | 227  | 
def make_tree(inp: XML.Body): List[Tree] =  | 
228  | 
      inp flatMap {
 | 
|
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
229  | 
case XML.Wrapped_Elem(markup1, markup2, body) =>  | 
| 
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
230  | 
List(make_block(make_tree(body), markup = (markup1, Some(markup2)), open_block = true))  | 
| 61874 | 231  | 
case XML.Elem(markup, body) =>  | 
232  | 
          markup match {
 | 
|
| 
81294
 
108284c8cbfd
removed obsolete markup for "open_block" (see also d5ad89fda714): Isabelle/Scala directly supports XML.Elem pretty-printing;
 
wenzelm 
parents: 
81121 
diff
changeset
 | 
233  | 
case Markup.Block(consistent, indent) =>  | 
| 81685 | 234  | 
List(make_block(make_tree(body), consistent = consistent, indent = indent))  | 
| 61874 | 235  | 
case Markup.Break(width, indent) =>  | 
| 
62785
 
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
 
wenzelm 
parents: 
61883 
diff
changeset
 | 
236  | 
List(Break(false, force_nat(width), force_nat(indent)))  | 
| 61874 | 237  | 
case Markup(Markup.ITEM, _) =>  | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
238  | 
List(make_block(make_tree(bullet ::: body), markup = item_markup, indent = 2))  | 
| 61874 | 239  | 
case _ =>  | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
240  | 
List(make_block(make_tree(body), markup = (markup, None), open_block = true))  | 
| 61874 | 241  | 
}  | 
242  | 
case XML.Text(text) =>  | 
|
243  | 
Library.separate(FBreak, split_lines(text).map(s => Str(s, metric(s))))  | 
|
| 
36817
 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 
wenzelm 
parents: 
36763 
diff
changeset
 | 
244  | 
}  | 
| 
 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 
wenzelm 
parents: 
36763 
diff
changeset
 | 
245  | 
|
| 
81698
 
17f1a78af3f5
more accurate indentation: retain (before: Double) until it is materialized as blanks;
 
wenzelm 
parents: 
81697 
diff
changeset
 | 
246  | 
def format(trees: List[Tree], before: Double, after: Double, text: Text): Text =  | 
| 36687 | 247  | 
      trees match {
 | 
248  | 
case Nil => text  | 
|
| 81697 | 249  | 
case End :: ts => format(ts, before, after, text.pop)  | 
| 
81121
 
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
 
wenzelm 
parents: 
81120 
diff
changeset
 | 
250  | 
case (block: Block) :: ts if block.open_block =>  | 
| 81697 | 251  | 
format(block.body ::: End :: ts, before, after, text.push(block.markup))  | 
| 81689 | 252  | 
case (block: Block) :: ts =>  | 
| 
81698
 
17f1a78af3f5
more accurate indentation: retain (before: Double) until it is materialized as blanks;
 
wenzelm 
parents: 
81697 
diff
changeset
 | 
253  | 
val pos1 = text.pos + block.indent  | 
| 
 
17f1a78af3f5
more accurate indentation: retain (before: Double) until it is materialized as blanks;
 
wenzelm 
parents: 
81697 
diff
changeset
 | 
254  | 
val pos2 = (pos1.round.toInt % emergencypos).toDouble  | 
| 81697 | 255  | 
val before1 = if (pos1 < emergencypos) pos1 else pos2  | 
| 81687 | 256  | 
val after1 = break_dist(ts, after)  | 
| 81689 | 257  | 
val body1 =  | 
258  | 
if (block.consistent && text.pos + block.length > margin - after1) force_all(block.body)  | 
|
259  | 
else block.body  | 
|
| 81687 | 260  | 
val btext1 =  | 
| 81697 | 261  | 
if (block.markup == no_markup) format(body1, before1, after1, text)  | 
| 80800 | 262  | 
            else {
 | 
| 81697 | 263  | 
val btext = format(body1, before1, after1, text.reset)  | 
| 
81710
 
c914db7419a3
misc tuning and clarification: more explicit types;
 
wenzelm 
parents: 
81698 
diff
changeset
 | 
264  | 
val elem = Elem_Buffer(block.markup, btext.result)  | 
| 
81694
 
75886eea238a
more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
 
wenzelm 
parents: 
81692 
diff
changeset
 | 
265  | 
btext.restore(text.add(elem))  | 
| 61871 | 266  | 
}  | 
| 81687 | 267  | 
val ts1 = if (text.nl < btext1.nl) force_next(ts) else ts  | 
| 81697 | 268  | 
format(ts1, before, after, btext1)  | 
| 61871 | 269  | 
case Break(force, wd, ind) :: ts =>  | 
270  | 
if (!force &&  | 
|
| 81697 | 271  | 
text.pos + wd <= ((margin - break_dist(ts, after)) max (before + breakgain)))  | 
272  | 
format(ts, before, after, text.blanks(wd))  | 
|
| 
81698
 
17f1a78af3f5
more accurate indentation: retain (before: Double) until it is materialized as blanks;
 
wenzelm 
parents: 
81697 
diff
changeset
 | 
273  | 
else format(ts, before, after, text.newline.blanks((before + ind).ceil.toInt))  | 
| 81697 | 274  | 
case Str(s, len) :: ts => format(ts, before, after, text.string(s, len))  | 
| 36687 | 275  | 
}  | 
| 
81698
 
17f1a78af3f5
more accurate indentation: retain (before: Double) until it is materialized as blanks;
 
wenzelm 
parents: 
81697 
diff
changeset
 | 
276  | 
format(make_tree(input), 0.0, 0.0, Text()).result  | 
| 36687 | 277  | 
}  | 
| 
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
 | 
278  | 
|
| 
67547
 
aefe7a7b330a
clarified breakgain: keeping it constant avoids margin fluctuation in Pretty_Tooltip vs. Pretty_Text_Area;
 
wenzelm 
parents: 
65130 
diff
changeset
 | 
279  | 
def string_of(input: XML.Body,  | 
| 80871 | 280  | 
margin: Double = default_margin,  | 
281  | 
breakgain: Double = default_breakgain,  | 
|
| 81346 | 282  | 
metric: Metric = Codepoint.Metric,  | 
| 80871 | 283  | 
pure: Boolean = false  | 
284  | 
  ): String = {
 | 
|
285  | 
output_content(pure, formatted(input, margin = margin, breakgain = breakgain, metric = metric))  | 
|
286  | 
}  | 
|
| 36683 | 287  | 
}  |