src/Pure/PIDE/document.scala
author wenzelm
Tue Feb 11 21:58:31 2014 +0100 (2014-02-11)
changeset 55432 9c53198dbb1c
parent 55431 e0f20a44ff9d
child 55434 aa2918d967f0
permissions -rw-r--r--
maintain multiple command chunks and markup trees: for main chunk and loaded files;
document view for all text areas, including auxiliary files;
     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 header: Node.Header = Node.bad_header("Bad theory header"),
   196     val perspective: Node.Perspective_Command =
   197       Node.Perspective(false, Command.Perspective.empty, Node.Overlays.empty),
   198     _commands: Node.Commands = Node.Commands.empty)
   199   {
   200     def clear: Node = new Node(header = header)
   201 
   202     def update_header(new_header: Node.Header): Node =
   203       new Node(new_header, perspective, _commands)
   204 
   205     def update_perspective(new_perspective: Node.Perspective_Command): Node =
   206       new Node(header, new_perspective, _commands)
   207 
   208     def same_perspective(other_perspective: Node.Perspective_Command): Boolean =
   209       perspective.required == other_perspective.required &&
   210       perspective.visible.same(other_perspective.visible) &&
   211       perspective.overlays == other_perspective.overlays
   212 
   213     def commands: Linear_Set[Command] = _commands.commands
   214     def thy_load_commands: List[Command] = _commands.thy_load_commands
   215 
   216     def update_commands(new_commands: Linear_Set[Command]): Node =
   217       if (new_commands eq _commands.commands) this
   218       else new Node(header, perspective, Node.Commands(new_commands))
   219 
   220     def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
   221       _commands.range(i)
   222 
   223     def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
   224       command_range(range.start) takeWhile { case (_, start) => start < range.stop }
   225 
   226     def command_start(cmd: Command): Option[Text.Offset] =
   227       Node.Commands.starts(commands.iterator).find(_._1 == cmd).map(_._2)
   228   }
   229 
   230 
   231   /* development graph */
   232 
   233   object Nodes
   234   {
   235     val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
   236   }
   237 
   238   final class Nodes private(graph: Graph[Node.Name, Node])
   239   {
   240     def get_name(s: String): Option[Node.Name] =
   241       graph.keys.find(name => name.node == s)
   242 
   243     def apply(name: Node.Name): Node =
   244       graph.default_node(name, Node.empty).get_node(name)
   245 
   246     def + (entry: (Node.Name, Node)): Nodes =
   247     {
   248       val (name, node) = entry
   249       val imports = node.header.imports
   250       val graph1 =
   251         (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty))
   252       val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
   253       val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name))
   254       new Nodes(graph3.map_node(name, _ => node))
   255     }
   256 
   257     def entries: Iterator[(Node.Name, Node)] =
   258       graph.entries.map({ case (name, (node, _)) => (name, node) })
   259 
   260     def thy_load_commands(file_name: Node.Name): List[Command] =
   261       (for {
   262         (_, node) <- entries
   263         cmd <- node.thy_load_commands.iterator
   264         name <- cmd.blobs_names.iterator
   265         if name == file_name
   266       } yield cmd).toList
   267 
   268     def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names)
   269     def topological_order: List[Node.Name] = graph.topological_order
   270   }
   271 
   272 
   273 
   274   /** versioning **/
   275 
   276   /* particular document versions */
   277 
   278   object Version
   279   {
   280     val init: Version = new Version()
   281 
   282     def make(syntax: Outer_Syntax, nodes: Nodes): Version =
   283       new Version(Document_ID.make(), syntax, nodes)
   284   }
   285 
   286   final class Version private(
   287     val id: Document_ID.Version = Document_ID.none,
   288     val syntax: Outer_Syntax = Outer_Syntax.empty,
   289     val nodes: Nodes = Nodes.empty)
   290   {
   291     def is_init: Boolean = id == Document_ID.none
   292   }
   293 
   294 
   295   /* changes of plain text, eventually resulting in document edits */
   296 
   297   object Change
   298   {
   299     val init: Change = new Change()
   300 
   301     def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
   302       new Change(Some(previous), edits, version)
   303   }
   304 
   305   final class Change private(
   306     val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
   307     val edits: List[Edit_Text] = Nil,
   308     val version: Future[Version] = Future.value(Version.init))
   309   {
   310     def is_finished: Boolean =
   311       (previous match { case None => true case Some(future) => future.is_finished }) &&
   312       version.is_finished
   313 
   314     def truncate: Change = new Change(None, Nil, version)
   315   }
   316 
   317 
   318   /* history navigation */
   319 
   320   object History
   321   {
   322     val init: History = new History()
   323   }
   324 
   325   final class History private(
   326     val undo_list: List[Change] = List(Change.init))  // non-empty list
   327   {
   328     def tip: Change = undo_list.head
   329     def + (change: Change): History = new History(change :: undo_list)
   330 
   331     def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
   332     {
   333       val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
   334       val (retained, dropped) = undo_list.splitAt(n max retain)
   335 
   336       retained.splitAt(retained.length - 1) match {
   337         case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
   338         case _ => None
   339       }
   340     }
   341   }
   342 
   343 
   344 
   345   /** global state -- document structure, execution process, editing history **/
   346 
   347   object Snapshot
   348   {
   349     val init = State.init.snapshot()
   350   }
   351 
   352   abstract class Snapshot
   353   {
   354     val state: State
   355     val version: Version
   356     val is_outdated: Boolean
   357     def convert(i: Text.Offset): Text.Offset
   358     def revert(i: Text.Offset): Text.Offset
   359     def convert(range: Text.Range): Text.Range
   360     def revert(range: Text.Range): Text.Range
   361 
   362     val node_name: Node.Name
   363     val node: Node
   364     val thy_load_commands: List[Command]
   365     def eq_content(other: Snapshot): Boolean
   366     def cumulate_markup[A](
   367       range: Text.Range,
   368       info: A,
   369       elements: Option[Set[String]],
   370       result: Command.State => (A, Text.Markup) => Option[A]): List[Text.Info[A]]
   371     def select_markup[A](
   372       range: Text.Range,
   373       elements: Option[Set[String]],
   374       result: Command.State => Text.Markup => Option[A]): List[Text.Info[A]]
   375   }
   376 
   377   type Assign_Update =
   378     List[(Document_ID.Command, List[Document_ID.Exec])]  // update of exec state assignment
   379 
   380   object State
   381   {
   382     class Fail(state: State) extends Exception
   383 
   384     object Assignment
   385     {
   386       val init: Assignment = new Assignment()
   387     }
   388 
   389     final class Assignment private(
   390       val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty,
   391       val is_finished: Boolean = false)
   392     {
   393       def check_finished: Assignment = { require(is_finished); this }
   394       def unfinished: Assignment = new Assignment(command_execs, false)
   395 
   396       def assign(update: Assign_Update): Assignment =
   397       {
   398         require(!is_finished)
   399         val command_execs1 =
   400           (command_execs /: update) {
   401             case (res, (command_id, exec_ids)) =>
   402               if (exec_ids.isEmpty) res - command_id
   403               else res + (command_id -> exec_ids)
   404           }
   405         new Assignment(command_execs1, true)
   406       }
   407     }
   408 
   409     val init: State =
   410       State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2
   411   }
   412 
   413   final case class State private(
   414     val versions: Map[Document_ID.Version, Version] = Map.empty,
   415     val blobs: Set[SHA1.Digest] = Set.empty,   // inlined files
   416     val commands: Map[Document_ID.Command, Command.State] = Map.empty,  // static markup from define_command
   417     val execs: Map[Document_ID.Exec, Command.State] = Map.empty,  // dynamic markup from execution
   418     val assignments: Map[Document_ID.Version, State.Assignment] = Map.empty,
   419     val history: History = History.init)
   420   {
   421     private def fail[A]: A = throw new State.Fail(this)
   422 
   423     def define_version(version: Version, assignment: State.Assignment): State =
   424     {
   425       val id = version.id
   426       copy(versions = versions + (id -> version),
   427         assignments = assignments + (id -> assignment.unfinished))
   428     }
   429 
   430     def define_blob(digest: SHA1.Digest): State = copy(blobs = blobs + digest)
   431     def defined_blob(digest: SHA1.Digest): Boolean = blobs.contains(digest)
   432 
   433     def define_command(command: Command): State =
   434     {
   435       val id = command.id
   436       copy(commands = commands + (id -> command.init_state))
   437     }
   438 
   439     def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id)
   440 
   441     def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] =
   442       commands.get(id) orElse execs.get(id) match {
   443         case None => None
   444         case Some(st) =>
   445           val command = st.command
   446           val node = version.nodes(command.node_name)
   447           if (node.commands.contains(command)) Some((node, command)) else None
   448       }
   449 
   450     def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail)
   451     def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail)
   452     def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail)
   453     def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail)
   454 
   455     def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) =
   456       execs.get(id) match {
   457         case Some(st) =>
   458           val new_st = st + (id, message)
   459           (new_st, copy(execs = execs + (id -> new_st)))
   460         case None =>
   461           commands.get(id) match {
   462             case Some(st) =>
   463               val new_st = st + (id, message)
   464               (new_st, copy(commands = commands + (id -> new_st)))
   465             case None => fail
   466           }
   467       }
   468 
   469     def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) =
   470     {
   471       val version = the_version(id)
   472 
   473       def upd(exec_id: Document_ID.Exec, st: Command.State)
   474           : Option[(Document_ID.Exec, Command.State)] =
   475         if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st)
   476 
   477       val (changed_commands, new_execs) =
   478         ((Nil: List[Command], execs) /: update) {
   479           case ((commands1, execs1), (command_id, exec)) =>
   480             val st = the_static_state(command_id)
   481             val command = st.command
   482             val commands2 = command :: commands1
   483             val execs2 =
   484               exec match {
   485                 case Nil => execs1
   486                 case eval_id :: print_ids =>
   487                   execs1 ++ upd(eval_id, st) ++
   488                     (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up)
   489               }
   490             (commands2, execs2)
   491         }
   492       val new_assignment = the_assignment(version).assign(update)
   493       val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
   494 
   495       (changed_commands, new_state)
   496     }
   497 
   498     def is_assigned(version: Version): Boolean =
   499       assignments.get(version.id) match {
   500         case Some(assgn) => assgn.is_finished
   501         case None => false
   502       }
   503 
   504     def is_stable(change: Change): Boolean =
   505       change.is_finished && is_assigned(change.version.get_finished)
   506 
   507     def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
   508     def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
   509     def tip_stable: Boolean = is_stable(history.tip)
   510     def tip_version: Version = history.tip.version.get_finished
   511 
   512     def continue_history(
   513         previous: Future[Version],
   514         edits: List[Edit_Text],
   515         version: Future[Version]): (Change, State) =
   516     {
   517       val change = Change.make(previous, edits, version)
   518       (change, copy(history = history + change))
   519     }
   520 
   521     def prune_history(retain: Int = 0): (List[Version], State) =
   522     {
   523       history.prune(is_stable, retain) match {
   524         case Some((dropped, history1)) =>
   525           val dropped_versions = dropped.map(change => change.version.get_finished)
   526           val state1 = copy(history = history1)
   527           (dropped_versions, state1)
   528         case None => fail
   529       }
   530     }
   531 
   532     def removed_versions(removed: List[Document_ID.Version]): State =
   533     {
   534       val versions1 = versions -- removed
   535       val assignments1 = assignments -- removed
   536       var blobs1 = Set.empty[SHA1.Digest]
   537       var commands1 = Map.empty[Document_ID.Command, Command.State]
   538       var execs1 = Map.empty[Document_ID.Exec, Command.State]
   539       for {
   540         (version_id, version) <- versions1.iterator
   541         command_execs = assignments1(version_id).command_execs
   542         (_, node) <- version.nodes.entries
   543         command <- node.commands.iterator
   544       } {
   545         for (digest <- command.blobs_digests; if !blobs1.contains(digest))
   546           blobs1 += digest
   547 
   548         if (!commands1.isDefinedAt(command.id))
   549           commands.get(command.id).foreach(st => commands1 += (command.id -> st))
   550 
   551         for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
   552           if (!execs1.isDefinedAt(exec_id))
   553             execs.get(exec_id).foreach(st => execs1 += (exec_id -> st))
   554         }
   555       }
   556       copy(versions = versions1, blobs = blobs1, commands = commands1, execs = execs1,
   557         assignments = assignments1)
   558     }
   559 
   560     def command_state(version: Version, command: Command): Command.State =
   561     {
   562       require(is_assigned(version))
   563       try {
   564         the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil)
   565           .map(the_dynamic_state(_)) match {
   566             case eval_state :: print_states => (eval_state /: print_states)(_ ++ _)
   567             case Nil => fail
   568           }
   569       }
   570       catch {
   571         case _: State.Fail =>
   572           try { the_static_state(command.id) }
   573           catch { case _: State.Fail => command.init_state }
   574       }
   575     }
   576 
   577     def markup_to_XML(version: Version, node: Node, filter: XML.Elem => Boolean): XML.Body =
   578       node.commands.toList.map(cmd => command_state(version, cmd).markup_to_XML(filter)).flatten
   579 
   580     // persistent user-view
   581     def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
   582       : Snapshot =
   583     {
   584       val stable = recent_stable
   585       val latest = history.tip
   586 
   587       // FIXME proper treatment of removed nodes
   588       val edits =
   589         (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
   590             (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
   591       lazy val reverse_edits = edits.reverse
   592 
   593       new Snapshot
   594       {
   595         /* global information */
   596 
   597         val state = State.this
   598         val version = stable.version.get_finished
   599         val is_outdated = !(pending_edits.isEmpty && latest == stable)
   600 
   601         def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
   602         def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
   603         def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
   604         def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
   605 
   606 
   607         /* local node content */
   608 
   609         val node_name = name
   610         val node = version.nodes(name)
   611 
   612         val thy_load_commands: List[Command] =
   613           if (node_name.is_theory) Nil
   614           else version.nodes.thy_load_commands(node_name)
   615 
   616         def eq_content(other: Snapshot): Boolean =
   617           !is_outdated && !other.is_outdated &&
   618             node.commands.size == other.node.commands.size &&
   619             ((node.commands.iterator zip other.node.commands.iterator) forall {
   620               case (cmd1, cmd2) =>
   621                 state.command_state(version, cmd1) eq_content
   622                   other.state.command_state(other.version, cmd2)
   623             })
   624 
   625         def cumulate_markup[A](range: Text.Range, info: A, elements: Option[Set[String]],
   626           result: Command.State => (A, Text.Markup) => Option[A]): List[Text.Info[A]] =
   627         {
   628           val former_range = revert(range)
   629           thy_load_commands match {
   630             case thy_load_command :: _ =>
   631               val file_name = node_name.node
   632               (for {
   633                 chunk <- thy_load_command.chunks.get(file_name).iterator
   634                 st = state.command_state(version, thy_load_command)
   635                 res = result(st)
   636                 Text.Info(r0, a) <- st.get_markup(file_name).cumulate[A](
   637                   former_range.restrict(chunk.range), info, elements,
   638                   { case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0), b)) }
   639                 ).iterator
   640               } yield Text.Info(convert(r0), a)).toList
   641 
   642             case _ =>
   643               (for {
   644                 (command, command_start) <- node.command_range(former_range)
   645                 st = state.command_state(version, command)
   646                 res = result(st)
   647                 Text.Info(r0, a) <- st.markup.cumulate[A](
   648                   (former_range - command_start).restrict(command.range), info, elements,
   649                   { case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b)) }
   650                 ).iterator
   651               } yield Text.Info(convert(r0 + command_start), a)).toList
   652           }
   653         }
   654 
   655         def select_markup[A](range: Text.Range, elements: Option[Set[String]],
   656           result: Command.State => Text.Markup => Option[A]): List[Text.Info[A]] =
   657         {
   658           def result1(st: Command.State): (Option[A], Text.Markup) => Option[Option[A]] =
   659           {
   660             val res = result(st)
   661             (_: Option[A], x: Text.Markup) =>
   662               res(x) match {
   663                 case None => None
   664                 case some => Some(some)
   665               }
   666           }
   667           for (Text.Info(r, Some(x)) <- cumulate_markup(range, None, elements, result1))
   668             yield Text.Info(r, x)
   669         }
   670       }
   671     }
   672   }
   673 }
   674