src/Tools/jEdit/src/rendering.scala
author wenzelm
Mon Aug 12 17:11:27 2013 +0200 (2013-08-12 ago)
changeset 52980 28f59ca8ce78
parent 52900 d29bf6db8a2d
child 53230 6589ff56cc3c
permissions -rw-r--r--
manage hyperlinks via PIDE editor interface;
     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[PIDE.editor.Hyperlink]] =
   203   {
   204     snapshot.cumulate_markup[List[Text.Info[PIDE.editor.Hyperlink]]](
   205       range, Nil, Some(hyperlink_include), _ =>
   206         {
   207           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   208           if Path.is_ok(name) =>
   209             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   210             val link = PIDE.editor.hyperlink_file(jedit_file)
   211             Some(Text.Info(snapshot.convert(info_range), link) :: links)
   212 
   213           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   214           if !props.exists(
   215             { case (Markup.KIND, Markup.ML_OPEN) => true
   216               case (Markup.KIND, Markup.ML_STRUCT) => true
   217               case _ => false }) =>
   218 
   219             props match {
   220               case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
   221                 Isabelle_System.source_file(Path.explode(name)) match {
   222                   case Some(path) =>
   223                     val jedit_file = Isabelle_System.platform_path(path)
   224                     val link = PIDE.editor.hyperlink_file(jedit_file, line)
   225                     Some(Text.Info(snapshot.convert(info_range), 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                     PIDE.editor.hyperlink_command(snapshot, command, offset)
   233                       .map(link => (Text.Info(snapshot.convert(info_range), link) :: links))
   234                   case None => None
   235                 }
   236 
   237               case _ => None
   238             }
   239 
   240           case _ => None
   241         }) match { case Text.Info(_, info :: _) :: _ => Some(info) case _ => None }
   242   }
   243 
   244 
   245   private val active_include =
   246     Set(Markup.BROWSER, Markup.GRAPHVIEW, Markup.SENDBACK, Markup.DIALOG)
   247 
   248   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   249     snapshot.select_markup(range, Some(active_include), command_state =>
   250         {
   251           case Text.Info(info_range, elem @ Protocol.Dialog(_, serial, _))
   252           if !command_state.results.defined(serial) =>
   253             Some(Text.Info(snapshot.convert(info_range), elem))
   254           case Text.Info(info_range, elem) =>
   255             if (elem.name == Markup.BROWSER ||
   256                 elem.name == Markup.GRAPHVIEW ||
   257                 elem.name == Markup.SENDBACK)
   258               Some(Text.Info(snapshot.convert(info_range), elem))
   259             else None
   260         }) match { case Text.Info(_, info) :: _ => Some(info) case _ => None }
   261 
   262 
   263   def command_results(range: Text.Range): Command.Results =
   264   {
   265     val results =
   266       snapshot.select_markup[Command.Results](range, None, command_state =>
   267         { case _ => Some(command_state.results) }).map(_.info)
   268     (Command.Results.empty /: results)(_ ++ _)
   269   }
   270 
   271   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   272   {
   273     val results =
   274       snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
   275         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
   276         {
   277           case (msgs, Text.Info(info_range,
   278             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   279           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   280             val entry: Command.Results.Entry =
   281               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   282             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   283 
   284           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   285           if !body.isEmpty =>
   286             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   287             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   288 
   289           case _ => None
   290         }).flatMap(_.info)
   291     if (results.isEmpty) None
   292     else {
   293       val r = Text.Range(results.head.range.start, results.last.range.stop)
   294       val msgs = Command.Results.make(results.map(_.info))
   295       Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
   296     }
   297   }
   298 
   299 
   300   private val tooltips: Map[String, String] =
   301     Map(
   302       Markup.SORT -> "sort",
   303       Markup.TYP -> "type",
   304       Markup.TERM -> "term",
   305       Markup.PROP -> "proposition",
   306       Markup.TOKEN_RANGE -> "inner syntax token",
   307       Markup.FREE -> "free variable",
   308       Markup.SKOLEM -> "skolem variable",
   309       Markup.BOUND -> "bound variable",
   310       Markup.VAR -> "schematic variable",
   311       Markup.TFREE -> "free type variable",
   312       Markup.TVAR -> "schematic type variable",
   313       Markup.ML_SOURCE -> "ML source",
   314       Markup.DOCUMENT_SOURCE -> "document source")
   315 
   316   private val tooltip_elements =
   317     Set(Markup.TIMING, Markup.ENTITY, Markup.SORTING, Markup.TYPING,
   318       Markup.ML_TYPING, Markup.PATH) ++ tooltips.keys
   319 
   320   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   321     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   322 
   323   private val timing_threshold: Double = options.real("jedit_timing_threshold")
   324 
   325   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   326   {
   327     def add(prev: Text.Info[(Timing, List[(Boolean, XML.Tree)])],
   328       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, List[(Boolean, XML.Tree)])] =
   329     {
   330       val r = snapshot.convert(r0)
   331       val (t, info) = prev.info
   332       if (prev.range == r) Text.Info(r, (t, p :: info)) else Text.Info(r, (t, List(p)))
   333     }
   334 
   335     val results =
   336       snapshot.cumulate_markup[Text.Info[(Timing, List[(Boolean, XML.Tree)])]](
   337         range, Text.Info(range, (Timing.zero, Nil)), Some(tooltip_elements), _ =>
   338         {
   339           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   340             Some(Text.Info(r, (t1 + t2, info)))
   341           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   342             val kind1 = space_explode('_', kind).mkString(" ")
   343             val txt1 = kind1 + " " + quote(name)
   344             val t = prev.info._1
   345             val txt2 =
   346               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   347                 "\n" + t.message
   348               else ""
   349             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   350           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   351           if Path.is_ok(name) =>
   352             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   353             Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
   354           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   355           if name == Markup.SORTING || name == Markup.TYPING =>
   356             Some(add(prev, r, (true, pretty_typing("::", body))))
   357           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   358             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   359           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   360             if (tooltips.isDefinedAt(name))
   361               Some(add(prev, r, (true, XML.Text(tooltips(name)))))
   362             else None
   363         }).map(_.info)
   364 
   365     results.map(_.info).flatMap(_._2) match {
   366       case Nil => None
   367       case tips =>
   368         val r = Text.Range(results.head.range.start, results.last.range.stop)
   369         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   370         Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
   371     }
   372   }
   373 
   374   val tooltip_margin: Int = options.int("jedit_tooltip_margin")
   375   val tooltip_bounds: Double = (options.real("jedit_tooltip_bounds") max 0.2) min 0.8
   376 
   377   lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
   378   lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
   379 
   380 
   381   private lazy val gutter_icons = Map(
   382     Rendering.information_pri -> JEdit_Lib.load_icon(options.string("gutter_information_icon")),
   383     Rendering.warning_pri -> JEdit_Lib.load_icon(options.string("gutter_warning_icon")),
   384     Rendering.legacy_pri -> JEdit_Lib.load_icon(options.string("gutter_legacy_icon")),
   385     Rendering.error_pri -> JEdit_Lib.load_icon(options.string("gutter_error_icon")))
   386 
   387   private val gutter_elements = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   388 
   389   def gutter_message(range: Text.Range): Option[Icon] =
   390   {
   391     val results =
   392       snapshot.cumulate_markup[Int](range, 0, Some(gutter_elements), _ =>
   393         {
   394           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _),
   395               List(XML.Elem(Markup(Markup.INFORMATION, _), _))))) =>
   396             Some(pri max Rendering.information_pri)
   397           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   398             body match {
   399               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   400                 Some(pri max Rendering.legacy_pri)
   401               case _ =>
   402                 Some(pri max Rendering.warning_pri)
   403             }
   404           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   405             Some(pri max Rendering.error_pri)
   406           case _ => None
   407         })
   408     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   409     gutter_icons.get(pri)
   410   }
   411 
   412 
   413   private val squiggly_colors = Map(
   414     Rendering.writeln_pri -> writeln_color,
   415     Rendering.information_pri -> information_color,
   416     Rendering.warning_pri -> warning_color,
   417     Rendering.error_pri -> error_color)
   418 
   419   private val squiggly_include = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   420 
   421   def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
   422   {
   423     val results =
   424       snapshot.cumulate_markup[Int](range, 0, Some(squiggly_include), _ =>
   425         {
   426           case (pri, Text.Info(_, elem)) =>
   427             if (Protocol.is_information(elem))
   428               Some(pri max Rendering.information_pri)
   429             else if (squiggly_include.contains(elem.name))
   430               Some(pri max Rendering.message_pri(elem.name))
   431             else None
   432         })
   433     for {
   434       Text.Info(r, pri) <- results
   435       color <- squiggly_colors.get(pri)
   436     } yield Text.Info(r, color)
   437   }
   438 
   439 
   440   private val message_colors = Map(
   441     Rendering.writeln_pri -> writeln_message_color,
   442     Rendering.information_pri -> information_message_color,
   443     Rendering.tracing_pri -> tracing_message_color,
   444     Rendering.warning_pri -> warning_message_color,
   445     Rendering.error_pri -> error_message_color)
   446 
   447   private val line_background_elements =
   448     Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE,
   449       Markup.ERROR_MESSAGE, Markup.INFORMATION)
   450 
   451   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   452   {
   453     val results =
   454       snapshot.cumulate_markup[Int](range, 0, Some(line_background_elements), _ =>
   455         {
   456           case (pri, Text.Info(_, elem)) =>
   457             val name = elem.name
   458             if (name == Markup.INFORMATION)
   459               Some(pri max Rendering.information_pri)
   460             else if (name == Markup.WRITELN_MESSAGE || name == Markup.TRACING_MESSAGE ||
   461                 elem.name == Markup.WARNING_MESSAGE || name == Markup.ERROR_MESSAGE)
   462               Some(pri max Rendering.message_pri(name))
   463             else None
   464         })
   465     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   466 
   467     val is_separator = pri > 0 &&
   468       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
   469         {
   470           case (_, Text.Info(_, elem)) =>
   471             if (elem.name == Markup.SEPARATOR) Some(true) else None
   472         }).exists(_.info)
   473 
   474     message_colors.get(pri).map((_, is_separator))
   475   }
   476 
   477 
   478   def output_messages(st: Command.State): List[XML.Tree] =
   479     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   480 
   481 
   482   private val background1_include =
   483     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   484       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   485       active_include
   486 
   487   def background1(range: Text.Range): List[Text.Info[Color]] =
   488   {
   489     if (snapshot.is_outdated) List(Text.Info(range, outdated_color))
   490     else
   491       for {
   492         Text.Info(r, result) <-
   493           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   494             range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
   495             {
   496               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   497               if (Protocol.command_status_markup(markup.name)) =>
   498                 Some((Some(Protocol.command_status(status, markup)), color))
   499               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   500                 Some((None, Some(bad_color)))
   501               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   502                 Some((None, Some(intensify_color)))
   503               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   504                 command_state.results.get(serial) match {
   505                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   506                     Some((None, Some(active_result_color)))
   507                   case _ =>
   508                     Some((None, Some(active_color)))
   509                 }
   510               case (_, Text.Info(_, elem)) =>
   511                 if (active_include(elem.name)) Some((None, Some(active_color)))
   512                 else None
   513             })
   514         color <-
   515           (result match {
   516             case (Some(status), opt_color) =>
   517               if (status.is_unprocessed) Some(unprocessed1_color)
   518               else if (status.is_running) Some(running1_color)
   519               else opt_color
   520             case (_, opt_color) => opt_color
   521           })
   522       } yield Text.Info(r, color)
   523   }
   524 
   525 
   526   def background2(range: Text.Range): List[Text.Info[Color]] =
   527     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
   528       {
   529         case Text.Info(_, elem) =>
   530           if (elem.name == Markup.TOKEN_RANGE) Some(light_color) else None
   531       })
   532 
   533 
   534   def bullet(range: Text.Range): List[Text.Info[Color]] =
   535     snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
   536       {
   537         case Text.Info(_, elem) =>
   538           if (elem.name == Markup.BULLET) Some(bullet_color) else None
   539       })
   540 
   541 
   542   private val foreground_include =
   543     Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
   544 
   545   def foreground(range: Text.Range): List[Text.Info[Color]] =
   546     snapshot.select_markup(range, Some(foreground_include), _ =>
   547       {
   548         case Text.Info(_, elem) =>
   549           if (elem.name == Markup.ANTIQ) Some(antiquoted_color)
   550           else if (foreground_include.contains(elem.name)) Some(quoted_color)
   551           else None
   552       })
   553 
   554 
   555   private val text_colors: Map[String, Color] = Map(
   556       Markup.KEYWORD1 -> keyword1_color,
   557       Markup.KEYWORD2 -> keyword2_color,
   558       Markup.STRING -> Color.BLACK,
   559       Markup.ALTSTRING -> Color.BLACK,
   560       Markup.VERBATIM -> Color.BLACK,
   561       Markup.LITERAL -> keyword1_color,
   562       Markup.DELIMITER -> Color.BLACK,
   563       Markup.TFREE -> tfree_color,
   564       Markup.TVAR -> tvar_color,
   565       Markup.FREE -> free_color,
   566       Markup.SKOLEM -> skolem_color,
   567       Markup.BOUND -> bound_color,
   568       Markup.VAR -> var_color,
   569       Markup.INNER_STRING -> inner_quoted_color,
   570       Markup.INNER_COMMENT -> inner_comment_color,
   571       Markup.DYNAMIC_FACT -> dynamic_color,
   572       Markup.ML_KEYWORD -> keyword1_color,
   573       Markup.ML_DELIMITER -> Color.BLACK,
   574       Markup.ML_NUMERAL -> inner_numeral_color,
   575       Markup.ML_CHAR -> inner_quoted_color,
   576       Markup.ML_STRING -> inner_quoted_color,
   577       Markup.ML_COMMENT -> inner_comment_color)
   578 
   579   private val text_color_elements = text_colors.keySet
   580 
   581   def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
   582   {
   583     if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
   584     else
   585       snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
   586         {
   587           case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
   588         })
   589   }
   590 
   591 
   592   /* nested text structure -- folds */
   593 
   594   private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   595 
   596   def fold_depth(range: Text.Range): List[Text.Info[Int]] =
   597     snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
   598       {
   599         case (depth, Text.Info(_, elem)) =>
   600           if (fold_depth_include(elem.name)) Some(depth + 1) else None
   601       })
   602 }