src/Pure/General/pretty.ML
author wenzelm
Sun Jan 21 19:53:29 2001 +0100 (2001-01-21)
changeset 10952 b520e4f1313b
parent 9730 11d137b25555
child 12421 54c06c1f88b8
permissions -rw-r--r--
support general indentation (e.g. for non-tt latex output);
tuned;
wenzelm@6118
     1
(*  Title:      Pure/General/pretty.ML
wenzelm@6116
     2
    ID:         $Id$
wenzelm@8806
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@10952
     4
    Author:     Markus Wenzel, TU Munich
wenzelm@8806
     5
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@6116
     6
wenzelm@6116
     7
Generic pretty printing module.
wenzelm@6116
     8
wenzelm@6116
     9
Loosely based on
wenzelm@6116
    10
  D. C. Oppen, "Pretty Printing",
wenzelm@6116
    11
  ACM Transactions on Programming Languages and Systems (1980), 465-483.
wenzelm@6116
    12
wenzelm@6116
    13
The object to be printed is given as a tree with indentation and line
wenzelm@6116
    14
breaking information.  A "break" inserts a newline if the text until
wenzelm@6116
    15
the next break is too long to fit on the current line.  After the newline,
wenzelm@6116
    16
text is indented to the level of the enclosing block.  Normally, if a block
wenzelm@6116
    17
is broken then all enclosing blocks will also be broken.  Only "inconsistent
wenzelm@6116
    18
breaks" are provided.
wenzelm@6116
    19
wenzelm@6116
    20
The stored length of a block is used in breakdist (to treat each inner block as
wenzelm@6116
    21
a unit for breaking).
wenzelm@6116
    22
*)
wenzelm@6116
    23
wenzelm@6116
    24
type pprint_args = (string -> unit) * (int -> unit) * (int -> unit) *
wenzelm@6116
    25
  (unit -> unit) * (unit -> unit);
wenzelm@6116
    26
wenzelm@6116
    27
signature PRETTY =
wenzelm@6116
    28
  sig
wenzelm@6116
    29
  type T
wenzelm@8456
    30
  val raw_str: string * real -> T
wenzelm@6116
    31
  val str: string -> T
wenzelm@6116
    32
  val brk: int -> T
wenzelm@6116
    33
  val fbrk: T
wenzelm@6116
    34
  val blk: int * T list -> T
wenzelm@6116
    35
  val quote: T -> T
wenzelm@6116
    36
  val commas: T list -> T list
wenzelm@6116
    37
  val breaks: T list -> T list
wenzelm@6116
    38
  val fbreaks: T list -> T list
wenzelm@6116
    39
  val block: T list -> T
wenzelm@6116
    40
  val strs: string list -> T
wenzelm@6116
    41
  val enclose: string -> string -> T list -> T
wenzelm@6116
    42
  val list: string -> string -> T list -> T
wenzelm@6116
    43
  val str_list: string -> string -> string list -> T
wenzelm@6116
    44
  val big_list: string -> T list -> T
wenzelm@8456
    45
  val chunks: T list -> T
wenzelm@9730
    46
  val indent: int -> T -> T
wenzelm@6116
    47
  val string_of: T -> string
wenzelm@6116
    48
  val writeln: T -> unit
wenzelm@6116
    49
  val str_of: T -> string
wenzelm@6116
    50
  val pprint: T -> pprint_args -> unit
wenzelm@6116
    51
  val setdepth: int -> unit
wenzelm@6116
    52
  val setmargin: int -> unit
wenzelm@6321
    53
  val setmp_margin: int -> ('a -> 'b) -> 'a -> 'b
wenzelm@6116
    54
  end;
wenzelm@6116
    55
wenzelm@6116
    56
structure Pretty : PRETTY =
wenzelm@6116
    57
struct
wenzelm@6116
    58
wenzelm@10952
    59
wenzelm@10952
    60
(** printing items: compound phrases, strings, and breaks **)
wenzelm@10952
    61
wenzelm@6116
    62
datatype T =
wenzelm@6116
    63
  Block of T list * int * int |         (*body, indentation, length*)
wenzelm@6116
    64
  String of string * int |              (*text, length*)
wenzelm@6116
    65
  Break of bool * int;                  (*mandatory flag, width if not taken*);
wenzelm@6116
    66
wenzelm@9730
    67
wenzelm@6116
    68
wenzelm@10952
    69
(** output text **)
wenzelm@10952
    70
wenzelm@10952
    71
val output_spaces = Symbol.output o Symbol.spaces;
wenzelm@10952
    72
val add_indent = Buffer.add o output_spaces;
wenzelm@10952
    73
fun set_indent wd = Buffer.empty |> add_indent wd;
wenzelm@6116
    74
wenzelm@10952
    75
val empty =
wenzelm@10952
    76
 {tx = Buffer.empty,
wenzelm@10952
    77
  ind = Buffer.empty,
wenzelm@10952
    78
  pos = 0,
wenzelm@10952
    79
  nl = 0};
wenzelm@6116
    80
wenzelm@10952
    81
fun newline {tx, ind, pos, nl} =
wenzelm@10952
    82
 {tx = Buffer.add (Symbol.output "\n") tx,
wenzelm@10952
    83
  ind = Buffer.empty,
wenzelm@10952
    84
  pos = 0,
wenzelm@10952
    85
  nl = nl + 1};
wenzelm@6116
    86
wenzelm@10952
    87
fun string (s, len) {tx, ind, pos: int, nl} =
wenzelm@10952
    88
 {tx = Buffer.add s tx,
wenzelm@10952
    89
  ind = Buffer.add s ind,
wenzelm@10952
    90
  pos = pos + len,
wenzelm@10952
    91
  nl = nl};
wenzelm@10952
    92
wenzelm@10952
    93
fun blanks wd = string (output_spaces wd, wd);
wenzelm@6116
    94
wenzelm@10952
    95
fun indentation (buf, len) {tx, ind, pos, nl} =
wenzelm@10952
    96
  let val s = Buffer.content buf in
wenzelm@10952
    97
   {tx = Buffer.add (Symbol.indent (s, len)) tx,
wenzelm@10952
    98
    ind = Buffer.add s ind,
wenzelm@10952
    99
    pos = pos + len,
wenzelm@10952
   100
    nl = nl}
wenzelm@10952
   101
  end;
wenzelm@6116
   102
wenzelm@6116
   103
wenzelm@10952
   104
wenzelm@10952
   105
(** formatting **)
wenzelm@6116
   106
wenzelm@6116
   107
(* margin *)
wenzelm@6116
   108
wenzelm@6321
   109
fun make_margin_info m =
wenzelm@6321
   110
 {margin = m,                   (*right margin, or page width*)
wenzelm@6321
   111
  breakgain = m div 20,         (*minimum added space required of a break*)
wenzelm@6321
   112
  emergencypos = m div 2};      (*position too far to right*)
wenzelm@6116
   113
wenzelm@6321
   114
val margin_info = ref (make_margin_info 76);
wenzelm@6321
   115
fun setmargin m = margin_info := make_margin_info m;
wenzelm@6321
   116
fun setmp_margin m f = setmp margin_info (make_margin_info m) f;
wenzelm@6116
   117
wenzelm@10952
   118
wenzelm@10952
   119
(* format *)
wenzelm@10952
   120
wenzelm@10952
   121
(*Add the lengths of the expressions until the next Break; if no Break then
wenzelm@10952
   122
  include "after", to account for text following this block.*)
wenzelm@10952
   123
fun breakdist (Block (_, _, len) :: es, after) = len + breakdist (es, after)
wenzelm@10952
   124
  | breakdist (String (s, len) :: es, after) = len + breakdist (es, after)
wenzelm@10952
   125
  | breakdist (Break _ :: es, after) = 0
wenzelm@10952
   126
  | breakdist ([], after) = after;
wenzelm@10952
   127
wenzelm@10952
   128
(*Search for the next break (at this or higher levels) and force it to occur.*)
wenzelm@6116
   129
fun forcenext [] = []
wenzelm@10952
   130
  | forcenext (Break (_, wd) :: es) = Break (true, 0) :: es
wenzelm@6116
   131
  | forcenext (e :: es) = e :: forcenext es;
wenzelm@6116
   132
wenzelm@6116
   133
(*es is list of expressions to print;
wenzelm@6116
   134
  blockin is the indentation of the current block;
wenzelm@10952
   135
  after is the width of the following context until next break.*)
wenzelm@6116
   136
fun format ([], _, _) text = text
wenzelm@10952
   137
  | format (e :: es, block as (blockind, blockin), after) (text as {ind, pos, nl, ...}) =
wenzelm@10952
   138
      (case e of
wenzelm@10952
   139
        Block (bes, indent, wd) =>
wenzelm@10952
   140
          let
wenzelm@10952
   141
            val {emergencypos, ...} = ! margin_info;
wenzelm@10952
   142
            val pos' = pos + indent;
wenzelm@10952
   143
            val pos'' = pos' mod emergencypos;
wenzelm@10952
   144
            val block' =
wenzelm@10952
   145
              if pos' < emergencypos then (ind |> add_indent indent, pos')
wenzelm@10952
   146
              else (set_indent pos'', pos'');
wenzelm@10952
   147
            val btext = format (bes, block', breakdist (es, after)) text;
wenzelm@10952
   148
            (*if this block was broken then force the next break*)
wenzelm@10952
   149
            val es2 = if nl < #nl btext then forcenext es else es;
wenzelm@10952
   150
          in format (es2, block, after) btext end
wenzelm@10952
   151
      | String str => format (es, block, after) (string str text)
wenzelm@10952
   152
      | Break (force, wd) =>
wenzelm@10952
   153
          let val {margin, breakgain, ...} = ! margin_info in
wenzelm@10952
   154
            (*no break if text to next break fits on this line
wenzelm@10952
   155
              or if breaking would add only breakgain to space*)
wenzelm@10952
   156
            format (es, block, after)
wenzelm@10952
   157
              (if not force andalso
wenzelm@10952
   158
                  pos + wd <= Int.max (margin - breakdist (es, after), blockin + breakgain)
wenzelm@10952
   159
                then text |> blanks wd  (*just insert wd blanks*)
wenzelm@10952
   160
                else text |> newline |> indentation block)
wenzelm@10952
   161
          end);
wenzelm@6116
   162
wenzelm@6116
   163
wenzelm@6116
   164
(*** Exported functions to create formatting expressions ***)
wenzelm@6116
   165
wenzelm@6116
   166
fun length (Block (_, _, len)) = len
wenzelm@6116
   167
  | length (String (_, len)) = len
wenzelm@6116
   168
  | length (Break(_, wd)) = wd;
wenzelm@6116
   169
wenzelm@8456
   170
fun raw_str (s, len) = String (s, Real.round len);
wenzelm@8456
   171
val str = String o apsnd Real.round o Symbol.output_width;
wenzelm@6116
   172
wenzelm@6116
   173
fun brk wd = Break (false, wd);
wenzelm@9121
   174
val fbrk = Break (true, 2);
wenzelm@6116
   175
wenzelm@6116
   176
fun blk (indent, es) =
wenzelm@6116
   177
  let
wenzelm@6116
   178
    fun sum([], k) = k
wenzelm@6116
   179
      | sum(e :: es, k) = sum (es, length e + k);
wenzelm@6116
   180
  in Block (es, indent, sum (es, 0)) end;
wenzelm@6116
   181
wenzelm@6116
   182
wenzelm@6116
   183
(* utils *)
wenzelm@6116
   184
wenzelm@6116
   185
fun quote prt =
wenzelm@6116
   186
  blk (1, [str "\"", prt, str "\""]);
wenzelm@6116
   187
wenzelm@6116
   188
fun commas prts =
wenzelm@6116
   189
  flat (separate [str ",", brk 1] (map (fn x => [x]) prts));
wenzelm@6116
   190
wenzelm@6116
   191
fun breaks prts = separate (brk 1) prts;
wenzelm@6116
   192
fun fbreaks prts = separate fbrk prts;
wenzelm@6116
   193
wenzelm@6116
   194
fun block prts = blk (2, prts);
wenzelm@6116
   195
wenzelm@9730
   196
val strs = block o breaks o map str;
wenzelm@6116
   197
wenzelm@6116
   198
fun enclose lpar rpar prts =
wenzelm@6116
   199
  block (str lpar :: (prts @ [str rpar]));
wenzelm@6116
   200
wenzelm@8456
   201
fun list lpar rpar prts = enclose lpar rpar (commas prts);
wenzelm@8456
   202
fun str_list lpar rpar strs = list lpar rpar (map str strs);
wenzelm@8456
   203
fun big_list name prts = block (fbreaks (str name :: prts));
wenzelm@8456
   204
fun chunks prts = blk (0, fbreaks prts);
wenzelm@6116
   205
wenzelm@9730
   206
fun indent 0 prt = prt
wenzelm@10952
   207
  | indent n prt = blk (0, [str (Symbol.spaces n), prt]);
wenzelm@9730
   208
wenzelm@6116
   209
wenzelm@6116
   210
(*** Pretty printing with depth limitation ***)
wenzelm@6116
   211
wenzelm@6116
   212
val depth       = ref 0;        (*maximum depth; 0 means no limit*)
wenzelm@6116
   213
wenzelm@6116
   214
fun setdepth dp = (depth := dp);
wenzelm@6116
   215
wenzelm@6116
   216
(*Recursively prune blocks, discarding all text exceeding depth dp*)
wenzelm@6116
   217
fun pruning dp (Block(bes,indent,wd)) =
wenzelm@6116
   218
      if dp>0 then blk(indent, map (pruning(dp-1)) bes)
wenzelm@6116
   219
              else str "..."
wenzelm@6116
   220
  | pruning dp e = e;
wenzelm@6116
   221
wenzelm@10952
   222
fun prune dp e = if dp > 0 then pruning dp e else e;
wenzelm@6116
   223
wenzelm@10952
   224
fun string_of e = Buffer.content (#tx (format ([prune (! depth) e], (Buffer.empty, 0), 0) empty));
wenzelm@6116
   225
val writeln = writeln o string_of;
wenzelm@6116
   226
wenzelm@6116
   227
wenzelm@6116
   228
(*Create a single flat string: no line breaking*)
wenzelm@6116
   229
fun str_of prt =
wenzelm@6116
   230
  let
wenzelm@6116
   231
    fun s_of (Block (prts, _, _)) = implode (map s_of prts)
wenzelm@6116
   232
      | s_of (String (s, _)) = s
wenzelm@10952
   233
      | s_of (Break (false, wd)) = output_spaces wd
wenzelm@10952
   234
      | s_of (Break (true, _)) = output_spaces 1;
wenzelm@6321
   235
  in s_of (prune (! depth) prt) end;
wenzelm@6116
   236
wenzelm@8456
   237
(*part of the interface to the ML toplevel pretty printers*)
wenzelm@6116
   238
fun pprint prt (put_str, begin_blk, put_brk, put_fbrk, end_blk) =
wenzelm@6116
   239
  let
wenzelm@6116
   240
    fun pp (Block (prts, ind, _)) = (begin_blk ind; pp_lst prts; end_blk ())
wenzelm@6116
   241
      | pp (String (s, _)) = put_str s
wenzelm@6116
   242
      | pp (Break (false, wd)) = put_brk wd
wenzelm@6116
   243
      | pp (Break (true, _)) = put_fbrk ()
wenzelm@6116
   244
    and pp_lst [] = ()
wenzelm@6116
   245
      | pp_lst (prt :: prts) = (pp prt; pp_lst prts);
wenzelm@6116
   246
  in
wenzelm@6116
   247
    pp (prune (! depth) prt)
wenzelm@6116
   248
  end;
wenzelm@6116
   249
wenzelm@6116
   250
wenzelm@6116
   251
end;