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