src/Pure/PIDE/document.scala
author wenzelm
Sat Sep 17 21:28:52 2011 +0200 (2011-09-17 ago)
changeset 44957 098dd95349e7
parent 44676 7de87f1ae965
child 44959 9476c856c4b9
permissions -rw-r--r--
more elaborate Node_Renderer, which paints node_name.theory only;
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@43662
    26
  val new_id = new Counter
wenzelm@38355
    27
wenzelm@38150
    28
wenzelm@34818
    29
wenzelm@38424
    30
  /** document structure **/
wenzelm@38424
    31
wenzelm@38424
    32
  /* named nodes -- development graph */
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@44957
    45
    }
wenzelm@44616
    46
    sealed case class Name(node: String, dir: String, theory: String)
wenzelm@44615
    47
    {
wenzelm@44615
    48
      override def hashCode: Int = node.hashCode
wenzelm@44615
    49
      override def equals(that: Any): Boolean =
wenzelm@44615
    50
        that match {
wenzelm@44615
    51
          case other: Name => node == other.node
wenzelm@44615
    52
          case _ => false
wenzelm@44615
    53
        }
wenzelm@44642
    54
      override def toString: String = theory
wenzelm@44615
    55
    }
wenzelm@44615
    56
wenzelm@44384
    57
    sealed abstract class Edit[A, B]
wenzelm@44156
    58
    {
wenzelm@44383
    59
      def foreach(f: A => Unit)
wenzelm@44383
    60
      {
wenzelm@44156
    61
        this match {
wenzelm@44383
    62
          case Edits(es) => es.foreach(f)
wenzelm@44383
    63
          case _ =>
wenzelm@44156
    64
        }
wenzelm@44383
    65
      }
wenzelm@44156
    66
    }
wenzelm@44384
    67
    case class Clear[A, B]() extends Edit[A, B]
wenzelm@44384
    68
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
wenzelm@44384
    69
    case class Header[A, B](header: Node_Header) extends Edit[A, B]
wenzelm@44384
    70
    case class Perspective[A, B](perspective: B) extends Edit[A, B]
wenzelm@44156
    71
wenzelm@44442
    72
    def norm_header(f: String => String, g: String => String, header: Node_Header): Node_Header =
wenzelm@44182
    73
      header match {
wenzelm@44442
    74
        case Exn.Res(h) => Exn.capture { h.norm_deps(f, g) }
wenzelm@44442
    75
        case exn => exn
wenzelm@44182
    76
      }
wenzelm@43697
    77
wenzelm@44474
    78
    val empty: Node =
wenzelm@44474
    79
      Node(Exn.Exn(ERROR("Bad theory header")), Command.Perspective.empty, Map(), Linear_Set())
wenzelm@38227
    80
wenzelm@38426
    81
    def command_starts(commands: Iterator[Command], offset: Text.Offset = 0)
wenzelm@38426
    82
      : Iterator[(Command, Text.Offset)] =
wenzelm@38227
    83
    {
wenzelm@38227
    84
      var i = offset
wenzelm@38227
    85
      for (command <- commands) yield {
wenzelm@38227
    86
        val start = i
wenzelm@38227
    87
        i += command.length
wenzelm@38227
    88
        (command, start)
wenzelm@38227
    89
      }
wenzelm@38227
    90
    }
wenzelm@38151
    91
  }
wenzelm@38151
    92
wenzelm@38880
    93
  private val block_size = 1024
wenzelm@38879
    94
wenzelm@43715
    95
  sealed case class Node(
wenzelm@44182
    96
    val header: Node_Header,
wenzelm@44385
    97
    val perspective: Command.Perspective,
wenzelm@43715
    98
    val blobs: Map[String, Blob],
wenzelm@43715
    99
    val commands: Linear_Set[Command])
wenzelm@38151
   100
  {
wenzelm@44443
   101
    def clear: Node = Node.empty.copy(header = header)
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@38879
   115
          next_block += 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@38879
   126
        val (cmd0, start0) = full_index._1(i / 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@38426
   142
    def proper_command_at(i: Text.Offset): Option[Command] =
wenzelm@38151
   143
      command_at(i) match {
wenzelm@38151
   144
        case Some((command, _)) =>
wenzelm@38151
   145
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
   146
        case None => None
wenzelm@38151
   147
      }
wenzelm@38879
   148
wenzelm@38879
   149
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@38879
   150
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38879
   151
wenzelm@38879
   152
    def command_starts: Iterator[(Command, Text.Offset)] =
wenzelm@38879
   153
      Node.command_starts(commands.iterator)
wenzelm@38151
   154
  }
wenzelm@38151
   155
wenzelm@38151
   156
wenzelm@38424
   157
wenzelm@38424
   158
  /** versioning **/
wenzelm@38424
   159
wenzelm@38424
   160
  /* particular document versions */
wenzelm@34485
   161
wenzelm@38417
   162
  object Version
wenzelm@38417
   163
  {
wenzelm@43722
   164
    val init: Version = Version(no_id, Map().withDefaultValue(Node.empty))
wenzelm@38417
   165
  }
wenzelm@38417
   166
wenzelm@44615
   167
  type Nodes = Map[Node.Name, Node]
wenzelm@44436
   168
  sealed case class Version(val id: Version_ID, val nodes: Nodes)
immler@34660
   169
wenzelm@34859
   170
wenzelm@38424
   171
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   172
wenzelm@38227
   173
  object Change
wenzelm@38227
   174
  {
wenzelm@44672
   175
    val init: Change = Change(Some(Future.value(Version.init)), Nil, Future.value(Version.init))
wenzelm@38227
   176
  }
wenzelm@38227
   177
wenzelm@43722
   178
  sealed case class Change(
wenzelm@44672
   179
    val previous: Option[Future[Version]],
wenzelm@40479
   180
    val edits: List[Edit_Text],
wenzelm@43722
   181
    val version: Future[Version])
wenzelm@38227
   182
  {
wenzelm@44672
   183
    def is_finished: Boolean =
wenzelm@44672
   184
      (previous match { case None => true case Some(future) => future.is_finished }) &&
wenzelm@44672
   185
      version.is_finished
wenzelm@44672
   186
wenzelm@44672
   187
    def truncate: Change = copy(previous = None, edits = Nil)
wenzelm@38227
   188
  }
wenzelm@38227
   189
wenzelm@38227
   190
wenzelm@38841
   191
  /* history navigation */
wenzelm@38841
   192
wenzelm@38841
   193
  object History
wenzelm@38841
   194
  {
wenzelm@44672
   195
    val init: History = History(List(Change.init))
wenzelm@38841
   196
  }
wenzelm@38841
   197
wenzelm@38841
   198
  // FIXME pruning, purging of state
wenzelm@44672
   199
  sealed case class History(val undo_list: List[Change])
wenzelm@38841
   200
  {
wenzelm@38841
   201
    require(!undo_list.isEmpty)
wenzelm@38841
   202
wenzelm@38841
   203
    def tip: Change = undo_list.head
wenzelm@44672
   204
    def +(change: Change): History = copy(undo_list = change :: undo_list)
wenzelm@38841
   205
  }
wenzelm@38841
   206
wenzelm@38841
   207
wenzelm@38841
   208
wenzelm@38841
   209
  /** global state -- document structure, execution process, editing history **/
wenzelm@38424
   210
wenzelm@38424
   211
  abstract class Snapshot
wenzelm@38424
   212
  {
wenzelm@44582
   213
    val state: State
wenzelm@38424
   214
    val version: Version
wenzelm@38424
   215
    val node: Node
wenzelm@38424
   216
    val is_outdated: Boolean
wenzelm@44582
   217
    def command_state(command: Command): Command.State
wenzelm@38427
   218
    def convert(i: Text.Offset): Text.Offset
wenzelm@39177
   219
    def convert(range: Text.Range): Text.Range
wenzelm@38427
   220
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   221
    def revert(range: Text.Range): Text.Range
wenzelm@39178
   222
    def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
wenzelm@39177
   223
      : Stream[Text.Info[Option[A]]]
wenzelm@38424
   224
  }
wenzelm@38424
   225
wenzelm@44476
   226
  type Assign =
wenzelm@44479
   227
   (List[(Document.Command_ID, Option[Document.Exec_ID])],  // exec state assignment
wenzelm@44476
   228
    List[(String, Option[Document.Command_ID])])  // last exec
wenzelm@44476
   229
wenzelm@44476
   230
  val no_assign: Assign = (Nil, Nil)
wenzelm@44476
   231
wenzelm@38370
   232
  object State
wenzelm@38370
   233
  {
wenzelm@38373
   234
    class Fail(state: State) extends Exception
wenzelm@38373
   235
wenzelm@44479
   236
    val init: State =
wenzelm@44479
   237
      State().define_version(Version.init, Assignment.init).assign(Version.init.id, no_assign)._2
wenzelm@44479
   238
wenzelm@44479
   239
    object Assignment
wenzelm@44479
   240
    {
wenzelm@44479
   241
      val init: Assignment = Assignment(Map.empty, Map.empty, false)
wenzelm@44479
   242
    }
wenzelm@38370
   243
wenzelm@38976
   244
    case class Assignment(
wenzelm@44477
   245
      val command_execs: Map[Command_ID, Exec_ID],
wenzelm@44477
   246
      val last_execs: Map[String, Option[Command_ID]],
wenzelm@44477
   247
      val is_finished: Boolean)
wenzelm@38370
   248
    {
wenzelm@44477
   249
      def check_finished: Assignment = { require(is_finished); this }
wenzelm@44479
   250
      def unfinished: Assignment = copy(is_finished = false)
wenzelm@44479
   251
wenzelm@44479
   252
      def assign(add_command_execs: List[(Command_ID, Option[Exec_ID])],
wenzelm@44477
   253
        add_last_execs: List[(String, Option[Command_ID])]): Assignment =
wenzelm@38370
   254
      {
wenzelm@38976
   255
        require(!is_finished)
wenzelm@44479
   256
        val command_execs1 =
wenzelm@44479
   257
          (command_execs /: add_command_execs) {
wenzelm@44479
   258
            case (res, (command_id, None)) => res - command_id
wenzelm@44479
   259
            case (res, (command_id, Some(exec_id))) => res + (command_id -> exec_id)
wenzelm@44479
   260
          }
wenzelm@44479
   261
        Assignment(command_execs1, last_execs ++ add_last_execs, true)
wenzelm@38150
   262
      }
wenzelm@38370
   263
    }
wenzelm@38370
   264
  }
wenzelm@38370
   265
wenzelm@43715
   266
  sealed case class State(
wenzelm@38417
   267
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@44446
   268
    val commands: Map[Command_ID, Command.State] = Map(),  // static markup from define_command
wenzelm@44446
   269
    val execs: Map[Exec_ID, Command.State] = Map(),  // dynamic markup from execution
wenzelm@43722
   270
    val assignments: Map[Version_ID, State.Assignment] = Map(),
wenzelm@38841
   271
    val history: History = History.init)
wenzelm@38370
   272
  {
wenzelm@38373
   273
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   274
wenzelm@44479
   275
    def define_version(version: Version, assignment: State.Assignment): State =
wenzelm@38370
   276
    {
wenzelm@38417
   277
      val id = version.id
wenzelm@38417
   278
      copy(versions = versions + (id -> version),
wenzelm@44479
   279
        assignments = assignments + (id -> assignment.unfinished))
wenzelm@38370
   280
    }
wenzelm@38370
   281
wenzelm@38373
   282
    def define_command(command: Command): State =
wenzelm@38373
   283
    {
wenzelm@38373
   284
      val id = command.id
wenzelm@38373
   285
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   286
    }
wenzelm@38373
   287
wenzelm@44582
   288
    def defined_command(id: Command_ID): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   289
wenzelm@44607
   290
    def find_command(version: Version, id: ID): Option[(Node, Command)] =
wenzelm@44582
   291
      commands.get(id) orElse execs.get(id) match {
wenzelm@44582
   292
        case None => None
wenzelm@44582
   293
        case Some(st) =>
wenzelm@44582
   294
          val command = st.command
wenzelm@44607
   295
          version.nodes.get(command.node_name).map((_, command))
wenzelm@44582
   296
      }
wenzelm@38373
   297
wenzelm@38417
   298
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@44479
   299
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)  // FIXME rename
wenzelm@44446
   300
    def the_exec(id: Exec_ID): Command.State = execs.getOrElse(id, fail)
wenzelm@43722
   301
    def the_assignment(version: Version): State.Assignment =
wenzelm@43722
   302
      assignments.getOrElse(version.id, fail)
wenzelm@38370
   303
wenzelm@38872
   304
    def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
wenzelm@38370
   305
      execs.get(id) match {
wenzelm@44446
   306
        case Some(st) =>
wenzelm@38370
   307
          val new_st = st.accumulate(message)
wenzelm@44446
   308
          (new_st, copy(execs = execs + (id -> new_st)))
wenzelm@38370
   309
        case None =>
wenzelm@38370
   310
          commands.get(id) match {
wenzelm@38370
   311
            case Some(st) =>
wenzelm@38370
   312
              val new_st = st.accumulate(message)
wenzelm@38370
   313
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   314
            case None => fail
wenzelm@38370
   315
          }
wenzelm@38370
   316
      }
wenzelm@38370
   317
wenzelm@44476
   318
    def assign(id: Version_ID, arg: Assign): (List[Command], State) =
wenzelm@38370
   319
    {
wenzelm@38417
   320
      val version = the_version(id)
wenzelm@44477
   321
      val (command_execs, last_execs) = arg
wenzelm@38370
   322
wenzelm@44543
   323
      val (changed_commands, new_execs) =
wenzelm@44479
   324
        ((Nil: List[Command], execs) /: command_execs) {
wenzelm@44543
   325
          case ((commands1, execs1), (cmd_id, exec)) =>
wenzelm@44479
   326
            val st = the_command(cmd_id)
wenzelm@44543
   327
            val commands2 = st.command :: commands1
wenzelm@44543
   328
            val execs2 =
wenzelm@44543
   329
              exec match {
wenzelm@44543
   330
                case None => execs1
wenzelm@44543
   331
                case Some(exec_id) => execs1 + (exec_id -> st)
wenzelm@44543
   332
              }
wenzelm@44543
   333
            (commands2, execs2)
wenzelm@38370
   334
        }
wenzelm@44477
   335
      val new_assignment = the_assignment(version).assign(command_execs, last_execs)
wenzelm@43722
   336
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   337
wenzelm@44543
   338
      (changed_commands, new_state)
wenzelm@38370
   339
    }
wenzelm@38370
   340
wenzelm@38417
   341
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   342
      assignments.get(version.id) match {
wenzelm@38370
   343
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   344
        case None => false
wenzelm@38370
   345
      }
wenzelm@38370
   346
wenzelm@44436
   347
    def is_stable(change: Change): Boolean =
wenzelm@44436
   348
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   349
wenzelm@44672
   350
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
wenzelm@44436
   351
    def tip_stable: Boolean = is_stable(history.tip)
wenzelm@44475
   352
    def tip_version: Version = history.tip.version.get_finished
wenzelm@44436
   353
wenzelm@44615
   354
    def last_exec_offset(name: Node.Name): Text.Offset =
wenzelm@44484
   355
    {
wenzelm@44484
   356
      val version = tip_version
wenzelm@44615
   357
      the_assignment(version).last_execs.get(name.node) match {
wenzelm@44484
   358
        case Some(Some(id)) =>
wenzelm@44484
   359
          val node = version.nodes(name)
wenzelm@44484
   360
          val cmd = the_command(id).command
wenzelm@44484
   361
          node.command_start(cmd) match {
wenzelm@44484
   362
            case None => 0
wenzelm@44484
   363
            case Some(start) => start + cmd.length
wenzelm@44484
   364
          }
wenzelm@44484
   365
        case _ => 0
wenzelm@44484
   366
      }
wenzelm@44484
   367
    }
wenzelm@44484
   368
wenzelm@44446
   369
    def continue_history(
wenzelm@44446
   370
        previous: Future[Version],
wenzelm@40479
   371
        edits: List[Edit_Text],
wenzelm@43722
   372
        version: Future[Version]): (Change, State) =
wenzelm@38841
   373
    {
wenzelm@44672
   374
      val change = Change(Some(previous), edits, version)
wenzelm@38841
   375
      (change, copy(history = history + change))
wenzelm@38841
   376
    }
wenzelm@38841
   377
wenzelm@44672
   378
    def prune_history(retain: Int = 0): (List[Version], State) =
wenzelm@44672
   379
    {
wenzelm@44672
   380
      val undo_list = history.undo_list
wenzelm@44672
   381
      val n = undo_list.iterator.zipWithIndex.find(p => is_stable(p._1)).get._2 + 1
wenzelm@44672
   382
      val (retained, dropped) = undo_list.splitAt(n max retain)
wenzelm@44672
   383
wenzelm@44672
   384
      retained.splitAt(retained.length - 1) match {
wenzelm@44672
   385
        case (prefix, List(last)) =>
wenzelm@44672
   386
          val dropped_versions = dropped.map(change => change.version.get_finished)
wenzelm@44672
   387
          val state1 = copy(history = History(prefix ::: List(last.truncate)))
wenzelm@44672
   388
          (dropped_versions, state1)
wenzelm@44672
   389
        case _ => fail
wenzelm@44672
   390
      }
wenzelm@44672
   391
    }
wenzelm@44672
   392
wenzelm@44676
   393
    def removed_versions(removed: List[Version_ID]): State =
wenzelm@44676
   394
    {
wenzelm@44676
   395
      val versions1 = versions -- removed
wenzelm@44676
   396
      val assignments1 = assignments -- removed
wenzelm@44676
   397
      var commands1 = Map.empty[Command_ID, Command.State]
wenzelm@44676
   398
      var execs1 = Map.empty[Exec_ID, Command.State]
wenzelm@44676
   399
      for {
wenzelm@44676
   400
        (version_id, version) <- versions1.iterator
wenzelm@44676
   401
        val command_execs = assignments1(version_id).command_execs
wenzelm@44676
   402
        (_, node) <- version.nodes.iterator
wenzelm@44676
   403
        command <- node.commands.iterator
wenzelm@44676
   404
      } {
wenzelm@44676
   405
        val id = command.id
wenzelm@44676
   406
        if (!commands1.isDefinedAt(id) && commands.isDefinedAt(id))
wenzelm@44676
   407
          commands1 += (id -> commands(id))
wenzelm@44676
   408
        if (command_execs.isDefinedAt(id)) {
wenzelm@44676
   409
          val exec_id = command_execs(id)
wenzelm@44676
   410
          if (!execs1.isDefinedAt(exec_id) && execs.isDefinedAt(exec_id))
wenzelm@44676
   411
            execs1 += (exec_id -> execs(exec_id))
wenzelm@44676
   412
        }
wenzelm@44676
   413
      }
wenzelm@44676
   414
      copy(versions = versions1, commands = commands1, execs = execs1, assignments = assignments1)
wenzelm@44676
   415
    }
wenzelm@44676
   416
wenzelm@44613
   417
    def command_state(version: Version, command: Command): Command.State =
wenzelm@44613
   418
    {
wenzelm@44613
   419
      require(is_assigned(version))
wenzelm@44613
   420
      try {
wenzelm@44613
   421
        the_exec(the_assignment(version).check_finished.command_execs
wenzelm@44613
   422
          .getOrElse(command.id, fail))
wenzelm@44613
   423
      }
wenzelm@44613
   424
      catch { case _: State.Fail => command.empty_state }
wenzelm@44613
   425
    }
wenzelm@44613
   426
wenzelm@38841
   427
wenzelm@38841
   428
    // persistent user-view
wenzelm@44615
   429
    def snapshot(name: Node.Name, pending_edits: List[Text.Edit]): Snapshot =
wenzelm@38370
   430
    {
wenzelm@44672
   431
      val stable = recent_stable
wenzelm@44436
   432
      val latest = history.tip
wenzelm@38841
   433
wenzelm@44156
   434
      // FIXME proper treatment of removed nodes
wenzelm@38841
   435
      val edits =
wenzelm@38841
   436
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@44156
   437
            (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
wenzelm@38841
   438
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   439
wenzelm@38841
   440
      new Snapshot
wenzelm@38841
   441
      {
wenzelm@44582
   442
        val state = State.this
wenzelm@39698
   443
        val version = stable.version.get_finished
wenzelm@38841
   444
        val node = version.nodes(name)
wenzelm@38841
   445
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@44613
   446
        def command_state(command: Command): Command.State = state.command_state(version, command)
wenzelm@38841
   447
wenzelm@38841
   448
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   449
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@43425
   450
        def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
wenzelm@43425
   451
        def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
wenzelm@39177
   452
wenzelm@39178
   453
        def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
wenzelm@39177
   454
          : Stream[Text.Info[Option[A]]] =
wenzelm@38845
   455
        {
wenzelm@38845
   456
          val former_range = revert(range)
wenzelm@38845
   457
          for {
wenzelm@39177
   458
            (command, command_start) <- node.command_range(former_range).toStream
wenzelm@44582
   459
            Text.Info(r0, x) <- command_state(command).markup.
wenzelm@38845
   460
              select((former_range - command_start).restrict(command.range)) {
wenzelm@38845
   461
                case Text.Info(r0, info)
wenzelm@38845
   462
                if result.isDefinedAt(Text.Info(convert(r0 + command_start), info)) =>
wenzelm@38845
   463
                  result(Text.Info(convert(r0 + command_start), info))
wenzelm@39177
   464
              }
wenzelm@43427
   465
          } yield Text.Info(convert(r0 + command_start), x)
wenzelm@38845
   466
        }
wenzelm@38841
   467
      }
wenzelm@38150
   468
    }
wenzelm@34485
   469
  }
wenzelm@34840
   470
}
wenzelm@34840
   471