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