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