src/Pure/PIDE/document.scala
author wenzelm
Tue Apr 15 16:44:06 2014 +0200 (2014-04-15)
changeset 56590 d01d183e84ea
parent 56514 db929027e701
child 56711 ef3d00153e3b
permissions -rw-r--r--
clarified treatment of markup ranges wrt. revert/convert: inflate_singularity allows to retrieve information like language_context more reliably during editing;
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@52977
    18
  /* overlays -- print functions with arguments */
wenzelm@52977
    19
wenzelm@52977
    20
  object Overlays
wenzelm@52977
    21
  {
wenzelm@52977
    22
    val empty = new Overlays(Map.empty)
wenzelm@52977
    23
  }
wenzelm@52977
    24
wenzelm@52977
    25
  final class Overlays private(rep: Map[Node.Name, Node.Overlays])
wenzelm@52977
    26
  {
wenzelm@52977
    27
    def apply(name: Document.Node.Name): Node.Overlays =
wenzelm@52977
    28
      rep.getOrElse(name, Node.Overlays.empty)
wenzelm@52977
    29
wenzelm@52977
    30
    private def update(name: Node.Name, f: Node.Overlays => Node.Overlays): Overlays =
wenzelm@52977
    31
    {
wenzelm@52977
    32
      val node_overlays = f(apply(name))
wenzelm@52977
    33
      new Overlays(if (node_overlays.is_empty) rep - name else rep + (name -> node_overlays))
wenzelm@52977
    34
    }
wenzelm@52977
    35
wenzelm@52977
    36
    def insert(command: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
    37
      update(command.node_name, _.insert(command, fn, args))
wenzelm@52977
    38
wenzelm@52977
    39
    def remove(command: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
    40
      update(command.node_name, _.remove(command, fn, args))
wenzelm@55800
    41
wenzelm@55800
    42
    override def toString: String = rep.mkString("Overlays(", ",", ")")
wenzelm@52977
    43
  }
wenzelm@52977
    44
wenzelm@52977
    45
wenzelm@55783
    46
  /* document blobs: auxiliary files */
wenzelm@55783
    47
wenzelm@56473
    48
  sealed case class Blob(bytes: Bytes, chunk: Text.Chunk, changed: Boolean)
wenzelm@56335
    49
  {
wenzelm@56335
    50
    def unchanged: Blob = copy(changed = false)
wenzelm@56335
    51
  }
wenzelm@55783
    52
wenzelm@55783
    53
  object Blobs
wenzelm@55783
    54
  {
wenzelm@56336
    55
    def apply(blobs: Map[Node.Name, Blob]): Blobs = new Blobs(blobs)
wenzelm@55783
    56
    val empty: Blobs = apply(Map.empty)
wenzelm@55783
    57
  }
wenzelm@55783
    58
wenzelm@56336
    59
  final class Blobs private(blobs: Map[Node.Name, Blob])
wenzelm@55783
    60
  {
wenzelm@55801
    61
    private lazy val digests: Map[SHA1.Digest, Blob] =
wenzelm@55801
    62
      for ((_, blob) <- blobs) yield (blob.bytes.sha1_digest, blob)
wenzelm@55801
    63
wenzelm@55801
    64
    def get(digest: SHA1.Digest): Option[Blob] = digests.get(digest)
wenzelm@56336
    65
    def get(name: Node.Name): Option[Blob] = blobs.get(name)
wenzelm@55783
    66
wenzelm@55783
    67
    def changed(name: Node.Name): Boolean =
wenzelm@55783
    68
      get(name) match {
wenzelm@55783
    69
        case Some(blob) => blob.changed
wenzelm@55783
    70
        case None => false
wenzelm@55783
    71
      }
wenzelm@55783
    72
wenzelm@55800
    73
    override def toString: String = blobs.mkString("Blobs(", ",", ")")
wenzelm@55783
    74
  }
wenzelm@55783
    75
wenzelm@55783
    76
wenzelm@55783
    77
  /* document nodes: theories and auxiliary files */
wenzelm@38424
    78
wenzelm@44615
    79
  type Edit[A, B] = (Node.Name, Node.Edit[A, B])
wenzelm@44384
    80
  type Edit_Text = Edit[Text.Edit, Text.Perspective]
wenzelm@52849
    81
  type Edit_Command = Edit[Command.Edit, Command.Perspective]
wenzelm@38151
    82
wenzelm@38151
    83
  object Node
wenzelm@38151
    84
  {
wenzelm@52887
    85
    val empty: Node = new Node()
wenzelm@52887
    86
wenzelm@52887
    87
wenzelm@52887
    88
    /* header and name */
wenzelm@52887
    89
wenzelm@48707
    90
    sealed case class Header(
wenzelm@46938
    91
      imports: List[Name],
wenzelm@48706
    92
      keywords: Thy_Header.Keywords,
wenzelm@48707
    93
      errors: List[String] = Nil)
wenzelm@48707
    94
    {
wenzelm@48707
    95
      def error(msg: String): Header = copy(errors = errors ::: List(msg))
wenzelm@54549
    96
wenzelm@54549
    97
      def cat_errors(msg2: String): Header =
wenzelm@54549
    98
        copy(errors = errors.map(msg1 => Library.cat_message(msg1, msg2)))
wenzelm@48707
    99
    }
wenzelm@48707
   100
wenzelm@51294
   101
    def bad_header(msg: String): Header = Header(Nil, Nil, List(msg))
wenzelm@46737
   102
wenzelm@54509
   103
    val no_header = bad_header("No theory header")
wenzelm@54509
   104
wenzelm@44957
   105
    object Name
wenzelm@44957
   106
    {
wenzelm@54515
   107
      val empty = Name("")
wenzelm@46723
   108
wenzelm@46723
   109
      object Ordering extends scala.math.Ordering[Name]
wenzelm@46723
   110
      {
wenzelm@46723
   111
        def compare(name1: Name, name2: Name): Int = name1.node compare name2.node
wenzelm@46723
   112
      }
wenzelm@44957
   113
    }
wenzelm@52887
   114
wenzelm@54515
   115
    sealed case class Name(node: String, master_dir: String = "", theory: String = "")
wenzelm@44615
   116
    {
wenzelm@44615
   117
      override def hashCode: Int = node.hashCode
wenzelm@44615
   118
      override def equals(that: Any): Boolean =
wenzelm@44615
   119
        that match {
wenzelm@44615
   120
          case other: Name => node == other.node
wenzelm@44615
   121
          case _ => false
wenzelm@44615
   122
        }
wenzelm@54509
   123
wenzelm@54509
   124
      def is_theory: Boolean = !theory.isEmpty
wenzelm@54510
   125
      override def toString: String = if (is_theory) theory else node
wenzelm@44615
   126
    }
wenzelm@44615
   127
wenzelm@52887
   128
wenzelm@52977
   129
    /* node overlays */
wenzelm@52887
   130
wenzelm@52887
   131
    object Overlays
wenzelm@52887
   132
    {
wenzelm@52976
   133
      val empty = new Overlays(Multi_Map.empty)
wenzelm@52887
   134
    }
wenzelm@52887
   135
wenzelm@52977
   136
    final class Overlays private(rep: Multi_Map[Command, (String, List[String])])
wenzelm@52887
   137
    {
wenzelm@52887
   138
      def commands: Set[Command] = rep.keySet
wenzelm@52887
   139
      def is_empty: Boolean = rep.isEmpty
wenzelm@52977
   140
      def dest: List[(Command, (String, List[String]))] = rep.iterator.toList
wenzelm@52977
   141
      def insert(cmd: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
   142
        new Overlays(rep.insert(cmd, (fn, args)))
wenzelm@52977
   143
      def remove(cmd: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
   144
        new Overlays(rep.remove(cmd, (fn, args)))
wenzelm@55800
   145
wenzelm@55800
   146
      override def toString: String = rep.mkString("Node.Overlays(", ",", ")")
wenzelm@52887
   147
    }
wenzelm@52887
   148
wenzelm@52887
   149
wenzelm@52887
   150
    /* edits */
wenzelm@52887
   151
wenzelm@44384
   152
    sealed abstract class Edit[A, B]
wenzelm@44156
   153
    {
wenzelm@44383
   154
      def foreach(f: A => Unit)
wenzelm@44383
   155
      {
wenzelm@44156
   156
        this match {
wenzelm@44383
   157
          case Edits(es) => es.foreach(f)
wenzelm@44383
   158
          case _ =>
wenzelm@44156
   159
        }
wenzelm@44383
   160
      }
wenzelm@44156
   161
    }
wenzelm@44384
   162
    case class Clear[A, B]() extends Edit[A, B]
wenzelm@56335
   163
    case class Blob[A, B](blob: Document.Blob) extends Edit[A, B]
wenzelm@54562
   164
wenzelm@44384
   165
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
wenzelm@48707
   166
    case class Deps[A, B](header: Header) extends Edit[A, B]
wenzelm@52849
   167
    case class Perspective[A, B](required: Boolean, visible: B, overlays: Overlays) extends Edit[A, B]
wenzelm@52808
   168
    type Perspective_Text = Perspective[Text.Edit, Text.Perspective]
wenzelm@52849
   169
    type Perspective_Command = Perspective[Command.Edit, Command.Perspective]
wenzelm@44156
   170
wenzelm@52887
   171
wenzelm@52887
   172
    /* commands */
wenzelm@52887
   173
wenzelm@52901
   174
    object Commands
wenzelm@52901
   175
    {
wenzelm@52901
   176
      def apply(commands: Linear_Set[Command]): Commands = new Commands(commands)
wenzelm@52901
   177
      val empty: Commands = apply(Linear_Set.empty)
wenzelm@52901
   178
wenzelm@52901
   179
      def starts(commands: Iterator[Command], offset: Text.Offset = 0)
wenzelm@52901
   180
        : Iterator[(Command, Text.Offset)] =
wenzelm@52901
   181
      {
wenzelm@52901
   182
        var i = offset
wenzelm@52901
   183
        for (command <- commands) yield {
wenzelm@52901
   184
          val start = i
wenzelm@52901
   185
          i += command.length
wenzelm@52901
   186
          (command, start)
wenzelm@52901
   187
        }
wenzelm@52901
   188
      }
wenzelm@52901
   189
wenzelm@56398
   190
      private val block_size = 256
wenzelm@52901
   191
    }
wenzelm@52901
   192
wenzelm@52901
   193
    final class Commands private(val commands: Linear_Set[Command])
wenzelm@38227
   194
    {
wenzelm@56314
   195
      lazy val load_commands: List[Command] =
wenzelm@54513
   196
        commands.iterator.filter(cmd => !cmd.blobs.isEmpty).toList
wenzelm@54462
   197
wenzelm@52901
   198
      private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
wenzelm@52901
   199
      {
wenzelm@52901
   200
        val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
wenzelm@52901
   201
        var next_block = 0
wenzelm@52901
   202
        var last_stop = 0
wenzelm@52901
   203
        for ((command, start) <- Commands.starts(commands.iterator)) {
wenzelm@52901
   204
          last_stop = start + command.length
wenzelm@52901
   205
          while (last_stop + 1 > next_block) {
wenzelm@52901
   206
            blocks += (command -> start)
wenzelm@52901
   207
            next_block += Commands.block_size
wenzelm@52901
   208
          }
wenzelm@52901
   209
        }
wenzelm@52901
   210
        (blocks.toArray, Text.Range(0, last_stop))
wenzelm@52901
   211
      }
wenzelm@52901
   212
wenzelm@52901
   213
      private def full_range: Text.Range = full_index._2
wenzelm@52901
   214
wenzelm@56373
   215
      def iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@52901
   216
      {
wenzelm@52901
   217
        if (!commands.isEmpty && full_range.contains(i)) {
wenzelm@52901
   218
          val (cmd0, start0) = full_index._1(i / Commands.block_size)
wenzelm@52901
   219
          Node.Commands.starts(commands.iterator(cmd0), start0) dropWhile {
wenzelm@52901
   220
            case (cmd, start) => start + cmd.length <= i }
wenzelm@52901
   221
        }
wenzelm@52901
   222
        else Iterator.empty
wenzelm@38227
   223
      }
wenzelm@38227
   224
    }
wenzelm@38151
   225
  }
wenzelm@38151
   226
wenzelm@46712
   227
  final class Node private(
wenzelm@56335
   228
    val get_blob: Option[Document.Blob] = None,
wenzelm@48707
   229
    val header: Node.Header = Node.bad_header("Bad theory header"),
wenzelm@52849
   230
    val perspective: Node.Perspective_Command =
wenzelm@52887
   231
      Node.Perspective(false, Command.Perspective.empty, Node.Overlays.empty),
wenzelm@52901
   232
    _commands: Node.Commands = Node.Commands.empty)
wenzelm@38151
   233
  {
wenzelm@46680
   234
    def clear: Node = new Node(header = header)
wenzelm@46680
   235
wenzelm@56335
   236
    def init_blob(blob: Document.Blob): Node = new Node(Some(blob.unchanged))
wenzelm@55435
   237
wenzelm@48707
   238
    def update_header(new_header: Node.Header): Node =
wenzelm@56335
   239
      new Node(get_blob, new_header, perspective, _commands)
wenzelm@46680
   240
wenzelm@52849
   241
    def update_perspective(new_perspective: Node.Perspective_Command): Node =
wenzelm@56335
   242
      new Node(get_blob, header, new_perspective, _commands)
wenzelm@46680
   243
wenzelm@52849
   244
    def same_perspective(other_perspective: Node.Perspective_Command): Boolean =
wenzelm@52849
   245
      perspective.required == other_perspective.required &&
wenzelm@52849
   246
      perspective.visible.same(other_perspective.visible) &&
wenzelm@52849
   247
      perspective.overlays == other_perspective.overlays
wenzelm@52808
   248
wenzelm@52901
   249
    def commands: Linear_Set[Command] = _commands.commands
wenzelm@56314
   250
    def load_commands: List[Command] = _commands.load_commands
wenzelm@43697
   251
wenzelm@52901
   252
    def update_commands(new_commands: Linear_Set[Command]): Node =
wenzelm@52918
   253
      if (new_commands eq _commands.commands) this
wenzelm@56335
   254
      else new Node(get_blob, header, perspective, Node.Commands(new_commands))
wenzelm@38151
   255
wenzelm@56373
   256
    def command_iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@56373
   257
      _commands.iterator(i)
wenzelm@38151
   258
wenzelm@56373
   259
    def command_iterator(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@56373
   260
      command_iterator(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
   261
wenzelm@38879
   262
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@52901
   263
      Node.Commands.starts(commands.iterator).find(_._1 == cmd).map(_._2)
wenzelm@38151
   264
  }
wenzelm@38151
   265
wenzelm@38151
   266
wenzelm@46723
   267
  /* development graph */
wenzelm@46723
   268
wenzelm@46723
   269
  object Nodes
wenzelm@46723
   270
  {
wenzelm@46723
   271
    val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
wenzelm@46723
   272
  }
wenzelm@46723
   273
wenzelm@46723
   274
  final class Nodes private(graph: Graph[Node.Name, Node])
wenzelm@46723
   275
  {
wenzelm@46723
   276
    def get_name(s: String): Option[Node.Name] =
wenzelm@56372
   277
      graph.keys_iterator.find(name => name.node == s)
wenzelm@46723
   278
wenzelm@46723
   279
    def apply(name: Node.Name): Node =
wenzelm@46723
   280
      graph.default_node(name, Node.empty).get_node(name)
wenzelm@46723
   281
wenzelm@46723
   282
    def + (entry: (Node.Name, Node)): Nodes =
wenzelm@46723
   283
    {
wenzelm@46723
   284
      val (name, node) = entry
wenzelm@48707
   285
      val imports = node.header.imports
wenzelm@46723
   286
      val graph1 =
wenzelm@46739
   287
        (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty))
wenzelm@46723
   288
      val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
wenzelm@46739
   289
      val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name))
wenzelm@46723
   290
      new Nodes(graph3.map_node(name, _ => node))
wenzelm@46723
   291
    }
wenzelm@46723
   292
wenzelm@56372
   293
    def iterator: Iterator[(Node.Name, Node)] =
wenzelm@56372
   294
      graph.iterator.map({ case (name, (node, _)) => (name, node) })
wenzelm@46723
   295
wenzelm@56314
   296
    def load_commands(file_name: Node.Name): List[Command] =
wenzelm@54528
   297
      (for {
wenzelm@56372
   298
        (_, node) <- iterator
wenzelm@56314
   299
        cmd <- node.load_commands.iterator
wenzelm@54530
   300
        name <- cmd.blobs_names.iterator
wenzelm@54528
   301
        if name == file_name
wenzelm@54528
   302
      } yield cmd).toList
wenzelm@54528
   303
wenzelm@46942
   304
    def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names)
wenzelm@46723
   305
    def topological_order: List[Node.Name] = graph.topological_order
wenzelm@56337
   306
wenzelm@56337
   307
    override def toString: String = topological_order.mkString("Nodes(", ",", ")")
wenzelm@46723
   308
  }
wenzelm@46723
   309
wenzelm@46723
   310
wenzelm@38424
   311
wenzelm@38424
   312
  /** versioning **/
wenzelm@38424
   313
wenzelm@38424
   314
  /* particular document versions */
wenzelm@34485
   315
wenzelm@38417
   316
  object Version
wenzelm@38417
   317
  {
wenzelm@46681
   318
    val init: Version = new Version()
wenzelm@46681
   319
wenzelm@56394
   320
    def make(syntax: Option[Prover.Syntax], nodes: Nodes): Version =
wenzelm@52530
   321
      new Version(Document_ID.make(), syntax, nodes)
wenzelm@38417
   322
  }
wenzelm@38417
   323
wenzelm@46712
   324
  final class Version private(
wenzelm@52530
   325
    val id: Document_ID.Version = Document_ID.none,
wenzelm@56394
   326
    val syntax: Option[Prover.Syntax] = None,
wenzelm@46723
   327
    val nodes: Nodes = Nodes.empty)
wenzelm@46941
   328
  {
wenzelm@55777
   329
    override def toString: String = "Version(" + id + ")"
wenzelm@46941
   330
  }
immler@34660
   331
wenzelm@34859
   332
wenzelm@38424
   333
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   334
wenzelm@38227
   335
  object Change
wenzelm@38227
   336
  {
wenzelm@46678
   337
    val init: Change = new Change()
wenzelm@46678
   338
wenzelm@46678
   339
    def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
wenzelm@46678
   340
      new Change(Some(previous), edits, version)
wenzelm@38227
   341
  }
wenzelm@38227
   342
wenzelm@46712
   343
  final class Change private(
wenzelm@46677
   344
    val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
wenzelm@46677
   345
    val edits: List[Edit_Text] = Nil,
wenzelm@46677
   346
    val version: Future[Version] = Future.value(Version.init))
wenzelm@38227
   347
  {
wenzelm@44672
   348
    def is_finished: Boolean =
wenzelm@44672
   349
      (previous match { case None => true case Some(future) => future.is_finished }) &&
wenzelm@44672
   350
      version.is_finished
wenzelm@44672
   351
wenzelm@46678
   352
    def truncate: Change = new Change(None, Nil, version)
wenzelm@38227
   353
  }
wenzelm@38227
   354
wenzelm@38227
   355
wenzelm@38841
   356
  /* history navigation */
wenzelm@38841
   357
wenzelm@38841
   358
  object History
wenzelm@38841
   359
  {
wenzelm@46679
   360
    val init: History = new History()
wenzelm@38841
   361
  }
wenzelm@38841
   362
wenzelm@46712
   363
  final class History private(
wenzelm@46679
   364
    val undo_list: List[Change] = List(Change.init))  // non-empty list
wenzelm@38841
   365
  {
wenzelm@46679
   366
    def tip: Change = undo_list.head
wenzelm@46679
   367
    def + (change: Change): History = new History(change :: undo_list)
wenzelm@38841
   368
wenzelm@46679
   369
    def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
wenzelm@46679
   370
    {
wenzelm@46679
   371
      val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
wenzelm@46679
   372
      val (retained, dropped) = undo_list.splitAt(n max retain)
wenzelm@46679
   373
wenzelm@46679
   374
      retained.splitAt(retained.length - 1) match {
wenzelm@46679
   375
        case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
wenzelm@46679
   376
        case _ => None
wenzelm@46679
   377
      }
wenzelm@46679
   378
    }
wenzelm@38841
   379
  }
wenzelm@38841
   380
wenzelm@38841
   381
wenzelm@55820
   382
  /* markup elements */
wenzelm@38841
   383
wenzelm@55820
   384
  object Elements
wenzelm@55820
   385
  {
wenzelm@55820
   386
    def apply(elems: Set[String]): Elements = new Elements(elems)
wenzelm@55820
   387
    def apply(elems: String*): Elements = apply(Set(elems: _*))
wenzelm@55820
   388
    val empty: Elements = apply()
wenzelm@55820
   389
    val full: Elements = new Full_Elements
wenzelm@55820
   390
  }
wenzelm@55820
   391
wenzelm@55820
   392
  sealed class Elements private[Document](private val rep: Set[String])
wenzelm@55820
   393
  {
wenzelm@55820
   394
    def apply(elem: String): Boolean = rep.contains(elem)
wenzelm@55820
   395
    def + (elem: String): Elements = new Elements(rep + elem)
wenzelm@55820
   396
    def ++ (elems: Elements): Elements = new Elements(rep ++ elems.rep)
wenzelm@55820
   397
    override def toString: String = rep.mkString("Elements(", ",", ")")
wenzelm@55820
   398
  }
wenzelm@55820
   399
wenzelm@55820
   400
  private class Full_Elements extends Elements(Set.empty)
wenzelm@55820
   401
  {
wenzelm@55820
   402
    override def apply(elem: String): Boolean = true
wenzelm@55820
   403
    override def toString: String = "Full_Elements()"
wenzelm@55820
   404
  }
wenzelm@55820
   405
wenzelm@55820
   406
wenzelm@55820
   407
  /* snapshot */
wenzelm@38424
   408
wenzelm@52972
   409
  object Snapshot
wenzelm@52972
   410
  {
wenzelm@52972
   411
    val init = State.init.snapshot()
wenzelm@52972
   412
  }
wenzelm@52972
   413
wenzelm@38424
   414
  abstract class Snapshot
wenzelm@38424
   415
  {
wenzelm@44582
   416
    val state: State
wenzelm@38424
   417
    val version: Version
wenzelm@38424
   418
    val is_outdated: Boolean
wenzelm@55649
   419
wenzelm@38427
   420
    def convert(i: Text.Offset): Text.Offset
wenzelm@52889
   421
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   422
    def convert(range: Text.Range): Text.Range
wenzelm@39177
   423
    def revert(range: Text.Range): Text.Range
wenzelm@52972
   424
wenzelm@52972
   425
    val node_name: Node.Name
wenzelm@52972
   426
    val node: Node
wenzelm@56314
   427
    val load_commands: List[Command]
wenzelm@56176
   428
    def is_loaded: Boolean
wenzelm@51494
   429
    def eq_content(other: Snapshot): Boolean
wenzelm@55649
   430
wenzelm@55651
   431
    def cumulate[A](
wenzelm@55649
   432
      range: Text.Range,
wenzelm@55649
   433
      info: A,
wenzelm@55820
   434
      elements: Elements,
wenzelm@56354
   435
      result: List[Command.State] => (A, Text.Markup) => Option[A],
wenzelm@55651
   436
      status: Boolean = false): List[Text.Info[A]]
wenzelm@55651
   437
wenzelm@55651
   438
    def select[A](
wenzelm@55649
   439
      range: Text.Range,
wenzelm@55820
   440
      elements: Elements,
wenzelm@56354
   441
      result: List[Command.State] => Text.Markup => Option[A],
wenzelm@55651
   442
      status: Boolean = false): List[Text.Info[A]]
wenzelm@38424
   443
  }
wenzelm@38424
   444
wenzelm@55820
   445
wenzelm@55820
   446
wenzelm@55820
   447
  /** global state -- document structure, execution process, editing history **/
wenzelm@55820
   448
wenzelm@52563
   449
  type Assign_Update =
wenzelm@52563
   450
    List[(Document_ID.Command, List[Document_ID.Exec])]  // update of exec state assignment
wenzelm@44476
   451
wenzelm@38370
   452
  object State
wenzelm@38370
   453
  {
wenzelm@38373
   454
    class Fail(state: State) extends Exception
wenzelm@38373
   455
wenzelm@44479
   456
    object Assignment
wenzelm@44479
   457
    {
wenzelm@46683
   458
      val init: Assignment = new Assignment()
wenzelm@44479
   459
    }
wenzelm@38370
   460
wenzelm@46712
   461
    final class Assignment private(
wenzelm@52530
   462
      val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty,
wenzelm@46677
   463
      val is_finished: Boolean = false)
wenzelm@38370
   464
    {
wenzelm@44477
   465
      def check_finished: Assignment = { require(is_finished); this }
wenzelm@47388
   466
      def unfinished: Assignment = new Assignment(command_execs, false)
wenzelm@44479
   467
wenzelm@52563
   468
      def assign(update: Assign_Update): Assignment =
wenzelm@38370
   469
      {
wenzelm@38976
   470
        require(!is_finished)
wenzelm@44479
   471
        val command_execs1 =
wenzelm@52563
   472
          (command_execs /: update) {
wenzelm@52563
   473
            case (res, (command_id, exec_ids)) =>
wenzelm@52563
   474
              if (exec_ids.isEmpty) res - command_id
wenzelm@52563
   475
              else res + (command_id -> exec_ids)
wenzelm@44479
   476
          }
wenzelm@47388
   477
        new Assignment(command_execs1, true)
wenzelm@38150
   478
      }
wenzelm@38370
   479
    }
wenzelm@46682
   480
wenzelm@46682
   481
    val init: State =
wenzelm@52563
   482
      State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2
wenzelm@38370
   483
  }
wenzelm@38370
   484
wenzelm@46712
   485
  final case class State private(
wenzelm@56470
   486
    /*reachable versions*/
wenzelm@52530
   487
    val versions: Map[Document_ID.Version, Version] = Map.empty,
wenzelm@56470
   488
    /*inlined auxiliary files*/
wenzelm@56470
   489
    val blobs: Set[SHA1.Digest] = Set.empty,
wenzelm@56470
   490
    /*static markup from define_command*/
wenzelm@56470
   491
    val commands: Map[Document_ID.Command, Command.State] = Map.empty,
wenzelm@56470
   492
    /*dynamic markup from execution*/
wenzelm@56470
   493
    val execs: Map[Document_ID.Exec, Command.State] = Map.empty,
wenzelm@56470
   494
    /*command-exec assignment for each version*/
wenzelm@52530
   495
    val assignments: Map[Document_ID.Version, State.Assignment] = Map.empty,
wenzelm@56470
   496
    /*commands with markup produced by other commands (imm_succs)*/
wenzelm@56475
   497
    val commands_redirection: Graph[Document_ID.Command, Unit] = Graph.long,
wenzelm@56470
   498
    /*explicit (linear) history*/
wenzelm@38841
   499
    val history: History = History.init)
wenzelm@38370
   500
  {
wenzelm@38373
   501
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   502
wenzelm@44479
   503
    def define_version(version: Version, assignment: State.Assignment): State =
wenzelm@38370
   504
    {
wenzelm@38417
   505
      val id = version.id
wenzelm@38417
   506
      copy(versions = versions + (id -> version),
wenzelm@44479
   507
        assignments = assignments + (id -> assignment.unfinished))
wenzelm@38370
   508
    }
wenzelm@38370
   509
wenzelm@54519
   510
    def define_blob(digest: SHA1.Digest): State = copy(blobs = blobs + digest)
wenzelm@54519
   511
    def defined_blob(digest: SHA1.Digest): Boolean = blobs.contains(digest)
wenzelm@54519
   512
wenzelm@38373
   513
    def define_command(command: Command): State =
wenzelm@38373
   514
    {
wenzelm@38373
   515
      val id = command.id
wenzelm@49414
   516
      copy(commands = commands + (id -> command.init_state))
wenzelm@38373
   517
    }
wenzelm@38373
   518
wenzelm@52530
   519
    def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   520
wenzelm@52531
   521
    def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] =
wenzelm@44582
   522
      commands.get(id) orElse execs.get(id) match {
wenzelm@44582
   523
        case None => None
wenzelm@44582
   524
        case Some(st) =>
wenzelm@44582
   525
          val command = st.command
wenzelm@46681
   526
          val node = version.nodes(command.node_name)
wenzelm@54328
   527
          if (node.commands.contains(command)) Some((node, command)) else None
wenzelm@44582
   528
      }
wenzelm@38373
   529
wenzelm@52530
   530
    def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail)
wenzelm@52564
   531
    def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail)
wenzelm@52564
   532
    def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail)
wenzelm@52563
   533
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail)
wenzelm@38370
   534
wenzelm@56470
   535
    private def self_id(st: Command.State)(id: Document_ID.Generic): Boolean =
wenzelm@56295
   536
      id == st.command.id ||
wenzelm@56295
   537
      (execs.get(id) match { case Some(st1) => st1.command.id == st.command.id case None => false })
wenzelm@56295
   538
wenzelm@56514
   539
    private def other_id(id: Document_ID.Generic): Option[(Text.Chunk.Id, Text.Chunk)] = None
wenzelm@56514
   540
    /* FIXME
wenzelm@56470
   541
      (execs.get(id) orElse commands.get(id)).map(st =>
wenzelm@56470
   542
        ((Text.Chunk.Id(st.command.id), st.command.chunk)))
wenzelm@56514
   543
    */
wenzelm@56470
   544
wenzelm@56475
   545
    private def redirection(st: Command.State): Graph[Document_ID.Command, Unit] =
wenzelm@56475
   546
      (commands_redirection /: st.markups.redirection_iterator)({ case (graph, id) =>
wenzelm@56470
   547
        graph.default_node(id, ()).default_node(st.command.id, ()).add_edge(id, st.command.id) })
wenzelm@56470
   548
wenzelm@52531
   549
    def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) =
wenzelm@56470
   550
    {
wenzelm@38370
   551
      execs.get(id) match {
wenzelm@44446
   552
        case Some(st) =>
wenzelm@56470
   553
          val new_st = st.accumulate(self_id(st), other_id _, message)
wenzelm@56470
   554
          val execs1 = execs + (id -> new_st)
wenzelm@56475
   555
          (new_st, copy(execs = execs1, commands_redirection = redirection(new_st)))
wenzelm@38370
   556
        case None =>
wenzelm@38370
   557
          commands.get(id) match {
wenzelm@38370
   558
            case Some(st) =>
wenzelm@56470
   559
              val new_st = st.accumulate(self_id(st), other_id _, message)
wenzelm@56470
   560
              val commands1 = commands + (id -> new_st)
wenzelm@56475
   561
              (new_st, copy(commands = commands1, commands_redirection = redirection(new_st)))
wenzelm@38370
   562
            case None => fail
wenzelm@38370
   563
          }
wenzelm@38370
   564
      }
wenzelm@56470
   565
    }
wenzelm@38370
   566
wenzelm@52563
   567
    def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) =
wenzelm@38370
   568
    {
wenzelm@38417
   569
      val version = the_version(id)
wenzelm@38370
   570
wenzelm@52566
   571
      def upd(exec_id: Document_ID.Exec, st: Command.State)
wenzelm@52566
   572
          : Option[(Document_ID.Exec, Command.State)] =
wenzelm@52566
   573
        if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st)
wenzelm@52566
   574
wenzelm@44543
   575
      val (changed_commands, new_execs) =
wenzelm@52563
   576
        ((Nil: List[Command], execs) /: update) {
wenzelm@52566
   577
          case ((commands1, execs1), (command_id, exec)) =>
wenzelm@52566
   578
            val st = the_static_state(command_id)
wenzelm@52566
   579
            val command = st.command
wenzelm@52527
   580
            val commands2 = command :: commands1
wenzelm@44543
   581
            val execs2 =
wenzelm@44543
   582
              exec match {
wenzelm@52527
   583
                case Nil => execs1
wenzelm@52527
   584
                case eval_id :: print_ids =>
wenzelm@52566
   585
                  execs1 ++ upd(eval_id, st) ++
wenzelm@52566
   586
                    (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up)
wenzelm@44543
   587
              }
wenzelm@44543
   588
            (commands2, execs2)
wenzelm@38370
   589
        }
wenzelm@52563
   590
      val new_assignment = the_assignment(version).assign(update)
wenzelm@43722
   591
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   592
wenzelm@44543
   593
      (changed_commands, new_state)
wenzelm@38370
   594
    }
wenzelm@38370
   595
wenzelm@38417
   596
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   597
      assignments.get(version.id) match {
wenzelm@38370
   598
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   599
        case None => false
wenzelm@38370
   600
      }
wenzelm@38370
   601
wenzelm@44436
   602
    def is_stable(change: Change): Boolean =
wenzelm@44436
   603
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   604
wenzelm@46944
   605
    def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
wenzelm@44672
   606
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
wenzelm@44436
   607
    def tip_stable: Boolean = is_stable(history.tip)
wenzelm@44475
   608
    def tip_version: Version = history.tip.version.get_finished
wenzelm@44436
   609
wenzelm@44446
   610
    def continue_history(
wenzelm@44446
   611
        previous: Future[Version],
wenzelm@40479
   612
        edits: List[Edit_Text],
wenzelm@43722
   613
        version: Future[Version]): (Change, State) =
wenzelm@38841
   614
    {
wenzelm@46678
   615
      val change = Change.make(previous, edits, version)
wenzelm@38841
   616
      (change, copy(history = history + change))
wenzelm@38841
   617
    }
wenzelm@38841
   618
wenzelm@44672
   619
    def prune_history(retain: Int = 0): (List[Version], State) =
wenzelm@44672
   620
    {
wenzelm@46679
   621
      history.prune(is_stable, retain) match {
wenzelm@46679
   622
        case Some((dropped, history1)) =>
wenzelm@44672
   623
          val dropped_versions = dropped.map(change => change.version.get_finished)
wenzelm@46679
   624
          val state1 = copy(history = history1)
wenzelm@44672
   625
          (dropped_versions, state1)
wenzelm@46679
   626
        case None => fail
wenzelm@44672
   627
      }
wenzelm@44672
   628
    }
wenzelm@44672
   629
wenzelm@52530
   630
    def removed_versions(removed: List[Document_ID.Version]): State =
wenzelm@44676
   631
    {
wenzelm@44676
   632
      val versions1 = versions -- removed
wenzelm@44676
   633
      val assignments1 = assignments -- removed
wenzelm@54519
   634
      var blobs1 = Set.empty[SHA1.Digest]
wenzelm@52530
   635
      var commands1 = Map.empty[Document_ID.Command, Command.State]
wenzelm@52530
   636
      var execs1 = Map.empty[Document_ID.Exec, Command.State]
wenzelm@44676
   637
      for {
wenzelm@44676
   638
        (version_id, version) <- versions1.iterator
wenzelm@46997
   639
        command_execs = assignments1(version_id).command_execs
wenzelm@56372
   640
        (_, node) <- version.nodes.iterator
wenzelm@44676
   641
        command <- node.commands.iterator
wenzelm@44676
   642
      } {
wenzelm@54519
   643
        for (digest <- command.blobs_digests; if !blobs1.contains(digest))
wenzelm@54519
   644
          blobs1 += digest
wenzelm@54519
   645
wenzelm@52568
   646
        if (!commands1.isDefinedAt(command.id))
wenzelm@52568
   647
          commands.get(command.id).foreach(st => commands1 += (command.id -> st))
wenzelm@54519
   648
wenzelm@52568
   649
        for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
wenzelm@52527
   650
          if (!execs1.isDefinedAt(exec_id))
wenzelm@52527
   651
            execs.get(exec_id).foreach(st => execs1 += (exec_id -> st))
wenzelm@44676
   652
        }
wenzelm@44676
   653
      }
wenzelm@56470
   654
      copy(
wenzelm@56470
   655
        versions = versions1,
wenzelm@56470
   656
        blobs = blobs1,
wenzelm@56470
   657
        commands = commands1,
wenzelm@56470
   658
        execs = execs1,
wenzelm@56475
   659
        commands_redirection = commands_redirection.restrict(commands1.isDefinedAt(_)),
wenzelm@54519
   660
        assignments = assignments1)
wenzelm@44676
   661
    }
wenzelm@44676
   662
wenzelm@56470
   663
    private def command_states_self(version: Version, command: Command)
wenzelm@56470
   664
      : List[(Document_ID.Generic, Command.State)] =
wenzelm@44613
   665
    {
wenzelm@44613
   666
      require(is_assigned(version))
wenzelm@44613
   667
      try {
wenzelm@52527
   668
        the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil)
wenzelm@56470
   669
          .map(id => id -> the_dynamic_state(id)) match {
wenzelm@52527
   670
            case Nil => fail
wenzelm@56470
   671
            case res => res
wenzelm@52527
   672
          }
wenzelm@44613
   673
      }
wenzelm@47395
   674
      catch {
wenzelm@47395
   675
        case _: State.Fail =>
wenzelm@56470
   676
          try { List(command.id -> the_static_state(command.id)) }
wenzelm@56470
   677
          catch { case _: State.Fail => List(command.id -> command.init_state) }
wenzelm@47395
   678
      }
wenzelm@44613
   679
    }
wenzelm@44613
   680
wenzelm@56470
   681
    def command_states(version: Version, command: Command): List[Command.State] =
wenzelm@56470
   682
    {
wenzelm@56470
   683
      val self = command_states_self(version, command)
wenzelm@56470
   684
      val others =
wenzelm@56475
   685
        if (commands_redirection.defined(command.id)) {
wenzelm@56470
   686
          (for {
wenzelm@56475
   687
            command_id <- commands_redirection.imm_succs(command.id).iterator
wenzelm@56470
   688
            (id, st) <- command_states_self(version, the_static_state(command_id).command)
wenzelm@56470
   689
            if !self.exists(_._1 == id)
wenzelm@56470
   690
          } yield (id, st)).toMap.valuesIterator.toList
wenzelm@56470
   691
        }
wenzelm@56470
   692
        else Nil
wenzelm@56489
   693
      self.map(_._2) ::: others.flatMap(_.redirect(command))
wenzelm@56470
   694
    }
wenzelm@56470
   695
wenzelm@56299
   696
    def command_results(version: Version, command: Command): Command.Results =
wenzelm@56299
   697
      Command.State.merge_results(command_states(version, command))
wenzelm@56299
   698
wenzelm@56301
   699
    def command_markup(version: Version, command: Command, index: Command.Markup_Index,
wenzelm@56301
   700
        range: Text.Range, elements: Elements): Markup_Tree =
wenzelm@56301
   701
      Command.State.merge_markup(command_states(version, command), index, range, elements)
wenzelm@56299
   702
wenzelm@56300
   703
    def markup_to_XML(version: Version, node: Node, elements: Elements): XML.Body =
wenzelm@56299
   704
      (for {
wenzelm@56299
   705
        command <- node.commands.iterator
wenzelm@56301
   706
        markup =
wenzelm@56301
   707
          command_markup(version, command, Command.Markup_Index.markup, command.range, elements)
wenzelm@56300
   708
        tree <- markup.to_XML(command.range, command.source, elements)
wenzelm@56299
   709
      } yield tree).toList
wenzelm@38841
   710
wenzelm@38841
   711
    // persistent user-view
wenzelm@49346
   712
    def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
wenzelm@49346
   713
      : Snapshot =
wenzelm@38370
   714
    {
wenzelm@44672
   715
      val stable = recent_stable
wenzelm@44436
   716
      val latest = history.tip
wenzelm@38841
   717
wenzelm@44156
   718
      // FIXME proper treatment of removed nodes
wenzelm@38841
   719
      val edits =
wenzelm@38841
   720
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@44156
   721
            (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
wenzelm@38841
   722
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   723
wenzelm@38841
   724
      new Snapshot
wenzelm@38841
   725
      {
wenzelm@52972
   726
        /* global information */
wenzelm@52972
   727
wenzelm@44582
   728
        val state = State.this
wenzelm@39698
   729
        val version = stable.version.get_finished
wenzelm@38841
   730
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38841
   731
wenzelm@55649
   732
wenzelm@55649
   733
        /* local node content */
wenzelm@55649
   734
wenzelm@38841
   735
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   736
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@43425
   737
        def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
wenzelm@43425
   738
        def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
wenzelm@39177
   739
wenzelm@52972
   740
        val node_name = name
wenzelm@52972
   741
        val node = version.nodes(name)
wenzelm@52972
   742
wenzelm@56314
   743
        val load_commands: List[Command] =
wenzelm@55432
   744
          if (node_name.is_theory) Nil
wenzelm@56314
   745
          else version.nodes.load_commands(node_name)
wenzelm@55432
   746
wenzelm@56314
   747
        val is_loaded: Boolean = node_name.is_theory || !load_commands.isEmpty
wenzelm@56176
   748
wenzelm@51494
   749
        def eq_content(other: Snapshot): Boolean =
wenzelm@55434
   750
        {
wenzelm@55434
   751
          def eq_commands(commands: (Command, Command)): Boolean =
wenzelm@56299
   752
          {
wenzelm@56299
   753
            val states1 = state.command_states(version, commands._1)
wenzelm@56299
   754
            val states2 = other.state.command_states(other.version, commands._2)
wenzelm@56299
   755
            states1.length == states2.length &&
wenzelm@56299
   756
            (states1 zip states2).forall({ case (st1, st2) => st1 eq_content st2 })
wenzelm@56299
   757
          }
wenzelm@55434
   758
wenzelm@49346
   759
          !is_outdated && !other.is_outdated &&
wenzelm@55434
   760
          node.commands.size == other.node.commands.size &&
wenzelm@55434
   761
          (node.commands.iterator zip other.node.commands.iterator).forall(eq_commands) &&
wenzelm@56314
   762
          load_commands.length == other.load_commands.length &&
wenzelm@56314
   763
          (load_commands zip other.load_commands).forall(eq_commands)
wenzelm@55434
   764
        }
wenzelm@49346
   765
wenzelm@55649
   766
wenzelm@55649
   767
        /* cumulate markup */
wenzelm@55649
   768
wenzelm@55651
   769
        def cumulate[A](
wenzelm@55649
   770
          range: Text.Range,
wenzelm@55649
   771
          info: A,
wenzelm@55820
   772
          elements: Elements,
wenzelm@56354
   773
          result: List[Command.State] => (A, Text.Markup) => Option[A],
wenzelm@55651
   774
          status: Boolean = false): List[Text.Info[A]] =
wenzelm@45459
   775
        {
wenzelm@56590
   776
          val former_range = revert(range).inflate_singularity
wenzelm@56462
   777
          val (chunk_name, command_iterator) =
wenzelm@56314
   778
            load_commands match {
wenzelm@56473
   779
              case command :: _ => (Text.Chunk.File(node_name.node), Iterator((command, 0)))
wenzelm@56468
   780
              case _ => (Text.Chunk.Default, node.command_iterator(former_range))
wenzelm@56309
   781
            }
wenzelm@56462
   782
          val markup_index = Command.Markup_Index(status, chunk_name)
wenzelm@56309
   783
          (for {
wenzelm@56373
   784
            (command, command_start) <- command_iterator
wenzelm@56462
   785
            chunk <- command.chunks.get(chunk_name).iterator
wenzelm@56309
   786
            states = state.command_states(version, command)
wenzelm@56354
   787
            res = result(states)
wenzelm@56590
   788
            markup_range <- (former_range - command_start).try_restrict(chunk.range).iterator
wenzelm@56590
   789
            markup = Command.State.merge_markup(states, markup_index, markup_range, elements)
wenzelm@56590
   790
            Text.Info(r0, a) <- markup.cumulate[A](markup_range, info, elements,
wenzelm@56309
   791
              {
wenzelm@56309
   792
                case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b))
wenzelm@56309
   793
              }).iterator
wenzelm@56590
   794
            r1 <- convert(r0 + command_start).try_restrict(range).iterator
wenzelm@56590
   795
          } yield Text.Info(r1, a)).toList
wenzelm@45459
   796
        }
wenzelm@45459
   797
wenzelm@55651
   798
        def select[A](
wenzelm@55651
   799
          range: Text.Range,
wenzelm@55820
   800
          elements: Elements,
wenzelm@56354
   801
          result: List[Command.State] => Text.Markup => Option[A],
wenzelm@55651
   802
          status: Boolean = false): List[Text.Info[A]] =
wenzelm@38845
   803
        {
wenzelm@56354
   804
          def result1(states: List[Command.State]): (Option[A], Text.Markup) => Option[Option[A]] =
wenzelm@50500
   805
          {
wenzelm@56354
   806
            val res = result(states)
wenzelm@52889
   807
            (_: Option[A], x: Text.Markup) =>
wenzelm@52889
   808
              res(x) match {
wenzelm@52889
   809
                case None => None
wenzelm@52889
   810
                case some => Some(some)
wenzelm@52889
   811
              }
wenzelm@50500
   812
          }
wenzelm@55651
   813
          for (Text.Info(r, Some(x)) <- cumulate(range, None, elements, result1 _, status))
wenzelm@46198
   814
            yield Text.Info(r, x)
wenzelm@38845
   815
        }
wenzelm@55800
   816
wenzelm@55800
   817
wenzelm@55800
   818
        /* output */
wenzelm@55800
   819
wenzelm@55800
   820
        override def toString: String =
wenzelm@55800
   821
          "Snapshot(node = " + node_name.node + ", version = " + version.id +
wenzelm@55800
   822
            (if (is_outdated) ", outdated" else "") + ")"
wenzelm@38841
   823
      }
wenzelm@38150
   824
    }
wenzelm@34485
   825
  }
wenzelm@34840
   826
}
wenzelm@34840
   827