src/Tools/jEdit/src/rendering.scala
author wenzelm
Wed Jun 22 16:04:03 2016 +0200 (2016-06-22 ago)
changeset 63347 e344dc82f6c2
parent 63340 29d7ac9bdcb1
child 63441 4c3fa4dba79f
permissions -rw-r--r--
report class parameters within instantiation;
     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
    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_keyword && token.source == Symbol.comment_decoded) JEditToken.NULL
    98     else if (token.is_delimiter) JEditToken.OPERATOR
    99     else token_style(token.kind)
   100 
   101 
   102   /* Isabelle/ML token markup */
   103 
   104   private val ml_token_style: Map[ML_Lex.Kind.Value, Byte] =
   105   {
   106     import JEditToken._
   107     Map[ML_Lex.Kind.Value, Byte](
   108       ML_Lex.Kind.KEYWORD -> NULL,
   109       ML_Lex.Kind.IDENT -> NULL,
   110       ML_Lex.Kind.LONG_IDENT -> NULL,
   111       ML_Lex.Kind.TYPE_VAR -> NULL,
   112       ML_Lex.Kind.WORD -> DIGIT,
   113       ML_Lex.Kind.INT -> DIGIT,
   114       ML_Lex.Kind.REAL -> DIGIT,
   115       ML_Lex.Kind.CHAR -> LITERAL2,
   116       ML_Lex.Kind.STRING -> LITERAL1,
   117       ML_Lex.Kind.SPACE -> NULL,
   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_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 caret_focus_elements = Markup.Elements(Markup.ENTITY)
   155 
   156   private val highlight_elements =
   157     Markup.Elements(Markup.EXPRESSION, Markup.CITATION, Markup.LANGUAGE, Markup.ML_TYPING,
   158       Markup.TOKEN_RANGE, Markup.ENTITY, Markup.PATH, Markup.DOC, Markup.URL, Markup.SORTING,
   159       Markup.TYPING, Markup.CLASS_PARAMETER, Markup.FREE, Markup.SKOLEM, Markup.BOUND,
   160       Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_BREAKPOINT,
   161       Markup.MARKDOWN_PARAGRAPH, Markup.Markdown_List.name)
   162 
   163   private val hyperlink_elements =
   164     Markup.Elements(Markup.ENTITY, Markup.PATH, Markup.DOC, Markup.POSITION,
   165       Markup.CITATION, Markup.URL)
   166 
   167   private val active_elements =
   168     Markup.Elements(Markup.DIALOG, Markup.BROWSER, Markup.GRAPHVIEW,
   169       Markup.SENDBACK, Markup.SIMP_TRACE_PANEL)
   170 
   171   private val tooltip_message_elements =
   172     Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR,
   173       Markup.BAD)
   174 
   175   private val tooltip_descriptions =
   176     Map(
   177       Markup.CITATION -> "citation",
   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     Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
   188       Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
   189       Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
   190       Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
   191 
   192   private val gutter_elements =
   193     Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR)
   194 
   195   private val squiggly_elements =
   196     Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR)
   197 
   198   private val line_background_elements =
   199     Markup.Elements(Markup.WRITELN_MESSAGE, Markup.STATE_MESSAGE, Markup.INFORMATION_MESSAGE,
   200       Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE, Markup.LEGACY_MESSAGE,
   201       Markup.ERROR_MESSAGE)
   202 
   203   private val separator_elements =
   204     Markup.Elements(Markup.SEPARATOR)
   205 
   206   private val background_elements =
   207     Protocol.proper_status_elements + Markup.WRITELN_MESSAGE +
   208       Markup.STATE_MESSAGE + Markup.INFORMATION_MESSAGE +
   209       Markup.TRACING_MESSAGE + Markup.WARNING_MESSAGE +
   210       Markup.LEGACY_MESSAGE + Markup.ERROR_MESSAGE +
   211       Markup.BAD + Markup.INTENSIFY + Markup.ENTITY +
   212       Markup.Markdown_Item.name ++ active_elements
   213 
   214   private val foreground_elements =
   215     Markup.Elements(Markup.STRING, Markup.ALT_STRING, Markup.VERBATIM,
   216       Markup.CARTOUCHE, Markup.ANTIQUOTED)
   217 
   218   private val bullet_elements =
   219     Markup.Elements(Markup.BULLET, Markup.ML_BREAKPOINT)
   220 
   221   private val fold_depth_elements =
   222     Markup.Elements(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   223 }
   224 
   225 
   226 class Rendering private(val snapshot: Document.Snapshot, val options: Options)
   227 {
   228   override def toString: String = "Rendering(" + snapshot.toString + ")"
   229 
   230 
   231   /* colors */
   232 
   233   def color_value(s: String): Color = Color_Value(options.string(s))
   234 
   235   val outdated_color = color_value("outdated_color")
   236   val unprocessed_color = color_value("unprocessed_color")
   237   val unprocessed1_color = color_value("unprocessed1_color")
   238   val running_color = color_value("running_color")
   239   val running1_color = color_value("running1_color")
   240   val bullet_color = color_value("bullet_color")
   241   val tooltip_color = color_value("tooltip_color")
   242   val writeln_color = color_value("writeln_color")
   243   val information_color = color_value("information_color")
   244   val warning_color = color_value("warning_color")
   245   val legacy_color = color_value("legacy_color")
   246   val error_color = color_value("error_color")
   247   val writeln_message_color = color_value("writeln_message_color")
   248   val information_message_color = color_value("information_message_color")
   249   val tracing_message_color = color_value("tracing_message_color")
   250   val warning_message_color = color_value("warning_message_color")
   251   val legacy_message_color = color_value("legacy_message_color")
   252   val error_message_color = color_value("error_message_color")
   253   val spell_checker_color = color_value("spell_checker_color")
   254   val bad_color = color_value("bad_color")
   255   val intensify_color = color_value("intensify_color")
   256   val entity_color = color_value("entity_color")
   257   val entity_ref_color = color_value("entity_ref_color")
   258   val breakpoint_disabled_color = color_value("breakpoint_disabled_color")
   259   val breakpoint_enabled_color = color_value("breakpoint_enabled_color")
   260   val caret_debugger_color = color_value("caret_debugger_color")
   261   val quoted_color = color_value("quoted_color")
   262   val antiquoted_color = color_value("antiquoted_color")
   263   val antiquote_color = color_value("antiquote_color")
   264   val highlight_color = color_value("highlight_color")
   265   val hyperlink_color = color_value("hyperlink_color")
   266   val active_color = color_value("active_color")
   267   val active_hover_color = color_value("active_hover_color")
   268   val active_result_color = color_value("active_result_color")
   269   val keyword1_color = color_value("keyword1_color")
   270   val keyword2_color = color_value("keyword2_color")
   271   val keyword3_color = color_value("keyword3_color")
   272   val quasi_keyword_color = color_value("quasi_keyword_color")
   273   val improper_color = color_value("improper_color")
   274   val operator_color = color_value("operator_color")
   275   val caret_invisible_color = color_value("caret_invisible_color")
   276   val completion_color = color_value("completion_color")
   277   val search_color = color_value("search_color")
   278 
   279   val tfree_color = color_value("tfree_color")
   280   val tvar_color = color_value("tvar_color")
   281   val free_color = color_value("free_color")
   282   val skolem_color = color_value("skolem_color")
   283   val bound_color = color_value("bound_color")
   284   val var_color = color_value("var_color")
   285   val inner_numeral_color = color_value("inner_numeral_color")
   286   val inner_quoted_color = color_value("inner_quoted_color")
   287   val inner_cartouche_color = color_value("inner_cartouche_color")
   288   val inner_comment_color = color_value("inner_comment_color")
   289   val dynamic_color = color_value("dynamic_color")
   290   val class_parameter_color = color_value("class_parameter_color")
   291 
   292   val markdown_item_color1 = color_value("markdown_item_color1")
   293   val markdown_item_color2 = color_value("markdown_item_color2")
   294   val markdown_item_color3 = color_value("markdown_item_color3")
   295   val markdown_item_color4 = color_value("markdown_item_color4")
   296 
   297 
   298   /* completion */
   299 
   300   def semantic_completion(completed_range: Option[Text.Range], range: Text.Range)
   301       : Option[Text.Info[Completion.Semantic]] =
   302     if (snapshot.is_outdated) None
   303     else {
   304       snapshot.select(range, Rendering.semantic_completion_elements, _ =>
   305         {
   306           case Completion.Semantic.Info(info) =>
   307             completed_range match {
   308               case Some(range0) if range0.contains(info.range) && range0 != info.range => None
   309               case _ => Some(info)
   310             }
   311           case _ => None
   312         }).headOption.map(_.info)
   313     }
   314 
   315   def language_context(range: Text.Range): Option[Completion.Language_Context] =
   316     snapshot.select(range, Rendering.language_context_elements, _ =>
   317       {
   318         case Text.Info(_, XML.Elem(Markup.Language(language, symbols, antiquotes, delimited), _)) =>
   319           if (delimited) Some(Completion.Language_Context(language, symbols, antiquotes))
   320           else None
   321         case Text.Info(_, elem)
   322         if elem.name == Markup.ML_STRING || elem.name == Markup.ML_COMMENT =>
   323           Some(Completion.Language_Context.ML_inner)
   324         case Text.Info(_, _) =>
   325           Some(Completion.Language_Context.inner)
   326       }).headOption.map(_.info)
   327 
   328   def language_path(range: Text.Range): Option[Text.Range] =
   329     snapshot.select(range, Rendering.language_elements, _ =>
   330       {
   331         case Text.Info(info_range, XML.Elem(Markup.Language(Markup.Language.PATH, _, _, _), _)) =>
   332           Some(snapshot.convert(info_range))
   333         case _ => None
   334       }).headOption.map(_.info)
   335 
   336   def citation(range: Text.Range): Option[Text.Info[String]] =
   337     snapshot.select(range, Rendering.citation_elements, _ =>
   338       {
   339         case Text.Info(info_range, XML.Elem(Markup.Citation(name), _)) =>
   340           Some(Text.Info(snapshot.convert(info_range), name))
   341         case _ => None
   342       }).headOption.map(_.info)
   343 
   344 
   345   /* spell checker */
   346 
   347   private lazy val spell_checker_elements =
   348     Markup.Elements(space_explode(',', options.string("spell_checker_elements")): _*)
   349 
   350   def spell_checker_ranges(range: Text.Range): List[Text.Range] =
   351     snapshot.select(range, spell_checker_elements, _ => _ => Some(())).map(_.range)
   352 
   353   def spell_checker_point(range: Text.Range): Option[Text.Range] =
   354     snapshot.select(range, spell_checker_elements, _ =>
   355       {
   356         case info => Some(snapshot.convert(info.range))
   357       }).headOption.map(_.info)
   358 
   359 
   360   /* breakpoints */
   361 
   362   def breakpoint(range: Text.Range): Option[(Command, Long)] =
   363     if (snapshot.is_outdated) None
   364     else
   365       snapshot.select(range, Rendering.breakpoint_elements, command_states =>
   366         {
   367           case Text.Info(_, Protocol.ML_Breakpoint(breakpoint)) =>
   368             command_states match {
   369               case st :: _ => Some((st.command, breakpoint))
   370               case _ => None
   371             }
   372           case _ => None
   373         }).headOption.map(_.info)
   374 
   375 
   376   /* command status overview */
   377 
   378   def overview_color(range: Text.Range): Option[Color] =
   379   {
   380     if (snapshot.is_outdated) None
   381     else {
   382       val results =
   383         snapshot.cumulate[List[Markup]](range, Nil, Protocol.liberal_status_elements, _ =>
   384           {
   385             case (status, Text.Info(_, elem)) => Some(elem.markup :: status)
   386           }, status = true)
   387       if (results.isEmpty) None
   388       else {
   389         val status = Protocol.Status.make(results.iterator.flatMap(_.info))
   390 
   391         if (status.is_running) Some(running_color)
   392         else if (status.is_failed) Some(error_color)
   393         else if (status.is_warned) Some(warning_color)
   394         else if (status.is_unprocessed) Some(unprocessed_color)
   395         else None
   396       }
   397     }
   398   }
   399 
   400 
   401   /* caret focus */
   402 
   403   def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
   404   {
   405     def focus(is_def: Boolean): Set[Long] =
   406     {
   407       val results =
   408         snapshot.cumulate[Set[Long]](caret_range, Set.empty, Rendering.caret_focus_elements, _ =>
   409             {
   410               case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   411                 props match {
   412                   case Markup.Entity.Def(i) if is_def => Some(serials + i)
   413                   case Markup.Entity.Ref(i) if !is_def => Some(serials + i)
   414                   case _ => None
   415                 }
   416               case _ => None
   417             })
   418       (Set.empty[Long] /: results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
   419     }
   420 
   421     val focus_defs = focus(true)
   422 
   423     if (focus_defs.nonEmpty) focus_defs
   424     else {
   425       val focus_refs = focus(false)
   426       def visible_def: Boolean =
   427         snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
   428           {
   429             case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   430               props match {
   431                 case Markup.Entity.Def(i) if focus_refs(i) => Some(true)
   432                 case _ => None
   433               }
   434           }).exists(info => info.info)
   435       if (focus_refs.nonEmpty && visible_def) focus_refs
   436       else Set.empty[Long]
   437     }
   438   }
   439 
   440   def caret_focus_ranges(caret_range: Text.Range, visible_range: Text.Range): List[Text.Range] =
   441   {
   442     val focus = caret_focus(caret_range, visible_range)
   443     if (focus.nonEmpty) {
   444       val results =
   445         snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
   446           {
   447             case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   448               props match {
   449                 case Markup.Entity.Def(i) if focus(i) => Some(true)
   450                 case Markup.Entity.Ref(i) if focus(i) => Some(true)
   451                 case _ => None
   452               }
   453           })
   454       for (info <- results if info.info) yield info.range
   455     }
   456     else Nil
   457   }
   458 
   459   def entity_ref(range: Text.Range, focus: Set[Long]): List[Text.Info[Color]] =
   460     snapshot.select(range, Rendering.caret_focus_elements, _ =>
   461       {
   462         case Text.Info(_, XML.Elem(Markup(Markup.ENTITY, Markup.Entity.Ref(i)), _)) if focus(i) =>
   463           Some(entity_ref_color)
   464         case _ => None
   465       })
   466 
   467 
   468   /* highlighted area */
   469 
   470   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   471     snapshot.select(range, Rendering.highlight_elements, _ =>
   472       {
   473         case info => Some(Text.Info(snapshot.convert(info.range), highlight_color))
   474       }).headOption.map(_.info)
   475 
   476 
   477   /* hyperlinks */
   478 
   479   private def jedit_file(name: String): String =
   480     if (Path.is_valid(name))
   481       PIDE.resources.append(snapshot.node_name.master_dir, Path.explode(name))
   482     else name
   483 
   484   def hyperlink(range: Text.Range): Option[Text.Info[PIDE.editor.Hyperlink]] =
   485   {
   486     snapshot.cumulate[Vector[Text.Info[PIDE.editor.Hyperlink]]](
   487       range, Vector.empty, Rendering.hyperlink_elements, _ =>
   488         {
   489           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _))) =>
   490             val link = PIDE.editor.hyperlink_file(true, jedit_file(name))
   491             Some(links :+ Text.Info(snapshot.convert(info_range), link))
   492 
   493           case (links, Text.Info(info_range, XML.Elem(Markup.Doc(name), _))) =>
   494             PIDE.editor.hyperlink_doc(name).map(link =>
   495               (links :+ Text.Info(snapshot.convert(info_range), link)))
   496 
   497           case (links, Text.Info(info_range, XML.Elem(Markup.Url(name), _))) =>
   498             val link = PIDE.editor.hyperlink_url(name)
   499             Some(links :+ Text.Info(snapshot.convert(info_range), link))
   500 
   501           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   502           if !props.exists(
   503             { case (Markup.KIND, Markup.ML_OPEN) => true
   504               case (Markup.KIND, Markup.ML_STRUCTURE) => true
   505               case _ => false }) =>
   506             val opt_link = PIDE.editor.hyperlink_def_position(true, snapshot, props)
   507             opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
   508 
   509           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
   510             val opt_link = PIDE.editor.hyperlink_position(true, snapshot, props)
   511             opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
   512 
   513           case (links, Text.Info(info_range, XML.Elem(Markup.Citation(name), _))) =>
   514             val opt_link =
   515               Bibtex_JEdit.entries_iterator.collectFirst(
   516                 { case (a, buffer, offset) if a == name =>
   517                     PIDE.editor.hyperlink_buffer(true, buffer, offset) })
   518             opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
   519 
   520           case _ => None
   521         }) match { case Text.Info(_, _ :+ info) :: _ => Some(info) case _ => None }
   522   }
   523 
   524 
   525   /* active elements */
   526 
   527   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   528     snapshot.select(range, Rendering.active_elements, command_states =>
   529       {
   530         case Text.Info(info_range, elem) =>
   531           if (elem.name == Markup.DIALOG) {
   532             elem match {
   533               case Protocol.Dialog(_, serial, _)
   534               if !command_states.exists(st => st.results.defined(serial)) =>
   535                 Some(Text.Info(snapshot.convert(info_range), elem))
   536               case _ => None
   537             }
   538           }
   539           else Some(Text.Info(snapshot.convert(info_range), elem))
   540       }).headOption.map(_.info)
   541 
   542   def command_results(range: Text.Range): Command.Results =
   543     Command.State.merge_results(
   544       snapshot.select[List[Command.State]](range, Markup.Elements.full, command_states =>
   545         { case _ => Some(command_states) }).flatMap(_.info))
   546 
   547 
   548   /* tooltip messages */
   549 
   550   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   551   {
   552     val results =
   553       snapshot.cumulate[List[Text.Info[Command.Results.Entry]]](
   554         range, Nil, Rendering.tooltip_message_elements, _ =>
   555         {
   556           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   557           if body.nonEmpty =>
   558             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   559             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   560 
   561           case (msgs, Text.Info(info_range,
   562             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body))) =>
   563             val entry: Command.Results.Entry =
   564               serial -> XML.Elem(Markup(Markup.message(name), props), body)
   565             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   566 
   567           case _ => None
   568         }).flatMap(_.info)
   569     if (results.isEmpty) None
   570     else {
   571       val r = Text.Range(results.head.range.start, results.last.range.stop)
   572       val msgs = Command.Results.make(results.map(_.info))
   573       Some(Text.Info(r, Pretty.separate(msgs.iterator.map(_._2).toList)))
   574     }
   575   }
   576 
   577 
   578   /* tooltips */
   579 
   580   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   581     Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
   582 
   583   private def timing_threshold: Double = options.real("jedit_timing_threshold")
   584 
   585   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   586   {
   587     def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
   588       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
   589     {
   590       val r = snapshot.convert(r0)
   591       val (t, info) = prev.info
   592       if (prev.range == r)
   593         Text.Info(r, (t, info :+ p))
   594       else Text.Info(r, (t, Vector(p)))
   595     }
   596 
   597     val results =
   598       snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
   599         range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
   600         {
   601           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   602             Some(Text.Info(r, (t1 + t2, info)))
   603 
   604           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
   605           if kind != "" &&
   606             kind != Markup.ML_DEF &&
   607             kind != Markup.ML_OPEN &&
   608             kind != Markup.ML_STRUCTURE =>
   609             val kind1 = Word.implode(Word.explode('_', kind))
   610             val txt1 =
   611               if (name == "") kind1
   612               else if (kind1 == "") quote(name)
   613               else kind1 + " " + quote(name)
   614             val t = prev.info._1
   615             val txt2 =
   616               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   617                 "\n" + t.message
   618               else ""
   619             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   620 
   621           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
   622             val file = jedit_file(name)
   623             val text =
   624               if (name == file) "file " + quote(file)
   625               else "path " + quote(name) + "\nfile " + quote(file)
   626             Some(add(prev, r, (true, XML.Text(text))))
   627 
   628           case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
   629             val text = "doc " + quote(name)
   630             Some(add(prev, r, (true, XML.Text(text))))
   631 
   632           case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
   633             Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
   634 
   635           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   636           if name == Markup.SORTING || name == Markup.TYPING =>
   637             Some(add(prev, r, (true, pretty_typing("::", body))))
   638 
   639           case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
   640             Some(add(prev, r, (true, Pretty.block(0, body))))
   641 
   642           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   643             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   644 
   645           case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
   646             val text =
   647               if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
   648               else "breakpoint (disabled)"
   649             Some(add(prev, r, (true, XML.Text(text))))
   650 
   651           case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
   652             val lang = Word.implode(Word.explode('_', language))
   653             Some(add(prev, r, (true, XML.Text("language: " + lang))))
   654 
   655           case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
   656             val descr = if (kind == "") "expression" else "expression: " + kind
   657             Some(add(prev, r, (true, XML.Text(descr))))
   658 
   659           case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
   660             Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
   661           case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
   662             Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
   663 
   664           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   665             Rendering.tooltip_descriptions.get(name).
   666               map(descr => add(prev, r, (true, XML.Text(descr))))
   667         }).map(_.info)
   668 
   669     results.map(_.info).flatMap(res => res._2.toList) match {
   670       case Nil => None
   671       case tips =>
   672         val r = Text.Range(results.head.range.start, results.last.range.stop)
   673         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   674         Some(Text.Info(r, Library.separate(Pretty.fbrk, all_tips)))
   675     }
   676   }
   677 
   678   def tooltip_margin: Int = options.int("jedit_tooltip_margin")
   679 
   680   lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
   681   lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
   682 
   683 
   684   /* gutter */
   685 
   686   private def gutter_message_pri(msg: XML.Tree): Int =
   687     if (Protocol.is_error(msg)) Rendering.error_pri
   688     else if (Protocol.is_legacy(msg)) Rendering.legacy_pri
   689     else if (Protocol.is_warning(msg)) Rendering.warning_pri
   690     else if (Protocol.is_information(msg)) Rendering.information_pri
   691     else 0
   692 
   693   private lazy val gutter_message_content = Map(
   694     Rendering.information_pri ->
   695       (JEdit_Lib.load_icon(options.string("gutter_information_icon")), information_message_color),
   696     Rendering.warning_pri ->
   697       (JEdit_Lib.load_icon(options.string("gutter_warning_icon")), warning_message_color),
   698     Rendering.legacy_pri ->
   699       (JEdit_Lib.load_icon(options.string("gutter_legacy_icon")), warning_message_color),
   700     Rendering.error_pri ->
   701       (JEdit_Lib.load_icon(options.string("gutter_error_icon")), error_message_color))
   702 
   703   def gutter_content(range: Text.Range): Option[(Icon, Color)] =
   704   {
   705     val pris =
   706       snapshot.cumulate[Int](range, 0, Rendering.gutter_elements, _ =>
   707         {
   708           case (pri, Text.Info(_, msg @ XML.Elem(Markup(_, Markup.Serial(serial)), _))) =>
   709             Some(pri max gutter_message_pri(msg))
   710           case _ => None
   711         }).map(_.info)
   712 
   713     gutter_message_content.get((0 /: pris)(_ max _))
   714   }
   715 
   716 
   717   /* squiggly underline */
   718 
   719   private lazy val squiggly_colors = Map(
   720     Rendering.writeln_pri -> writeln_color,
   721     Rendering.information_pri -> information_color,
   722     Rendering.warning_pri -> warning_color,
   723     Rendering.legacy_pri -> legacy_color,
   724     Rendering.error_pri -> error_color)
   725 
   726   def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
   727   {
   728     val results =
   729       snapshot.cumulate[Int](range, 0, Rendering.squiggly_elements, _ =>
   730         {
   731           case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
   732         })
   733     for {
   734       Text.Info(r, pri) <- results
   735       color <- squiggly_colors.get(pri)
   736     } yield Text.Info(r, color)
   737   }
   738 
   739 
   740   /* message output */
   741 
   742   private lazy val message_colors = Map(
   743     Rendering.writeln_pri -> writeln_message_color,
   744     Rendering.information_pri -> information_message_color,
   745     Rendering.tracing_pri -> tracing_message_color,
   746     Rendering.warning_pri -> warning_message_color,
   747     Rendering.legacy_pri -> legacy_message_color,
   748     Rendering.error_pri -> error_message_color)
   749 
   750   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   751   {
   752     val results =
   753       snapshot.cumulate[Int](range, 0, Rendering.line_background_elements, _ =>
   754         {
   755           case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
   756         })
   757     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   758 
   759     message_colors.get(pri).map(message_color =>
   760       {
   761         val is_separator =
   762           snapshot.cumulate[Boolean](range, false, Rendering.separator_elements, _ =>
   763             {
   764               case _ => Some(true)
   765             }).exists(_.info)
   766         (message_color, is_separator)
   767       })
   768   }
   769 
   770   def output_messages(results: Command.Results): List[XML.Tree] =
   771   {
   772     val (states, other) =
   773       results.iterator.map(_._2).filterNot(Protocol.is_result(_)).toList
   774         .partition(Protocol.is_state(_))
   775     states ::: other
   776   }
   777 
   778 
   779   /* text background */
   780 
   781   def background(range: Text.Range, focus: Set[Long]): List[Text.Info[Color]] =
   782   {
   783     for {
   784       Text.Info(r, result) <-
   785         snapshot.cumulate[(List[Markup], Option[Color])](
   786           range, (List(Markup.Empty), None), Rendering.background_elements,
   787           command_states =>
   788             {
   789               case (((status, color), Text.Info(_, XML.Elem(markup, _))))
   790               if status.nonEmpty && Protocol.proper_status_elements(markup.name) =>
   791                 Some((markup :: status, color))
   792               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   793                 Some((Nil, Some(bad_color)))
   794               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   795                 Some((Nil, Some(intensify_color)))
   796               case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   797                 props match {
   798                   case Markup.Entity.Def(i) if focus(i) => Some((Nil, Some(entity_color)))
   799                   case Markup.Entity.Ref(i) if focus(i) => Some((Nil, Some(entity_color)))
   800                   case _ => None
   801                 }
   802               case (_, Text.Info(_, XML.Elem(Markup.Markdown_Item(depth), _))) =>
   803                 val color =
   804                   depth match {
   805                     case 1 => markdown_item_color1
   806                     case 2 => markdown_item_color2
   807                     case 3 => markdown_item_color3
   808                     case _ => markdown_item_color4
   809                   }
   810                 Some((Nil, Some(color)))
   811               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   812                 command_states.collectFirst(
   813                   { case st if st.results.defined(serial) => st.results.get(serial).get }) match
   814                 {
   815                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   816                     Some((Nil, Some(active_result_color)))
   817                   case _ =>
   818                     Some((Nil, Some(active_color)))
   819                 }
   820               case (_, Text.Info(_, elem)) =>
   821                 if (Rendering.active_elements(elem.name)) Some((Nil, Some(active_color)))
   822                 else None
   823             })
   824       color <-
   825         (result match {
   826           case (markups, opt_color) if markups.nonEmpty =>
   827             val status = Protocol.Status.make(markups.iterator)
   828             if (status.is_unprocessed) Some(unprocessed1_color)
   829             else if (status.is_running) Some(running1_color)
   830             else opt_color
   831           case (_, opt_color) => opt_color
   832         })
   833     } yield Text.Info(r, color)
   834   }
   835 
   836 
   837   /* text foreground */
   838 
   839   def foreground(range: Text.Range): List[Text.Info[Color]] =
   840     snapshot.select(range, Rendering.foreground_elements, _ =>
   841       {
   842         case Text.Info(_, elem) =>
   843           if (elem.name == Markup.ANTIQUOTED) Some(antiquoted_color) else Some(quoted_color)
   844       })
   845 
   846 
   847   /* text color */
   848 
   849   val foreground_color = jEdit.getColorProperty("view.fgColor")
   850 
   851   private lazy val text_colors: Map[String, Color] = Map(
   852       Markup.KEYWORD1 -> keyword1_color,
   853       Markup.KEYWORD2 -> keyword2_color,
   854       Markup.KEYWORD3 -> keyword3_color,
   855       Markup.QUASI_KEYWORD -> quasi_keyword_color,
   856       Markup.IMPROPER -> improper_color,
   857       Markup.OPERATOR -> operator_color,
   858       Markup.STRING -> foreground_color,
   859       Markup.ALT_STRING -> foreground_color,
   860       Markup.VERBATIM -> foreground_color,
   861       Markup.CARTOUCHE -> foreground_color,
   862       Markup.LITERAL -> keyword1_color,
   863       Markup.DELIMITER -> foreground_color,
   864       Markup.TFREE -> tfree_color,
   865       Markup.TVAR -> tvar_color,
   866       Markup.FREE -> free_color,
   867       Markup.SKOLEM -> skolem_color,
   868       Markup.BOUND -> bound_color,
   869       Markup.VAR -> var_color,
   870       Markup.INNER_STRING -> inner_quoted_color,
   871       Markup.INNER_CARTOUCHE -> inner_cartouche_color,
   872       Markup.INNER_COMMENT -> inner_comment_color,
   873       Markup.DYNAMIC_FACT -> dynamic_color,
   874       Markup.CLASS_PARAMETER -> class_parameter_color,
   875       Markup.ANTIQUOTE -> antiquote_color,
   876       Markup.ML_KEYWORD1 -> keyword1_color,
   877       Markup.ML_KEYWORD2 -> keyword2_color,
   878       Markup.ML_KEYWORD3 -> keyword3_color,
   879       Markup.ML_DELIMITER -> foreground_color,
   880       Markup.ML_NUMERAL -> inner_numeral_color,
   881       Markup.ML_CHAR -> inner_quoted_color,
   882       Markup.ML_STRING -> inner_quoted_color,
   883       Markup.ML_COMMENT -> inner_comment_color,
   884       Markup.SML_STRING -> inner_quoted_color,
   885       Markup.SML_COMMENT -> inner_comment_color)
   886 
   887   private lazy val text_color_elements =
   888     Markup.Elements(text_colors.keySet)
   889 
   890   def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
   891   {
   892     if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
   893     else
   894       snapshot.cumulate(range, color, text_color_elements, _ =>
   895         {
   896           case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
   897         })
   898   }
   899 
   900 
   901   /* virtual bullets */
   902 
   903   def bullet(range: Text.Range): List[Text.Info[Color]] =
   904     snapshot.select(range, Rendering.bullet_elements, _ =>
   905       {
   906         case Text.Info(_, Protocol.ML_Breakpoint(breakpoint)) =>
   907           Debugger.active_breakpoint_state(breakpoint).map(b =>
   908             if (b) breakpoint_enabled_color else breakpoint_disabled_color)
   909         case _ => Some(bullet_color)
   910       })
   911 
   912 
   913   /* text folds */
   914 
   915   def fold_depth(range: Text.Range): List[Text.Info[Int]] =
   916     snapshot.cumulate[Int](range, 0, Rendering.fold_depth_elements, _ =>
   917       {
   918         case (depth, _) => Some(depth + 1)
   919       })
   920 }