src/Tools/jEdit/src/rendering.scala
author Lars Hupel <lars.hupel@mytum.de>
Tue Feb 04 09:04:59 2014 +0000 (2014-02-04 ago)
changeset 55316 885500f4aa6a
parent 55033 8e8243975860
child 55500 cdbbaa3074a8
permissions -rw-r--r--
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
     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, View}
    18 
    19 import scala.collection.immutable.SortedMap
    20 
    21 
    22 object Rendering
    23 {
    24   def apply(snapshot: Document.Snapshot, options: Options): Rendering =
    25     new Rendering(snapshot, options)
    26 
    27 
    28   /* message priorities */
    29 
    30   private val writeln_pri = 1
    31   private val information_pri = 2
    32   private val tracing_pri = 3
    33   private val warning_pri = 4
    34   private val legacy_pri = 5
    35   private val error_pri = 6
    36 
    37   private val message_pri = Map(
    38     Markup.WRITELN -> writeln_pri, Markup.WRITELN_MESSAGE -> writeln_pri,
    39     Markup.TRACING -> tracing_pri, Markup.TRACING_MESSAGE -> tracing_pri,
    40     Markup.WARNING -> warning_pri, Markup.WARNING_MESSAGE -> warning_pri,
    41     Markup.ERROR -> error_pri, Markup.ERROR_MESSAGE -> error_pri)
    42 
    43 
    44   /* jEdit font */
    45 
    46   def font_family(): String = jEdit.getProperty("view.font")
    47 
    48   private def view_font_size(): Int = jEdit.getIntegerProperty("view.fontsize", 16)
    49   private val font_size0 = 5
    50   private val font_size1 = 250
    51 
    52   def font_size(scale: String): Float =
    53     (view_font_size() * PIDE.options.real(scale)).toFloat max font_size0 min font_size1
    54 
    55   def font_size_change(view: View, change: Int => Int)
    56   {
    57     val size = change(view_font_size()) max font_size0 min font_size1
    58     jEdit.setIntegerProperty("view.fontsize", size)
    59     jEdit.propertiesChanged()
    60     jEdit.saveSettings()
    61     view.getStatus.setMessageAndClear("Text font size: " + size)
    62   }
    63 
    64 
    65   /* popup window bounds */
    66 
    67   def popup_bounds: Double = (PIDE.options.real("jedit_popup_bounds") max 0.2) min 0.8
    68 
    69 
    70   /* token markup -- text styles */
    71 
    72   private val command_style: Map[String, Byte] =
    73   {
    74     import JEditToken._
    75     Map[String, Byte](
    76       Keyword.THY_END -> KEYWORD2,
    77       Keyword.THY_SCRIPT -> LABEL,
    78       Keyword.QED_SCRIPT -> DIGIT,
    79       Keyword.PRF_SCRIPT -> DIGIT,
    80       Keyword.PRF_ASM -> KEYWORD3,
    81       Keyword.PRF_ASM_GOAL -> KEYWORD3,
    82       Keyword.PRF_ASM_GOAL_SCRIPT -> DIGIT
    83     ).withDefaultValue(KEYWORD1)
    84   }
    85 
    86   private val token_style: Map[Token.Kind.Value, Byte] =
    87   {
    88     import JEditToken._
    89     Map[Token.Kind.Value, Byte](
    90       Token.Kind.KEYWORD -> KEYWORD2,
    91       Token.Kind.IDENT -> NULL,
    92       Token.Kind.LONG_IDENT -> NULL,
    93       Token.Kind.SYM_IDENT -> NULL,
    94       Token.Kind.VAR -> NULL,
    95       Token.Kind.TYPE_IDENT -> NULL,
    96       Token.Kind.TYPE_VAR -> NULL,
    97       Token.Kind.NAT -> NULL,
    98       Token.Kind.FLOAT -> NULL,
    99       Token.Kind.STRING -> LITERAL1,
   100       Token.Kind.ALT_STRING -> LITERAL2,
   101       Token.Kind.VERBATIM -> COMMENT3,
   102       Token.Kind.CARTOUCHE -> COMMENT4,
   103       Token.Kind.SPACE -> NULL,
   104       Token.Kind.COMMENT -> COMMENT1,
   105       Token.Kind.ERROR -> INVALID
   106     ).withDefaultValue(NULL)
   107   }
   108 
   109   def token_markup(syntax: Outer_Syntax, token: Token): Byte =
   110     if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
   111     else if (token.is_operator) JEditToken.OPERATOR
   112     else token_style(token.kind)
   113 }
   114 
   115 
   116 class Rendering private(val snapshot: Document.Snapshot, val options: Options)
   117 {
   118   /* colors */
   119 
   120   def color_value(s: String): Color = Color_Value(options.string(s))
   121 
   122   val outdated_color = color_value("outdated_color")
   123   val unprocessed_color = color_value("unprocessed_color")
   124   val unprocessed1_color = color_value("unprocessed1_color")
   125   val running_color = color_value("running_color")
   126   val running1_color = color_value("running1_color")
   127   val light_color = color_value("light_color")
   128   val bullet_color = color_value("bullet_color")
   129   val tooltip_color = color_value("tooltip_color")
   130   val writeln_color = color_value("writeln_color")
   131   val information_color = color_value("information_color")
   132   val warning_color = color_value("warning_color")
   133   val error_color = color_value("error_color")
   134   val error1_color = color_value("error1_color")
   135   val writeln_message_color = color_value("writeln_message_color")
   136   val information_message_color = color_value("information_message_color")
   137   val tracing_message_color = color_value("tracing_message_color")
   138   val warning_message_color = color_value("warning_message_color")
   139   val error_message_color = color_value("error_message_color")
   140   val bad_color = color_value("bad_color")
   141   val intensify_color = color_value("intensify_color")
   142   val quoted_color = color_value("quoted_color")
   143   val antiquoted_color = color_value("antiquoted_color")
   144   val highlight_color = color_value("highlight_color")
   145   val hyperlink_color = color_value("hyperlink_color")
   146   val active_color = color_value("active_color")
   147   val active_hover_color = color_value("active_hover_color")
   148   val active_result_color = color_value("active_result_color")
   149   val keyword1_color = color_value("keyword1_color")
   150   val keyword2_color = color_value("keyword2_color")
   151 
   152   val tfree_color = color_value("tfree_color")
   153   val tvar_color = color_value("tvar_color")
   154   val free_color = color_value("free_color")
   155   val skolem_color = color_value("skolem_color")
   156   val bound_color = color_value("bound_color")
   157   val var_color = color_value("var_color")
   158   val inner_numeral_color = color_value("inner_numeral_color")
   159   val inner_quoted_color = color_value("inner_quoted_color")
   160   val inner_cartouche_color = color_value("inner_cartouche_color")
   161   val inner_comment_color = color_value("inner_comment_color")
   162   val dynamic_color = color_value("dynamic_color")
   163 
   164 
   165   /* command overview */
   166 
   167   val overview_limit = options.int("jedit_text_overview_limit")
   168 
   169   private val overview_include = Protocol.command_status_markup + Markup.WARNING + Markup.ERROR
   170 
   171   def overview_color(range: Text.Range): Option[Color] =
   172   {
   173     if (snapshot.is_outdated) None
   174     else {
   175       val results =
   176         snapshot.cumulate_markup[(Protocol.Status, Int)](
   177           range, (Protocol.Status.init, 0), Some(overview_include), _ =>
   178           {
   179             case ((status, pri), Text.Info(_, elem)) =>
   180               if (elem.name == Markup.WARNING || elem.name == Markup.ERROR)
   181                 Some((status, pri max Rendering.message_pri(elem.name)))
   182               else if (overview_include(elem.name))
   183                 Some((Protocol.command_status(status, elem.markup), pri))
   184               else None
   185           })
   186       if (results.isEmpty) None
   187       else {
   188         val (status, pri) =
   189           ((Protocol.Status.init, 0) /: results) {
   190             case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
   191 
   192         if (status.is_running) Some(running_color)
   193         else if (pri == Rendering.warning_pri) Some(warning_color)
   194         else if (pri == Rendering.error_pri) Some(error_color)
   195         else if (status.is_unprocessed) Some(unprocessed_color)
   196         else if (status.is_failed) Some(error_color)
   197         else None
   198       }
   199     }
   200   }
   201 
   202 
   203   /* markup selectors */
   204 
   205   private val highlight_include =
   206     Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   207       Markup.ENTITY, Markup.PATH, Markup.URL, Markup.SORTING, Markup.TYPING, Markup.FREE,
   208       Markup.SKOLEM, Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE,
   209       Markup.DOCUMENT_SOURCE)
   210 
   211   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   212   {
   213     snapshot.select_markup(range, Some(highlight_include), _ =>
   214         {
   215           case Text.Info(info_range, elem) =>
   216             if (highlight_include(elem.name))
   217               Some(Text.Info(snapshot.convert(info_range), highlight_color))
   218             else None
   219         }) match { case Text.Info(_, info) :: _ => Some(info) case _ => None }
   220   }
   221 
   222 
   223   private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH, Markup.URL)
   224 
   225   def hyperlink(range: Text.Range): Option[Text.Info[PIDE.editor.Hyperlink]] =
   226   {
   227     snapshot.cumulate_markup[List[Text.Info[PIDE.editor.Hyperlink]]](
   228       range, Nil, Some(hyperlink_include), _ =>
   229         {
   230           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   231           if Path.is_ok(name) =>
   232             val jedit_file = PIDE.thy_load.append(snapshot.node_name.master_dir, Path.explode(name))
   233             val link = PIDE.editor.hyperlink_file(jedit_file)
   234             Some(Text.Info(snapshot.convert(info_range), link) :: links)
   235 
   236           case (links, Text.Info(info_range, XML.Elem(Markup.Url(name), _))) =>
   237             val link = PIDE.editor.hyperlink_url(name)
   238             Some(Text.Info(snapshot.convert(info_range), link) :: links)
   239 
   240           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   241           if !props.exists(
   242             { case (Markup.KIND, Markup.ML_OPEN) => true
   243               case (Markup.KIND, Markup.ML_STRUCT) => true
   244               case _ => false }) =>
   245 
   246             props match {
   247               case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
   248                 Isabelle_System.source_file(Path.explode(name)) match {
   249                   case Some(path) =>
   250                     val jedit_file = Isabelle_System.platform_path(path)
   251                     val link = PIDE.editor.hyperlink_file(jedit_file, line)
   252                     Some(Text.Info(snapshot.convert(info_range), link) :: links)
   253                   case None => None
   254                 }
   255 
   256               case Position.Def_Id_Offset(id, offset) =>
   257                 snapshot.state.find_command(snapshot.version, id) match {
   258                   case Some((node, command)) =>
   259                     PIDE.editor.hyperlink_command(snapshot, command, offset)
   260                       .map(link => (Text.Info(snapshot.convert(info_range), link) :: links))
   261                   case None => None
   262                 }
   263 
   264               case _ => None
   265             }
   266 
   267           case _ => None
   268         }) match { case Text.Info(_, info :: _) :: _ => Some(info) case _ => None }
   269   }
   270 
   271 
   272   private val active_include =
   273     Set(Markup.BROWSER, Markup.GRAPHVIEW, Markup.SENDBACK, Markup.DIALOG, Markup.SIMP_TRACE)
   274 
   275   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   276     snapshot.select_markup(range, Some(active_include), command_state =>
   277         {
   278           case Text.Info(info_range, elem @ Protocol.Dialog(_, serial, _))
   279           if !command_state.results.defined(serial) =>
   280             Some(Text.Info(snapshot.convert(info_range), elem))
   281           case Text.Info(info_range, elem) =>
   282             if (elem.name == Markup.BROWSER ||
   283                 elem.name == Markup.GRAPHVIEW ||
   284                 elem.name == Markup.SENDBACK ||
   285                 elem.name == Markup.SIMP_TRACE)
   286               Some(Text.Info(snapshot.convert(info_range), elem))
   287             else None
   288         }) match { case Text.Info(_, info) :: _ => Some(info) case _ => None }
   289 
   290 
   291   def command_results(range: Text.Range): Command.Results =
   292   {
   293     val results =
   294       snapshot.select_markup[Command.Results](range, None, command_state =>
   295         { case _ => Some(command_state.results) }).map(_.info)
   296     (Command.Results.empty /: results)(_ ++ _)
   297   }
   298 
   299   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   300   {
   301     val results =
   302       snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
   303         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
   304         {
   305           case (msgs, Text.Info(info_range,
   306             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   307           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   308             val entry: Command.Results.Entry =
   309               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   310             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   311 
   312           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   313           if !body.isEmpty =>
   314             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   315             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   316 
   317           case _ => None
   318         }).flatMap(_.info)
   319     if (results.isEmpty) None
   320     else {
   321       val r = Text.Range(results.head.range.start, results.last.range.stop)
   322       val msgs = Command.Results.make(results.map(_.info))
   323       Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
   324     }
   325   }
   326 
   327 
   328   private val tooltips: Map[String, String] =
   329     Map(
   330       Markup.SORT -> "sort",
   331       Markup.TYP -> "type",
   332       Markup.TERM -> "term",
   333       Markup.PROP -> "proposition",
   334       Markup.TOKEN_RANGE -> "inner syntax token",
   335       Markup.FREE -> "free variable",
   336       Markup.SKOLEM -> "skolem variable",
   337       Markup.BOUND -> "bound variable",
   338       Markup.VAR -> "schematic variable",
   339       Markup.TFREE -> "free type variable",
   340       Markup.TVAR -> "schematic type variable",
   341       Markup.ML_SOURCE -> "ML source",
   342       Markup.DOCUMENT_SOURCE -> "document source")
   343 
   344   private val tooltip_elements =
   345     Set(Markup.TIMING, Markup.ENTITY, Markup.SORTING, Markup.TYPING,
   346       Markup.ML_TYPING, Markup.PATH, Markup.URL) ++ tooltips.keys
   347 
   348   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   349     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   350 
   351   private val timing_threshold: Double = options.real("jedit_timing_threshold")
   352 
   353   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   354   {
   355     def add(prev: Text.Info[(Timing, List[(Boolean, XML.Tree)])],
   356       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, List[(Boolean, XML.Tree)])] =
   357     {
   358       val r = snapshot.convert(r0)
   359       val (t, info) = prev.info
   360       if (prev.range == r) Text.Info(r, (t, p :: info)) else Text.Info(r, (t, List(p)))
   361     }
   362 
   363     val results =
   364       snapshot.cumulate_markup[Text.Info[(Timing, List[(Boolean, XML.Tree)])]](
   365         range, Text.Info(range, (Timing.zero, Nil)), Some(tooltip_elements), _ =>
   366         {
   367           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   368             Some(Text.Info(r, (t1 + t2, info)))
   369           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   370             val kind1 = space_explode('_', kind).mkString(" ")
   371             val txt1 = kind1 + " " + quote(name)
   372             val t = prev.info._1
   373             val txt2 =
   374               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   375                 "\n" + t.message
   376               else ""
   377             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   378           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   379           if Path.is_ok(name) =>
   380             val jedit_file = PIDE.thy_load.append(snapshot.node_name.master_dir, Path.explode(name))
   381             Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
   382           case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
   383             Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
   384           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   385           if name == Markup.SORTING || name == Markup.TYPING =>
   386             Some(add(prev, r, (true, pretty_typing("::", body))))
   387           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   388             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   389           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   390             if (tooltips.isDefinedAt(name))
   391               Some(add(prev, r, (true, XML.Text(tooltips(name)))))
   392             else None
   393         }).map(_.info)
   394 
   395     results.map(_.info).flatMap(_._2) match {
   396       case Nil => None
   397       case tips =>
   398         val r = Text.Range(results.head.range.start, results.last.range.stop)
   399         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   400         Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
   401     }
   402   }
   403 
   404   val tooltip_margin: Int = options.int("jedit_tooltip_margin")
   405 
   406   lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
   407   lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
   408 
   409 
   410   private lazy val gutter_icons = Map(
   411     Rendering.information_pri -> JEdit_Lib.load_icon(options.string("gutter_information_icon")),
   412     Rendering.warning_pri -> JEdit_Lib.load_icon(options.string("gutter_warning_icon")),
   413     Rendering.legacy_pri -> JEdit_Lib.load_icon(options.string("gutter_legacy_icon")),
   414     Rendering.error_pri -> JEdit_Lib.load_icon(options.string("gutter_error_icon")))
   415 
   416   private val gutter_elements = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   417 
   418   def gutter_message(range: Text.Range): Option[Icon] =
   419   {
   420     val results =
   421       snapshot.cumulate_markup[Int](range, 0, Some(gutter_elements), _ =>
   422         {
   423           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _),
   424               List(XML.Elem(Markup(Markup.INFORMATION, _), _))))) =>
   425             Some(pri max Rendering.information_pri)
   426           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   427             body match {
   428               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   429                 Some(pri max Rendering.legacy_pri)
   430               case _ =>
   431                 Some(pri max Rendering.warning_pri)
   432             }
   433           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   434             Some(pri max Rendering.error_pri)
   435           case _ => None
   436         })
   437     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   438     gutter_icons.get(pri)
   439   }
   440 
   441 
   442   private val squiggly_colors = Map(
   443     Rendering.writeln_pri -> writeln_color,
   444     Rendering.information_pri -> information_color,
   445     Rendering.warning_pri -> warning_color,
   446     Rendering.error_pri -> error_color)
   447 
   448   private val squiggly_include = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   449 
   450   def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
   451   {
   452     val results =
   453       snapshot.cumulate_markup[Int](range, 0, Some(squiggly_include), _ =>
   454         {
   455           case (pri, Text.Info(_, elem)) =>
   456             if (Protocol.is_information(elem))
   457               Some(pri max Rendering.information_pri)
   458             else if (squiggly_include.contains(elem.name))
   459               Some(pri max Rendering.message_pri(elem.name))
   460             else None
   461         })
   462     for {
   463       Text.Info(r, pri) <- results
   464       color <- squiggly_colors.get(pri)
   465     } yield Text.Info(r, color)
   466   }
   467 
   468 
   469   private val message_colors = Map(
   470     Rendering.writeln_pri -> writeln_message_color,
   471     Rendering.information_pri -> information_message_color,
   472     Rendering.tracing_pri -> tracing_message_color,
   473     Rendering.warning_pri -> warning_message_color,
   474     Rendering.error_pri -> error_message_color)
   475 
   476   private val line_background_elements =
   477     Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE,
   478       Markup.ERROR_MESSAGE, Markup.INFORMATION)
   479 
   480   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   481   {
   482     val results =
   483       snapshot.cumulate_markup[Int](range, 0, Some(line_background_elements), _ =>
   484         {
   485           case (pri, Text.Info(_, elem)) =>
   486             val name = elem.name
   487             if (name == Markup.INFORMATION)
   488               Some(pri max Rendering.information_pri)
   489             else if (name == Markup.WRITELN_MESSAGE || name == Markup.TRACING_MESSAGE ||
   490                 elem.name == Markup.WARNING_MESSAGE || name == Markup.ERROR_MESSAGE)
   491               Some(pri max Rendering.message_pri(name))
   492             else None
   493         })
   494     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   495 
   496     val is_separator = pri > 0 &&
   497       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
   498         {
   499           case (_, Text.Info(_, elem)) =>
   500             if (elem.name == Markup.SEPARATOR) Some(true) else None
   501         }).exists(_.info)
   502 
   503     message_colors.get(pri).map((_, is_separator))
   504   }
   505 
   506 
   507   def output_messages(st: Command.State): List[XML.Tree] =
   508     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   509 
   510 
   511   private val background1_include =
   512     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   513       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   514       active_include
   515 
   516   def background1(range: Text.Range): List[Text.Info[Color]] =
   517   {
   518     if (snapshot.is_outdated) List(Text.Info(range, outdated_color))
   519     else
   520       for {
   521         Text.Info(r, result) <-
   522           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   523             range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
   524             {
   525               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   526               if (Protocol.command_status_markup(markup.name)) =>
   527                 Some((Some(Protocol.command_status(status, markup)), color))
   528               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   529                 Some((None, Some(bad_color)))
   530               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   531                 Some((None, Some(intensify_color)))
   532               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   533                 command_state.results.get(serial) match {
   534                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   535                     Some((None, Some(active_result_color)))
   536                   case _ =>
   537                     Some((None, Some(active_color)))
   538                 }
   539               case (_, Text.Info(_, elem)) =>
   540                 if (active_include(elem.name)) Some((None, Some(active_color)))
   541                 else None
   542             })
   543         color <-
   544           (result match {
   545             case (Some(status), opt_color) =>
   546               if (status.is_unprocessed) Some(unprocessed1_color)
   547               else if (status.is_running) Some(running1_color)
   548               else opt_color
   549             case (_, opt_color) => opt_color
   550           })
   551       } yield Text.Info(r, color)
   552   }
   553 
   554 
   555   def background2(range: Text.Range): List[Text.Info[Color]] =
   556     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
   557       {
   558         case Text.Info(_, elem) =>
   559           if (elem.name == Markup.TOKEN_RANGE) Some(light_color) else None
   560       })
   561 
   562 
   563   def bullet(range: Text.Range): List[Text.Info[Color]] =
   564     snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
   565       {
   566         case Text.Info(_, elem) =>
   567           if (elem.name == Markup.BULLET) Some(bullet_color) else None
   568       })
   569 
   570 
   571   private val foreground_include =
   572     Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
   573 
   574   def foreground(range: Text.Range): List[Text.Info[Color]] =
   575     snapshot.select_markup(range, Some(foreground_include), _ =>
   576       {
   577         case Text.Info(_, elem) =>
   578           if (elem.name == Markup.ANTIQ) Some(antiquoted_color)
   579           else if (foreground_include.contains(elem.name)) Some(quoted_color)
   580           else None
   581       })
   582 
   583 
   584   private val text_colors: Map[String, Color] = Map(
   585       Markup.KEYWORD1 -> keyword1_color,
   586       Markup.KEYWORD2 -> keyword2_color,
   587       Markup.STRING -> Color.BLACK,
   588       Markup.ALTSTRING -> Color.BLACK,
   589       Markup.VERBATIM -> Color.BLACK,
   590       Markup.LITERAL -> keyword1_color,
   591       Markup.DELIMITER -> Color.BLACK,
   592       Markup.TFREE -> tfree_color,
   593       Markup.TVAR -> tvar_color,
   594       Markup.FREE -> free_color,
   595       Markup.SKOLEM -> skolem_color,
   596       Markup.BOUND -> bound_color,
   597       Markup.VAR -> var_color,
   598       Markup.INNER_STRING -> inner_quoted_color,
   599       Markup.INNER_CARTOUCHE -> inner_cartouche_color,
   600       Markup.INNER_COMMENT -> inner_comment_color,
   601       Markup.DYNAMIC_FACT -> dynamic_color,
   602       Markup.ML_KEYWORD -> keyword1_color,
   603       Markup.ML_DELIMITER -> Color.BLACK,
   604       Markup.ML_NUMERAL -> inner_numeral_color,
   605       Markup.ML_CHAR -> inner_quoted_color,
   606       Markup.ML_STRING -> inner_quoted_color,
   607       Markup.ML_COMMENT -> inner_comment_color)
   608 
   609   private val text_color_elements = text_colors.keySet
   610 
   611   def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
   612   {
   613     if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
   614     else
   615       snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
   616         {
   617           case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
   618         })
   619   }
   620 
   621 
   622   /* nested text structure -- folds */
   623 
   624   private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   625 
   626   def fold_depth(range: Text.Range): List[Text.Info[Int]] =
   627     snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
   628       {
   629         case (depth, Text.Info(_, elem)) =>
   630           if (fold_depth_include(elem.name)) Some(depth + 1) else None
   631       })
   632 }