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