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