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