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