src/Tools/jEdit/src/rendering.scala
author wenzelm
Sat Dec 15 12:55:11 2012 +0100 (2012-12-15 ago)
changeset 50545 00bdc48c5f71
parent 50543 42bbe637be54
child 50547 ebd75dfaab73
permissions -rw-r--r--
explicit text_fold markup, which is used by default in Pretty.chunks/chunks2;
     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 -> LABEL,
    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 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 highlight_color = color_value("highlight_color")
   137   val hyperlink_color = color_value("hyperlink_color")
   138   val active_color = color_value("active_color")
   139   val active_hover_color = color_value("active_hover_color")
   140   val active_result_color = color_value("active_result_color")
   141   val keyword1_color = color_value("keyword1_color")
   142   val keyword2_color = color_value("keyword2_color")
   143 
   144   val tfree_color = color_value("tfree_color")
   145   val tvar_color = color_value("tvar_color")
   146   val free_color = color_value("free_color")
   147   val skolem_color = color_value("skolem_color")
   148   val bound_color = color_value("bound_color")
   149   val var_color = color_value("var_color")
   150   val inner_numeral_color = color_value("inner_numeral_color")
   151   val inner_quoted_color = color_value("inner_quoted_color")
   152   val inner_comment_color = color_value("inner_comment_color")
   153   val antiquotation_color = color_value("antiquotation_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   def overview_color(range: Text.Range): Option[Color] =
   162   {
   163     if (snapshot.is_outdated) None
   164     else {
   165       val results =
   166         snapshot.cumulate_markup[(Protocol.Status, Int)](
   167           range, (Protocol.Status.init, 0),
   168           Some(Protocol.command_status_markup + Markup.WARNING + Markup.ERROR), _ =>
   169           {
   170             case ((status, pri), Text.Info(_, XML.Elem(markup, _))) =>
   171               if (markup.name == Markup.WARNING || markup.name == Markup.ERROR)
   172                 (status, pri max Rendering.message_pri(markup.name))
   173               else (Protocol.command_status(status, markup), pri)
   174           })
   175       if (results.isEmpty) None
   176       else {
   177         val (status, pri) =
   178           ((Protocol.Status.init, 0) /: results) {
   179             case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
   180 
   181         if (pri == Rendering.warning_pri) Some(warning_color)
   182         else if (pri == Rendering.error_pri) Some(error_color)
   183         else if (status.is_unprocessed) Some(unprocessed_color)
   184         else if (status.is_running) Some(running_color)
   185         else if (status.is_failed) Some(error_color)
   186         else None
   187       }
   188     }
   189   }
   190 
   191 
   192   /* markup selectors */
   193 
   194   private val highlight_include =
   195     Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   196       Markup.ENTITY, Markup.PATH, Markup.SORTING, Markup.TYPING, Markup.FREE, Markup.SKOLEM,
   197       Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOCUMENT_SOURCE)
   198 
   199   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   200   {
   201     snapshot.select_markup(range, Some(highlight_include), _ =>
   202         {
   203           case Text.Info(info_range, XML.Elem(Markup(name, _), _)) if highlight_include(name) =>
   204             Text.Info(snapshot.convert(info_range), highlight_color)
   205         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   206   }
   207 
   208 
   209   private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH)
   210 
   211   def hyperlink(range: Text.Range): Option[Text.Info[Hyperlink]] =
   212   {
   213     snapshot.cumulate_markup[List[Text.Info[Hyperlink]]](range, Nil, Some(hyperlink_include), _ =>
   214         {
   215           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   216           if Path.is_ok(name) =>
   217             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   218             Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, 0, 0)) :: links
   219 
   220           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   221           if !props.exists(
   222             { case (Markup.KIND, Markup.ML_OPEN) => true
   223               case (Markup.KIND, Markup.ML_STRUCT) => true
   224               case _ => false }) =>
   225 
   226             props match {
   227               case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
   228                 Isabelle_System.source_file(Path.explode(name)) match {
   229                   case Some(path) =>
   230                     val jedit_file = Isabelle_System.platform_path(path)
   231                     Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, line, 0)) :: links
   232                   case None => links
   233                 }
   234 
   235               case Position.Def_Id_Offset(id, offset) =>
   236                 snapshot.state.find_command(snapshot.version, id) match {
   237                   case Some((node, command)) =>
   238                     val sources =
   239                       node.commands.iterator.takeWhile(_ != command).map(_.source) ++
   240                         Iterator.single(command.source(Text.Range(0, command.decode(offset))))
   241                     val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
   242                     Text.Info(snapshot.convert(info_range),
   243                       Hyperlink(command.node_name.node, line, column)) :: links
   244                   case None => links
   245                 }
   246 
   247               case _ => links
   248             }
   249         }) match { case Text.Info(_, info :: _) #:: _ => Some(info) case _ => None }
   250   }
   251 
   252 
   253   private val active_include = Set(Markup.GRAPHVIEW, Markup.SENDBACK, Markup.DIALOG)
   254 
   255   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   256     snapshot.select_markup(range, Some(active_include), command_state =>
   257         {
   258           case Text.Info(info_range, elem @ Protocol.Dialog(_, serial, _))
   259           if !command_state.results.defined(serial) =>
   260             Text.Info(snapshot.convert(info_range), elem)
   261           case Text.Info(info_range, elem @ XML.Elem(Markup(name, _), _))
   262           if name == Markup.GRAPHVIEW || name == Markup.SENDBACK =>
   263             Text.Info(snapshot.convert(info_range), elem)
   264         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   265 
   266 
   267   def command_results(range: Text.Range): Command.Results =
   268   {
   269     val results =
   270       snapshot.select_markup[Command.Results](range, None, command_state =>
   271         { case _ => command_state.results }).map(_.info)
   272     (Command.Results.empty /: results)(_ ++ _)
   273   }
   274 
   275   def tooltip_message(range: Text.Range): XML.Body =
   276   {
   277     val msgs =
   278       Command.Results.merge(
   279         snapshot.cumulate_markup[Command.Results](range, Command.Results.empty,
   280           Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
   281           {
   282             case (msgs, Text.Info(_, XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   283             if name == Markup.WRITELN ||
   284                 name == Markup.WARNING ||
   285                 name == Markup.ERROR =>
   286               msgs + (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   287             case (msgs, Text.Info(_, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   288             if !body.isEmpty => msgs + (Document.new_id() -> msg)
   289           }).map(_.info))
   290     Pretty.separate(msgs.entries.map(_._2).toList)
   291   }
   292 
   293 
   294   private val tooltips: Map[String, String] =
   295     Map(
   296       Markup.SORT -> "sort",
   297       Markup.TYP -> "type",
   298       Markup.TERM -> "term",
   299       Markup.PROP -> "proposition",
   300       Markup.TOKEN_RANGE -> "inner syntax token",
   301       Markup.FREE -> "free variable",
   302       Markup.SKOLEM -> "skolem variable",
   303       Markup.BOUND -> "bound variable",
   304       Markup.VAR -> "schematic variable",
   305       Markup.TFREE -> "free type variable",
   306       Markup.TVAR -> "schematic type variable",
   307       Markup.ML_SOURCE -> "ML source",
   308       Markup.DOCUMENT_SOURCE -> "document source")
   309 
   310   private val tooltip_elements =
   311     Set(Markup.ENTITY, Markup.SORTING, Markup.TYPING, Markup.ML_TYPING, Markup.PATH) ++
   312       tooltips.keys
   313 
   314   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   315     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   316 
   317   def tooltip(range: Text.Range): XML.Body =
   318   {
   319     def add(prev: Text.Info[List[(Boolean, XML.Tree)]], r: Text.Range, p: (Boolean, XML.Tree)) =
   320       if (prev.range == r) Text.Info(r, p :: prev.info) else Text.Info(r, List(p))
   321 
   322     val tips =
   323       snapshot.cumulate_markup[Text.Info[(List[(Boolean, XML.Tree)])]](
   324         range, Text.Info(range, Nil), Some(tooltip_elements), _ =>
   325         {
   326           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   327             val kind1 = space_explode('_', kind).mkString(" ")
   328             add(prev, r, (true, XML.Text(kind1 + " " + quote(name))))
   329           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   330           if Path.is_ok(name) =>
   331             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   332             add(prev, r, (true, XML.Text("file " + quote(jedit_file))))
   333           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   334           if name == Markup.SORTING || name == Markup.TYPING =>
   335             add(prev, r, (true, pretty_typing("::", body)))
   336           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   337             add(prev, r, (false, pretty_typing("ML:", body)))
   338           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _)))
   339           if tooltips.isDefinedAt(name) => add(prev, r, (true, XML.Text(tooltips(name))))
   340         }).toList.flatMap(_.info.info)
   341 
   342     val all_tips =
   343       (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   344     Library.separate(Pretty.FBreak, all_tips.toList)
   345   }
   346 
   347 
   348   def gutter_message(range: Text.Range): Option[Icon] =
   349   {
   350     val results =
   351       snapshot.cumulate_markup[Int](range, 0, Some(Set(Markup.WARNING, Markup.ERROR)), _ =>
   352         {
   353           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   354             body match {
   355               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   356                 pri max Rendering.legacy_pri
   357               case _ => pri max Rendering.warning_pri
   358             }
   359           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   360             pri max Rendering.error_pri
   361         })
   362     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   363     Rendering.gutter_icons.get(pri)
   364   }
   365 
   366 
   367   private val squiggly_colors = Map(
   368     Rendering.writeln_pri -> writeln_color,
   369     Rendering.warning_pri -> warning_color,
   370     Rendering.error_pri -> error_color)
   371 
   372   def squiggly_underline(range: Text.Range): Stream[Text.Info[Color]] =
   373   {
   374     val results =
   375       snapshot.cumulate_markup[Int](range, 0,
   376         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)), _ =>
   377         {
   378           case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
   379           if name == Markup.WRITELN ||
   380             name == Markup.WARNING ||
   381             name == Markup.ERROR => pri max Rendering.message_pri(name)
   382         })
   383     for {
   384       Text.Info(r, pri) <- results
   385       color <- squiggly_colors.get(pri)
   386     } yield Text.Info(r, color)
   387   }
   388 
   389 
   390   private val messages_include =
   391     Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE, Markup.ERROR_MESSAGE)
   392 
   393   private val message_colors = Map(
   394     Rendering.writeln_pri -> writeln_message_color,
   395     Rendering.tracing_pri -> tracing_message_color,
   396     Rendering.warning_pri -> warning_message_color,
   397     Rendering.error_pri -> error_message_color)
   398 
   399   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   400   {
   401     val results =
   402       snapshot.cumulate_markup[Int](range, 0, Some(messages_include), _ =>
   403         {
   404           case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
   405           if name == Markup.WRITELN_MESSAGE ||
   406             name == Markup.TRACING_MESSAGE ||
   407             name == Markup.WARNING_MESSAGE ||
   408             name == Markup.ERROR_MESSAGE => pri max Rendering.message_pri(name)
   409         })
   410     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   411 
   412     val is_separator = pri > 0 &&
   413       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
   414         {
   415           case (_, Text.Info(_, XML.Elem(Markup(Markup.SEPARATOR, _), _))) => true
   416         }).exists(_.info)
   417 
   418     message_colors.get(pri).map((_, is_separator))
   419   }
   420 
   421 
   422   def output_messages(st: Command.State): List[XML.Tree] =
   423   {
   424     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   425   }
   426 
   427 
   428   private val background1_include =
   429     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   430       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   431       active_include
   432 
   433   def background1(range: Text.Range): Stream[Text.Info[Color]] =
   434   {
   435     if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
   436     else
   437       for {
   438         Text.Info(r, result) <-
   439           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   440             range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
   441             {
   442               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   443               if (Protocol.command_status_markup(markup.name)) =>
   444                 (Some(Protocol.command_status(status, markup)), color)
   445               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   446                 (None, Some(bad_color))
   447               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   448                 (None, Some(intensify_color))
   449               case (acc, Text.Info(_, elem @ XML.Elem(Markup(Markup.DIALOG, _), _))) =>
   450                 // FIXME pattern match problem in scala-2.9.2 (!??)
   451                 elem match {
   452                   case Protocol.Dialog(_, serial, result) =>
   453                     command_state.results.get(serial) match {
   454                       case Some(Protocol.Dialog_Result(res)) if res == result =>
   455                         (None, Some(active_result_color))
   456                       case _ =>
   457                         (None, Some(active_color))
   458                     }
   459                   case _ => acc
   460                 }
   461               case (_, Text.Info(_, XML.Elem(markup, _))) if active_include(markup.name) =>
   462                 (None, Some(active_color))
   463             })
   464         color <-
   465           (result match {
   466             case (Some(status), opt_color) =>
   467               if (status.is_unprocessed) Some(unprocessed1_color)
   468               else if (status.is_running) Some(running1_color)
   469               else opt_color
   470             case (_, opt_color) => opt_color
   471           })
   472       } yield Text.Info(r, color)
   473   }
   474 
   475 
   476   def background2(range: Text.Range): Stream[Text.Info[Color]] =
   477     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
   478       {
   479         case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => light_color
   480       })
   481 
   482 
   483   def foreground(range: Text.Range): Stream[Text.Info[Color]] =
   484     snapshot.select_markup(range, Some(Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM)), _ =>
   485       {
   486         case Text.Info(_, XML.Elem(Markup(Markup.STRING, _), _)) => quoted_color
   487         case Text.Info(_, XML.Elem(Markup(Markup.ALTSTRING, _), _)) => quoted_color
   488         case Text.Info(_, XML.Elem(Markup(Markup.VERBATIM, _), _)) => quoted_color
   489       })
   490 
   491 
   492   private val text_colors: Map[String, Color] = Map(
   493       Markup.KEYWORD1 -> keyword1_color,
   494       Markup.KEYWORD2 -> keyword2_color,
   495       Markup.STRING -> Color.BLACK,
   496       Markup.ALTSTRING -> Color.BLACK,
   497       Markup.VERBATIM -> Color.BLACK,
   498       Markup.LITERAL -> keyword1_color,
   499       Markup.DELIMITER -> Color.BLACK,
   500       Markup.TFREE -> tfree_color,
   501       Markup.TVAR -> tvar_color,
   502       Markup.FREE -> free_color,
   503       Markup.SKOLEM -> skolem_color,
   504       Markup.BOUND -> bound_color,
   505       Markup.VAR -> var_color,
   506       Markup.INNER_STRING -> inner_quoted_color,
   507       Markup.INNER_COMMENT -> inner_comment_color,
   508       Markup.DYNAMIC_FACT -> dynamic_color,
   509       Markup.ML_KEYWORD -> keyword1_color,
   510       Markup.ML_DELIMITER -> Color.BLACK,
   511       Markup.ML_NUMERAL -> inner_numeral_color,
   512       Markup.ML_CHAR -> inner_quoted_color,
   513       Markup.ML_STRING -> inner_quoted_color,
   514       Markup.ML_COMMENT -> inner_comment_color,
   515       Markup.ANTIQ -> antiquotation_color)
   516 
   517   private val text_color_elements = Set.empty[String] ++ text_colors.keys
   518 
   519   def text_color(range: Text.Range, color: Color)
   520       : Stream[Text.Info[Color]] =
   521   {
   522     if (color == Token_Markup.hidden_color) Stream(Text.Info(range, color))
   523     else
   524       snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
   525         {
   526           case (_, Text.Info(_, XML.Elem(Markup(m, _), _)))
   527           if text_colors.isDefinedAt(m) => text_colors(m)
   528         })
   529   }
   530 
   531 
   532   /* nested text structure -- folds */
   533 
   534   private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   535 
   536   def fold_depth(range: Text.Range): Stream[Text.Info[Int]] =
   537     snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
   538       {
   539         case (depth, Text.Info(_, XML.Elem(Markup(name, _), _)))
   540         if fold_depth_include(name) => depth + 1
   541       })
   542 }