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