src/Tools/jEdit/src/jedit_rendering.scala
author wenzelm
Fri Dec 23 11:21:38 2016 +0100 (2016-12-23)
changeset 64660 ef85bb6491b3
parent 64654 31b681e38c70
child 64676 fd2df1ea3bb4
permissions -rw-r--r--
omit unused markup;
     1 /*  Title:      Tools/jEdit/src/jedit_rendering.scala
     2     Author:     Makarius
     3 
     4 Isabelle/jEdit-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 JEdit_Rendering
    23 {
    24   def apply(snapshot: Document.Snapshot, options: Options): JEdit_Rendering =
    25     new JEdit_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.kinds.getOrElse(token.content, ""))
    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 indentation_elements =
   141     Markup.Elements(Markup.Command_Indent.name)
   142 
   143   private val semantic_completion_elements =
   144     Markup.Elements(Markup.COMPLETION, Markup.NO_COMPLETION)
   145 
   146   private val language_context_elements =
   147     Markup.Elements(Markup.STRING, Markup.ALT_STRING, Markup.VERBATIM,
   148       Markup.CARTOUCHE, Markup.COMMENT, Markup.LANGUAGE,
   149       Markup.ML_STRING, Markup.ML_COMMENT)
   150 
   151   private val language_elements = Markup.Elements(Markup.LANGUAGE)
   152 
   153   private val citation_elements = Markup.Elements(Markup.CITATION)
   154 
   155   private val breakpoint_elements = Markup.Elements(Markup.ML_BREAKPOINT)
   156 
   157   private val caret_focus_elements = Markup.Elements(Markup.ENTITY)
   158 
   159   private val highlight_elements =
   160     Markup.Elements(Markup.EXPRESSION, Markup.CITATION, Markup.LANGUAGE, Markup.ML_TYPING,
   161       Markup.TOKEN_RANGE, Markup.ENTITY, Markup.PATH, Markup.DOC, Markup.URL, Markup.SORTING,
   162       Markup.TYPING, Markup.CLASS_PARAMETER, Markup.FREE, Markup.SKOLEM, Markup.BOUND,
   163       Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_BREAKPOINT,
   164       Markup.MARKDOWN_PARAGRAPH, Markup.Markdown_List.name)
   165 
   166   private val hyperlink_elements =
   167     Markup.Elements(Markup.ENTITY, Markup.PATH, Markup.DOC, Markup.POSITION,
   168       Markup.CITATION, Markup.URL)
   169 
   170   private val active_elements =
   171     Markup.Elements(Markup.DIALOG, Markup.BROWSER, Markup.GRAPHVIEW,
   172       Markup.SENDBACK, Markup.JEDIT_ACTION, Markup.SIMP_TRACE_PANEL)
   173 
   174   private val tooltip_message_elements =
   175     Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR,
   176       Markup.BAD)
   177 
   178   private val gutter_elements =
   179     Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR)
   180 
   181   private val squiggly_elements =
   182     Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR)
   183 
   184   private val line_background_elements =
   185     Markup.Elements(Markup.WRITELN_MESSAGE, Markup.STATE_MESSAGE, Markup.INFORMATION_MESSAGE,
   186       Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE, Markup.LEGACY_MESSAGE,
   187       Markup.ERROR_MESSAGE)
   188 
   189   private val separator_elements =
   190     Markup.Elements(Markup.SEPARATOR)
   191 
   192   private val background_elements =
   193     Protocol.proper_status_elements + Markup.WRITELN_MESSAGE +
   194       Markup.STATE_MESSAGE + Markup.INFORMATION_MESSAGE +
   195       Markup.TRACING_MESSAGE + Markup.WARNING_MESSAGE +
   196       Markup.LEGACY_MESSAGE + Markup.ERROR_MESSAGE +
   197       Markup.BAD + Markup.INTENSIFY + Markup.ENTITY +
   198       Markup.Markdown_Item.name ++ active_elements
   199 
   200   private val foreground_elements =
   201     Markup.Elements(Markup.STRING, Markup.ALT_STRING, Markup.VERBATIM,
   202       Markup.CARTOUCHE, Markup.ANTIQUOTED)
   203 
   204   private val bullet_elements =
   205     Markup.Elements(Markup.BULLET, Markup.ML_BREAKPOINT)
   206 
   207   private val fold_depth_elements =
   208     Markup.Elements(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   209 }
   210 
   211 
   212 class JEdit_Rendering(snapshot: Document.Snapshot, options: Options)
   213   extends Rendering(snapshot, options, PIDE.resources)
   214 {
   215   /* colors */
   216 
   217   def color_value(s: String): Color = Color_Value(options.string(s))
   218 
   219   val outdated_color = color_value("outdated_color")
   220   val unprocessed_color = color_value("unprocessed_color")
   221   val unprocessed1_color = color_value("unprocessed1_color")
   222   val running_color = color_value("running_color")
   223   val running1_color = color_value("running1_color")
   224   val bullet_color = color_value("bullet_color")
   225   val tooltip_color = color_value("tooltip_color")
   226   val writeln_color = color_value("writeln_color")
   227   val information_color = color_value("information_color")
   228   val warning_color = color_value("warning_color")
   229   val legacy_color = color_value("legacy_color")
   230   val error_color = color_value("error_color")
   231   val writeln_message_color = color_value("writeln_message_color")
   232   val information_message_color = color_value("information_message_color")
   233   val tracing_message_color = color_value("tracing_message_color")
   234   val warning_message_color = color_value("warning_message_color")
   235   val legacy_message_color = color_value("legacy_message_color")
   236   val error_message_color = color_value("error_message_color")
   237   val spell_checker_color = color_value("spell_checker_color")
   238   val bad_color = color_value("bad_color")
   239   val intensify_color = color_value("intensify_color")
   240   val entity_color = color_value("entity_color")
   241   val entity_ref_color = color_value("entity_ref_color")
   242   val breakpoint_disabled_color = color_value("breakpoint_disabled_color")
   243   val breakpoint_enabled_color = color_value("breakpoint_enabled_color")
   244   val caret_debugger_color = color_value("caret_debugger_color")
   245   val quoted_color = color_value("quoted_color")
   246   val antiquoted_color = color_value("antiquoted_color")
   247   val antiquote_color = color_value("antiquote_color")
   248   val highlight_color = color_value("highlight_color")
   249   val hyperlink_color = color_value("hyperlink_color")
   250   val active_color = color_value("active_color")
   251   val active_hover_color = color_value("active_hover_color")
   252   val active_result_color = color_value("active_result_color")
   253   val keyword1_color = color_value("keyword1_color")
   254   val keyword2_color = color_value("keyword2_color")
   255   val keyword3_color = color_value("keyword3_color")
   256   val quasi_keyword_color = color_value("quasi_keyword_color")
   257   val improper_color = color_value("improper_color")
   258   val operator_color = color_value("operator_color")
   259   val caret_invisible_color = color_value("caret_invisible_color")
   260   val completion_color = color_value("completion_color")
   261   val search_color = color_value("search_color")
   262 
   263   val tfree_color = color_value("tfree_color")
   264   val tvar_color = color_value("tvar_color")
   265   val free_color = color_value("free_color")
   266   val skolem_color = color_value("skolem_color")
   267   val bound_color = color_value("bound_color")
   268   val var_color = color_value("var_color")
   269   val inner_numeral_color = color_value("inner_numeral_color")
   270   val inner_quoted_color = color_value("inner_quoted_color")
   271   val inner_cartouche_color = color_value("inner_cartouche_color")
   272   val inner_comment_color = color_value("inner_comment_color")
   273   val dynamic_color = color_value("dynamic_color")
   274   val class_parameter_color = color_value("class_parameter_color")
   275 
   276   val markdown_item_color1 = color_value("markdown_item_color1")
   277   val markdown_item_color2 = color_value("markdown_item_color2")
   278   val markdown_item_color3 = color_value("markdown_item_color3")
   279   val markdown_item_color4 = color_value("markdown_item_color4")
   280 
   281 
   282   /* indentation */
   283 
   284   def indentation(range: Text.Range): Int =
   285     snapshot.select(range, JEdit_Rendering.indentation_elements, _ =>
   286       {
   287         case Text.Info(_, XML.Elem(Markup.Command_Indent(i), _)) => Some(i)
   288         case _ => None
   289       }).headOption.map(_.info).getOrElse(0)
   290 
   291 
   292   /* completion */
   293 
   294   def semantic_completion(completed_range: Option[Text.Range], range: Text.Range)
   295       : Option[Text.Info[Completion.Semantic]] =
   296     if (snapshot.is_outdated) None
   297     else {
   298       snapshot.select(range, JEdit_Rendering.semantic_completion_elements, _ =>
   299         {
   300           case Completion.Semantic.Info(info) =>
   301             completed_range match {
   302               case Some(range0) if range0.contains(info.range) && range0 != info.range => None
   303               case _ => Some(info)
   304             }
   305           case _ => None
   306         }).headOption.map(_.info)
   307     }
   308 
   309   def language_context(range: Text.Range): Option[Completion.Language_Context] =
   310     snapshot.select(range, JEdit_Rendering.language_context_elements, _ =>
   311       {
   312         case Text.Info(_, XML.Elem(Markup.Language(language, symbols, antiquotes, delimited), _)) =>
   313           if (delimited) Some(Completion.Language_Context(language, symbols, antiquotes))
   314           else None
   315         case Text.Info(_, elem)
   316         if elem.name == Markup.ML_STRING || elem.name == Markup.ML_COMMENT =>
   317           Some(Completion.Language_Context.ML_inner)
   318         case Text.Info(_, _) =>
   319           Some(Completion.Language_Context.inner)
   320       }).headOption.map(_.info)
   321 
   322   def language_path(range: Text.Range): Option[Text.Range] =
   323     snapshot.select(range, JEdit_Rendering.language_elements, _ =>
   324       {
   325         case Text.Info(info_range, XML.Elem(Markup.Language(Markup.Language.PATH, _, _, _), _)) =>
   326           Some(snapshot.convert(info_range))
   327         case _ => None
   328       }).headOption.map(_.info)
   329 
   330   def citation(range: Text.Range): Option[Text.Info[String]] =
   331     snapshot.select(range, JEdit_Rendering.citation_elements, _ =>
   332       {
   333         case Text.Info(info_range, XML.Elem(Markup.Citation(name), _)) =>
   334           Some(Text.Info(snapshot.convert(info_range), name))
   335         case _ => None
   336       }).headOption.map(_.info)
   337 
   338 
   339   /* spell checker */
   340 
   341   private lazy val spell_checker_elements =
   342     Markup.Elements(space_explode(',', options.string("spell_checker_elements")): _*)
   343 
   344   def spell_checker_ranges(range: Text.Range): List[Text.Range] =
   345     snapshot.select(range, spell_checker_elements, _ => _ => Some(())).map(_.range)
   346 
   347   def spell_checker_point(range: Text.Range): Option[Text.Range] =
   348     snapshot.select(range, spell_checker_elements, _ =>
   349       {
   350         case info => Some(snapshot.convert(info.range))
   351       }).headOption.map(_.info)
   352 
   353 
   354   /* breakpoints */
   355 
   356   def breakpoint(range: Text.Range): Option[(Command, Long)] =
   357     if (snapshot.is_outdated) None
   358     else
   359       snapshot.select(range, JEdit_Rendering.breakpoint_elements, command_states =>
   360         {
   361           case Text.Info(_, Protocol.ML_Breakpoint(breakpoint)) =>
   362             command_states match {
   363               case st :: _ => Some((st.command, breakpoint))
   364               case _ => None
   365             }
   366           case _ => None
   367         }).headOption.map(_.info)
   368 
   369 
   370   /* command status overview */
   371 
   372   def overview_color(range: Text.Range): Option[Color] =
   373   {
   374     if (snapshot.is_outdated) None
   375     else {
   376       val results =
   377         snapshot.cumulate[List[Markup]](range, Nil, Protocol.liberal_status_elements, _ =>
   378           {
   379             case (status, Text.Info(_, elem)) => Some(elem.markup :: status)
   380           }, status = true)
   381       if (results.isEmpty) None
   382       else {
   383         val status = Protocol.Status.make(results.iterator.flatMap(_.info))
   384 
   385         if (status.is_running) Some(running_color)
   386         else if (status.is_failed) Some(error_color)
   387         else if (status.is_warned) Some(warning_color)
   388         else if (status.is_unprocessed) Some(unprocessed_color)
   389         else None
   390       }
   391     }
   392   }
   393 
   394 
   395   /* caret focus */
   396 
   397   def entity_focus(range: Text.Range,
   398     check: (Boolean, Long) => Boolean = (is_def: Boolean, i: Long) => is_def): Set[Long] =
   399   {
   400     val results =
   401       snapshot.cumulate[Set[Long]](range, Set.empty, JEdit_Rendering.caret_focus_elements, _ =>
   402           {
   403             case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   404               props match {
   405                 case Markup.Entity.Def(i) if check(true, i) => Some(serials + i)
   406                 case Markup.Entity.Ref(i) if check(false, i) => Some(serials + i)
   407                 case _ => None
   408               }
   409             case _ => None
   410           })
   411     (Set.empty[Long] /: results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
   412   }
   413 
   414   def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
   415   {
   416     val focus_defs = entity_focus(caret_range)
   417     if (focus_defs.nonEmpty) focus_defs
   418     else {
   419       val visible_defs = entity_focus(visible_range)
   420       entity_focus(caret_range, (is_def: Boolean, i: Long) => !is_def && visible_defs.contains(i))
   421     }
   422   }
   423 
   424   def caret_focus_ranges(caret_range: Text.Range, visible_range: Text.Range): List[Text.Range] =
   425   {
   426     val focus = caret_focus(caret_range, visible_range)
   427     if (focus.nonEmpty) {
   428       val results =
   429         snapshot.cumulate[Boolean](visible_range, false, JEdit_Rendering.caret_focus_elements, _ =>
   430           {
   431             case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   432               props match {
   433                 case Markup.Entity.Def(i) if focus(i) => Some(true)
   434                 case Markup.Entity.Ref(i) if focus(i) => Some(true)
   435                 case _ => None
   436               }
   437           })
   438       for (info <- results if info.info) yield info.range
   439     }
   440     else Nil
   441   }
   442 
   443   def entity_ref(range: Text.Range, focus: Set[Long]): List[Text.Info[Color]] =
   444     snapshot.select(range, JEdit_Rendering.caret_focus_elements, _ =>
   445       {
   446         case Text.Info(_, XML.Elem(Markup(Markup.ENTITY, Markup.Entity.Ref(i)), _)) if focus(i) =>
   447           Some(entity_ref_color)
   448         case _ => None
   449       })
   450 
   451 
   452   /* highlighted area */
   453 
   454   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   455     snapshot.select(range, JEdit_Rendering.highlight_elements, _ =>
   456       {
   457         case info => Some(Text.Info(snapshot.convert(info.range), highlight_color))
   458       }).headOption.map(_.info)
   459 
   460 
   461   /* hyperlinks */
   462 
   463   def hyperlink(range: Text.Range): Option[Text.Info[PIDE.editor.Hyperlink]] =
   464   {
   465     snapshot.cumulate[Vector[Text.Info[PIDE.editor.Hyperlink]]](
   466       range, Vector.empty, JEdit_Rendering.hyperlink_elements, _ =>
   467         {
   468           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _))) =>
   469             val file = resources.append_file(snapshot.node_name.master_dir, name)
   470             val link = PIDE.editor.hyperlink_file(true, file)
   471             Some(links :+ Text.Info(snapshot.convert(info_range), link))
   472 
   473           case (links, Text.Info(info_range, XML.Elem(Markup.Doc(name), _))) =>
   474             PIDE.editor.hyperlink_doc(name).map(link =>
   475               (links :+ Text.Info(snapshot.convert(info_range), link)))
   476 
   477           case (links, Text.Info(info_range, XML.Elem(Markup.Url(name), _))) =>
   478             val link = PIDE.editor.hyperlink_url(name)
   479             Some(links :+ Text.Info(snapshot.convert(info_range), link))
   480 
   481           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   482             val opt_link = PIDE.editor.hyperlink_def_position(true, snapshot, props)
   483             opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
   484 
   485           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
   486             val opt_link = PIDE.editor.hyperlink_position(true, snapshot, props)
   487             opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
   488 
   489           case (links, Text.Info(info_range, XML.Elem(Markup.Citation(name), _))) =>
   490             val opt_link =
   491               Bibtex_JEdit.entries_iterator.collectFirst(
   492                 { case (a, buffer, offset) if a == name =>
   493                     PIDE.editor.hyperlink_buffer(true, buffer, offset) })
   494             opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
   495 
   496           case _ => None
   497         }) match { case Text.Info(_, _ :+ info) :: _ => Some(info) case _ => None }
   498   }
   499 
   500 
   501   /* active elements */
   502 
   503   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   504     snapshot.select(range, JEdit_Rendering.active_elements, command_states =>
   505       {
   506         case Text.Info(info_range, elem) =>
   507           if (elem.name == Markup.DIALOG) {
   508             elem match {
   509               case Protocol.Dialog(_, serial, _)
   510               if !command_states.exists(st => st.results.defined(serial)) =>
   511                 Some(Text.Info(snapshot.convert(info_range), elem))
   512               case _ => None
   513             }
   514           }
   515           else Some(Text.Info(snapshot.convert(info_range), elem))
   516       }).headOption.map(_.info)
   517 
   518   def command_results(range: Text.Range): Command.Results =
   519     Command.State.merge_results(
   520       snapshot.select[List[Command.State]](range, Markup.Elements.full, command_states =>
   521         { case _ => Some(command_states) }).flatMap(_.info))
   522 
   523 
   524   /* tooltip messages */
   525 
   526   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   527   {
   528     val results =
   529       snapshot.cumulate[List[Text.Info[Command.Results.Entry]]](
   530         range, Nil, JEdit_Rendering.tooltip_message_elements, _ =>
   531         {
   532           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   533           if body.nonEmpty =>
   534             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   535             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   536 
   537           case (msgs, Text.Info(info_range,
   538             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body))) =>
   539             val entry: Command.Results.Entry =
   540               serial -> XML.Elem(Markup(Markup.message(name), props), body)
   541             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   542 
   543           case _ => None
   544         }).flatMap(_.info)
   545     if (results.isEmpty) None
   546     else {
   547       val r = Text.Range(results.head.range.start, results.last.range.stop)
   548       val msgs = Command.Results.make(results.map(_.info))
   549       Some(Text.Info(r, Pretty.separate(msgs.iterator.map(_._2).toList)))
   550     }
   551   }
   552 
   553 
   554   /* tooltips */
   555 
   556   def tooltip_margin: Int = options.int("jedit_tooltip_margin")
   557   def timing_threshold: Double = options.real("jedit_timing_threshold")
   558 
   559   lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
   560   lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
   561 
   562 
   563   /* gutter */
   564 
   565   private def gutter_message_pri(msg: XML.Tree): Int =
   566     if (Protocol.is_error(msg)) JEdit_Rendering.error_pri
   567     else if (Protocol.is_legacy(msg)) JEdit_Rendering.legacy_pri
   568     else if (Protocol.is_warning(msg)) JEdit_Rendering.warning_pri
   569     else if (Protocol.is_information(msg)) JEdit_Rendering.information_pri
   570     else 0
   571 
   572   private lazy val gutter_message_content = Map(
   573     JEdit_Rendering.information_pri ->
   574       (JEdit_Lib.load_icon(options.string("gutter_information_icon")), information_message_color),
   575     JEdit_Rendering.warning_pri ->
   576       (JEdit_Lib.load_icon(options.string("gutter_warning_icon")), warning_message_color),
   577     JEdit_Rendering.legacy_pri ->
   578       (JEdit_Lib.load_icon(options.string("gutter_legacy_icon")), warning_message_color),
   579     JEdit_Rendering.error_pri ->
   580       (JEdit_Lib.load_icon(options.string("gutter_error_icon")), error_message_color))
   581 
   582   def gutter_content(range: Text.Range): Option[(Icon, Color)] =
   583   {
   584     val pris =
   585       snapshot.cumulate[Int](range, 0, JEdit_Rendering.gutter_elements, _ =>
   586         {
   587           case (pri, Text.Info(_, msg @ XML.Elem(Markup(_, Markup.Serial(serial)), _))) =>
   588             Some(pri max gutter_message_pri(msg))
   589           case _ => None
   590         }).map(_.info)
   591 
   592     gutter_message_content.get((0 /: pris)(_ max _))
   593   }
   594 
   595 
   596   /* squiggly underline */
   597 
   598   private lazy val squiggly_colors = Map(
   599     JEdit_Rendering.writeln_pri -> writeln_color,
   600     JEdit_Rendering.information_pri -> information_color,
   601     JEdit_Rendering.warning_pri -> warning_color,
   602     JEdit_Rendering.legacy_pri -> legacy_color,
   603     JEdit_Rendering.error_pri -> error_color)
   604 
   605   def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
   606   {
   607     val results =
   608       snapshot.cumulate[Int](range, 0, JEdit_Rendering.squiggly_elements, _ =>
   609         {
   610           case (pri, Text.Info(_, elem)) => Some(pri max JEdit_Rendering.message_pri(elem.name))
   611         })
   612     for {
   613       Text.Info(r, pri) <- results
   614       color <- squiggly_colors.get(pri)
   615     } yield Text.Info(r, color)
   616   }
   617 
   618 
   619   /* message output */
   620 
   621   private lazy val message_colors = Map(
   622     JEdit_Rendering.writeln_pri -> writeln_message_color,
   623     JEdit_Rendering.information_pri -> information_message_color,
   624     JEdit_Rendering.tracing_pri -> tracing_message_color,
   625     JEdit_Rendering.warning_pri -> warning_message_color,
   626     JEdit_Rendering.legacy_pri -> legacy_message_color,
   627     JEdit_Rendering.error_pri -> error_message_color)
   628 
   629   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   630   {
   631     val results =
   632       snapshot.cumulate[Int](range, 0, JEdit_Rendering.line_background_elements, _ =>
   633         {
   634           case (pri, Text.Info(_, elem)) => Some(pri max JEdit_Rendering.message_pri(elem.name))
   635         })
   636     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   637 
   638     message_colors.get(pri).map(message_color =>
   639       {
   640         val is_separator =
   641           snapshot.cumulate[Boolean](range, false, JEdit_Rendering.separator_elements, _ =>
   642             {
   643               case _ => Some(true)
   644             }).exists(_.info)
   645         (message_color, is_separator)
   646       })
   647   }
   648 
   649   def output_messages(results: Command.Results): List[XML.Tree] =
   650   {
   651     val (states, other) =
   652       results.iterator.map(_._2).filterNot(Protocol.is_result(_)).toList
   653         .partition(Protocol.is_state(_))
   654     states ::: other
   655   }
   656 
   657 
   658   /* text background */
   659 
   660   def background(range: Text.Range, focus: Set[Long]): List[Text.Info[Color]] =
   661   {
   662     for {
   663       Text.Info(r, result) <-
   664         snapshot.cumulate[(List[Markup], Option[Color])](
   665           range, (List(Markup.Empty), None), JEdit_Rendering.background_elements,
   666           command_states =>
   667             {
   668               case (((status, color), Text.Info(_, XML.Elem(markup, _))))
   669               if status.nonEmpty && Protocol.proper_status_elements(markup.name) =>
   670                 Some((markup :: status, color))
   671               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   672                 Some((Nil, Some(bad_color)))
   673               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   674                 Some((Nil, Some(intensify_color)))
   675               case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   676                 props match {
   677                   case Markup.Entity.Def(i) if focus(i) => Some((Nil, Some(entity_color)))
   678                   case Markup.Entity.Ref(i) if focus(i) => Some((Nil, Some(entity_color)))
   679                   case _ => None
   680                 }
   681               case (_, Text.Info(_, XML.Elem(Markup.Markdown_Item(depth), _))) =>
   682                 val color =
   683                   depth match {
   684                     case 1 => markdown_item_color1
   685                     case 2 => markdown_item_color2
   686                     case 3 => markdown_item_color3
   687                     case _ => markdown_item_color4
   688                   }
   689                 Some((Nil, Some(color)))
   690               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   691                 command_states.collectFirst(
   692                   { case st if st.results.defined(serial) => st.results.get(serial).get }) match
   693                 {
   694                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   695                     Some((Nil, Some(active_result_color)))
   696                   case _ =>
   697                     Some((Nil, Some(active_color)))
   698                 }
   699               case (_, Text.Info(_, elem)) =>
   700                 if (JEdit_Rendering.active_elements(elem.name)) Some((Nil, Some(active_color)))
   701                 else None
   702             })
   703       color <-
   704         (result match {
   705           case (markups, opt_color) if markups.nonEmpty =>
   706             val status = Protocol.Status.make(markups.iterator)
   707             if (status.is_unprocessed) Some(unprocessed1_color)
   708             else if (status.is_running) Some(running1_color)
   709             else opt_color
   710           case (_, opt_color) => opt_color
   711         })
   712     } yield Text.Info(r, color)
   713   }
   714 
   715 
   716   /* text foreground */
   717 
   718   def foreground(range: Text.Range): List[Text.Info[Color]] =
   719     snapshot.select(range, JEdit_Rendering.foreground_elements, _ =>
   720       {
   721         case Text.Info(_, elem) =>
   722           if (elem.name == Markup.ANTIQUOTED) Some(antiquoted_color) else Some(quoted_color)
   723       })
   724 
   725 
   726   /* text color */
   727 
   728   val foreground_color = jEdit.getColorProperty("view.fgColor")
   729 
   730   private lazy val text_colors: Map[String, Color] = Map(
   731       Markup.KEYWORD1 -> keyword1_color,
   732       Markup.KEYWORD2 -> keyword2_color,
   733       Markup.KEYWORD3 -> keyword3_color,
   734       Markup.QUASI_KEYWORD -> quasi_keyword_color,
   735       Markup.IMPROPER -> improper_color,
   736       Markup.OPERATOR -> operator_color,
   737       Markup.STRING -> foreground_color,
   738       Markup.ALT_STRING -> foreground_color,
   739       Markup.VERBATIM -> foreground_color,
   740       Markup.CARTOUCHE -> foreground_color,
   741       Markup.LITERAL -> keyword1_color,
   742       Markup.DELIMITER -> foreground_color,
   743       Markup.TFREE -> tfree_color,
   744       Markup.TVAR -> tvar_color,
   745       Markup.FREE -> free_color,
   746       Markup.SKOLEM -> skolem_color,
   747       Markup.BOUND -> bound_color,
   748       Markup.VAR -> var_color,
   749       Markup.INNER_STRING -> inner_quoted_color,
   750       Markup.INNER_CARTOUCHE -> inner_cartouche_color,
   751       Markup.INNER_COMMENT -> inner_comment_color,
   752       Markup.DYNAMIC_FACT -> dynamic_color,
   753       Markup.CLASS_PARAMETER -> class_parameter_color,
   754       Markup.ANTIQUOTE -> antiquote_color,
   755       Markup.ML_KEYWORD1 -> keyword1_color,
   756       Markup.ML_KEYWORD2 -> keyword2_color,
   757       Markup.ML_KEYWORD3 -> keyword3_color,
   758       Markup.ML_DELIMITER -> foreground_color,
   759       Markup.ML_NUMERAL -> inner_numeral_color,
   760       Markup.ML_CHAR -> inner_quoted_color,
   761       Markup.ML_STRING -> inner_quoted_color,
   762       Markup.ML_COMMENT -> inner_comment_color,
   763       Markup.SML_STRING -> inner_quoted_color,
   764       Markup.SML_COMMENT -> inner_comment_color)
   765 
   766   private lazy val text_color_elements =
   767     Markup.Elements(text_colors.keySet)
   768 
   769   def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
   770   {
   771     if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
   772     else
   773       snapshot.cumulate(range, color, text_color_elements, _ =>
   774         {
   775           case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
   776         })
   777   }
   778 
   779 
   780   /* virtual bullets */
   781 
   782   def bullet(range: Text.Range): List[Text.Info[Color]] =
   783     snapshot.select(range, JEdit_Rendering.bullet_elements, _ =>
   784       {
   785         case Text.Info(_, Protocol.ML_Breakpoint(breakpoint)) =>
   786           Debugger.active_breakpoint_state(breakpoint).map(b =>
   787             if (b) breakpoint_enabled_color else breakpoint_disabled_color)
   788         case _ => Some(bullet_color)
   789       })
   790 
   791 
   792   /* text folds */
   793 
   794   def fold_depth(range: Text.Range): List[Text.Info[Int]] =
   795     snapshot.cumulate[Int](range, 0, JEdit_Rendering.fold_depth_elements, _ =>
   796       {
   797         case (depth, _) => Some(depth + 1)
   798       })
   799 }