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