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