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