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