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