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