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