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