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