src/Tools/jEdit/src/isabelle_rendering.scala
author wenzelm
Sun Nov 25 18:50:13 2012 +0100 (2012-11-25)
changeset 50200 2c94c065564e
parent 50199 6d04e2422769
child 50201 c26369c9eda6
permissions -rw-r--r--
prefer strict error;
wenzelm@45665
     1
/*  Title:      Tools/jEdit/src/isabelle_rendering.scala
wenzelm@39178
     2
    Author:     Makarius
wenzelm@39178
     3
wenzelm@50199
     4
Isabelle-specific implementation of quasi-abstract rendering and
wenzelm@50199
     5
markup interpretation.
wenzelm@39178
     6
*/
wenzelm@39178
     7
wenzelm@39178
     8
package isabelle.jedit
wenzelm@39178
     9
wenzelm@39178
    10
wenzelm@39178
    11
import isabelle._
wenzelm@39178
    12
wenzelm@39178
    13
import java.awt.Color
wenzelm@46227
    14
import javax.swing.Icon
wenzelm@39178
    15
wenzelm@43414
    16
import org.gjt.sp.jedit.syntax.{Token => JEditToken}
wenzelm@49406
    17
import org.gjt.sp.jedit.GUIUtilities
wenzelm@39178
    18
wenzelm@45460
    19
import scala.collection.immutable.SortedMap
wenzelm@45460
    20
wenzelm@39178
    21
wenzelm@50199
    22
object Rendering
wenzelm@39178
    23
{
wenzelm@50199
    24
  def apply(snapshot: Document.Snapshot, options: Options): Rendering =
wenzelm@50199
    25
    new Rendering(snapshot, options)
wenzelm@39178
    26
wenzelm@49356
    27
wenzelm@49406
    28
  /* message priorities */
wenzelm@43388
    29
wenzelm@46227
    30
  private val writeln_pri = 1
wenzelm@49474
    31
  private val tracing_pri = 2
wenzelm@49474
    32
  private val warning_pri = 3
wenzelm@49474
    33
  private val legacy_pri = 4
wenzelm@49474
    34
  private val error_pri = 5
wenzelm@49474
    35
wenzelm@49474
    36
  private val message_pri = Map(
wenzelm@49474
    37
    Isabelle_Markup.WRITELN -> writeln_pri, Isabelle_Markup.WRITELN_MESSAGE -> writeln_pri,
wenzelm@49474
    38
    Isabelle_Markup.TRACING -> tracing_pri, Isabelle_Markup.TRACING_MESSAGE -> tracing_pri,
wenzelm@49474
    39
    Isabelle_Markup.WARNING -> warning_pri, Isabelle_Markup.WARNING_MESSAGE -> warning_pri,
wenzelm@49474
    40
    Isabelle_Markup.ERROR -> error_pri, Isabelle_Markup.ERROR_MESSAGE -> error_pri)
wenzelm@39178
    41
wenzelm@49406
    42
wenzelm@49406
    43
  /* icons */
wenzelm@49406
    44
wenzelm@49406
    45
  private def load_icon(name: String): Icon =
wenzelm@49406
    46
  {
wenzelm@49406
    47
    val icon = GUIUtilities.loadIcon(name)
wenzelm@50200
    48
    if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name)
wenzelm@50200
    49
    else icon
wenzelm@49406
    50
  }
wenzelm@49406
    51
wenzelm@46234
    52
  private val gutter_icons = Map(
wenzelm@49406
    53
    warning_pri -> load_icon("16x16/status/dialog-information.png"),
wenzelm@49406
    54
    legacy_pri -> load_icon("16x16/status/dialog-warning.png"),
wenzelm@49406
    55
    error_pri -> load_icon("16x16/status/dialog-error.png"))
wenzelm@46234
    56
wenzelm@49727
    57
  val tooltip_close_icon = load_icon("16x16/actions/document-close.png")
wenzelm@49726
    58
  val tooltip_detach_icon = load_icon("16x16/actions/window-new.png")
wenzelm@49725
    59
wenzelm@46234
    60
wenzelm@39179
    61
  /* token markup -- text styles */
wenzelm@39179
    62
wenzelm@39179
    63
  private val command_style: Map[String, Byte] =
wenzelm@39179
    64
  {
wenzelm@43414
    65
    import JEditToken._
wenzelm@39179
    66
    Map[String, Byte](
wenzelm@39179
    67
      Keyword.THY_END -> KEYWORD2,
wenzelm@39179
    68
      Keyword.THY_SCRIPT -> LABEL,
wenzelm@39179
    69
      Keyword.PRF_SCRIPT -> LABEL,
wenzelm@39179
    70
      Keyword.PRF_ASM -> KEYWORD3,
wenzelm@39179
    71
      Keyword.PRF_ASM_GOAL -> KEYWORD3
wenzelm@39179
    72
    ).withDefaultValue(KEYWORD1)
wenzelm@39179
    73
  }
wenzelm@39179
    74
wenzelm@43414
    75
  private val token_style: Map[Token.Kind.Value, Byte] =
wenzelm@39179
    76
  {
wenzelm@43414
    77
    import JEditToken._
wenzelm@43414
    78
    Map[Token.Kind.Value, Byte](
wenzelm@43414
    79
      Token.Kind.KEYWORD -> KEYWORD2,
wenzelm@43414
    80
      Token.Kind.IDENT -> NULL,
wenzelm@43414
    81
      Token.Kind.LONG_IDENT -> NULL,
wenzelm@43414
    82
      Token.Kind.SYM_IDENT -> NULL,
wenzelm@43414
    83
      Token.Kind.VAR -> NULL,
wenzelm@43414
    84
      Token.Kind.TYPE_IDENT -> NULL,
wenzelm@43414
    85
      Token.Kind.TYPE_VAR -> NULL,
wenzelm@43414
    86
      Token.Kind.NAT -> NULL,
wenzelm@43414
    87
      Token.Kind.FLOAT -> NULL,
wenzelm@43431
    88
      Token.Kind.STRING -> LITERAL1,
wenzelm@43431
    89
      Token.Kind.ALT_STRING -> LITERAL2,
wenzelm@43414
    90
      Token.Kind.VERBATIM -> COMMENT3,
wenzelm@43414
    91
      Token.Kind.SPACE -> NULL,
wenzelm@43414
    92
      Token.Kind.COMMENT -> COMMENT1,
wenzelm@48754
    93
      Token.Kind.ERROR -> INVALID
wenzelm@39179
    94
    ).withDefaultValue(NULL)
wenzelm@39179
    95
  }
wenzelm@39179
    96
wenzelm@43414
    97
  def token_markup(syntax: Outer_Syntax, token: Token): Byte =
wenzelm@43430
    98
    if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
wenzelm@43430
    99
    else if (token.is_operator) JEditToken.OPERATOR
wenzelm@43414
   100
    else token_style(token.kind)
wenzelm@39178
   101
}
wenzelm@49356
   102
wenzelm@49356
   103
wenzelm@50199
   104
class Rendering private(val snapshot: Document.Snapshot, val options: Options)
wenzelm@49356
   105
{
wenzelm@49356
   106
  /* colors */
wenzelm@49356
   107
wenzelm@49356
   108
  def color_value(s: String): Color = Color_Value(options.string(s))
wenzelm@49356
   109
wenzelm@49356
   110
  val outdated_color = color_value("outdated_color")
wenzelm@49356
   111
  val unprocessed_color = color_value("unprocessed_color")
wenzelm@49356
   112
  val unprocessed1_color = color_value("unprocessed1_color")
wenzelm@49356
   113
  val running_color = color_value("running_color")
wenzelm@49356
   114
  val running1_color = color_value("running1_color")
wenzelm@49356
   115
  val light_color = color_value("light_color")
wenzelm@49706
   116
  val tooltip_color = color_value("tooltip_color")
wenzelm@49356
   117
  val writeln_color = color_value("writeln_color")
wenzelm@49356
   118
  val warning_color = color_value("warning_color")
wenzelm@49356
   119
  val error_color = color_value("error_color")
wenzelm@49356
   120
  val error1_color = color_value("error1_color")
wenzelm@49418
   121
  val writeln_message_color = color_value("writeln_message_color")
wenzelm@49418
   122
  val tracing_message_color = color_value("tracing_message_color")
wenzelm@49418
   123
  val warning_message_color = color_value("warning_message_color")
wenzelm@49418
   124
  val error_message_color = color_value("error_message_color")
wenzelm@49356
   125
  val bad_color = color_value("bad_color")
wenzelm@49358
   126
  val intensify_color = color_value("intensify_color")
wenzelm@49356
   127
  val quoted_color = color_value("quoted_color")
wenzelm@49358
   128
  val highlight_color = color_value("highlight_color")
wenzelm@49356
   129
  val hyperlink_color = color_value("hyperlink_color")
wenzelm@49492
   130
  val sendback_color = color_value("sendback_color")
wenzelm@49492
   131
  val sendback_active_color = color_value("sendback_active_color")
wenzelm@49356
   132
  val keyword1_color = color_value("keyword1_color")
wenzelm@49356
   133
  val keyword2_color = color_value("keyword2_color")
wenzelm@49356
   134
wenzelm@49356
   135
  val tfree_color = color_value("tfree_color")
wenzelm@49356
   136
  val tvar_color = color_value("tvar_color")
wenzelm@49356
   137
  val free_color = color_value("free_color")
wenzelm@49356
   138
  val skolem_color = color_value("skolem_color")
wenzelm@49356
   139
  val bound_color = color_value("bound_color")
wenzelm@49356
   140
  val var_color = color_value("var_color")
wenzelm@49356
   141
  val inner_numeral_color = color_value("inner_numeral_color")
wenzelm@49356
   142
  val inner_quoted_color = color_value("inner_quoted_color")
wenzelm@49356
   143
  val inner_comment_color = color_value("inner_comment_color")
wenzelm@49356
   144
  val antiquotation_color = color_value("antiquotation_color")
wenzelm@49356
   145
  val dynamic_color = color_value("dynamic_color")
wenzelm@49356
   146
wenzelm@49356
   147
wenzelm@49356
   148
  /* command overview */
wenzelm@49356
   149
wenzelm@49969
   150
  val overview_limit = options.int("jedit_text_overview_limit")
wenzelm@49969
   151
wenzelm@49356
   152
  def overview_color(range: Text.Range): Option[Color] =
wenzelm@49356
   153
  {
wenzelm@49356
   154
    if (snapshot.is_outdated) None
wenzelm@49356
   155
    else {
wenzelm@49356
   156
      val results =
wenzelm@49356
   157
        snapshot.cumulate_markup[(Protocol.Status, Int)](
wenzelm@49356
   158
          range, (Protocol.Status.init, 0),
wenzelm@49356
   159
          Some(Protocol.command_status_markup + Isabelle_Markup.WARNING + Isabelle_Markup.ERROR),
wenzelm@49356
   160
          {
wenzelm@49356
   161
            case ((status, pri), Text.Info(_, XML.Elem(markup, _))) =>
wenzelm@49474
   162
              if (markup.name == Isabelle_Markup.WARNING || markup.name == Isabelle_Markup.ERROR)
wenzelm@50199
   163
                (status, pri max Rendering.message_pri(markup.name))
wenzelm@49356
   164
              else (Protocol.command_status(status, markup), pri)
wenzelm@49356
   165
          })
wenzelm@49356
   166
      if (results.isEmpty) None
wenzelm@49356
   167
      else {
wenzelm@49356
   168
        val (status, pri) =
wenzelm@49356
   169
          ((Protocol.Status.init, 0) /: results) {
wenzelm@49356
   170
            case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
wenzelm@49356
   171
wenzelm@50199
   172
        if (pri == Rendering.warning_pri) Some(warning_color)
wenzelm@50199
   173
        else if (pri == Rendering.error_pri) Some(error_color)
wenzelm@49356
   174
        else if (status.is_unprocessed) Some(unprocessed_color)
wenzelm@49356
   175
        else if (status.is_running) Some(running_color)
wenzelm@49356
   176
        else if (status.is_failed) Some(error_color)
wenzelm@49356
   177
        else None
wenzelm@49356
   178
      }
wenzelm@49356
   179
    }
wenzelm@49356
   180
  }
wenzelm@49356
   181
wenzelm@49356
   182
wenzelm@49356
   183
  /* markup selectors */
wenzelm@49356
   184
wenzelm@49358
   185
  private val highlight_include =
wenzelm@49356
   186
    Set(Isabelle_Markup.SORT, Isabelle_Markup.TYP, Isabelle_Markup.TERM, Isabelle_Markup.PROP,
wenzelm@49356
   187
      Isabelle_Markup.ML_TYPING, Isabelle_Markup.TOKEN_RANGE, Isabelle_Markup.ENTITY,
wenzelm@49674
   188
      Isabelle_Markup.PATH, Isabelle_Markup.SORTING, Isabelle_Markup.TYPING, Isabelle_Markup.FREE,
wenzelm@49674
   189
      Isabelle_Markup.SKOLEM, Isabelle_Markup.BOUND, Isabelle_Markup.VAR, Isabelle_Markup.TFREE,
wenzelm@49674
   190
      Isabelle_Markup.TVAR, Isabelle_Markup.ML_SOURCE, Isabelle_Markup.DOCUMENT_SOURCE)
wenzelm@49356
   191
wenzelm@49358
   192
  def highlight(range: Text.Range): Option[Text.Info[Color]] =
wenzelm@49356
   193
  {
wenzelm@49358
   194
    snapshot.select_markup(range, Some(highlight_include),
wenzelm@49356
   195
        {
wenzelm@49358
   196
          case Text.Info(info_range, XML.Elem(Markup(name, _), _)) if highlight_include(name) =>
wenzelm@49358
   197
            Text.Info(snapshot.convert(info_range), highlight_color)
wenzelm@49356
   198
        }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
wenzelm@49356
   199
  }
wenzelm@49356
   200
wenzelm@49356
   201
wenzelm@49356
   202
  private val hyperlink_include = Set(Isabelle_Markup.ENTITY, Isabelle_Markup.PATH)
wenzelm@49356
   203
wenzelm@49356
   204
  def hyperlink(range: Text.Range): Option[Text.Info[Hyperlink]] =
wenzelm@49356
   205
  {
wenzelm@49356
   206
    snapshot.cumulate_markup[List[Text.Info[Hyperlink]]](range, Nil, Some(hyperlink_include),
wenzelm@49356
   207
        {
wenzelm@49356
   208
          case (links, Text.Info(info_range, XML.Elem(Isabelle_Markup.Path(name), _)))
wenzelm@49356
   209
          if Path.is_ok(name) =>
wenzelm@49356
   210
            val jedit_file = Isabelle.thy_load.append(snapshot.node_name.dir, Path.explode(name))
wenzelm@49356
   211
            Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, 0, 0)) :: links
wenzelm@49356
   212
wenzelm@49356
   213
          case (links, Text.Info(info_range, XML.Elem(Markup(Isabelle_Markup.ENTITY, props), _)))
wenzelm@49815
   214
          if !props.exists(
wenzelm@49356
   215
            { case (Markup.KIND, Isabelle_Markup.ML_OPEN) => true
wenzelm@49356
   216
              case (Markup.KIND, Isabelle_Markup.ML_STRUCT) => true
wenzelm@49815
   217
              case _ => false }) =>
wenzelm@49356
   218
wenzelm@49356
   219
            props match {
wenzelm@49419
   220
              case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
wenzelm@49356
   221
                Isabelle_System.source_file(Path.explode(name)) match {
wenzelm@49356
   222
                  case Some(path) =>
wenzelm@49356
   223
                    val jedit_file = Isabelle_System.platform_path(path)
wenzelm@49356
   224
                    Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, line, 0)) :: links
wenzelm@49356
   225
                  case None => links
wenzelm@49356
   226
                }
wenzelm@49356
   227
wenzelm@49419
   228
              case Position.Def_Id_Offset(id, offset) =>
wenzelm@49356
   229
                snapshot.state.find_command(snapshot.version, id) match {
wenzelm@49356
   230
                  case Some((node, command)) =>
wenzelm@49356
   231
                    val sources =
wenzelm@49356
   232
                      node.commands.iterator.takeWhile(_ != command).map(_.source) ++
wenzelm@49356
   233
                        Iterator.single(command.source(Text.Range(0, command.decode(offset))))
wenzelm@49356
   234
                    val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@49356
   235
                    Text.Info(snapshot.convert(info_range),
wenzelm@49356
   236
                      Hyperlink(command.node_name.node, line, column)) :: links
wenzelm@49356
   237
                  case None => links
wenzelm@49356
   238
                }
wenzelm@49356
   239
wenzelm@49356
   240
              case _ => links
wenzelm@49356
   241
            }
wenzelm@49356
   242
        }) match { case Text.Info(_, info :: _) #:: _ => Some(info) case _ => None }
wenzelm@49356
   243
  }
wenzelm@49356
   244
wenzelm@49356
   245
wenzelm@50164
   246
  def sendback(range: Text.Range): Option[Text.Info[Option[Document.Exec_ID]]] =
wenzelm@50163
   247
    snapshot.select_markup(range, Some(Set(Isabelle_Markup.SENDBACK)),
wenzelm@50163
   248
        {
wenzelm@50164
   249
          case Text.Info(info_range, XML.Elem(Markup(Isabelle_Markup.SENDBACK, props), _)) =>
wenzelm@50164
   250
            Text.Info(snapshot.convert(info_range), Position.Id.unapply(props))
wenzelm@50163
   251
        }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
wenzelm@49492
   252
wenzelm@49492
   253
wenzelm@49700
   254
  def tooltip_message(range: Text.Range): XML.Body =
wenzelm@49356
   255
  {
wenzelm@49356
   256
    val msgs =
wenzelm@49700
   257
      snapshot.cumulate_markup[SortedMap[Long, XML.Tree]](range, SortedMap.empty,
wenzelm@49356
   258
        Some(Set(Isabelle_Markup.WRITELN, Isabelle_Markup.WARNING, Isabelle_Markup.ERROR,
wenzelm@49356
   259
          Isabelle_Markup.BAD)),
wenzelm@49356
   260
        {
wenzelm@49704
   261
          case (msgs, Text.Info(_,
wenzelm@49704
   262
            XML.Elem(Markup(name, props @ Isabelle_Markup.Serial(serial)), body)))
wenzelm@49704
   263
          if name == Isabelle_Markup.WRITELN ||
wenzelm@49704
   264
              name == Isabelle_Markup.WARNING ||
wenzelm@49704
   265
              name == Isabelle_Markup.ERROR =>
wenzelm@49704
   266
            msgs + (serial -> XML.Elem(Markup(Isabelle_Markup.message(name), props), body))
wenzelm@49356
   267
          case (msgs, Text.Info(_, msg @ XML.Elem(Markup(Isabelle_Markup.BAD, _), body)))
wenzelm@49700
   268
          if !body.isEmpty => msgs + (Document.new_id() -> msg)
wenzelm@49356
   269
        }).toList.flatMap(_.info)
wenzelm@49827
   270
    Pretty.separate(msgs.iterator.map(_._2).toList)
wenzelm@49356
   271
  }
wenzelm@49356
   272
wenzelm@49356
   273
wenzelm@49356
   274
  private val tooltips: Map[String, String] =
wenzelm@49356
   275
    Map(
wenzelm@49356
   276
      Isabelle_Markup.SORT -> "sort",
wenzelm@49356
   277
      Isabelle_Markup.TYP -> "type",
wenzelm@49356
   278
      Isabelle_Markup.TERM -> "term",
wenzelm@49356
   279
      Isabelle_Markup.PROP -> "proposition",
wenzelm@49356
   280
      Isabelle_Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@49356
   281
      Isabelle_Markup.FREE -> "free variable",
wenzelm@49356
   282
      Isabelle_Markup.SKOLEM -> "skolem variable",
wenzelm@49356
   283
      Isabelle_Markup.BOUND -> "bound variable",
wenzelm@49356
   284
      Isabelle_Markup.VAR -> "schematic variable",
wenzelm@49356
   285
      Isabelle_Markup.TFREE -> "free type variable",
wenzelm@49356
   286
      Isabelle_Markup.TVAR -> "schematic type variable",
wenzelm@49356
   287
      Isabelle_Markup.ML_SOURCE -> "ML source",
wenzelm@49356
   288
      Isabelle_Markup.DOCUMENT_SOURCE -> "document source")
wenzelm@49356
   289
wenzelm@49356
   290
  private val tooltip_elements =
wenzelm@49674
   291
    Set(Isabelle_Markup.ENTITY, Isabelle_Markup.SORTING, Isabelle_Markup.TYPING,
wenzelm@49674
   292
      Isabelle_Markup.ML_TYPING, Isabelle_Markup.PATH) ++ tooltips.keys
wenzelm@49356
   293
wenzelm@49700
   294
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@49700
   295
    Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
wenzelm@49356
   296
wenzelm@49700
   297
  def tooltip(range: Text.Range): XML.Body =
wenzelm@49356
   298
  {
wenzelm@49700
   299
    def add(prev: Text.Info[List[(Boolean, XML.Tree)]], r: Text.Range, p: (Boolean, XML.Tree)) =
wenzelm@49356
   300
      if (prev.range == r) Text.Info(r, p :: prev.info) else Text.Info(r, List(p))
wenzelm@49356
   301
wenzelm@49356
   302
    val tips =
wenzelm@49700
   303
      snapshot.cumulate_markup[Text.Info[(List[(Boolean, XML.Tree)])]](
wenzelm@49356
   304
        range, Text.Info(range, Nil), Some(tooltip_elements),
wenzelm@49356
   305
        {
wenzelm@49356
   306
          case (prev, Text.Info(r, XML.Elem(Isabelle_Markup.Entity(kind, name), _))) =>
wenzelm@49356
   307
            val kind1 = space_explode('_', kind).mkString(" ")
wenzelm@49700
   308
            add(prev, r, (true, XML.Text(kind1 + " " + quote(name))))
wenzelm@49356
   309
          case (prev, Text.Info(r, XML.Elem(Isabelle_Markup.Path(name), _)))
wenzelm@49356
   310
          if Path.is_ok(name) =>
wenzelm@49356
   311
            val jedit_file = Isabelle.thy_load.append(snapshot.node_name.dir, Path.explode(name))
wenzelm@49700
   312
            add(prev, r, (true, XML.Text("file " + quote(jedit_file))))
wenzelm@49674
   313
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@49674
   314
          if name == Isabelle_Markup.SORTING || name == Isabelle_Markup.TYPING =>
wenzelm@49700
   315
            add(prev, r, (true, pretty_typing("::", body)))
wenzelm@49356
   316
          case (prev, Text.Info(r, XML.Elem(Markup(Isabelle_Markup.ML_TYPING, _), body))) =>
wenzelm@49700
   317
            add(prev, r, (false, pretty_typing("ML:", body)))
wenzelm@49356
   318
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _)))
wenzelm@49700
   319
          if tooltips.isDefinedAt(name) => add(prev, r, (true, XML.Text(tooltips(name))))
wenzelm@49356
   320
        }).toList.flatMap(_.info.info)
wenzelm@49356
   321
wenzelm@49356
   322
    val all_tips =
wenzelm@49356
   323
      (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@49700
   324
    Library.separate(Pretty.FBreak, all_tips.toList)
wenzelm@49356
   325
  }
wenzelm@49356
   326
wenzelm@49356
   327
wenzelm@49356
   328
  def gutter_message(range: Text.Range): Option[Icon] =
wenzelm@49356
   329
  {
wenzelm@49356
   330
    val results =
wenzelm@49356
   331
      snapshot.cumulate_markup[Int](range, 0,
wenzelm@49356
   332
        Some(Set(Isabelle_Markup.WARNING, Isabelle_Markup.ERROR)),
wenzelm@49356
   333
        {
wenzelm@49356
   334
          case (pri, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.WARNING, _), body))) =>
wenzelm@49356
   335
            body match {
wenzelm@49356
   336
              case List(XML.Elem(Markup(Isabelle_Markup.LEGACY, _), _)) =>
wenzelm@50199
   337
                pri max Rendering.legacy_pri
wenzelm@50199
   338
              case _ => pri max Rendering.warning_pri
wenzelm@49356
   339
            }
wenzelm@49356
   340
          case (pri, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.ERROR, _), _))) =>
wenzelm@50199
   341
            pri max Rendering.error_pri
wenzelm@49356
   342
        })
wenzelm@49356
   343
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@50199
   344
    Rendering.gutter_icons.get(pri)
wenzelm@49356
   345
  }
wenzelm@49356
   346
wenzelm@49356
   347
wenzelm@49356
   348
  private val squiggly_colors = Map(
wenzelm@50199
   349
    Rendering.writeln_pri -> writeln_color,
wenzelm@50199
   350
    Rendering.warning_pri -> warning_color,
wenzelm@50199
   351
    Rendering.error_pri -> error_color)
wenzelm@49356
   352
wenzelm@49356
   353
  def squiggly_underline(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   354
  {
wenzelm@49356
   355
    val results =
wenzelm@49356
   356
      snapshot.cumulate_markup[Int](range, 0,
wenzelm@49356
   357
        Some(Set(Isabelle_Markup.WRITELN, Isabelle_Markup.WARNING, Isabelle_Markup.ERROR)),
wenzelm@49356
   358
        {
wenzelm@49474
   359
          case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
wenzelm@49474
   360
          if name == Isabelle_Markup.WRITELN ||
wenzelm@49474
   361
            name == Isabelle_Markup.WARNING ||
wenzelm@50199
   362
            name == Isabelle_Markup.ERROR => pri max Rendering.message_pri(name)
wenzelm@49356
   363
        })
wenzelm@49356
   364
    for {
wenzelm@49356
   365
      Text.Info(r, pri) <- results
wenzelm@49356
   366
      color <- squiggly_colors.get(pri)
wenzelm@49356
   367
    } yield Text.Info(r, color)
wenzelm@49356
   368
  }
wenzelm@49356
   369
wenzelm@49474
   370
wenzelm@49493
   371
  private val messages_include =
wenzelm@49493
   372
    Set(Isabelle_Markup.WRITELN_MESSAGE, Isabelle_Markup.TRACING_MESSAGE,
wenzelm@49493
   373
      Isabelle_Markup.WARNING_MESSAGE, Isabelle_Markup.ERROR_MESSAGE)
wenzelm@49493
   374
wenzelm@49474
   375
  private val message_colors = Map(
wenzelm@50199
   376
    Rendering.writeln_pri -> writeln_message_color,
wenzelm@50199
   377
    Rendering.tracing_pri -> tracing_message_color,
wenzelm@50199
   378
    Rendering.warning_pri -> warning_message_color,
wenzelm@50199
   379
    Rendering.error_pri -> error_message_color)
wenzelm@49474
   380
wenzelm@49473
   381
  def line_background(range: Text.Range): Option[(Color, Boolean)] =
wenzelm@49473
   382
  {
wenzelm@49474
   383
    val results =
wenzelm@49493
   384
      snapshot.cumulate_markup[Int](range, 0, Some(messages_include),
wenzelm@49473
   385
        {
wenzelm@49474
   386
          case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
wenzelm@49474
   387
          if name == Isabelle_Markup.WRITELN_MESSAGE ||
wenzelm@49474
   388
            name == Isabelle_Markup.TRACING_MESSAGE ||
wenzelm@49474
   389
            name == Isabelle_Markup.WARNING_MESSAGE ||
wenzelm@50199
   390
            name == Isabelle_Markup.ERROR_MESSAGE => pri max Rendering.message_pri(name)
wenzelm@49474
   391
        })
wenzelm@49474
   392
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@49474
   393
wenzelm@49474
   394
    val is_separator = pri > 0 &&
wenzelm@49474
   395
      snapshot.cumulate_markup[Boolean](range, false, Some(Set(Isabelle_Markup.SEPARATOR)),
wenzelm@49473
   396
        {
wenzelm@49473
   397
          case (_, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.SEPARATOR, _), _))) => true
wenzelm@49473
   398
        }).exists(_.info)
wenzelm@49473
   399
wenzelm@49474
   400
    message_colors.get(pri).map((_, is_separator))
wenzelm@49473
   401
  }
wenzelm@49356
   402
wenzelm@49492
   403
wenzelm@49492
   404
  private val background1_include =
wenzelm@49492
   405
    Protocol.command_status_markup + Isabelle_Markup.WRITELN_MESSAGE +
wenzelm@49492
   406
      Isabelle_Markup.TRACING_MESSAGE + Isabelle_Markup.WARNING_MESSAGE +
wenzelm@49492
   407
      Isabelle_Markup.ERROR_MESSAGE + Isabelle_Markup.BAD + Isabelle_Markup.INTENSIFY +
wenzelm@49492
   408
      Isabelle_Markup.SENDBACK
wenzelm@49492
   409
wenzelm@49356
   410
  def background1(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   411
  {
wenzelm@49356
   412
    if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
wenzelm@49356
   413
    else
wenzelm@49356
   414
      for {
wenzelm@49356
   415
        Text.Info(r, result) <-
wenzelm@49356
   416
          snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
wenzelm@49492
   417
            range, (Some(Protocol.Status.init), None), Some(background1_include),
wenzelm@49356
   418
            {
wenzelm@49356
   419
              case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@49356
   420
              if (Protocol.command_status_markup(markup.name)) =>
wenzelm@49356
   421
                (Some(Protocol.command_status(status, markup)), color)
wenzelm@49356
   422
              case (_, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.BAD, _), _))) =>
wenzelm@49356
   423
                (None, Some(bad_color))
wenzelm@49358
   424
              case (_, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.INTENSIFY, _), _))) =>
wenzelm@49358
   425
                (None, Some(intensify_color))
wenzelm@49492
   426
              case (_, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.SENDBACK, _), _))) =>
wenzelm@49492
   427
                (None, Some(sendback_color))
wenzelm@49356
   428
            })
wenzelm@49356
   429
        color <-
wenzelm@49356
   430
          (result match {
wenzelm@49356
   431
            case (Some(status), opt_color) =>
wenzelm@49356
   432
              if (status.is_unprocessed) Some(unprocessed1_color)
wenzelm@49356
   433
              else if (status.is_running) Some(running1_color)
wenzelm@49356
   434
              else opt_color
wenzelm@49356
   435
            case (_, opt_color) => opt_color
wenzelm@49356
   436
          })
wenzelm@49356
   437
      } yield Text.Info(r, color)
wenzelm@49356
   438
  }
wenzelm@49356
   439
wenzelm@49356
   440
wenzelm@49356
   441
  def background2(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   442
    snapshot.select_markup(range,
wenzelm@49356
   443
      Some(Set(Isabelle_Markup.TOKEN_RANGE)),
wenzelm@49356
   444
      {
wenzelm@49356
   445
        case Text.Info(_, XML.Elem(Markup(Isabelle_Markup.TOKEN_RANGE, _), _)) => light_color
wenzelm@49356
   446
      })
wenzelm@49356
   447
wenzelm@49356
   448
wenzelm@49356
   449
  def foreground(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   450
    snapshot.select_markup(range,
wenzelm@49356
   451
      Some(Set(Isabelle_Markup.STRING, Isabelle_Markup.ALTSTRING, Isabelle_Markup.VERBATIM)),
wenzelm@49356
   452
      {
wenzelm@49356
   453
        case Text.Info(_, XML.Elem(Markup(Isabelle_Markup.STRING, _), _)) => quoted_color
wenzelm@49356
   454
        case Text.Info(_, XML.Elem(Markup(Isabelle_Markup.ALTSTRING, _), _)) => quoted_color
wenzelm@49356
   455
        case Text.Info(_, XML.Elem(Markup(Isabelle_Markup.VERBATIM, _), _)) => quoted_color
wenzelm@49356
   456
      })
wenzelm@49356
   457
wenzelm@49356
   458
wenzelm@49356
   459
  private val text_colors: Map[String, Color] = Map(
wenzelm@49554
   460
      Isabelle_Markup.KEYWORD1 -> keyword1_color,
wenzelm@49554
   461
      Isabelle_Markup.KEYWORD2 -> keyword2_color,
wenzelm@49356
   462
      Isabelle_Markup.STRING -> Color.BLACK,
wenzelm@49356
   463
      Isabelle_Markup.ALTSTRING -> Color.BLACK,
wenzelm@49356
   464
      Isabelle_Markup.VERBATIM -> Color.BLACK,
wenzelm@49356
   465
      Isabelle_Markup.LITERAL -> keyword1_color,
wenzelm@49356
   466
      Isabelle_Markup.DELIMITER -> Color.BLACK,
wenzelm@49356
   467
      Isabelle_Markup.TFREE -> tfree_color,
wenzelm@49356
   468
      Isabelle_Markup.TVAR -> tvar_color,
wenzelm@49356
   469
      Isabelle_Markup.FREE -> free_color,
wenzelm@49356
   470
      Isabelle_Markup.SKOLEM -> skolem_color,
wenzelm@49356
   471
      Isabelle_Markup.BOUND -> bound_color,
wenzelm@49356
   472
      Isabelle_Markup.VAR -> var_color,
wenzelm@49356
   473
      Isabelle_Markup.INNER_STRING -> inner_quoted_color,
wenzelm@49356
   474
      Isabelle_Markup.INNER_COMMENT -> inner_comment_color,
wenzelm@49356
   475
      Isabelle_Markup.DYNAMIC_FACT -> dynamic_color,
wenzelm@49356
   476
      Isabelle_Markup.ML_KEYWORD -> keyword1_color,
wenzelm@49356
   477
      Isabelle_Markup.ML_DELIMITER -> Color.BLACK,
wenzelm@49356
   478
      Isabelle_Markup.ML_NUMERAL -> inner_numeral_color,
wenzelm@49356
   479
      Isabelle_Markup.ML_CHAR -> inner_quoted_color,
wenzelm@49356
   480
      Isabelle_Markup.ML_STRING -> inner_quoted_color,
wenzelm@49356
   481
      Isabelle_Markup.ML_COMMENT -> inner_comment_color,
wenzelm@49356
   482
      Isabelle_Markup.ANTIQ -> antiquotation_color)
wenzelm@49356
   483
wenzelm@49356
   484
  private val text_color_elements = Set.empty[String] ++ text_colors.keys
wenzelm@49356
   485
wenzelm@49356
   486
  def text_color(range: Text.Range, color: Color)
wenzelm@49356
   487
      : Stream[Text.Info[Color]] =
wenzelm@49356
   488
  {
wenzelm@50196
   489
    if (color == Token_Markup.hidden_color) Stream(Text.Info(range, color))
wenzelm@50196
   490
    else
wenzelm@50196
   491
      snapshot.cumulate_markup(range, color, Some(text_color_elements),
wenzelm@50196
   492
        {
wenzelm@50196
   493
          case (_, Text.Info(_, XML.Elem(Markup(m, _), _)))
wenzelm@50196
   494
          if text_colors.isDefinedAt(m) => text_colors(m)
wenzelm@50196
   495
        })
wenzelm@49356
   496
  }
wenzelm@49356
   497
}