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