src/Tools/jEdit/src/rendering.scala
author wenzelm
Wed Aug 07 13:46:32 2013 +0200 (2013-08-07 ago)
changeset 52889 ea3338812e67
parent 52873 9e934d4fff00
child 52890 36e2c0c308eb
permissions -rw-r--r--
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
     1 /*  Title:      Tools/jEdit/src/rendering.scala
     2     Author:     Makarius
     3 
     4 Isabelle-specific implementation of quasi-abstract rendering and
     5 markup interpretation.
     6 */
     7 
     8 package isabelle.jedit
     9 
    10 
    11 import isabelle._
    12 
    13 import java.awt.Color
    14 import javax.swing.Icon
    15 
    16 import org.gjt.sp.jedit.syntax.{Token => JEditToken}
    17 import org.gjt.sp.jedit.jEdit
    18 
    19 import scala.collection.immutable.SortedMap
    20 
    21 
    22 object Rendering
    23 {
    24   def apply(snapshot: Document.Snapshot, options: Options): Rendering =
    25     new Rendering(snapshot, options)
    26 
    27 
    28   /* message priorities */
    29 
    30   private val writeln_pri = 1
    31   private val information_pri = 2
    32   private val tracing_pri = 3
    33   private val warning_pri = 4
    34   private val legacy_pri = 5
    35   private val error_pri = 6
    36 
    37   private val message_pri = Map(
    38     Markup.WRITELN -> writeln_pri, Markup.WRITELN_MESSAGE -> writeln_pri,
    39     Markup.TRACING -> tracing_pri, Markup.TRACING_MESSAGE -> tracing_pri,
    40     Markup.WARNING -> warning_pri, Markup.WARNING_MESSAGE -> warning_pri,
    41     Markup.ERROR -> error_pri, Markup.ERROR_MESSAGE -> error_pri)
    42 
    43 
    44   /* jEdit font */
    45 
    46   def font_family(): String = jEdit.getProperty("view.font")
    47 
    48   def font_size(scale: String): Float =
    49     (jEdit.getIntegerProperty("view.fontsize", 16) * PIDE.options.real(scale)).toFloat
    50 
    51 
    52   /* token markup -- text styles */
    53 
    54   private val command_style: Map[String, Byte] =
    55   {
    56     import JEditToken._
    57     Map[String, Byte](
    58       Keyword.THY_END -> KEYWORD2,
    59       Keyword.THY_SCRIPT -> LABEL,
    60       Keyword.PRF_SCRIPT -> DIGIT,
    61       Keyword.PRF_ASM -> KEYWORD3,
    62       Keyword.PRF_ASM_GOAL -> KEYWORD3
    63     ).withDefaultValue(KEYWORD1)
    64   }
    65 
    66   private val token_style: Map[Token.Kind.Value, Byte] =
    67   {
    68     import JEditToken._
    69     Map[Token.Kind.Value, Byte](
    70       Token.Kind.KEYWORD -> KEYWORD2,
    71       Token.Kind.IDENT -> NULL,
    72       Token.Kind.LONG_IDENT -> NULL,
    73       Token.Kind.SYM_IDENT -> NULL,
    74       Token.Kind.VAR -> NULL,
    75       Token.Kind.TYPE_IDENT -> NULL,
    76       Token.Kind.TYPE_VAR -> NULL,
    77       Token.Kind.NAT -> NULL,
    78       Token.Kind.FLOAT -> NULL,
    79       Token.Kind.STRING -> LITERAL1,
    80       Token.Kind.ALT_STRING -> LITERAL2,
    81       Token.Kind.VERBATIM -> COMMENT3,
    82       Token.Kind.SPACE -> NULL,
    83       Token.Kind.COMMENT -> COMMENT1,
    84       Token.Kind.ERROR -> INVALID
    85     ).withDefaultValue(NULL)
    86   }
    87 
    88   def token_markup(syntax: Outer_Syntax, token: Token): Byte =
    89     if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
    90     else if (token.is_operator) JEditToken.OPERATOR
    91     else token_style(token.kind)
    92 }
    93 
    94 
    95 class Rendering private(val snapshot: Document.Snapshot, val options: Options)
    96 {
    97   /* colors */
    98 
    99   def color_value(s: String): Color = Color_Value(options.string(s))
   100 
   101   val outdated_color = color_value("outdated_color")
   102   val unprocessed_color = color_value("unprocessed_color")
   103   val unprocessed1_color = color_value("unprocessed1_color")
   104   val running_color = color_value("running_color")
   105   val running1_color = color_value("running1_color")
   106   val light_color = color_value("light_color")
   107   val bullet_color = color_value("bullet_color")
   108   val tooltip_color = color_value("tooltip_color")
   109   val writeln_color = color_value("writeln_color")
   110   val information_color = color_value("information_color")
   111   val warning_color = color_value("warning_color")
   112   val error_color = color_value("error_color")
   113   val error1_color = color_value("error1_color")
   114   val writeln_message_color = color_value("writeln_message_color")
   115   val information_message_color = color_value("information_message_color")
   116   val tracing_message_color = color_value("tracing_message_color")
   117   val warning_message_color = color_value("warning_message_color")
   118   val error_message_color = color_value("error_message_color")
   119   val bad_color = color_value("bad_color")
   120   val intensify_color = color_value("intensify_color")
   121   val quoted_color = color_value("quoted_color")
   122   val antiquoted_color = color_value("antiquoted_color")
   123   val highlight_color = color_value("highlight_color")
   124   val hyperlink_color = color_value("hyperlink_color")
   125   val active_color = color_value("active_color")
   126   val active_hover_color = color_value("active_hover_color")
   127   val active_result_color = color_value("active_result_color")
   128   val keyword1_color = color_value("keyword1_color")
   129   val keyword2_color = color_value("keyword2_color")
   130 
   131   val tfree_color = color_value("tfree_color")
   132   val tvar_color = color_value("tvar_color")
   133   val free_color = color_value("free_color")
   134   val skolem_color = color_value("skolem_color")
   135   val bound_color = color_value("bound_color")
   136   val var_color = color_value("var_color")
   137   val inner_numeral_color = color_value("inner_numeral_color")
   138   val inner_quoted_color = color_value("inner_quoted_color")
   139   val inner_comment_color = color_value("inner_comment_color")
   140   val dynamic_color = color_value("dynamic_color")
   141 
   142 
   143   /* command overview */
   144 
   145   val overview_limit = options.int("jedit_text_overview_limit")
   146 
   147   private val overview_include = Protocol.command_status_markup + Markup.WARNING + Markup.ERROR
   148 
   149   def overview_color(range: Text.Range): Option[Color] =
   150   {
   151     if (snapshot.is_outdated) None
   152     else {
   153       val results =
   154         snapshot.cumulate_markup[(Protocol.Status, Int)](
   155           range, (Protocol.Status.init, 0), Some(overview_include), _ =>
   156           {
   157             case ((status, pri), Text.Info(_, XML.Elem(markup, _))) =>
   158               if (markup.name == Markup.WARNING || markup.name == Markup.ERROR)
   159                 Some((status, pri max Rendering.message_pri(markup.name)))
   160               else if (overview_include(markup.name))
   161                 Some((Protocol.command_status(status, markup), pri))
   162               else None
   163           })
   164       if (results.isEmpty) None
   165       else {
   166         val (status, pri) =
   167           ((Protocol.Status.init, 0) /: results) {
   168             case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
   169 
   170         if (status.is_running) Some(running_color)
   171         else if (pri == Rendering.warning_pri) Some(warning_color)
   172         else if (pri == Rendering.error_pri) Some(error_color)
   173         else if (status.is_unprocessed) Some(unprocessed_color)
   174         else if (status.is_failed) Some(error_color)
   175         else None
   176       }
   177     }
   178   }
   179 
   180 
   181   /* markup selectors */
   182 
   183   private val highlight_include =
   184     Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   185       Markup.ENTITY, Markup.PATH, Markup.SORTING, Markup.TYPING, Markup.FREE, Markup.SKOLEM,
   186       Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOCUMENT_SOURCE)
   187 
   188   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   189   {
   190     snapshot.select_markup(range, Some(highlight_include), _ =>
   191         {
   192           case Text.Info(info_range, XML.Elem(markup, _)) =>
   193             if (highlight_include(markup.name))
   194               Some(Text.Info(snapshot.convert(info_range), highlight_color))
   195             else None
   196         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   197   }
   198 
   199 
   200   private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH)
   201 
   202   def hyperlink(range: Text.Range): Option[Text.Info[Hyperlink]] =
   203   {
   204     snapshot.cumulate_markup[List[Text.Info[Hyperlink]]](range, Nil, Some(hyperlink_include), _ =>
   205         {
   206           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   207           if Path.is_ok(name) =>
   208             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   209             val link = Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, 0, 0))
   210             Some(link :: links)
   211 
   212           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   213           if !props.exists(
   214             { case (Markup.KIND, Markup.ML_OPEN) => true
   215               case (Markup.KIND, Markup.ML_STRUCT) => true
   216               case _ => false }) =>
   217 
   218             props match {
   219               case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
   220                 Isabelle_System.source_file(Path.explode(name)) match {
   221                   case Some(path) =>
   222                     val jedit_file = Isabelle_System.platform_path(path)
   223                     val link =
   224                       Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, line, 0))
   225                     Some(link :: links)
   226                   case None => None
   227                 }
   228 
   229               case Position.Def_Id_Offset(id, offset) =>
   230                 snapshot.state.find_command(snapshot.version, id) match {
   231                   case Some((node, command)) =>
   232                     val sources =
   233                       node.commands.iterator.takeWhile(_ != command).map(_.source) ++
   234                         Iterator.single(command.source(Text.Range(0, command.decode(offset))))
   235                     val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
   236                     val link =
   237                       Text.Info(snapshot.convert(info_range),
   238                         Hyperlink(command.node_name.node, line, column))
   239                     Some(link :: links)
   240                   case None => None
   241                 }
   242 
   243               case _ => None
   244             }
   245 
   246           case _ => None
   247         }) match { case Text.Info(_, info :: _) #:: _ => Some(info) case _ => None }
   248   }
   249 
   250 
   251   private val active_include =
   252     Set(Markup.BROWSER, Markup.GRAPHVIEW, Markup.SENDBACK, Markup.DIALOG)
   253 
   254   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   255     snapshot.select_markup(range, Some(active_include), command_state =>
   256         {
   257           case Text.Info(info_range, elem @ Protocol.Dialog(_, serial, _))
   258           if !command_state.results.defined(serial) =>
   259             Some(Text.Info(snapshot.convert(info_range), elem))
   260           case Text.Info(info_range, elem @ XML.Elem(Markup(name, _), _)) =>
   261             if (name == Markup.BROWSER || name == Markup.GRAPHVIEW || name == Markup.SENDBACK)
   262               Some(Text.Info(snapshot.convert(info_range), elem))
   263             else None
   264         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   265 
   266 
   267   def command_results(range: Text.Range): Command.Results =
   268   {
   269     val results =
   270       snapshot.select_markup[Command.Results](range, None, command_state =>
   271         { case _ => Some(command_state.results) }).map(_.info)
   272     (Command.Results.empty /: results)(_ ++ _)
   273   }
   274 
   275   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   276   {
   277     val results =
   278       snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
   279         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
   280         {
   281           case (msgs, Text.Info(info_range,
   282             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   283           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   284             val entry: Command.Results.Entry =
   285               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   286             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   287 
   288           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   289           if !body.isEmpty =>
   290             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   291             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   292 
   293           case _ => None
   294         }).toList.flatMap(_.info)
   295     if (results.isEmpty) None
   296     else {
   297       val r = Text.Range(results.head.range.start, results.last.range.stop)
   298       val msgs = Command.Results.make(results.map(_.info))
   299       Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
   300     }
   301   }
   302 
   303 
   304   private val tooltips: Map[String, String] =
   305     Map(
   306       Markup.SORT -> "sort",
   307       Markup.TYP -> "type",
   308       Markup.TERM -> "term",
   309       Markup.PROP -> "proposition",
   310       Markup.TOKEN_RANGE -> "inner syntax token",
   311       Markup.FREE -> "free variable",
   312       Markup.SKOLEM -> "skolem variable",
   313       Markup.BOUND -> "bound variable",
   314       Markup.VAR -> "schematic variable",
   315       Markup.TFREE -> "free type variable",
   316       Markup.TVAR -> "schematic type variable",
   317       Markup.ML_SOURCE -> "ML source",
   318       Markup.DOCUMENT_SOURCE -> "document source")
   319 
   320   private val tooltip_elements =
   321     Set(Markup.TIMING, Markup.ENTITY, Markup.SORTING, Markup.TYPING,
   322       Markup.ML_TYPING, Markup.PATH) ++ tooltips.keys
   323 
   324   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   325     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   326 
   327   private val timing_threshold: Double = options.real("jedit_timing_threshold")
   328 
   329   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   330   {
   331     def add(prev: Text.Info[(Timing, List[(Boolean, XML.Tree)])],
   332       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, List[(Boolean, XML.Tree)])] =
   333     {
   334       val r = snapshot.convert(r0)
   335       val (t, info) = prev.info
   336       if (prev.range == r) Text.Info(r, (t, p :: info)) else Text.Info(r, (t, List(p)))
   337     }
   338 
   339     val results =
   340       snapshot.cumulate_markup[Text.Info[(Timing, List[(Boolean, XML.Tree)])]](
   341         range, Text.Info(range, (Timing.zero, Nil)), Some(tooltip_elements), _ =>
   342         {
   343           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   344             Some(Text.Info(r, (t1 + t2, info)))
   345           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   346             val kind1 = space_explode('_', kind).mkString(" ")
   347             val txt1 = kind1 + " " + quote(name)
   348             val t = prev.info._1
   349             val txt2 =
   350               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   351                 "\n" + t.message
   352               else ""
   353             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   354           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   355           if Path.is_ok(name) =>
   356             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   357             Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
   358           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   359           if name == Markup.SORTING || name == Markup.TYPING =>
   360             Some(add(prev, r, (true, pretty_typing("::", body))))
   361           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   362             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   363           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   364             if (tooltips.isDefinedAt(name))
   365               Some(add(prev, r, (true, XML.Text(tooltips(name)))))
   366             else None
   367         }).toList.map(_.info)
   368 
   369     results.map(_.info).flatMap(_._2) match {
   370       case Nil => None
   371       case tips =>
   372         val r = Text.Range(results.head.range.start, results.last.range.stop)
   373         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   374         Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
   375     }
   376   }
   377 
   378   val tooltip_margin: Int = options.int("jedit_tooltip_margin")
   379   val tooltip_bounds: Double = (options.real("jedit_tooltip_bounds") max 0.2) min 0.8
   380 
   381   lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
   382   lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
   383 
   384 
   385   private lazy val gutter_icons = Map(
   386     Rendering.information_pri -> JEdit_Lib.load_icon(options.string("gutter_information_icon")),
   387     Rendering.warning_pri -> JEdit_Lib.load_icon(options.string("gutter_warning_icon")),
   388     Rendering.legacy_pri -> JEdit_Lib.load_icon(options.string("gutter_legacy_icon")),
   389     Rendering.error_pri -> JEdit_Lib.load_icon(options.string("gutter_error_icon")))
   390 
   391   private val gutter_elements = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   392 
   393   def gutter_message(range: Text.Range): Option[Icon] =
   394   {
   395     val results =
   396       snapshot.cumulate_markup[Int](range, 0, Some(gutter_elements), _ =>
   397         {
   398           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _),
   399               List(XML.Elem(Markup(Markup.INFORMATION, _), _))))) =>
   400             Some(pri max Rendering.information_pri)
   401           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   402             body match {
   403               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   404                 Some(pri max Rendering.legacy_pri)
   405               case _ =>
   406                 Some(pri max Rendering.warning_pri)
   407             }
   408           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   409             Some(pri max Rendering.error_pri)
   410           case _ => None
   411         })
   412     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   413     gutter_icons.get(pri)
   414   }
   415 
   416 
   417   private val squiggly_colors = Map(
   418     Rendering.writeln_pri -> writeln_color,
   419     Rendering.information_pri -> information_color,
   420     Rendering.warning_pri -> warning_color,
   421     Rendering.error_pri -> error_color)
   422 
   423   private val squiggly_include = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   424 
   425   def squiggly_underline(range: Text.Range): Stream[Text.Info[Color]] =
   426   {
   427     val results =
   428       snapshot.cumulate_markup[Int](range, 0, Some(squiggly_include), _ =>
   429         {
   430           case (pri, Text.Info(_, msg @ XML.Elem(markup, _))) =>
   431             if (Protocol.is_information(msg))
   432               Some(pri max Rendering.information_pri)
   433             else if (squiggly_include.contains(markup.name))
   434               Some(pri max Rendering.message_pri(markup.name))
   435             else None
   436         })
   437     for {
   438       Text.Info(r, pri) <- results
   439       color <- squiggly_colors.get(pri)
   440     } yield Text.Info(r, color)
   441   }
   442 
   443 
   444   private val message_colors = Map(
   445     Rendering.writeln_pri -> writeln_message_color,
   446     Rendering.information_pri -> information_message_color,
   447     Rendering.tracing_pri -> tracing_message_color,
   448     Rendering.warning_pri -> warning_message_color,
   449     Rendering.error_pri -> error_message_color)
   450 
   451   private val line_background_elements =
   452     Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE,
   453       Markup.ERROR_MESSAGE, Markup.INFORMATION)
   454 
   455   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   456   {
   457     val results =
   458       snapshot.cumulate_markup[Int](range, 0, Some(line_background_elements), _ =>
   459         {
   460           case (pri, Text.Info(_, XML.Elem(Markup(Markup.INFORMATION, _), _))) =>
   461             Some(pri max Rendering.information_pri)
   462           case (pri, Text.Info(_, XML.Elem(Markup(name, _), _))) =>
   463             if (name == Markup.WRITELN_MESSAGE || name == Markup.TRACING_MESSAGE ||
   464                 name == Markup.WARNING_MESSAGE || name == Markup.ERROR_MESSAGE)
   465               Some(pri max Rendering.message_pri(name))
   466             else None
   467         })
   468     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   469 
   470     val is_separator = pri > 0 &&
   471       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
   472         {
   473           case (_, Text.Info(_, XML.Elem(Markup(Markup.SEPARATOR, _), _))) => Some(true)
   474           case _ => None
   475         }).exists(_.info)
   476 
   477     message_colors.get(pri).map((_, is_separator))
   478   }
   479 
   480 
   481   def output_messages(st: Command.State): List[XML.Tree] =
   482   {
   483     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   484   }
   485 
   486 
   487   private val background1_include =
   488     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   489       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   490       active_include
   491 
   492   def background1(range: Text.Range): Stream[Text.Info[Color]] =
   493   {
   494     if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
   495     else
   496       for {
   497         Text.Info(r, result) <-
   498           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   499             range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
   500             {
   501               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   502               if (Protocol.command_status_markup(markup.name)) =>
   503                 Some((Some(Protocol.command_status(status, markup)), color))
   504               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   505                 Some((None, Some(bad_color)))
   506               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   507                 Some((None, Some(intensify_color)))
   508               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   509                 command_state.results.get(serial) match {
   510                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   511                     Some((None, Some(active_result_color)))
   512                   case _ =>
   513                     Some((None, Some(active_color)))
   514                 }
   515               case (_, Text.Info(_, XML.Elem(markup, _))) =>
   516                 if (active_include(markup.name)) Some((None, Some(active_color)))
   517                 else None
   518             })
   519         color <-
   520           (result match {
   521             case (Some(status), opt_color) =>
   522               if (status.is_unprocessed) Some(unprocessed1_color)
   523               else if (status.is_running) Some(running1_color)
   524               else opt_color
   525             case (_, opt_color) => opt_color
   526           })
   527       } yield Text.Info(r, color)
   528   }
   529 
   530 
   531   def background2(range: Text.Range): Stream[Text.Info[Color]] =
   532     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
   533       {
   534         case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => Some(light_color)
   535         case _ => None
   536       })
   537 
   538 
   539   def bullet(range: Text.Range): Stream[Text.Info[Color]] =
   540     snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
   541       {
   542         case Text.Info(_, XML.Elem(Markup(Markup.BULLET, _), _)) => Some(bullet_color)
   543         case _ => None
   544       })
   545 
   546 
   547   private val foreground_elements =
   548     Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
   549 
   550   def foreground(range: Text.Range): Stream[Text.Info[Color]] =
   551     snapshot.select_markup(range, Some(foreground_elements), _ =>
   552       {
   553         case Text.Info(_, XML.Elem(Markup(Markup.STRING, _), _)) => Some(quoted_color)
   554         case Text.Info(_, XML.Elem(Markup(Markup.ALTSTRING, _), _)) => Some(quoted_color)
   555         case Text.Info(_, XML.Elem(Markup(Markup.VERBATIM, _), _)) => Some(quoted_color)
   556         case Text.Info(_, XML.Elem(Markup(Markup.ANTIQ, _), _)) => Some(antiquoted_color)
   557         case _ => None
   558       })
   559 
   560 
   561   private val text_colors: Map[String, Color] = Map(
   562       Markup.KEYWORD1 -> keyword1_color,
   563       Markup.KEYWORD2 -> keyword2_color,
   564       Markup.STRING -> Color.BLACK,
   565       Markup.ALTSTRING -> Color.BLACK,
   566       Markup.VERBATIM -> Color.BLACK,
   567       Markup.LITERAL -> keyword1_color,
   568       Markup.DELIMITER -> Color.BLACK,
   569       Markup.TFREE -> tfree_color,
   570       Markup.TVAR -> tvar_color,
   571       Markup.FREE -> free_color,
   572       Markup.SKOLEM -> skolem_color,
   573       Markup.BOUND -> bound_color,
   574       Markup.VAR -> var_color,
   575       Markup.INNER_STRING -> inner_quoted_color,
   576       Markup.INNER_COMMENT -> inner_comment_color,
   577       Markup.DYNAMIC_FACT -> dynamic_color,
   578       Markup.ML_KEYWORD -> keyword1_color,
   579       Markup.ML_DELIMITER -> Color.BLACK,
   580       Markup.ML_NUMERAL -> inner_numeral_color,
   581       Markup.ML_CHAR -> inner_quoted_color,
   582       Markup.ML_STRING -> inner_quoted_color,
   583       Markup.ML_COMMENT -> inner_comment_color)
   584 
   585   private val text_color_elements = text_colors.keySet
   586 
   587   def text_color(range: Text.Range, color: Color)
   588       : Stream[Text.Info[Color]] =
   589   {
   590     if (color == Token_Markup.hidden_color) Stream(Text.Info(range, color))
   591     else
   592       snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
   593         {
   594           case (_, Text.Info(_, XML.Elem(markup, _))) => text_colors.get(markup.name)
   595         })
   596   }
   597 
   598 
   599   /* nested text structure -- folds */
   600 
   601   private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   602 
   603   def fold_depth(range: Text.Range): Stream[Text.Info[Int]] =
   604     snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
   605       {
   606         case (depth, Text.Info(_, XML.Elem(markup, _))) =>
   607           if (fold_depth_include(markup.name)) Some(depth + 1) else None
   608       })
   609 }