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