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