src/Pure/PIDE/isar_document.ML
author wenzelm
Fri Aug 26 15:09:54 2011 +0200 (2011-08-26 ago)
changeset 44479 9a04e7502e22
parent 44476 e8a87398f35d
child 44481 bb42bc831570
permissions -rw-r--r--
refined document state assignment: observe perspective, more explicit assignment message;
misc tuning and clarification;
wenzelm@38483
     1
(*  Title:      Pure/PIDE/isar_document.ML
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@38567
     4
Protocol message formats for interactive Isar documents.
wenzelm@38412
     5
*)
wenzelm@38412
     6
wenzelm@43713
     7
structure Isar_Document: sig end =
wenzelm@38412
     8
struct
wenzelm@38412
     9
wenzelm@38418
    10
val _ =
wenzelm@38418
    11
  Isabelle_Process.add_command "Isar_Document.define_command"
wenzelm@43713
    12
    (fn [id, text] => Document.change_state (Document.define_command (Document.parse_id id) text));
wenzelm@38412
    13
wenzelm@38418
    14
val _ =
wenzelm@44436
    15
  Isabelle_Process.add_command "Isar_Document.update_perspective"
wenzelm@44436
    16
    (fn [old_id_string, new_id_string, name, ids_yxml] => Document.change_state (fn state =>
wenzelm@44436
    17
      let
wenzelm@44436
    18
        val old_id = Document.parse_id old_id_string;
wenzelm@44436
    19
        val new_id = Document.parse_id new_id_string;
wenzelm@44436
    20
        val ids = YXML.parse_body ids_yxml
wenzelm@44436
    21
          |> let open XML.Decode in list int end;
wenzelm@44436
    22
wenzelm@44436
    23
        val _ = Future.join_tasks (Document.cancel_execution state);
wenzelm@44436
    24
      in
wenzelm@44436
    25
        state
wenzelm@44436
    26
        |> Document.update_perspective old_id new_id name ids
wenzelm@44436
    27
        |> Document.execute new_id
wenzelm@44436
    28
      end));
wenzelm@44436
    29
wenzelm@44436
    30
val _ =
wenzelm@38418
    31
  Isabelle_Process.add_command "Isar_Document.edit_version"
wenzelm@44157
    32
    (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@38418
    33
      let
wenzelm@38418
    34
        val old_id = Document.parse_id old_id_string;
wenzelm@38418
    35
        val new_id = Document.parse_id new_id_string;
wenzelm@44157
    36
        val edits =
wenzelm@44157
    37
          YXML.parse_body edits_yxml |>
wenzelm@44157
    38
            let open XML.Decode in
wenzelm@44157
    39
              list (pair string
wenzelm@44157
    40
                (variant
wenzelm@44185
    41
                 [fn ([], []) => Document.Clear,
wenzelm@44157
    42
                  fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@44157
    43
                  fn ([], a) =>
wenzelm@44185
    44
                    Document.Header
wenzelm@44185
    45
                      (Exn.Res (triple string (list string) (list (pair string bool)) a)),
wenzelm@44384
    46
                  fn ([a], []) => Document.Header (Exn.Exn (ERROR a)),
wenzelm@44384
    47
                  fn (a, []) => Document.Perspective (map int_atom a)]))
wenzelm@44157
    48
            end;
wenzelm@38412
    49
wenzelm@44436
    50
        val running = Document.cancel_execution state;
wenzelm@44476
    51
        val (assignment, state') = Document.edit old_id new_id edits state;
wenzelm@44436
    52
        val _ = Future.join_tasks running;
wenzelm@44436
    53
        val _ = Document.join_commands state';
wenzelm@44436
    54
        val _ =
wenzelm@44436
    55
          Output.status (Markup.markup (Markup.assign new_id)
wenzelm@44476
    56
            (assignment |>
wenzelm@44476
    57
              let open XML.Encode
wenzelm@44479
    58
              in pair (list (pair int (option int))) (list (pair string (option int))) end
wenzelm@44476
    59
              |> YXML.string_of_body));
wenzelm@44436
    60
        val state'' = Document.execute new_id state';
wenzelm@44436
    61
      in state'' end));
wenzelm@38412
    62
wenzelm@43748
    63
val _ =
wenzelm@43748
    64
  Isabelle_Process.add_command "Isar_Document.invoke_scala"
wenzelm@43748
    65
    (fn [id, tag, res] => Invoke_Scala.fulfill_method id tag res);
wenzelm@43748
    66
wenzelm@38412
    67
end;
wenzelm@38412
    68