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