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