src/Tools/jEdit/src/rendering.scala
author wenzelm
Wed Aug 07 19:59:58 2013 +0200 (2013-08-07 ago)
changeset 52900 d29bf6db8a2d
parent 52890 36e2c0c308eb
child 52980 28f59ca8ce78
permissions -rw-r--r--
more elementary list structures for markup tree traversal;
     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(_, elem)) =>
   158               if (elem.name == Markup.WARNING || elem.name == Markup.ERROR)
   159                 Some((status, pri max Rendering.message_pri(elem.name)))
   160               else if (overview_include(elem.name))
   161                 Some((Protocol.command_status(status, elem.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, elem) =>
   193             if (highlight_include(elem.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) =>
   261             if (elem.name == Markup.BROWSER ||
   262                 elem.name == Markup.GRAPHVIEW ||
   263                 elem.name == Markup.SENDBACK)
   264               Some(Text.Info(snapshot.convert(info_range), elem))
   265             else None
   266         }) match { case Text.Info(_, info) :: _ => Some(info) case _ => None }
   267 
   268 
   269   def command_results(range: Text.Range): Command.Results =
   270   {
   271     val results =
   272       snapshot.select_markup[Command.Results](range, None, command_state =>
   273         { case _ => Some(command_state.results) }).map(_.info)
   274     (Command.Results.empty /: results)(_ ++ _)
   275   }
   276 
   277   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   278   {
   279     val results =
   280       snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
   281         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
   282         {
   283           case (msgs, Text.Info(info_range,
   284             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   285           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   286             val entry: Command.Results.Entry =
   287               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   288             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   289 
   290           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   291           if !body.isEmpty =>
   292             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   293             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   294 
   295           case _ => None
   296         }).flatMap(_.info)
   297     if (results.isEmpty) None
   298     else {
   299       val r = Text.Range(results.head.range.start, results.last.range.stop)
   300       val msgs = Command.Results.make(results.map(_.info))
   301       Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
   302     }
   303   }
   304 
   305 
   306   private val tooltips: Map[String, String] =
   307     Map(
   308       Markup.SORT -> "sort",
   309       Markup.TYP -> "type",
   310       Markup.TERM -> "term",
   311       Markup.PROP -> "proposition",
   312       Markup.TOKEN_RANGE -> "inner syntax token",
   313       Markup.FREE -> "free variable",
   314       Markup.SKOLEM -> "skolem variable",
   315       Markup.BOUND -> "bound variable",
   316       Markup.VAR -> "schematic variable",
   317       Markup.TFREE -> "free type variable",
   318       Markup.TVAR -> "schematic type variable",
   319       Markup.ML_SOURCE -> "ML source",
   320       Markup.DOCUMENT_SOURCE -> "document source")
   321 
   322   private val tooltip_elements =
   323     Set(Markup.TIMING, Markup.ENTITY, Markup.SORTING, Markup.TYPING,
   324       Markup.ML_TYPING, Markup.PATH) ++ tooltips.keys
   325 
   326   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   327     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   328 
   329   private val timing_threshold: Double = options.real("jedit_timing_threshold")
   330 
   331   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   332   {
   333     def add(prev: Text.Info[(Timing, List[(Boolean, XML.Tree)])],
   334       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, List[(Boolean, XML.Tree)])] =
   335     {
   336       val r = snapshot.convert(r0)
   337       val (t, info) = prev.info
   338       if (prev.range == r) Text.Info(r, (t, p :: info)) else Text.Info(r, (t, List(p)))
   339     }
   340 
   341     val results =
   342       snapshot.cumulate_markup[Text.Info[(Timing, List[(Boolean, XML.Tree)])]](
   343         range, Text.Info(range, (Timing.zero, Nil)), Some(tooltip_elements), _ =>
   344         {
   345           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   346             Some(Text.Info(r, (t1 + t2, info)))
   347           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   348             val kind1 = space_explode('_', kind).mkString(" ")
   349             val txt1 = kind1 + " " + quote(name)
   350             val t = prev.info._1
   351             val txt2 =
   352               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   353                 "\n" + t.message
   354               else ""
   355             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   356           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   357           if Path.is_ok(name) =>
   358             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   359             Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
   360           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   361           if name == Markup.SORTING || name == Markup.TYPING =>
   362             Some(add(prev, r, (true, pretty_typing("::", body))))
   363           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   364             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   365           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   366             if (tooltips.isDefinedAt(name))
   367               Some(add(prev, r, (true, XML.Text(tooltips(name)))))
   368             else None
   369         }).map(_.info)
   370 
   371     results.map(_.info).flatMap(_._2) match {
   372       case Nil => None
   373       case tips =>
   374         val r = Text.Range(results.head.range.start, results.last.range.stop)
   375         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   376         Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
   377     }
   378   }
   379 
   380   val tooltip_margin: Int = options.int("jedit_tooltip_margin")
   381   val tooltip_bounds: Double = (options.real("jedit_tooltip_bounds") max 0.2) min 0.8
   382 
   383   lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
   384   lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
   385 
   386 
   387   private lazy val gutter_icons = Map(
   388     Rendering.information_pri -> JEdit_Lib.load_icon(options.string("gutter_information_icon")),
   389     Rendering.warning_pri -> JEdit_Lib.load_icon(options.string("gutter_warning_icon")),
   390     Rendering.legacy_pri -> JEdit_Lib.load_icon(options.string("gutter_legacy_icon")),
   391     Rendering.error_pri -> JEdit_Lib.load_icon(options.string("gutter_error_icon")))
   392 
   393   private val gutter_elements = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   394 
   395   def gutter_message(range: Text.Range): Option[Icon] =
   396   {
   397     val results =
   398       snapshot.cumulate_markup[Int](range, 0, Some(gutter_elements), _ =>
   399         {
   400           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _),
   401               List(XML.Elem(Markup(Markup.INFORMATION, _), _))))) =>
   402             Some(pri max Rendering.information_pri)
   403           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   404             body match {
   405               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   406                 Some(pri max Rendering.legacy_pri)
   407               case _ =>
   408                 Some(pri max Rendering.warning_pri)
   409             }
   410           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   411             Some(pri max Rendering.error_pri)
   412           case _ => None
   413         })
   414     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   415     gutter_icons.get(pri)
   416   }
   417 
   418 
   419   private val squiggly_colors = Map(
   420     Rendering.writeln_pri -> writeln_color,
   421     Rendering.information_pri -> information_color,
   422     Rendering.warning_pri -> warning_color,
   423     Rendering.error_pri -> error_color)
   424 
   425   private val squiggly_include = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   426 
   427   def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
   428   {
   429     val results =
   430       snapshot.cumulate_markup[Int](range, 0, Some(squiggly_include), _ =>
   431         {
   432           case (pri, Text.Info(_, elem)) =>
   433             if (Protocol.is_information(elem))
   434               Some(pri max Rendering.information_pri)
   435             else if (squiggly_include.contains(elem.name))
   436               Some(pri max Rendering.message_pri(elem.name))
   437             else None
   438         })
   439     for {
   440       Text.Info(r, pri) <- results
   441       color <- squiggly_colors.get(pri)
   442     } yield Text.Info(r, color)
   443   }
   444 
   445 
   446   private val message_colors = Map(
   447     Rendering.writeln_pri -> writeln_message_color,
   448     Rendering.information_pri -> information_message_color,
   449     Rendering.tracing_pri -> tracing_message_color,
   450     Rendering.warning_pri -> warning_message_color,
   451     Rendering.error_pri -> error_message_color)
   452 
   453   private val line_background_elements =
   454     Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE,
   455       Markup.ERROR_MESSAGE, Markup.INFORMATION)
   456 
   457   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   458   {
   459     val results =
   460       snapshot.cumulate_markup[Int](range, 0, Some(line_background_elements), _ =>
   461         {
   462           case (pri, Text.Info(_, elem)) =>
   463             val name = elem.name
   464             if (name == Markup.INFORMATION)
   465               Some(pri max Rendering.information_pri)
   466             else if (name == Markup.WRITELN_MESSAGE || name == Markup.TRACING_MESSAGE ||
   467                 elem.name == Markup.WARNING_MESSAGE || name == Markup.ERROR_MESSAGE)
   468               Some(pri max Rendering.message_pri(name))
   469             else None
   470         })
   471     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   472 
   473     val is_separator = pri > 0 &&
   474       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
   475         {
   476           case (_, Text.Info(_, elem)) =>
   477             if (elem.name == Markup.SEPARATOR) Some(true) else None
   478         }).exists(_.info)
   479 
   480     message_colors.get(pri).map((_, is_separator))
   481   }
   482 
   483 
   484   def output_messages(st: Command.State): List[XML.Tree] =
   485     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   486 
   487 
   488   private val background1_include =
   489     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   490       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   491       active_include
   492 
   493   def background1(range: Text.Range): List[Text.Info[Color]] =
   494   {
   495     if (snapshot.is_outdated) List(Text.Info(range, outdated_color))
   496     else
   497       for {
   498         Text.Info(r, result) <-
   499           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   500             range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
   501             {
   502               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   503               if (Protocol.command_status_markup(markup.name)) =>
   504                 Some((Some(Protocol.command_status(status, markup)), color))
   505               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   506                 Some((None, Some(bad_color)))
   507               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   508                 Some((None, Some(intensify_color)))
   509               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   510                 command_state.results.get(serial) match {
   511                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   512                     Some((None, Some(active_result_color)))
   513                   case _ =>
   514                     Some((None, Some(active_color)))
   515                 }
   516               case (_, Text.Info(_, elem)) =>
   517                 if (active_include(elem.name)) Some((None, Some(active_color)))
   518                 else None
   519             })
   520         color <-
   521           (result match {
   522             case (Some(status), opt_color) =>
   523               if (status.is_unprocessed) Some(unprocessed1_color)
   524               else if (status.is_running) Some(running1_color)
   525               else opt_color
   526             case (_, opt_color) => opt_color
   527           })
   528       } yield Text.Info(r, color)
   529   }
   530 
   531 
   532   def background2(range: Text.Range): List[Text.Info[Color]] =
   533     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
   534       {
   535         case Text.Info(_, elem) =>
   536           if (elem.name == Markup.TOKEN_RANGE) Some(light_color) else None
   537       })
   538 
   539 
   540   def bullet(range: Text.Range): List[Text.Info[Color]] =
   541     snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
   542       {
   543         case Text.Info(_, elem) =>
   544           if (elem.name == Markup.BULLET) Some(bullet_color) else None
   545       })
   546 
   547 
   548   private val foreground_include =
   549     Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
   550 
   551   def foreground(range: Text.Range): List[Text.Info[Color]] =
   552     snapshot.select_markup(range, Some(foreground_include), _ =>
   553       {
   554         case Text.Info(_, elem) =>
   555           if (elem.name == Markup.ANTIQ) Some(antiquoted_color)
   556           else if (foreground_include.contains(elem.name)) Some(quoted_color)
   557           else 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): List[Text.Info[Color]] =
   588   {
   589     if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
   590     else
   591       snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
   592         {
   593           case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
   594         })
   595   }
   596 
   597 
   598   /* nested text structure -- folds */
   599 
   600   private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   601 
   602   def fold_depth(range: Text.Range): List[Text.Info[Int]] =
   603     snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
   604       {
   605         case (depth, Text.Info(_, elem)) =>
   606           if (fold_depth_include(elem.name)) Some(depth + 1) else None
   607       })
   608 }