src/Tools/jEdit/src/rendering.scala
author wenzelm
Tue Aug 27 16:09:28 2013 +0200 (2013-08-27 ago)
changeset 53230 6589ff56cc3c
parent 52980 28f59ca8ce78
child 53272 0dfd78ff7696
permissions -rw-r--r--
determine completion geometry like tooltip;
just one option jedit_popup_bounds for tooltip and completion;
     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   /* popup window bounds */
    53 
    54   def popup_bounds: Double = (PIDE.options.real("jedit_popup_bounds") max 0.2) min 0.8
    55 
    56 
    57   /* token markup -- text styles */
    58 
    59   private val command_style: Map[String, Byte] =
    60   {
    61     import JEditToken._
    62     Map[String, Byte](
    63       Keyword.THY_END -> KEYWORD2,
    64       Keyword.THY_SCRIPT -> LABEL,
    65       Keyword.PRF_SCRIPT -> DIGIT,
    66       Keyword.PRF_ASM -> KEYWORD3,
    67       Keyword.PRF_ASM_GOAL -> KEYWORD3
    68     ).withDefaultValue(KEYWORD1)
    69   }
    70 
    71   private val token_style: Map[Token.Kind.Value, Byte] =
    72   {
    73     import JEditToken._
    74     Map[Token.Kind.Value, Byte](
    75       Token.Kind.KEYWORD -> KEYWORD2,
    76       Token.Kind.IDENT -> NULL,
    77       Token.Kind.LONG_IDENT -> NULL,
    78       Token.Kind.SYM_IDENT -> NULL,
    79       Token.Kind.VAR -> NULL,
    80       Token.Kind.TYPE_IDENT -> NULL,
    81       Token.Kind.TYPE_VAR -> NULL,
    82       Token.Kind.NAT -> NULL,
    83       Token.Kind.FLOAT -> NULL,
    84       Token.Kind.STRING -> LITERAL1,
    85       Token.Kind.ALT_STRING -> LITERAL2,
    86       Token.Kind.VERBATIM -> COMMENT3,
    87       Token.Kind.SPACE -> NULL,
    88       Token.Kind.COMMENT -> COMMENT1,
    89       Token.Kind.ERROR -> INVALID
    90     ).withDefaultValue(NULL)
    91   }
    92 
    93   def token_markup(syntax: Outer_Syntax, token: Token): Byte =
    94     if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
    95     else if (token.is_operator) JEditToken.OPERATOR
    96     else token_style(token.kind)
    97 }
    98 
    99 
   100 class Rendering private(val snapshot: Document.Snapshot, val options: Options)
   101 {
   102   /* colors */
   103 
   104   def color_value(s: String): Color = Color_Value(options.string(s))
   105 
   106   val outdated_color = color_value("outdated_color")
   107   val unprocessed_color = color_value("unprocessed_color")
   108   val unprocessed1_color = color_value("unprocessed1_color")
   109   val running_color = color_value("running_color")
   110   val running1_color = color_value("running1_color")
   111   val light_color = color_value("light_color")
   112   val bullet_color = color_value("bullet_color")
   113   val tooltip_color = color_value("tooltip_color")
   114   val writeln_color = color_value("writeln_color")
   115   val information_color = color_value("information_color")
   116   val warning_color = color_value("warning_color")
   117   val error_color = color_value("error_color")
   118   val error1_color = color_value("error1_color")
   119   val writeln_message_color = color_value("writeln_message_color")
   120   val information_message_color = color_value("information_message_color")
   121   val tracing_message_color = color_value("tracing_message_color")
   122   val warning_message_color = color_value("warning_message_color")
   123   val error_message_color = color_value("error_message_color")
   124   val bad_color = color_value("bad_color")
   125   val intensify_color = color_value("intensify_color")
   126   val quoted_color = color_value("quoted_color")
   127   val antiquoted_color = color_value("antiquoted_color")
   128   val highlight_color = color_value("highlight_color")
   129   val hyperlink_color = color_value("hyperlink_color")
   130   val active_color = color_value("active_color")
   131   val active_hover_color = color_value("active_hover_color")
   132   val active_result_color = color_value("active_result_color")
   133   val keyword1_color = color_value("keyword1_color")
   134   val keyword2_color = color_value("keyword2_color")
   135 
   136   val tfree_color = color_value("tfree_color")
   137   val tvar_color = color_value("tvar_color")
   138   val free_color = color_value("free_color")
   139   val skolem_color = color_value("skolem_color")
   140   val bound_color = color_value("bound_color")
   141   val var_color = color_value("var_color")
   142   val inner_numeral_color = color_value("inner_numeral_color")
   143   val inner_quoted_color = color_value("inner_quoted_color")
   144   val inner_comment_color = color_value("inner_comment_color")
   145   val dynamic_color = color_value("dynamic_color")
   146 
   147 
   148   /* command overview */
   149 
   150   val overview_limit = options.int("jedit_text_overview_limit")
   151 
   152   private val overview_include = Protocol.command_status_markup + Markup.WARNING + Markup.ERROR
   153 
   154   def overview_color(range: Text.Range): Option[Color] =
   155   {
   156     if (snapshot.is_outdated) None
   157     else {
   158       val results =
   159         snapshot.cumulate_markup[(Protocol.Status, Int)](
   160           range, (Protocol.Status.init, 0), Some(overview_include), _ =>
   161           {
   162             case ((status, pri), Text.Info(_, elem)) =>
   163               if (elem.name == Markup.WARNING || elem.name == Markup.ERROR)
   164                 Some((status, pri max Rendering.message_pri(elem.name)))
   165               else if (overview_include(elem.name))
   166                 Some((Protocol.command_status(status, elem.markup), pri))
   167               else None
   168           })
   169       if (results.isEmpty) None
   170       else {
   171         val (status, pri) =
   172           ((Protocol.Status.init, 0) /: results) {
   173             case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
   174 
   175         if (status.is_running) Some(running_color)
   176         else if (pri == Rendering.warning_pri) Some(warning_color)
   177         else if (pri == Rendering.error_pri) Some(error_color)
   178         else if (status.is_unprocessed) Some(unprocessed_color)
   179         else if (status.is_failed) Some(error_color)
   180         else None
   181       }
   182     }
   183   }
   184 
   185 
   186   /* markup selectors */
   187 
   188   private val highlight_include =
   189     Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   190       Markup.ENTITY, Markup.PATH, Markup.SORTING, Markup.TYPING, Markup.FREE, Markup.SKOLEM,
   191       Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOCUMENT_SOURCE)
   192 
   193   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   194   {
   195     snapshot.select_markup(range, Some(highlight_include), _ =>
   196         {
   197           case Text.Info(info_range, elem) =>
   198             if (highlight_include(elem.name))
   199               Some(Text.Info(snapshot.convert(info_range), highlight_color))
   200             else None
   201         }) match { case Text.Info(_, info) :: _ => Some(info) case _ => None }
   202   }
   203 
   204 
   205   private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH)
   206 
   207   def hyperlink(range: Text.Range): Option[Text.Info[PIDE.editor.Hyperlink]] =
   208   {
   209     snapshot.cumulate_markup[List[Text.Info[PIDE.editor.Hyperlink]]](
   210       range, Nil, Some(hyperlink_include), _ =>
   211         {
   212           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   213           if Path.is_ok(name) =>
   214             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   215             val link = PIDE.editor.hyperlink_file(jedit_file)
   216             Some(Text.Info(snapshot.convert(info_range), link) :: links)
   217 
   218           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   219           if !props.exists(
   220             { case (Markup.KIND, Markup.ML_OPEN) => true
   221               case (Markup.KIND, Markup.ML_STRUCT) => true
   222               case _ => false }) =>
   223 
   224             props match {
   225               case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
   226                 Isabelle_System.source_file(Path.explode(name)) match {
   227                   case Some(path) =>
   228                     val jedit_file = Isabelle_System.platform_path(path)
   229                     val link = PIDE.editor.hyperlink_file(jedit_file, line)
   230                     Some(Text.Info(snapshot.convert(info_range), link) :: links)
   231                   case None => None
   232                 }
   233 
   234               case Position.Def_Id_Offset(id, offset) =>
   235                 snapshot.state.find_command(snapshot.version, id) match {
   236                   case Some((node, command)) =>
   237                     PIDE.editor.hyperlink_command(snapshot, command, offset)
   238                       .map(link => (Text.Info(snapshot.convert(info_range), link) :: links))
   239                   case None => None
   240                 }
   241 
   242               case _ => None
   243             }
   244 
   245           case _ => None
   246         }) match { case Text.Info(_, info :: _) :: _ => Some(info) case _ => None }
   247   }
   248 
   249 
   250   private val active_include =
   251     Set(Markup.BROWSER, Markup.GRAPHVIEW, Markup.SENDBACK, Markup.DIALOG)
   252 
   253   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   254     snapshot.select_markup(range, Some(active_include), command_state =>
   255         {
   256           case Text.Info(info_range, elem @ Protocol.Dialog(_, serial, _))
   257           if !command_state.results.defined(serial) =>
   258             Some(Text.Info(snapshot.convert(info_range), elem))
   259           case Text.Info(info_range, elem) =>
   260             if (elem.name == Markup.BROWSER ||
   261                 elem.name == Markup.GRAPHVIEW ||
   262                 elem.name == Markup.SENDBACK)
   263               Some(Text.Info(snapshot.convert(info_range), elem))
   264             else None
   265         }) match { case Text.Info(_, info) :: _ => Some(info) case _ => None }
   266 
   267 
   268   def command_results(range: Text.Range): Command.Results =
   269   {
   270     val results =
   271       snapshot.select_markup[Command.Results](range, None, command_state =>
   272         { case _ => Some(command_state.results) }).map(_.info)
   273     (Command.Results.empty /: results)(_ ++ _)
   274   }
   275 
   276   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   277   {
   278     val results =
   279       snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
   280         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
   281         {
   282           case (msgs, Text.Info(info_range,
   283             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   284           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   285             val entry: Command.Results.Entry =
   286               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   287             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   288 
   289           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   290           if !body.isEmpty =>
   291             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   292             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   293 
   294           case _ => None
   295         }).flatMap(_.info)
   296     if (results.isEmpty) None
   297     else {
   298       val r = Text.Range(results.head.range.start, results.last.range.stop)
   299       val msgs = Command.Results.make(results.map(_.info))
   300       Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
   301     }
   302   }
   303 
   304 
   305   private val tooltips: Map[String, String] =
   306     Map(
   307       Markup.SORT -> "sort",
   308       Markup.TYP -> "type",
   309       Markup.TERM -> "term",
   310       Markup.PROP -> "proposition",
   311       Markup.TOKEN_RANGE -> "inner syntax token",
   312       Markup.FREE -> "free variable",
   313       Markup.SKOLEM -> "skolem variable",
   314       Markup.BOUND -> "bound variable",
   315       Markup.VAR -> "schematic variable",
   316       Markup.TFREE -> "free type variable",
   317       Markup.TVAR -> "schematic type variable",
   318       Markup.ML_SOURCE -> "ML source",
   319       Markup.DOCUMENT_SOURCE -> "document source")
   320 
   321   private val tooltip_elements =
   322     Set(Markup.TIMING, Markup.ENTITY, Markup.SORTING, Markup.TYPING,
   323       Markup.ML_TYPING, Markup.PATH) ++ tooltips.keys
   324 
   325   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   326     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   327 
   328   private val timing_threshold: Double = options.real("jedit_timing_threshold")
   329 
   330   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   331   {
   332     def add(prev: Text.Info[(Timing, List[(Boolean, XML.Tree)])],
   333       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, List[(Boolean, XML.Tree)])] =
   334     {
   335       val r = snapshot.convert(r0)
   336       val (t, info) = prev.info
   337       if (prev.range == r) Text.Info(r, (t, p :: info)) else Text.Info(r, (t, List(p)))
   338     }
   339 
   340     val results =
   341       snapshot.cumulate_markup[Text.Info[(Timing, List[(Boolean, XML.Tree)])]](
   342         range, Text.Info(range, (Timing.zero, Nil)), Some(tooltip_elements), _ =>
   343         {
   344           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   345             Some(Text.Info(r, (t1 + t2, info)))
   346           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   347             val kind1 = space_explode('_', kind).mkString(" ")
   348             val txt1 = kind1 + " " + quote(name)
   349             val t = prev.info._1
   350             val txt2 =
   351               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   352                 "\n" + t.message
   353               else ""
   354             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   355           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   356           if Path.is_ok(name) =>
   357             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   358             Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
   359           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   360           if name == Markup.SORTING || name == Markup.TYPING =>
   361             Some(add(prev, r, (true, pretty_typing("::", body))))
   362           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   363             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   364           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   365             if (tooltips.isDefinedAt(name))
   366               Some(add(prev, r, (true, XML.Text(tooltips(name)))))
   367             else None
   368         }).map(_.info)
   369 
   370     results.map(_.info).flatMap(_._2) match {
   371       case Nil => None
   372       case tips =>
   373         val r = Text.Range(results.head.range.start, results.last.range.stop)
   374         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   375         Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
   376     }
   377   }
   378 
   379   val tooltip_margin: Int = options.int("jedit_tooltip_margin")
   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): List[Text.Info[Color]] =
   426   {
   427     val results =
   428       snapshot.cumulate_markup[Int](range, 0, Some(squiggly_include), _ =>
   429         {
   430           case (pri, Text.Info(_, elem)) =>
   431             if (Protocol.is_information(elem))
   432               Some(pri max Rendering.information_pri)
   433             else if (squiggly_include.contains(elem.name))
   434               Some(pri max Rendering.message_pri(elem.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(_, elem)) =>
   461             val name = elem.name
   462             if (name == Markup.INFORMATION)
   463               Some(pri max Rendering.information_pri)
   464             else if (name == Markup.WRITELN_MESSAGE || name == Markup.TRACING_MESSAGE ||
   465                 elem.name == Markup.WARNING_MESSAGE || name == Markup.ERROR_MESSAGE)
   466               Some(pri max Rendering.message_pri(name))
   467             else None
   468         })
   469     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   470 
   471     val is_separator = pri > 0 &&
   472       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
   473         {
   474           case (_, Text.Info(_, elem)) =>
   475             if (elem.name == Markup.SEPARATOR) Some(true) else None
   476         }).exists(_.info)
   477 
   478     message_colors.get(pri).map((_, is_separator))
   479   }
   480 
   481 
   482   def output_messages(st: Command.State): List[XML.Tree] =
   483     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   484 
   485 
   486   private val background1_include =
   487     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   488       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   489       active_include
   490 
   491   def background1(range: Text.Range): List[Text.Info[Color]] =
   492   {
   493     if (snapshot.is_outdated) List(Text.Info(range, outdated_color))
   494     else
   495       for {
   496         Text.Info(r, result) <-
   497           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   498             range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
   499             {
   500               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   501               if (Protocol.command_status_markup(markup.name)) =>
   502                 Some((Some(Protocol.command_status(status, markup)), color))
   503               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   504                 Some((None, Some(bad_color)))
   505               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   506                 Some((None, Some(intensify_color)))
   507               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   508                 command_state.results.get(serial) match {
   509                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   510                     Some((None, Some(active_result_color)))
   511                   case _ =>
   512                     Some((None, Some(active_color)))
   513                 }
   514               case (_, Text.Info(_, elem)) =>
   515                 if (active_include(elem.name)) Some((None, Some(active_color)))
   516                 else None
   517             })
   518         color <-
   519           (result match {
   520             case (Some(status), opt_color) =>
   521               if (status.is_unprocessed) Some(unprocessed1_color)
   522               else if (status.is_running) Some(running1_color)
   523               else opt_color
   524             case (_, opt_color) => opt_color
   525           })
   526       } yield Text.Info(r, color)
   527   }
   528 
   529 
   530   def background2(range: Text.Range): List[Text.Info[Color]] =
   531     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
   532       {
   533         case Text.Info(_, elem) =>
   534           if (elem.name == Markup.TOKEN_RANGE) Some(light_color) else None
   535       })
   536 
   537 
   538   def bullet(range: Text.Range): List[Text.Info[Color]] =
   539     snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
   540       {
   541         case Text.Info(_, elem) =>
   542           if (elem.name == Markup.BULLET) Some(bullet_color) else None
   543       })
   544 
   545 
   546   private val foreground_include =
   547     Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
   548 
   549   def foreground(range: Text.Range): List[Text.Info[Color]] =
   550     snapshot.select_markup(range, Some(foreground_include), _ =>
   551       {
   552         case Text.Info(_, elem) =>
   553           if (elem.name == Markup.ANTIQ) Some(antiquoted_color)
   554           else if (foreground_include.contains(elem.name)) Some(quoted_color)
   555           else None
   556       })
   557 
   558 
   559   private val text_colors: Map[String, Color] = Map(
   560       Markup.KEYWORD1 -> keyword1_color,
   561       Markup.KEYWORD2 -> keyword2_color,
   562       Markup.STRING -> Color.BLACK,
   563       Markup.ALTSTRING -> Color.BLACK,
   564       Markup.VERBATIM -> Color.BLACK,
   565       Markup.LITERAL -> keyword1_color,
   566       Markup.DELIMITER -> Color.BLACK,
   567       Markup.TFREE -> tfree_color,
   568       Markup.TVAR -> tvar_color,
   569       Markup.FREE -> free_color,
   570       Markup.SKOLEM -> skolem_color,
   571       Markup.BOUND -> bound_color,
   572       Markup.VAR -> var_color,
   573       Markup.INNER_STRING -> inner_quoted_color,
   574       Markup.INNER_COMMENT -> inner_comment_color,
   575       Markup.DYNAMIC_FACT -> dynamic_color,
   576       Markup.ML_KEYWORD -> keyword1_color,
   577       Markup.ML_DELIMITER -> Color.BLACK,
   578       Markup.ML_NUMERAL -> inner_numeral_color,
   579       Markup.ML_CHAR -> inner_quoted_color,
   580       Markup.ML_STRING -> inner_quoted_color,
   581       Markup.ML_COMMENT -> inner_comment_color)
   582 
   583   private val text_color_elements = text_colors.keySet
   584 
   585   def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
   586   {
   587     if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
   588     else
   589       snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
   590         {
   591           case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
   592         })
   593   }
   594 
   595 
   596   /* nested text structure -- folds */
   597 
   598   private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   599 
   600   def fold_depth(range: Text.Range): List[Text.Info[Int]] =
   601     snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
   602       {
   603         case (depth, Text.Info(_, elem)) =>
   604           if (fold_depth_include(elem.name)) Some(depth + 1) else None
   605       })
   606 }