src/Pure/General/pretty.ML
changeset 48704 85a3de10567d
parent 46894 e2ad717ec889
child 49554 7b7bd2d7661d
equal deleted inserted replaced
48703:d408ff0abf23 48704:85a3de10567d
    19 a unit for breaking).
    19 a unit for breaking).
    20 *)
    20 *)
    21 
    21 
    22 signature PRETTY =
    22 signature PRETTY =
    23 sig
    23 sig
       
    24   val spaces: int -> string
    24   val default_indent: string -> int -> Output.output
    25   val default_indent: string -> int -> Output.output
    25   val add_mode: string -> (string -> int -> Output.output) -> unit
    26   val add_mode: string -> (string -> int -> Output.output) -> unit
    26   type T
    27   type T
    27   val str: string -> T
    28   val str: string -> T
    28   val brk: int -> T
    29   val brk: int -> T
    67 end;
    68 end;
    68 
    69 
    69 structure Pretty: PRETTY =
    70 structure Pretty: PRETTY =
    70 struct
    71 struct
    71 
    72 
       
    73 (** spaces **)
       
    74 
       
    75 local
       
    76   val small_spaces = Vector.tabulate (65, fn i => Library.replicate_string i Symbol.space);
       
    77 in
       
    78   fun spaces k =
       
    79     if k < 64 then Vector.sub (small_spaces, k)
       
    80     else Library.replicate_string (k div 64) (Vector.sub (small_spaces, 64)) ^
       
    81       Vector.sub (small_spaces, k mod 64);
       
    82 end;
       
    83 
       
    84 
       
    85 
    72 (** print mode operations **)
    86 (** print mode operations **)
    73 
    87 
    74 fun default_indent (_: string) = Symbol.spaces;
    88 fun default_indent (_: string) = spaces;
    75 
    89 
    76 local
    90 local
    77   val default = {indent = default_indent};
    91   val default = {indent = default_indent};
    78   val modes = Synchronized.var "Pretty.modes" (Symtab.make [("", default)]);
    92   val modes = Synchronized.var "Pretty.modes" (Symtab.make [("", default)]);
    79 in
    93 in
    87       (Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ()));
   101       (Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ()));
    88 end;
   102 end;
    89 
   103 
    90 fun mode_indent x y = #indent (get_mode ()) x y;
   104 fun mode_indent x y = #indent (get_mode ()) x y;
    91 
   105 
    92 val output_spaces = Output.output o Symbol.spaces;
   106 val output_spaces = Output.output o spaces;
    93 val add_indent = Buffer.add o output_spaces;
   107 val add_indent = Buffer.add o output_spaces;
    94 
   108 
    95 
   109 
    96 
   110 
    97 (** printing items: compound phrases, strings, and breaks **)
   111 (** printing items: compound phrases, strings, and breaks **)
   165 fun str_list lpar rpar strs = list lpar rpar (map str strs);
   179 fun str_list lpar rpar strs = list lpar rpar (map str strs);
   166 
   180 
   167 fun big_list name prts = block (fbreaks (str name :: prts));
   181 fun big_list name prts = block (fbreaks (str name :: prts));
   168 
   182 
   169 fun indent 0 prt = prt
   183 fun indent 0 prt = prt
   170   | indent n prt = blk (0, [str (Symbol.spaces n), prt]);
   184   | indent n prt = blk (0, [str (spaces n), prt]);
   171 
   185 
   172 fun unbreakable (Break (_, wd)) = String (output_spaces wd, wd)
   186 fun unbreakable (Break (_, wd)) = String (output_spaces wd, wd)
   173   | unbreakable (Block (m, es, indent, wd)) = Block (m, map unbreakable es, indent, wd)
   187   | unbreakable (Block (m, es, indent, wd)) = Block (m, map unbreakable es, indent, wd)
   174   | unbreakable (e as String _) = e;
   188   | unbreakable (e as String _) = e;
   175 
   189