src/Tools/jEdit/src/rendering.scala
author wenzelm
Mon Mar 03 10:41:58 2014 +0100 (2014-03-03 ago)
changeset 55876 142139457653
parent 55837 154855d9a564
child 55877 65c9968286d5
permissions -rw-r--r--
tuned signature;
     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   def hyperlink(range: Text.Range): Option[Text.Info[PIDE.editor.Hyperlink]] =
   331   {
   332     snapshot.cumulate[Vector[Text.Info[PIDE.editor.Hyperlink]]](
   333       range, Vector.empty, Rendering.hyperlink_elements, _ =>
   334         {
   335           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   336           if Path.is_ok(name) =>
   337             val jedit_file = PIDE.thy_load.append(snapshot.node_name.master_dir, Path.explode(name))
   338             val link = PIDE.editor.hyperlink_file(jedit_file)
   339             Some(links :+ Text.Info(snapshot.convert(info_range), link))
   340 
   341           case (links, Text.Info(info_range, XML.Elem(Markup.Url(name), _))) =>
   342             val link = PIDE.editor.hyperlink_url(name)
   343             Some(links :+ Text.Info(snapshot.convert(info_range), link))
   344 
   345           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   346           if !props.exists(
   347             { case (Markup.KIND, Markup.ML_OPEN) => true
   348               case (Markup.KIND, Markup.ML_STRUCTURE) => true
   349               case _ => false }) =>
   350 
   351             val opt_link =
   352               props match {
   353                 case Position.Def_Line_File(line, name) =>
   354                   PIDE.editor.hyperlink_source_file(name, line)
   355                 case Position.Def_Id_Offset(id, offset) =>
   356                   PIDE.editor.hyperlink_command_id(snapshot, id, offset)
   357                 case _ => None
   358               }
   359             opt_link.map(link => (links :+ Text.Info(snapshot.convert(info_range), link)))
   360 
   361           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
   362             val opt_link =
   363               props match {
   364                 case Position.Line_File(line, name) =>
   365                   PIDE.editor.hyperlink_source_file(name, line)
   366                 case Position.Id_Offset(id, offset) =>
   367                   PIDE.editor.hyperlink_command_id(snapshot, id, offset)
   368                 case _ => None
   369               }
   370             opt_link.map(link => links :+ (Text.Info(snapshot.convert(info_range), link)))
   371 
   372           case _ => None
   373         }) match { case Text.Info(_, _ :+ info) :: _ => Some(info) case _ => None }
   374   }
   375 
   376 
   377   /* active elements */
   378 
   379   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   380     snapshot.select(range, Rendering.active_elements, command_state =>
   381       {
   382         case Text.Info(info_range, elem) =>
   383           if (elem.name == Markup.DIALOG) {
   384             elem match {
   385               case Protocol.Dialog(_, serial, _)
   386               if !command_state.results.defined(serial) =>
   387                 Some(Text.Info(snapshot.convert(info_range), elem))
   388               case _ => None
   389             }
   390           }
   391           else Some(Text.Info(snapshot.convert(info_range), elem))
   392       }).headOption.map(_.info)
   393 
   394   def command_results(range: Text.Range): Command.Results =
   395   {
   396     val results =
   397       snapshot.select[Command.Results](range, Document.Elements.full, command_state =>
   398         { case _ => Some(command_state.results) }).map(_.info)
   399     (Command.Results.empty /: results)(_ ++ _)
   400   }
   401 
   402 
   403   /* tooltip messages */
   404 
   405   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   406   {
   407     val results =
   408       snapshot.cumulate[List[Text.Info[Command.Results.Entry]]](
   409         range, Nil, Rendering.tooltip_message_elements, _ =>
   410         {
   411           case (msgs, Text.Info(info_range,
   412             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   413           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   414             val entry: Command.Results.Entry =
   415               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   416             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   417 
   418           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   419           if !body.isEmpty =>
   420             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   421             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   422 
   423           case _ => None
   424         }).flatMap(_.info)
   425     if (results.isEmpty) None
   426     else {
   427       val r = Text.Range(results.head.range.start, results.last.range.stop)
   428       val msgs = Command.Results.make(results.map(_.info))
   429       Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
   430     }
   431   }
   432 
   433 
   434   /* tooltips */
   435 
   436   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   437     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   438 
   439   private def timing_threshold: Double = options.real("jedit_timing_threshold")
   440 
   441   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   442   {
   443     def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
   444       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
   445     {
   446       val r = snapshot.convert(r0)
   447       val (t, info) = prev.info
   448       if (prev.range == r)
   449         Text.Info(r, (t, info :+ p))
   450       else Text.Info(r, (t, Vector(p)))
   451     }
   452 
   453     val results =
   454       snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
   455         range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
   456         {
   457           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   458             Some(Text.Info(r, (t1 + t2, info)))
   459           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   460             val kind1 = space_explode('_', kind).mkString(" ")
   461             val txt1 = kind1 + " " + quote(name)
   462             val t = prev.info._1
   463             val txt2 =
   464               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   465                 "\n" + t.message
   466               else ""
   467             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   468           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   469           if Path.is_ok(name) =>
   470             val jedit_file = PIDE.thy_load.append(snapshot.node_name.master_dir, Path.explode(name))
   471             Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
   472           case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
   473             Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
   474           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   475           if name == Markup.SORTING || name == Markup.TYPING =>
   476             Some(add(prev, r, (true, pretty_typing("::", body))))
   477           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   478             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   479           case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
   480             Some(add(prev, r, (true, XML.Text("language: " + language))))
   481           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   482             Rendering.tooltip_descriptions.get(name).
   483               map(descr => add(prev, r, (true, XML.Text(descr))))
   484         }).map(_.info)
   485 
   486     results.map(_.info).flatMap(res => res._2.toList) match {
   487       case Nil => None
   488       case tips =>
   489         val r = Text.Range(results.head.range.start, results.last.range.stop)
   490         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   491         Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
   492     }
   493   }
   494 
   495   def tooltip_margin: Int = options.int("jedit_tooltip_margin")
   496 
   497   lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
   498   lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
   499 
   500 
   501   /* gutter icons */
   502 
   503   private lazy val gutter_icons = Map(
   504     Rendering.information_pri -> JEdit_Lib.load_icon(options.string("gutter_information_icon")),
   505     Rendering.warning_pri -> JEdit_Lib.load_icon(options.string("gutter_warning_icon")),
   506     Rendering.legacy_pri -> JEdit_Lib.load_icon(options.string("gutter_legacy_icon")),
   507     Rendering.error_pri -> JEdit_Lib.load_icon(options.string("gutter_error_icon")))
   508 
   509   def gutter_message(range: Text.Range): Option[Icon] =
   510   {
   511     val results =
   512       snapshot.cumulate[Int](range, 0, Rendering.gutter_elements, _ =>
   513         {
   514           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _),
   515               List(XML.Elem(Markup(Markup.INFORMATION, _), _))))) =>
   516             Some(pri max Rendering.information_pri)
   517           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   518             body match {
   519               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   520                 Some(pri max Rendering.legacy_pri)
   521               case _ =>
   522                 Some(pri max Rendering.warning_pri)
   523             }
   524           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   525             Some(pri max Rendering.error_pri)
   526           case _ => None
   527         })
   528     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   529     gutter_icons.get(pri)
   530   }
   531 
   532 
   533   /* squiggly underline */
   534 
   535   private lazy val squiggly_colors = Map(
   536     Rendering.writeln_pri -> writeln_color,
   537     Rendering.information_pri -> information_color,
   538     Rendering.warning_pri -> warning_color,
   539     Rendering.error_pri -> error_color)
   540 
   541   def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
   542   {
   543     val results =
   544       snapshot.cumulate[Int](range, 0, Rendering.squiggly_elements, _ =>
   545         {
   546           case (pri, Text.Info(_, elem)) =>
   547             if (Protocol.is_information(elem))
   548               Some(pri max Rendering.information_pri)
   549             else
   550               Some(pri max Rendering.message_pri(elem.name))
   551         })
   552     for {
   553       Text.Info(r, pri) <- results
   554       color <- squiggly_colors.get(pri)
   555     } yield Text.Info(r, color)
   556   }
   557 
   558 
   559   /* message output */
   560 
   561   private lazy val message_colors = Map(
   562     Rendering.writeln_pri -> writeln_message_color,
   563     Rendering.information_pri -> information_message_color,
   564     Rendering.tracing_pri -> tracing_message_color,
   565     Rendering.warning_pri -> warning_message_color,
   566     Rendering.error_pri -> error_message_color)
   567 
   568   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   569   {
   570     val results =
   571       snapshot.cumulate[Int](range, 0, Rendering.line_background_elements, _ =>
   572         {
   573           case (pri, Text.Info(_, elem)) =>
   574             if (elem.name == Markup.INFORMATION)
   575               Some(pri max Rendering.information_pri)
   576             else
   577               Some(pri max Rendering.message_pri(elem.name))
   578         })
   579     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   580 
   581     val is_separator =
   582       pri > 0 &&
   583       snapshot.cumulate[Boolean](range, false, Rendering.separator_elements, _ =>
   584         {
   585           case _ => Some(true)
   586         }).exists(_.info)
   587 
   588     message_colors.get(pri).map((_, is_separator))
   589   }
   590 
   591   def output_messages(st: Command.State): List[XML.Tree] =
   592     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   593 
   594 
   595   /* text background */
   596 
   597   def background(range: Text.Range): List[Text.Info[Color]] =
   598   {
   599     if (snapshot.is_outdated) List(Text.Info(range, outdated_color))
   600     else
   601       for {
   602         Text.Info(r, result) <-
   603           snapshot.cumulate[(Option[Protocol.Status], Option[Color])](
   604             range, (Some(Protocol.Status.init), None), Rendering.background_elements,
   605             command_state =>
   606               {
   607                 case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   608                 if (Protocol.command_status_elements(markup.name)) =>
   609                   Some((Some(Protocol.command_status(status, markup)), color))
   610                 case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   611                   Some((None, Some(bad_color)))
   612                 case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   613                   Some((None, Some(intensify_color)))
   614                 case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   615                   command_state.results.get(serial) match {
   616                     case Some(Protocol.Dialog_Result(res)) if res == result =>
   617                       Some((None, Some(active_result_color)))
   618                     case _ =>
   619                       Some((None, Some(active_color)))
   620                   }
   621                 case (_, Text.Info(_, elem)) =>
   622                   if (Rendering.active_elements(elem.name)) Some((None, Some(active_color)))
   623                   else None
   624               })
   625         color <-
   626           (result match {
   627             case (Some(status), opt_color) =>
   628               if (status.is_unprocessed) Some(unprocessed1_color)
   629               else if (status.is_running) Some(running1_color)
   630               else opt_color
   631             case (_, opt_color) => opt_color
   632           })
   633       } yield Text.Info(r, color)
   634   }
   635 
   636 
   637   /* text foreground */
   638 
   639   def foreground(range: Text.Range): List[Text.Info[Color]] =
   640     snapshot.select(range, Rendering.foreground_elements, _ =>
   641       {
   642         case Text.Info(_, elem) =>
   643           if (elem.name == Markup.ANTIQUOTED) Some(antiquoted_color) else Some(quoted_color)
   644       })
   645 
   646 
   647   /* text color */
   648 
   649   private lazy val text_colors: Map[String, Color] = Map(
   650       Markup.KEYWORD1 -> keyword1_color,
   651       Markup.KEYWORD2 -> keyword2_color,
   652       Markup.KEYWORD3 -> keyword3_color,
   653       Markup.STRING -> Color.BLACK,
   654       Markup.ALTSTRING -> Color.BLACK,
   655       Markup.VERBATIM -> Color.BLACK,
   656       Markup.CARTOUCHE -> Color.BLACK,
   657       Markup.LITERAL -> keyword1_color,
   658       Markup.DELIMITER -> Color.BLACK,
   659       Markup.TFREE -> tfree_color,
   660       Markup.TVAR -> tvar_color,
   661       Markup.FREE -> free_color,
   662       Markup.SKOLEM -> skolem_color,
   663       Markup.BOUND -> bound_color,
   664       Markup.VAR -> var_color,
   665       Markup.INNER_STRING -> inner_quoted_color,
   666       Markup.INNER_CARTOUCHE -> inner_cartouche_color,
   667       Markup.INNER_COMMENT -> inner_comment_color,
   668       Markup.DYNAMIC_FACT -> dynamic_color,
   669       Markup.ANTIQUOTE -> antiquote_color,
   670       Markup.ML_KEYWORD1 -> keyword1_color,
   671       Markup.ML_KEYWORD2 -> keyword2_color,
   672       Markup.ML_KEYWORD3 -> keyword3_color,
   673       Markup.ML_DELIMITER -> Color.BLACK,
   674       Markup.ML_NUMERAL -> inner_numeral_color,
   675       Markup.ML_CHAR -> inner_quoted_color,
   676       Markup.ML_STRING -> inner_quoted_color,
   677       Markup.ML_COMMENT -> inner_comment_color)
   678 
   679   private lazy val text_color_elements =
   680     Document.Elements(text_colors.keySet)
   681 
   682   def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
   683   {
   684     if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
   685     else
   686       snapshot.cumulate(range, color, text_color_elements, _ =>
   687         {
   688           case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
   689         })
   690   }
   691 
   692 
   693   /* virtual bullets */
   694 
   695   def bullet(range: Text.Range): List[Text.Info[Color]] =
   696     snapshot.select(range, Rendering.bullet_elements, _ => _ => Some(bullet_color))
   697 
   698 
   699   /* text folds */
   700 
   701   def fold_depth(range: Text.Range): List[Text.Info[Int]] =
   702     snapshot.cumulate[Int](range, 0, Rendering.fold_depth_elements, _ =>
   703       {
   704         case (depth, _) => Some(depth + 1)
   705       })
   706 }