src/Tools/jEdit/src/isabelle_rendering.scala
author wenzelm
Fri Sep 14 13:52:16 2012 +0200 (2012-09-14)
changeset 49356 6e0c0ffb6ec7
parent 49355 7d1af0a6e797
child 49358 0fa351b1bd14
permissions -rw-r--r--
more static handling of rendering options;
wenzelm@45665
     1
/*  Title:      Tools/jEdit/src/isabelle_rendering.scala
wenzelm@39178
     2
    Author:     Makarius
wenzelm@39178
     3
wenzelm@39178
     4
Isabelle specific physical rendering and markup selection.
wenzelm@39178
     5
*/
wenzelm@39178
     6
wenzelm@39178
     7
package isabelle.jedit
wenzelm@39178
     8
wenzelm@39178
     9
wenzelm@39178
    10
import isabelle._
wenzelm@39178
    11
wenzelm@39178
    12
import java.awt.Color
wenzelm@46227
    13
import javax.swing.Icon
wenzelm@48921
    14
import java.io.{File => JFile}
wenzelm@39178
    15
wenzelm@43414
    16
import org.gjt.sp.jedit.syntax.{Token => JEditToken}
wenzelm@39178
    17
wenzelm@45460
    18
import scala.collection.immutable.SortedMap
wenzelm@45460
    19
wenzelm@39178
    20
wenzelm@45665
    21
object Isabelle_Rendering
wenzelm@39178
    22
{
wenzelm@49356
    23
  def apply(snapshot: Document.Snapshot, options: Options): Isabelle_Rendering =
wenzelm@49356
    24
    new Isabelle_Rendering(snapshot, options)
wenzelm@39178
    25
wenzelm@49356
    26
wenzelm@49356
    27
  /* physical rendering */
wenzelm@43388
    28
wenzelm@46227
    29
  private val writeln_pri = 1
wenzelm@46227
    30
  private val warning_pri = 2
wenzelm@46227
    31
  private val legacy_pri = 3
wenzelm@46227
    32
  private val error_pri = 4
wenzelm@39178
    33
wenzelm@46234
    34
  private val gutter_icons = Map(
wenzelm@46234
    35
    warning_pri -> Isabelle.load_icon("16x16/status/dialog-information.png"),
wenzelm@46234
    36
    legacy_pri -> Isabelle.load_icon("16x16/status/dialog-warning.png"),
wenzelm@46234
    37
    error_pri -> Isabelle.load_icon("16x16/status/dialog-error.png"))
wenzelm@46234
    38
wenzelm@46234
    39
wenzelm@39179
    40
  /* token markup -- text styles */
wenzelm@39179
    41
wenzelm@39179
    42
  private val command_style: Map[String, Byte] =
wenzelm@39179
    43
  {
wenzelm@43414
    44
    import JEditToken._
wenzelm@39179
    45
    Map[String, Byte](
wenzelm@39179
    46
      Keyword.THY_END -> KEYWORD2,
wenzelm@39179
    47
      Keyword.THY_SCRIPT -> LABEL,
wenzelm@39179
    48
      Keyword.PRF_SCRIPT -> LABEL,
wenzelm@39179
    49
      Keyword.PRF_ASM -> KEYWORD3,
wenzelm@39179
    50
      Keyword.PRF_ASM_GOAL -> KEYWORD3
wenzelm@39179
    51
    ).withDefaultValue(KEYWORD1)
wenzelm@39179
    52
  }
wenzelm@39179
    53
wenzelm@43414
    54
  private val token_style: Map[Token.Kind.Value, Byte] =
wenzelm@39179
    55
  {
wenzelm@43414
    56
    import JEditToken._
wenzelm@43414
    57
    Map[Token.Kind.Value, Byte](
wenzelm@43414
    58
      Token.Kind.KEYWORD -> KEYWORD2,
wenzelm@43414
    59
      Token.Kind.IDENT -> NULL,
wenzelm@43414
    60
      Token.Kind.LONG_IDENT -> NULL,
wenzelm@43414
    61
      Token.Kind.SYM_IDENT -> NULL,
wenzelm@43414
    62
      Token.Kind.VAR -> NULL,
wenzelm@43414
    63
      Token.Kind.TYPE_IDENT -> NULL,
wenzelm@43414
    64
      Token.Kind.TYPE_VAR -> NULL,
wenzelm@43414
    65
      Token.Kind.NAT -> NULL,
wenzelm@43414
    66
      Token.Kind.FLOAT -> NULL,
wenzelm@43431
    67
      Token.Kind.STRING -> LITERAL1,
wenzelm@43431
    68
      Token.Kind.ALT_STRING -> LITERAL2,
wenzelm@43414
    69
      Token.Kind.VERBATIM -> COMMENT3,
wenzelm@43414
    70
      Token.Kind.SPACE -> NULL,
wenzelm@43414
    71
      Token.Kind.COMMENT -> COMMENT1,
wenzelm@48754
    72
      Token.Kind.ERROR -> INVALID
wenzelm@39179
    73
    ).withDefaultValue(NULL)
wenzelm@39179
    74
  }
wenzelm@39179
    75
wenzelm@43414
    76
  def token_markup(syntax: Outer_Syntax, token: Token): Byte =
wenzelm@43430
    77
    if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
wenzelm@43430
    78
    else if (token.is_operator) JEditToken.OPERATOR
wenzelm@43414
    79
    else token_style(token.kind)
wenzelm@39178
    80
}
wenzelm@49356
    81
wenzelm@49356
    82
wenzelm@49356
    83
class Isabelle_Rendering private(val snapshot: Document.Snapshot, val options: Options)
wenzelm@49356
    84
{
wenzelm@49356
    85
  /* colors */
wenzelm@49356
    86
wenzelm@49356
    87
  def color_value(s: String): Color = Color_Value(options.string(s))
wenzelm@49356
    88
wenzelm@49356
    89
  val outdated_color = color_value("outdated_color")
wenzelm@49356
    90
  val unprocessed_color = color_value("unprocessed_color")
wenzelm@49356
    91
  val unprocessed1_color = color_value("unprocessed1_color")
wenzelm@49356
    92
  val running_color = color_value("running_color")
wenzelm@49356
    93
  val running1_color = color_value("running1_color")
wenzelm@49356
    94
  val light_color = color_value("light_color")
wenzelm@49356
    95
  val writeln_color = color_value("writeln_color")
wenzelm@49356
    96
  val warning_color = color_value("warning_color")
wenzelm@49356
    97
  val error_color = color_value("error_color")
wenzelm@49356
    98
  val error1_color = color_value("error1_color")
wenzelm@49356
    99
  val bad_color = color_value("bad_color")
wenzelm@49356
   100
  val hilite_color = color_value("hilite_color")
wenzelm@49356
   101
  val quoted_color = color_value("quoted_color")
wenzelm@49356
   102
  val subexp_color = color_value("subexp_color")
wenzelm@49356
   103
  val hyperlink_color = color_value("hyperlink_color")
wenzelm@49356
   104
  val keyword1_color = color_value("keyword1_color")
wenzelm@49356
   105
  val keyword2_color = color_value("keyword2_color")
wenzelm@49356
   106
wenzelm@49356
   107
  val tfree_color = color_value("tfree_color")
wenzelm@49356
   108
  val tvar_color = color_value("tvar_color")
wenzelm@49356
   109
  val free_color = color_value("free_color")
wenzelm@49356
   110
  val skolem_color = color_value("skolem_color")
wenzelm@49356
   111
  val bound_color = color_value("bound_color")
wenzelm@49356
   112
  val var_color = color_value("var_color")
wenzelm@49356
   113
  val inner_numeral_color = color_value("inner_numeral_color")
wenzelm@49356
   114
  val inner_quoted_color = color_value("inner_quoted_color")
wenzelm@49356
   115
  val inner_comment_color = color_value("inner_comment_color")
wenzelm@49356
   116
  val antiquotation_color = color_value("antiquotation_color")
wenzelm@49356
   117
  val dynamic_color = color_value("dynamic_color")
wenzelm@49356
   118
wenzelm@49356
   119
wenzelm@49356
   120
  /* command overview */
wenzelm@49356
   121
wenzelm@49356
   122
  def overview_color(range: Text.Range): Option[Color] =
wenzelm@49356
   123
  {
wenzelm@49356
   124
    if (snapshot.is_outdated) None
wenzelm@49356
   125
    else {
wenzelm@49356
   126
      val results =
wenzelm@49356
   127
        snapshot.cumulate_markup[(Protocol.Status, Int)](
wenzelm@49356
   128
          range, (Protocol.Status.init, 0),
wenzelm@49356
   129
          Some(Protocol.command_status_markup + Isabelle_Markup.WARNING + Isabelle_Markup.ERROR),
wenzelm@49356
   130
          {
wenzelm@49356
   131
            case ((status, pri), Text.Info(_, XML.Elem(markup, _))) =>
wenzelm@49356
   132
              if (markup.name == Isabelle_Markup.WARNING)
wenzelm@49356
   133
                (status, pri max Isabelle_Rendering.warning_pri)
wenzelm@49356
   134
              else if (markup.name == Isabelle_Markup.ERROR)
wenzelm@49356
   135
                (status, pri max Isabelle_Rendering.error_pri)
wenzelm@49356
   136
              else (Protocol.command_status(status, markup), pri)
wenzelm@49356
   137
          })
wenzelm@49356
   138
      if (results.isEmpty) None
wenzelm@49356
   139
      else {
wenzelm@49356
   140
        val (status, pri) =
wenzelm@49356
   141
          ((Protocol.Status.init, 0) /: results) {
wenzelm@49356
   142
            case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
wenzelm@49356
   143
wenzelm@49356
   144
        if (pri == Isabelle_Rendering.warning_pri) Some(warning_color)
wenzelm@49356
   145
        else if (pri == Isabelle_Rendering.error_pri) Some(error_color)
wenzelm@49356
   146
        else if (status.is_unprocessed) Some(unprocessed_color)
wenzelm@49356
   147
        else if (status.is_running) Some(running_color)
wenzelm@49356
   148
        else if (status.is_failed) Some(error_color)
wenzelm@49356
   149
        else None
wenzelm@49356
   150
      }
wenzelm@49356
   151
    }
wenzelm@49356
   152
  }
wenzelm@49356
   153
wenzelm@49356
   154
wenzelm@49356
   155
  /* markup selectors */
wenzelm@49356
   156
wenzelm@49356
   157
  private val subexp_include =
wenzelm@49356
   158
    Set(Isabelle_Markup.SORT, Isabelle_Markup.TYP, Isabelle_Markup.TERM, Isabelle_Markup.PROP,
wenzelm@49356
   159
      Isabelle_Markup.ML_TYPING, Isabelle_Markup.TOKEN_RANGE, Isabelle_Markup.ENTITY,
wenzelm@49356
   160
      Isabelle_Markup.PATH, Isabelle_Markup.TYPING, Isabelle_Markup.FREE, Isabelle_Markup.SKOLEM,
wenzelm@49356
   161
      Isabelle_Markup.BOUND, Isabelle_Markup.VAR, Isabelle_Markup.TFREE, Isabelle_Markup.TVAR,
wenzelm@49356
   162
      Isabelle_Markup.ML_SOURCE, Isabelle_Markup.DOCUMENT_SOURCE)
wenzelm@49356
   163
wenzelm@49356
   164
  def subexp(range: Text.Range): Option[Text.Info[Color]] =
wenzelm@49356
   165
  {
wenzelm@49356
   166
    snapshot.select_markup(range, Some(subexp_include),
wenzelm@49356
   167
        {
wenzelm@49356
   168
          case Text.Info(info_range, XML.Elem(Markup(name, _), _)) if subexp_include(name) =>
wenzelm@49356
   169
            Text.Info(snapshot.convert(info_range), subexp_color)
wenzelm@49356
   170
        }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
wenzelm@49356
   171
  }
wenzelm@49356
   172
wenzelm@49356
   173
wenzelm@49356
   174
  private val hyperlink_include = Set(Isabelle_Markup.ENTITY, Isabelle_Markup.PATH)
wenzelm@49356
   175
wenzelm@49356
   176
  def hyperlink(range: Text.Range): Option[Text.Info[Hyperlink]] =
wenzelm@49356
   177
  {
wenzelm@49356
   178
    snapshot.cumulate_markup[List[Text.Info[Hyperlink]]](range, Nil, Some(hyperlink_include),
wenzelm@49356
   179
        {
wenzelm@49356
   180
          case (links, Text.Info(info_range, XML.Elem(Isabelle_Markup.Path(name), _)))
wenzelm@49356
   181
          if Path.is_ok(name) =>
wenzelm@49356
   182
            val jedit_file = Isabelle.thy_load.append(snapshot.node_name.dir, Path.explode(name))
wenzelm@49356
   183
            Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, 0, 0)) :: links
wenzelm@49356
   184
wenzelm@49356
   185
          case (links, Text.Info(info_range, XML.Elem(Markup(Isabelle_Markup.ENTITY, props), _)))
wenzelm@49356
   186
          if (props.find(
wenzelm@49356
   187
            { case (Markup.KIND, Isabelle_Markup.ML_OPEN) => true
wenzelm@49356
   188
              case (Markup.KIND, Isabelle_Markup.ML_STRUCT) => true
wenzelm@49356
   189
              case _ => false }).isEmpty) =>
wenzelm@49356
   190
wenzelm@49356
   191
            props match {
wenzelm@49356
   192
              case Position.Line_File(line, name) if Path.is_ok(name) =>
wenzelm@49356
   193
                Isabelle_System.source_file(Path.explode(name)) match {
wenzelm@49356
   194
                  case Some(path) =>
wenzelm@49356
   195
                    val jedit_file = Isabelle_System.platform_path(path)
wenzelm@49356
   196
                    Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, line, 0)) :: links
wenzelm@49356
   197
                  case None => links
wenzelm@49356
   198
                }
wenzelm@49356
   199
wenzelm@49356
   200
              case Position.Id_Offset(id, offset) if !snapshot.is_outdated =>
wenzelm@49356
   201
                snapshot.state.find_command(snapshot.version, id) match {
wenzelm@49356
   202
                  case Some((node, command)) =>
wenzelm@49356
   203
                    val sources =
wenzelm@49356
   204
                      node.commands.iterator.takeWhile(_ != command).map(_.source) ++
wenzelm@49356
   205
                        Iterator.single(command.source(Text.Range(0, command.decode(offset))))
wenzelm@49356
   206
                    val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@49356
   207
                    Text.Info(snapshot.convert(info_range),
wenzelm@49356
   208
                      Hyperlink(command.node_name.node, line, column)) :: links
wenzelm@49356
   209
                  case None => links
wenzelm@49356
   210
                }
wenzelm@49356
   211
wenzelm@49356
   212
              case _ => links
wenzelm@49356
   213
            }
wenzelm@49356
   214
        }) match { case Text.Info(_, info :: _) #:: _ => Some(info) case _ => None }
wenzelm@49356
   215
  }
wenzelm@49356
   216
wenzelm@49356
   217
wenzelm@49356
   218
  private def tooltip_text(msg: XML.Tree): String =
wenzelm@49356
   219
    Pretty.string_of(List(msg), margin = options.int("jedit_tooltip_margin"))
wenzelm@49356
   220
wenzelm@49356
   221
  def tooltip_message(range: Text.Range): Option[String] =
wenzelm@49356
   222
  {
wenzelm@49356
   223
    val msgs =
wenzelm@49356
   224
      snapshot.cumulate_markup[SortedMap[Long, String]](range, SortedMap.empty,
wenzelm@49356
   225
        Some(Set(Isabelle_Markup.WRITELN, Isabelle_Markup.WARNING, Isabelle_Markup.ERROR,
wenzelm@49356
   226
          Isabelle_Markup.BAD)),
wenzelm@49356
   227
        {
wenzelm@49356
   228
          case (msgs, Text.Info(_, msg @
wenzelm@49356
   229
              XML.Elem(Markup(markup, Isabelle_Markup.Serial(serial)), _)))
wenzelm@49356
   230
          if markup == Isabelle_Markup.WRITELN ||
wenzelm@49356
   231
              markup == Isabelle_Markup.WARNING ||
wenzelm@49356
   232
              markup == Isabelle_Markup.ERROR =>
wenzelm@49356
   233
            msgs + (serial -> tooltip_text(msg))
wenzelm@49356
   234
          case (msgs, Text.Info(_, msg @ XML.Elem(Markup(Isabelle_Markup.BAD, _), body)))
wenzelm@49356
   235
          if !body.isEmpty => msgs + (Document.new_id() -> tooltip_text(msg))
wenzelm@49356
   236
        }).toList.flatMap(_.info)
wenzelm@49356
   237
    if (msgs.isEmpty) None else Some(cat_lines(msgs.iterator.map(_._2)))
wenzelm@49356
   238
  }
wenzelm@49356
   239
wenzelm@49356
   240
wenzelm@49356
   241
  private val tooltips: Map[String, String] =
wenzelm@49356
   242
    Map(
wenzelm@49356
   243
      Isabelle_Markup.SORT -> "sort",
wenzelm@49356
   244
      Isabelle_Markup.TYP -> "type",
wenzelm@49356
   245
      Isabelle_Markup.TERM -> "term",
wenzelm@49356
   246
      Isabelle_Markup.PROP -> "proposition",
wenzelm@49356
   247
      Isabelle_Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@49356
   248
      Isabelle_Markup.FREE -> "free variable",
wenzelm@49356
   249
      Isabelle_Markup.SKOLEM -> "skolem variable",
wenzelm@49356
   250
      Isabelle_Markup.BOUND -> "bound variable",
wenzelm@49356
   251
      Isabelle_Markup.VAR -> "schematic variable",
wenzelm@49356
   252
      Isabelle_Markup.TFREE -> "free type variable",
wenzelm@49356
   253
      Isabelle_Markup.TVAR -> "schematic type variable",
wenzelm@49356
   254
      Isabelle_Markup.ML_SOURCE -> "ML source",
wenzelm@49356
   255
      Isabelle_Markup.DOCUMENT_SOURCE -> "document source")
wenzelm@49356
   256
wenzelm@49356
   257
  private val tooltip_elements =
wenzelm@49356
   258
    Set(Isabelle_Markup.ENTITY, Isabelle_Markup.TYPING, Isabelle_Markup.ML_TYPING,
wenzelm@49356
   259
      Isabelle_Markup.PATH) ++ tooltips.keys
wenzelm@49356
   260
wenzelm@49356
   261
  private def string_of_typing(kind: String, body: XML.Body): String =
wenzelm@49356
   262
    Pretty.string_of(List(Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)),
wenzelm@49356
   263
      margin = options.int("jedit_tooltip_margin"))
wenzelm@49356
   264
wenzelm@49356
   265
  def tooltip(range: Text.Range): Option[String] =
wenzelm@49356
   266
  {
wenzelm@49356
   267
    def add(prev: Text.Info[List[(Boolean, String)]], r: Text.Range, p: (Boolean, String)) =
wenzelm@49356
   268
      if (prev.range == r) Text.Info(r, p :: prev.info) else Text.Info(r, List(p))
wenzelm@49356
   269
wenzelm@49356
   270
    val tips =
wenzelm@49356
   271
      snapshot.cumulate_markup[Text.Info[(List[(Boolean, String)])]](
wenzelm@49356
   272
        range, Text.Info(range, Nil), Some(tooltip_elements),
wenzelm@49356
   273
        {
wenzelm@49356
   274
          case (prev, Text.Info(r, XML.Elem(Isabelle_Markup.Entity(kind, name), _))) =>
wenzelm@49356
   275
            val kind1 = space_explode('_', kind).mkString(" ")
wenzelm@49356
   276
            add(prev, r, (true, kind1 + " " + quote(name)))
wenzelm@49356
   277
          case (prev, Text.Info(r, XML.Elem(Isabelle_Markup.Path(name), _)))
wenzelm@49356
   278
          if Path.is_ok(name) =>
wenzelm@49356
   279
            val jedit_file = Isabelle.thy_load.append(snapshot.node_name.dir, Path.explode(name))
wenzelm@49356
   280
            add(prev, r, (true, "file " + quote(jedit_file)))
wenzelm@49356
   281
          case (prev, Text.Info(r, XML.Elem(Markup(Isabelle_Markup.TYPING, _), body))) =>
wenzelm@49356
   282
            add(prev, r, (true, string_of_typing("::", body)))
wenzelm@49356
   283
          case (prev, Text.Info(r, XML.Elem(Markup(Isabelle_Markup.ML_TYPING, _), body))) =>
wenzelm@49356
   284
            add(prev, r, (false, string_of_typing("ML:", body)))
wenzelm@49356
   285
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _)))
wenzelm@49356
   286
          if tooltips.isDefinedAt(name) => add(prev, r, (true, tooltips(name)))
wenzelm@49356
   287
        }).toList.flatMap(_.info.info)
wenzelm@49356
   288
wenzelm@49356
   289
    val all_tips =
wenzelm@49356
   290
      (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@49356
   291
    if (all_tips.isEmpty) None else Some(cat_lines(all_tips))
wenzelm@49356
   292
  }
wenzelm@49356
   293
wenzelm@49356
   294
wenzelm@49356
   295
  def gutter_message(range: Text.Range): Option[Icon] =
wenzelm@49356
   296
  {
wenzelm@49356
   297
    val results =
wenzelm@49356
   298
      snapshot.cumulate_markup[Int](range, 0,
wenzelm@49356
   299
        Some(Set(Isabelle_Markup.WARNING, Isabelle_Markup.ERROR)),
wenzelm@49356
   300
        {
wenzelm@49356
   301
          case (pri, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.WARNING, _), body))) =>
wenzelm@49356
   302
            body match {
wenzelm@49356
   303
              case List(XML.Elem(Markup(Isabelle_Markup.LEGACY, _), _)) =>
wenzelm@49356
   304
                pri max Isabelle_Rendering.legacy_pri
wenzelm@49356
   305
              case _ => pri max Isabelle_Rendering.warning_pri
wenzelm@49356
   306
            }
wenzelm@49356
   307
          case (pri, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.ERROR, _), _))) =>
wenzelm@49356
   308
            pri max Isabelle_Rendering.error_pri
wenzelm@49356
   309
        })
wenzelm@49356
   310
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@49356
   311
    Isabelle_Rendering.gutter_icons.get(pri)
wenzelm@49356
   312
  }
wenzelm@49356
   313
wenzelm@49356
   314
wenzelm@49356
   315
  private val squiggly_colors = Map(
wenzelm@49356
   316
    Isabelle_Rendering.writeln_pri -> writeln_color,
wenzelm@49356
   317
    Isabelle_Rendering.warning_pri -> warning_color,
wenzelm@49356
   318
    Isabelle_Rendering.error_pri -> error_color)
wenzelm@49356
   319
wenzelm@49356
   320
  def squiggly_underline(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   321
  {
wenzelm@49356
   322
    val results =
wenzelm@49356
   323
      snapshot.cumulate_markup[Int](range, 0,
wenzelm@49356
   324
        Some(Set(Isabelle_Markup.WRITELN, Isabelle_Markup.WARNING, Isabelle_Markup.ERROR)),
wenzelm@49356
   325
        {
wenzelm@49356
   326
          case (pri, Text.Info(_, XML.Elem(Markup(name, _), _))) =>
wenzelm@49356
   327
            name match {
wenzelm@49356
   328
              case Isabelle_Markup.WRITELN => pri max Isabelle_Rendering.writeln_pri
wenzelm@49356
   329
              case Isabelle_Markup.WARNING => pri max Isabelle_Rendering.warning_pri
wenzelm@49356
   330
              case Isabelle_Markup.ERROR => pri max Isabelle_Rendering.error_pri
wenzelm@49356
   331
              case _ => pri
wenzelm@49356
   332
            }
wenzelm@49356
   333
        })
wenzelm@49356
   334
    for {
wenzelm@49356
   335
      Text.Info(r, pri) <- results
wenzelm@49356
   336
      color <- squiggly_colors.get(pri)
wenzelm@49356
   337
    } yield Text.Info(r, color)
wenzelm@49356
   338
  }
wenzelm@49356
   339
wenzelm@49356
   340
wenzelm@49356
   341
  def background1(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   342
  {
wenzelm@49356
   343
    if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
wenzelm@49356
   344
    else
wenzelm@49356
   345
      for {
wenzelm@49356
   346
        Text.Info(r, result) <-
wenzelm@49356
   347
          snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
wenzelm@49356
   348
            range, (Some(Protocol.Status.init), None),
wenzelm@49356
   349
            Some(Protocol.command_status_markup + Isabelle_Markup.BAD + Isabelle_Markup.HILITE),
wenzelm@49356
   350
            {
wenzelm@49356
   351
              case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@49356
   352
              if (Protocol.command_status_markup(markup.name)) =>
wenzelm@49356
   353
                (Some(Protocol.command_status(status, markup)), color)
wenzelm@49356
   354
              case (_, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.BAD, _), _))) =>
wenzelm@49356
   355
                (None, Some(bad_color))
wenzelm@49356
   356
              case (_, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.HILITE, _), _))) =>
wenzelm@49356
   357
                (None, Some(hilite_color))
wenzelm@49356
   358
            })
wenzelm@49356
   359
        color <-
wenzelm@49356
   360
          (result match {
wenzelm@49356
   361
            case (Some(status), opt_color) =>
wenzelm@49356
   362
              if (status.is_unprocessed) Some(unprocessed1_color)
wenzelm@49356
   363
              else if (status.is_running) Some(running1_color)
wenzelm@49356
   364
              else opt_color
wenzelm@49356
   365
            case (_, opt_color) => opt_color
wenzelm@49356
   366
          })
wenzelm@49356
   367
      } yield Text.Info(r, color)
wenzelm@49356
   368
  }
wenzelm@49356
   369
wenzelm@49356
   370
wenzelm@49356
   371
  def background2(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   372
    snapshot.select_markup(range,
wenzelm@49356
   373
      Some(Set(Isabelle_Markup.TOKEN_RANGE)),
wenzelm@49356
   374
      {
wenzelm@49356
   375
        case Text.Info(_, XML.Elem(Markup(Isabelle_Markup.TOKEN_RANGE, _), _)) => light_color
wenzelm@49356
   376
      })
wenzelm@49356
   377
wenzelm@49356
   378
wenzelm@49356
   379
  def foreground(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   380
    snapshot.select_markup(range,
wenzelm@49356
   381
      Some(Set(Isabelle_Markup.STRING, Isabelle_Markup.ALTSTRING, Isabelle_Markup.VERBATIM)),
wenzelm@49356
   382
      {
wenzelm@49356
   383
        case Text.Info(_, XML.Elem(Markup(Isabelle_Markup.STRING, _), _)) => quoted_color
wenzelm@49356
   384
        case Text.Info(_, XML.Elem(Markup(Isabelle_Markup.ALTSTRING, _), _)) => quoted_color
wenzelm@49356
   385
        case Text.Info(_, XML.Elem(Markup(Isabelle_Markup.VERBATIM, _), _)) => quoted_color
wenzelm@49356
   386
      })
wenzelm@49356
   387
wenzelm@49356
   388
wenzelm@49356
   389
  private val text_colors: Map[String, Color] = Map(
wenzelm@49356
   390
      Isabelle_Markup.STRING -> Color.BLACK,
wenzelm@49356
   391
      Isabelle_Markup.ALTSTRING -> Color.BLACK,
wenzelm@49356
   392
      Isabelle_Markup.VERBATIM -> Color.BLACK,
wenzelm@49356
   393
      Isabelle_Markup.LITERAL -> keyword1_color,
wenzelm@49356
   394
      Isabelle_Markup.DELIMITER -> Color.BLACK,
wenzelm@49356
   395
      Isabelle_Markup.TFREE -> tfree_color,
wenzelm@49356
   396
      Isabelle_Markup.TVAR -> tvar_color,
wenzelm@49356
   397
      Isabelle_Markup.FREE -> free_color,
wenzelm@49356
   398
      Isabelle_Markup.SKOLEM -> skolem_color,
wenzelm@49356
   399
      Isabelle_Markup.BOUND -> bound_color,
wenzelm@49356
   400
      Isabelle_Markup.VAR -> var_color,
wenzelm@49356
   401
      Isabelle_Markup.INNER_STRING -> inner_quoted_color,
wenzelm@49356
   402
      Isabelle_Markup.INNER_COMMENT -> inner_comment_color,
wenzelm@49356
   403
      Isabelle_Markup.DYNAMIC_FACT -> dynamic_color,
wenzelm@49356
   404
      Isabelle_Markup.ML_KEYWORD -> keyword1_color,
wenzelm@49356
   405
      Isabelle_Markup.ML_DELIMITER -> Color.BLACK,
wenzelm@49356
   406
      Isabelle_Markup.ML_NUMERAL -> inner_numeral_color,
wenzelm@49356
   407
      Isabelle_Markup.ML_CHAR -> inner_quoted_color,
wenzelm@49356
   408
      Isabelle_Markup.ML_STRING -> inner_quoted_color,
wenzelm@49356
   409
      Isabelle_Markup.ML_COMMENT -> inner_comment_color,
wenzelm@49356
   410
      Isabelle_Markup.ANTIQ -> antiquotation_color)
wenzelm@49356
   411
wenzelm@49356
   412
  private val text_color_elements = Set.empty[String] ++ text_colors.keys
wenzelm@49356
   413
wenzelm@49356
   414
  def text_color(range: Text.Range, color: Color)
wenzelm@49356
   415
      : Stream[Text.Info[Color]] =
wenzelm@49356
   416
  {
wenzelm@49356
   417
    snapshot.cumulate_markup(range, color, Some(text_color_elements),
wenzelm@49356
   418
      {
wenzelm@49356
   419
        case (_, Text.Info(_, XML.Elem(Markup(m, _), _)))
wenzelm@49356
   420
        if text_colors.isDefinedAt(m) => text_colors(m)
wenzelm@49356
   421
      })
wenzelm@49356
   422
  }
wenzelm@49356
   423
}