src/Pure/PIDE/document.scala
author wenzelm
Fri Feb 28 11:58:26 2014 +0100 (2014-02-28)
changeset 55801 28b59620f0d0
parent 55800 d3c9fa520689
child 55820 61869776ce1f
permissions -rw-r--r--
tuned data structure;
     1 /*  Title:      Pure/PIDE/document.scala
     2     Author:     Makarius
     3 
     4 Document as collection of named nodes, each consisting of an editable
     5 list of commands, associated with asynchronous execution process.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import scala.collection.mutable
    12 
    13 
    14 object Document
    15 {
    16   /** document structure **/
    17 
    18   /* overlays -- print functions with arguments */
    19 
    20   object Overlays
    21   {
    22     val empty = new Overlays(Map.empty)
    23   }
    24 
    25   final class Overlays private(rep: Map[Node.Name, Node.Overlays])
    26   {
    27     def apply(name: Document.Node.Name): Node.Overlays =
    28       rep.getOrElse(name, Node.Overlays.empty)
    29 
    30     private def update(name: Node.Name, f: Node.Overlays => Node.Overlays): Overlays =
    31     {
    32       val node_overlays = f(apply(name))
    33       new Overlays(if (node_overlays.is_empty) rep - name else rep + (name -> node_overlays))
    34     }
    35 
    36     def insert(command: Command, fn: String, args: List[String]): Overlays =
    37       update(command.node_name, _.insert(command, fn, args))
    38 
    39     def remove(command: Command, fn: String, args: List[String]): Overlays =
    40       update(command.node_name, _.remove(command, fn, args))
    41 
    42     override def toString: String = rep.mkString("Overlays(", ",", ")")
    43   }
    44 
    45 
    46   /* document blobs: auxiliary files */
    47 
    48   sealed case class Blob(bytes: Bytes, file: Command.File, changed: Boolean)
    49 
    50   object Blobs
    51   {
    52     def apply(blobs: Map[Node.Name, Blob]): Blobs = new Blobs(blobs)
    53     val empty: Blobs = apply(Map.empty)
    54   }
    55 
    56   final class Blobs private(blobs: Map[Node.Name, Blob])
    57   {
    58     private lazy val digests: Map[SHA1.Digest, Blob] =
    59       for ((_, blob) <- blobs) yield (blob.bytes.sha1_digest, blob)
    60 
    61     def get(digest: SHA1.Digest): Option[Blob] = digests.get(digest)
    62     def get(name: Node.Name): Option[Blob] = blobs.get(name)
    63 
    64     def changed(name: Node.Name): Boolean =
    65       get(name) match {
    66         case Some(blob) => blob.changed
    67         case None => false
    68       }
    69 
    70     override def toString: String = blobs.mkString("Blobs(", ",", ")")
    71   }
    72 
    73 
    74   /* document nodes: theories and auxiliary files */
    75 
    76   type Edit[A, B] = (Node.Name, Node.Edit[A, B])
    77   type Edit_Text = Edit[Text.Edit, Text.Perspective]
    78   type Edit_Command = Edit[Command.Edit, Command.Perspective]
    79 
    80   object Node
    81   {
    82     val empty: Node = new Node()
    83 
    84 
    85     /* header and name */
    86 
    87     sealed case class Header(
    88       imports: List[Name],
    89       keywords: Thy_Header.Keywords,
    90       errors: List[String] = Nil)
    91     {
    92       def error(msg: String): Header = copy(errors = errors ::: List(msg))
    93 
    94       def cat_errors(msg2: String): Header =
    95         copy(errors = errors.map(msg1 => Library.cat_message(msg1, msg2)))
    96     }
    97 
    98     def bad_header(msg: String): Header = Header(Nil, Nil, List(msg))
    99 
   100     val no_header = bad_header("No theory header")
   101 
   102     object Name
   103     {
   104       val empty = Name("")
   105 
   106       object Ordering extends scala.math.Ordering[Name]
   107       {
   108         def compare(name1: Name, name2: Name): Int = name1.node compare name2.node
   109       }
   110     }
   111 
   112     sealed case class Name(node: String, master_dir: String = "", theory: String = "")
   113     {
   114       override def hashCode: Int = node.hashCode
   115       override def equals(that: Any): Boolean =
   116         that match {
   117           case other: Name => node == other.node
   118           case _ => false
   119         }
   120 
   121       def is_theory: Boolean = !theory.isEmpty
   122       override def toString: String = if (is_theory) theory else node
   123     }
   124 
   125 
   126     /* node overlays */
   127 
   128     object Overlays
   129     {
   130       val empty = new Overlays(Multi_Map.empty)
   131     }
   132 
   133     final class Overlays private(rep: Multi_Map[Command, (String, List[String])])
   134     {
   135       def commands: Set[Command] = rep.keySet
   136       def is_empty: Boolean = rep.isEmpty
   137       def dest: List[(Command, (String, List[String]))] = rep.iterator.toList
   138       def insert(cmd: Command, fn: String, args: List[String]): Overlays =
   139         new Overlays(rep.insert(cmd, (fn, args)))
   140       def remove(cmd: Command, fn: String, args: List[String]): Overlays =
   141         new Overlays(rep.remove(cmd, (fn, args)))
   142 
   143       override def toString: String = rep.mkString("Node.Overlays(", ",", ")")
   144     }
   145 
   146 
   147     /* edits */
   148 
   149     sealed abstract class Edit[A, B]
   150     {
   151       def foreach(f: A => Unit)
   152       {
   153         this match {
   154           case Edits(es) => es.foreach(f)
   155           case _ =>
   156         }
   157       }
   158     }
   159     case class Clear[A, B]() extends Edit[A, B]
   160     case class Blob[A, B]() extends Edit[A, B]
   161 
   162     case class Edits[A, B](edits: List[A]) extends Edit[A, B]
   163     case class Deps[A, B](header: Header) extends Edit[A, B]
   164     case class Perspective[A, B](required: Boolean, visible: B, overlays: Overlays) extends Edit[A, B]
   165     type Perspective_Text = Perspective[Text.Edit, Text.Perspective]
   166     type Perspective_Command = Perspective[Command.Edit, Command.Perspective]
   167 
   168 
   169     /* commands */
   170 
   171     object Commands
   172     {
   173       def apply(commands: Linear_Set[Command]): Commands = new Commands(commands)
   174       val empty: Commands = apply(Linear_Set.empty)
   175 
   176       def starts(commands: Iterator[Command], offset: Text.Offset = 0)
   177         : Iterator[(Command, Text.Offset)] =
   178       {
   179         var i = offset
   180         for (command <- commands) yield {
   181           val start = i
   182           i += command.length
   183           (command, start)
   184         }
   185       }
   186 
   187       private val block_size = 1024
   188     }
   189 
   190     final class Commands private(val commands: Linear_Set[Command])
   191     {
   192       lazy val thy_load_commands: List[Command] =
   193         commands.iterator.filter(cmd => !cmd.blobs.isEmpty).toList
   194 
   195       private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
   196       {
   197         val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
   198         var next_block = 0
   199         var last_stop = 0
   200         for ((command, start) <- Commands.starts(commands.iterator)) {
   201           last_stop = start + command.length
   202           while (last_stop + 1 > next_block) {
   203             blocks += (command -> start)
   204             next_block += Commands.block_size
   205           }
   206         }
   207         (blocks.toArray, Text.Range(0, last_stop))
   208       }
   209 
   210       private def full_range: Text.Range = full_index._2
   211 
   212       def range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
   213       {
   214         if (!commands.isEmpty && full_range.contains(i)) {
   215           val (cmd0, start0) = full_index._1(i / Commands.block_size)
   216           Node.Commands.starts(commands.iterator(cmd0), start0) dropWhile {
   217             case (cmd, start) => start + cmd.length <= i }
   218         }
   219         else Iterator.empty
   220       }
   221     }
   222   }
   223 
   224   final class Node private(
   225     val is_blob: Boolean = false,
   226     val header: Node.Header = Node.bad_header("Bad theory header"),
   227     val perspective: Node.Perspective_Command =
   228       Node.Perspective(false, Command.Perspective.empty, Node.Overlays.empty),
   229     _commands: Node.Commands = Node.Commands.empty)
   230   {
   231     def clear: Node = new Node(header = header)
   232 
   233     def init_blob: Node = new Node(is_blob = true)
   234 
   235     def update_header(new_header: Node.Header): Node =
   236       new Node(is_blob, new_header, perspective, _commands)
   237 
   238     def update_perspective(new_perspective: Node.Perspective_Command): Node =
   239       new Node(is_blob, header, new_perspective, _commands)
   240 
   241     def same_perspective(other_perspective: Node.Perspective_Command): Boolean =
   242       perspective.required == other_perspective.required &&
   243       perspective.visible.same(other_perspective.visible) &&
   244       perspective.overlays == other_perspective.overlays
   245 
   246     def commands: Linear_Set[Command] = _commands.commands
   247     def thy_load_commands: List[Command] = _commands.thy_load_commands
   248 
   249     def update_commands(new_commands: Linear_Set[Command]): Node =
   250       if (new_commands eq _commands.commands) this
   251       else new Node(is_blob, header, perspective, Node.Commands(new_commands))
   252 
   253     def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
   254       _commands.range(i)
   255 
   256     def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
   257       command_range(range.start) takeWhile { case (_, start) => start < range.stop }
   258 
   259     def command_start(cmd: Command): Option[Text.Offset] =
   260       Node.Commands.starts(commands.iterator).find(_._1 == cmd).map(_._2)
   261   }
   262 
   263 
   264   /* development graph */
   265 
   266   object Nodes
   267   {
   268     val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
   269   }
   270 
   271   final class Nodes private(graph: Graph[Node.Name, Node])
   272   {
   273     def get_name(s: String): Option[Node.Name] =
   274       graph.keys.find(name => name.node == s)
   275 
   276     def apply(name: Node.Name): Node =
   277       graph.default_node(name, Node.empty).get_node(name)
   278 
   279     def + (entry: (Node.Name, Node)): Nodes =
   280     {
   281       val (name, node) = entry
   282       val imports = node.header.imports
   283       val graph1 =
   284         (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty))
   285       val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
   286       val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name))
   287       new Nodes(graph3.map_node(name, _ => node))
   288     }
   289 
   290     def entries: Iterator[(Node.Name, Node)] =
   291       graph.entries.map({ case (name, (node, _)) => (name, node) })
   292 
   293     def thy_load_commands(file_name: Node.Name): List[Command] =
   294       (for {
   295         (_, node) <- entries
   296         cmd <- node.thy_load_commands.iterator
   297         name <- cmd.blobs_names.iterator
   298         if name == file_name
   299       } yield cmd).toList
   300 
   301     def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names)
   302     def topological_order: List[Node.Name] = graph.topological_order
   303   }
   304 
   305 
   306 
   307   /** versioning **/
   308 
   309   /* particular document versions */
   310 
   311   object Version
   312   {
   313     val init: Version = new Version()
   314 
   315     def make(syntax: Outer_Syntax, nodes: Nodes): Version =
   316       new Version(Document_ID.make(), syntax, nodes)
   317   }
   318 
   319   final class Version private(
   320     val id: Document_ID.Version = Document_ID.none,
   321     val syntax: Outer_Syntax = Outer_Syntax.empty,
   322     val nodes: Nodes = Nodes.empty)
   323   {
   324     def is_init: Boolean = id == Document_ID.none
   325 
   326     override def toString: String = "Version(" + id + ")"
   327   }
   328 
   329 
   330   /* changes of plain text, eventually resulting in document edits */
   331 
   332   object Change
   333   {
   334     val init: Change = new Change()
   335 
   336     def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
   337       new Change(Some(previous), edits, version)
   338   }
   339 
   340   final class Change private(
   341     val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
   342     val edits: List[Edit_Text] = Nil,
   343     val version: Future[Version] = Future.value(Version.init))
   344   {
   345     def is_finished: Boolean =
   346       (previous match { case None => true case Some(future) => future.is_finished }) &&
   347       version.is_finished
   348 
   349     def truncate: Change = new Change(None, Nil, version)
   350   }
   351 
   352 
   353   /* history navigation */
   354 
   355   object History
   356   {
   357     val init: History = new History()
   358   }
   359 
   360   final class History private(
   361     val undo_list: List[Change] = List(Change.init))  // non-empty list
   362   {
   363     def tip: Change = undo_list.head
   364     def + (change: Change): History = new History(change :: undo_list)
   365 
   366     def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
   367     {
   368       val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
   369       val (retained, dropped) = undo_list.splitAt(n max retain)
   370 
   371       retained.splitAt(retained.length - 1) match {
   372         case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
   373         case _ => None
   374       }
   375     }
   376   }
   377 
   378 
   379 
   380   /** global state -- document structure, execution process, editing history **/
   381 
   382   object Snapshot
   383   {
   384     val init = State.init.snapshot()
   385   }
   386 
   387   abstract class Snapshot
   388   {
   389     val state: State
   390     val version: Version
   391     val is_outdated: Boolean
   392 
   393     def convert(i: Text.Offset): Text.Offset
   394     def revert(i: Text.Offset): Text.Offset
   395     def convert(range: Text.Range): Text.Range
   396     def revert(range: Text.Range): Text.Range
   397 
   398     val node_name: Node.Name
   399     val node: Node
   400     val thy_load_commands: List[Command]
   401     def eq_content(other: Snapshot): Boolean
   402 
   403     def cumulate[A](
   404       range: Text.Range,
   405       info: A,
   406       elements: String => Boolean,
   407       result: Command.State => (A, Text.Markup) => Option[A],
   408       status: Boolean = false): List[Text.Info[A]]
   409 
   410     def select[A](
   411       range: Text.Range,
   412       elements: String => Boolean,
   413       result: Command.State => Text.Markup => Option[A],
   414       status: Boolean = false): List[Text.Info[A]]
   415   }
   416 
   417   type Assign_Update =
   418     List[(Document_ID.Command, List[Document_ID.Exec])]  // update of exec state assignment
   419 
   420   object State
   421   {
   422     class Fail(state: State) extends Exception
   423 
   424     object Assignment
   425     {
   426       val init: Assignment = new Assignment()
   427     }
   428 
   429     final class Assignment private(
   430       val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty,
   431       val is_finished: Boolean = false)
   432     {
   433       def check_finished: Assignment = { require(is_finished); this }
   434       def unfinished: Assignment = new Assignment(command_execs, false)
   435 
   436       def assign(update: Assign_Update): Assignment =
   437       {
   438         require(!is_finished)
   439         val command_execs1 =
   440           (command_execs /: update) {
   441             case (res, (command_id, exec_ids)) =>
   442               if (exec_ids.isEmpty) res - command_id
   443               else res + (command_id -> exec_ids)
   444           }
   445         new Assignment(command_execs1, true)
   446       }
   447     }
   448 
   449     val init: State =
   450       State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2
   451   }
   452 
   453   final case class State private(
   454     val versions: Map[Document_ID.Version, Version] = Map.empty,
   455     val blobs: Set[SHA1.Digest] = Set.empty,   // inlined files
   456     val commands: Map[Document_ID.Command, Command.State] = Map.empty,  // static markup from define_command
   457     val execs: Map[Document_ID.Exec, Command.State] = Map.empty,  // dynamic markup from execution
   458     val assignments: Map[Document_ID.Version, State.Assignment] = Map.empty,
   459     val history: History = History.init)
   460   {
   461     private def fail[A]: A = throw new State.Fail(this)
   462 
   463     def define_version(version: Version, assignment: State.Assignment): State =
   464     {
   465       val id = version.id
   466       copy(versions = versions + (id -> version),
   467         assignments = assignments + (id -> assignment.unfinished))
   468     }
   469 
   470     def define_blob(digest: SHA1.Digest): State = copy(blobs = blobs + digest)
   471     def defined_blob(digest: SHA1.Digest): Boolean = blobs.contains(digest)
   472 
   473     def define_command(command: Command): State =
   474     {
   475       val id = command.id
   476       copy(commands = commands + (id -> command.init_state))
   477     }
   478 
   479     def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id)
   480 
   481     def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] =
   482       commands.get(id) orElse execs.get(id) match {
   483         case None => None
   484         case Some(st) =>
   485           val command = st.command
   486           val node = version.nodes(command.node_name)
   487           if (node.commands.contains(command)) Some((node, command)) else None
   488       }
   489 
   490     def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail)
   491     def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail)
   492     def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail)
   493     def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail)
   494 
   495     def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) =
   496       execs.get(id) match {
   497         case Some(st) =>
   498           val new_st = st + (id, message)
   499           (new_st, copy(execs = execs + (id -> new_st)))
   500         case None =>
   501           commands.get(id) match {
   502             case Some(st) =>
   503               val new_st = st + (id, message)
   504               (new_st, copy(commands = commands + (id -> new_st)))
   505             case None => fail
   506           }
   507       }
   508 
   509     def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) =
   510     {
   511       val version = the_version(id)
   512 
   513       def upd(exec_id: Document_ID.Exec, st: Command.State)
   514           : Option[(Document_ID.Exec, Command.State)] =
   515         if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st)
   516 
   517       val (changed_commands, new_execs) =
   518         ((Nil: List[Command], execs) /: update) {
   519           case ((commands1, execs1), (command_id, exec)) =>
   520             val st = the_static_state(command_id)
   521             val command = st.command
   522             val commands2 = command :: commands1
   523             val execs2 =
   524               exec match {
   525                 case Nil => execs1
   526                 case eval_id :: print_ids =>
   527                   execs1 ++ upd(eval_id, st) ++
   528                     (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up)
   529               }
   530             (commands2, execs2)
   531         }
   532       val new_assignment = the_assignment(version).assign(update)
   533       val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
   534 
   535       (changed_commands, new_state)
   536     }
   537 
   538     def is_assigned(version: Version): Boolean =
   539       assignments.get(version.id) match {
   540         case Some(assgn) => assgn.is_finished
   541         case None => false
   542       }
   543 
   544     def is_stable(change: Change): Boolean =
   545       change.is_finished && is_assigned(change.version.get_finished)
   546 
   547     def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
   548     def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
   549     def tip_stable: Boolean = is_stable(history.tip)
   550     def tip_version: Version = history.tip.version.get_finished
   551 
   552     def continue_history(
   553         previous: Future[Version],
   554         edits: List[Edit_Text],
   555         version: Future[Version]): (Change, State) =
   556     {
   557       val change = Change.make(previous, edits, version)
   558       (change, copy(history = history + change))
   559     }
   560 
   561     def prune_history(retain: Int = 0): (List[Version], State) =
   562     {
   563       history.prune(is_stable, retain) match {
   564         case Some((dropped, history1)) =>
   565           val dropped_versions = dropped.map(change => change.version.get_finished)
   566           val state1 = copy(history = history1)
   567           (dropped_versions, state1)
   568         case None => fail
   569       }
   570     }
   571 
   572     def removed_versions(removed: List[Document_ID.Version]): State =
   573     {
   574       val versions1 = versions -- removed
   575       val assignments1 = assignments -- removed
   576       var blobs1 = Set.empty[SHA1.Digest]
   577       var commands1 = Map.empty[Document_ID.Command, Command.State]
   578       var execs1 = Map.empty[Document_ID.Exec, Command.State]
   579       for {
   580         (version_id, version) <- versions1.iterator
   581         command_execs = assignments1(version_id).command_execs
   582         (_, node) <- version.nodes.entries
   583         command <- node.commands.iterator
   584       } {
   585         for (digest <- command.blobs_digests; if !blobs1.contains(digest))
   586           blobs1 += digest
   587 
   588         if (!commands1.isDefinedAt(command.id))
   589           commands.get(command.id).foreach(st => commands1 += (command.id -> st))
   590 
   591         for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
   592           if (!execs1.isDefinedAt(exec_id))
   593             execs.get(exec_id).foreach(st => execs1 += (exec_id -> st))
   594         }
   595       }
   596       copy(versions = versions1, blobs = blobs1, commands = commands1, execs = execs1,
   597         assignments = assignments1)
   598     }
   599 
   600     def command_state(version: Version, command: Command): Command.State =
   601     {
   602       require(is_assigned(version))
   603       try {
   604         the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil)
   605           .map(the_dynamic_state(_)) match {
   606             case eval_state :: print_states => (eval_state /: print_states)(_ ++ _)
   607             case Nil => fail
   608           }
   609       }
   610       catch {
   611         case _: State.Fail =>
   612           try { the_static_state(command.id) }
   613           catch { case _: State.Fail => command.init_state }
   614       }
   615     }
   616 
   617     def markup_to_XML(version: Version, node: Node, filter: XML.Elem => Boolean): XML.Body =
   618       node.commands.toList.map(cmd => command_state(version, cmd).markup_to_XML(filter)).flatten
   619 
   620     // persistent user-view
   621     def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
   622       : Snapshot =
   623     {
   624       val stable = recent_stable
   625       val latest = history.tip
   626 
   627       // FIXME proper treatment of removed nodes
   628       val edits =
   629         (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
   630             (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
   631       lazy val reverse_edits = edits.reverse
   632 
   633       new Snapshot
   634       {
   635         /* global information */
   636 
   637         val state = State.this
   638         val version = stable.version.get_finished
   639         val is_outdated = !(pending_edits.isEmpty && latest == stable)
   640 
   641 
   642         /* local node content */
   643 
   644         def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
   645         def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
   646         def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
   647         def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
   648 
   649         val node_name = name
   650         val node = version.nodes(name)
   651 
   652         val thy_load_commands: List[Command] =
   653           if (node_name.is_theory) Nil
   654           else version.nodes.thy_load_commands(node_name)
   655 
   656         def eq_content(other: Snapshot): Boolean =
   657         {
   658           def eq_commands(commands: (Command, Command)): Boolean =
   659             state.command_state(version, commands._1) eq_content
   660               other.state.command_state(other.version, commands._2)
   661 
   662           !is_outdated && !other.is_outdated &&
   663           node.commands.size == other.node.commands.size &&
   664           (node.commands.iterator zip other.node.commands.iterator).forall(eq_commands) &&
   665           thy_load_commands.length == other.thy_load_commands.length &&
   666           (thy_load_commands zip other.thy_load_commands).forall(eq_commands)
   667         }
   668 
   669 
   670         /* cumulate markup */
   671 
   672         def cumulate[A](
   673           range: Text.Range,
   674           info: A,
   675           elements: String => Boolean,
   676           result: Command.State => (A, Text.Markup) => Option[A],
   677           status: Boolean = false): List[Text.Info[A]] =
   678         {
   679           val former_range = revert(range)
   680           thy_load_commands match {
   681             case thy_load_command :: _ =>
   682               val file_name = node_name.node
   683               val markup_index = Command.Markup_Index(status, file_name)
   684               (for {
   685                 chunk <- thy_load_command.chunks.get(file_name).iterator
   686                 st = state.command_state(version, thy_load_command)
   687                 res = result(st)
   688                 Text.Info(r0, a) <- st.markup(markup_index).cumulate[A](
   689                   former_range.restrict(chunk.range), info, elements,
   690                   { case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0), b)) }
   691                 ).iterator
   692               } yield Text.Info(convert(r0), a)).toList
   693 
   694             case _ =>
   695               val markup_index = Command.Markup_Index(status, "")
   696               (for {
   697                 (command, command_start) <- node.command_range(former_range)
   698                 st = state.command_state(version, command)
   699                 res = result(st)
   700                 Text.Info(r0, a) <- st.markup(markup_index).cumulate[A](
   701                   (former_range - command_start).restrict(command.range), info, elements,
   702                   {
   703                     case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b))
   704                   }).iterator
   705               } yield Text.Info(convert(r0 + command_start), a)).toList
   706           }
   707         }
   708 
   709         def select[A](
   710           range: Text.Range,
   711           elements: String => Boolean,
   712           result: Command.State => Text.Markup => Option[A],
   713           status: Boolean = false): List[Text.Info[A]] =
   714         {
   715           def result1(st: Command.State): (Option[A], Text.Markup) => Option[Option[A]] =
   716           {
   717             val res = result(st)
   718             (_: Option[A], x: Text.Markup) =>
   719               res(x) match {
   720                 case None => None
   721                 case some => Some(some)
   722               }
   723           }
   724           for (Text.Info(r, Some(x)) <- cumulate(range, None, elements, result1 _, status))
   725             yield Text.Info(r, x)
   726         }
   727 
   728 
   729         /* output */
   730 
   731         override def toString: String =
   732           "Snapshot(node = " + node_name.node + ", version = " + version.id +
   733             (if (is_outdated) ", outdated" else "") + ")"
   734       }
   735     }
   736   }
   737 }
   738