src/Pure/PIDE/document.ML
author wenzelm
Wed May 16 21:07:12 2018 +0200 (13 months ago ago)
changeset 68197 7857817403e4
parent 68185 6c693b2700b3
child 68324 bf7336731981
permissions -rw-r--r--
clarified "consolidation" vs. "presentation";
wenzelm@38150
     1
(*  Title:      Pure/PIDE/document.ML
wenzelm@38150
     2
    Author:     Makarius
wenzelm@38150
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@52536
     5
list of commands, associated with asynchronous execution process.
wenzelm@38150
     6
*)
wenzelm@38150
     7
wenzelm@38150
     8
signature DOCUMENT =
wenzelm@38150
     9
sig
wenzelm@52796
    10
  val timing: bool Unsynchronized.ref
wenzelm@59715
    11
  type node_header = {master: string, header: Thy_Header.header, errors: string list}
wenzelm@52862
    12
  type overlay = Document_ID.command * (string * string list)
wenzelm@44157
    13
  datatype node_edit =
wenzelm@52530
    14
    Edits of (Document_ID.command option * Document_ID.command option) list |
wenzelm@48707
    15
    Deps of node_header |
wenzelm@52849
    16
    Perspective of bool * Document_ID.command list * overlay list
wenzelm@44156
    17
  type edit = string * node_edit
wenzelm@38418
    18
  type state
wenzelm@38418
    19
  val init_state: state
wenzelm@54519
    20
  val define_blob: string -> string -> state -> state
wenzelm@56458
    21
  type blob_digest = (string * string option) Exn.result
wenzelm@59685
    22
  val define_command: Document_ID.command -> string -> blob_digest list -> int ->
wenzelm@59085
    23
    ((int * int) * string) list -> state -> state
wenzelm@60880
    24
  val command_exec: state -> string -> Document_ID.command -> Command.exec option
wenzelm@52530
    25
  val remove_versions: Document_ID.version list -> state -> state
wenzelm@52774
    26
  val start_execution: state -> state
wenzelm@66382
    27
  val consolidate_execution: state -> unit
wenzelm@52530
    28
  val update: Document_ID.version -> Document_ID.version -> edit list -> state ->
wenzelm@52655
    29
    Document_ID.exec list * (Document_ID.command * Document_ID.exec list) list * state
wenzelm@43713
    30
  val state: unit -> state
wenzelm@43713
    31
  val change_state: (state -> state) -> unit
wenzelm@38150
    32
end;
wenzelm@38150
    33
wenzelm@38150
    34
structure Document: DOCUMENT =
wenzelm@38150
    35
struct
wenzelm@38150
    36
wenzelm@52796
    37
val timing = Unsynchronized.ref false;
wenzelm@52796
    38
fun timeit msg e = cond_timeit (! timing) msg e;
wenzelm@52796
    39
wenzelm@52796
    40
wenzelm@52796
    41
wenzelm@38418
    42
(** document structure **)
wenzelm@38418
    43
wenzelm@52530
    44
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ Document_ID.print id);
wenzelm@52530
    45
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ Document_ID.print id);
wenzelm@52530
    46
wenzelm@59715
    47
type node_header =
wenzelm@59715
    48
  {master: string, header: Thy_Header.header, errors: string list};
wenzelm@52849
    49
wenzelm@52849
    50
type perspective =
wenzelm@52849
    51
 {required: bool,  (*required node*)
wenzelm@52849
    52
  visible: Inttab.set,  (*visible commands*)
wenzelm@52849
    53
  visible_last: Document_ID.command option,  (*last visible command*)
wenzelm@57874
    54
  overlays: (string * string list) list Inttab.table};  (*command id -> print functions with args*)
wenzelm@52849
    55
wenzelm@52530
    56
structure Entries = Linear_Set(type key = Document_ID.command val ord = int_ord);
wenzelm@38448
    57
wenzelm@68197
    58
type consolidation = (int -> int option) * Thy_Output.segment list;
wenzelm@68197
    59
wenzelm@43668
    60
abstype node = Node of
wenzelm@48707
    61
 {header: node_header,  (*master directory, theory header, errors*)
wenzelm@59086
    62
  keywords: Keyword.keywords option,  (*outer syntax keywords*)
wenzelm@52849
    63
  perspective: perspective,  (*command perspective*)
wenzelm@66372
    64
  entries: Command.exec option Entries.T,  (*command entries with executions*)
wenzelm@66382
    65
  result: Command.eval option,  (*result of last execution*)
wenzelm@68197
    66
  consolidation: consolidation future}  (*consolidation status of eval forks*)
wenzelm@50862
    67
and version = Version of node String_Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    68
with
wenzelm@38418
    69
wenzelm@68197
    70
fun make_node (header, keywords, perspective, entries, result, consolidation) =
wenzelm@59086
    71
  Node {header = header, keywords = keywords, perspective = perspective,
wenzelm@68197
    72
    entries = entries, result = result, consolidation = consolidation};
wenzelm@43668
    73
wenzelm@68197
    74
fun map_node f (Node {header, keywords, perspective, entries, result, consolidation}) =
wenzelm@68197
    75
  make_node (f (header, keywords, perspective, entries, result, consolidation));
wenzelm@38418
    76
wenzelm@52849
    77
fun make_perspective (required, command_ids, overlays) : perspective =
wenzelm@52849
    78
 {required = required,
wenzelm@52849
    79
  visible = Inttab.make_set command_ids,
wenzelm@52849
    80
  visible_last = try List.last command_ids,
wenzelm@52862
    81
  overlays = Inttab.make_list overlays};
wenzelm@44441
    82
wenzelm@59715
    83
val no_header: node_header =
wenzelm@59715
    84
  {master = "", header = Thy_Header.make ("", Position.none) [] [], errors = []};
wenzelm@52849
    85
val no_perspective = make_perspective (false, [], []);
wenzelm@44386
    86
wenzelm@68197
    87
val empty_node =
wenzelm@68197
    88
  make_node (no_header, NONE, no_perspective, Entries.empty, NONE, Future.value (K NONE, []));
wenzelm@44386
    89
wenzelm@57615
    90
fun is_no_perspective ({required, visible, visible_last, overlays}: perspective) =
wenzelm@57615
    91
  not required andalso
wenzelm@57615
    92
  Inttab.is_empty visible andalso
wenzelm@57615
    93
  is_none visible_last andalso
wenzelm@57615
    94
  Inttab.is_empty overlays;
wenzelm@57615
    95
wenzelm@68197
    96
fun is_empty_node (Node {header, keywords, perspective, entries, result, consolidation}) =
wenzelm@57615
    97
  header = no_header andalso
wenzelm@59086
    98
  is_none keywords andalso
wenzelm@57615
    99
  is_no_perspective perspective andalso
wenzelm@57615
   100
  Entries.is_empty entries andalso
wenzelm@66382
   101
  is_none result andalso
wenzelm@68197
   102
  Future.is_finished consolidation;
wenzelm@57615
   103
wenzelm@44386
   104
wenzelm@44386
   105
(* basic components *)
wenzelm@44386
   106
wenzelm@59715
   107
fun master_directory (Node {header = {master, ...}, ...}) =
wenzelm@54558
   108
  (case try Url.explode master of
wenzelm@54558
   109
    SOME (Url.File path) => path
wenzelm@54558
   110
  | _ => Path.current);
wenzelm@54558
   111
wenzelm@59715
   112
fun set_header master header errors =
wenzelm@68197
   113
  map_node (fn (_, keywords, perspective, entries, result, consolidation) =>
wenzelm@66382
   114
    ({master = master, header = header, errors = errors},
wenzelm@68197
   115
      keywords, perspective, entries, result, consolidation));
wenzelm@59086
   116
wenzelm@59715
   117
fun get_header (Node {header, ...}) = header;
wenzelm@48707
   118
wenzelm@59086
   119
fun set_keywords keywords =
wenzelm@68197
   120
  map_node (fn (header, _, perspective, entries, result, consolidation) =>
wenzelm@68197
   121
    (header, keywords, perspective, entries, result, consolidation));
wenzelm@59086
   122
wenzelm@59086
   123
fun get_keywords (Node {keywords, ...}) = keywords;
wenzelm@59086
   124
wenzelm@48927
   125
fun read_header node span =
wenzelm@48927
   126
  let
wenzelm@59715
   127
    val {header, errors, ...} = get_header node;
wenzelm@59715
   128
    val _ =
wenzelm@59715
   129
      if null errors then ()
wenzelm@59715
   130
      else
wenzelm@59715
   131
        cat_lines errors |>
wenzelm@59715
   132
        (case Position.get_id (Position.thread_data ()) of
wenzelm@59715
   133
          NONE => I
wenzelm@59715
   134
        | SOME id => Protocol_Message.command_positions_yxml id)
wenzelm@59715
   135
        |> error;
wenzelm@59934
   136
    val {name = (name, _), imports, ...} = header;
wenzelm@67500
   137
    val {name = (_, pos), imports = imports', keywords} =
wenzelm@67500
   138
      Thy_Header.read_tokens Position.none span;
wenzelm@63022
   139
    val imports'' = (map #1 imports ~~ map #2 imports') handle ListPair.UnequalLengths => imports;
wenzelm@63022
   140
  in Thy_Header.make (name, pos) imports'' keywords end;
wenzelm@54526
   141
wenzelm@44385
   142
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@59086
   143
wenzelm@52808
   144
fun set_perspective args =
wenzelm@68197
   145
  map_node (fn (header, keywords, _, entries, result, consolidation) =>
wenzelm@68197
   146
    (header, keywords, make_perspective args, entries, result, consolidation));
wenzelm@44385
   147
wenzelm@52849
   148
val required_node = #required o get_perspective;
wenzelm@52849
   149
val visible_command = Inttab.defined o #visible o get_perspective;
wenzelm@52849
   150
val visible_last = #visible_last o get_perspective;
wenzelm@47345
   151
val visible_node = is_some o visible_last
wenzelm@52850
   152
val overlays = Inttab.lookup_list o #overlays o get_perspective;
wenzelm@47345
   153
wenzelm@44444
   154
fun map_entries f =
wenzelm@68197
   155
  map_node (fn (header, keywords, perspective, entries, result, consolidation) =>
wenzelm@68197
   156
    (header, keywords, perspective, f entries, result, consolidation));
wenzelm@59086
   157
wenzelm@52761
   158
fun get_entries (Node {entries, ...}) = entries;
wenzelm@44645
   159
wenzelm@44645
   160
fun iterate_entries f = Entries.iterate NONE f o get_entries;
wenzelm@52761
   161
fun iterate_entries_after start f (Node {entries, ...}) =
wenzelm@44645
   162
  (case Entries.get_after entries start of
wenzelm@44645
   163
    NONE => I
wenzelm@44645
   164
  | SOME id => Entries.iterate (SOME id) f entries);
wenzelm@43668
   165
wenzelm@47339
   166
fun get_result (Node {result, ...}) = result;
wenzelm@59086
   167
wenzelm@44385
   168
fun set_result result =
wenzelm@68197
   169
  map_node (fn (header, keywords, perspective, entries, _, consolidation) =>
wenzelm@68197
   170
    (header, keywords, perspective, entries, result, consolidation));
wenzelm@52566
   171
wenzelm@52772
   172
fun pending_result node =
wenzelm@52772
   173
  (case get_result node of
wenzelm@52772
   174
    SOME eval => not (Command.eval_finished eval)
wenzelm@52772
   175
  | NONE => false);
wenzelm@52533
   176
wenzelm@59198
   177
fun finished_result node =
wenzelm@59198
   178
  (case get_result node of
wenzelm@59198
   179
    SOME eval => Command.eval_finished eval
wenzelm@59198
   180
  | NONE => false);
wenzelm@59198
   181
wenzelm@66382
   182
fun finished_result_theory node =
wenzelm@68185
   183
  if finished_result node then
wenzelm@66382
   184
    let val st = Command.eval_result_state (the (get_result node))
wenzelm@68185
   185
    in SOME (Toplevel.end_theory Position.none st) handle ERROR _ => NONE end
wenzelm@68185
   186
  else NONE;
wenzelm@66382
   187
wenzelm@68197
   188
val reset_consolidation =
wenzelm@66382
   189
  map_node (fn (header, keywords, perspective, entries, result, _) =>
wenzelm@68197
   190
    (header, keywords, perspective, entries, result, Future.promise I));
wenzelm@66382
   191
wenzelm@68197
   192
fun get_consolidation (Node {consolidation, ...}) = consolidation;
wenzelm@66382
   193
wenzelm@50862
   194
fun get_node nodes name = String_Graph.get_node nodes name
wenzelm@50862
   195
  handle String_Graph.UNDEF _ => empty_node;
wenzelm@50862
   196
fun default_node name = String_Graph.default_node (name, empty_node);
wenzelm@50862
   197
fun update_node name f = default_node name #> String_Graph.map_node name f;
wenzelm@38418
   198
wenzelm@38418
   199
wenzelm@38448
   200
(* node edits and associated executions *)
wenzelm@38150
   201
wenzelm@52862
   202
type overlay = Document_ID.command * (string * string list);
wenzelm@52849
   203
wenzelm@44157
   204
datatype node_edit =
wenzelm@52530
   205
  Edits of (Document_ID.command option * Document_ID.command option) list |
wenzelm@48707
   206
  Deps of node_header |
wenzelm@52849
   207
  Perspective of bool * Document_ID.command list * overlay list;
wenzelm@44157
   208
wenzelm@44156
   209
type edit = string * node_edit;
wenzelm@38448
   210
wenzelm@49064
   211
val after_entry = Entries.get_after o get_entries;
wenzelm@44479
   212
wenzelm@49064
   213
fun lookup_entry node id =
wenzelm@49064
   214
  (case Entries.lookup (get_entries node) id of
wenzelm@44480
   215
    NONE => NONE
wenzelm@44480
   216
  | SOME (exec, _) => exec);
wenzelm@44480
   217
wenzelm@49064
   218
fun the_entry node id =
wenzelm@49064
   219
  (case Entries.lookup (get_entries node) id of
wenzelm@38448
   220
    NONE => err_undef "command entry" id
wenzelm@44476
   221
  | SOME (exec, _) => exec);
wenzelm@38150
   222
wenzelm@52566
   223
fun assign_entry (command_id, exec) node =
wenzelm@52566
   224
  if is_none (Entries.lookup (get_entries node) command_id) then node
wenzelm@52566
   225
  else map_entries (Entries.update (command_id, exec)) node;
wenzelm@38418
   226
wenzelm@38448
   227
fun reset_after id entries =
wenzelm@38448
   228
  (case Entries.get_after entries id of
wenzelm@38448
   229
    NONE => entries
wenzelm@38448
   230
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   231
wenzelm@43668
   232
val edit_node = map_entries o fold
wenzelm@43668
   233
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   234
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   235
wenzelm@38418
   236
wenzelm@38418
   237
(* version operations *)
wenzelm@38418
   238
wenzelm@50862
   239
val empty_version = Version String_Graph.empty;
wenzelm@44185
   240
wenzelm@38418
   241
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   242
val node_of = get_node o nodes_of;
wenzelm@38418
   243
wenzelm@44185
   244
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   245
wenzelm@44157
   246
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   247
  Version
wenzelm@44436
   248
    (case node_edit of
wenzelm@54562
   249
      Edits edits => update_node name (edit_node edits) nodes
wenzelm@59715
   250
    | Deps {master, header, errors} =>
wenzelm@44436
   251
        let
wenzelm@48927
   252
          val imports = map fst (#imports header);
wenzelm@44436
   253
          val nodes1 = nodes
wenzelm@44436
   254
            |> default_node name
wenzelm@48927
   255
            |> fold default_node imports;
wenzelm@44436
   256
          val nodes2 = nodes1
wenzelm@50862
   257
            |> String_Graph.Keys.fold
wenzelm@50862
   258
                (fn dep => String_Graph.del_edge (dep, name)) (String_Graph.imm_preds nodes1 name);
wenzelm@59086
   259
          val (nodes3, errors1) =
wenzelm@59086
   260
            (String_Graph.add_deps_acyclic (name, imports) nodes2, errors)
wenzelm@59086
   261
              handle String_Graph.CYCLES cs => (nodes2, errors @ map cycle_msg cs);
wenzelm@59715
   262
        in String_Graph.map_node name (set_header master header errors1) nodes3 end
wenzelm@47406
   263
    | Perspective perspective => update_node name (set_perspective perspective) nodes);
wenzelm@44444
   264
wenzelm@59086
   265
fun update_keywords name nodes =
wenzelm@59086
   266
  nodes |> String_Graph.map_node name (fn node =>
wenzelm@59086
   267
    if is_empty_node node then node
wenzelm@59086
   268
    else
wenzelm@59086
   269
      let
wenzelm@59715
   270
        val {master, header, errors} = get_header node;
wenzelm@59086
   271
        val imports_keywords = map_filter (get_keywords o get_node nodes o #1) (#imports header);
wenzelm@59086
   272
        val keywords =
wenzelm@59086
   273
          Library.foldl Keyword.merge_keywords (Session.get_keywords (), imports_keywords);
wenzelm@59086
   274
        val (keywords', errors') =
wenzelm@59086
   275
          (Keyword.add_keywords (#keywords header) keywords, errors)
wenzelm@59086
   276
            handle ERROR msg =>
wenzelm@59086
   277
              (keywords, if member (op =) errors msg then errors else errors @ [msg]);
wenzelm@59086
   278
      in
wenzelm@59086
   279
        node
wenzelm@59715
   280
        |> set_header master header errors'
wenzelm@59086
   281
        |> set_keywords (SOME keywords')
wenzelm@59086
   282
      end);
wenzelm@59086
   283
wenzelm@59086
   284
fun edit_keywords edits (Version nodes) =
wenzelm@59086
   285
  Version
wenzelm@59086
   286
    (fold update_keywords
wenzelm@59086
   287
      (String_Graph.all_succs nodes (map_filter (fn (a, Deps _) => SOME a | _ => NONE) edits))
wenzelm@59086
   288
      nodes);
wenzelm@59086
   289
wenzelm@44222
   290
fun put_node (name, node) (Version nodes) =
wenzelm@57616
   291
  let
wenzelm@57616
   292
    val nodes1 = update_node name (K node) nodes;
wenzelm@57616
   293
    val nodes2 =
wenzelm@57616
   294
      if String_Graph.is_maximal nodes1 name andalso is_empty_node node
wenzelm@57616
   295
      then String_Graph.del_node name nodes1
wenzelm@57616
   296
      else nodes1;
wenzelm@57616
   297
  in Version nodes2 end;
wenzelm@38418
   298
wenzelm@38150
   299
end;
wenzelm@38150
   300
wenzelm@38418
   301
wenzelm@52595
   302
wenzelm@52595
   303
(** main state -- document structure and execution process **)
wenzelm@52536
   304
wenzelm@56458
   305
type blob_digest = (string * string option) Exn.result;  (*file node name, raw digest*)
wenzelm@55798
   306
wenzelm@52774
   307
type execution =
wenzelm@52774
   308
 {version_id: Document_ID.version,  (*static version id*)
wenzelm@52774
   309
  execution_id: Document_ID.execution,  (*dynamic execution id*)
wenzelm@59193
   310
  delay_request: unit future};  (*pending event timer request*)
wenzelm@52604
   311
wenzelm@52774
   312
val no_execution: execution =
wenzelm@59193
   313
  {version_id = Document_ID.none,
wenzelm@59193
   314
   execution_id = Document_ID.none,
wenzelm@59193
   315
   delay_request = Future.value ()};
wenzelm@52774
   316
wenzelm@59193
   317
fun new_execution version_id delay_request : execution =
wenzelm@59193
   318
  {version_id = version_id,
wenzelm@59193
   319
   execution_id = Execution.start (),
wenzelm@59193
   320
   delay_request = delay_request};
wenzelm@52604
   321
wenzelm@38418
   322
abstype state = State of
wenzelm@52536
   323
 {versions: version Inttab.table,  (*version id -> document content*)
wenzelm@55798
   324
  blobs: (SHA1.digest * string list) Symtab.table,  (*raw digest -> digest, lines*)
wenzelm@59689
   325
  commands: (string * blob_digest list * int * Token.T list lazy) Inttab.table,
wenzelm@59689
   326
    (*command id -> name, inlined files, token index of files, command span*)
wenzelm@52536
   327
  execution: execution}  (*current execution process*)
wenzelm@38418
   328
with
wenzelm@38418
   329
wenzelm@54519
   330
fun make_state (versions, blobs, commands, execution) =
wenzelm@54519
   331
  State {versions = versions, blobs = blobs, commands = commands, execution = execution};
wenzelm@38418
   332
wenzelm@54519
   333
fun map_state f (State {versions, blobs, commands, execution}) =
wenzelm@54519
   334
  make_state (f (versions, blobs, commands, execution));
wenzelm@38418
   335
wenzelm@38418
   336
val init_state =
wenzelm@54519
   337
  make_state (Inttab.make [(Document_ID.none, empty_version)],
wenzelm@54519
   338
    Symtab.empty, Inttab.empty, no_execution);
wenzelm@38418
   339
wenzelm@38418
   340
wenzelm@38418
   341
(* document versions *)
wenzelm@38418
   342
wenzelm@52536
   343
fun define_version version_id version =
wenzelm@59193
   344
  map_state (fn (versions, blobs, commands, {delay_request, ...}) =>
wenzelm@47420
   345
    let
wenzelm@52536
   346
      val versions' = Inttab.update_new (version_id, version) versions
wenzelm@47420
   347
        handle Inttab.DUP dup => err_dup "document version" dup;
wenzelm@59193
   348
      val execution' = new_execution version_id delay_request;
wenzelm@54519
   349
    in (versions', blobs, commands, execution') end);
wenzelm@38418
   350
wenzelm@52536
   351
fun the_version (State {versions, ...}) version_id =
wenzelm@52536
   352
  (case Inttab.lookup versions version_id of
wenzelm@52536
   353
    NONE => err_undef "document version" version_id
wenzelm@38418
   354
  | SOME version => version);
wenzelm@38418
   355
wenzelm@52536
   356
fun delete_version version_id versions =
wenzelm@52536
   357
  Inttab.delete version_id versions
wenzelm@52536
   358
    handle Inttab.UNDEF _ => err_undef "document version" version_id;
wenzelm@44673
   359
wenzelm@38418
   360
wenzelm@54519
   361
(* inlined files *)
wenzelm@54519
   362
wenzelm@54519
   363
fun define_blob digest text =
wenzelm@54519
   364
  map_state (fn (versions, blobs, commands, execution) =>
wenzelm@57638
   365
    let val blobs' = Symtab.update (digest, (SHA1.fake digest, split_lines text)) blobs
wenzelm@54519
   366
    in (versions, blobs', commands, execution) end);
wenzelm@54519
   367
wenzelm@54519
   368
fun the_blob (State {blobs, ...}) digest =
wenzelm@54519
   369
  (case Symtab.lookup blobs digest of
wenzelm@54519
   370
    NONE => error ("Undefined blob: " ^ digest)
wenzelm@55788
   371
  | SOME content => content);
wenzelm@54519
   372
wenzelm@56447
   373
fun resolve_blob state (blob_digest: blob_digest) =
wenzelm@61077
   374
  blob_digest |> Exn.map_res (fn (file_node, raw_digest) =>
wenzelm@56458
   375
    (file_node, Option.map (the_blob state) raw_digest));
wenzelm@56447
   376
wenzelm@59685
   377
fun blob_reports pos (blob_digest: blob_digest) =
wenzelm@59685
   378
  (case blob_digest of Exn.Res (file_node, _) => [(pos, Markup.path file_node)] | _ => []);
wenzelm@59685
   379
wenzelm@54519
   380
wenzelm@38418
   381
(* commands *)
wenzelm@38418
   382
wenzelm@59704
   383
fun define_command command_id name blobs_digests blobs_index toks =
wenzelm@54519
   384
  map_state (fn (versions, blobs, commands, execution) =>
wenzelm@38418
   385
    let
wenzelm@52536
   386
      val id = Document_ID.print command_id;
wenzelm@52536
   387
      val span =
wenzelm@66168
   388
        Lazy.lazy_name "Document.define_command" (fn () =>
wenzelm@52536
   389
          Position.setmp_thread_data (Position.id_only id)
wenzelm@59685
   390
            (fn () =>
wenzelm@59685
   391
              let
wenzelm@59685
   392
                val (tokens, _) = fold_map Token.make toks (Position.id id);
wenzelm@59685
   393
                val _ =
wenzelm@59704
   394
                  if blobs_index < 0
wenzelm@59704
   395
                  then (*inlined errors*)
wenzelm@59704
   396
                    map_filter Exn.get_exn blobs_digests
wenzelm@59704
   397
                    |> List.app (Output.error_message o Runtime.exn_message)
wenzelm@59704
   398
                  else (*auxiliary files*)
wenzelm@60034
   399
                    let val pos = Token.pos_of (nth tokens blobs_index)
wenzelm@60034
   400
                    in Position.reports (maps (blob_reports pos) blobs_digests) end;
wenzelm@59685
   401
              in tokens end) ());
wenzelm@59685
   402
      val commands' =
wenzelm@59704
   403
        Inttab.update_new (command_id, (name, blobs_digests, blobs_index, span)) commands
wenzelm@59685
   404
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@47395
   405
      val _ =
wenzelm@52536
   406
        Position.setmp_thread_data (Position.id_only id)
wenzelm@50201
   407
          (fn () => Output.status (Markup.markup_only Markup.accepted)) ();
wenzelm@54519
   408
    in (versions, blobs, commands', execution) end);
wenzelm@38418
   409
wenzelm@52536
   410
fun the_command (State {commands, ...}) command_id =
wenzelm@52536
   411
  (case Inttab.lookup commands command_id of
wenzelm@52536
   412
    NONE => err_undef "command" command_id
wenzelm@44644
   413
  | SOME command => command);
wenzelm@38418
   414
wenzelm@52569
   415
val the_command_name = #1 oo the_command;
wenzelm@52569
   416
wenzelm@68185
   417
fun force_command_span state =
wenzelm@68185
   418
  Outer_Syntax.make_span o Lazy.force o #4 o the_command state;
wenzelm@68185
   419
wenzelm@66382
   420
wenzelm@66382
   421
(* execution *)
wenzelm@66382
   422
wenzelm@66382
   423
fun get_execution (State {execution, ...}) = execution;
wenzelm@66382
   424
fun get_execution_version state = the_version state (#version_id (get_execution state));
wenzelm@66382
   425
wenzelm@60880
   426
fun command_exec state node_name command_id =
wenzelm@60880
   427
  let
wenzelm@66382
   428
    val version = get_execution_version state;
wenzelm@66382
   429
    val node = get_node (nodes_of version) node_name;
wenzelm@60880
   430
  in the_entry node command_id end;
wenzelm@60880
   431
wenzelm@47420
   432
end;
wenzelm@38418
   433
wenzelm@52566
   434
wenzelm@52566
   435
(* remove_versions *)
wenzelm@52566
   436
wenzelm@54519
   437
fun remove_versions version_ids state = state |> map_state (fn (versions, _, _, execution) =>
wenzelm@47420
   438
  let
wenzelm@52536
   439
    val _ =
wenzelm@52536
   440
      member (op =) version_ids (#version_id execution) andalso
wenzelm@52536
   441
        error ("Attempt to remove execution version " ^ Document_ID.print (#version_id execution));
wenzelm@47343
   442
wenzelm@52536
   443
    val versions' = fold delete_version version_ids versions;
wenzelm@47420
   444
    val commands' =
wenzelm@47420
   445
      (versions', Inttab.empty) |->
wenzelm@47420
   446
        Inttab.fold (fn (_, version) => nodes_of version |>
wenzelm@50862
   447
          String_Graph.fold (fn (_, (node, _)) => node |>
wenzelm@52536
   448
            iterate_entries (fn ((_, command_id), _) =>
wenzelm@52536
   449
              SOME o Inttab.insert (K true) (command_id, the_command state command_id))));
wenzelm@54519
   450
    val blobs' =
wenzelm@54519
   451
      (commands', Symtab.empty) |->
wenzelm@59689
   452
        Inttab.fold (fn (_, (_, blobs, _, _)) => blobs |>
wenzelm@56458
   453
          fold (fn Exn.Res (_, SOME b) => Symtab.update (b, the_blob state b) | _ => I));
wenzelm@54519
   454
wenzelm@54519
   455
  in (versions', blobs', commands', execution) end);
wenzelm@38418
   456
wenzelm@38418
   457
wenzelm@52604
   458
(* document execution *)
wenzelm@47389
   459
wenzelm@60198
   460
fun make_required nodes =
wenzelm@60198
   461
  let
wenzelm@60198
   462
    fun all_preds P =
wenzelm@60198
   463
      String_Graph.fold (fn (a, (node, _)) => P node ? cons a) nodes []
wenzelm@60198
   464
      |> String_Graph.all_preds nodes
wenzelm@60198
   465
      |> Symtab.make_set;
wenzelm@60198
   466
wenzelm@60198
   467
    val all_visible = all_preds visible_node;
wenzelm@60198
   468
    val all_required = all_preds required_node;
wenzelm@60198
   469
  in
wenzelm@60198
   470
    Symtab.fold (fn (a, ()) =>
wenzelm@60198
   471
      exists (Symtab.defined all_visible) (String_Graph.immediate_succs nodes a) ?
wenzelm@60198
   472
        Symtab.update (a, ())) all_visible all_required
wenzelm@60198
   473
  end;
wenzelm@60198
   474
wenzelm@54519
   475
fun start_execution state = state |> map_state (fn (versions, blobs, commands, execution) =>
wenzelm@52796
   476
  timeit "Document.start_execution" (fn () =>
wenzelm@52796
   477
    let
wenzelm@59193
   478
      val {version_id, execution_id, delay_request} = execution;
wenzelm@52798
   479
wenzelm@52798
   480
      val delay = seconds (Options.default_real "editor_execution_delay");
wenzelm@52774
   481
wenzelm@52798
   482
      val _ = Future.cancel delay_request;
wenzelm@62826
   483
      val delay_request' = Event_Timer.future (Time.now () + delay);
wenzelm@52798
   484
wenzelm@59198
   485
      fun finished_import (name, (node, _)) =
wenzelm@65445
   486
        finished_result node orelse is_some (Thy_Info.lookup_theory name);
wenzelm@60198
   487
wenzelm@60198
   488
      val nodes = nodes_of (the_version state version_id);
wenzelm@60198
   489
      val required = make_required nodes;
wenzelm@59193
   490
      val _ =
wenzelm@60198
   491
        nodes |> String_Graph.schedule
wenzelm@52573
   492
          (fn deps => fn (name, node) =>
wenzelm@60198
   493
            if Symtab.defined required name orelse visible_node node orelse pending_result node then
wenzelm@52774
   494
              let
wenzelm@52775
   495
                fun body () =
wenzelm@59347
   496
                  (if forall finished_import deps then
wenzelm@59198
   497
                    iterate_entries (fn (_, opt_exec) => fn () =>
wenzelm@59198
   498
                      (case opt_exec of
wenzelm@59198
   499
                        SOME exec =>
wenzelm@59198
   500
                          if Execution.is_running execution_id
wenzelm@59198
   501
                          then SOME (Command.exec execution_id exec)
wenzelm@59198
   502
                          else NONE
wenzelm@59198
   503
                      | NONE => NONE)) node ()
wenzelm@59347
   504
                   else ())
wenzelm@62505
   505
                   handle exn => (Output.system_message (Runtime.exn_message exn); Exn.reraise exn);
wenzelm@52774
   506
                val future =
wenzelm@52774
   507
                  (singleton o Future.forks)
wenzelm@59193
   508
                   {name = "theory:" ^ name,
wenzelm@59193
   509
                    group = SOME (Future.new_group NONE),
wenzelm@59198
   510
                    deps = Future.task_of delay_request' :: maps (the_list o #2 o #2) deps,
wenzelm@59193
   511
                    pri = 0, interrupts = false} body;
wenzelm@59198
   512
              in (node, SOME (Future.task_of future)) end
wenzelm@59198
   513
            else (node, NONE));
wenzelm@52798
   514
      val execution' =
wenzelm@59193
   515
        {version_id = version_id, execution_id = execution_id, delay_request = delay_request'};
wenzelm@54519
   516
    in (versions, blobs, commands, execution') end));
wenzelm@47345
   517
wenzelm@68197
   518
fun presentation options thy node_name node =
wenzelm@68197
   519
  if Options.bool options "editor_presentation" then
wenzelm@68197
   520
    let
wenzelm@68197
   521
      val (adjust, segments) = Future.get_finished (get_consolidation node);
wenzelm@68197
   522
      val presentation_context: Thy_Info.presentation_context =
wenzelm@68197
   523
       {options = options,
wenzelm@68197
   524
        file_pos = Position.file node_name,
wenzelm@68197
   525
        adjust_pos = Position.adjust_offsets adjust,
wenzelm@68197
   526
        segments = segments};
wenzelm@68197
   527
    in Thy_Info.apply_presentation presentation_context thy end
wenzelm@68197
   528
  else ();
wenzelm@68197
   529
wenzelm@66382
   530
fun consolidate_execution state =
wenzelm@68185
   531
  let
wenzelm@68197
   532
    fun check_consolidation node_name node =
wenzelm@68197
   533
      if Future.is_finished (get_consolidation node) then ()
wenzelm@68185
   534
      else
wenzelm@68185
   535
        (case finished_result_theory node of
wenzelm@68185
   536
          NONE => ()
wenzelm@68185
   537
        | SOME thy =>
wenzelm@68185
   538
            let
wenzelm@68185
   539
              val result_id = Command.eval_exec_id (the (get_result node));
wenzelm@68185
   540
              val eval_ids =
wenzelm@68185
   541
                iterate_entries (fn (_, opt_exec) => fn eval_ids =>
wenzelm@68185
   542
                  (case opt_exec of
wenzelm@68185
   543
                    SOME (eval, _) => SOME (cons (Command.eval_exec_id eval) eval_ids)
wenzelm@68185
   544
                  | NONE => NONE)) node [];
wenzelm@68185
   545
            in
wenzelm@68185
   546
              (case Execution.snapshot eval_ids of
wenzelm@68185
   547
                [] =>
wenzelm@68185
   548
                  let
wenzelm@68185
   549
                    val (_, offsets, rev_segments) =
wenzelm@68185
   550
                      iterate_entries (fn (_, opt_exec) => fn (offset, offsets, segments) =>
wenzelm@68185
   551
                        (case opt_exec of
wenzelm@68185
   552
                          SOME (eval, _) =>
wenzelm@68185
   553
                            let
wenzelm@68185
   554
                              val command_id = Command.eval_command_id eval;
wenzelm@68185
   555
                              val span = force_command_span state command_id;
wenzelm@68185
   556
wenzelm@68185
   557
                              val exec_id = Command.eval_exec_id eval;
wenzelm@68185
   558
                              val tr = Command.eval_result_command eval;
wenzelm@68185
   559
                              val st' = Command.eval_result_state eval;
wenzelm@68185
   560
wenzelm@68185
   561
                              val offset' = offset + the_default 0 (Command_Span.symbol_length span);
wenzelm@68185
   562
                              val offsets' = offsets
wenzelm@68185
   563
                                |> Inttab.update (command_id, offset)
wenzelm@68185
   564
                                |> Inttab.update (exec_id, offset);
wenzelm@68185
   565
                              val segments' = (span, tr, st') :: segments;
wenzelm@68185
   566
                            in SOME (offset', offsets', segments') end
wenzelm@68185
   567
                        | NONE => NONE)) node (0, Inttab.empty, []);
wenzelm@68185
   568
wenzelm@68185
   569
                    val adjust = Inttab.lookup offsets;
wenzelm@68185
   570
                    val segments =
wenzelm@68185
   571
                      rev rev_segments |> map (fn (span, tr, st') =>
wenzelm@68185
   572
                        {span = Command_Span.adjust_offsets adjust span, command = tr, state = st'});
wenzelm@68185
   573
wenzelm@68197
   574
                    val _ = Future.fulfill (get_consolidation node) (adjust, segments);
wenzelm@68185
   575
                    val _ =
wenzelm@68185
   576
                      Position.setmp_thread_data (Position.id_only (Document_ID.print result_id))
wenzelm@68185
   577
                        (fn () =>
wenzelm@68197
   578
                          (Output.status (Markup.markup_only Markup.consolidated);
wenzelm@68197
   579
                            (* FIXME avoid user operations on protocol thread *)
wenzelm@68197
   580
                            presentation (Options.default ()) thy node_name node)) ();
wenzelm@68185
   581
                  in () end
wenzelm@68185
   582
              | _ => ())
wenzelm@68185
   583
            end);
wenzelm@68185
   584
      in
wenzelm@68197
   585
        String_Graph.fold (fn (node_name, (node, _)) => fn () => check_consolidation node_name node)
wenzelm@68185
   586
          (nodes_of (get_execution_version state)) ()
wenzelm@68185
   587
      end;
wenzelm@66382
   588
wenzelm@47345
   589
wenzelm@47420
   590
wenzelm@47420
   591
(** document update **)
wenzelm@38418
   592
wenzelm@52566
   593
(* exec state assignment *)
wenzelm@52566
   594
wenzelm@52566
   595
type assign_update = Command.exec option Inttab.table;  (*command id -> exec*)
wenzelm@52566
   596
wenzelm@52566
   597
val assign_update_empty: assign_update = Inttab.empty;
wenzelm@52566
   598
fun assign_update_defined (tab: assign_update) command_id = Inttab.defined tab command_id;
wenzelm@52566
   599
fun assign_update_apply (tab: assign_update) node = Inttab.fold assign_entry tab node;
wenzelm@52566
   600
wenzelm@52566
   601
fun assign_update_new upd (tab: assign_update) =
wenzelm@52566
   602
  Inttab.update_new upd tab
wenzelm@52566
   603
    handle Inttab.DUP dup => err_dup "exec state assignment" dup;
wenzelm@52566
   604
wenzelm@52566
   605
fun assign_update_result (tab: assign_update) =
wenzelm@52566
   606
  Inttab.fold (fn (command_id, exec) => cons (command_id, Command.exec_ids exec)) tab [];
wenzelm@52566
   607
wenzelm@52566
   608
wenzelm@52566
   609
(* update *)
wenzelm@52566
   610
wenzelm@38418
   611
local
wenzelm@38418
   612
wenzelm@48927
   613
fun init_theory deps node span =
wenzelm@47335
   614
  let
wenzelm@54526
   615
    val master_dir = master_directory node;
wenzelm@54526
   616
    val header = read_header node span;
wenzelm@48927
   617
    val imports = #imports header;
wenzelm@59716
   618
wenzelm@59716
   619
    fun maybe_end_theory pos st =
wenzelm@59716
   620
      SOME (Toplevel.end_theory pos st)
wenzelm@59716
   621
        handle ERROR msg => (Output.error_message msg; NONE);
wenzelm@59716
   622
    val parents_reports =
wenzelm@59716
   623
      imports |> map_filter (fn (import, pos) =>
wenzelm@65445
   624
        (case Thy_Info.lookup_theory import of
wenzelm@59716
   625
          NONE =>
wenzelm@59716
   626
            maybe_end_theory pos
wenzelm@52536
   627
              (case get_result (snd (the (AList.lookup (op =) deps import))) of
wenzelm@52536
   628
                NONE => Toplevel.toplevel
wenzelm@59716
   629
              | SOME eval => Command.eval_result_state eval)
wenzelm@59716
   630
        | some => some)
wenzelm@59716
   631
        |> Option.map (fn thy => (thy, (pos, Theory.get_markup thy))));
wenzelm@59716
   632
wenzelm@59716
   633
    val parents =
wenzelm@67380
   634
      if null parents_reports then [Theory.get_pure ()] else map #1 parents_reports;
wenzelm@59716
   635
    val _ = Position.reports (map #2 parents_reports);
wenzelm@56208
   636
  in Resources.begin_theory master_dir header parents end;
wenzelm@47335
   637
wenzelm@67215
   638
fun check_root_theory node =
wenzelm@67215
   639
  let
wenzelm@67215
   640
    val master_dir = master_directory node;
wenzelm@67215
   641
    val header = #header (get_header node);
wenzelm@67215
   642
    val header_name = #1 (#name header);
wenzelm@67215
   643
    val parent =
wenzelm@67215
   644
      if header_name = Sessions.root_name then
wenzelm@67215
   645
        SOME (Thy_Info.get_theory Sessions.theory_name)
wenzelm@67215
   646
      else if member (op =) Thy_Header.ml_roots header_name then
wenzelm@67215
   647
        SOME (Thy_Info.get_theory Thy_Header.ml_bootstrapN)
wenzelm@67215
   648
      else NONE;
wenzelm@67215
   649
  in parent |> Option.map (fn thy => Resources.begin_theory master_dir header [thy]) end;
wenzelm@62895
   650
wenzelm@47407
   651
fun check_theory full name node =
wenzelm@65445
   652
  is_some (Thy_Info.lookup_theory name) orelse
wenzelm@59715
   653
  null (#errors (get_header node)) andalso (not full orelse is_some (get_result node));
wenzelm@47335
   654
wenzelm@58923
   655
fun last_common keywords state node_required node0 node =
wenzelm@44482
   656
  let
wenzelm@44645
   657
    fun update_flags prev (visible, initial) =
wenzelm@44645
   658
      let
wenzelm@52569
   659
        val visible' = visible andalso prev <> visible_last node;
wenzelm@44645
   660
        val initial' = initial andalso
wenzelm@44645
   661
          (case prev of
wenzelm@44645
   662
            NONE => true
wenzelm@59735
   663
          | SOME command_id => the_command_name state command_id <> Thy_Header.theoryN);
wenzelm@44645
   664
      in (visible', initial') end;
wenzelm@52569
   665
wenzelm@52784
   666
    fun get_common ((prev, command_id), opt_exec) (_, ok, flags, assign_update) =
wenzelm@52784
   667
      if ok then
wenzelm@47630
   668
        let
wenzelm@52784
   669
          val flags' as (visible', _) = update_flags prev flags;
wenzelm@52784
   670
          val ok' =
wenzelm@52586
   671
            (case (lookup_entry node0 command_id, opt_exec) of
wenzelm@52784
   672
              (SOME (eval0, _), SOME (eval, _)) =>
wenzelm@52784
   673
                Command.eval_eq (eval0, eval) andalso
wenzelm@52784
   674
                  (visible' orelse node_required orelse Command.eval_running eval)
wenzelm@52586
   675
            | _ => false);
wenzelm@52784
   676
          val assign_update' = assign_update |> ok' ?
wenzelm@52569
   677
            (case opt_exec of
wenzelm@52569
   678
              SOME (eval, prints) =>
wenzelm@52569
   679
                let
wenzelm@52569
   680
                  val command_visible = visible_command node command_id;
wenzelm@52850
   681
                  val command_overlays = overlays node command_id;
wenzelm@52569
   682
                  val command_name = the_command_name state command_id;
wenzelm@52569
   683
                in
wenzelm@58923
   684
                  (case
wenzelm@58923
   685
                    Command.print command_visible command_overlays keywords command_name eval prints
wenzelm@58923
   686
                   of
wenzelm@52569
   687
                    SOME prints' => assign_update_new (command_id, SOME (eval, prints'))
wenzelm@52569
   688
                  | NONE => I)
wenzelm@52569
   689
                end
wenzelm@52569
   690
            | NONE => I);
wenzelm@52784
   691
        in SOME (prev, ok', flags', assign_update') end
wenzelm@47630
   692
      else NONE;
wenzelm@52784
   693
    val (common, ok, flags, assign_update') =
wenzelm@52569
   694
      iterate_entries get_common node (NONE, true, (true, true), assign_update_empty);
wenzelm@52569
   695
    val (common', flags') =
wenzelm@52784
   696
      if ok then
wenzelm@52569
   697
        let val last = Entries.get_after (get_entries node) common
wenzelm@52569
   698
        in (last, update_flags last flags) end
wenzelm@52569
   699
      else (common, flags);
wenzelm@52569
   700
  in (assign_update', common', flags') end;
wenzelm@44645
   701
wenzelm@62895
   702
fun illegal_init _ = error "Illegal theory header";
wenzelm@52534
   703
wenzelm@58923
   704
fun new_exec keywords state node proper_init command_id' (assign_update, command_exec, init) =
wenzelm@47407
   705
  if not proper_init andalso is_none init then NONE
wenzelm@44645
   706
  else
wenzelm@44645
   707
    let
wenzelm@52850
   708
      val command_visible = visible_command node command_id';
wenzelm@52850
   709
      val command_overlays = overlays node command_id';
wenzelm@59689
   710
      val (command_name, blob_digests, blobs_index, span0) = the_command state command_id';
wenzelm@56447
   711
      val blobs = map (resolve_blob state) blob_digests;
wenzelm@54519
   712
      val span = Lazy.force span0;
wenzelm@52566
   713
wenzelm@54519
   714
      val eval' =
wenzelm@66370
   715
        Command.eval keywords (master_directory node) (fn () => the_default illegal_init init span)
wenzelm@68185
   716
          (blobs, blobs_index) command_id' span (#1 (#2 command_exec));
wenzelm@58923
   717
      val prints' =
wenzelm@58923
   718
        perhaps (Command.print command_visible command_overlays keywords command_name eval') [];
wenzelm@52566
   719
      val exec' = (eval', prints');
wenzelm@44482
   720
wenzelm@52566
   721
      val assign_update' = assign_update_new (command_id', SOME exec') assign_update;
wenzelm@59735
   722
      val init' = if command_name = Thy_Header.theoryN then NONE else init;
wenzelm@66370
   723
    in SOME (assign_update', (command_id', exec'), init') end;
wenzelm@52602
   724
wenzelm@52655
   725
fun removed_execs node0 (command_id, exec_ids) =
wenzelm@52655
   726
  subtract (op =) exec_ids (Command.exec_ids (lookup_entry node0 command_id));
wenzelm@52602
   727
wenzelm@38418
   728
in
wenzelm@38418
   729
wenzelm@59056
   730
fun update old_version_id new_version_id edits state = Runtime.exn_trace_system (fn () =>
wenzelm@38418
   731
  let
wenzelm@52536
   732
    val old_version = the_version state old_version_id;
wenzelm@59086
   733
    val new_version =
wenzelm@59086
   734
      timeit "Document.edit_nodes"
wenzelm@59086
   735
        (fn () => old_version |> fold edit_nodes edits |> edit_keywords edits);
wenzelm@38418
   736
wenzelm@44544
   737
    val nodes = nodes_of new_version;
wenzelm@52776
   738
    val required = make_required nodes;
wenzelm@52776
   739
    val required0 = make_required (nodes_of old_version);
wenzelm@52772
   740
    val edited = fold (fn (name, _) => Symtab.update (name, ())) edits Symtab.empty;
wenzelm@44544
   741
wenzelm@47628
   742
    val updated = timeit "Document.update" (fn () =>
wenzelm@50862
   743
      nodes |> String_Graph.schedule
wenzelm@44199
   744
        (fn deps => fn (name, node) =>
wenzelm@47406
   745
          (singleton o Future.forks)
wenzelm@47406
   746
            {name = "Document.update", group = NONE,
wenzelm@52716
   747
              deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
wenzelm@59056
   748
            (fn () =>
wenzelm@59056
   749
              timeit ("Document.update " ^ name) (fn () =>
wenzelm@59056
   750
                Runtime.exn_trace_system (fn () =>
wenzelm@44482
   751
                  let
wenzelm@67215
   752
                    val root_theory = check_root_theory node;
wenzelm@59086
   753
                    val keywords = the_default (Session.get_keywords ()) (get_keywords node);
wenzelm@59056
   754
                    val imports = map (apsnd Future.join) deps;
wenzelm@59056
   755
                    val imports_result_changed = exists (#4 o #1 o #2) imports;
wenzelm@59056
   756
                    val node_required = Symtab.defined required name;
wenzelm@59056
   757
                  in
wenzelm@59056
   758
                    if Symtab.defined edited name orelse visible_node node orelse
wenzelm@59056
   759
                      imports_result_changed orelse Symtab.defined required0 name <> node_required
wenzelm@59056
   760
                    then
wenzelm@59056
   761
                      let
wenzelm@59056
   762
                        val node0 = node_of old_version name;
wenzelm@59056
   763
                        val init = init_theory imports node;
wenzelm@59056
   764
                        val proper_init =
wenzelm@67215
   765
                          is_some root_theory orelse
wenzelm@62895
   766
                            check_theory false name node andalso
wenzelm@62895
   767
                            forall (fn (name, (_, node)) => check_theory true name node) imports;
wenzelm@47406
   768
wenzelm@59056
   769
                        val (print_execs, common, (still_visible, initial)) =
wenzelm@59056
   770
                          if imports_result_changed then (assign_update_empty, NONE, (true, true))
wenzelm@59056
   771
                          else last_common keywords state node_required node0 node;
wenzelm@62895
   772
wenzelm@62895
   773
                        val common_command_exec =
wenzelm@62895
   774
                          (case common of
wenzelm@62895
   775
                            SOME id => (id, the_default Command.no_exec (the_entry node id))
wenzelm@67215
   776
                          | NONE => (Document_ID.none, Command.init_exec root_theory));
wenzelm@44479
   777
wenzelm@66370
   778
                        val (updated_execs, (command_id', exec'), _) =
wenzelm@59056
   779
                          (print_execs, common_command_exec, if initial then SOME init else NONE)
wenzelm@59056
   780
                          |> (still_visible orelse node_required) ?
wenzelm@59056
   781
                            iterate_entries_after common
wenzelm@59056
   782
                              (fn ((prev, id), _) => fn res =>
wenzelm@59056
   783
                                if not node_required andalso prev = visible_last node then NONE
wenzelm@59056
   784
                                else new_exec keywords state node proper_init id res) node;
wenzelm@44479
   785
wenzelm@59056
   786
                        val assigned_execs =
wenzelm@59056
   787
                          (node0, updated_execs) |-> iterate_entries_after common
wenzelm@59056
   788
                            (fn ((_, command_id0), exec0) => fn res =>
wenzelm@59056
   789
                              if is_none exec0 then NONE
wenzelm@59056
   790
                              else if assign_update_defined updated_execs command_id0 then SOME res
wenzelm@59056
   791
                              else SOME (assign_update_new (command_id0, NONE) res));
wenzelm@44480
   792
wenzelm@59056
   793
                        val last_exec =
wenzelm@59056
   794
                          if command_id' = Document_ID.none then NONE else SOME command_id';
wenzelm@59056
   795
                        val result =
wenzelm@59056
   796
                          if is_none last_exec orelse is_some (after_entry node last_exec) then NONE
wenzelm@66370
   797
                          else SOME (#1 exec');
wenzelm@44480
   798
wenzelm@59056
   799
                        val assign_update = assign_update_result assigned_execs;
wenzelm@59056
   800
                        val removed = maps (removed_execs node0) assign_update;
wenzelm@59056
   801
                        val _ = List.app Execution.cancel removed;
wenzelm@52602
   802
wenzelm@66382
   803
                        val result_changed =
wenzelm@66382
   804
                          not (eq_option Command.eval_eq (get_result node0, result));
wenzelm@59056
   805
                        val node' = node
wenzelm@59056
   806
                          |> assign_update_apply assigned_execs
wenzelm@66382
   807
                          |> set_result result
wenzelm@68197
   808
                          |> result_changed ? reset_consolidation;
wenzelm@59056
   809
                        val assigned_node = SOME (name, node');
wenzelm@59056
   810
                      in ((removed, assign_update, assigned_node, result_changed), node') end
wenzelm@59056
   811
                    else (([], [], NONE, false), node)
wenzelm@59056
   812
                  end))))
wenzelm@47628
   813
      |> Future.joins |> map #1);
wenzelm@44476
   814
wenzelm@52655
   815
    val removed = maps #1 updated;
wenzelm@52655
   816
    val assign_update = maps #2 updated;
wenzelm@52602
   817
    val assigned_nodes = map_filter #3 updated;
wenzelm@52602
   818
wenzelm@44197
   819
    val state' = state
wenzelm@52602
   820
      |> define_version new_version_id (fold put_node assigned_nodes new_version);
wenzelm@52602
   821
wenzelm@59056
   822
  in (removed, assign_update, state') end);
wenzelm@38418
   823
wenzelm@38418
   824
end;
wenzelm@38418
   825
wenzelm@38418
   826
wenzelm@43713
   827
wenzelm@43713
   828
(** global state **)
wenzelm@43713
   829
wenzelm@52508
   830
val global_state = Synchronized.var "Document.global_state" init_state;
wenzelm@43713
   831
wenzelm@43713
   832
fun state () = Synchronized.value global_state;
wenzelm@43713
   833
val change_state = Synchronized.change global_state;
wenzelm@43713
   834
wenzelm@38418
   835
end;