src/Pure/PIDE/markup.ML
changeset 45670 b84170538043
parent 45666 d83797ef0d2d
child 45674 eb65c9d17e2f
equal deleted inserted replaced
45669:06e259492f6b 45670:b84170538043
       
     1 (*  Title:      Pure/PIDE/markup.ML
       
     2     Author:     Makarius
       
     3 
       
     4 Generic markup elements.
       
     5 *)
       
     6 
       
     7 signature MARKUP =
       
     8 sig
       
     9   val parse_int: string -> int
       
    10   val print_int: int -> string
       
    11   type T = string * Properties.T
       
    12   val empty: T
       
    13   val is_empty: T -> bool
       
    14   val properties: Properties.T -> T -> T
       
    15   val nameN: string
       
    16   val name: string -> T -> T
       
    17   val kindN: string
       
    18   val elapsedN: string
       
    19   val cpuN: string
       
    20   val gcN: string
       
    21   val timingN: string val timing: Timing.timing -> T
       
    22   val no_output: Output.output * Output.output
       
    23   val default_output: T -> Output.output * Output.output
       
    24   val add_mode: string -> (T -> Output.output * Output.output) -> unit
       
    25   val output: T -> Output.output * Output.output
       
    26   val enclose: T -> Output.output -> Output.output
       
    27   val markup: T -> string -> string
       
    28   val markup_only: T -> string
       
    29 end;
       
    30 
       
    31 structure Markup: MARKUP =
       
    32 struct
       
    33 
       
    34 (** markup elements **)
       
    35 
       
    36 (* integers *)
       
    37 
       
    38 fun parse_int s =
       
    39   let val i = Int.fromString s in
       
    40     if is_none i orelse String.isPrefix "~" s
       
    41     then raise Fail ("Bad integer: " ^ quote s)
       
    42     else the i
       
    43   end;
       
    44 
       
    45 val print_int = signed_string_of_int;
       
    46 
       
    47 
       
    48 (* basic markup *)
       
    49 
       
    50 type T = string * Properties.T;
       
    51 
       
    52 val empty = ("", []);
       
    53 
       
    54 fun is_empty ("", _) = true
       
    55   | is_empty _ = false;
       
    56 
       
    57 
       
    58 fun properties more_props ((elem, props): T) =
       
    59   (elem, fold_rev Properties.put more_props props);
       
    60 
       
    61 
       
    62 (* misc properties *)
       
    63 
       
    64 val nameN = "name";
       
    65 fun name a = properties [(nameN, a)];
       
    66 
       
    67 val kindN = "kind";
       
    68 
       
    69 
       
    70 (* timing *)
       
    71 
       
    72 val timingN = "timing";
       
    73 val elapsedN = "elapsed";
       
    74 val cpuN = "cpu";
       
    75 val gcN = "gc";
       
    76 
       
    77 fun timing {elapsed, cpu, gc} =
       
    78   (timingN,
       
    79    [(elapsedN, Time.toString elapsed),
       
    80     (cpuN, Time.toString cpu),
       
    81     (gcN, Time.toString gc)]);
       
    82 
       
    83 
       
    84 
       
    85 (** print mode operations **)
       
    86 
       
    87 val no_output = ("", "");
       
    88 fun default_output (_: T) = no_output;
       
    89 
       
    90 local
       
    91   val default = {output = default_output};
       
    92   val modes = Synchronized.var "Markup.modes" (Symtab.make [("", default)]);
       
    93 in
       
    94   fun add_mode name output =
       
    95     Synchronized.change modes (Symtab.update_new (name, {output = output}));
       
    96   fun get_mode () =
       
    97     the_default default
       
    98       (Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ()));
       
    99 end;
       
   100 
       
   101 fun output m = if is_empty m then no_output else #output (get_mode ()) m;
       
   102 
       
   103 val enclose = output #-> Library.enclose;
       
   104 
       
   105 fun markup m =
       
   106   let val (bg, en) = output m
       
   107   in Library.enclose (Output.escape bg) (Output.escape en) end;
       
   108 
       
   109 fun markup_only m = markup m "";
       
   110 
       
   111 end;