src/Tools/jEdit/src/rendering.scala
author wenzelm
Tue May 21 16:51:16 2013 +0200 (2013-05-21 ago)
changeset 52102 59cc8881e911
parent 52101 7679178b0aa5
child 52471 ff0e0bb81597
permissions -rw-r--r--
tuned;
     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   private def load_icon(name: String): Icon =
    46   {
    47     val icon = GUIUtilities.loadIcon(name)
    48     if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name)
    49     else icon
    50   }
    51 
    52   private val gutter_icons = Map(
    53     warning_pri -> load_icon("16x16/status/dialog-information.png"),
    54     legacy_pri -> load_icon("16x16/status/dialog-warning.png"),
    55     error_pri -> load_icon("16x16/status/dialog-error.png"))
    56 
    57   val tooltip_close_icon = load_icon("16x16/actions/document-close.png")
    58   val tooltip_detach_icon = load_icon("16x16/actions/window-new.png")
    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 quoted_color = color_value("quoted_color")
   137   val antiquoted_color = color_value("antiquoted_color")
   138   val highlight_color = color_value("highlight_color")
   139   val hyperlink_color = color_value("hyperlink_color")
   140   val active_color = color_value("active_color")
   141   val active_hover_color = color_value("active_hover_color")
   142   val active_result_color = color_value("active_result_color")
   143   val keyword1_color = color_value("keyword1_color")
   144   val keyword2_color = color_value("keyword2_color")
   145 
   146   val tfree_color = color_value("tfree_color")
   147   val tvar_color = color_value("tvar_color")
   148   val free_color = color_value("free_color")
   149   val skolem_color = color_value("skolem_color")
   150   val bound_color = color_value("bound_color")
   151   val var_color = color_value("var_color")
   152   val inner_numeral_color = color_value("inner_numeral_color")
   153   val inner_quoted_color = color_value("inner_quoted_color")
   154   val inner_comment_color = color_value("inner_comment_color")
   155   val dynamic_color = color_value("dynamic_color")
   156 
   157 
   158   /* command overview */
   159 
   160   val overview_limit = options.int("jedit_text_overview_limit")
   161 
   162   private val overview_include = Protocol.command_status_markup + Markup.WARNING + Markup.ERROR
   163 
   164   def overview_color(range: Text.Range): Option[Color] =
   165   {
   166     if (snapshot.is_outdated) None
   167     else {
   168       val results =
   169         snapshot.cumulate_markup[(Protocol.Status, Int)](
   170           range, (Protocol.Status.init, 0), Some(overview_include), _ =>
   171           {
   172             case ((status, pri), Text.Info(_, XML.Elem(markup, _)))
   173             if overview_include(markup.name) =>
   174               if (markup.name == Markup.WARNING || markup.name == Markup.ERROR)
   175                 (status, pri max Rendering.message_pri(markup.name))
   176               else (Protocol.command_status(status, markup), pri)
   177           })
   178       if (results.isEmpty) None
   179       else {
   180         val (status, pri) =
   181           ((Protocol.Status.init, 0) /: results) {
   182             case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
   183 
   184         if (status.is_running) Some(running_color)
   185         else if (pri == Rendering.warning_pri) Some(warning_color)
   186         else if (pri == Rendering.error_pri) Some(error_color)
   187         else if (status.is_unprocessed) Some(unprocessed_color)
   188         else if (status.is_failed) Some(error_color)
   189         else None
   190       }
   191     }
   192   }
   193 
   194 
   195   /* markup selectors */
   196 
   197   private val highlight_include =
   198     Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   199       Markup.ENTITY, Markup.PATH, Markup.SORTING, Markup.TYPING, Markup.FREE, Markup.SKOLEM,
   200       Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOCUMENT_SOURCE)
   201 
   202   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   203   {
   204     snapshot.select_markup(range, Some(highlight_include), _ =>
   205         {
   206           case Text.Info(info_range, XML.Elem(Markup(name, _), _)) if highlight_include(name) =>
   207             Text.Info(snapshot.convert(info_range), highlight_color)
   208         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   209   }
   210 
   211 
   212   private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH)
   213 
   214   def hyperlink(range: Text.Range): Option[Text.Info[Hyperlink]] =
   215   {
   216     snapshot.cumulate_markup[List[Text.Info[Hyperlink]]](range, Nil, Some(hyperlink_include), _ =>
   217         {
   218           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   219           if Path.is_ok(name) =>
   220             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   221             Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, 0, 0)) :: links
   222 
   223           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   224           if !props.exists(
   225             { case (Markup.KIND, Markup.ML_OPEN) => true
   226               case (Markup.KIND, Markup.ML_STRUCT) => true
   227               case _ => false }) =>
   228 
   229             props match {
   230               case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
   231                 Isabelle_System.source_file(Path.explode(name)) match {
   232                   case Some(path) =>
   233                     val jedit_file = Isabelle_System.platform_path(path)
   234                     Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, line, 0)) :: links
   235                   case None => links
   236                 }
   237 
   238               case Position.Def_Id_Offset(id, offset) =>
   239                 snapshot.state.find_command(snapshot.version, id) match {
   240                   case Some((node, command)) =>
   241                     val sources =
   242                       node.commands.iterator.takeWhile(_ != command).map(_.source) ++
   243                         Iterator.single(command.source(Text.Range(0, command.decode(offset))))
   244                     val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
   245                     Text.Info(snapshot.convert(info_range),
   246                       Hyperlink(command.node_name.node, line, column)) :: links
   247                   case None => links
   248                 }
   249 
   250               case _ => links
   251             }
   252         }) match { case Text.Info(_, info :: _) #:: _ => Some(info) case _ => None }
   253   }
   254 
   255 
   256   private val active_include =
   257     Set(Markup.BROWSER, Markup.GRAPHVIEW, Markup.SENDBACK, Markup.DIALOG)
   258 
   259   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   260     snapshot.select_markup(range, Some(active_include), command_state =>
   261         {
   262           case Text.Info(info_range, elem @ Protocol.Dialog(_, serial, _))
   263           if !command_state.results.defined(serial) =>
   264             Text.Info(snapshot.convert(info_range), elem)
   265           case Text.Info(info_range, elem @ XML.Elem(Markup(name, _), _))
   266           if name == Markup.BROWSER || name == Markup.GRAPHVIEW || name == Markup.SENDBACK =>
   267             Text.Info(snapshot.convert(info_range), elem)
   268         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   269 
   270 
   271   def command_results(range: Text.Range): Command.Results =
   272   {
   273     val results =
   274       snapshot.select_markup[Command.Results](range, None, command_state =>
   275         { case _ => command_state.results }).map(_.info)
   276     (Command.Results.empty /: results)(_ ++ _)
   277   }
   278 
   279   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   280   {
   281     val results =
   282       snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
   283         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
   284         {
   285           case (msgs, Text.Info(info_range,
   286             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   287           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   288             val entry: Command.Results.Entry =
   289               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   290             Text.Info(snapshot.convert(info_range), entry) :: msgs
   291 
   292           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   293           if !body.isEmpty =>
   294             val entry: Command.Results.Entry = (Document.new_id(), msg)
   295             Text.Info(snapshot.convert(info_range), entry) :: msgs
   296         }).toList.flatMap(_.info)
   297     if (results.isEmpty) None
   298     else {
   299       val r = Text.Range(results.head.range.start, results.last.range.stop)
   300       val msgs = Command.Results.make(results.map(_.info))
   301       Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
   302     }
   303   }
   304 
   305 
   306   private val tooltips: Map[String, String] =
   307     Map(
   308       Markup.SORT -> "sort",
   309       Markup.TYP -> "type",
   310       Markup.TERM -> "term",
   311       Markup.PROP -> "proposition",
   312       Markup.TOKEN_RANGE -> "inner syntax token",
   313       Markup.FREE -> "free variable",
   314       Markup.SKOLEM -> "skolem variable",
   315       Markup.BOUND -> "bound variable",
   316       Markup.VAR -> "schematic variable",
   317       Markup.TFREE -> "free type variable",
   318       Markup.TVAR -> "schematic type variable",
   319       Markup.ML_SOURCE -> "ML source",
   320       Markup.DOCUMENT_SOURCE -> "document source")
   321 
   322   private val tooltip_elements =
   323     Set(Markup.TIMING, Markup.ENTITY, Markup.SORTING, Markup.TYPING,
   324       Markup.ML_TYPING, Markup.PATH) ++ tooltips.keys
   325 
   326   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   327     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   328 
   329   private val timing_threshold: Double = options.real("jedit_timing_threshold")
   330 
   331   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   332   {
   333     def add(prev: Text.Info[(Timing, List[(Boolean, XML.Tree)])],
   334       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, List[(Boolean, XML.Tree)])] =
   335     {
   336       val r = snapshot.convert(r0)
   337       val (t, info) = prev.info
   338       if (prev.range == r) Text.Info(r, (t, p :: info)) else Text.Info(r, (t, List(p)))
   339     }
   340 
   341     val results =
   342       snapshot.cumulate_markup[Text.Info[(Timing, List[(Boolean, XML.Tree)])]](
   343         range, Text.Info(range, (Timing.zero, Nil)), Some(tooltip_elements), _ =>
   344         {
   345           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   346             Text.Info(r, (t1 + t2, info))
   347           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   348             val kind1 = space_explode('_', kind).mkString(" ")
   349             val txt1 = kind1 + " " + quote(name)
   350             val t = prev.info._1
   351             val txt2 =
   352               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   353                 "\n" + t.message
   354               else ""
   355             add(prev, r, (true, XML.Text(txt1 + txt2)))
   356           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   357           if Path.is_ok(name) =>
   358             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   359             add(prev, r, (true, XML.Text("file " + quote(jedit_file))))
   360           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   361           if name == Markup.SORTING || name == Markup.TYPING =>
   362             add(prev, r, (true, pretty_typing("::", body)))
   363           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   364             add(prev, r, (false, pretty_typing("ML:", body)))
   365           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _)))
   366           if tooltips.isDefinedAt(name) =>
   367             add(prev, r, (true, XML.Text(tooltips(name))))
   368         }).toList.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   val tooltip_bounds: Double = (options.real("jedit_tooltip_bounds") max 0.2) min 0.8
   381 
   382 
   383   def gutter_message(range: Text.Range): Option[Icon] =
   384   {
   385     val results =
   386       snapshot.cumulate_markup[Int](range, 0, Some(Set(Markup.WARNING, Markup.ERROR)), _ =>
   387         {
   388           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   389             body match {
   390               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   391                 pri max Rendering.legacy_pri
   392               case _ => pri max Rendering.warning_pri
   393             }
   394           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   395             pri max Rendering.error_pri
   396         })
   397     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   398     Rendering.gutter_icons.get(pri)
   399   }
   400 
   401 
   402   private val squiggly_colors = Map(
   403     Rendering.writeln_pri -> writeln_color,
   404     Rendering.warning_pri -> warning_color,
   405     Rendering.error_pri -> error_color)
   406 
   407   def squiggly_underline(range: Text.Range): Stream[Text.Info[Color]] =
   408   {
   409     val results =
   410       snapshot.cumulate_markup[Int](range, 0,
   411         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)), _ =>
   412         {
   413           case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
   414           if name == Markup.WRITELN ||
   415             name == Markup.WARNING ||
   416             name == Markup.ERROR => pri max Rendering.message_pri(name)
   417         })
   418     for {
   419       Text.Info(r, pri) <- results
   420       color <- squiggly_colors.get(pri)
   421     } yield Text.Info(r, color)
   422   }
   423 
   424 
   425   private val messages_include =
   426     Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE, Markup.ERROR_MESSAGE)
   427 
   428   private val message_colors = Map(
   429     Rendering.writeln_pri -> writeln_message_color,
   430     Rendering.tracing_pri -> tracing_message_color,
   431     Rendering.warning_pri -> warning_message_color,
   432     Rendering.error_pri -> error_message_color)
   433 
   434   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   435   {
   436     val results =
   437       snapshot.cumulate_markup[Int](range, 0, Some(messages_include), _ =>
   438         {
   439           case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
   440           if name == Markup.WRITELN_MESSAGE ||
   441             name == Markup.TRACING_MESSAGE ||
   442             name == Markup.WARNING_MESSAGE ||
   443             name == Markup.ERROR_MESSAGE => pri max Rendering.message_pri(name)
   444         })
   445     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   446 
   447     val is_separator = pri > 0 &&
   448       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
   449         {
   450           case (_, Text.Info(_, XML.Elem(Markup(Markup.SEPARATOR, _), _))) => true
   451         }).exists(_.info)
   452 
   453     message_colors.get(pri).map((_, is_separator))
   454   }
   455 
   456 
   457   def output_messages(st: Command.State): List[XML.Tree] =
   458   {
   459     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   460   }
   461 
   462 
   463   private val background1_include =
   464     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   465       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   466       active_include
   467 
   468   def background1(range: Text.Range): Stream[Text.Info[Color]] =
   469   {
   470     if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
   471     else
   472       for {
   473         Text.Info(r, result) <-
   474           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   475             range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
   476             {
   477               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   478               if (Protocol.command_status_markup(markup.name)) =>
   479                 (Some(Protocol.command_status(status, markup)), color)
   480               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   481                 (None, Some(bad_color))
   482               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   483                 (None, Some(intensify_color))
   484               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   485                 command_state.results.get(serial) match {
   486                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   487                     (None, Some(active_result_color))
   488                   case _ =>
   489                     (None, Some(active_color))
   490                 }
   491               case (_, Text.Info(_, XML.Elem(markup, _))) if active_include(markup.name) =>
   492                 (None, Some(active_color))
   493             })
   494         color <-
   495           (result match {
   496             case (Some(status), opt_color) =>
   497               if (status.is_unprocessed) Some(unprocessed1_color)
   498               else if (status.is_running) Some(running1_color)
   499               else opt_color
   500             case (_, opt_color) => opt_color
   501           })
   502       } yield Text.Info(r, color)
   503   }
   504 
   505 
   506   def background2(range: Text.Range): Stream[Text.Info[Color]] =
   507     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
   508       {
   509         case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => light_color
   510       })
   511 
   512 
   513   def bullet(range: Text.Range): Stream[Text.Info[Color]] =
   514     snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
   515       {
   516         case Text.Info(_, XML.Elem(Markup(Markup.BULLET, _), _)) => bullet_color
   517       })
   518 
   519 
   520   private val foreground_elements =
   521     Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
   522 
   523   def foreground(range: Text.Range): Stream[Text.Info[Color]] =
   524     snapshot.select_markup(range, Some(foreground_elements), _ =>
   525       {
   526         case Text.Info(_, XML.Elem(Markup(Markup.STRING, _), _)) => quoted_color
   527         case Text.Info(_, XML.Elem(Markup(Markup.ALTSTRING, _), _)) => quoted_color
   528         case Text.Info(_, XML.Elem(Markup(Markup.VERBATIM, _), _)) => quoted_color
   529         case Text.Info(_, XML.Elem(Markup(Markup.ANTIQ, _), _)) => antiquoted_color
   530       })
   531 
   532 
   533   private val text_colors: Map[String, Color] = Map(
   534       Markup.KEYWORD1 -> keyword1_color,
   535       Markup.KEYWORD2 -> keyword2_color,
   536       Markup.STRING -> Color.BLACK,
   537       Markup.ALTSTRING -> Color.BLACK,
   538       Markup.VERBATIM -> Color.BLACK,
   539       Markup.LITERAL -> keyword1_color,
   540       Markup.DELIMITER -> Color.BLACK,
   541       Markup.TFREE -> tfree_color,
   542       Markup.TVAR -> tvar_color,
   543       Markup.FREE -> free_color,
   544       Markup.SKOLEM -> skolem_color,
   545       Markup.BOUND -> bound_color,
   546       Markup.VAR -> var_color,
   547       Markup.INNER_STRING -> inner_quoted_color,
   548       Markup.INNER_COMMENT -> inner_comment_color,
   549       Markup.DYNAMIC_FACT -> dynamic_color,
   550       Markup.ML_KEYWORD -> keyword1_color,
   551       Markup.ML_DELIMITER -> Color.BLACK,
   552       Markup.ML_NUMERAL -> inner_numeral_color,
   553       Markup.ML_CHAR -> inner_quoted_color,
   554       Markup.ML_STRING -> inner_quoted_color,
   555       Markup.ML_COMMENT -> inner_comment_color)
   556 
   557   private val text_color_elements = text_colors.keySet
   558 
   559   def text_color(range: Text.Range, color: Color)
   560       : Stream[Text.Info[Color]] =
   561   {
   562     if (color == Token_Markup.hidden_color) Stream(Text.Info(range, color))
   563     else
   564       snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
   565         {
   566           case (_, Text.Info(_, XML.Elem(Markup(m, _), _)))
   567           if text_colors.isDefinedAt(m) => text_colors(m)
   568         })
   569   }
   570 
   571 
   572   /* nested text structure -- folds */
   573 
   574   private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   575 
   576   def fold_depth(range: Text.Range): Stream[Text.Info[Int]] =
   577     snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
   578       {
   579         case (depth, Text.Info(_, XML.Elem(Markup(name, _), _)))
   580         if fold_depth_include(name) => depth + 1
   581       })
   582 }