src/Tools/jEdit/src/rendering.scala
author wenzelm
Sun Nov 25 20:59:32 2012 +0100 (2012-11-25)
changeset 50205 788c8263e634
parent 50202 ec0f2f8dbeb9
child 50206 6626bc5ed053
permissions -rw-r--r--
renamed main plugin object to PIDE;
wenzelm@50202
     1
/*  Title:      Tools/jEdit/src/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@50201
    37
    Markup.WRITELN -> writeln_pri, Markup.WRITELN_MESSAGE -> writeln_pri,
wenzelm@50201
    38
    Markup.TRACING -> tracing_pri, Markup.TRACING_MESSAGE -> tracing_pri,
wenzelm@50201
    39
    Markup.WARNING -> warning_pri, Markup.WARNING_MESSAGE -> warning_pri,
wenzelm@50201
    40
    Markup.ERROR -> error_pri, 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@50201
   159
          Some(Protocol.command_status_markup + Markup.WARNING + Markup.ERROR),
wenzelm@49356
   160
          {
wenzelm@49356
   161
            case ((status, pri), Text.Info(_, XML.Elem(markup, _))) =>
wenzelm@50201
   162
              if (markup.name == Markup.WARNING || markup.name == 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@50201
   186
    Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
wenzelm@50201
   187
      Markup.ENTITY, Markup.PATH, Markup.SORTING, Markup.TYPING, Markup.FREE, Markup.SKOLEM,
wenzelm@50201
   188
      Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOCUMENT_SOURCE)
wenzelm@49356
   189
wenzelm@49358
   190
  def highlight(range: Text.Range): Option[Text.Info[Color]] =
wenzelm@49356
   191
  {
wenzelm@49358
   192
    snapshot.select_markup(range, Some(highlight_include),
wenzelm@49356
   193
        {
wenzelm@49358
   194
          case Text.Info(info_range, XML.Elem(Markup(name, _), _)) if highlight_include(name) =>
wenzelm@49358
   195
            Text.Info(snapshot.convert(info_range), highlight_color)
wenzelm@49356
   196
        }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
wenzelm@49356
   197
  }
wenzelm@49356
   198
wenzelm@49356
   199
wenzelm@50201
   200
  private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH)
wenzelm@49356
   201
wenzelm@49356
   202
  def hyperlink(range: Text.Range): Option[Text.Info[Hyperlink]] =
wenzelm@49356
   203
  {
wenzelm@49356
   204
    snapshot.cumulate_markup[List[Text.Info[Hyperlink]]](range, Nil, Some(hyperlink_include),
wenzelm@49356
   205
        {
wenzelm@50201
   206
          case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
wenzelm@49356
   207
          if Path.is_ok(name) =>
wenzelm@50205
   208
            val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
wenzelm@49356
   209
            Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, 0, 0)) :: links
wenzelm@49356
   210
wenzelm@50201
   211
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
wenzelm@49815
   212
          if !props.exists(
wenzelm@50201
   213
            { case (Markup.KIND, Markup.ML_OPEN) => true
wenzelm@50201
   214
              case (Markup.KIND, Markup.ML_STRUCT) => true
wenzelm@49815
   215
              case _ => false }) =>
wenzelm@49356
   216
wenzelm@49356
   217
            props match {
wenzelm@49419
   218
              case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
wenzelm@49356
   219
                Isabelle_System.source_file(Path.explode(name)) match {
wenzelm@49356
   220
                  case Some(path) =>
wenzelm@49356
   221
                    val jedit_file = Isabelle_System.platform_path(path)
wenzelm@49356
   222
                    Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, line, 0)) :: links
wenzelm@49356
   223
                  case None => links
wenzelm@49356
   224
                }
wenzelm@49356
   225
wenzelm@49419
   226
              case Position.Def_Id_Offset(id, offset) =>
wenzelm@49356
   227
                snapshot.state.find_command(snapshot.version, id) match {
wenzelm@49356
   228
                  case Some((node, command)) =>
wenzelm@49356
   229
                    val sources =
wenzelm@49356
   230
                      node.commands.iterator.takeWhile(_ != command).map(_.source) ++
wenzelm@49356
   231
                        Iterator.single(command.source(Text.Range(0, command.decode(offset))))
wenzelm@49356
   232
                    val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@49356
   233
                    Text.Info(snapshot.convert(info_range),
wenzelm@49356
   234
                      Hyperlink(command.node_name.node, line, column)) :: links
wenzelm@49356
   235
                  case None => links
wenzelm@49356
   236
                }
wenzelm@49356
   237
wenzelm@49356
   238
              case _ => links
wenzelm@49356
   239
            }
wenzelm@49356
   240
        }) match { case Text.Info(_, info :: _) #:: _ => Some(info) case _ => None }
wenzelm@49356
   241
  }
wenzelm@49356
   242
wenzelm@49356
   243
wenzelm@50164
   244
  def sendback(range: Text.Range): Option[Text.Info[Option[Document.Exec_ID]]] =
wenzelm@50201
   245
    snapshot.select_markup(range, Some(Set(Markup.SENDBACK)),
wenzelm@50163
   246
        {
wenzelm@50201
   247
          case Text.Info(info_range, XML.Elem(Markup(Markup.SENDBACK, props), _)) =>
wenzelm@50164
   248
            Text.Info(snapshot.convert(info_range), Position.Id.unapply(props))
wenzelm@50163
   249
        }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
wenzelm@49492
   250
wenzelm@49492
   251
wenzelm@49700
   252
  def tooltip_message(range: Text.Range): XML.Body =
wenzelm@49356
   253
  {
wenzelm@49356
   254
    val msgs =
wenzelm@49700
   255
      snapshot.cumulate_markup[SortedMap[Long, XML.Tree]](range, SortedMap.empty,
wenzelm@50201
   256
        Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)),
wenzelm@49356
   257
        {
wenzelm@50201
   258
          case (msgs, Text.Info(_, XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
wenzelm@50201
   259
          if name == Markup.WRITELN ||
wenzelm@50201
   260
              name == Markup.WARNING ||
wenzelm@50201
   261
              name == Markup.ERROR =>
wenzelm@50201
   262
            msgs + (serial -> XML.Elem(Markup(Markup.message(name), props), body))
wenzelm@50201
   263
          case (msgs, Text.Info(_, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
wenzelm@49700
   264
          if !body.isEmpty => msgs + (Document.new_id() -> msg)
wenzelm@49356
   265
        }).toList.flatMap(_.info)
wenzelm@49827
   266
    Pretty.separate(msgs.iterator.map(_._2).toList)
wenzelm@49356
   267
  }
wenzelm@49356
   268
wenzelm@49356
   269
wenzelm@49356
   270
  private val tooltips: Map[String, String] =
wenzelm@49356
   271
    Map(
wenzelm@50201
   272
      Markup.SORT -> "sort",
wenzelm@50201
   273
      Markup.TYP -> "type",
wenzelm@50201
   274
      Markup.TERM -> "term",
wenzelm@50201
   275
      Markup.PROP -> "proposition",
wenzelm@50201
   276
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@50201
   277
      Markup.FREE -> "free variable",
wenzelm@50201
   278
      Markup.SKOLEM -> "skolem variable",
wenzelm@50201
   279
      Markup.BOUND -> "bound variable",
wenzelm@50201
   280
      Markup.VAR -> "schematic variable",
wenzelm@50201
   281
      Markup.TFREE -> "free type variable",
wenzelm@50201
   282
      Markup.TVAR -> "schematic type variable",
wenzelm@50201
   283
      Markup.ML_SOURCE -> "ML source",
wenzelm@50201
   284
      Markup.DOCUMENT_SOURCE -> "document source")
wenzelm@49356
   285
wenzelm@49356
   286
  private val tooltip_elements =
wenzelm@50201
   287
    Set(Markup.ENTITY, Markup.SORTING, Markup.TYPING, Markup.ML_TYPING, Markup.PATH) ++
wenzelm@50201
   288
      tooltips.keys
wenzelm@49356
   289
wenzelm@49700
   290
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@49700
   291
    Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
wenzelm@49356
   292
wenzelm@49700
   293
  def tooltip(range: Text.Range): XML.Body =
wenzelm@49356
   294
  {
wenzelm@49700
   295
    def add(prev: Text.Info[List[(Boolean, XML.Tree)]], r: Text.Range, p: (Boolean, XML.Tree)) =
wenzelm@49356
   296
      if (prev.range == r) Text.Info(r, p :: prev.info) else Text.Info(r, List(p))
wenzelm@49356
   297
wenzelm@49356
   298
    val tips =
wenzelm@49700
   299
      snapshot.cumulate_markup[Text.Info[(List[(Boolean, XML.Tree)])]](
wenzelm@49356
   300
        range, Text.Info(range, Nil), Some(tooltip_elements),
wenzelm@49356
   301
        {
wenzelm@50201
   302
          case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
wenzelm@49356
   303
            val kind1 = space_explode('_', kind).mkString(" ")
wenzelm@49700
   304
            add(prev, r, (true, XML.Text(kind1 + " " + quote(name))))
wenzelm@50201
   305
          case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
wenzelm@49356
   306
          if Path.is_ok(name) =>
wenzelm@50205
   307
            val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
wenzelm@49700
   308
            add(prev, r, (true, XML.Text("file " + quote(jedit_file))))
wenzelm@49674
   309
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@50201
   310
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@49700
   311
            add(prev, r, (true, pretty_typing("::", body)))
wenzelm@50201
   312
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@49700
   313
            add(prev, r, (false, pretty_typing("ML:", body)))
wenzelm@49356
   314
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _)))
wenzelm@49700
   315
          if tooltips.isDefinedAt(name) => add(prev, r, (true, XML.Text(tooltips(name))))
wenzelm@49356
   316
        }).toList.flatMap(_.info.info)
wenzelm@49356
   317
wenzelm@49356
   318
    val all_tips =
wenzelm@49356
   319
      (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@49700
   320
    Library.separate(Pretty.FBreak, all_tips.toList)
wenzelm@49356
   321
  }
wenzelm@49356
   322
wenzelm@49356
   323
wenzelm@49356
   324
  def gutter_message(range: Text.Range): Option[Icon] =
wenzelm@49356
   325
  {
wenzelm@49356
   326
    val results =
wenzelm@50201
   327
      snapshot.cumulate_markup[Int](range, 0, Some(Set(Markup.WARNING, Markup.ERROR)),
wenzelm@49356
   328
        {
wenzelm@50201
   329
          case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
wenzelm@49356
   330
            body match {
wenzelm@50201
   331
              case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
wenzelm@50199
   332
                pri max Rendering.legacy_pri
wenzelm@50199
   333
              case _ => pri max Rendering.warning_pri
wenzelm@49356
   334
            }
wenzelm@50201
   335
          case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
wenzelm@50199
   336
            pri max Rendering.error_pri
wenzelm@49356
   337
        })
wenzelm@49356
   338
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@50199
   339
    Rendering.gutter_icons.get(pri)
wenzelm@49356
   340
  }
wenzelm@49356
   341
wenzelm@49356
   342
wenzelm@49356
   343
  private val squiggly_colors = Map(
wenzelm@50199
   344
    Rendering.writeln_pri -> writeln_color,
wenzelm@50199
   345
    Rendering.warning_pri -> warning_color,
wenzelm@50199
   346
    Rendering.error_pri -> error_color)
wenzelm@49356
   347
wenzelm@49356
   348
  def squiggly_underline(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   349
  {
wenzelm@49356
   350
    val results =
wenzelm@49356
   351
      snapshot.cumulate_markup[Int](range, 0,
wenzelm@50201
   352
        Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)),
wenzelm@49356
   353
        {
wenzelm@49474
   354
          case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
wenzelm@50201
   355
          if name == Markup.WRITELN ||
wenzelm@50201
   356
            name == Markup.WARNING ||
wenzelm@50201
   357
            name == Markup.ERROR => pri max Rendering.message_pri(name)
wenzelm@49356
   358
        })
wenzelm@49356
   359
    for {
wenzelm@49356
   360
      Text.Info(r, pri) <- results
wenzelm@49356
   361
      color <- squiggly_colors.get(pri)
wenzelm@49356
   362
    } yield Text.Info(r, color)
wenzelm@49356
   363
  }
wenzelm@49356
   364
wenzelm@49474
   365
wenzelm@49493
   366
  private val messages_include =
wenzelm@50201
   367
    Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE, Markup.ERROR_MESSAGE)
wenzelm@49493
   368
wenzelm@49474
   369
  private val message_colors = Map(
wenzelm@50199
   370
    Rendering.writeln_pri -> writeln_message_color,
wenzelm@50199
   371
    Rendering.tracing_pri -> tracing_message_color,
wenzelm@50199
   372
    Rendering.warning_pri -> warning_message_color,
wenzelm@50199
   373
    Rendering.error_pri -> error_message_color)
wenzelm@49474
   374
wenzelm@49473
   375
  def line_background(range: Text.Range): Option[(Color, Boolean)] =
wenzelm@49473
   376
  {
wenzelm@49474
   377
    val results =
wenzelm@49493
   378
      snapshot.cumulate_markup[Int](range, 0, Some(messages_include),
wenzelm@49473
   379
        {
wenzelm@49474
   380
          case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
wenzelm@50201
   381
          if name == Markup.WRITELN_MESSAGE ||
wenzelm@50201
   382
            name == Markup.TRACING_MESSAGE ||
wenzelm@50201
   383
            name == Markup.WARNING_MESSAGE ||
wenzelm@50201
   384
            name == Markup.ERROR_MESSAGE => pri max Rendering.message_pri(name)
wenzelm@49474
   385
        })
wenzelm@49474
   386
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@49474
   387
wenzelm@49474
   388
    val is_separator = pri > 0 &&
wenzelm@50201
   389
      snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)),
wenzelm@49473
   390
        {
wenzelm@50201
   391
          case (_, Text.Info(_, XML.Elem(Markup(Markup.SEPARATOR, _), _))) => true
wenzelm@49473
   392
        }).exists(_.info)
wenzelm@49473
   393
wenzelm@49474
   394
    message_colors.get(pri).map((_, is_separator))
wenzelm@49473
   395
  }
wenzelm@49356
   396
wenzelm@49492
   397
wenzelm@49492
   398
  private val background1_include =
wenzelm@50201
   399
    Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
wenzelm@50201
   400
      Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY +
wenzelm@50201
   401
      Markup.SENDBACK
wenzelm@49492
   402
wenzelm@49356
   403
  def background1(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   404
  {
wenzelm@49356
   405
    if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
wenzelm@49356
   406
    else
wenzelm@49356
   407
      for {
wenzelm@49356
   408
        Text.Info(r, result) <-
wenzelm@49356
   409
          snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
wenzelm@49492
   410
            range, (Some(Protocol.Status.init), None), Some(background1_include),
wenzelm@49356
   411
            {
wenzelm@49356
   412
              case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@49356
   413
              if (Protocol.command_status_markup(markup.name)) =>
wenzelm@49356
   414
                (Some(Protocol.command_status(status, markup)), color)
wenzelm@50201
   415
              case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
wenzelm@49356
   416
                (None, Some(bad_color))
wenzelm@50201
   417
              case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
wenzelm@49358
   418
                (None, Some(intensify_color))
wenzelm@50201
   419
              case (_, Text.Info(_, XML.Elem(Markup(Markup.SENDBACK, _), _))) =>
wenzelm@49492
   420
                (None, Some(sendback_color))
wenzelm@49356
   421
            })
wenzelm@49356
   422
        color <-
wenzelm@49356
   423
          (result match {
wenzelm@49356
   424
            case (Some(status), opt_color) =>
wenzelm@49356
   425
              if (status.is_unprocessed) Some(unprocessed1_color)
wenzelm@49356
   426
              else if (status.is_running) Some(running1_color)
wenzelm@49356
   427
              else opt_color
wenzelm@49356
   428
            case (_, opt_color) => opt_color
wenzelm@49356
   429
          })
wenzelm@49356
   430
      } yield Text.Info(r, color)
wenzelm@49356
   431
  }
wenzelm@49356
   432
wenzelm@49356
   433
wenzelm@49356
   434
  def background2(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@50201
   435
    snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)),
wenzelm@49356
   436
      {
wenzelm@50201
   437
        case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => light_color
wenzelm@49356
   438
      })
wenzelm@49356
   439
wenzelm@49356
   440
wenzelm@49356
   441
  def foreground(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@50201
   442
    snapshot.select_markup(range, Some(Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM)),
wenzelm@49356
   443
      {
wenzelm@50201
   444
        case Text.Info(_, XML.Elem(Markup(Markup.STRING, _), _)) => quoted_color
wenzelm@50201
   445
        case Text.Info(_, XML.Elem(Markup(Markup.ALTSTRING, _), _)) => quoted_color
wenzelm@50201
   446
        case Text.Info(_, XML.Elem(Markup(Markup.VERBATIM, _), _)) => quoted_color
wenzelm@49356
   447
      })
wenzelm@49356
   448
wenzelm@49356
   449
wenzelm@49356
   450
  private val text_colors: Map[String, Color] = Map(
wenzelm@50201
   451
      Markup.KEYWORD1 -> keyword1_color,
wenzelm@50201
   452
      Markup.KEYWORD2 -> keyword2_color,
wenzelm@50201
   453
      Markup.STRING -> Color.BLACK,
wenzelm@50201
   454
      Markup.ALTSTRING -> Color.BLACK,
wenzelm@50201
   455
      Markup.VERBATIM -> Color.BLACK,
wenzelm@50201
   456
      Markup.LITERAL -> keyword1_color,
wenzelm@50201
   457
      Markup.DELIMITER -> Color.BLACK,
wenzelm@50201
   458
      Markup.TFREE -> tfree_color,
wenzelm@50201
   459
      Markup.TVAR -> tvar_color,
wenzelm@50201
   460
      Markup.FREE -> free_color,
wenzelm@50201
   461
      Markup.SKOLEM -> skolem_color,
wenzelm@50201
   462
      Markup.BOUND -> bound_color,
wenzelm@50201
   463
      Markup.VAR -> var_color,
wenzelm@50201
   464
      Markup.INNER_STRING -> inner_quoted_color,
wenzelm@50201
   465
      Markup.INNER_COMMENT -> inner_comment_color,
wenzelm@50201
   466
      Markup.DYNAMIC_FACT -> dynamic_color,
wenzelm@50201
   467
      Markup.ML_KEYWORD -> keyword1_color,
wenzelm@50201
   468
      Markup.ML_DELIMITER -> Color.BLACK,
wenzelm@50201
   469
      Markup.ML_NUMERAL -> inner_numeral_color,
wenzelm@50201
   470
      Markup.ML_CHAR -> inner_quoted_color,
wenzelm@50201
   471
      Markup.ML_STRING -> inner_quoted_color,
wenzelm@50201
   472
      Markup.ML_COMMENT -> inner_comment_color,
wenzelm@50201
   473
      Markup.ANTIQ -> antiquotation_color)
wenzelm@49356
   474
wenzelm@49356
   475
  private val text_color_elements = Set.empty[String] ++ text_colors.keys
wenzelm@49356
   476
wenzelm@49356
   477
  def text_color(range: Text.Range, color: Color)
wenzelm@49356
   478
      : Stream[Text.Info[Color]] =
wenzelm@49356
   479
  {
wenzelm@50196
   480
    if (color == Token_Markup.hidden_color) Stream(Text.Info(range, color))
wenzelm@50196
   481
    else
wenzelm@50196
   482
      snapshot.cumulate_markup(range, color, Some(text_color_elements),
wenzelm@50196
   483
        {
wenzelm@50196
   484
          case (_, Text.Info(_, XML.Elem(Markup(m, _), _)))
wenzelm@50196
   485
          if text_colors.isDefinedAt(m) => text_colors(m)
wenzelm@50196
   486
        })
wenzelm@49356
   487
  }
wenzelm@49356
   488
}