src/Tools/jEdit/src/rendering.scala
author wenzelm
Mon Mar 03 11:58:07 2014 +0100 (2014-03-03 ago)
changeset 55879 ac979f750c1a
parent 55878 6d092a5166f1
child 55883 6f50d445f0e3
permissions -rw-r--r--
clarified path checks: avoid crash of rendering due to spurious errors;
     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   /* popup window bounds */
    45 
    46   def popup_bounds: Double = (PIDE.options.real("jedit_popup_bounds") max 0.2) min 0.8
    47 
    48 
    49   /* Isabelle/Isar token markup */
    50 
    51   private val command_style: Map[String, Byte] =
    52   {
    53     import JEditToken._
    54     Map[String, Byte](
    55       Keyword.THY_END -> KEYWORD2,
    56       Keyword.THY_SCRIPT -> LABEL,
    57       Keyword.QED_SCRIPT -> DIGIT,
    58       Keyword.PRF_SCRIPT -> DIGIT,
    59       Keyword.PRF_ASM -> KEYWORD3,
    60       Keyword.PRF_ASM_GOAL -> KEYWORD3,
    61       Keyword.PRF_ASM_GOAL_SCRIPT -> DIGIT
    62     ).withDefaultValue(KEYWORD1)
    63   }
    64 
    65   private val token_style: Map[Token.Kind.Value, Byte] =
    66   {
    67     import JEditToken._
    68     Map[Token.Kind.Value, Byte](
    69       Token.Kind.KEYWORD -> KEYWORD2,
    70       Token.Kind.IDENT -> NULL,
    71       Token.Kind.LONG_IDENT -> NULL,
    72       Token.Kind.SYM_IDENT -> NULL,
    73       Token.Kind.VAR -> NULL,
    74       Token.Kind.TYPE_IDENT -> NULL,
    75       Token.Kind.TYPE_VAR -> NULL,
    76       Token.Kind.NAT -> NULL,
    77       Token.Kind.FLOAT -> NULL,
    78       Token.Kind.STRING -> LITERAL1,
    79       Token.Kind.ALT_STRING -> LITERAL2,
    80       Token.Kind.VERBATIM -> COMMENT3,
    81       Token.Kind.CARTOUCHE -> COMMENT4,
    82       Token.Kind.SPACE -> NULL,
    83       Token.Kind.COMMENT -> COMMENT1,
    84       Token.Kind.ERROR -> INVALID
    85     ).withDefaultValue(NULL)
    86   }
    87 
    88   def token_markup(syntax: Outer_Syntax, token: Token): Byte =
    89     if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
    90     else if (token.is_delimiter) JEditToken.OPERATOR
    91     else token_style(token.kind)
    92 
    93 
    94   /* Isabelle/ML token markup */
    95 
    96   private val ml_token_style: Map[ML_Lex.Kind.Value, Byte] =
    97   {
    98     import JEditToken._
    99     Map[ML_Lex.Kind.Value, Byte](
   100       ML_Lex.Kind.KEYWORD -> NULL,
   101       ML_Lex.Kind.IDENT -> NULL,
   102       ML_Lex.Kind.LONG_IDENT -> NULL,
   103       ML_Lex.Kind.TYPE_VAR -> NULL,
   104       ML_Lex.Kind.WORD -> DIGIT,
   105       ML_Lex.Kind.INT -> DIGIT,
   106       ML_Lex.Kind.REAL -> DIGIT,
   107       ML_Lex.Kind.CHAR -> LITERAL2,
   108       ML_Lex.Kind.STRING -> LITERAL1,
   109       ML_Lex.Kind.SPACE -> NULL,
   110       ML_Lex.Kind.COMMENT -> COMMENT1,
   111       ML_Lex.Kind.ANTIQ -> NULL,
   112       ML_Lex.Kind.ANTIQ_START -> LITERAL4,
   113       ML_Lex.Kind.ANTIQ_STOP -> LITERAL4,
   114       ML_Lex.Kind.ANTIQ_OTHER -> NULL,
   115       ML_Lex.Kind.ANTIQ_STRING -> NULL,
   116       ML_Lex.Kind.ANTIQ_ALT_STRING -> NULL,
   117       ML_Lex.Kind.ANTIQ_CARTOUCHE -> NULL,
   118       ML_Lex.Kind.ERROR -> INVALID
   119     ).withDefaultValue(NULL)
   120   }
   121 
   122   def ml_token_markup(token: ML_Lex.Token): Byte =
   123     if (!token.is_keyword) ml_token_style(token.kind)
   124     else if (token.is_delimiter) JEditToken.OPERATOR
   125     else if (ML_Lex.keywords2(token.source)) JEditToken.KEYWORD2
   126     else if (ML_Lex.keywords3(token.source)) JEditToken.KEYWORD3
   127     else JEditToken.KEYWORD1
   128 
   129 
   130   /* markup elements */
   131 
   132   private val completion_names_elements =
   133     Document.Elements(Markup.COMPLETION)
   134 
   135   private val completion_language_elements =
   136     Document.Elements(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM,
   137       Markup.CARTOUCHE, Markup.COMMENT, Markup.LANGUAGE,
   138       Markup.ML_STRING, Markup.ML_COMMENT)
   139 
   140   private val highlight_elements =
   141     Document.Elements(Markup.LANGUAGE, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   142       Markup.ENTITY, Markup.PATH, Markup.URL, Markup.SORTING,
   143       Markup.TYPING, Markup.FREE, Markup.SKOLEM, Markup.BOUND,
   144       Markup.VAR, Markup.TFREE, Markup.TVAR)
   145 
   146   private val hyperlink_elements =
   147     Document.Elements(Markup.ENTITY, Markup.PATH, Markup.POSITION, Markup.URL)
   148 
   149   private val active_elements =
   150     Document.Elements(Markup.DIALOG, Markup.BROWSER, Markup.GRAPHVIEW,
   151       Markup.SENDBACK, Markup.SIMP_TRACE)
   152 
   153   private val tooltip_message_elements =
   154     Document.Elements(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)
   155 
   156   private val tooltip_descriptions =
   157     Map(
   158       Markup.TOKEN_RANGE -> "inner syntax token",
   159       Markup.FREE -> "free variable",
   160       Markup.SKOLEM -> "skolem variable",
   161       Markup.BOUND -> "bound variable",
   162       Markup.VAR -> "schematic variable",
   163       Markup.TFREE -> "free type variable",
   164       Markup.TVAR -> "schematic type variable")
   165 
   166   private val tooltip_elements =
   167     Document.Elements(Markup.LANGUAGE, Markup.TIMING, Markup.ENTITY, Markup.SORTING,
   168       Markup.TYPING, Markup.ML_TYPING, Markup.PATH, Markup.URL) ++
   169     Document.Elements(tooltip_descriptions.keySet)
   170 
   171   private val gutter_elements =
   172     Document.Elements(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   173 
   174   private val squiggly_elements =
   175     Document.Elements(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   176 
   177   private val line_background_elements =
   178     Document.Elements(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE,
   179       Markup.WARNING_MESSAGE, Markup.ERROR_MESSAGE,
   180       Markup.INFORMATION)
   181 
   182   private val separator_elements =
   183     Document.Elements(Markup.SEPARATOR)
   184 
   185   private val background_elements =
   186     Protocol.command_status_elements + Markup.WRITELN_MESSAGE +
   187       Markup.TRACING_MESSAGE + Markup.WARNING_MESSAGE +
   188       Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   189       active_elements
   190 
   191   private val foreground_elements =
   192     Document.Elements(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM,
   193       Markup.CARTOUCHE, Markup.ANTIQUOTED)
   194 
   195   private val bullet_elements =
   196     Document.Elements(Markup.BULLET)
   197 
   198   private val fold_depth_elements =
   199     Document.Elements(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   200 }
   201 
   202 
   203 class Rendering private(val snapshot: Document.Snapshot, val options: Options)
   204 {
   205   override def toString: String = "Rendering(" + snapshot.toString + ")"
   206 
   207 
   208   /* colors */
   209 
   210   def color_value(s: String): Color = Color_Value(options.string(s))
   211 
   212   val outdated_color = color_value("outdated_color")
   213   val unprocessed_color = color_value("unprocessed_color")
   214   val unprocessed1_color = color_value("unprocessed1_color")
   215   val running_color = color_value("running_color")
   216   val running1_color = color_value("running1_color")
   217   val bullet_color = color_value("bullet_color")
   218   val tooltip_color = color_value("tooltip_color")
   219   val writeln_color = color_value("writeln_color")
   220   val information_color = color_value("information_color")
   221   val warning_color = color_value("warning_color")
   222   val error_color = color_value("error_color")
   223   val error1_color = color_value("error1_color")
   224   val writeln_message_color = color_value("writeln_message_color")
   225   val information_message_color = color_value("information_message_color")
   226   val tracing_message_color = color_value("tracing_message_color")
   227   val warning_message_color = color_value("warning_message_color")
   228   val error_message_color = color_value("error_message_color")
   229   val bad_color = color_value("bad_color")
   230   val intensify_color = color_value("intensify_color")
   231   val quoted_color = color_value("quoted_color")
   232   val antiquoted_color = color_value("antiquoted_color")
   233   val antiquote_color = color_value("antiquote_color")
   234   val highlight_color = color_value("highlight_color")
   235   val hyperlink_color = color_value("hyperlink_color")
   236   val active_color = color_value("active_color")
   237   val active_hover_color = color_value("active_hover_color")
   238   val active_result_color = color_value("active_result_color")
   239   val keyword1_color = color_value("keyword1_color")
   240   val keyword2_color = color_value("keyword2_color")
   241   val keyword3_color = color_value("keyword3_color")
   242   val caret_invisible_color = color_value("caret_invisible_color")
   243   val completion_color = color_value("completion_color")
   244 
   245   val tfree_color = color_value("tfree_color")
   246   val tvar_color = color_value("tvar_color")
   247   val free_color = color_value("free_color")
   248   val skolem_color = color_value("skolem_color")
   249   val bound_color = color_value("bound_color")
   250   val var_color = color_value("var_color")
   251   val inner_numeral_color = color_value("inner_numeral_color")
   252   val inner_quoted_color = color_value("inner_quoted_color")
   253   val inner_cartouche_color = color_value("inner_cartouche_color")
   254   val inner_comment_color = color_value("inner_comment_color")
   255   val dynamic_color = color_value("dynamic_color")
   256 
   257 
   258   /* completion */
   259 
   260   def completion_names(range: Text.Range): Option[Completion.Names] =
   261     if (snapshot.is_outdated) None
   262     else {
   263       snapshot.select(range, Rendering.completion_names_elements, _ =>
   264         {
   265           case Completion.Names.Info(names) => Some(names)
   266           case _ => None
   267         }).headOption.map(_.info)
   268     }
   269 
   270   def completion_language(range: Text.Range): Option[Completion.Language_Context] =
   271     snapshot.select(range, Rendering.completion_language_elements, _ =>
   272       {
   273         case Text.Info(_, XML.Elem(Markup.Language(language, symbols, antiquotes, delimited), _)) =>
   274           if (delimited) Some(Completion.Language_Context(language, symbols, antiquotes))
   275           else None
   276         case Text.Info(_, elem)
   277         if elem.name == Markup.ML_STRING || elem.name == Markup.ML_COMMENT =>
   278           Some(Completion.Language_Context.ML_inner)
   279         case Text.Info(_, _) =>
   280           Some(Completion.Language_Context.inner)
   281       }).headOption.map(_.info)
   282 
   283 
   284   /* command status overview */
   285 
   286   def overview_limit: Int = options.int("jedit_text_overview_limit")
   287 
   288   def overview_color(range: Text.Range): Option[Color] =
   289   {
   290     if (snapshot.is_outdated) None
   291     else {
   292       val results =
   293         snapshot.cumulate[(Protocol.Status, Int)](
   294           range, (Protocol.Status.init, 0), Protocol.status_elements, _ =>
   295           {
   296             case ((status, pri), Text.Info(_, elem)) =>
   297               if (Protocol.command_status_elements(elem.name))
   298                 Some((Protocol.command_status(status, elem.markup), pri))
   299               else
   300                 Some((status, pri max Rendering.message_pri(elem.name)))
   301           }, status = true)
   302       if (results.isEmpty) None
   303       else {
   304         val (status, pri) =
   305           ((Protocol.Status.init, 0) /: results) {
   306             case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
   307 
   308         if (status.is_running) Some(running_color)
   309         else if (pri == Rendering.warning_pri) Some(warning_color)
   310         else if (pri == Rendering.error_pri) Some(error_color)
   311         else if (status.is_unprocessed) Some(unprocessed_color)
   312         else if (status.is_failed) Some(error_color)
   313         else None
   314       }
   315     }
   316   }
   317 
   318 
   319   /* highlighted area */
   320 
   321   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   322     snapshot.select(range, Rendering.highlight_elements, _ =>
   323       {
   324         case info => Some(Text.Info(snapshot.convert(info.range), highlight_color))
   325       }).headOption.map(_.info)
   326 
   327 
   328   /* hyperlinks */
   329 
   330   private def hyperlink_file(props: Properties.T): Option[PIDE.editor.Hyperlink] =
   331     props match {
   332       case Position.Def_Line_File(line, name) =>
   333         val offset = Position.Def_Offset.unapply(props) getOrElse 0
   334         PIDE.editor.hyperlink_source_file(name, line, offset)
   335       case Position.Def_Id_Offset(id, offset) =>
   336         PIDE.editor.hyperlink_command_id(snapshot, id, offset)
   337       case _ => None
   338     }
   339 
   340   def hyperlink(range: Text.Range): Option[Text.Info[PIDE.editor.Hyperlink]] =
   341   {
   342     snapshot.cumulate[Vector[Text.Info[PIDE.editor.Hyperlink]]](
   343       range, Vector.empty, Rendering.hyperlink_elements, _ =>
   344         {
   345           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   346           if Path.is_valid(name) =>
   347             val jedit_file = PIDE.thy_load.append(snapshot.node_name.master_dir, Path.explode(name))
   348             val link = PIDE.editor.hyperlink_file(jedit_file)
   349             Some(links :+ Text.Info(snapshot.convert(info_range), link))
   350 
   351           case (links, Text.Info(info_range, XML.Elem(Markup.Url(name), _))) =>
   352             val link = PIDE.editor.hyperlink_url(name)
   353             Some(links :+ Text.Info(snapshot.convert(info_range), link))
   354 
   355           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   356           if !props.exists(
   357             { case (Markup.KIND, Markup.ML_OPEN) => true
   358               case (Markup.KIND, Markup.ML_STRUCTURE) => true
   359               case _ => false }) =>
   360             hyperlink_file(props).map(link =>
   361               (links :+ Text.Info(snapshot.convert(info_range), link)))
   362 
   363           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
   364             hyperlink_file(props).map(link =>
   365               (links :+ Text.Info(snapshot.convert(info_range), link)))
   366 
   367           case _ => None
   368         }) match { case Text.Info(_, _ :+ info) :: _ => Some(info) case _ => None }
   369   }
   370 
   371 
   372   /* active elements */
   373 
   374   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   375     snapshot.select(range, Rendering.active_elements, command_state =>
   376       {
   377         case Text.Info(info_range, elem) =>
   378           if (elem.name == Markup.DIALOG) {
   379             elem match {
   380               case Protocol.Dialog(_, serial, _)
   381               if !command_state.results.defined(serial) =>
   382                 Some(Text.Info(snapshot.convert(info_range), elem))
   383               case _ => None
   384             }
   385           }
   386           else Some(Text.Info(snapshot.convert(info_range), elem))
   387       }).headOption.map(_.info)
   388 
   389   def command_results(range: Text.Range): Command.Results =
   390   {
   391     val results =
   392       snapshot.select[Command.Results](range, Document.Elements.full, command_state =>
   393         { case _ => Some(command_state.results) }).map(_.info)
   394     (Command.Results.empty /: results)(_ ++ _)
   395   }
   396 
   397 
   398   /* tooltip messages */
   399 
   400   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   401   {
   402     val results =
   403       snapshot.cumulate[List[Text.Info[Command.Results.Entry]]](
   404         range, Nil, Rendering.tooltip_message_elements, _ =>
   405         {
   406           case (msgs, Text.Info(info_range,
   407             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   408           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   409             val entry: Command.Results.Entry =
   410               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   411             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   412 
   413           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   414           if !body.isEmpty =>
   415             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   416             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   417 
   418           case _ => None
   419         }).flatMap(_.info)
   420     if (results.isEmpty) None
   421     else {
   422       val r = Text.Range(results.head.range.start, results.last.range.stop)
   423       val msgs = Command.Results.make(results.map(_.info))
   424       Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
   425     }
   426   }
   427 
   428 
   429   /* tooltips */
   430 
   431   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   432     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   433 
   434   private def timing_threshold: Double = options.real("jedit_timing_threshold")
   435 
   436   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   437   {
   438     def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
   439       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
   440     {
   441       val r = snapshot.convert(r0)
   442       val (t, info) = prev.info
   443       if (prev.range == r)
   444         Text.Info(r, (t, info :+ p))
   445       else Text.Info(r, (t, Vector(p)))
   446     }
   447 
   448     val results =
   449       snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
   450         range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
   451         {
   452           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   453             Some(Text.Info(r, (t1 + t2, info)))
   454           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   455             val kind1 = space_explode('_', kind).mkString(" ")
   456             val txt1 = kind1 + " " + quote(name)
   457             val t = prev.info._1
   458             val txt2 =
   459               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   460                 "\n" + t.message
   461               else ""
   462             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   463           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   464           if Path.is_valid(name) =>
   465             val jedit_file = PIDE.thy_load.append(snapshot.node_name.master_dir, Path.explode(name))
   466             Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
   467           case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
   468             Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
   469           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   470           if name == Markup.SORTING || name == Markup.TYPING =>
   471             Some(add(prev, r, (true, pretty_typing("::", body))))
   472           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   473             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   474           case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
   475             Some(add(prev, r, (true, XML.Text("language: " + language))))
   476           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   477             Rendering.tooltip_descriptions.get(name).
   478               map(descr => add(prev, r, (true, XML.Text(descr))))
   479         }).map(_.info)
   480 
   481     results.map(_.info).flatMap(res => res._2.toList) match {
   482       case Nil => None
   483       case tips =>
   484         val r = Text.Range(results.head.range.start, results.last.range.stop)
   485         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   486         Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
   487     }
   488   }
   489 
   490   def tooltip_margin: Int = options.int("jedit_tooltip_margin")
   491 
   492   lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
   493   lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
   494 
   495 
   496   /* gutter icons */
   497 
   498   private lazy val gutter_icons = Map(
   499     Rendering.information_pri -> JEdit_Lib.load_icon(options.string("gutter_information_icon")),
   500     Rendering.warning_pri -> JEdit_Lib.load_icon(options.string("gutter_warning_icon")),
   501     Rendering.legacy_pri -> JEdit_Lib.load_icon(options.string("gutter_legacy_icon")),
   502     Rendering.error_pri -> JEdit_Lib.load_icon(options.string("gutter_error_icon")))
   503 
   504   def gutter_message(range: Text.Range): Option[Icon] =
   505   {
   506     val results =
   507       snapshot.cumulate[Int](range, 0, Rendering.gutter_elements, _ =>
   508         {
   509           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _),
   510               List(XML.Elem(Markup(Markup.INFORMATION, _), _))))) =>
   511             Some(pri max Rendering.information_pri)
   512           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   513             body match {
   514               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   515                 Some(pri max Rendering.legacy_pri)
   516               case _ =>
   517                 Some(pri max Rendering.warning_pri)
   518             }
   519           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   520             Some(pri max Rendering.error_pri)
   521           case _ => None
   522         })
   523     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   524     gutter_icons.get(pri)
   525   }
   526 
   527 
   528   /* squiggly underline */
   529 
   530   private lazy val squiggly_colors = Map(
   531     Rendering.writeln_pri -> writeln_color,
   532     Rendering.information_pri -> information_color,
   533     Rendering.warning_pri -> warning_color,
   534     Rendering.error_pri -> error_color)
   535 
   536   def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
   537   {
   538     val results =
   539       snapshot.cumulate[Int](range, 0, Rendering.squiggly_elements, _ =>
   540         {
   541           case (pri, Text.Info(_, elem)) =>
   542             if (Protocol.is_information(elem))
   543               Some(pri max Rendering.information_pri)
   544             else
   545               Some(pri max Rendering.message_pri(elem.name))
   546         })
   547     for {
   548       Text.Info(r, pri) <- results
   549       color <- squiggly_colors.get(pri)
   550     } yield Text.Info(r, color)
   551   }
   552 
   553 
   554   /* message output */
   555 
   556   private lazy val message_colors = Map(
   557     Rendering.writeln_pri -> writeln_message_color,
   558     Rendering.information_pri -> information_message_color,
   559     Rendering.tracing_pri -> tracing_message_color,
   560     Rendering.warning_pri -> warning_message_color,
   561     Rendering.error_pri -> error_message_color)
   562 
   563   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   564   {
   565     val results =
   566       snapshot.cumulate[Int](range, 0, Rendering.line_background_elements, _ =>
   567         {
   568           case (pri, Text.Info(_, elem)) =>
   569             if (elem.name == Markup.INFORMATION)
   570               Some(pri max Rendering.information_pri)
   571             else
   572               Some(pri max Rendering.message_pri(elem.name))
   573         })
   574     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   575 
   576     val is_separator =
   577       pri > 0 &&
   578       snapshot.cumulate[Boolean](range, false, Rendering.separator_elements, _ =>
   579         {
   580           case _ => Some(true)
   581         }).exists(_.info)
   582 
   583     message_colors.get(pri).map((_, is_separator))
   584   }
   585 
   586   def output_messages(st: Command.State): List[XML.Tree] =
   587     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   588 
   589 
   590   /* text background */
   591 
   592   def background(range: Text.Range): List[Text.Info[Color]] =
   593   {
   594     if (snapshot.is_outdated) List(Text.Info(range, outdated_color))
   595     else
   596       for {
   597         Text.Info(r, result) <-
   598           snapshot.cumulate[(Option[Protocol.Status], Option[Color])](
   599             range, (Some(Protocol.Status.init), None), Rendering.background_elements,
   600             command_state =>
   601               {
   602                 case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   603                 if (Protocol.command_status_elements(markup.name)) =>
   604                   Some((Some(Protocol.command_status(status, markup)), color))
   605                 case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   606                   Some((None, Some(bad_color)))
   607                 case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   608                   Some((None, Some(intensify_color)))
   609                 case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   610                   command_state.results.get(serial) match {
   611                     case Some(Protocol.Dialog_Result(res)) if res == result =>
   612                       Some((None, Some(active_result_color)))
   613                     case _ =>
   614                       Some((None, Some(active_color)))
   615                   }
   616                 case (_, Text.Info(_, elem)) =>
   617                   if (Rendering.active_elements(elem.name)) Some((None, Some(active_color)))
   618                   else None
   619               })
   620         color <-
   621           (result match {
   622             case (Some(status), opt_color) =>
   623               if (status.is_unprocessed) Some(unprocessed1_color)
   624               else if (status.is_running) Some(running1_color)
   625               else opt_color
   626             case (_, opt_color) => opt_color
   627           })
   628       } yield Text.Info(r, color)
   629   }
   630 
   631 
   632   /* text foreground */
   633 
   634   def foreground(range: Text.Range): List[Text.Info[Color]] =
   635     snapshot.select(range, Rendering.foreground_elements, _ =>
   636       {
   637         case Text.Info(_, elem) =>
   638           if (elem.name == Markup.ANTIQUOTED) Some(antiquoted_color) else Some(quoted_color)
   639       })
   640 
   641 
   642   /* text color */
   643 
   644   private lazy val text_colors: Map[String, Color] = Map(
   645       Markup.KEYWORD1 -> keyword1_color,
   646       Markup.KEYWORD2 -> keyword2_color,
   647       Markup.KEYWORD3 -> keyword3_color,
   648       Markup.STRING -> Color.BLACK,
   649       Markup.ALTSTRING -> Color.BLACK,
   650       Markup.VERBATIM -> Color.BLACK,
   651       Markup.CARTOUCHE -> Color.BLACK,
   652       Markup.LITERAL -> keyword1_color,
   653       Markup.DELIMITER -> Color.BLACK,
   654       Markup.TFREE -> tfree_color,
   655       Markup.TVAR -> tvar_color,
   656       Markup.FREE -> free_color,
   657       Markup.SKOLEM -> skolem_color,
   658       Markup.BOUND -> bound_color,
   659       Markup.VAR -> var_color,
   660       Markup.INNER_STRING -> inner_quoted_color,
   661       Markup.INNER_CARTOUCHE -> inner_cartouche_color,
   662       Markup.INNER_COMMENT -> inner_comment_color,
   663       Markup.DYNAMIC_FACT -> dynamic_color,
   664       Markup.ANTIQUOTE -> antiquote_color,
   665       Markup.ML_KEYWORD1 -> keyword1_color,
   666       Markup.ML_KEYWORD2 -> keyword2_color,
   667       Markup.ML_KEYWORD3 -> keyword3_color,
   668       Markup.ML_DELIMITER -> Color.BLACK,
   669       Markup.ML_NUMERAL -> inner_numeral_color,
   670       Markup.ML_CHAR -> inner_quoted_color,
   671       Markup.ML_STRING -> inner_quoted_color,
   672       Markup.ML_COMMENT -> inner_comment_color)
   673 
   674   private lazy val text_color_elements =
   675     Document.Elements(text_colors.keySet)
   676 
   677   def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
   678   {
   679     if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
   680     else
   681       snapshot.cumulate(range, color, text_color_elements, _ =>
   682         {
   683           case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
   684         })
   685   }
   686 
   687 
   688   /* virtual bullets */
   689 
   690   def bullet(range: Text.Range): List[Text.Info[Color]] =
   691     snapshot.select(range, Rendering.bullet_elements, _ => _ => Some(bullet_color))
   692 
   693 
   694   /* text folds */
   695 
   696   def fold_depth(range: Text.Range): List[Text.Info[Int]] =
   697     snapshot.cumulate[Int](range, 0, Rendering.fold_depth_elements, _ =>
   698       {
   699         case (depth, _) => Some(depth + 1)
   700       })
   701 }