src/Tools/jEdit/src/rendering.scala
author wenzelm
Wed Aug 07 14:13:59 2013 +0200 (2013-08-07)
changeset 52890 36e2c0c308eb
parent 52889 ea3338812e67
child 52900 d29bf6db8a2d
permissions -rw-r--r--
tuned signature;
tuned;
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@52890
   157
            case ((status, pri), Text.Info(_, elem)) =>
wenzelm@52890
   158
              if (elem.name == Markup.WARNING || elem.name == Markup.ERROR)
wenzelm@52890
   159
                Some((status, pri max Rendering.message_pri(elem.name)))
wenzelm@52890
   160
              else if (overview_include(elem.name))
wenzelm@52890
   161
                Some((Protocol.command_status(status, elem.markup), pri))
wenzelm@52889
   162
              else None
wenzelm@49356
   163
          })
wenzelm@49356
   164
      if (results.isEmpty) None
wenzelm@49356
   165
      else {
wenzelm@49356
   166
        val (status, pri) =
wenzelm@49356
   167
          ((Protocol.Status.init, 0) /: results) {
wenzelm@49356
   168
            case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
wenzelm@49356
   169
wenzelm@50895
   170
        if (status.is_running) Some(running_color)
wenzelm@50895
   171
        else if (pri == Rendering.warning_pri) Some(warning_color)
wenzelm@50199
   172
        else if (pri == Rendering.error_pri) Some(error_color)
wenzelm@49356
   173
        else if (status.is_unprocessed) Some(unprocessed_color)
wenzelm@49356
   174
        else if (status.is_failed) Some(error_color)
wenzelm@49356
   175
        else None
wenzelm@49356
   176
      }
wenzelm@49356
   177
    }
wenzelm@49356
   178
  }
wenzelm@49356
   179
wenzelm@49356
   180
wenzelm@49356
   181
  /* markup selectors */
wenzelm@49356
   182
wenzelm@49358
   183
  private val highlight_include =
wenzelm@50201
   184
    Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
wenzelm@50201
   185
      Markup.ENTITY, Markup.PATH, Markup.SORTING, Markup.TYPING, Markup.FREE, Markup.SKOLEM,
wenzelm@50201
   186
      Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOCUMENT_SOURCE)
wenzelm@49356
   187
wenzelm@49358
   188
  def highlight(range: Text.Range): Option[Text.Info[Color]] =
wenzelm@49356
   189
  {
wenzelm@50500
   190
    snapshot.select_markup(range, Some(highlight_include), _ =>
wenzelm@49356
   191
        {
wenzelm@52890
   192
          case Text.Info(info_range, elem) =>
wenzelm@52890
   193
            if (highlight_include(elem.name))
wenzelm@52889
   194
              Some(Text.Info(snapshot.convert(info_range), highlight_color))
wenzelm@52889
   195
            else None
wenzelm@49356
   196
        }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
wenzelm@49356
   197
  }
wenzelm@49356
   198
wenzelm@49356
   199
wenzelm@50201
   200
  private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH)
wenzelm@49356
   201
wenzelm@49356
   202
  def hyperlink(range: Text.Range): Option[Text.Info[Hyperlink]] =
wenzelm@49356
   203
  {
wenzelm@50500
   204
    snapshot.cumulate_markup[List[Text.Info[Hyperlink]]](range, Nil, Some(hyperlink_include), _ =>
wenzelm@49356
   205
        {
wenzelm@50201
   206
          case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
wenzelm@49356
   207
          if Path.is_ok(name) =>
wenzelm@50205
   208
            val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
wenzelm@52889
   209
            val link = Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, 0, 0))
wenzelm@52889
   210
            Some(link :: links)
wenzelm@49356
   211
wenzelm@50201
   212
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
wenzelm@49815
   213
          if !props.exists(
wenzelm@50201
   214
            { case (Markup.KIND, Markup.ML_OPEN) => true
wenzelm@50201
   215
              case (Markup.KIND, Markup.ML_STRUCT) => true
wenzelm@49815
   216
              case _ => false }) =>
wenzelm@49356
   217
wenzelm@49356
   218
            props match {
wenzelm@49419
   219
              case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
wenzelm@49356
   220
                Isabelle_System.source_file(Path.explode(name)) match {
wenzelm@49356
   221
                  case Some(path) =>
wenzelm@49356
   222
                    val jedit_file = Isabelle_System.platform_path(path)
wenzelm@52889
   223
                    val link =
wenzelm@52889
   224
                      Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, line, 0))
wenzelm@52889
   225
                    Some(link :: links)
wenzelm@52889
   226
                  case None => None
wenzelm@49356
   227
                }
wenzelm@49356
   228
wenzelm@49419
   229
              case Position.Def_Id_Offset(id, offset) =>
wenzelm@49356
   230
                snapshot.state.find_command(snapshot.version, id) match {
wenzelm@49356
   231
                  case Some((node, command)) =>
wenzelm@49356
   232
                    val sources =
wenzelm@49356
   233
                      node.commands.iterator.takeWhile(_ != command).map(_.source) ++
wenzelm@49356
   234
                        Iterator.single(command.source(Text.Range(0, command.decode(offset))))
wenzelm@49356
   235
                    val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@52889
   236
                    val link =
wenzelm@52889
   237
                      Text.Info(snapshot.convert(info_range),
wenzelm@52889
   238
                        Hyperlink(command.node_name.node, line, column))
wenzelm@52889
   239
                    Some(link :: links)
wenzelm@52889
   240
                  case None => None
wenzelm@49356
   241
                }
wenzelm@49356
   242
wenzelm@52889
   243
              case _ => None
wenzelm@49356
   244
            }
wenzelm@52889
   245
wenzelm@52889
   246
          case _ => None
wenzelm@49356
   247
        }) match { case Text.Info(_, info :: _) #:: _ => Some(info) case _ => None }
wenzelm@49356
   248
  }
wenzelm@49356
   249
wenzelm@49356
   250
wenzelm@50715
   251
  private val active_include =
wenzelm@50715
   252
    Set(Markup.BROWSER, Markup.GRAPHVIEW, Markup.SENDBACK, Markup.DIALOG)
wenzelm@50450
   253
wenzelm@50450
   254
  def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
wenzelm@50500
   255
    snapshot.select_markup(range, Some(active_include), command_state =>
wenzelm@50501
   256
        {
wenzelm@50501
   257
          case Text.Info(info_range, elem @ Protocol.Dialog(_, serial, _))
wenzelm@50507
   258
          if !command_state.results.defined(serial) =>
wenzelm@52889
   259
            Some(Text.Info(snapshot.convert(info_range), elem))
wenzelm@52890
   260
          case Text.Info(info_range, elem) =>
wenzelm@52890
   261
            if (elem.name == Markup.BROWSER ||
wenzelm@52890
   262
                elem.name == Markup.GRAPHVIEW ||
wenzelm@52890
   263
                elem.name == Markup.SENDBACK)
wenzelm@52889
   264
              Some(Text.Info(snapshot.convert(info_range), elem))
wenzelm@52889
   265
            else None
wenzelm@50163
   266
        }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
wenzelm@49492
   267
wenzelm@49492
   268
wenzelm@50502
   269
  def command_results(range: Text.Range): Command.Results =
wenzelm@50502
   270
  {
wenzelm@50502
   271
    val results =
wenzelm@50502
   272
      snapshot.select_markup[Command.Results](range, None, command_state =>
wenzelm@52889
   273
        { case _ => Some(command_state.results) }).map(_.info)
wenzelm@50507
   274
    (Command.Results.empty /: results)(_ ++ _)
wenzelm@50502
   275
  }
wenzelm@50502
   276
wenzelm@51496
   277
  def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@49356
   278
  {
wenzelm@51496
   279
    val results =
wenzelm@51496
   280
      snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
wenzelm@51496
   281
        Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
wenzelm@51496
   282
        {
wenzelm@51496
   283
          case (msgs, Text.Info(info_range,
wenzelm@51496
   284
            XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
wenzelm@51496
   285
          if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
wenzelm@51496
   286
            val entry: Command.Results.Entry =
wenzelm@51496
   287
              (serial -> XML.Elem(Markup(Markup.message(name), props), body))
wenzelm@52889
   288
            Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
wenzelm@51496
   289
wenzelm@51496
   290
          case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
wenzelm@51496
   291
          if !body.isEmpty =>
wenzelm@52530
   292
            val entry: Command.Results.Entry = (Document_ID.make(), msg)
wenzelm@52889
   293
            Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
wenzelm@52889
   294
wenzelm@52889
   295
          case _ => None
wenzelm@51496
   296
        }).toList.flatMap(_.info)
wenzelm@51496
   297
    if (results.isEmpty) None
wenzelm@51496
   298
    else {
wenzelm@51496
   299
      val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@51496
   300
      val msgs = Command.Results.make(results.map(_.info))
wenzelm@51496
   301
      Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
wenzelm@51496
   302
    }
wenzelm@49356
   303
  }
wenzelm@49356
   304
wenzelm@49356
   305
wenzelm@49356
   306
  private val tooltips: Map[String, String] =
wenzelm@49356
   307
    Map(
wenzelm@50201
   308
      Markup.SORT -> "sort",
wenzelm@50201
   309
      Markup.TYP -> "type",
wenzelm@50201
   310
      Markup.TERM -> "term",
wenzelm@50201
   311
      Markup.PROP -> "proposition",
wenzelm@50201
   312
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@50201
   313
      Markup.FREE -> "free variable",
wenzelm@50201
   314
      Markup.SKOLEM -> "skolem variable",
wenzelm@50201
   315
      Markup.BOUND -> "bound variable",
wenzelm@50201
   316
      Markup.VAR -> "schematic variable",
wenzelm@50201
   317
      Markup.TFREE -> "free type variable",
wenzelm@50201
   318
      Markup.TVAR -> "schematic type variable",
wenzelm@50201
   319
      Markup.ML_SOURCE -> "ML source",
wenzelm@50201
   320
      Markup.DOCUMENT_SOURCE -> "document source")
wenzelm@49356
   321
wenzelm@49356
   322
  private val tooltip_elements =
wenzelm@51588
   323
    Set(Markup.TIMING, Markup.ENTITY, Markup.SORTING, Markup.TYPING,
wenzelm@51588
   324
      Markup.ML_TYPING, Markup.PATH) ++ tooltips.keys
wenzelm@49356
   325
wenzelm@49700
   326
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@49700
   327
    Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
wenzelm@49356
   328
wenzelm@51588
   329
  private val timing_threshold: Double = options.real("jedit_timing_threshold")
wenzelm@51588
   330
wenzelm@51496
   331
  def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@49356
   332
  {
wenzelm@51588
   333
    def add(prev: Text.Info[(Timing, List[(Boolean, XML.Tree)])],
wenzelm@51588
   334
      r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, List[(Boolean, XML.Tree)])] =
wenzelm@51496
   335
    {
wenzelm@51496
   336
      val r = snapshot.convert(r0)
wenzelm@51588
   337
      val (t, info) = prev.info
wenzelm@51588
   338
      if (prev.range == r) Text.Info(r, (t, p :: info)) else Text.Info(r, (t, List(p)))
wenzelm@51496
   339
    }
wenzelm@49356
   340
wenzelm@51496
   341
    val results =
wenzelm@51588
   342
      snapshot.cumulate_markup[Text.Info[(Timing, List[(Boolean, XML.Tree)])]](
wenzelm@51588
   343
        range, Text.Info(range, (Timing.zero, Nil)), Some(tooltip_elements), _ =>
wenzelm@49356
   344
        {
wenzelm@51588
   345
          case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
wenzelm@52889
   346
            Some(Text.Info(r, (t1 + t2, info)))
wenzelm@50201
   347
          case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
wenzelm@49356
   348
            val kind1 = space_explode('_', kind).mkString(" ")
wenzelm@51588
   349
            val txt1 = kind1 + " " + quote(name)
wenzelm@51588
   350
            val t = prev.info._1
wenzelm@51588
   351
            val txt2 =
wenzelm@51588
   352
              if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
wenzelm@51588
   353
                "\n" + t.message
wenzelm@51588
   354
              else ""
wenzelm@52889
   355
            Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
wenzelm@50201
   356
          case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
wenzelm@49356
   357
          if Path.is_ok(name) =>
wenzelm@50205
   358
            val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
wenzelm@52889
   359
            Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
wenzelm@49674
   360
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@50201
   361
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@52889
   362
            Some(add(prev, r, (true, pretty_typing("::", body))))
wenzelm@50201
   363
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@52889
   364
            Some(add(prev, r, (false, pretty_typing("ML:", body))))
wenzelm@52889
   365
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
wenzelm@52889
   366
            if (tooltips.isDefinedAt(name))
wenzelm@52889
   367
              Some(add(prev, r, (true, XML.Text(tooltips(name)))))
wenzelm@52889
   368
            else None
wenzelm@51496
   369
        }).toList.map(_.info)
wenzelm@49356
   370
wenzelm@51588
   371
    results.map(_.info).flatMap(_._2) match {
wenzelm@51496
   372
      case Nil => None
wenzelm@51496
   373
      case tips =>
wenzelm@51496
   374
        val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@51496
   375
        val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@51496
   376
        Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
wenzelm@51496
   377
    }
wenzelm@49356
   378
  }
wenzelm@49356
   379
wenzelm@50554
   380
  val tooltip_margin: Int = options.int("jedit_tooltip_margin")
wenzelm@50554
   381
  val tooltip_bounds: Double = (options.real("jedit_tooltip_bounds") max 0.2) min 0.8
wenzelm@50554
   382
wenzelm@52873
   383
  lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
wenzelm@52873
   384
  lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
wenzelm@52472
   385
wenzelm@52472
   386
wenzelm@52472
   387
  private lazy val gutter_icons = Map(
wenzelm@52873
   388
    Rendering.information_pri -> JEdit_Lib.load_icon(options.string("gutter_information_icon")),
wenzelm@52873
   389
    Rendering.warning_pri -> JEdit_Lib.load_icon(options.string("gutter_warning_icon")),
wenzelm@52873
   390
    Rendering.legacy_pri -> JEdit_Lib.load_icon(options.string("gutter_legacy_icon")),
wenzelm@52873
   391
    Rendering.error_pri -> JEdit_Lib.load_icon(options.string("gutter_error_icon")))
wenzelm@49356
   392
wenzelm@52644
   393
  private val gutter_elements = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
wenzelm@52644
   394
wenzelm@49356
   395
  def gutter_message(range: Text.Range): Option[Icon] =
wenzelm@49356
   396
  {
wenzelm@49356
   397
    val results =
wenzelm@52644
   398
      snapshot.cumulate_markup[Int](range, 0, Some(gutter_elements), _ =>
wenzelm@49356
   399
        {
wenzelm@52644
   400
          case (pri, Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@52644
   401
              List(XML.Elem(Markup(Markup.INFORMATION, _), _))))) =>
wenzelm@52889
   402
            Some(pri max Rendering.information_pri)
wenzelm@50201
   403
          case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
wenzelm@49356
   404
            body match {
wenzelm@50201
   405
              case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
wenzelm@52889
   406
                Some(pri max Rendering.legacy_pri)
wenzelm@52889
   407
              case _ =>
wenzelm@52889
   408
                Some(pri max Rendering.warning_pri)
wenzelm@49356
   409
            }
wenzelm@50201
   410
          case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
wenzelm@52889
   411
            Some(pri max Rendering.error_pri)
wenzelm@52889
   412
          case _ => None
wenzelm@49356
   413
        })
wenzelm@49356
   414
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@52472
   415
    gutter_icons.get(pri)
wenzelm@49356
   416
  }
wenzelm@49356
   417
wenzelm@49356
   418
wenzelm@49356
   419
  private val squiggly_colors = Map(
wenzelm@50199
   420
    Rendering.writeln_pri -> writeln_color,
wenzelm@52650
   421
    Rendering.information_pri -> information_color,
wenzelm@50199
   422
    Rendering.warning_pri -> warning_color,
wenzelm@50199
   423
    Rendering.error_pri -> error_color)
wenzelm@49356
   424
wenzelm@52889
   425
  private val squiggly_include = Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)
wenzelm@52644
   426
wenzelm@49356
   427
  def squiggly_underline(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   428
  {
wenzelm@49356
   429
    val results =
wenzelm@52889
   430
      snapshot.cumulate_markup[Int](range, 0, Some(squiggly_include), _ =>
wenzelm@49356
   431
        {
wenzelm@52890
   432
          case (pri, Text.Info(_, elem)) =>
wenzelm@52890
   433
            if (Protocol.is_information(elem))
wenzelm@52889
   434
              Some(pri max Rendering.information_pri)
wenzelm@52890
   435
            else if (squiggly_include.contains(elem.name))
wenzelm@52890
   436
              Some(pri max Rendering.message_pri(elem.name))
wenzelm@52889
   437
            else None
wenzelm@49356
   438
        })
wenzelm@49356
   439
    for {
wenzelm@49356
   440
      Text.Info(r, pri) <- results
wenzelm@49356
   441
      color <- squiggly_colors.get(pri)
wenzelm@49356
   442
    } yield Text.Info(r, color)
wenzelm@49356
   443
  }
wenzelm@49356
   444
wenzelm@49474
   445
wenzelm@49474
   446
  private val message_colors = Map(
wenzelm@50199
   447
    Rendering.writeln_pri -> writeln_message_color,
wenzelm@52650
   448
    Rendering.information_pri -> information_message_color,
wenzelm@50199
   449
    Rendering.tracing_pri -> tracing_message_color,
wenzelm@50199
   450
    Rendering.warning_pri -> warning_message_color,
wenzelm@50199
   451
    Rendering.error_pri -> error_message_color)
wenzelm@49474
   452
wenzelm@52650
   453
  private val line_background_elements =
wenzelm@52650
   454
    Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE,
wenzelm@52650
   455
      Markup.ERROR_MESSAGE, Markup.INFORMATION)
wenzelm@52650
   456
wenzelm@49473
   457
  def line_background(range: Text.Range): Option[(Color, Boolean)] =
wenzelm@49473
   458
  {
wenzelm@49474
   459
    val results =
wenzelm@52650
   460
      snapshot.cumulate_markup[Int](range, 0, Some(line_background_elements), _ =>
wenzelm@49473
   461
        {
wenzelm@52890
   462
          case (pri, Text.Info(_, elem)) =>
wenzelm@52890
   463
            val name = elem.name
wenzelm@52890
   464
            if (name == Markup.INFORMATION)
wenzelm@52890
   465
              Some(pri max Rendering.information_pri)
wenzelm@52890
   466
            else if (name == Markup.WRITELN_MESSAGE || name == Markup.TRACING_MESSAGE ||
wenzelm@52890
   467
                elem.name == Markup.WARNING_MESSAGE || name == Markup.ERROR_MESSAGE)
wenzelm@52889
   468
              Some(pri max Rendering.message_pri(name))
wenzelm@52889
   469
            else None
wenzelm@49474
   470
        })
wenzelm@49474
   471
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@49474
   472
wenzelm@49474
   473
    val is_separator = pri > 0 &&
wenzelm@50500
   474
      snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
wenzelm@49473
   475
        {
wenzelm@52890
   476
          case (_, Text.Info(_, elem)) =>
wenzelm@52890
   477
            if (elem.name == Markup.SEPARATOR) Some(true) else None
wenzelm@49473
   478
        }).exists(_.info)
wenzelm@49473
   479
wenzelm@49474
   480
    message_colors.get(pri).map((_, is_separator))
wenzelm@49473
   481
  }
wenzelm@49356
   482
wenzelm@49492
   483
wenzelm@50500
   484
  def output_messages(st: Command.State): List[XML.Tree] =
wenzelm@50500
   485
  {
wenzelm@50507
   486
    st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
wenzelm@50500
   487
  }
wenzelm@50500
   488
wenzelm@50500
   489
wenzelm@49492
   490
  private val background1_include =
wenzelm@50201
   491
    Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
wenzelm@52650
   492
      Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
wenzelm@52650
   493
      active_include
wenzelm@49492
   494
wenzelm@49356
   495
  def background1(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   496
  {
wenzelm@49356
   497
    if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
wenzelm@49356
   498
    else
wenzelm@49356
   499
      for {
wenzelm@49356
   500
        Text.Info(r, result) <-
wenzelm@50500
   501
          snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
wenzelm@50500
   502
            range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
wenzelm@49356
   503
            {
wenzelm@50500
   504
              case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@49356
   505
              if (Protocol.command_status_markup(markup.name)) =>
wenzelm@52889
   506
                Some((Some(Protocol.command_status(status, markup)), color))
wenzelm@50500
   507
              case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
wenzelm@52889
   508
                Some((None, Some(bad_color)))
wenzelm@50500
   509
              case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
wenzelm@52889
   510
                Some((None, Some(intensify_color)))
wenzelm@52081
   511
              case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
wenzelm@52081
   512
                command_state.results.get(serial) match {
wenzelm@52081
   513
                  case Some(Protocol.Dialog_Result(res)) if res == result =>
wenzelm@52889
   514
                    Some((None, Some(active_result_color)))
wenzelm@52081
   515
                  case _ =>
wenzelm@52889
   516
                    Some((None, Some(active_color)))
wenzelm@50500
   517
                }
wenzelm@52890
   518
              case (_, Text.Info(_, elem)) =>
wenzelm@52890
   519
                if (active_include(elem.name)) Some((None, Some(active_color)))
wenzelm@52889
   520
                else None
wenzelm@49356
   521
            })
wenzelm@49356
   522
        color <-
wenzelm@49356
   523
          (result match {
wenzelm@50500
   524
            case (Some(status), opt_color) =>
wenzelm@49356
   525
              if (status.is_unprocessed) Some(unprocessed1_color)
wenzelm@49356
   526
              else if (status.is_running) Some(running1_color)
wenzelm@49356
   527
              else opt_color
wenzelm@50500
   528
            case (_, opt_color) => opt_color
wenzelm@49356
   529
          })
wenzelm@49356
   530
      } yield Text.Info(r, color)
wenzelm@49356
   531
  }
wenzelm@49356
   532
wenzelm@49356
   533
wenzelm@49356
   534
  def background2(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@50500
   535
    snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
wenzelm@49356
   536
      {
wenzelm@52890
   537
        case Text.Info(_, elem) =>
wenzelm@52890
   538
          if (elem.name == Markup.TOKEN_RANGE) Some(light_color) else None
wenzelm@49356
   539
      })
wenzelm@49356
   540
wenzelm@49356
   541
wenzelm@51574
   542
  def bullet(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@51574
   543
    snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
wenzelm@51574
   544
      {
wenzelm@52890
   545
        case Text.Info(_, elem) =>
wenzelm@52890
   546
          if (elem.name == Markup.BULLET) Some(bullet_color) else None
wenzelm@51574
   547
      })
wenzelm@51574
   548
wenzelm@51574
   549
wenzelm@52890
   550
  private val foreground_include =
wenzelm@52101
   551
    Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
wenzelm@52101
   552
wenzelm@49356
   553
  def foreground(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@52890
   554
    snapshot.select_markup(range, Some(foreground_include), _ =>
wenzelm@49356
   555
      {
wenzelm@52890
   556
        case Text.Info(_, elem) =>
wenzelm@52890
   557
          if (elem.name == Markup.ANTIQ) Some(antiquoted_color)
wenzelm@52890
   558
          else if (foreground_include.contains(elem.name)) Some(quoted_color)
wenzelm@52890
   559
          else None
wenzelm@49356
   560
      })
wenzelm@49356
   561
wenzelm@49356
   562
wenzelm@49356
   563
  private val text_colors: Map[String, Color] = Map(
wenzelm@50201
   564
      Markup.KEYWORD1 -> keyword1_color,
wenzelm@50201
   565
      Markup.KEYWORD2 -> keyword2_color,
wenzelm@50201
   566
      Markup.STRING -> Color.BLACK,
wenzelm@50201
   567
      Markup.ALTSTRING -> Color.BLACK,
wenzelm@50201
   568
      Markup.VERBATIM -> Color.BLACK,
wenzelm@50201
   569
      Markup.LITERAL -> keyword1_color,
wenzelm@50201
   570
      Markup.DELIMITER -> Color.BLACK,
wenzelm@50201
   571
      Markup.TFREE -> tfree_color,
wenzelm@50201
   572
      Markup.TVAR -> tvar_color,
wenzelm@50201
   573
      Markup.FREE -> free_color,
wenzelm@50201
   574
      Markup.SKOLEM -> skolem_color,
wenzelm@50201
   575
      Markup.BOUND -> bound_color,
wenzelm@50201
   576
      Markup.VAR -> var_color,
wenzelm@50201
   577
      Markup.INNER_STRING -> inner_quoted_color,
wenzelm@50201
   578
      Markup.INNER_COMMENT -> inner_comment_color,
wenzelm@50201
   579
      Markup.DYNAMIC_FACT -> dynamic_color,
wenzelm@50201
   580
      Markup.ML_KEYWORD -> keyword1_color,
wenzelm@50201
   581
      Markup.ML_DELIMITER -> Color.BLACK,
wenzelm@50201
   582
      Markup.ML_NUMERAL -> inner_numeral_color,
wenzelm@50201
   583
      Markup.ML_CHAR -> inner_quoted_color,
wenzelm@50201
   584
      Markup.ML_STRING -> inner_quoted_color,
wenzelm@52101
   585
      Markup.ML_COMMENT -> inner_comment_color)
wenzelm@49356
   586
wenzelm@52102
   587
  private val text_color_elements = text_colors.keySet
wenzelm@49356
   588
wenzelm@49356
   589
  def text_color(range: Text.Range, color: Color)
wenzelm@49356
   590
      : Stream[Text.Info[Color]] =
wenzelm@49356
   591
  {
wenzelm@50196
   592
    if (color == Token_Markup.hidden_color) Stream(Text.Info(range, color))
wenzelm@50196
   593
    else
wenzelm@50500
   594
      snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
wenzelm@50196
   595
        {
wenzelm@52890
   596
          case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
wenzelm@50196
   597
        })
wenzelm@49356
   598
  }
wenzelm@50542
   599
wenzelm@50542
   600
wenzelm@50542
   601
  /* nested text structure -- folds */
wenzelm@50542
   602
wenzelm@50545
   603
  private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
wenzelm@50542
   604
wenzelm@50542
   605
  def fold_depth(range: Text.Range): Stream[Text.Info[Int]] =
wenzelm@50542
   606
    snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
wenzelm@50542
   607
      {
wenzelm@52890
   608
        case (depth, Text.Info(_, elem)) =>
wenzelm@52890
   609
          if (fold_depth_include(elem.name)) Some(depth + 1) else None
wenzelm@50542
   610
      })
wenzelm@49356
   611
}