src/Pure/PIDE/protocol.ML
author wenzelm
Fri Jul 12 11:28:03 2013 +0200 (2013-07-12)
changeset 52606 0d68d108d7e0
parent 52605 a2a805549c74
child 52607 33a133d50616
permissions -rw-r--r--
tuned signature;
tuned comments;
wenzelm@45709
     1
(*  Title:      Pure/PIDE/protocol.ML
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@45709
     4
Protocol message formats for interactive proof documents.
wenzelm@38412
     5
*)
wenzelm@38412
     6
wenzelm@45709
     7
structure Protocol: sig end =
wenzelm@38412
     8
struct
wenzelm@38412
     9
wenzelm@38418
    10
val _ =
wenzelm@52585
    11
  Isabelle_Process.protocol_command "echo" (fn args => List.app writeln args);
wenzelm@52585
    12
wenzelm@52585
    13
val _ =
wenzelm@46119
    14
  Isabelle_Process.protocol_command "Document.define_command"
wenzelm@44644
    15
    (fn [id, name, text] =>
wenzelm@52530
    16
      Document.change_state (Document.define_command (Document_ID.parse id) name text));
wenzelm@38412
    17
wenzelm@38418
    18
val _ =
wenzelm@47343
    19
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@52606
    20
    (fn [] => Execution.discontinue ());
wenzelm@47343
    21
wenzelm@47343
    22
val _ =
wenzelm@46119
    23
  Isabelle_Process.protocol_command "Document.cancel_execution"
wenzelm@52606
    24
    (fn [] => (Execution.discontinue (); Execution.terminate_all ()));
wenzelm@44612
    25
wenzelm@44612
    26
val _ =
wenzelm@46119
    27
  Isabelle_Process.protocol_command "Document.update"
wenzelm@44157
    28
    (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@38418
    29
      let
wenzelm@52606
    30
        val _ = Execution.discontinue ();
wenzelm@47404
    31
wenzelm@52530
    32
        val old_id = Document_ID.parse old_id_string;
wenzelm@52530
    33
        val new_id = Document_ID.parse new_id_string;
wenzelm@44157
    34
        val edits =
wenzelm@44157
    35
          YXML.parse_body edits_yxml |>
wenzelm@44157
    36
            let open XML.Decode in
wenzelm@44157
    37
              list (pair string
wenzelm@44157
    38
                (variant
wenzelm@48755
    39
                 [fn ([], []) => Document.Clear,  (* FIXME unused !? *)
wenzelm@44157
    40
                  fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@44157
    41
                  fn ([], a) =>
wenzelm@46938
    42
                    let
wenzelm@51294
    43
                      val (master, (name, (imports, (keywords, errors)))) =
wenzelm@48707
    44
                        pair string (pair string (pair (list string)
wenzelm@48864
    45
                          (pair (list (pair string
wenzelm@48864
    46
                            (option (pair (pair string (list string)) (list string)))))
wenzelm@51294
    47
                            (list string)))) a;
wenzelm@48927
    48
                      val imports' = map (rpair Position.none) imports;
wenzelm@51294
    49
                      val header = Thy_Header.make (name, Position.none) imports' keywords;
wenzelm@51294
    50
                    in Document.Deps (master, header, errors) end,
wenzelm@44384
    51
                  fn (a, []) => Document.Perspective (map int_atom a)]))
wenzelm@44157
    52
            end;
wenzelm@38412
    53
wenzelm@52601
    54
        val (assign_update, state') = Document.update old_id new_id edits state;
wenzelm@47420
    55
wenzelm@49931
    56
        val _ = List.app Future.cancel_group (Goal.reset_futures ());
wenzelm@52605
    57
        val _ = Execution.purge_canceled ();
wenzelm@50505
    58
        val _ = Isabelle_Process.reset_tracing ();
wenzelm@52601
    59
wenzelm@52601
    60
        val _ =
wenzelm@52601
    61
          Output.protocol_message Markup.assign_update
wenzelm@52601
    62
            ((new_id, assign_update) |>
wenzelm@52601
    63
              let open XML.Encode
wenzelm@52601
    64
              in pair int (list (pair int (list int))) end
wenzelm@52601
    65
              |> YXML.string_of_body);
wenzelm@52573
    66
        val _ =
wenzelm@52579
    67
          Event_Timer.request (Time.+ (Time.now (), seconds 0.02))
wenzelm@52573
    68
            (fn () => Document.start_execution state');
wenzelm@47420
    69
      in state' end));
wenzelm@38412
    70
wenzelm@43748
    71
val _ =
wenzelm@46119
    72
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
    73
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
    74
      let
wenzelm@44673
    75
        val versions =
wenzelm@44673
    76
          YXML.parse_body versions_yxml |>
wenzelm@44673
    77
            let open XML.Decode in list int end;
wenzelm@44676
    78
        val state1 = Document.remove_versions versions state;
wenzelm@50201
    79
        val _ = Output.protocol_message Markup.removed_versions versions_yxml;
wenzelm@44676
    80
      in state1 end));
wenzelm@44673
    81
wenzelm@44673
    82
val _ =
wenzelm@50498
    83
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
    84
    (fn [serial, result] =>
wenzelm@50500
    85
      Active.dialog_result (Markup.parse_int serial) result
wenzelm@50498
    86
        handle exn => if Exn.is_interrupt exn then () else reraise exn);
wenzelm@50498
    87
wenzelm@38412
    88
end;
wenzelm@38412
    89