src/Tools/jEdit/src/rendering.scala
author wenzelm
Tue Nov 19 12:57:56 2013 +0100 (2013-11-19 ago)
changeset 54515 570ba266f5b5
parent 53571 e58ca0311c0f
child 54702 3daeba5130f0
permissions -rw-r--r--
clarified boundary cases of Document.Node.Name;
     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   /* token markup -- text styles */
    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.SPACE -> NULL,
   103       Token.Kind.COMMENT -> COMMENT1,
   104       Token.Kind.ERROR -> INVALID
   105     ).withDefaultValue(NULL)
   106   }
   107 
   108   def token_markup(syntax: Outer_Syntax, token: Token): Byte =
   109     if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
   110     else if (token.is_operator) JEditToken.OPERATOR
   111     else token_style(token.kind)
   112 }
   113 
   114 
   115 class Rendering private(val snapshot: Document.Snapshot, val options: Options)
   116 {
   117   /* colors */
   118 
   119   def color_value(s: String): Color = Color_Value(options.string(s))
   120 
   121   val outdated_color = color_value("outdated_color")
   122   val unprocessed_color = color_value("unprocessed_color")
   123   val unprocessed1_color = color_value("unprocessed1_color")
   124   val running_color = color_value("running_color")
   125   val running1_color = color_value("running1_color")
   126   val light_color = color_value("light_color")
   127   val bullet_color = color_value("bullet_color")
   128   val tooltip_color = color_value("tooltip_color")
   129   val writeln_color = color_value("writeln_color")
   130   val information_color = color_value("information_color")
   131   val warning_color = color_value("warning_color")
   132   val error_color = color_value("error_color")
   133   val error1_color = color_value("error1_color")
   134   val writeln_message_color = color_value("writeln_message_color")
   135   val information_message_color = color_value("information_message_color")
   136   val tracing_message_color = color_value("tracing_message_color")
   137   val warning_message_color = color_value("warning_message_color")
   138   val error_message_color = color_value("error_message_color")
   139   val bad_color = color_value("bad_color")
   140   val intensify_color = color_value("intensify_color")
   141   val quoted_color = color_value("quoted_color")
   142   val antiquoted_color = color_value("antiquoted_color")
   143   val highlight_color = color_value("highlight_color")
   144   val hyperlink_color = color_value("hyperlink_color")
   145   val active_color = color_value("active_color")
   146   val active_hover_color = color_value("active_hover_color")
   147   val active_result_color = color_value("active_result_color")
   148   val keyword1_color = color_value("keyword1_color")
   149   val keyword2_color = color_value("keyword2_color")
   150 
   151   val tfree_color = color_value("tfree_color")
   152   val tvar_color = color_value("tvar_color")
   153   val free_color = color_value("free_color")
   154   val skolem_color = color_value("skolem_color")
   155   val bound_color = color_value("bound_color")
   156   val var_color = color_value("var_color")
   157   val inner_numeral_color = color_value("inner_numeral_color")
   158   val inner_quoted_color = color_value("inner_quoted_color")
   159   val inner_comment_color = color_value("inner_comment_color")
   160   val dynamic_color = color_value("dynamic_color")
   161 
   162 
   163   /* command overview */
   164 
   165   val overview_limit = options.int("jedit_text_overview_limit")
   166 
   167   private val overview_include = Protocol.command_status_markup + Markup.WARNING + Markup.ERROR
   168 
   169   def overview_color(range: Text.Range): Option[Color] =
   170   {
   171     if (snapshot.is_outdated) None
   172     else {
   173       val results =
   174         snapshot.cumulate_markup[(Protocol.Status, Int)](
   175           range, (Protocol.Status.init, 0), Some(overview_include), _ =>
   176           {
   177             case ((status, pri), Text.Info(_, elem)) =>
   178               if (elem.name == Markup.WARNING || elem.name == Markup.ERROR)
   179                 Some((status, pri max Rendering.message_pri(elem.name)))
   180               else if (overview_include(elem.name))
   181                 Some((Protocol.command_status(status, elem.markup), pri))
   182               else None
   183           })
   184       if (results.isEmpty) None
   185       else {
   186         val (status, pri) =
   187           ((Protocol.Status.init, 0) /: results) {
   188             case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
   189 
   190         if (status.is_running) Some(running_color)
   191         else if (pri == Rendering.warning_pri) Some(warning_color)
   192         else if (pri == Rendering.error_pri) Some(error_color)
   193         else if (status.is_unprocessed) Some(unprocessed_color)
   194         else if (status.is_failed) Some(error_color)
   195         else None
   196       }
   197     }
   198   }
   199 
   200 
   201   /* markup selectors */
   202 
   203   private val highlight_include =
   204     Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   205       Markup.ENTITY, Markup.PATH, Markup.SORTING, Markup.TYPING, Markup.FREE, Markup.SKOLEM,
   206       Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOCUMENT_SOURCE)
   207 
   208   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   209   {
   210     snapshot.select_markup(range, Some(highlight_include), _ =>
   211         {
   212           case Text.Info(info_range, elem) =>
   213             if (highlight_include(elem.name))
   214               Some(Text.Info(snapshot.convert(info_range), highlight_color))
   215             else None
   216         }) match { case Text.Info(_, info) :: _ => Some(info) case _ => None }
   217   }
   218 
   219 
   220   private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH)
   221 
   222   def hyperlink(range: Text.Range): Option[Text.Info[PIDE.editor.Hyperlink]] =
   223   {
   224     snapshot.cumulate_markup[List[Text.Info[PIDE.editor.Hyperlink]]](
   225       range, Nil, Some(hyperlink_include), _ =>
   226         {
   227           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   228           if Path.is_ok(name) =>
   229             val jedit_file = PIDE.thy_load.append(snapshot.node_name.master_dir, Path.explode(name))
   230             val link = PIDE.editor.hyperlink_file(jedit_file)
   231             Some(Text.Info(snapshot.convert(info_range), link) :: links)
   232 
   233           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   234           if !props.exists(
   235             { case (Markup.KIND, Markup.ML_OPEN) => true
   236               case (Markup.KIND, Markup.ML_STRUCT) => true
   237               case _ => false }) =>
   238 
   239             props match {
   240               case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
   241                 Isabelle_System.source_file(Path.explode(name)) match {
   242                   case Some(path) =>
   243                     val jedit_file = Isabelle_System.platform_path(path)
   244                     val link = PIDE.editor.hyperlink_file(jedit_file, line)
   245                     Some(Text.Info(snapshot.convert(info_range), link) :: links)
   246                   case None => None
   247                 }
   248 
   249               case Position.Def_Id_Offset(id, offset) =>
   250                 snapshot.state.find_command(snapshot.version, id) match {
   251                   case Some((node, command)) =>
   252                     PIDE.editor.hyperlink_command(snapshot, command, offset)
   253                       .map(link => (Text.Info(snapshot.convert(info_range), link) :: links))
   254                   case None => None
   255                 }
   256 
   257               case _ => None
   258             }
   259 
   260           case _ => None
   261         }) match { case Text.Info(_, info :: _) :: _ => Some(info) case _ => None }
   262   }
   263 
   264 
   265   private val active_include =
   266     Set(Markup.BROWSER, Markup.GRAPHVIEW, Markup.SENDBACK, Markup.DIALOG)
   267 
   268   def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
   269     snapshot.select_markup(range, Some(active_include), command_state =>
   270         {
   271           case Text.Info(info_range, elem @ Protocol.Dialog(_, serial, _))
   272           if !command_state.results.defined(serial) =>
   273             Some(Text.Info(snapshot.convert(info_range), elem))
   274           case Text.Info(info_range, elem) =>
   275             if (elem.name == Markup.BROWSER ||
   276                 elem.name == Markup.GRAPHVIEW ||
   277                 elem.name == Markup.SENDBACK)
   278               Some(Text.Info(snapshot.convert(info_range), elem))
   279             else None
   280         }) match { case Text.Info(_, info) :: _ => Some(info) case _ => None }
   281 
   282 
   283   def command_results(range: Text.Range): Command.Results =
   284   {
   285     val results =
   286       snapshot.select_markup[Command.Results](range, None, command_state =>
   287         { case _ => Some(command_state.results) }).map(_.info)
   288     (Command.Results.empty /: results)(_ ++ _)
   289   }
   290 
   291   def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
   292   {
   293     val results =
   294       snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
   295         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
   296         {
   297           case (msgs, Text.Info(info_range,
   298             XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   299           if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
   300             val entry: Command.Results.Entry =
   301               (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   302             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   303 
   304           case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   305           if !body.isEmpty =>
   306             val entry: Command.Results.Entry = (Document_ID.make(), msg)
   307             Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
   308 
   309           case _ => None
   310         }).flatMap(_.info)
   311     if (results.isEmpty) None
   312     else {
   313       val r = Text.Range(results.head.range.start, results.last.range.stop)
   314       val msgs = Command.Results.make(results.map(_.info))
   315       Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
   316     }
   317   }
   318 
   319 
   320   private val tooltips: Map[String, String] =
   321     Map(
   322       Markup.SORT -> "sort",
   323       Markup.TYP -> "type",
   324       Markup.TERM -> "term",
   325       Markup.PROP -> "proposition",
   326       Markup.TOKEN_RANGE -> "inner syntax token",
   327       Markup.FREE -> "free variable",
   328       Markup.SKOLEM -> "skolem variable",
   329       Markup.BOUND -> "bound variable",
   330       Markup.VAR -> "schematic variable",
   331       Markup.TFREE -> "free type variable",
   332       Markup.TVAR -> "schematic type variable",
   333       Markup.ML_SOURCE -> "ML source",
   334       Markup.DOCUMENT_SOURCE -> "document source")
   335 
   336   private val tooltip_elements =
   337     Set(Markup.TIMING, Markup.ENTITY, Markup.SORTING, Markup.TYPING,
   338       Markup.ML_TYPING, Markup.PATH) ++ tooltips.keys
   339 
   340   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   341     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   342 
   343   private val timing_threshold: Double = options.real("jedit_timing_threshold")
   344 
   345   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
   346   {
   347     def add(prev: Text.Info[(Timing, List[(Boolean, XML.Tree)])],
   348       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, List[(Boolean, XML.Tree)])] =
   349     {
   350       val r = snapshot.convert(r0)
   351       val (t, info) = prev.info
   352       if (prev.range == r) Text.Info(r, (t, p :: info)) else Text.Info(r, (t, List(p)))
   353     }
   354 
   355     val results =
   356       snapshot.cumulate_markup[Text.Info[(Timing, List[(Boolean, XML.Tree)])]](
   357         range, Text.Info(range, (Timing.zero, Nil)), Some(tooltip_elements), _ =>
   358         {
   359           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   360             Some(Text.Info(r, (t1 + t2, info)))
   361           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   362             val kind1 = space_explode('_', kind).mkString(" ")
   363             val txt1 = kind1 + " " + quote(name)
   364             val t = prev.info._1
   365             val txt2 =
   366               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   367                 "\n" + t.message
   368               else ""
   369             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   370           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   371           if Path.is_ok(name) =>
   372             val jedit_file = PIDE.thy_load.append(snapshot.node_name.master_dir, Path.explode(name))
   373             Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
   374           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   375           if name == Markup.SORTING || name == Markup.TYPING =>
   376             Some(add(prev, r, (true, pretty_typing("::", body))))
   377           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   378             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   379           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   380             if (tooltips.isDefinedAt(name))
   381               Some(add(prev, r, (true, XML.Text(tooltips(name)))))
   382             else None
   383         }).map(_.info)
   384 
   385     results.map(_.info).flatMap(_._2) match {
   386       case Nil => None
   387       case tips =>
   388         val r = Text.Range(results.head.range.start, results.last.range.stop)
   389         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   390         Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
   391     }
   392   }
   393 
   394   val tooltip_margin: Int = options.int("jedit_tooltip_margin")
   395 
   396   lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
   397   lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
   398 
   399 
   400   private lazy val gutter_icons = Map(
   401     Rendering.information_pri -> JEdit_Lib.load_icon(options.string("gutter_information_icon")),
   402     Rendering.warning_pri -> JEdit_Lib.load_icon(options.string("gutter_warning_icon")),
   403     Rendering.legacy_pri -> JEdit_Lib.load_icon(options.string("gutter_legacy_icon")),
   404     Rendering.error_pri -> JEdit_Lib.load_icon(options.string("gutter_error_icon")))
   405 
   406   private val gutter_elements = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   407 
   408   def gutter_message(range: Text.Range): Option[Icon] =
   409   {
   410     val results =
   411       snapshot.cumulate_markup[Int](range, 0, Some(gutter_elements), _ =>
   412         {
   413           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _),
   414               List(XML.Elem(Markup(Markup.INFORMATION, _), _))))) =>
   415             Some(pri max Rendering.information_pri)
   416           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   417             body match {
   418               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   419                 Some(pri max Rendering.legacy_pri)
   420               case _ =>
   421                 Some(pri max Rendering.warning_pri)
   422             }
   423           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   424             Some(pri max Rendering.error_pri)
   425           case _ => None
   426         })
   427     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   428     gutter_icons.get(pri)
   429   }
   430 
   431 
   432   private val squiggly_colors = Map(
   433     Rendering.writeln_pri -> writeln_color,
   434     Rendering.information_pri -> information_color,
   435     Rendering.warning_pri -> warning_color,
   436     Rendering.error_pri -> error_color)
   437 
   438   private val squiggly_include = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
   439 
   440   def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
   441   {
   442     val results =
   443       snapshot.cumulate_markup[Int](range, 0, Some(squiggly_include), _ =>
   444         {
   445           case (pri, Text.Info(_, elem)) =>
   446             if (Protocol.is_information(elem))
   447               Some(pri max Rendering.information_pri)
   448             else if (squiggly_include.contains(elem.name))
   449               Some(pri max Rendering.message_pri(elem.name))
   450             else None
   451         })
   452     for {
   453       Text.Info(r, pri) <- results
   454       color <- squiggly_colors.get(pri)
   455     } yield Text.Info(r, color)
   456   }
   457 
   458 
   459   private val message_colors = Map(
   460     Rendering.writeln_pri -> writeln_message_color,
   461     Rendering.information_pri -> information_message_color,
   462     Rendering.tracing_pri -> tracing_message_color,
   463     Rendering.warning_pri -> warning_message_color,
   464     Rendering.error_pri -> error_message_color)
   465 
   466   private val line_background_elements =
   467     Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE,
   468       Markup.ERROR_MESSAGE, Markup.INFORMATION)
   469 
   470   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   471   {
   472     val results =
   473       snapshot.cumulate_markup[Int](range, 0, Some(line_background_elements), _ =>
   474         {
   475           case (pri, Text.Info(_, elem)) =>
   476             val name = elem.name
   477             if (name == Markup.INFORMATION)
   478               Some(pri max Rendering.information_pri)
   479             else if (name == Markup.WRITELN_MESSAGE || name == Markup.TRACING_MESSAGE ||
   480                 elem.name == Markup.WARNING_MESSAGE || name == Markup.ERROR_MESSAGE)
   481               Some(pri max Rendering.message_pri(name))
   482             else None
   483         })
   484     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   485 
   486     val is_separator = pri > 0 &&
   487       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
   488         {
   489           case (_, Text.Info(_, elem)) =>
   490             if (elem.name == Markup.SEPARATOR) Some(true) else None
   491         }).exists(_.info)
   492 
   493     message_colors.get(pri).map((_, is_separator))
   494   }
   495 
   496 
   497   def output_messages(st: Command.State): List[XML.Tree] =
   498     st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
   499 
   500 
   501   private val background1_include =
   502     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   503       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
   504       active_include
   505 
   506   def background1(range: Text.Range): List[Text.Info[Color]] =
   507   {
   508     if (snapshot.is_outdated) List(Text.Info(range, outdated_color))
   509     else
   510       for {
   511         Text.Info(r, result) <-
   512           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   513             range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
   514             {
   515               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   516               if (Protocol.command_status_markup(markup.name)) =>
   517                 Some((Some(Protocol.command_status(status, markup)), color))
   518               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   519                 Some((None, Some(bad_color)))
   520               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   521                 Some((None, Some(intensify_color)))
   522               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   523                 command_state.results.get(serial) match {
   524                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   525                     Some((None, Some(active_result_color)))
   526                   case _ =>
   527                     Some((None, Some(active_color)))
   528                 }
   529               case (_, Text.Info(_, elem)) =>
   530                 if (active_include(elem.name)) Some((None, Some(active_color)))
   531                 else None
   532             })
   533         color <-
   534           (result match {
   535             case (Some(status), opt_color) =>
   536               if (status.is_unprocessed) Some(unprocessed1_color)
   537               else if (status.is_running) Some(running1_color)
   538               else opt_color
   539             case (_, opt_color) => opt_color
   540           })
   541       } yield Text.Info(r, color)
   542   }
   543 
   544 
   545   def background2(range: Text.Range): List[Text.Info[Color]] =
   546     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
   547       {
   548         case Text.Info(_, elem) =>
   549           if (elem.name == Markup.TOKEN_RANGE) Some(light_color) else None
   550       })
   551 
   552 
   553   def bullet(range: Text.Range): List[Text.Info[Color]] =
   554     snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
   555       {
   556         case Text.Info(_, elem) =>
   557           if (elem.name == Markup.BULLET) Some(bullet_color) else None
   558       })
   559 
   560 
   561   private val foreground_include =
   562     Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
   563 
   564   def foreground(range: Text.Range): List[Text.Info[Color]] =
   565     snapshot.select_markup(range, Some(foreground_include), _ =>
   566       {
   567         case Text.Info(_, elem) =>
   568           if (elem.name == Markup.ANTIQ) Some(antiquoted_color)
   569           else if (foreground_include.contains(elem.name)) Some(quoted_color)
   570           else None
   571       })
   572 
   573 
   574   private val text_colors: Map[String, Color] = Map(
   575       Markup.KEYWORD1 -> keyword1_color,
   576       Markup.KEYWORD2 -> keyword2_color,
   577       Markup.STRING -> Color.BLACK,
   578       Markup.ALTSTRING -> Color.BLACK,
   579       Markup.VERBATIM -> Color.BLACK,
   580       Markup.LITERAL -> keyword1_color,
   581       Markup.DELIMITER -> Color.BLACK,
   582       Markup.TFREE -> tfree_color,
   583       Markup.TVAR -> tvar_color,
   584       Markup.FREE -> free_color,
   585       Markup.SKOLEM -> skolem_color,
   586       Markup.BOUND -> bound_color,
   587       Markup.VAR -> var_color,
   588       Markup.INNER_STRING -> inner_quoted_color,
   589       Markup.INNER_COMMENT -> inner_comment_color,
   590       Markup.DYNAMIC_FACT -> dynamic_color,
   591       Markup.ML_KEYWORD -> keyword1_color,
   592       Markup.ML_DELIMITER -> Color.BLACK,
   593       Markup.ML_NUMERAL -> inner_numeral_color,
   594       Markup.ML_CHAR -> inner_quoted_color,
   595       Markup.ML_STRING -> inner_quoted_color,
   596       Markup.ML_COMMENT -> inner_comment_color)
   597 
   598   private val text_color_elements = text_colors.keySet
   599 
   600   def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
   601   {
   602     if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
   603     else
   604       snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
   605         {
   606           case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
   607         })
   608   }
   609 
   610 
   611   /* nested text structure -- folds */
   612 
   613   private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
   614 
   615   def fold_depth(range: Text.Range): List[Text.Info[Int]] =
   616     snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
   617       {
   618         case (depth, Text.Info(_, elem)) =>
   619           if (fold_depth_include(elem.name)) Some(depth + 1) else None
   620       })
   621 }