src/Pure/PIDE/document.scala
author wenzelm
Fri Aug 02 14:26:09 2013 +0200 (2013-08-02)
changeset 52849 199e9fa5a5c2
parent 52808 143f225e50f5
child 52861 e93d73b51fd0
permissions -rw-r--r--
maintain overlays within node perspective;
tuned signature;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/document.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@36676
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@38418
     5
list of commands, associated with asynchronous execution process.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34760
    10
wenzelm@38150
    11
import scala.collection.mutable
wenzelm@37073
    12
wenzelm@37073
    13
wenzelm@34823
    14
object Document
wenzelm@34483
    15
{
wenzelm@38424
    16
  /** document structure **/
wenzelm@38424
    17
wenzelm@52849
    18
  /* overlays -- print functions with arguments */
wenzelm@52849
    19
wenzelm@52849
    20
  type Overlay = (Command, String, List[String])
wenzelm@52849
    21
wenzelm@52849
    22
  object Overlays
wenzelm@52849
    23
  {
wenzelm@52849
    24
    val empty = new Overlays(Set.empty)
wenzelm@52849
    25
  }
wenzelm@52849
    26
wenzelm@52849
    27
  final class Overlays private(rep: Set[Overlay])
wenzelm@52849
    28
  {
wenzelm@52849
    29
    def + (o: Overlay) = new Overlays(rep + o)
wenzelm@52849
    30
    def - (o: Overlay) = new Overlays(rep - o)
wenzelm@52849
    31
    def dest: List[Overlay] = rep.toList
wenzelm@52849
    32
  }
wenzelm@52849
    33
wenzelm@52849
    34
wenzelm@46723
    35
  /* individual nodes */
wenzelm@38424
    36
wenzelm@44615
    37
  type Edit[A, B] = (Node.Name, Node.Edit[A, B])
wenzelm@44384
    38
  type Edit_Text = Edit[Text.Edit, Text.Perspective]
wenzelm@52849
    39
  type Edit_Command = Edit[Command.Edit, Command.Perspective]
wenzelm@38151
    40
wenzelm@38151
    41
  object Node
wenzelm@38151
    42
  {
wenzelm@48707
    43
    sealed case class Header(
wenzelm@46938
    44
      imports: List[Name],
wenzelm@48706
    45
      keywords: Thy_Header.Keywords,
wenzelm@48707
    46
      errors: List[String] = Nil)
wenzelm@48707
    47
    {
wenzelm@48707
    48
      def error(msg: String): Header = copy(errors = errors ::: List(msg))
wenzelm@48707
    49
    }
wenzelm@48707
    50
wenzelm@51294
    51
    def bad_header(msg: String): Header = Header(Nil, Nil, List(msg))
wenzelm@46737
    52
wenzelm@44957
    53
    object Name
wenzelm@44957
    54
    {
wenzelm@44957
    55
      val empty = Name("", "", "")
wenzelm@46723
    56
wenzelm@46723
    57
      object Ordering extends scala.math.Ordering[Name]
wenzelm@46723
    58
      {
wenzelm@46723
    59
        def compare(name1: Name, name2: Name): Int = name1.node compare name2.node
wenzelm@46723
    60
      }
wenzelm@44957
    61
    }
wenzelm@44616
    62
    sealed case class Name(node: String, dir: String, theory: String)
wenzelm@44615
    63
    {
wenzelm@44615
    64
      override def hashCode: Int = node.hashCode
wenzelm@44615
    65
      override def equals(that: Any): Boolean =
wenzelm@44615
    66
        that match {
wenzelm@44615
    67
          case other: Name => node == other.node
wenzelm@44615
    68
          case _ => false
wenzelm@44615
    69
        }
wenzelm@44642
    70
      override def toString: String = theory
wenzelm@44615
    71
    }
wenzelm@44615
    72
wenzelm@44384
    73
    sealed abstract class Edit[A, B]
wenzelm@44156
    74
    {
wenzelm@44383
    75
      def foreach(f: A => Unit)
wenzelm@44383
    76
      {
wenzelm@44156
    77
        this match {
wenzelm@44383
    78
          case Edits(es) => es.foreach(f)
wenzelm@44383
    79
          case _ =>
wenzelm@44156
    80
        }
wenzelm@44383
    81
      }
wenzelm@44156
    82
    }
wenzelm@44384
    83
    case class Clear[A, B]() extends Edit[A, B]
wenzelm@44384
    84
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
wenzelm@48707
    85
    case class Deps[A, B](header: Header) extends Edit[A, B]
wenzelm@52849
    86
    case class Perspective[A, B](required: Boolean, visible: B, overlays: Overlays) extends Edit[A, B]
wenzelm@52808
    87
    type Perspective_Text = Perspective[Text.Edit, Text.Perspective]
wenzelm@52849
    88
    type Perspective_Command = Perspective[Command.Edit, Command.Perspective]
wenzelm@44156
    89
wenzelm@38426
    90
    def command_starts(commands: Iterator[Command], offset: Text.Offset = 0)
wenzelm@38426
    91
      : Iterator[(Command, Text.Offset)] =
wenzelm@38227
    92
    {
wenzelm@38227
    93
      var i = offset
wenzelm@38227
    94
      for (command <- commands) yield {
wenzelm@38227
    95
        val start = i
wenzelm@38227
    96
        i += command.length
wenzelm@38227
    97
        (command, start)
wenzelm@38227
    98
      }
wenzelm@38227
    99
    }
wenzelm@46680
   100
wenzelm@46680
   101
    private val block_size = 1024
wenzelm@46680
   102
wenzelm@46680
   103
    val empty: Node = new Node()
wenzelm@38151
   104
  }
wenzelm@38151
   105
wenzelm@46712
   106
  final class Node private(
wenzelm@48707
   107
    val header: Node.Header = Node.bad_header("Bad theory header"),
wenzelm@52849
   108
    val perspective: Node.Perspective_Command =
wenzelm@52849
   109
      Node.Perspective(false, Command.Perspective.empty, Overlays.empty),
wenzelm@46677
   110
    val commands: Linear_Set[Command] = Linear_Set.empty)
wenzelm@38151
   111
  {
wenzelm@46680
   112
    def clear: Node = new Node(header = header)
wenzelm@46680
   113
wenzelm@48707
   114
    def update_header(new_header: Node.Header): Node =
wenzelm@49678
   115
      new Node(new_header, perspective, commands)
wenzelm@46680
   116
wenzelm@52849
   117
    def update_perspective(new_perspective: Node.Perspective_Command): Node =
wenzelm@49678
   118
      new Node(header, new_perspective, commands)
wenzelm@46680
   119
wenzelm@52849
   120
    def same_perspective(other_perspective: Node.Perspective_Command): Boolean =
wenzelm@52849
   121
      perspective.required == other_perspective.required &&
wenzelm@52849
   122
      perspective.visible.same(other_perspective.visible) &&
wenzelm@52849
   123
      perspective.overlays == other_perspective.overlays
wenzelm@52808
   124
wenzelm@46680
   125
    def update_commands(new_commands: Linear_Set[Command]): Node =
wenzelm@49678
   126
      new Node(header, perspective, new_commands)
wenzelm@44443
   127
wenzelm@44443
   128
wenzelm@43697
   129
    /* commands */
wenzelm@43697
   130
wenzelm@38879
   131
    private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
wenzelm@38879
   132
    {
wenzelm@38879
   133
      val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
wenzelm@38879
   134
      var next_block = 0
wenzelm@38879
   135
      var last_stop = 0
wenzelm@38879
   136
      for ((command, start) <- Node.command_starts(commands.iterator)) {
wenzelm@38879
   137
        last_stop = start + command.length
wenzelm@38885
   138
        while (last_stop + 1 > next_block) {
wenzelm@38879
   139
          blocks += (command -> start)
wenzelm@46680
   140
          next_block += Node.block_size
wenzelm@38879
   141
        }
wenzelm@38879
   142
      }
wenzelm@38879
   143
      (blocks.toArray, Text.Range(0, last_stop))
wenzelm@38879
   144
    }
wenzelm@38151
   145
wenzelm@38879
   146
    def full_range: Text.Range = full_index._2
wenzelm@38151
   147
wenzelm@38569
   148
    def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@38879
   149
    {
wenzelm@38879
   150
      if (!commands.isEmpty && full_range.contains(i)) {
wenzelm@46680
   151
        val (cmd0, start0) = full_index._1(i / Node.block_size)
wenzelm@38879
   152
        Node.command_starts(commands.iterator(cmd0), start0) dropWhile {
wenzelm@38879
   153
          case (cmd, start) => start + cmd.length <= i }
wenzelm@38879
   154
      }
wenzelm@38879
   155
      else Iterator.empty
wenzelm@38879
   156
    }
wenzelm@38151
   157
wenzelm@38569
   158
    def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@38569
   159
      command_range(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
   160
wenzelm@38426
   161
    def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
wenzelm@38151
   162
    {
wenzelm@38151
   163
      val range = command_range(i)
wenzelm@38151
   164
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
   165
    }
wenzelm@38151
   166
wenzelm@38879
   167
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@46677
   168
      Node.command_starts(commands.iterator).find(_._1 == cmd).map(_._2)
wenzelm@38151
   169
  }
wenzelm@38151
   170
wenzelm@38151
   171
wenzelm@46723
   172
  /* development graph */
wenzelm@46723
   173
wenzelm@46723
   174
  object Nodes
wenzelm@46723
   175
  {
wenzelm@46723
   176
    val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
wenzelm@46723
   177
  }
wenzelm@46723
   178
wenzelm@46723
   179
  final class Nodes private(graph: Graph[Node.Name, Node])
wenzelm@46723
   180
  {
wenzelm@46723
   181
    def get_name(s: String): Option[Node.Name] =
wenzelm@46723
   182
      graph.keys.find(name => name.node == s)
wenzelm@46723
   183
wenzelm@46723
   184
    def apply(name: Node.Name): Node =
wenzelm@46723
   185
      graph.default_node(name, Node.empty).get_node(name)
wenzelm@46723
   186
wenzelm@46723
   187
    def + (entry: (Node.Name, Node)): Nodes =
wenzelm@46723
   188
    {
wenzelm@46723
   189
      val (name, node) = entry
wenzelm@48707
   190
      val imports = node.header.imports
wenzelm@46723
   191
      val graph1 =
wenzelm@46739
   192
        (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty))
wenzelm@46723
   193
      val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
wenzelm@46739
   194
      val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name))
wenzelm@46723
   195
      new Nodes(graph3.map_node(name, _ => node))
wenzelm@46723
   196
    }
wenzelm@46723
   197
wenzelm@46723
   198
    def entries: Iterator[(Node.Name, Node)] =
wenzelm@46723
   199
      graph.entries.map({ case (name, (node, _)) => (name, node) })
wenzelm@46723
   200
wenzelm@46942
   201
    def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names)
wenzelm@46723
   202
    def topological_order: List[Node.Name] = graph.topological_order
wenzelm@46723
   203
  }
wenzelm@46723
   204
wenzelm@46723
   205
wenzelm@38424
   206
wenzelm@38424
   207
  /** versioning **/
wenzelm@38424
   208
wenzelm@38424
   209
  /* particular document versions */
wenzelm@34485
   210
wenzelm@38417
   211
  object Version
wenzelm@38417
   212
  {
wenzelm@46681
   213
    val init: Version = new Version()
wenzelm@46681
   214
wenzelm@46941
   215
    def make(syntax: Outer_Syntax, nodes: Nodes): Version =
wenzelm@52530
   216
      new Version(Document_ID.make(), syntax, nodes)
wenzelm@38417
   217
  }
wenzelm@38417
   218
wenzelm@46712
   219
  final class Version private(
wenzelm@52530
   220
    val id: Document_ID.Version = Document_ID.none,
wenzelm@46941
   221
    val syntax: Outer_Syntax = Outer_Syntax.empty,
wenzelm@46723
   222
    val nodes: Nodes = Nodes.empty)
wenzelm@46941
   223
  {
wenzelm@52530
   224
    def is_init: Boolean = id == Document_ID.none
wenzelm@46941
   225
  }
immler@34660
   226
wenzelm@34859
   227
wenzelm@38424
   228
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   229
wenzelm@38227
   230
  object Change
wenzelm@38227
   231
  {
wenzelm@46678
   232
    val init: Change = new Change()
wenzelm@46678
   233
wenzelm@46678
   234
    def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
wenzelm@46678
   235
      new Change(Some(previous), edits, version)
wenzelm@38227
   236
  }
wenzelm@38227
   237
wenzelm@46712
   238
  final class Change private(
wenzelm@46677
   239
    val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
wenzelm@46677
   240
    val edits: List[Edit_Text] = Nil,
wenzelm@46677
   241
    val version: Future[Version] = Future.value(Version.init))
wenzelm@38227
   242
  {
wenzelm@44672
   243
    def is_finished: Boolean =
wenzelm@44672
   244
      (previous match { case None => true case Some(future) => future.is_finished }) &&
wenzelm@44672
   245
      version.is_finished
wenzelm@44672
   246
wenzelm@46678
   247
    def truncate: Change = new Change(None, Nil, version)
wenzelm@38227
   248
  }
wenzelm@38227
   249
wenzelm@38227
   250
wenzelm@38841
   251
  /* history navigation */
wenzelm@38841
   252
wenzelm@38841
   253
  object History
wenzelm@38841
   254
  {
wenzelm@46679
   255
    val init: History = new History()
wenzelm@38841
   256
  }
wenzelm@38841
   257
wenzelm@46712
   258
  final class History private(
wenzelm@46679
   259
    val undo_list: List[Change] = List(Change.init))  // non-empty list
wenzelm@38841
   260
  {
wenzelm@46679
   261
    def tip: Change = undo_list.head
wenzelm@46679
   262
    def + (change: Change): History = new History(change :: undo_list)
wenzelm@38841
   263
wenzelm@46679
   264
    def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
wenzelm@46679
   265
    {
wenzelm@46679
   266
      val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
wenzelm@46679
   267
      val (retained, dropped) = undo_list.splitAt(n max retain)
wenzelm@46679
   268
wenzelm@46679
   269
      retained.splitAt(retained.length - 1) match {
wenzelm@46679
   270
        case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
wenzelm@46679
   271
        case _ => None
wenzelm@46679
   272
      }
wenzelm@46679
   273
    }
wenzelm@38841
   274
  }
wenzelm@38841
   275
wenzelm@38841
   276
wenzelm@38841
   277
wenzelm@38841
   278
  /** global state -- document structure, execution process, editing history **/
wenzelm@38424
   279
wenzelm@38424
   280
  abstract class Snapshot
wenzelm@38424
   281
  {
wenzelm@44582
   282
    val state: State
wenzelm@38424
   283
    val version: Version
wenzelm@48923
   284
    val node_name: Node.Name
wenzelm@38424
   285
    val node: Node
wenzelm@38424
   286
    val is_outdated: Boolean
wenzelm@38427
   287
    def convert(i: Text.Offset): Text.Offset
wenzelm@39177
   288
    def convert(range: Text.Range): Text.Range
wenzelm@38427
   289
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   290
    def revert(range: Text.Range): Text.Range
wenzelm@51494
   291
    def eq_content(other: Snapshot): Boolean
wenzelm@52508
   292
    def cumulate_markup[A](
wenzelm@52508
   293
      range: Text.Range,
wenzelm@52508
   294
      info: A,
wenzelm@52508
   295
      elements: Option[Set[String]],
wenzelm@50500
   296
      result: Command.State => PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]]
wenzelm@52508
   297
    def select_markup[A](
wenzelm@52508
   298
      range: Text.Range,
wenzelm@52508
   299
      elements: Option[Set[String]],
wenzelm@50500
   300
      result: Command.State => PartialFunction[Text.Markup, A]): Stream[Text.Info[A]]
wenzelm@38424
   301
  }
wenzelm@38424
   302
wenzelm@52563
   303
  type Assign_Update =
wenzelm@52563
   304
    List[(Document_ID.Command, List[Document_ID.Exec])]  // update of exec state assignment
wenzelm@44476
   305
wenzelm@38370
   306
  object State
wenzelm@38370
   307
  {
wenzelm@38373
   308
    class Fail(state: State) extends Exception
wenzelm@38373
   309
wenzelm@44479
   310
    object Assignment
wenzelm@44479
   311
    {
wenzelm@46683
   312
      val init: Assignment = new Assignment()
wenzelm@44479
   313
    }
wenzelm@38370
   314
wenzelm@46712
   315
    final class Assignment private(
wenzelm@52530
   316
      val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty,
wenzelm@46677
   317
      val is_finished: Boolean = false)
wenzelm@38370
   318
    {
wenzelm@44477
   319
      def check_finished: Assignment = { require(is_finished); this }
wenzelm@47388
   320
      def unfinished: Assignment = new Assignment(command_execs, false)
wenzelm@44479
   321
wenzelm@52563
   322
      def assign(update: Assign_Update): Assignment =
wenzelm@38370
   323
      {
wenzelm@38976
   324
        require(!is_finished)
wenzelm@44479
   325
        val command_execs1 =
wenzelm@52563
   326
          (command_execs /: update) {
wenzelm@52563
   327
            case (res, (command_id, exec_ids)) =>
wenzelm@52563
   328
              if (exec_ids.isEmpty) res - command_id
wenzelm@52563
   329
              else res + (command_id -> exec_ids)
wenzelm@44479
   330
          }
wenzelm@47388
   331
        new Assignment(command_execs1, true)
wenzelm@38150
   332
      }
wenzelm@38370
   333
    }
wenzelm@46682
   334
wenzelm@46682
   335
    val init: State =
wenzelm@52563
   336
      State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2
wenzelm@38370
   337
  }
wenzelm@38370
   338
wenzelm@46712
   339
  final case class State private(
wenzelm@52530
   340
    val versions: Map[Document_ID.Version, Version] = Map.empty,
wenzelm@52530
   341
    val commands: Map[Document_ID.Command, Command.State] = Map.empty,  // static markup from define_command
wenzelm@52530
   342
    val execs: Map[Document_ID.Exec, Command.State] = Map.empty,  // dynamic markup from execution
wenzelm@52530
   343
    val assignments: Map[Document_ID.Version, State.Assignment] = Map.empty,
wenzelm@38841
   344
    val history: History = History.init)
wenzelm@38370
   345
  {
wenzelm@38373
   346
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   347
wenzelm@44479
   348
    def define_version(version: Version, assignment: State.Assignment): State =
wenzelm@38370
   349
    {
wenzelm@38417
   350
      val id = version.id
wenzelm@38417
   351
      copy(versions = versions + (id -> version),
wenzelm@44479
   352
        assignments = assignments + (id -> assignment.unfinished))
wenzelm@38370
   353
    }
wenzelm@38370
   354
wenzelm@38373
   355
    def define_command(command: Command): State =
wenzelm@38373
   356
    {
wenzelm@38373
   357
      val id = command.id
wenzelm@49414
   358
      copy(commands = commands + (id -> command.init_state))
wenzelm@38373
   359
    }
wenzelm@38373
   360
wenzelm@52530
   361
    def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   362
wenzelm@52531
   363
    def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] =
wenzelm@44582
   364
      commands.get(id) orElse execs.get(id) match {
wenzelm@44582
   365
        case None => None
wenzelm@44582
   366
        case Some(st) =>
wenzelm@44582
   367
          val command = st.command
wenzelm@46681
   368
          val node = version.nodes(command.node_name)
wenzelm@46681
   369
          Some((node, command))
wenzelm@44582
   370
      }
wenzelm@38373
   371
wenzelm@52530
   372
    def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail)
wenzelm@52564
   373
    def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail)
wenzelm@52564
   374
    def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail)
wenzelm@52563
   375
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail)
wenzelm@38370
   376
wenzelm@52531
   377
    def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) =
wenzelm@38370
   378
      execs.get(id) match {
wenzelm@44446
   379
        case Some(st) =>
wenzelm@49527
   380
          val new_st = st + (id, message)
wenzelm@44446
   381
          (new_st, copy(execs = execs + (id -> new_st)))
wenzelm@38370
   382
        case None =>
wenzelm@38370
   383
          commands.get(id) match {
wenzelm@38370
   384
            case Some(st) =>
wenzelm@49527
   385
              val new_st = st + (id, message)
wenzelm@38370
   386
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   387
            case None => fail
wenzelm@38370
   388
          }
wenzelm@38370
   389
      }
wenzelm@38370
   390
wenzelm@52563
   391
    def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) =
wenzelm@38370
   392
    {
wenzelm@38417
   393
      val version = the_version(id)
wenzelm@38370
   394
wenzelm@52566
   395
      def upd(exec_id: Document_ID.Exec, st: Command.State)
wenzelm@52566
   396
          : Option[(Document_ID.Exec, Command.State)] =
wenzelm@52566
   397
        if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st)
wenzelm@52566
   398
wenzelm@44543
   399
      val (changed_commands, new_execs) =
wenzelm@52563
   400
        ((Nil: List[Command], execs) /: update) {
wenzelm@52566
   401
          case ((commands1, execs1), (command_id, exec)) =>
wenzelm@52566
   402
            val st = the_static_state(command_id)
wenzelm@52566
   403
            val command = st.command
wenzelm@52527
   404
            val commands2 = command :: commands1
wenzelm@44543
   405
            val execs2 =
wenzelm@44543
   406
              exec match {
wenzelm@52527
   407
                case Nil => execs1
wenzelm@52527
   408
                case eval_id :: print_ids =>
wenzelm@52566
   409
                  execs1 ++ upd(eval_id, st) ++
wenzelm@52566
   410
                    (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up)
wenzelm@44543
   411
              }
wenzelm@44543
   412
            (commands2, execs2)
wenzelm@38370
   413
        }
wenzelm@52563
   414
      val new_assignment = the_assignment(version).assign(update)
wenzelm@43722
   415
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   416
wenzelm@44543
   417
      (changed_commands, new_state)
wenzelm@38370
   418
    }
wenzelm@38370
   419
wenzelm@38417
   420
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   421
      assignments.get(version.id) match {
wenzelm@38370
   422
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   423
        case None => false
wenzelm@38370
   424
      }
wenzelm@38370
   425
wenzelm@44436
   426
    def is_stable(change: Change): Boolean =
wenzelm@44436
   427
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   428
wenzelm@46944
   429
    def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
wenzelm@44672
   430
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
wenzelm@44436
   431
    def tip_stable: Boolean = is_stable(history.tip)
wenzelm@44475
   432
    def tip_version: Version = history.tip.version.get_finished
wenzelm@44436
   433
wenzelm@44446
   434
    def continue_history(
wenzelm@44446
   435
        previous: Future[Version],
wenzelm@40479
   436
        edits: List[Edit_Text],
wenzelm@43722
   437
        version: Future[Version]): (Change, State) =
wenzelm@38841
   438
    {
wenzelm@46678
   439
      val change = Change.make(previous, edits, version)
wenzelm@38841
   440
      (change, copy(history = history + change))
wenzelm@38841
   441
    }
wenzelm@38841
   442
wenzelm@44672
   443
    def prune_history(retain: Int = 0): (List[Version], State) =
wenzelm@44672
   444
    {
wenzelm@46679
   445
      history.prune(is_stable, retain) match {
wenzelm@46679
   446
        case Some((dropped, history1)) =>
wenzelm@44672
   447
          val dropped_versions = dropped.map(change => change.version.get_finished)
wenzelm@46679
   448
          val state1 = copy(history = history1)
wenzelm@44672
   449
          (dropped_versions, state1)
wenzelm@46679
   450
        case None => fail
wenzelm@44672
   451
      }
wenzelm@44672
   452
    }
wenzelm@44672
   453
wenzelm@52530
   454
    def removed_versions(removed: List[Document_ID.Version]): State =
wenzelm@44676
   455
    {
wenzelm@44676
   456
      val versions1 = versions -- removed
wenzelm@44676
   457
      val assignments1 = assignments -- removed
wenzelm@52530
   458
      var commands1 = Map.empty[Document_ID.Command, Command.State]
wenzelm@52530
   459
      var execs1 = Map.empty[Document_ID.Exec, Command.State]
wenzelm@44676
   460
      for {
wenzelm@44676
   461
        (version_id, version) <- versions1.iterator
wenzelm@46997
   462
        command_execs = assignments1(version_id).command_execs
wenzelm@46723
   463
        (_, node) <- version.nodes.entries
wenzelm@44676
   464
        command <- node.commands.iterator
wenzelm@44676
   465
      } {
wenzelm@52568
   466
        if (!commands1.isDefinedAt(command.id))
wenzelm@52568
   467
          commands.get(command.id).foreach(st => commands1 += (command.id -> st))
wenzelm@52568
   468
        for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
wenzelm@52527
   469
          if (!execs1.isDefinedAt(exec_id))
wenzelm@52527
   470
            execs.get(exec_id).foreach(st => execs1 += (exec_id -> st))
wenzelm@44676
   471
        }
wenzelm@44676
   472
      }
wenzelm@44676
   473
      copy(versions = versions1, commands = commands1, execs = execs1, assignments = assignments1)
wenzelm@44676
   474
    }
wenzelm@44676
   475
wenzelm@44613
   476
    def command_state(version: Version, command: Command): Command.State =
wenzelm@44613
   477
    {
wenzelm@44613
   478
      require(is_assigned(version))
wenzelm@44613
   479
      try {
wenzelm@52527
   480
        the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil)
wenzelm@52564
   481
          .map(the_dynamic_state(_)) match {
wenzelm@52527
   482
            case eval_state :: print_states => (eval_state /: print_states)(_ ++ _)
wenzelm@52527
   483
            case Nil => fail
wenzelm@52527
   484
          }
wenzelm@44613
   485
      }
wenzelm@47395
   486
      catch {
wenzelm@47395
   487
        case _: State.Fail =>
wenzelm@52564
   488
          try { the_static_state(command.id) }
wenzelm@49414
   489
          catch { case _: State.Fail => command.init_state }
wenzelm@47395
   490
      }
wenzelm@44613
   491
    }
wenzelm@44613
   492
wenzelm@49645
   493
    def markup_to_XML(version: Version, node: Node, filter: XML.Elem => Boolean): XML.Body =
wenzelm@49645
   494
      node.commands.toList.map(cmd => command_state(version, cmd).markup_to_XML(filter)).flatten
wenzelm@38841
   495
wenzelm@38841
   496
    // persistent user-view
wenzelm@49346
   497
    def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
wenzelm@49346
   498
      : Snapshot =
wenzelm@38370
   499
    {
wenzelm@44672
   500
      val stable = recent_stable
wenzelm@44436
   501
      val latest = history.tip
wenzelm@38841
   502
wenzelm@44156
   503
      // FIXME proper treatment of removed nodes
wenzelm@38841
   504
      val edits =
wenzelm@38841
   505
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@44156
   506
            (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
wenzelm@38841
   507
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   508
wenzelm@38841
   509
      new Snapshot
wenzelm@38841
   510
      {
wenzelm@44582
   511
        val state = State.this
wenzelm@39698
   512
        val version = stable.version.get_finished
wenzelm@48923
   513
        val node_name = name
wenzelm@38841
   514
        val node = version.nodes(name)
wenzelm@38841
   515
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38841
   516
wenzelm@38841
   517
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   518
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@43425
   519
        def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
wenzelm@43425
   520
        def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
wenzelm@39177
   521
wenzelm@51494
   522
        def eq_content(other: Snapshot): Boolean =
wenzelm@49346
   523
          !is_outdated && !other.is_outdated &&
wenzelm@49346
   524
            node.commands.size == other.node.commands.size &&
wenzelm@49346
   525
            ((node.commands.iterator zip other.node.commands.iterator) forall {
wenzelm@49346
   526
              case (cmd1, cmd2) =>
wenzelm@51494
   527
                state.command_state(version, cmd1) eq_content
wenzelm@51494
   528
                  other.state.command_state(other.version, cmd2)
wenzelm@49346
   529
            })
wenzelm@49346
   530
wenzelm@46178
   531
        def cumulate_markup[A](range: Text.Range, info: A, elements: Option[Set[String]],
wenzelm@50500
   532
          result: Command.State => PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
wenzelm@45459
   533
        {
wenzelm@45467
   534
          val former_range = revert(range)
wenzelm@45459
   535
          for {
wenzelm@45459
   536
            (command, command_start) <- node.command_range(former_range).toStream
wenzelm@50500
   537
            st = state.command_state(version, command)
wenzelm@50500
   538
            res = result(st)
wenzelm@50500
   539
            Text.Info(r0, a) <- st.markup.
wenzelm@46178
   540
              cumulate[A]((former_range - command_start).restrict(command.range), info, elements,
wenzelm@46178
   541
                {
wenzelm@46178
   542
                  case (a, Text.Info(r0, b))
wenzelm@50500
   543
                  if res.isDefinedAt((a, Text.Info(convert(r0 + command_start), b))) =>
wenzelm@50500
   544
                    res((a, Text.Info(convert(r0 + command_start), b)))
wenzelm@46178
   545
                })
wenzelm@45459
   546
          } yield Text.Info(convert(r0 + command_start), a)
wenzelm@45459
   547
        }
wenzelm@45459
   548
wenzelm@46178
   549
        def select_markup[A](range: Text.Range, elements: Option[Set[String]],
wenzelm@50500
   550
          result: Command.State => PartialFunction[Text.Markup, A]): Stream[Text.Info[A]] =
wenzelm@38845
   551
        {
wenzelm@50500
   552
          def result1(st: Command.State) =
wenzelm@50500
   553
          {
wenzelm@50500
   554
            val res = result(st)
wenzelm@45468
   555
            new PartialFunction[(Option[A], Text.Markup), Option[A]] {
wenzelm@50500
   556
              def isDefinedAt(arg: (Option[A], Text.Markup)): Boolean = res.isDefinedAt(arg._2)
wenzelm@50500
   557
              def apply(arg: (Option[A], Text.Markup)): Option[A] = Some(res(arg._2))
wenzelm@45468
   558
            }
wenzelm@50500
   559
          }
wenzelm@46198
   560
          for (Text.Info(r, Some(x)) <- cumulate_markup(range, None, elements, result1))
wenzelm@46198
   561
            yield Text.Info(r, x)
wenzelm@38845
   562
        }
wenzelm@38841
   563
      }
wenzelm@38150
   564
    }
wenzelm@34485
   565
  }
wenzelm@34840
   566
}
wenzelm@34840
   567