src/Pure/PIDE/protocol.ML
author wenzelm
Wed May 22 14:10:45 2013 +0200 (2013-05-22 ago)
changeset 52111 1fd184eaa310
parent 51294 0850d43cb355
child 52527 dbac84eab3bc
permissions -rw-r--r--
explicit management of Session.Protocol_Handlers, with protocol state and functions;
more self-contained ML/Scala module Invoke_Scala;
     1 (*  Title:      Pure/PIDE/protocol.ML
     2     Author:     Makarius
     3 
     4 Protocol message formats for interactive proof documents.
     5 *)
     6 
     7 structure Protocol: sig end =
     8 struct
     9 
    10 val _ =
    11   Isabelle_Process.protocol_command "Document.define_command"
    12     (fn [id, name, text] =>
    13       Document.change_state (Document.define_command (Document.parse_id id) name text));
    14 
    15 val _ =
    16   Isabelle_Process.protocol_command "Document.discontinue_execution"
    17     (fn [] => Document.discontinue_execution (Document.state ()));
    18 
    19 val _ =
    20   Isabelle_Process.protocol_command "Document.cancel_execution"
    21     (fn [] => Document.cancel_execution (Document.state ()));
    22 
    23 val _ =
    24   Isabelle_Process.protocol_command "Document.update"
    25     (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
    26       let
    27         val _ = Document.cancel_execution state;
    28 
    29         val old_id = Document.parse_id old_id_string;
    30         val new_id = Document.parse_id new_id_string;
    31         val edits =
    32           YXML.parse_body edits_yxml |>
    33             let open XML.Decode in
    34               list (pair string
    35                 (variant
    36                  [fn ([], []) => Document.Clear,  (* FIXME unused !? *)
    37                   fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
    38                   fn ([], a) =>
    39                     let
    40                       val (master, (name, (imports, (keywords, errors)))) =
    41                         pair string (pair string (pair (list string)
    42                           (pair (list (pair string
    43                             (option (pair (pair string (list string)) (list string)))))
    44                             (list string)))) a;
    45                       val imports' = map (rpair Position.none) imports;
    46                       val header = Thy_Header.make (name, Position.none) imports' keywords;
    47                     in Document.Deps (master, header, errors) end,
    48                   fn (a, []) => Document.Perspective (map int_atom a)]))
    49             end;
    50 
    51         val (assignment, state') = Document.update old_id new_id edits state;
    52         val _ =
    53           Output.protocol_message Markup.assign_execs
    54             ((new_id, assignment) |>
    55               let open XML.Encode
    56               in pair int (list (pair int (option int))) end
    57               |> YXML.string_of_body);
    58 
    59         val _ = List.app Future.cancel_group (Goal.reset_futures ());
    60         val _ = Isabelle_Process.reset_tracing ();
    61         val _ = Document.start_execution state';
    62       in state' end));
    63 
    64 val _ =
    65   Isabelle_Process.protocol_command "Document.remove_versions"
    66     (fn [versions_yxml] => Document.change_state (fn state =>
    67       let
    68         val versions =
    69           YXML.parse_body versions_yxml |>
    70             let open XML.Decode in list int end;
    71         val state1 = Document.remove_versions versions state;
    72         val _ = Output.protocol_message Markup.removed_versions versions_yxml;
    73       in state1 end));
    74 
    75 val _ =
    76   Isabelle_Process.protocol_command "Document.dialog_result"
    77     (fn [serial, result] =>
    78       Active.dialog_result (Markup.parse_int serial) result
    79         handle exn => if Exn.is_interrupt exn then () else reraise exn);
    80 
    81 end;
    82