src/Tools/jEdit/src/rendering.scala
author wenzelm
Fri Jun 28 14:51:19 2013 +0200 (2013-06-28)
changeset 52472 3a43a8b1ecb0
parent 52471 ff0e0bb81597
child 52530 99dd8b4ef3fe
permissions -rw-r--r--
load icons via options -- prefer IntelliJ IDEA for now;
     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 tracing_pri = 2
    32   private val warning_pri = 3
    33   private val legacy_pri = 4
    34   private val error_pri = 5
    35 
    36   private val message_pri = Map(
    37     Markup.WRITELN -> writeln_pri, Markup.WRITELN_MESSAGE -> writeln_pri,
    38     Markup.TRACING -> tracing_pri, Markup.TRACING_MESSAGE -> tracing_pri,
    39     Markup.WARNING -> warning_pri, Markup.WARNING_MESSAGE -> warning_pri,
    40     Markup.ERROR -> error_pri, Markup.ERROR_MESSAGE -> error_pri)
    41 
    42 
    43   /* icons */
    44 
    45   def load_icon(name: String): Icon =
    46   {
    47     val name1 =
    48       if (name.startsWith("idea-icons/")) {
    49         val file =
    50           Isabelle_System.platform_file_url(Path.explode("$JEDIT_HOME/dist/jars/idea-icons.jar"))
    51         "jar:" + file + "!/" + name
    52       }
    53       else name
    54     val icon = GUIUtilities.loadIcon(name1)
    55     if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name)
    56     else icon
    57   }
    58 
    59 
    60   /* jEdit font */
    61 
    62   def font_family(): String = jEdit.getProperty("view.font")
    63 
    64   def font_size(scale: String): Float =
    65     (jEdit.getIntegerProperty("view.fontsize", 16) * PIDE.options.real(scale)).toFloat
    66 
    67 
    68   /* token markup -- text styles */
    69 
    70   private val command_style: Map[String, Byte] =
    71   {
    72     import JEditToken._
    73     Map[String, Byte](
    74       Keyword.THY_END -> KEYWORD2,
    75       Keyword.THY_SCRIPT -> LABEL,
    76       Keyword.PRF_SCRIPT -> DIGIT,
    77       Keyword.PRF_ASM -> KEYWORD3,
    78       Keyword.PRF_ASM_GOAL -> KEYWORD3
    79     ).withDefaultValue(KEYWORD1)
    80   }
    81 
    82   private val token_style: Map[Token.Kind.Value, Byte] =
    83   {
    84     import JEditToken._
    85     Map[Token.Kind.Value, Byte](
    86       Token.Kind.KEYWORD -> KEYWORD2,
    87       Token.Kind.IDENT -> NULL,
    88       Token.Kind.LONG_IDENT -> NULL,
    89       Token.Kind.SYM_IDENT -> NULL,
    90       Token.Kind.VAR -> NULL,
    91       Token.Kind.TYPE_IDENT -> NULL,
    92       Token.Kind.TYPE_VAR -> NULL,
    93       Token.Kind.NAT -> NULL,
    94       Token.Kind.FLOAT -> NULL,
    95       Token.Kind.STRING -> LITERAL1,
    96       Token.Kind.ALT_STRING -> LITERAL2,
    97       Token.Kind.VERBATIM -> COMMENT3,
    98       Token.Kind.SPACE -> NULL,
    99       Token.Kind.COMMENT -> COMMENT1,
   100       Token.Kind.ERROR -> INVALID
   101     ).withDefaultValue(NULL)
   102   }
   103 
   104   def token_markup(syntax: Outer_Syntax, token: Token): Byte =
   105     if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
   106     else if (token.is_operator) JEditToken.OPERATOR
   107     else token_style(token.kind)
   108 }
   109 
   110 
   111 class Rendering private(val snapshot: Document.Snapshot, val options: Options)
   112 {
   113   /* colors */
   114 
   115   def color_value(s: String): Color = Color_Value(options.string(s))
   116 
   117   val outdated_color = color_value("outdated_color")
   118   val unprocessed_color = color_value("unprocessed_color")
   119   val unprocessed1_color = color_value("unprocessed1_color")
   120   val running_color = color_value("running_color")
   121   val running1_color = color_value("running1_color")
   122   val light_color = color_value("light_color")
   123   val bullet_color = color_value("bullet_color")
   124   val tooltip_color = color_value("tooltip_color")
   125   val writeln_color = color_value("writeln_color")
   126   val warning_color = color_value("warning_color")
   127   val error_color = color_value("error_color")
   128   val error1_color = color_value("error1_color")
   129   val writeln_message_color = color_value("writeln_message_color")
   130   val tracing_message_color = color_value("tracing_message_color")
   131   val warning_message_color = color_value("warning_message_color")
   132   val error_message_color = color_value("error_message_color")
   133   val bad_color = color_value("bad_color")
   134   val intensify_color = color_value("intensify_color")
   135   val quoted_color = color_value("quoted_color")
   136   val antiquoted_color = color_value("antiquoted_color")
   137   val highlight_color = color_value("highlight_color")
   138   val hyperlink_color = color_value("hyperlink_color")
   139   val active_color = color_value("active_color")
   140   val active_hover_color = color_value("active_hover_color")
   141   val active_result_color = color_value("active_result_color")
   142   val keyword1_color = color_value("keyword1_color")
   143   val keyword2_color = color_value("keyword2_color")
   144 
   145   val tfree_color = color_value("tfree_color")
   146   val tvar_color = color_value("tvar_color")
   147   val free_color = color_value("free_color")
   148   val skolem_color = color_value("skolem_color")
   149   val bound_color = color_value("bound_color")
   150   val var_color = color_value("var_color")
   151   val inner_numeral_color = color_value("inner_numeral_color")
   152   val inner_quoted_color = color_value("inner_quoted_color")
   153   val inner_comment_color = color_value("inner_comment_color")
   154   val dynamic_color = color_value("dynamic_color")
   155 
   156 
   157   /* command overview */
   158 
   159   val overview_limit = options.int("jedit_text_overview_limit")
   160 
   161   private val overview_include = Protocol.command_status_markup + Markup.WARNING + Markup.ERROR
   162 
   163   def overview_color(range: Text.Range): Option[Color] =
   164   {
   165     if (snapshot.is_outdated) None
   166     else {
   167       val results =
   168         snapshot.cumulate_markup[(Protocol.Status, Int)](
   169           range, (Protocol.Status.init, 0), Some(overview_include), _ =>
   170           {
   171             case ((status, pri), Text.Info(_, XML.Elem(markup, _)))
   172             if overview_include(markup.name) =>
   173               if (markup.name == Markup.WARNING || markup.name == Markup.ERROR)
   174                 (status, pri max Rendering.message_pri(markup.name))
   175               else (Protocol.command_status(status, markup), pri)
   176           })
   177       if (results.isEmpty) None
   178       else {
   179         val (status, pri) =
   180           ((Protocol.Status.init, 0) /: results) {
   181             case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
   182 
   183         if (status.is_running) Some(running_color)
   184         else if (pri == Rendering.warning_pri) Some(warning_color)
   185         else if (pri == Rendering.error_pri) Some(error_color)
   186         else if (status.is_unprocessed) Some(unprocessed_color)
   187         else if (status.is_failed) Some(error_color)
   188         else None
   189       }
   190     }
   191   }
   192 
   193 
   194   /* markup selectors */
   195 
   196   private val highlight_include =
   197     Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   198       Markup.ENTITY, Markup.PATH, Markup.SORTING, Markup.TYPING, Markup.FREE, Markup.SKOLEM,
   199       Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOCUMENT_SOURCE)
   200 
   201   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   202   {
   203     snapshot.select_markup(range, Some(highlight_include), _ =>
   204         {
   205           case Text.Info(info_range, XML.Elem(Markup(name, _), _)) if highlight_include(name) =>
   206             Text.Info(snapshot.convert(info_range), highlight_color)
   207         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   208   }
   209 
   210 
   211   private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH)
   212 
   213   def hyperlink(range: Text.Range): Option[Text.Info[Hyperlink]] =
   214   {
   215     snapshot.cumulate_markup[List[Text.Info[Hyperlink]]](range, Nil, Some(hyperlink_include), _ =>
   216         {
   217           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   218           if Path.is_ok(name) =>
   219             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   220             Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, 0, 0)) :: links
   221 
   222           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   223           if !props.exists(
   224             { case (Markup.KIND, Markup.ML_OPEN) => true
   225               case (Markup.KIND, Markup.ML_STRUCT) => true
   226               case _ => false }) =>
   227 
   228             props match {
   229               case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
   230                 Isabelle_System.source_file(Path.explode(name)) match {
   231                   case Some(path) =>
   232                     val jedit_file = Isabelle_System.platform_path(path)
   233                     Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, line, 0)) :: links
   234                   case None => links
   235                 }
   236 
   237               case Position.Def_Id_Offset(id, offset) =>
   238                 snapshot.state.find_command(snapshot.version, id) match {
   239                   case Some((node, command)) =>
   240                     val sources =
   241                       node.commands.iterator.takeWhile(_ != command).map(_.source) ++
   242                         Iterator.single(command.source(Text.Range(0, command.decode(offset))))
   243                     val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
   244                     Text.Info(snapshot.convert(info_range),
   245                       Hyperlink(command.node_name.node, line, column)) :: links
   246                   case None => links
   247                 }
   248 
   249               case _ => links
   250             }
   251         }) match { case Text.Info(_, info :: _) #:: _ => Some(info) case _ => None }
   252   }
   253 
   254 
   255   private val active_include =
   256     Set(Markup.BROWSER, Markup.GRAPHVIEW, Markup.SENDBACK, Markup.DIALOG)
   257 
   258   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   259     snapshot.select_markup(range, Some(active_include), command_state =>
   260         {
   261           case Text.Info(info_range, elem @ Protocol.Dialog(_, serial, _))
   262           if !command_state.results.defined(serial) =>
   263             Text.Info(snapshot.convert(info_range), elem)
   264           case Text.Info(info_range, elem @ XML.Elem(Markup(name, _), _))
   265           if name == Markup.BROWSER || name == Markup.GRAPHVIEW || name == Markup.SENDBACK =>
   266             Text.Info(snapshot.convert(info_range), elem)
   267         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   268 
   269 
   270   def command_results(range: Text.Range): Command.Results =
   271   {
   272     val results =
   273       snapshot.select_markup[Command.Results](range, None, command_state =>
   274         { case _ => command_state.results }).map(_.info)
   275     (Command.Results.empty /: results)(_ ++ _)
   276   }
   277 
   278   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   279   {
   280     val results =
   281       snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
   282         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
   283         {
   284           case (msgs, Text.Info(info_range,
   285             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   286           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   287             val entry: Command.Results.Entry =
   288               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   289             Text.Info(snapshot.convert(info_range), entry) :: msgs
   290 
   291           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   292           if !body.isEmpty =>
   293             val entry: Command.Results.Entry = (Document.new_id(), msg)
   294             Text.Info(snapshot.convert(info_range), entry) :: msgs
   295         }).toList.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             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             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             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             add(prev, r, (true, pretty_typing("::", body)))
   362           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   363             add(prev, r, (false, pretty_typing("ML:", body)))
   364           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _)))
   365           if tooltips.isDefinedAt(name) =>
   366             add(prev, r, (true, XML.Text(tooltips(name))))
   367         }).toList.map(_.info)
   368 
   369     results.map(_.info).flatMap(_._2) match {
   370       case Nil => None
   371       case tips =>
   372         val r = Text.Range(results.head.range.start, results.last.range.stop)
   373         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   374         Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
   375     }
   376   }
   377 
   378   val tooltip_margin: Int = options.int("jedit_tooltip_margin")
   379   val tooltip_bounds: Double = (options.real("jedit_tooltip_bounds") max 0.2) min 0.8
   380 
   381   lazy val tooltip_close_icon = Rendering.load_icon(options.string("tooltip_close_icon"))
   382   lazy val tooltip_detach_icon = Rendering.load_icon(options.string("tooltip_detach_icon"))
   383 
   384 
   385   private lazy val gutter_icons = Map(
   386     Rendering.warning_pri -> Rendering.load_icon(options.string("gutter_warning_icon")),
   387     Rendering.legacy_pri -> Rendering.load_icon(options.string("gutter_legacy_icon")),
   388     Rendering.error_pri -> Rendering.load_icon(options.string("gutter_error_icon")))
   389 
   390   def gutter_message(range: Text.Range): Option[Icon] =
   391   {
   392     val results =
   393       snapshot.cumulate_markup[Int](range, 0, Some(Set(Markup.WARNING, Markup.ERROR)), _ =>
   394         {
   395           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   396             body match {
   397               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   398                 pri max Rendering.legacy_pri
   399               case _ => pri max Rendering.warning_pri
   400             }
   401           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   402             pri max Rendering.error_pri
   403         })
   404     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   405     gutter_icons.get(pri)
   406   }
   407 
   408 
   409   private val squiggly_colors = Map(
   410     Rendering.writeln_pri -> writeln_color,
   411     Rendering.warning_pri -> warning_color,
   412     Rendering.error_pri -> error_color)
   413 
   414   def squiggly_underline(range: Text.Range): Stream[Text.Info[Color]] =
   415   {
   416     val results =
   417       snapshot.cumulate_markup[Int](range, 0,
   418         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)), _ =>
   419         {
   420           case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
   421           if name == Markup.WRITELN ||
   422             name == Markup.WARNING ||
   423             name == Markup.ERROR => pri max Rendering.message_pri(name)
   424         })
   425     for {
   426       Text.Info(r, pri) <- results
   427       color <- squiggly_colors.get(pri)
   428     } yield Text.Info(r, color)
   429   }
   430 
   431 
   432   private val messages_include =
   433     Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE, Markup.ERROR_MESSAGE)
   434 
   435   private val message_colors = Map(
   436     Rendering.writeln_pri -> writeln_message_color,
   437     Rendering.tracing_pri -> tracing_message_color,
   438     Rendering.warning_pri -> warning_message_color,
   439     Rendering.error_pri -> error_message_color)
   440 
   441   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   442   {
   443     val results =
   444       snapshot.cumulate_markup[Int](range, 0, Some(messages_include), _ =>
   445         {
   446           case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
   447           if name == Markup.WRITELN_MESSAGE ||
   448             name == Markup.TRACING_MESSAGE ||
   449             name == Markup.WARNING_MESSAGE ||
   450             name == Markup.ERROR_MESSAGE => pri max Rendering.message_pri(name)
   451         })
   452     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   453 
   454     val is_separator = pri > 0 &&
   455       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
   456         {
   457           case (_, Text.Info(_, XML.Elem(Markup(Markup.SEPARATOR, _), _))) => true
   458         }).exists(_.info)
   459 
   460     message_colors.get(pri).map((_, is_separator))
   461   }
   462 
   463 
   464   def output_messages(st: Command.State): List[XML.Tree] =
   465   {
   466     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   467   }
   468 
   469 
   470   private val background1_include =
   471     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   472       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   473       active_include
   474 
   475   def background1(range: Text.Range): Stream[Text.Info[Color]] =
   476   {
   477     if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
   478     else
   479       for {
   480         Text.Info(r, result) <-
   481           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   482             range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
   483             {
   484               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   485               if (Protocol.command_status_markup(markup.name)) =>
   486                 (Some(Protocol.command_status(status, markup)), color)
   487               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   488                 (None, Some(bad_color))
   489               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   490                 (None, Some(intensify_color))
   491               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   492                 command_state.results.get(serial) match {
   493                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   494                     (None, Some(active_result_color))
   495                   case _ =>
   496                     (None, Some(active_color))
   497                 }
   498               case (_, Text.Info(_, XML.Elem(markup, _))) if active_include(markup.name) =>
   499                 (None, Some(active_color))
   500             })
   501         color <-
   502           (result match {
   503             case (Some(status), opt_color) =>
   504               if (status.is_unprocessed) Some(unprocessed1_color)
   505               else if (status.is_running) Some(running1_color)
   506               else opt_color
   507             case (_, opt_color) => opt_color
   508           })
   509       } yield Text.Info(r, color)
   510   }
   511 
   512 
   513   def background2(range: Text.Range): Stream[Text.Info[Color]] =
   514     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
   515       {
   516         case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => light_color
   517       })
   518 
   519 
   520   def bullet(range: Text.Range): Stream[Text.Info[Color]] =
   521     snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
   522       {
   523         case Text.Info(_, XML.Elem(Markup(Markup.BULLET, _), _)) => bullet_color
   524       })
   525 
   526 
   527   private val foreground_elements =
   528     Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
   529 
   530   def foreground(range: Text.Range): Stream[Text.Info[Color]] =
   531     snapshot.select_markup(range, Some(foreground_elements), _ =>
   532       {
   533         case Text.Info(_, XML.Elem(Markup(Markup.STRING, _), _)) => quoted_color
   534         case Text.Info(_, XML.Elem(Markup(Markup.ALTSTRING, _), _)) => quoted_color
   535         case Text.Info(_, XML.Elem(Markup(Markup.VERBATIM, _), _)) => quoted_color
   536         case Text.Info(_, XML.Elem(Markup(Markup.ANTIQ, _), _)) => antiquoted_color
   537       })
   538 
   539 
   540   private val text_colors: Map[String, Color] = Map(
   541       Markup.KEYWORD1 -> keyword1_color,
   542       Markup.KEYWORD2 -> keyword2_color,
   543       Markup.STRING -> Color.BLACK,
   544       Markup.ALTSTRING -> Color.BLACK,
   545       Markup.VERBATIM -> Color.BLACK,
   546       Markup.LITERAL -> keyword1_color,
   547       Markup.DELIMITER -> Color.BLACK,
   548       Markup.TFREE -> tfree_color,
   549       Markup.TVAR -> tvar_color,
   550       Markup.FREE -> free_color,
   551       Markup.SKOLEM -> skolem_color,
   552       Markup.BOUND -> bound_color,
   553       Markup.VAR -> var_color,
   554       Markup.INNER_STRING -> inner_quoted_color,
   555       Markup.INNER_COMMENT -> inner_comment_color,
   556       Markup.DYNAMIC_FACT -> dynamic_color,
   557       Markup.ML_KEYWORD -> keyword1_color,
   558       Markup.ML_DELIMITER -> Color.BLACK,
   559       Markup.ML_NUMERAL -> inner_numeral_color,
   560       Markup.ML_CHAR -> inner_quoted_color,
   561       Markup.ML_STRING -> inner_quoted_color,
   562       Markup.ML_COMMENT -> inner_comment_color)
   563 
   564   private val text_color_elements = text_colors.keySet
   565 
   566   def text_color(range: Text.Range, color: Color)
   567       : Stream[Text.Info[Color]] =
   568   {
   569     if (color == Token_Markup.hidden_color) Stream(Text.Info(range, color))
   570     else
   571       snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
   572         {
   573           case (_, Text.Info(_, XML.Elem(Markup(m, _), _)))
   574           if text_colors.isDefinedAt(m) => text_colors(m)
   575         })
   576   }
   577 
   578 
   579   /* nested text structure -- folds */
   580 
   581   private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   582 
   583   def fold_depth(range: Text.Range): Stream[Text.Info[Int]] =
   584     snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
   585       {
   586         case (depth, Text.Info(_, XML.Elem(Markup(name, _), _)))
   587         if fold_depth_include(name) => depth + 1
   588       })
   589 }