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