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