src/Pure/PIDE/document.scala
author wenzelm
Wed Mar 26 19:42:16 2014 +0100 (2014-03-26)
changeset 56295 a40e67ce4f84
parent 56176 0bc9b0ad6287
child 56298 cf7710540f39
permissions -rw-r--r--
clarified valid_id: always standardize towards static command.id;
     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   /* markup elements */
   380 
   381   object Elements
   382   {
   383     def apply(elems: Set[String]): Elements = new Elements(elems)
   384     def apply(elems: String*): Elements = apply(Set(elems: _*))
   385     val empty: Elements = apply()
   386     val full: Elements = new Full_Elements
   387   }
   388 
   389   sealed class Elements private[Document](private val rep: Set[String])
   390   {
   391     def apply(elem: String): Boolean = rep.contains(elem)
   392     def + (elem: String): Elements = new Elements(rep + elem)
   393     def ++ (elems: Elements): Elements = new Elements(rep ++ elems.rep)
   394     override def toString: String = rep.mkString("Elements(", ",", ")")
   395   }
   396 
   397   private class Full_Elements extends Elements(Set.empty)
   398   {
   399     override def apply(elem: String): Boolean = true
   400     override def toString: String = "Full_Elements()"
   401   }
   402 
   403 
   404   /* snapshot */
   405 
   406   object Snapshot
   407   {
   408     val init = State.init.snapshot()
   409   }
   410 
   411   abstract class Snapshot
   412   {
   413     val state: State
   414     val version: Version
   415     val is_outdated: Boolean
   416 
   417     def convert(i: Text.Offset): Text.Offset
   418     def revert(i: Text.Offset): Text.Offset
   419     def convert(range: Text.Range): Text.Range
   420     def revert(range: Text.Range): Text.Range
   421 
   422     val node_name: Node.Name
   423     val node: Node
   424     val thy_load_commands: List[Command]
   425     def is_loaded: Boolean
   426     def eq_content(other: Snapshot): Boolean
   427 
   428     def cumulate[A](
   429       range: Text.Range,
   430       info: A,
   431       elements: Elements,
   432       result: Command.State => (A, Text.Markup) => Option[A],
   433       status: Boolean = false): List[Text.Info[A]]
   434 
   435     def select[A](
   436       range: Text.Range,
   437       elements: Elements,
   438       result: Command.State => Text.Markup => Option[A],
   439       status: Boolean = false): List[Text.Info[A]]
   440   }
   441 
   442 
   443 
   444   /** global state -- document structure, execution process, editing history **/
   445 
   446   type Assign_Update =
   447     List[(Document_ID.Command, List[Document_ID.Exec])]  // update of exec state assignment
   448 
   449   object State
   450   {
   451     class Fail(state: State) extends Exception
   452 
   453     object Assignment
   454     {
   455       val init: Assignment = new Assignment()
   456     }
   457 
   458     final class Assignment private(
   459       val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty,
   460       val is_finished: Boolean = false)
   461     {
   462       def check_finished: Assignment = { require(is_finished); this }
   463       def unfinished: Assignment = new Assignment(command_execs, false)
   464 
   465       def assign(update: Assign_Update): Assignment =
   466       {
   467         require(!is_finished)
   468         val command_execs1 =
   469           (command_execs /: update) {
   470             case (res, (command_id, exec_ids)) =>
   471               if (exec_ids.isEmpty) res - command_id
   472               else res + (command_id -> exec_ids)
   473           }
   474         new Assignment(command_execs1, true)
   475       }
   476     }
   477 
   478     val init: State =
   479       State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2
   480   }
   481 
   482   final case class State private(
   483     val versions: Map[Document_ID.Version, Version] = Map.empty,
   484     val blobs: Set[SHA1.Digest] = Set.empty,   // inlined files
   485     val commands: Map[Document_ID.Command, Command.State] = Map.empty,  // static markup from define_command
   486     val execs: Map[Document_ID.Exec, Command.State] = Map.empty,  // dynamic markup from execution
   487     val assignments: Map[Document_ID.Version, State.Assignment] = Map.empty,
   488     val history: History = History.init)
   489   {
   490     private def fail[A]: A = throw new State.Fail(this)
   491 
   492     def define_version(version: Version, assignment: State.Assignment): State =
   493     {
   494       val id = version.id
   495       copy(versions = versions + (id -> version),
   496         assignments = assignments + (id -> assignment.unfinished))
   497     }
   498 
   499     def define_blob(digest: SHA1.Digest): State = copy(blobs = blobs + digest)
   500     def defined_blob(digest: SHA1.Digest): Boolean = blobs.contains(digest)
   501 
   502     def define_command(command: Command): State =
   503     {
   504       val id = command.id
   505       copy(commands = commands + (id -> command.init_state))
   506     }
   507 
   508     def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id)
   509 
   510     def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] =
   511       commands.get(id) orElse execs.get(id) match {
   512         case None => None
   513         case Some(st) =>
   514           val command = st.command
   515           val node = version.nodes(command.node_name)
   516           if (node.commands.contains(command)) Some((node, command)) else None
   517       }
   518 
   519     def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail)
   520     def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail)
   521     def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail)
   522     def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail)
   523 
   524     def valid_id(st: Command.State)(id: Document_ID.Generic): Boolean =
   525       id == st.command.id ||
   526       (execs.get(id) match { case Some(st1) => st1.command.id == st.command.id case None => false })
   527 
   528     def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) =
   529       execs.get(id) match {
   530         case Some(st) =>
   531           val new_st = st + (valid_id(st), message)
   532           (new_st, copy(execs = execs + (id -> new_st)))
   533         case None =>
   534           commands.get(id) match {
   535             case Some(st) =>
   536               val new_st = st + (valid_id(st), message)
   537               (new_st, copy(commands = commands + (id -> new_st)))
   538             case None => fail
   539           }
   540       }
   541 
   542     def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) =
   543     {
   544       val version = the_version(id)
   545 
   546       def upd(exec_id: Document_ID.Exec, st: Command.State)
   547           : Option[(Document_ID.Exec, Command.State)] =
   548         if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st)
   549 
   550       val (changed_commands, new_execs) =
   551         ((Nil: List[Command], execs) /: update) {
   552           case ((commands1, execs1), (command_id, exec)) =>
   553             val st = the_static_state(command_id)
   554             val command = st.command
   555             val commands2 = command :: commands1
   556             val execs2 =
   557               exec match {
   558                 case Nil => execs1
   559                 case eval_id :: print_ids =>
   560                   execs1 ++ upd(eval_id, st) ++
   561                     (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up)
   562               }
   563             (commands2, execs2)
   564         }
   565       val new_assignment = the_assignment(version).assign(update)
   566       val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
   567 
   568       (changed_commands, new_state)
   569     }
   570 
   571     def is_assigned(version: Version): Boolean =
   572       assignments.get(version.id) match {
   573         case Some(assgn) => assgn.is_finished
   574         case None => false
   575       }
   576 
   577     def is_stable(change: Change): Boolean =
   578       change.is_finished && is_assigned(change.version.get_finished)
   579 
   580     def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
   581     def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
   582     def tip_stable: Boolean = is_stable(history.tip)
   583     def tip_version: Version = history.tip.version.get_finished
   584 
   585     def continue_history(
   586         previous: Future[Version],
   587         edits: List[Edit_Text],
   588         version: Future[Version]): (Change, State) =
   589     {
   590       val change = Change.make(previous, edits, version)
   591       (change, copy(history = history + change))
   592     }
   593 
   594     def prune_history(retain: Int = 0): (List[Version], State) =
   595     {
   596       history.prune(is_stable, retain) match {
   597         case Some((dropped, history1)) =>
   598           val dropped_versions = dropped.map(change => change.version.get_finished)
   599           val state1 = copy(history = history1)
   600           (dropped_versions, state1)
   601         case None => fail
   602       }
   603     }
   604 
   605     def removed_versions(removed: List[Document_ID.Version]): State =
   606     {
   607       val versions1 = versions -- removed
   608       val assignments1 = assignments -- removed
   609       var blobs1 = Set.empty[SHA1.Digest]
   610       var commands1 = Map.empty[Document_ID.Command, Command.State]
   611       var execs1 = Map.empty[Document_ID.Exec, Command.State]
   612       for {
   613         (version_id, version) <- versions1.iterator
   614         command_execs = assignments1(version_id).command_execs
   615         (_, node) <- version.nodes.entries
   616         command <- node.commands.iterator
   617       } {
   618         for (digest <- command.blobs_digests; if !blobs1.contains(digest))
   619           blobs1 += digest
   620 
   621         if (!commands1.isDefinedAt(command.id))
   622           commands.get(command.id).foreach(st => commands1 += (command.id -> st))
   623 
   624         for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
   625           if (!execs1.isDefinedAt(exec_id))
   626             execs.get(exec_id).foreach(st => execs1 += (exec_id -> st))
   627         }
   628       }
   629       copy(versions = versions1, blobs = blobs1, commands = commands1, execs = execs1,
   630         assignments = assignments1)
   631     }
   632 
   633     def command_state(version: Version, command: Command): Command.State =
   634     {
   635       require(is_assigned(version))
   636       try {
   637         the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil)
   638           .map(the_dynamic_state(_)) match {
   639             case eval_state :: print_states => (eval_state /: print_states)(_ ++ _)
   640             case Nil => fail
   641           }
   642       }
   643       catch {
   644         case _: State.Fail =>
   645           try { the_static_state(command.id) }
   646           catch { case _: State.Fail => command.init_state }
   647       }
   648     }
   649 
   650     def markup_to_XML(version: Version, node: Node, filter: XML.Elem => Boolean): XML.Body =
   651       node.commands.toList.map(cmd => command_state(version, cmd).markup_to_XML(filter)).flatten
   652 
   653     // persistent user-view
   654     def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
   655       : Snapshot =
   656     {
   657       val stable = recent_stable
   658       val latest = history.tip
   659 
   660       // FIXME proper treatment of removed nodes
   661       val edits =
   662         (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
   663             (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
   664       lazy val reverse_edits = edits.reverse
   665 
   666       new Snapshot
   667       {
   668         /* global information */
   669 
   670         val state = State.this
   671         val version = stable.version.get_finished
   672         val is_outdated = !(pending_edits.isEmpty && latest == stable)
   673 
   674 
   675         /* local node content */
   676 
   677         def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
   678         def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
   679         def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
   680         def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
   681 
   682         val node_name = name
   683         val node = version.nodes(name)
   684 
   685         val thy_load_commands: List[Command] =
   686           if (node_name.is_theory) Nil
   687           else version.nodes.thy_load_commands(node_name)
   688 
   689         val is_loaded: Boolean = node_name.is_theory || !thy_load_commands.isEmpty
   690 
   691         def eq_content(other: Snapshot): Boolean =
   692         {
   693           def eq_commands(commands: (Command, Command)): Boolean =
   694             state.command_state(version, commands._1) eq_content
   695               other.state.command_state(other.version, commands._2)
   696 
   697           !is_outdated && !other.is_outdated &&
   698           node.commands.size == other.node.commands.size &&
   699           (node.commands.iterator zip other.node.commands.iterator).forall(eq_commands) &&
   700           thy_load_commands.length == other.thy_load_commands.length &&
   701           (thy_load_commands zip other.thy_load_commands).forall(eq_commands)
   702         }
   703 
   704 
   705         /* cumulate markup */
   706 
   707         def cumulate[A](
   708           range: Text.Range,
   709           info: A,
   710           elements: Elements,
   711           result: Command.State => (A, Text.Markup) => Option[A],
   712           status: Boolean = false): List[Text.Info[A]] =
   713         {
   714           val former_range = revert(range)
   715           thy_load_commands match {
   716             case thy_load_command :: _ =>
   717               val file_name = node_name.node
   718               val markup_index = Command.Markup_Index(status, file_name)
   719               (for {
   720                 chunk <- thy_load_command.chunks.get(file_name).iterator
   721                 st = state.command_state(version, thy_load_command)
   722                 res = result(st)
   723                 Text.Info(r0, a) <- st.markup(markup_index).cumulate[A](
   724                   former_range.restrict(chunk.range), info, elements,
   725                   { case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0), b)) }
   726                 ).iterator
   727               } yield Text.Info(convert(r0), a)).toList
   728 
   729             case _ =>
   730               val markup_index = Command.Markup_Index(status, "")
   731               (for {
   732                 (command, command_start) <- node.command_range(former_range)
   733                 st = state.command_state(version, command)
   734                 res = result(st)
   735                 Text.Info(r0, a) <- st.markup(markup_index).cumulate[A](
   736                   (former_range - command_start).restrict(command.range), info, elements,
   737                   {
   738                     case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b))
   739                   }).iterator
   740               } yield Text.Info(convert(r0 + command_start), a)).toList
   741           }
   742         }
   743 
   744         def select[A](
   745           range: Text.Range,
   746           elements: Elements,
   747           result: Command.State => Text.Markup => Option[A],
   748           status: Boolean = false): List[Text.Info[A]] =
   749         {
   750           def result1(st: Command.State): (Option[A], Text.Markup) => Option[Option[A]] =
   751           {
   752             val res = result(st)
   753             (_: Option[A], x: Text.Markup) =>
   754               res(x) match {
   755                 case None => None
   756                 case some => Some(some)
   757               }
   758           }
   759           for (Text.Info(r, Some(x)) <- cumulate(range, None, elements, result1 _, status))
   760             yield Text.Info(r, x)
   761         }
   762 
   763 
   764         /* output */
   765 
   766         override def toString: String =
   767           "Snapshot(node = " + node_name.node + ", version = " + version.id +
   768             (if (is_outdated) ", outdated" else "") + ")"
   769       }
   770     }
   771   }
   772 }
   773