src/Pure/PIDE/document.scala
author wenzelm
Wed Feb 26 20:56:55 2014 +0100 (2014-02-26)
changeset 55777 90484dff4dc4
parent 55710 2d623ab55672
child 55782 47ed6a67a304
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     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     override def toString: String = "Version(" + id + ")"
   297   }
   298 
   299 
   300   /* changes of plain text, eventually resulting in document edits */
   301 
   302   object Change
   303   {
   304     val init: Change = new Change()
   305 
   306     def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
   307       new Change(Some(previous), edits, version)
   308   }
   309 
   310   final class Change private(
   311     val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
   312     val edits: List[Edit_Text] = Nil,
   313     val version: Future[Version] = Future.value(Version.init))
   314   {
   315     def is_finished: Boolean =
   316       (previous match { case None => true case Some(future) => future.is_finished }) &&
   317       version.is_finished
   318 
   319     def truncate: Change = new Change(None, Nil, version)
   320   }
   321 
   322 
   323   /* history navigation */
   324 
   325   object History
   326   {
   327     val init: History = new History()
   328   }
   329 
   330   final class History private(
   331     val undo_list: List[Change] = List(Change.init))  // non-empty list
   332   {
   333     def tip: Change = undo_list.head
   334     def + (change: Change): History = new History(change :: undo_list)
   335 
   336     def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
   337     {
   338       val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
   339       val (retained, dropped) = undo_list.splitAt(n max retain)
   340 
   341       retained.splitAt(retained.length - 1) match {
   342         case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
   343         case _ => None
   344       }
   345     }
   346   }
   347 
   348 
   349 
   350   /** global state -- document structure, execution process, editing history **/
   351 
   352   object Snapshot
   353   {
   354     val init = State.init.snapshot()
   355   }
   356 
   357   abstract class Snapshot
   358   {
   359     val state: State
   360     val version: Version
   361     val is_outdated: Boolean
   362 
   363     def convert(i: Text.Offset): Text.Offset
   364     def revert(i: Text.Offset): Text.Offset
   365     def convert(range: Text.Range): Text.Range
   366     def revert(range: Text.Range): Text.Range
   367 
   368     val node_name: Node.Name
   369     val node: Node
   370     val thy_load_commands: List[Command]
   371     def eq_content(other: Snapshot): Boolean
   372 
   373     def cumulate[A](
   374       range: Text.Range,
   375       info: A,
   376       elements: String => Boolean,
   377       result: Command.State => (A, Text.Markup) => Option[A],
   378       status: Boolean = false): List[Text.Info[A]]
   379 
   380     def select[A](
   381       range: Text.Range,
   382       elements: String => Boolean,
   383       result: Command.State => Text.Markup => Option[A],
   384       status: Boolean = false): List[Text.Info[A]]
   385   }
   386 
   387   type Assign_Update =
   388     List[(Document_ID.Command, List[Document_ID.Exec])]  // update of exec state assignment
   389 
   390   object State
   391   {
   392     class Fail(state: State) extends Exception
   393 
   394     object Assignment
   395     {
   396       val init: Assignment = new Assignment()
   397     }
   398 
   399     final class Assignment private(
   400       val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty,
   401       val is_finished: Boolean = false)
   402     {
   403       def check_finished: Assignment = { require(is_finished); this }
   404       def unfinished: Assignment = new Assignment(command_execs, false)
   405 
   406       def assign(update: Assign_Update): Assignment =
   407       {
   408         require(!is_finished)
   409         val command_execs1 =
   410           (command_execs /: update) {
   411             case (res, (command_id, exec_ids)) =>
   412               if (exec_ids.isEmpty) res - command_id
   413               else res + (command_id -> exec_ids)
   414           }
   415         new Assignment(command_execs1, true)
   416       }
   417     }
   418 
   419     val init: State =
   420       State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2
   421   }
   422 
   423   final case class State private(
   424     val versions: Map[Document_ID.Version, Version] = Map.empty,
   425     val blobs: Set[SHA1.Digest] = Set.empty,   // inlined files
   426     val commands: Map[Document_ID.Command, Command.State] = Map.empty,  // static markup from define_command
   427     val execs: Map[Document_ID.Exec, Command.State] = Map.empty,  // dynamic markup from execution
   428     val assignments: Map[Document_ID.Version, State.Assignment] = Map.empty,
   429     val history: History = History.init)
   430   {
   431     private def fail[A]: A = throw new State.Fail(this)
   432 
   433     def define_version(version: Version, assignment: State.Assignment): State =
   434     {
   435       val id = version.id
   436       copy(versions = versions + (id -> version),
   437         assignments = assignments + (id -> assignment.unfinished))
   438     }
   439 
   440     def define_blob(digest: SHA1.Digest): State = copy(blobs = blobs + digest)
   441     def defined_blob(digest: SHA1.Digest): Boolean = blobs.contains(digest)
   442 
   443     def define_command(command: Command): State =
   444     {
   445       val id = command.id
   446       copy(commands = commands + (id -> command.init_state))
   447     }
   448 
   449     def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id)
   450 
   451     def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] =
   452       commands.get(id) orElse execs.get(id) match {
   453         case None => None
   454         case Some(st) =>
   455           val command = st.command
   456           val node = version.nodes(command.node_name)
   457           if (node.commands.contains(command)) Some((node, command)) else None
   458       }
   459 
   460     def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail)
   461     def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail)
   462     def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail)
   463     def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail)
   464 
   465     def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) =
   466       execs.get(id) match {
   467         case Some(st) =>
   468           val new_st = st + (id, message)
   469           (new_st, copy(execs = execs + (id -> new_st)))
   470         case None =>
   471           commands.get(id) match {
   472             case Some(st) =>
   473               val new_st = st + (id, message)
   474               (new_st, copy(commands = commands + (id -> new_st)))
   475             case None => fail
   476           }
   477       }
   478 
   479     def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) =
   480     {
   481       val version = the_version(id)
   482 
   483       def upd(exec_id: Document_ID.Exec, st: Command.State)
   484           : Option[(Document_ID.Exec, Command.State)] =
   485         if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st)
   486 
   487       val (changed_commands, new_execs) =
   488         ((Nil: List[Command], execs) /: update) {
   489           case ((commands1, execs1), (command_id, exec)) =>
   490             val st = the_static_state(command_id)
   491             val command = st.command
   492             val commands2 = command :: commands1
   493             val execs2 =
   494               exec match {
   495                 case Nil => execs1
   496                 case eval_id :: print_ids =>
   497                   execs1 ++ upd(eval_id, st) ++
   498                     (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up)
   499               }
   500             (commands2, execs2)
   501         }
   502       val new_assignment = the_assignment(version).assign(update)
   503       val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
   504 
   505       (changed_commands, new_state)
   506     }
   507 
   508     def is_assigned(version: Version): Boolean =
   509       assignments.get(version.id) match {
   510         case Some(assgn) => assgn.is_finished
   511         case None => false
   512       }
   513 
   514     def is_stable(change: Change): Boolean =
   515       change.is_finished && is_assigned(change.version.get_finished)
   516 
   517     def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
   518     def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
   519     def tip_stable: Boolean = is_stable(history.tip)
   520     def tip_version: Version = history.tip.version.get_finished
   521 
   522     def continue_history(
   523         previous: Future[Version],
   524         edits: List[Edit_Text],
   525         version: Future[Version]): (Change, State) =
   526     {
   527       val change = Change.make(previous, edits, version)
   528       (change, copy(history = history + change))
   529     }
   530 
   531     def prune_history(retain: Int = 0): (List[Version], State) =
   532     {
   533       history.prune(is_stable, retain) match {
   534         case Some((dropped, history1)) =>
   535           val dropped_versions = dropped.map(change => change.version.get_finished)
   536           val state1 = copy(history = history1)
   537           (dropped_versions, state1)
   538         case None => fail
   539       }
   540     }
   541 
   542     def removed_versions(removed: List[Document_ID.Version]): State =
   543     {
   544       val versions1 = versions -- removed
   545       val assignments1 = assignments -- removed
   546       var blobs1 = Set.empty[SHA1.Digest]
   547       var commands1 = Map.empty[Document_ID.Command, Command.State]
   548       var execs1 = Map.empty[Document_ID.Exec, Command.State]
   549       for {
   550         (version_id, version) <- versions1.iterator
   551         command_execs = assignments1(version_id).command_execs
   552         (_, node) <- version.nodes.entries
   553         command <- node.commands.iterator
   554       } {
   555         for (digest <- command.blobs_digests; if !blobs1.contains(digest))
   556           blobs1 += digest
   557 
   558         if (!commands1.isDefinedAt(command.id))
   559           commands.get(command.id).foreach(st => commands1 += (command.id -> st))
   560 
   561         for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
   562           if (!execs1.isDefinedAt(exec_id))
   563             execs.get(exec_id).foreach(st => execs1 += (exec_id -> st))
   564         }
   565       }
   566       copy(versions = versions1, blobs = blobs1, commands = commands1, execs = execs1,
   567         assignments = assignments1)
   568     }
   569 
   570     def command_state(version: Version, command: Command): Command.State =
   571     {
   572       require(is_assigned(version))
   573       try {
   574         the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil)
   575           .map(the_dynamic_state(_)) match {
   576             case eval_state :: print_states => (eval_state /: print_states)(_ ++ _)
   577             case Nil => fail
   578           }
   579       }
   580       catch {
   581         case _: State.Fail =>
   582           try { the_static_state(command.id) }
   583           catch { case _: State.Fail => command.init_state }
   584       }
   585     }
   586 
   587     def markup_to_XML(version: Version, node: Node, filter: XML.Elem => Boolean): XML.Body =
   588       node.commands.toList.map(cmd => command_state(version, cmd).markup_to_XML(filter)).flatten
   589 
   590     // persistent user-view
   591     def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
   592       : Snapshot =
   593     {
   594       val stable = recent_stable
   595       val latest = history.tip
   596 
   597       // FIXME proper treatment of removed nodes
   598       val edits =
   599         (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
   600             (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
   601       lazy val reverse_edits = edits.reverse
   602 
   603       new Snapshot
   604       {
   605         /* global information */
   606 
   607         val state = State.this
   608         val version = stable.version.get_finished
   609         val is_outdated = !(pending_edits.isEmpty && latest == stable)
   610 
   611 
   612         /* local node content */
   613 
   614         def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
   615         def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
   616         def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
   617         def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
   618 
   619         val node_name = name
   620         val node = version.nodes(name)
   621 
   622         val thy_load_commands: List[Command] =
   623           if (node_name.is_theory) Nil
   624           else version.nodes.thy_load_commands(node_name)
   625 
   626         def eq_content(other: Snapshot): Boolean =
   627         {
   628           def eq_commands(commands: (Command, Command)): Boolean =
   629             state.command_state(version, commands._1) eq_content
   630               other.state.command_state(other.version, commands._2)
   631 
   632           !is_outdated && !other.is_outdated &&
   633           node.commands.size == other.node.commands.size &&
   634           (node.commands.iterator zip other.node.commands.iterator).forall(eq_commands) &&
   635           thy_load_commands.length == other.thy_load_commands.length &&
   636           (thy_load_commands zip other.thy_load_commands).forall(eq_commands)
   637         }
   638 
   639         override def toString: String =
   640           "Snapshot(node = " + node_name.node + ", version = " + version.id +
   641             (if (is_outdated) ", outdated" else "") + ")"
   642 
   643 
   644         /* cumulate markup */
   645 
   646         def cumulate[A](
   647           range: Text.Range,
   648           info: A,
   649           elements: String => Boolean,
   650           result: Command.State => (A, Text.Markup) => Option[A],
   651           status: Boolean = false): List[Text.Info[A]] =
   652         {
   653           val former_range = revert(range)
   654           thy_load_commands match {
   655             case thy_load_command :: _ =>
   656               val file_name = node_name.node
   657               val markup_index = Command.Markup_Index(status, file_name)
   658               (for {
   659                 chunk <- thy_load_command.chunks.get(file_name).iterator
   660                 st = state.command_state(version, thy_load_command)
   661                 res = result(st)
   662                 Text.Info(r0, a) <- st.markup(markup_index).cumulate[A](
   663                   former_range.restrict(chunk.range), info, elements,
   664                   { case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0), b)) }
   665                 ).iterator
   666               } yield Text.Info(convert(r0), a)).toList
   667 
   668             case _ =>
   669               val markup_index = Command.Markup_Index(status, "")
   670               (for {
   671                 (command, command_start) <- node.command_range(former_range)
   672                 st = state.command_state(version, command)
   673                 res = result(st)
   674                 Text.Info(r0, a) <- st.markup(markup_index).cumulate[A](
   675                   (former_range - command_start).restrict(command.range), info, elements,
   676                   {
   677                     case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b))
   678                   }).iterator
   679               } yield Text.Info(convert(r0 + command_start), a)).toList
   680           }
   681         }
   682 
   683         def select[A](
   684           range: Text.Range,
   685           elements: String => Boolean,
   686           result: Command.State => Text.Markup => Option[A],
   687           status: Boolean = false): List[Text.Info[A]] =
   688         {
   689           def result1(st: Command.State): (Option[A], Text.Markup) => Option[Option[A]] =
   690           {
   691             val res = result(st)
   692             (_: Option[A], x: Text.Markup) =>
   693               res(x) match {
   694                 case None => None
   695                 case some => Some(some)
   696               }
   697           }
   698           for (Text.Info(r, Some(x)) <- cumulate(range, None, elements, result1 _, status))
   699             yield Text.Info(r, x)
   700         }
   701       }
   702     }
   703   }
   704 }
   705