src/Pure/PIDE/document.scala
author wenzelm
Mon Feb 27 23:35:11 2012 +0100 (2012-02-27)
changeset 46723 54ea872b60ea
parent 46712 8650d9a95736
child 46737 09ab89658a5d
permissions -rw-r--r--
more explicit development graph;
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@38227
    16
  /* formal identifiers */
wenzelm@38150
    17
wenzelm@38363
    18
  type ID = Long
wenzelm@43780
    19
  val ID = Properties.Value.Long
wenzelm@38414
    20
wenzelm@38373
    21
  type Version_ID = ID
wenzelm@38363
    22
  type Command_ID = ID
wenzelm@38373
    23
  type Exec_ID = ID
wenzelm@38150
    24
wenzelm@43662
    25
  val no_id: ID = 0
wenzelm@45243
    26
  val new_id = Counter()
wenzelm@38355
    27
wenzelm@38150
    28
wenzelm@34818
    29
wenzelm@38424
    30
  /** document structure **/
wenzelm@38424
    31
wenzelm@46723
    32
  /* individual nodes */
wenzelm@38424
    33
wenzelm@44615
    34
  type Edit[A, B] = (Node.Name, Node.Edit[A, B])
wenzelm@44384
    35
  type Edit_Text = Edit[Text.Edit, Text.Perspective]
wenzelm@44384
    36
  type Edit_Command = Edit[(Option[Command], Option[Command]), Command.Perspective]
wenzelm@38151
    37
wenzelm@44182
    38
  type Node_Header = Exn.Result[Thy_Header]
wenzelm@44182
    39
wenzelm@38151
    40
  object Node
wenzelm@38151
    41
  {
wenzelm@44957
    42
    object Name
wenzelm@44957
    43
    {
wenzelm@44957
    44
      val empty = Name("", "", "")
wenzelm@44959
    45
      def apply(path: Path): Name =
wenzelm@44959
    46
      {
wenzelm@44959
    47
        val node = path.implode
wenzelm@44959
    48
        val dir = path.dir.implode
wenzelm@44959
    49
        val theory = Thy_Header.thy_name(node) getOrElse error("Bad theory file name: " + path)
wenzelm@44959
    50
        Name(node, dir, theory)
wenzelm@44959
    51
      }
wenzelm@46723
    52
wenzelm@46723
    53
      object Ordering extends scala.math.Ordering[Name]
wenzelm@46723
    54
      {
wenzelm@46723
    55
        def compare(name1: Name, name2: Name): Int = name1.node compare name2.node
wenzelm@46723
    56
      }
wenzelm@44957
    57
    }
wenzelm@44616
    58
    sealed case class Name(node: String, dir: String, theory: String)
wenzelm@44615
    59
    {
wenzelm@44615
    60
      override def hashCode: Int = node.hashCode
wenzelm@44615
    61
      override def equals(that: Any): Boolean =
wenzelm@44615
    62
        that match {
wenzelm@44615
    63
          case other: Name => node == other.node
wenzelm@44615
    64
          case _ => false
wenzelm@44615
    65
        }
wenzelm@44642
    66
      override def toString: String = theory
wenzelm@44615
    67
    }
wenzelm@44615
    68
wenzelm@44384
    69
    sealed abstract class Edit[A, B]
wenzelm@44156
    70
    {
wenzelm@44383
    71
      def foreach(f: A => Unit)
wenzelm@44383
    72
      {
wenzelm@44156
    73
        this match {
wenzelm@44383
    74
          case Edits(es) => es.foreach(f)
wenzelm@44383
    75
          case _ =>
wenzelm@44156
    76
        }
wenzelm@44383
    77
      }
wenzelm@44156
    78
    }
wenzelm@44384
    79
    case class Clear[A, B]() extends Edit[A, B]
wenzelm@44384
    80
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
wenzelm@44384
    81
    case class Header[A, B](header: Node_Header) extends Edit[A, B]
wenzelm@44384
    82
    case class Perspective[A, B](perspective: B) extends Edit[A, B]
wenzelm@44156
    83
wenzelm@44442
    84
    def norm_header(f: String => String, g: String => String, header: Node_Header): Node_Header =
wenzelm@44182
    85
      header match {
wenzelm@44442
    86
        case Exn.Res(h) => Exn.capture { h.norm_deps(f, g) }
wenzelm@44442
    87
        case exn => exn
wenzelm@44182
    88
      }
wenzelm@43697
    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@46677
   107
    val header: Node_Header = Exn.Exn(ERROR("Bad theory header")),
wenzelm@46677
   108
    val perspective: Command.Perspective = Command.Perspective.empty,
wenzelm@46677
   109
    val blobs: Map[String, Blob] = Map.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@46680
   114
    def update_header(new_header: Node_Header): Node =
wenzelm@46680
   115
      new Node(new_header, perspective, blobs, commands)
wenzelm@46680
   116
wenzelm@46680
   117
    def update_perspective(new_perspective: Command.Perspective): Node =
wenzelm@46680
   118
      new Node(header, new_perspective, blobs, commands)
wenzelm@46680
   119
wenzelm@46680
   120
    def update_blobs(new_blobs: Map[String, Blob]): Node =
wenzelm@46680
   121
      new Node(header, perspective, new_blobs, commands)
wenzelm@46680
   122
wenzelm@46680
   123
    def update_commands(new_commands: Linear_Set[Command]): Node =
wenzelm@46680
   124
      new Node(header, perspective, blobs, new_commands)
wenzelm@44443
   125
wenzelm@44443
   126
wenzelm@43697
   127
    /* commands */
wenzelm@43697
   128
wenzelm@38879
   129
    private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
wenzelm@38879
   130
    {
wenzelm@38879
   131
      val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
wenzelm@38879
   132
      var next_block = 0
wenzelm@38879
   133
      var last_stop = 0
wenzelm@38879
   134
      for ((command, start) <- Node.command_starts(commands.iterator)) {
wenzelm@38879
   135
        last_stop = start + command.length
wenzelm@38885
   136
        while (last_stop + 1 > next_block) {
wenzelm@38879
   137
          blocks += (command -> start)
wenzelm@46680
   138
          next_block += Node.block_size
wenzelm@38879
   139
        }
wenzelm@38879
   140
      }
wenzelm@38879
   141
      (blocks.toArray, Text.Range(0, last_stop))
wenzelm@38879
   142
    }
wenzelm@38151
   143
wenzelm@38879
   144
    def full_range: Text.Range = full_index._2
wenzelm@38151
   145
wenzelm@38569
   146
    def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@38879
   147
    {
wenzelm@38879
   148
      if (!commands.isEmpty && full_range.contains(i)) {
wenzelm@46680
   149
        val (cmd0, start0) = full_index._1(i / Node.block_size)
wenzelm@38879
   150
        Node.command_starts(commands.iterator(cmd0), start0) dropWhile {
wenzelm@38879
   151
          case (cmd, start) => start + cmd.length <= i }
wenzelm@38879
   152
      }
wenzelm@38879
   153
      else Iterator.empty
wenzelm@38879
   154
    }
wenzelm@38151
   155
wenzelm@38569
   156
    def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@38569
   157
      command_range(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
   158
wenzelm@38426
   159
    def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
wenzelm@38151
   160
    {
wenzelm@38151
   161
      val range = command_range(i)
wenzelm@38151
   162
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
   163
    }
wenzelm@38151
   164
wenzelm@38426
   165
    def proper_command_at(i: Text.Offset): Option[Command] =
wenzelm@38151
   166
      command_at(i) match {
wenzelm@38151
   167
        case Some((command, _)) =>
wenzelm@38151
   168
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
   169
        case None => None
wenzelm@38151
   170
      }
wenzelm@38879
   171
wenzelm@38879
   172
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@46677
   173
      Node.command_starts(commands.iterator).find(_._1 == cmd).map(_._2)
wenzelm@38151
   174
  }
wenzelm@38151
   175
wenzelm@38151
   176
wenzelm@46723
   177
  /* development graph */
wenzelm@46723
   178
wenzelm@46723
   179
  object Nodes
wenzelm@46723
   180
  {
wenzelm@46723
   181
    val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
wenzelm@46723
   182
  }
wenzelm@46723
   183
wenzelm@46723
   184
  final class Nodes private(graph: Graph[Node.Name, Node])
wenzelm@46723
   185
  {
wenzelm@46723
   186
    def get_name(s: String): Option[Node.Name] =
wenzelm@46723
   187
      graph.keys.find(name => name.node == s)
wenzelm@46723
   188
wenzelm@46723
   189
    def apply(name: Node.Name): Node =
wenzelm@46723
   190
      graph.default_node(name, Node.empty).get_node(name)
wenzelm@46723
   191
wenzelm@46723
   192
    def + (entry: (Node.Name, Node)): Nodes =
wenzelm@46723
   193
    {
wenzelm@46723
   194
      val (name, node) = entry
wenzelm@46723
   195
      val parents =
wenzelm@46723
   196
        node.header match {
wenzelm@46723
   197
          case Exn.Res(header) =>
wenzelm@46723
   198
            // FIXME official names of yet unknown nodes!?
wenzelm@46723
   199
            for (imp <- header.imports; imp_name <- get_name(imp)) yield imp_name
wenzelm@46723
   200
          case _ => Nil
wenzelm@46723
   201
        }
wenzelm@46723
   202
      val graph1 =
wenzelm@46723
   203
        (graph.default_node(name, Node.empty) /: parents)((g, p) => g.default_node(p, Node.empty))
wenzelm@46723
   204
      val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
wenzelm@46723
   205
      val graph3 = (graph2 /: parents)((g, dep) => g.add_edge(dep, name))
wenzelm@46723
   206
      new Nodes(graph3.map_node(name, _ => node))
wenzelm@46723
   207
    }
wenzelm@46723
   208
wenzelm@46723
   209
    def entries: Iterator[(Node.Name, Node)] =
wenzelm@46723
   210
      graph.entries.map({ case (name, (node, _)) => (name, node) })
wenzelm@46723
   211
wenzelm@46723
   212
    def topological_order: List[Node.Name] = graph.topological_order
wenzelm@46723
   213
  }
wenzelm@46723
   214
wenzelm@46723
   215
wenzelm@38424
   216
wenzelm@38424
   217
  /** versioning **/
wenzelm@38424
   218
wenzelm@38424
   219
  /* particular document versions */
wenzelm@34485
   220
wenzelm@38417
   221
  object Version
wenzelm@38417
   222
  {
wenzelm@46681
   223
    val init: Version = new Version()
wenzelm@46681
   224
wenzelm@46681
   225
    def make(nodes: Nodes): Version = new Version(new_id(), nodes)
wenzelm@38417
   226
  }
wenzelm@38417
   227
wenzelm@46712
   228
  final class Version private(
wenzelm@46677
   229
    val id: Version_ID = no_id,
wenzelm@46723
   230
    val nodes: Nodes = Nodes.empty)
immler@34660
   231
wenzelm@34859
   232
wenzelm@38424
   233
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   234
wenzelm@38227
   235
  object Change
wenzelm@38227
   236
  {
wenzelm@46678
   237
    val init: Change = new Change()
wenzelm@46678
   238
wenzelm@46678
   239
    def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
wenzelm@46678
   240
      new Change(Some(previous), edits, version)
wenzelm@38227
   241
  }
wenzelm@38227
   242
wenzelm@46712
   243
  final class Change private(
wenzelm@46677
   244
    val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
wenzelm@46677
   245
    val edits: List[Edit_Text] = Nil,
wenzelm@46677
   246
    val version: Future[Version] = Future.value(Version.init))
wenzelm@38227
   247
  {
wenzelm@44672
   248
    def is_finished: Boolean =
wenzelm@44672
   249
      (previous match { case None => true case Some(future) => future.is_finished }) &&
wenzelm@44672
   250
      version.is_finished
wenzelm@44672
   251
wenzelm@46678
   252
    def truncate: Change = new Change(None, Nil, version)
wenzelm@38227
   253
  }
wenzelm@38227
   254
wenzelm@38227
   255
wenzelm@38841
   256
  /* history navigation */
wenzelm@38841
   257
wenzelm@38841
   258
  object History
wenzelm@38841
   259
  {
wenzelm@46679
   260
    val init: History = new History()
wenzelm@38841
   261
  }
wenzelm@38841
   262
wenzelm@46712
   263
  final class History private(
wenzelm@46679
   264
    val undo_list: List[Change] = List(Change.init))  // non-empty list
wenzelm@38841
   265
  {
wenzelm@46679
   266
    def tip: Change = undo_list.head
wenzelm@46679
   267
    def + (change: Change): History = new History(change :: undo_list)
wenzelm@38841
   268
wenzelm@46679
   269
    def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
wenzelm@46679
   270
    {
wenzelm@46679
   271
      val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
wenzelm@46679
   272
      val (retained, dropped) = undo_list.splitAt(n max retain)
wenzelm@46679
   273
wenzelm@46679
   274
      retained.splitAt(retained.length - 1) match {
wenzelm@46679
   275
        case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
wenzelm@46679
   276
        case _ => None
wenzelm@46679
   277
      }
wenzelm@46679
   278
    }
wenzelm@38841
   279
  }
wenzelm@38841
   280
wenzelm@38841
   281
wenzelm@38841
   282
wenzelm@38841
   283
  /** global state -- document structure, execution process, editing history **/
wenzelm@38424
   284
wenzelm@38424
   285
  abstract class Snapshot
wenzelm@38424
   286
  {
wenzelm@44582
   287
    val state: State
wenzelm@38424
   288
    val version: Version
wenzelm@38424
   289
    val node: Node
wenzelm@38424
   290
    val is_outdated: Boolean
wenzelm@38427
   291
    def convert(i: Text.Offset): Text.Offset
wenzelm@39177
   292
    def convert(range: Text.Range): Text.Range
wenzelm@38427
   293
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   294
    def revert(range: Text.Range): Text.Range
wenzelm@46178
   295
    def cumulate_markup[A](range: Text.Range, info: A, elements: Option[Set[String]],
wenzelm@46178
   296
      result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]]
wenzelm@46178
   297
    def select_markup[A](range: Text.Range, elements: Option[Set[String]],
wenzelm@46198
   298
      result: PartialFunction[Text.Markup, A]): Stream[Text.Info[A]]
wenzelm@38424
   299
  }
wenzelm@38424
   300
wenzelm@44476
   301
  type Assign =
wenzelm@44479
   302
   (List[(Document.Command_ID, Option[Document.Exec_ID])],  // exec state assignment
wenzelm@44476
   303
    List[(String, Option[Document.Command_ID])])  // last exec
wenzelm@44476
   304
wenzelm@38370
   305
  object State
wenzelm@38370
   306
  {
wenzelm@38373
   307
    class Fail(state: State) extends Exception
wenzelm@38373
   308
wenzelm@44479
   309
    object Assignment
wenzelm@44479
   310
    {
wenzelm@46683
   311
      val init: Assignment = new Assignment()
wenzelm@44479
   312
    }
wenzelm@38370
   313
wenzelm@46712
   314
    final class Assignment private(
wenzelm@46677
   315
      val command_execs: Map[Command_ID, Exec_ID] = Map.empty,
wenzelm@46677
   316
      val last_execs: Map[String, Option[Command_ID]] = 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@46683
   320
      def unfinished: Assignment = new Assignment(command_execs, last_execs, false)
wenzelm@44479
   321
wenzelm@44479
   322
      def assign(add_command_execs: List[(Command_ID, Option[Exec_ID])],
wenzelm@44477
   323
        add_last_execs: List[(String, Option[Command_ID])]): Assignment =
wenzelm@38370
   324
      {
wenzelm@38976
   325
        require(!is_finished)
wenzelm@44479
   326
        val command_execs1 =
wenzelm@44479
   327
          (command_execs /: add_command_execs) {
wenzelm@44479
   328
            case (res, (command_id, None)) => res - command_id
wenzelm@44479
   329
            case (res, (command_id, Some(exec_id))) => res + (command_id -> exec_id)
wenzelm@44479
   330
          }
wenzelm@46683
   331
        new Assignment(command_execs1, last_execs ++ add_last_execs, true)
wenzelm@38150
   332
      }
wenzelm@38370
   333
    }
wenzelm@46682
   334
wenzelm@46682
   335
    val init: State =
wenzelm@46682
   336
      State().define_version(Version.init, Assignment.init).assign(Version.init.id)._2
wenzelm@38370
   337
  }
wenzelm@38370
   338
wenzelm@46712
   339
  final case class State private(
wenzelm@46677
   340
    val versions: Map[Version_ID, Version] = Map.empty,
wenzelm@46677
   341
    val commands: Map[Command_ID, Command.State] = Map.empty,  // static markup from define_command
wenzelm@46677
   342
    val execs: Map[Exec_ID, Command.State] = Map.empty,  // dynamic markup from execution
wenzelm@46677
   343
    val assignments: Map[Version_ID, 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@38373
   358
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   359
    }
wenzelm@38373
   360
wenzelm@44582
   361
    def defined_command(id: Command_ID): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   362
wenzelm@44607
   363
    def find_command(version: Version, id: ID): 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@38417
   372
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@44479
   373
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)  // FIXME rename
wenzelm@44446
   374
    def the_exec(id: Exec_ID): Command.State = execs.getOrElse(id, fail)
wenzelm@43722
   375
    def the_assignment(version: Version): State.Assignment =
wenzelm@43722
   376
      assignments.getOrElse(version.id, fail)
wenzelm@38370
   377
wenzelm@38872
   378
    def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
wenzelm@38370
   379
      execs.get(id) match {
wenzelm@44446
   380
        case Some(st) =>
wenzelm@46152
   381
          val new_st = st + message
wenzelm@44446
   382
          (new_st, copy(execs = execs + (id -> new_st)))
wenzelm@38370
   383
        case None =>
wenzelm@38370
   384
          commands.get(id) match {
wenzelm@38370
   385
            case Some(st) =>
wenzelm@46152
   386
              val new_st = st + message
wenzelm@38370
   387
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   388
            case None => fail
wenzelm@38370
   389
          }
wenzelm@38370
   390
      }
wenzelm@38370
   391
wenzelm@46682
   392
    def assign(id: Version_ID, arg: Assign = (Nil, Nil)): (List[Command], State) =
wenzelm@38370
   393
    {
wenzelm@38417
   394
      val version = the_version(id)
wenzelm@44477
   395
      val (command_execs, last_execs) = arg
wenzelm@38370
   396
wenzelm@44543
   397
      val (changed_commands, new_execs) =
wenzelm@44479
   398
        ((Nil: List[Command], execs) /: command_execs) {
wenzelm@44543
   399
          case ((commands1, execs1), (cmd_id, exec)) =>
wenzelm@44479
   400
            val st = the_command(cmd_id)
wenzelm@44543
   401
            val commands2 = st.command :: commands1
wenzelm@44543
   402
            val execs2 =
wenzelm@44543
   403
              exec match {
wenzelm@44543
   404
                case None => execs1
wenzelm@44543
   405
                case Some(exec_id) => execs1 + (exec_id -> st)
wenzelm@44543
   406
              }
wenzelm@44543
   407
            (commands2, execs2)
wenzelm@38370
   408
        }
wenzelm@44477
   409
      val new_assignment = the_assignment(version).assign(command_execs, last_execs)
wenzelm@43722
   410
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   411
wenzelm@44543
   412
      (changed_commands, new_state)
wenzelm@38370
   413
    }
wenzelm@38370
   414
wenzelm@38417
   415
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   416
      assignments.get(version.id) match {
wenzelm@38370
   417
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   418
        case None => false
wenzelm@38370
   419
      }
wenzelm@38370
   420
wenzelm@44436
   421
    def is_stable(change: Change): Boolean =
wenzelm@44436
   422
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   423
wenzelm@44672
   424
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
wenzelm@44436
   425
    def tip_stable: Boolean = is_stable(history.tip)
wenzelm@44475
   426
    def tip_version: Version = history.tip.version.get_finished
wenzelm@44436
   427
wenzelm@44615
   428
    def last_exec_offset(name: Node.Name): Text.Offset =
wenzelm@44484
   429
    {
wenzelm@44484
   430
      val version = tip_version
wenzelm@44615
   431
      the_assignment(version).last_execs.get(name.node) match {
wenzelm@44484
   432
        case Some(Some(id)) =>
wenzelm@44484
   433
          val node = version.nodes(name)
wenzelm@44484
   434
          val cmd = the_command(id).command
wenzelm@44484
   435
          node.command_start(cmd) match {
wenzelm@44484
   436
            case None => 0
wenzelm@44484
   437
            case Some(start) => start + cmd.length
wenzelm@44484
   438
          }
wenzelm@44484
   439
        case _ => 0
wenzelm@44484
   440
      }
wenzelm@44484
   441
    }
wenzelm@44484
   442
wenzelm@44446
   443
    def continue_history(
wenzelm@44446
   444
        previous: Future[Version],
wenzelm@40479
   445
        edits: List[Edit_Text],
wenzelm@43722
   446
        version: Future[Version]): (Change, State) =
wenzelm@38841
   447
    {
wenzelm@46678
   448
      val change = Change.make(previous, edits, version)
wenzelm@38841
   449
      (change, copy(history = history + change))
wenzelm@38841
   450
    }
wenzelm@38841
   451
wenzelm@44672
   452
    def prune_history(retain: Int = 0): (List[Version], State) =
wenzelm@44672
   453
    {
wenzelm@46679
   454
      history.prune(is_stable, retain) match {
wenzelm@46679
   455
        case Some((dropped, history1)) =>
wenzelm@44672
   456
          val dropped_versions = dropped.map(change => change.version.get_finished)
wenzelm@46679
   457
          val state1 = copy(history = history1)
wenzelm@44672
   458
          (dropped_versions, state1)
wenzelm@46679
   459
        case None => fail
wenzelm@44672
   460
      }
wenzelm@44672
   461
    }
wenzelm@44672
   462
wenzelm@44676
   463
    def removed_versions(removed: List[Version_ID]): State =
wenzelm@44676
   464
    {
wenzelm@44676
   465
      val versions1 = versions -- removed
wenzelm@44676
   466
      val assignments1 = assignments -- removed
wenzelm@44676
   467
      var commands1 = Map.empty[Command_ID, Command.State]
wenzelm@44676
   468
      var execs1 = Map.empty[Exec_ID, Command.State]
wenzelm@44676
   469
      for {
wenzelm@44676
   470
        (version_id, version) <- versions1.iterator
wenzelm@44676
   471
        val command_execs = assignments1(version_id).command_execs
wenzelm@46723
   472
        (_, node) <- version.nodes.entries
wenzelm@44676
   473
        command <- node.commands.iterator
wenzelm@44676
   474
      } {
wenzelm@44676
   475
        val id = command.id
wenzelm@44676
   476
        if (!commands1.isDefinedAt(id) && commands.isDefinedAt(id))
wenzelm@44676
   477
          commands1 += (id -> commands(id))
wenzelm@44676
   478
        if (command_execs.isDefinedAt(id)) {
wenzelm@44676
   479
          val exec_id = command_execs(id)
wenzelm@44676
   480
          if (!execs1.isDefinedAt(exec_id) && execs.isDefinedAt(exec_id))
wenzelm@44676
   481
            execs1 += (exec_id -> execs(exec_id))
wenzelm@44676
   482
        }
wenzelm@44676
   483
      }
wenzelm@44676
   484
      copy(versions = versions1, commands = commands1, execs = execs1, assignments = assignments1)
wenzelm@44676
   485
    }
wenzelm@44676
   486
wenzelm@44613
   487
    def command_state(version: Version, command: Command): Command.State =
wenzelm@44613
   488
    {
wenzelm@44613
   489
      require(is_assigned(version))
wenzelm@44613
   490
      try {
wenzelm@44613
   491
        the_exec(the_assignment(version).check_finished.command_execs
wenzelm@44613
   492
          .getOrElse(command.id, fail))
wenzelm@44613
   493
      }
wenzelm@44613
   494
      catch { case _: State.Fail => command.empty_state }
wenzelm@44613
   495
    }
wenzelm@44613
   496
wenzelm@38841
   497
wenzelm@38841
   498
    // persistent user-view
wenzelm@44615
   499
    def snapshot(name: Node.Name, pending_edits: List[Text.Edit]): Snapshot =
wenzelm@38370
   500
    {
wenzelm@44672
   501
      val stable = recent_stable
wenzelm@44436
   502
      val latest = history.tip
wenzelm@38841
   503
wenzelm@44156
   504
      // FIXME proper treatment of removed nodes
wenzelm@38841
   505
      val edits =
wenzelm@38841
   506
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@44156
   507
            (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
wenzelm@38841
   508
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   509
wenzelm@38841
   510
      new Snapshot
wenzelm@38841
   511
      {
wenzelm@44582
   512
        val state = State.this
wenzelm@39698
   513
        val version = stable.version.get_finished
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@46178
   522
        def cumulate_markup[A](range: Text.Range, info: A, elements: Option[Set[String]],
wenzelm@46178
   523
          result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
wenzelm@45459
   524
        {
wenzelm@45467
   525
          val former_range = revert(range)
wenzelm@45459
   526
          for {
wenzelm@45459
   527
            (command, command_start) <- node.command_range(former_range).toStream
wenzelm@46208
   528
            Text.Info(r0, a) <- state.command_state(version, command).markup.
wenzelm@46178
   529
              cumulate[A]((former_range - command_start).restrict(command.range), info, elements,
wenzelm@46178
   530
                {
wenzelm@46178
   531
                  case (a, Text.Info(r0, b))
wenzelm@46178
   532
                  if result.isDefinedAt((a, Text.Info(convert(r0 + command_start), b))) =>
wenzelm@46178
   533
                    result((a, Text.Info(convert(r0 + command_start), b)))
wenzelm@46178
   534
                })
wenzelm@45459
   535
          } yield Text.Info(convert(r0 + command_start), a)
wenzelm@45459
   536
        }
wenzelm@45459
   537
wenzelm@46178
   538
        def select_markup[A](range: Text.Range, elements: Option[Set[String]],
wenzelm@46198
   539
          result: PartialFunction[Text.Markup, A]): Stream[Text.Info[A]] =
wenzelm@38845
   540
        {
wenzelm@45468
   541
          val result1 =
wenzelm@45468
   542
            new PartialFunction[(Option[A], Text.Markup), Option[A]] {
wenzelm@45468
   543
              def isDefinedAt(arg: (Option[A], Text.Markup)): Boolean = result.isDefinedAt(arg._2)
wenzelm@45468
   544
              def apply(arg: (Option[A], Text.Markup)): Option[A] = Some(result(arg._2))
wenzelm@45468
   545
            }
wenzelm@46198
   546
          for (Text.Info(r, Some(x)) <- cumulate_markup(range, None, elements, result1))
wenzelm@46198
   547
            yield Text.Info(r, x)
wenzelm@38845
   548
        }
wenzelm@38841
   549
      }
wenzelm@38150
   550
    }
wenzelm@34485
   551
  }
wenzelm@34840
   552
}
wenzelm@34840
   553