src/Pure/PIDE/document.scala
author wenzelm
Tue Aug 12 18:36:43 2014 +0200 (2014-08-12)
changeset 57916 2c2c24dbf0a4
parent 57842 8e4ae2db1849
child 57976 bf99106b6672
permissions -rw-r--r--
generic process wrapping in Prover;
clarified module arrangement;
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@54509
   116
      def is_theory: Boolean = !theory.isEmpty
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@54513
   211
        commands.iterator.filter(cmd => !cmd.blobs.isEmpty).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@52901
   232
        if (!commands.isEmpty && 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@57615
   247
    val perspective: Node.Perspective_Command = Node.no_perspective_command,
wenzelm@52901
   248
    _commands: Node.Commands = Node.Commands.empty)
wenzelm@38151
   249
  {
wenzelm@57615
   250
    def is_empty: Boolean =
wenzelm@57615
   251
      get_blob.isEmpty &&
wenzelm@57615
   252
      header == Node.no_header &&
wenzelm@57615
   253
      Node.is_no_perspective_command(perspective) &&
wenzelm@57615
   254
      commands.isEmpty
wenzelm@57615
   255
wenzelm@57615
   256
    def commands: Linear_Set[Command] = _commands.commands
wenzelm@57615
   257
    def load_commands: List[Command] = _commands.load_commands
wenzelm@57615
   258
wenzelm@46680
   259
    def clear: Node = new Node(header = header)
wenzelm@46680
   260
wenzelm@56335
   261
    def init_blob(blob: Document.Blob): Node = new Node(Some(blob.unchanged))
wenzelm@55435
   262
wenzelm@48707
   263
    def update_header(new_header: Node.Header): Node =
wenzelm@56335
   264
      new Node(get_blob, new_header, perspective, _commands)
wenzelm@46680
   265
wenzelm@52849
   266
    def update_perspective(new_perspective: Node.Perspective_Command): Node =
wenzelm@56335
   267
      new Node(get_blob, header, new_perspective, _commands)
wenzelm@46680
   268
wenzelm@52849
   269
    def same_perspective(other_perspective: Node.Perspective_Command): Boolean =
wenzelm@52849
   270
      perspective.required == other_perspective.required &&
wenzelm@52849
   271
      perspective.visible.same(other_perspective.visible) &&
wenzelm@52849
   272
      perspective.overlays == other_perspective.overlays
wenzelm@52808
   273
wenzelm@52901
   274
    def update_commands(new_commands: Linear_Set[Command]): Node =
wenzelm@52918
   275
      if (new_commands eq _commands.commands) this
wenzelm@56335
   276
      else new Node(get_blob, header, perspective, Node.Commands(new_commands))
wenzelm@38151
   277
wenzelm@56373
   278
    def command_iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@56373
   279
      _commands.iterator(i)
wenzelm@38151
   280
wenzelm@56373
   281
    def command_iterator(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@56373
   282
      command_iterator(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
   283
wenzelm@38879
   284
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@52901
   285
      Node.Commands.starts(commands.iterator).find(_._1 == cmd).map(_._2)
wenzelm@38151
   286
  }
wenzelm@38151
   287
wenzelm@38151
   288
wenzelm@46723
   289
  /* development graph */
wenzelm@46723
   290
wenzelm@46723
   291
  object Nodes
wenzelm@46723
   292
  {
wenzelm@46723
   293
    val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
wenzelm@46723
   294
  }
wenzelm@46723
   295
wenzelm@46723
   296
  final class Nodes private(graph: Graph[Node.Name, Node])
wenzelm@46723
   297
  {
wenzelm@46723
   298
    def apply(name: Node.Name): Node =
wenzelm@46723
   299
      graph.default_node(name, Node.empty).get_node(name)
wenzelm@46723
   300
wenzelm@57619
   301
    def is_hidden(name: Node.Name): Boolean =
wenzelm@57619
   302
    {
wenzelm@57619
   303
      val graph1 = graph.default_node(name, Node.empty)
wenzelm@57619
   304
      graph1.is_maximal(name) && graph1.get_node(name).is_empty
wenzelm@57619
   305
    }
wenzelm@57617
   306
wenzelm@46723
   307
    def + (entry: (Node.Name, Node)): Nodes =
wenzelm@46723
   308
    {
wenzelm@46723
   309
      val (name, node) = entry
wenzelm@48707
   310
      val imports = node.header.imports
wenzelm@46723
   311
      val graph1 =
wenzelm@46739
   312
        (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty))
wenzelm@46723
   313
      val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
wenzelm@46739
   314
      val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name))
wenzelm@57616
   315
      new Nodes(
wenzelm@57616
   316
        if (graph3.is_maximal(name) && node.is_empty) graph3.del_node(name)
wenzelm@57616
   317
        else graph3.map_node(name, _ => node)
wenzelm@57616
   318
      )
wenzelm@46723
   319
    }
wenzelm@46723
   320
wenzelm@56372
   321
    def iterator: Iterator[(Node.Name, Node)] =
wenzelm@56372
   322
      graph.iterator.map({ case (name, (node, _)) => (name, node) })
wenzelm@46723
   323
wenzelm@56314
   324
    def load_commands(file_name: Node.Name): List[Command] =
wenzelm@54528
   325
      (for {
wenzelm@56372
   326
        (_, node) <- iterator
wenzelm@56314
   327
        cmd <- node.load_commands.iterator
wenzelm@54530
   328
        name <- cmd.blobs_names.iterator
wenzelm@54528
   329
        if name == file_name
wenzelm@54528
   330
      } yield cmd).toList
wenzelm@54528
   331
wenzelm@46942
   332
    def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names)
wenzelm@46723
   333
    def topological_order: List[Node.Name] = graph.topological_order
wenzelm@56337
   334
wenzelm@56337
   335
    override def toString: String = topological_order.mkString("Nodes(", ",", ")")
wenzelm@46723
   336
  }
wenzelm@46723
   337
wenzelm@46723
   338
wenzelm@38424
   339
wenzelm@38424
   340
  /** versioning **/
wenzelm@38424
   341
wenzelm@38424
   342
  /* particular document versions */
wenzelm@34485
   343
wenzelm@38417
   344
  object Version
wenzelm@38417
   345
  {
wenzelm@46681
   346
    val init: Version = new Version()
wenzelm@46681
   347
wenzelm@56394
   348
    def make(syntax: Option[Prover.Syntax], nodes: Nodes): Version =
wenzelm@52530
   349
      new Version(Document_ID.make(), syntax, nodes)
wenzelm@38417
   350
  }
wenzelm@38417
   351
wenzelm@46712
   352
  final class Version private(
wenzelm@52530
   353
    val id: Document_ID.Version = Document_ID.none,
wenzelm@56394
   354
    val syntax: Option[Prover.Syntax] = None,
wenzelm@46723
   355
    val nodes: Nodes = Nodes.empty)
wenzelm@46941
   356
  {
wenzelm@55777
   357
    override def toString: String = "Version(" + id + ")"
wenzelm@46941
   358
  }
immler@34660
   359
wenzelm@34859
   360
wenzelm@38424
   361
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   362
wenzelm@38227
   363
  object Change
wenzelm@38227
   364
  {
wenzelm@46678
   365
    val init: Change = new Change()
wenzelm@46678
   366
wenzelm@46678
   367
    def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
wenzelm@57620
   368
      new Change(Some(previous), edits.reverse, version)
wenzelm@38227
   369
  }
wenzelm@38227
   370
wenzelm@46712
   371
  final class Change private(
wenzelm@46677
   372
    val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
wenzelm@57620
   373
    val rev_edits: List[Edit_Text] = Nil,
wenzelm@46677
   374
    val version: Future[Version] = Future.value(Version.init))
wenzelm@38227
   375
  {
wenzelm@44672
   376
    def is_finished: Boolean =
wenzelm@44672
   377
      (previous match { case None => true case Some(future) => future.is_finished }) &&
wenzelm@44672
   378
      version.is_finished
wenzelm@44672
   379
wenzelm@46678
   380
    def truncate: Change = new Change(None, Nil, version)
wenzelm@38227
   381
  }
wenzelm@38227
   382
wenzelm@38227
   383
wenzelm@38841
   384
  /* history navigation */
wenzelm@38841
   385
wenzelm@38841
   386
  object History
wenzelm@38841
   387
  {
wenzelm@46679
   388
    val init: History = new History()
wenzelm@38841
   389
  }
wenzelm@38841
   390
wenzelm@46712
   391
  final class History private(
wenzelm@46679
   392
    val undo_list: List[Change] = List(Change.init))  // non-empty list
wenzelm@38841
   393
  {
wenzelm@46679
   394
    def tip: Change = undo_list.head
wenzelm@46679
   395
    def + (change: Change): History = new History(change :: undo_list)
wenzelm@38841
   396
wenzelm@46679
   397
    def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
wenzelm@46679
   398
    {
wenzelm@46679
   399
      val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
wenzelm@46679
   400
      val (retained, dropped) = undo_list.splitAt(n max retain)
wenzelm@46679
   401
wenzelm@46679
   402
      retained.splitAt(retained.length - 1) match {
wenzelm@46679
   403
        case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
wenzelm@46679
   404
        case _ => None
wenzelm@46679
   405
      }
wenzelm@46679
   406
    }
wenzelm@38841
   407
  }
wenzelm@38841
   408
wenzelm@38841
   409
wenzelm@55820
   410
  /* snapshot */
wenzelm@38424
   411
wenzelm@52972
   412
  object Snapshot
wenzelm@52972
   413
  {
wenzelm@52972
   414
    val init = State.init.snapshot()
wenzelm@52972
   415
  }
wenzelm@52972
   416
wenzelm@38424
   417
  abstract class Snapshot
wenzelm@38424
   418
  {
wenzelm@44582
   419
    val state: State
wenzelm@38424
   420
    val version: Version
wenzelm@38424
   421
    val is_outdated: Boolean
wenzelm@55649
   422
wenzelm@38427
   423
    def convert(i: Text.Offset): Text.Offset
wenzelm@52889
   424
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   425
    def convert(range: Text.Range): Text.Range
wenzelm@39177
   426
    def revert(range: Text.Range): Text.Range
wenzelm@52972
   427
wenzelm@52972
   428
    val node_name: Node.Name
wenzelm@52972
   429
    val node: Node
wenzelm@56314
   430
    val load_commands: List[Command]
wenzelm@56176
   431
    def is_loaded: Boolean
wenzelm@51494
   432
    def eq_content(other: Snapshot): Boolean
wenzelm@55649
   433
wenzelm@55651
   434
    def cumulate[A](
wenzelm@55649
   435
      range: Text.Range,
wenzelm@55649
   436
      info: A,
wenzelm@56743
   437
      elements: Markup.Elements,
wenzelm@56354
   438
      result: List[Command.State] => (A, Text.Markup) => Option[A],
wenzelm@55651
   439
      status: Boolean = false): List[Text.Info[A]]
wenzelm@55651
   440
wenzelm@55651
   441
    def select[A](
wenzelm@55649
   442
      range: Text.Range,
wenzelm@56743
   443
      elements: Markup.Elements,
wenzelm@56354
   444
      result: List[Command.State] => Text.Markup => Option[A],
wenzelm@55651
   445
      status: Boolean = false): List[Text.Info[A]]
wenzelm@38424
   446
  }
wenzelm@38424
   447
wenzelm@55820
   448
wenzelm@55820
   449
wenzelm@55820
   450
  /** global state -- document structure, execution process, editing history **/
wenzelm@55820
   451
wenzelm@52563
   452
  type Assign_Update =
wenzelm@52563
   453
    List[(Document_ID.Command, List[Document_ID.Exec])]  // update of exec state assignment
wenzelm@44476
   454
wenzelm@38370
   455
  object State
wenzelm@38370
   456
  {
wenzelm@38373
   457
    class Fail(state: State) extends Exception
wenzelm@38373
   458
wenzelm@44479
   459
    object Assignment
wenzelm@44479
   460
    {
wenzelm@46683
   461
      val init: Assignment = new Assignment()
wenzelm@44479
   462
    }
wenzelm@38370
   463
wenzelm@46712
   464
    final class Assignment private(
wenzelm@52530
   465
      val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty,
wenzelm@46677
   466
      val is_finished: Boolean = false)
wenzelm@38370
   467
    {
wenzelm@44477
   468
      def check_finished: Assignment = { require(is_finished); this }
wenzelm@47388
   469
      def unfinished: Assignment = new Assignment(command_execs, false)
wenzelm@44479
   470
wenzelm@52563
   471
      def assign(update: Assign_Update): Assignment =
wenzelm@38370
   472
      {
wenzelm@38976
   473
        require(!is_finished)
wenzelm@44479
   474
        val command_execs1 =
wenzelm@52563
   475
          (command_execs /: update) {
wenzelm@52563
   476
            case (res, (command_id, exec_ids)) =>
wenzelm@52563
   477
              if (exec_ids.isEmpty) res - command_id
wenzelm@52563
   478
              else res + (command_id -> exec_ids)
wenzelm@44479
   479
          }
wenzelm@47388
   480
        new Assignment(command_execs1, true)
wenzelm@38150
   481
      }
wenzelm@38370
   482
    }
wenzelm@46682
   483
wenzelm@46682
   484
    val init: State =
wenzelm@52563
   485
      State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2
wenzelm@38370
   486
  }
wenzelm@38370
   487
wenzelm@46712
   488
  final case class State private(
wenzelm@56470
   489
    /*reachable versions*/
wenzelm@52530
   490
    val versions: Map[Document_ID.Version, Version] = Map.empty,
wenzelm@56470
   491
    /*inlined auxiliary files*/
wenzelm@56470
   492
    val blobs: Set[SHA1.Digest] = Set.empty,
wenzelm@56470
   493
    /*static markup from define_command*/
wenzelm@56470
   494
    val commands: Map[Document_ID.Command, Command.State] = Map.empty,
wenzelm@56470
   495
    /*dynamic markup from execution*/
wenzelm@56470
   496
    val execs: Map[Document_ID.Exec, Command.State] = Map.empty,
wenzelm@56470
   497
    /*command-exec assignment for each version*/
wenzelm@52530
   498
    val assignments: Map[Document_ID.Version, State.Assignment] = Map.empty,
wenzelm@56470
   499
    /*commands with markup produced by other commands (imm_succs)*/
wenzelm@56475
   500
    val commands_redirection: Graph[Document_ID.Command, Unit] = Graph.long,
wenzelm@56470
   501
    /*explicit (linear) history*/
wenzelm@38841
   502
    val history: History = History.init)
wenzelm@38370
   503
  {
wenzelm@38373
   504
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   505
wenzelm@44479
   506
    def define_version(version: Version, assignment: State.Assignment): State =
wenzelm@38370
   507
    {
wenzelm@38417
   508
      val id = version.id
wenzelm@38417
   509
      copy(versions = versions + (id -> version),
wenzelm@44479
   510
        assignments = assignments + (id -> assignment.unfinished))
wenzelm@38370
   511
    }
wenzelm@38370
   512
wenzelm@54519
   513
    def define_blob(digest: SHA1.Digest): State = copy(blobs = blobs + digest)
wenzelm@54519
   514
    def defined_blob(digest: SHA1.Digest): Boolean = blobs.contains(digest)
wenzelm@54519
   515
wenzelm@38373
   516
    def define_command(command: Command): State =
wenzelm@38373
   517
    {
wenzelm@38373
   518
      val id = command.id
wenzelm@49414
   519
      copy(commands = commands + (id -> command.init_state))
wenzelm@38373
   520
    }
wenzelm@38373
   521
wenzelm@52530
   522
    def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   523
wenzelm@52531
   524
    def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] =
wenzelm@44582
   525
      commands.get(id) orElse execs.get(id) match {
wenzelm@44582
   526
        case None => None
wenzelm@44582
   527
        case Some(st) =>
wenzelm@44582
   528
          val command = st.command
wenzelm@46681
   529
          val node = version.nodes(command.node_name)
wenzelm@54328
   530
          if (node.commands.contains(command)) Some((node, command)) else None
wenzelm@44582
   531
      }
wenzelm@38373
   532
wenzelm@52530
   533
    def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail)
wenzelm@52564
   534
    def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail)
wenzelm@52564
   535
    def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail)
wenzelm@52563
   536
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail)
wenzelm@38370
   537
wenzelm@56470
   538
    private def self_id(st: Command.State)(id: Document_ID.Generic): Boolean =
wenzelm@56295
   539
      id == st.command.id ||
wenzelm@56295
   540
      (execs.get(id) match { case Some(st1) => st1.command.id == st.command.id case None => false })
wenzelm@56295
   541
wenzelm@56746
   542
    private def other_id(id: Document_ID.Generic)
wenzelm@56746
   543
      : Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)] = None
wenzelm@56514
   544
    /* FIXME
wenzelm@56470
   545
      (execs.get(id) orElse commands.get(id)).map(st =>
wenzelm@56746
   546
        ((Symbol.Text_Chunk.Id(st.command.id), st.command.chunk)))
wenzelm@56514
   547
    */
wenzelm@56470
   548
wenzelm@56475
   549
    private def redirection(st: Command.State): Graph[Document_ID.Command, Unit] =
wenzelm@56475
   550
      (commands_redirection /: st.markups.redirection_iterator)({ case (graph, id) =>
wenzelm@56470
   551
        graph.default_node(id, ()).default_node(st.command.id, ()).add_edge(id, st.command.id) })
wenzelm@56470
   552
wenzelm@52531
   553
    def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) =
wenzelm@56470
   554
    {
wenzelm@38370
   555
      execs.get(id) match {
wenzelm@44446
   556
        case Some(st) =>
wenzelm@56470
   557
          val new_st = st.accumulate(self_id(st), other_id _, message)
wenzelm@56470
   558
          val execs1 = execs + (id -> new_st)
wenzelm@56475
   559
          (new_st, copy(execs = execs1, commands_redirection = redirection(new_st)))
wenzelm@38370
   560
        case None =>
wenzelm@38370
   561
          commands.get(id) match {
wenzelm@38370
   562
            case Some(st) =>
wenzelm@56470
   563
              val new_st = st.accumulate(self_id(st), other_id _, message)
wenzelm@56470
   564
              val commands1 = commands + (id -> new_st)
wenzelm@56475
   565
              (new_st, copy(commands = commands1, commands_redirection = redirection(new_st)))
wenzelm@38370
   566
            case None => fail
wenzelm@38370
   567
          }
wenzelm@38370
   568
      }
wenzelm@56470
   569
    }
wenzelm@38370
   570
wenzelm@52563
   571
    def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) =
wenzelm@38370
   572
    {
wenzelm@38417
   573
      val version = the_version(id)
wenzelm@38370
   574
wenzelm@52566
   575
      def upd(exec_id: Document_ID.Exec, st: Command.State)
wenzelm@52566
   576
          : Option[(Document_ID.Exec, Command.State)] =
wenzelm@52566
   577
        if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st)
wenzelm@52566
   578
wenzelm@44543
   579
      val (changed_commands, new_execs) =
wenzelm@52563
   580
        ((Nil: List[Command], execs) /: update) {
wenzelm@52566
   581
          case ((commands1, execs1), (command_id, exec)) =>
wenzelm@52566
   582
            val st = the_static_state(command_id)
wenzelm@52566
   583
            val command = st.command
wenzelm@52527
   584
            val commands2 = command :: commands1
wenzelm@44543
   585
            val execs2 =
wenzelm@44543
   586
              exec match {
wenzelm@52527
   587
                case Nil => execs1
wenzelm@52527
   588
                case eval_id :: print_ids =>
wenzelm@52566
   589
                  execs1 ++ upd(eval_id, st) ++
wenzelm@52566
   590
                    (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up)
wenzelm@44543
   591
              }
wenzelm@44543
   592
            (commands2, execs2)
wenzelm@38370
   593
        }
wenzelm@52563
   594
      val new_assignment = the_assignment(version).assign(update)
wenzelm@43722
   595
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   596
wenzelm@44543
   597
      (changed_commands, new_state)
wenzelm@38370
   598
    }
wenzelm@38370
   599
wenzelm@38417
   600
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   601
      assignments.get(version.id) match {
wenzelm@38370
   602
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   603
        case None => false
wenzelm@38370
   604
      }
wenzelm@38370
   605
wenzelm@44436
   606
    def is_stable(change: Change): Boolean =
wenzelm@44436
   607
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   608
wenzelm@46944
   609
    def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
wenzelm@44672
   610
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
wenzelm@44436
   611
    def tip_stable: Boolean = is_stable(history.tip)
wenzelm@44475
   612
    def tip_version: Version = history.tip.version.get_finished
wenzelm@44436
   613
wenzelm@44446
   614
    def continue_history(
wenzelm@56711
   615
      previous: Future[Version],
wenzelm@56711
   616
      edits: List[Edit_Text],
wenzelm@56711
   617
      version: Future[Version]): State =
wenzelm@38841
   618
    {
wenzelm@46678
   619
      val change = Change.make(previous, edits, version)
wenzelm@56711
   620
      copy(history = history + change)
wenzelm@38841
   621
    }
wenzelm@38841
   622
wenzelm@44672
   623
    def prune_history(retain: Int = 0): (List[Version], State) =
wenzelm@44672
   624
    {
wenzelm@46679
   625
      history.prune(is_stable, retain) match {
wenzelm@46679
   626
        case Some((dropped, history1)) =>
wenzelm@44672
   627
          val dropped_versions = dropped.map(change => change.version.get_finished)
wenzelm@46679
   628
          val state1 = copy(history = history1)
wenzelm@44672
   629
          (dropped_versions, state1)
wenzelm@46679
   630
        case None => fail
wenzelm@44672
   631
      }
wenzelm@44672
   632
    }
wenzelm@44672
   633
wenzelm@52530
   634
    def removed_versions(removed: List[Document_ID.Version]): State =
wenzelm@44676
   635
    {
wenzelm@44676
   636
      val versions1 = versions -- removed
wenzelm@44676
   637
      val assignments1 = assignments -- removed
wenzelm@54519
   638
      var blobs1 = Set.empty[SHA1.Digest]
wenzelm@52530
   639
      var commands1 = Map.empty[Document_ID.Command, Command.State]
wenzelm@52530
   640
      var execs1 = Map.empty[Document_ID.Exec, Command.State]
wenzelm@44676
   641
      for {
wenzelm@44676
   642
        (version_id, version) <- versions1.iterator
wenzelm@46997
   643
        command_execs = assignments1(version_id).command_execs
wenzelm@56372
   644
        (_, node) <- version.nodes.iterator
wenzelm@44676
   645
        command <- node.commands.iterator
wenzelm@44676
   646
      } {
wenzelm@57842
   647
        for ((_, digest) <- command.blobs_defined; if !blobs1.contains(digest))
wenzelm@54519
   648
          blobs1 += digest
wenzelm@54519
   649
wenzelm@52568
   650
        if (!commands1.isDefinedAt(command.id))
wenzelm@52568
   651
          commands.get(command.id).foreach(st => commands1 += (command.id -> st))
wenzelm@54519
   652
wenzelm@52568
   653
        for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
wenzelm@52527
   654
          if (!execs1.isDefinedAt(exec_id))
wenzelm@52527
   655
            execs.get(exec_id).foreach(st => execs1 += (exec_id -> st))
wenzelm@44676
   656
        }
wenzelm@44676
   657
      }
wenzelm@56470
   658
      copy(
wenzelm@56470
   659
        versions = versions1,
wenzelm@56470
   660
        blobs = blobs1,
wenzelm@56470
   661
        commands = commands1,
wenzelm@56470
   662
        execs = execs1,
wenzelm@56475
   663
        commands_redirection = commands_redirection.restrict(commands1.isDefinedAt(_)),
wenzelm@54519
   664
        assignments = assignments1)
wenzelm@44676
   665
    }
wenzelm@44676
   666
wenzelm@56470
   667
    private def command_states_self(version: Version, command: Command)
wenzelm@56470
   668
      : List[(Document_ID.Generic, Command.State)] =
wenzelm@44613
   669
    {
wenzelm@44613
   670
      require(is_assigned(version))
wenzelm@44613
   671
      try {
wenzelm@52527
   672
        the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil)
wenzelm@56470
   673
          .map(id => id -> the_dynamic_state(id)) match {
wenzelm@52527
   674
            case Nil => fail
wenzelm@56470
   675
            case res => res
wenzelm@52527
   676
          }
wenzelm@44613
   677
      }
wenzelm@47395
   678
      catch {
wenzelm@47395
   679
        case _: State.Fail =>
wenzelm@56470
   680
          try { List(command.id -> the_static_state(command.id)) }
wenzelm@56470
   681
          catch { case _: State.Fail => List(command.id -> command.init_state) }
wenzelm@47395
   682
      }
wenzelm@44613
   683
    }
wenzelm@44613
   684
wenzelm@56470
   685
    def command_states(version: Version, command: Command): List[Command.State] =
wenzelm@56470
   686
    {
wenzelm@56470
   687
      val self = command_states_self(version, command)
wenzelm@56470
   688
      val others =
wenzelm@56475
   689
        if (commands_redirection.defined(command.id)) {
wenzelm@56470
   690
          (for {
wenzelm@56475
   691
            command_id <- commands_redirection.imm_succs(command.id).iterator
wenzelm@56470
   692
            (id, st) <- command_states_self(version, the_static_state(command_id).command)
wenzelm@56470
   693
            if !self.exists(_._1 == id)
wenzelm@56470
   694
          } yield (id, st)).toMap.valuesIterator.toList
wenzelm@56470
   695
        }
wenzelm@56470
   696
        else Nil
wenzelm@56489
   697
      self.map(_._2) ::: others.flatMap(_.redirect(command))
wenzelm@56470
   698
    }
wenzelm@56470
   699
wenzelm@56299
   700
    def command_results(version: Version, command: Command): Command.Results =
wenzelm@56299
   701
      Command.State.merge_results(command_states(version, command))
wenzelm@56299
   702
wenzelm@56301
   703
    def command_markup(version: Version, command: Command, index: Command.Markup_Index,
wenzelm@56743
   704
        range: Text.Range, elements: Markup.Elements): Markup_Tree =
wenzelm@56301
   705
      Command.State.merge_markup(command_states(version, command), index, range, elements)
wenzelm@56299
   706
wenzelm@56743
   707
    def markup_to_XML(version: Version, node: Node, elements: Markup.Elements): XML.Body =
wenzelm@56299
   708
      (for {
wenzelm@56299
   709
        command <- node.commands.iterator
wenzelm@56301
   710
        markup =
wenzelm@56301
   711
          command_markup(version, command, Command.Markup_Index.markup, command.range, elements)
wenzelm@56300
   712
        tree <- markup.to_XML(command.range, command.source, elements)
wenzelm@56299
   713
      } yield tree).toList
wenzelm@38841
   714
wenzelm@38841
   715
    // persistent user-view
wenzelm@49346
   716
    def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
wenzelm@49346
   717
      : Snapshot =
wenzelm@38370
   718
    {
wenzelm@44672
   719
      val stable = recent_stable
wenzelm@44436
   720
      val latest = history.tip
wenzelm@38841
   721
wenzelm@57620
   722
wenzelm@57620
   723
      /* pending edits and unstable changes */
wenzelm@57620
   724
wenzelm@57620
   725
      val rev_pending_changes =
wenzelm@57620
   726
        for {
wenzelm@57620
   727
          change <- history.undo_list.takeWhile(_ != stable)
wenzelm@57620
   728
          (a, edits) <- change.rev_edits
wenzelm@57620
   729
          if a == name
wenzelm@57620
   730
        } yield edits
wenzelm@57620
   731
wenzelm@57620
   732
      val is_cleared = rev_pending_changes.exists({ case Node.Clear() => true case _ => false })
wenzelm@38841
   733
      val edits =
wenzelm@57620
   734
        if (is_cleared) Nil
wenzelm@57620
   735
        else
wenzelm@57620
   736
          (pending_edits /: rev_pending_changes)({
wenzelm@57620
   737
            case (edits, Node.Edits(es)) => es ::: edits
wenzelm@57620
   738
            case (edits, _) => edits
wenzelm@57620
   739
          })
wenzelm@38841
   740
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   741
wenzelm@38841
   742
      new Snapshot
wenzelm@38841
   743
      {
wenzelm@52972
   744
        /* global information */
wenzelm@52972
   745
wenzelm@44582
   746
        val state = State.this
wenzelm@39698
   747
        val version = stable.version.get_finished
wenzelm@38841
   748
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38841
   749
wenzelm@55649
   750
wenzelm@55649
   751
        /* local node content */
wenzelm@55649
   752
wenzelm@57620
   753
        def convert(offset: Text.Offset) =
wenzelm@57620
   754
          if (is_cleared) 0 else (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@57620
   755
        def revert(offset: Text.Offset) =
wenzelm@57620
   756
          if (is_cleared) 0 else (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@57620
   757
wenzelm@57620
   758
        def convert(range: Text.Range) = range.map(convert(_))
wenzelm@57620
   759
        def revert(range: Text.Range) = range.map(revert(_))
wenzelm@39177
   760
wenzelm@52972
   761
        val node_name = name
wenzelm@52972
   762
        val node = version.nodes(name)
wenzelm@52972
   763
wenzelm@56314
   764
        val load_commands: List[Command] =
wenzelm@55432
   765
          if (node_name.is_theory) Nil
wenzelm@56314
   766
          else version.nodes.load_commands(node_name)
wenzelm@55432
   767
wenzelm@56314
   768
        val is_loaded: Boolean = node_name.is_theory || !load_commands.isEmpty
wenzelm@56176
   769
wenzelm@51494
   770
        def eq_content(other: Snapshot): Boolean =
wenzelm@55434
   771
        {
wenzelm@55434
   772
          def eq_commands(commands: (Command, Command)): Boolean =
wenzelm@56299
   773
          {
wenzelm@56299
   774
            val states1 = state.command_states(version, commands._1)
wenzelm@56299
   775
            val states2 = other.state.command_states(other.version, commands._2)
wenzelm@56299
   776
            states1.length == states2.length &&
wenzelm@56299
   777
            (states1 zip states2).forall({ case (st1, st2) => st1 eq_content st2 })
wenzelm@56299
   778
          }
wenzelm@55434
   779
wenzelm@49346
   780
          !is_outdated && !other.is_outdated &&
wenzelm@55434
   781
          node.commands.size == other.node.commands.size &&
wenzelm@55434
   782
          (node.commands.iterator zip other.node.commands.iterator).forall(eq_commands) &&
wenzelm@56314
   783
          load_commands.length == other.load_commands.length &&
wenzelm@56314
   784
          (load_commands zip other.load_commands).forall(eq_commands)
wenzelm@55434
   785
        }
wenzelm@49346
   786
wenzelm@55649
   787
wenzelm@55649
   788
        /* cumulate markup */
wenzelm@55649
   789
wenzelm@55651
   790
        def cumulate[A](
wenzelm@55649
   791
          range: Text.Range,
wenzelm@55649
   792
          info: A,
wenzelm@56743
   793
          elements: Markup.Elements,
wenzelm@56354
   794
          result: List[Command.State] => (A, Text.Markup) => Option[A],
wenzelm@55651
   795
          status: Boolean = false): List[Text.Info[A]] =
wenzelm@45459
   796
        {
wenzelm@56590
   797
          val former_range = revert(range).inflate_singularity
wenzelm@56462
   798
          val (chunk_name, command_iterator) =
wenzelm@56314
   799
            load_commands match {
wenzelm@56746
   800
              case command :: _ => (Symbol.Text_Chunk.File(node_name.node), Iterator((command, 0)))
wenzelm@56746
   801
              case _ => (Symbol.Text_Chunk.Default, node.command_iterator(former_range))
wenzelm@56309
   802
            }
wenzelm@56462
   803
          val markup_index = Command.Markup_Index(status, chunk_name)
wenzelm@56309
   804
          (for {
wenzelm@56373
   805
            (command, command_start) <- command_iterator
wenzelm@56462
   806
            chunk <- command.chunks.get(chunk_name).iterator
wenzelm@56309
   807
            states = state.command_states(version, command)
wenzelm@56354
   808
            res = result(states)
wenzelm@56590
   809
            markup_range <- (former_range - command_start).try_restrict(chunk.range).iterator
wenzelm@56590
   810
            markup = Command.State.merge_markup(states, markup_index, markup_range, elements)
wenzelm@56590
   811
            Text.Info(r0, a) <- markup.cumulate[A](markup_range, info, elements,
wenzelm@56309
   812
              {
wenzelm@56309
   813
                case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b))
wenzelm@56309
   814
              }).iterator
wenzelm@56590
   815
            r1 <- convert(r0 + command_start).try_restrict(range).iterator
wenzelm@56590
   816
          } yield Text.Info(r1, a)).toList
wenzelm@45459
   817
        }
wenzelm@45459
   818
wenzelm@55651
   819
        def select[A](
wenzelm@55651
   820
          range: Text.Range,
wenzelm@56743
   821
          elements: Markup.Elements,
wenzelm@56354
   822
          result: List[Command.State] => Text.Markup => Option[A],
wenzelm@55651
   823
          status: Boolean = false): List[Text.Info[A]] =
wenzelm@38845
   824
        {
wenzelm@56354
   825
          def result1(states: List[Command.State]): (Option[A], Text.Markup) => Option[Option[A]] =
wenzelm@50500
   826
          {
wenzelm@56354
   827
            val res = result(states)
wenzelm@52889
   828
            (_: Option[A], x: Text.Markup) =>
wenzelm@52889
   829
              res(x) match {
wenzelm@52889
   830
                case None => None
wenzelm@52889
   831
                case some => Some(some)
wenzelm@52889
   832
              }
wenzelm@50500
   833
          }
wenzelm@55651
   834
          for (Text.Info(r, Some(x)) <- cumulate(range, None, elements, result1 _, status))
wenzelm@46198
   835
            yield Text.Info(r, x)
wenzelm@38845
   836
        }
wenzelm@55800
   837
wenzelm@55800
   838
wenzelm@55800
   839
        /* output */
wenzelm@55800
   840
wenzelm@55800
   841
        override def toString: String =
wenzelm@55800
   842
          "Snapshot(node = " + node_name.node + ", version = " + version.id +
wenzelm@55800
   843
            (if (is_outdated) ", outdated" else "") + ")"
wenzelm@38841
   844
      }
wenzelm@38150
   845
    }
wenzelm@34485
   846
  }
wenzelm@34840
   847
}
wenzelm@34840
   848