src/Pure/PIDE/isar_document.ML
author wenzelm
Wed Aug 31 20:47:33 2011 +0200 (2011-08-31)
changeset 44612 990ac978854c
parent 44610 49657380fba6
child 44644 317e4962dd0f
permissions -rw-r--r--
explicit cancel_execution before queueing new edits -- potential performance improvement for machines with few cores;
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@44612
    15
  Isabelle_Process.add_command "Isar_Document.cancel_execution"
wenzelm@44612
    16
    (fn [] => ignore (Document.cancel_execution (Document.state ())));
wenzelm@44612
    17
wenzelm@44612
    18
val _ =
wenzelm@44436
    19
  Isabelle_Process.add_command "Isar_Document.update_perspective"
wenzelm@44436
    20
    (fn [old_id_string, new_id_string, name, ids_yxml] => Document.change_state (fn state =>
wenzelm@44436
    21
      let
wenzelm@44436
    22
        val old_id = Document.parse_id old_id_string;
wenzelm@44436
    23
        val new_id = Document.parse_id new_id_string;
wenzelm@44436
    24
        val ids = YXML.parse_body ids_yxml
wenzelm@44436
    25
          |> let open XML.Decode in list int end;
wenzelm@44436
    26
wenzelm@44436
    27
        val _ = Future.join_tasks (Document.cancel_execution state);
wenzelm@44436
    28
      in
wenzelm@44436
    29
        state
wenzelm@44436
    30
        |> Document.update_perspective old_id new_id name ids
wenzelm@44436
    31
        |> Document.execute new_id
wenzelm@44436
    32
      end));
wenzelm@44436
    33
wenzelm@44436
    34
val _ =
wenzelm@44481
    35
  Isabelle_Process.add_command "Isar_Document.update"
wenzelm@44157
    36
    (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@38418
    37
      let
wenzelm@38418
    38
        val old_id = Document.parse_id old_id_string;
wenzelm@38418
    39
        val new_id = Document.parse_id new_id_string;
wenzelm@44157
    40
        val edits =
wenzelm@44157
    41
          YXML.parse_body edits_yxml |>
wenzelm@44157
    42
            let open XML.Decode in
wenzelm@44157
    43
              list (pair string
wenzelm@44157
    44
                (variant
wenzelm@44185
    45
                 [fn ([], []) => Document.Clear,
wenzelm@44157
    46
                  fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@44157
    47
                  fn ([], a) =>
wenzelm@44185
    48
                    Document.Header
wenzelm@44185
    49
                      (Exn.Res (triple string (list string) (list (pair string bool)) a)),
wenzelm@44384
    50
                  fn ([a], []) => Document.Header (Exn.Exn (ERROR a)),
wenzelm@44384
    51
                  fn (a, []) => Document.Perspective (map int_atom a)]))
wenzelm@44157
    52
            end;
wenzelm@38412
    53
wenzelm@44436
    54
        val running = Document.cancel_execution state;
wenzelm@44610
    55
        val (assignment, state1) = Document.update old_id new_id edits state;
wenzelm@44436
    56
        val _ = Future.join_tasks running;
wenzelm@44610
    57
        val state2 = Document.join_commands state1;
wenzelm@44436
    58
        val _ =
wenzelm@44436
    59
          Output.status (Markup.markup (Markup.assign new_id)
wenzelm@44476
    60
            (assignment |>
wenzelm@44476
    61
              let open XML.Encode
wenzelm@44479
    62
              in pair (list (pair int (option int))) (list (pair string (option int))) end
wenzelm@44476
    63
              |> YXML.string_of_body));
wenzelm@44610
    64
        val state3 = Document.execute new_id state2;
wenzelm@44610
    65
      in state3 end));
wenzelm@38412
    66
wenzelm@43748
    67
val _ =
wenzelm@43748
    68
  Isabelle_Process.add_command "Isar_Document.invoke_scala"
wenzelm@43748
    69
    (fn [id, tag, res] => Invoke_Scala.fulfill_method id tag res);
wenzelm@43748
    70
wenzelm@38412
    71
end;
wenzelm@38412
    72