src/Pure/Tools/print_operation.ML
author wenzelm
Tue Nov 07 16:50:26 2017 +0100 (20 months ago)
changeset 67026 687c822ee5e3
parent 64677 8dc24130e8fe
permissions -rw-r--r--
tuned signature;
wenzelm@56864
     1
(*  Title:      Pure/Tools/print_operation.ML
wenzelm@56864
     2
    Author:     Makarius
wenzelm@56864
     3
wenzelm@56864
     4
Print operations as asynchronous query.
wenzelm@56864
     5
*)
wenzelm@56864
     6
wenzelm@56864
     7
wenzelm@56864
     8
signature PRINT_OPERATION =
wenzelm@56864
     9
sig
wenzelm@57604
    10
  val register: string -> string -> (Toplevel.state -> Pretty.T list) -> unit
wenzelm@56864
    11
end;
wenzelm@56864
    12
wenzelm@56864
    13
structure Print_Operation: PRINT_OPERATION =
wenzelm@56864
    14
struct
wenzelm@56864
    15
wenzelm@56864
    16
(* maintain print operations *)
wenzelm@56864
    17
wenzelm@56864
    18
local
wenzelm@56864
    19
wenzelm@56864
    20
val print_operations =
wenzelm@56864
    21
  Synchronized.var "print_operations"
wenzelm@57604
    22
    ([]: (string * (string * (Toplevel.state -> Pretty.T list))) list);
wenzelm@56864
    23
wenzelm@56864
    24
fun report () =
wenzelm@56864
    25
  Output.try_protocol_message Markup.print_operations
wenzelm@56864
    26
    let
wenzelm@56864
    27
      val yxml =
wenzelm@56867
    28
        Synchronized.value print_operations
wenzelm@56867
    29
        |> map (fn (x, (y, _)) => (x, y)) |> rev
wenzelm@56864
    30
        |> let open XML.Encode in list (pair string string) end
wenzelm@56864
    31
        |> YXML.string_of_body;
wenzelm@56864
    32
    in [yxml] end;
wenzelm@56864
    33
wenzelm@56864
    34
val _ = Isabelle_Process.protocol_command "print_operations" (fn [] => report ());
wenzelm@56864
    35
wenzelm@56864
    36
val _ = Session.protocol_handler "isabelle.Print_Operation$Handler";
wenzelm@56864
    37
wenzelm@56864
    38
in
wenzelm@56864
    39
wenzelm@56864
    40
fun register name description pr =
wenzelm@56864
    41
 (Synchronized.change print_operations (fn tab =>
wenzelm@56867
    42
   (if not (AList.defined (op =) tab name) then ()
wenzelm@56864
    43
    else warning ("Redefining print operation: " ^ quote name);
wenzelm@56867
    44
    AList.update (op =) (name, (description, pr)) tab));
wenzelm@56864
    45
  report ());
wenzelm@56864
    46
wenzelm@56864
    47
val _ =
wenzelm@60610
    48
  Query_Operation.register {name = "print_operation", pri = Task_Queue.urgent_pri}
wenzelm@61223
    49
    (fn {state, args, writeln_result, ...} =>
wenzelm@60610
    50
      let
wenzelm@60610
    51
        val _ = Toplevel.context_of state handle Toplevel.UNDEF => error "Unknown context";
wenzelm@64677
    52
        fun err s = Pretty.mark_str (Markup.bad (), s);
wenzelm@60610
    53
        fun print name =
wenzelm@60610
    54
          (case AList.lookup (op =) (Synchronized.value print_operations) name of
wenzelm@60610
    55
            SOME (_, pr) => (pr state handle Toplevel.UNDEF => [err "Unknown context"])
wenzelm@60610
    56
          | NONE => [err ("Unknown print operation: " ^ quote name)]);
wenzelm@61223
    57
      in writeln_result (Pretty.string_of (Pretty.chunks (maps print args))) end);
wenzelm@56864
    58
wenzelm@56864
    59
end;
wenzelm@56864
    60
wenzelm@56864
    61
wenzelm@56864
    62
(* common print operations *)
wenzelm@56864
    63
wenzelm@56864
    64
val _ =
wenzelm@57604
    65
  register "context" "context of local theory target" Toplevel.pretty_context;
wenzelm@56867
    66
wenzelm@56867
    67
val _ =
wenzelm@57605
    68
  register "cases" "cases of proof context"
wenzelm@57605
    69
    (Proof_Context.pretty_cases o Toplevel.context_of);
wenzelm@56867
    70
wenzelm@56867
    71
val _ =
wenzelm@56868
    72
  register "terms" "term bindings of proof context"
wenzelm@57604
    73
    (Proof_Context.pretty_term_bindings o Toplevel.context_of);
wenzelm@56867
    74
wenzelm@56867
    75
val _ =
wenzelm@56868
    76
  register "theorems" "theorems of local theory or proof context"
wenzelm@57605
    77
    (Isar_Cmd.pretty_theorems false);
wenzelm@56864
    78
wenzelm@56864
    79
end;