src/Tools/jEdit/src/rendering.scala
author wenzelm
Wed Aug 07 13:46:32 2013 +0200 (2013-08-07 ago)
changeset 52889 ea3338812e67
parent 52873 9e934d4fff00
child 52890 36e2c0c308eb
permissions -rw-r--r--
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
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@52889
   157
            case ((status, pri), Text.Info(_, XML.Elem(markup, _))) =>
wenzelm@50201
   158
              if (markup.name == Markup.WARNING || markup.name == Markup.ERROR)
wenzelm@52889
   159
                Some((status, pri max Rendering.message_pri(markup.name)))
wenzelm@52889
   160
              else if (overview_include(markup.name))
wenzelm@52889
   161
                Some((Protocol.command_status(status, 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@52889
   192
          case Text.Info(info_range, XML.Elem(markup, _)) =>
wenzelm@52889
   193
            if (highlight_include(markup.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@52889
   260
          case Text.Info(info_range, elem @ XML.Elem(Markup(name, _), _)) =>
wenzelm@52889
   261
            if (name == Markup.BROWSER || name == Markup.GRAPHVIEW || name == Markup.SENDBACK)
wenzelm@52889
   262
              Some(Text.Info(snapshot.convert(info_range), elem))
wenzelm@52889
   263
            else None
wenzelm@50163
   264
        }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
wenzelm@49492
   265
wenzelm@49492
   266
wenzelm@50502
   267
  def command_results(range: Text.Range): Command.Results =
wenzelm@50502
   268
  {
wenzelm@50502
   269
    val results =
wenzelm@50502
   270
      snapshot.select_markup[Command.Results](range, None, command_state =>
wenzelm@52889
   271
        { case _ => Some(command_state.results) }).map(_.info)
wenzelm@50507
   272
    (Command.Results.empty /: results)(_ ++ _)
wenzelm@50502
   273
  }
wenzelm@50502
   274
wenzelm@51496
   275
  def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@49356
   276
  {
wenzelm@51496
   277
    val results =
wenzelm@51496
   278
      snapshot.cumulate_markup[List[Text.Info[Command.Results.Entry]]](range, Nil,
wenzelm@51496
   279
        Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)), _ =>
wenzelm@51496
   280
        {
wenzelm@51496
   281
          case (msgs, Text.Info(info_range,
wenzelm@51496
   282
            XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
wenzelm@51496
   283
          if name == Markup.WRITELN || name == Markup.WARNING || name == Markup.ERROR =>
wenzelm@51496
   284
            val entry: Command.Results.Entry =
wenzelm@51496
   285
              (serial -> XML.Elem(Markup(Markup.message(name), props), body))
wenzelm@52889
   286
            Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
wenzelm@51496
   287
wenzelm@51496
   288
          case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
wenzelm@51496
   289
          if !body.isEmpty =>
wenzelm@52530
   290
            val entry: Command.Results.Entry = (Document_ID.make(), msg)
wenzelm@52889
   291
            Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
wenzelm@52889
   292
wenzelm@52889
   293
          case _ => None
wenzelm@51496
   294
        }).toList.flatMap(_.info)
wenzelm@51496
   295
    if (results.isEmpty) None
wenzelm@51496
   296
    else {
wenzelm@51496
   297
      val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@51496
   298
      val msgs = Command.Results.make(results.map(_.info))
wenzelm@51496
   299
      Some(Text.Info(r, Pretty.separate(msgs.entries.map(_._2).toList)))
wenzelm@51496
   300
    }
wenzelm@49356
   301
  }
wenzelm@49356
   302
wenzelm@49356
   303
wenzelm@49356
   304
  private val tooltips: Map[String, String] =
wenzelm@49356
   305
    Map(
wenzelm@50201
   306
      Markup.SORT -> "sort",
wenzelm@50201
   307
      Markup.TYP -> "type",
wenzelm@50201
   308
      Markup.TERM -> "term",
wenzelm@50201
   309
      Markup.PROP -> "proposition",
wenzelm@50201
   310
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@50201
   311
      Markup.FREE -> "free variable",
wenzelm@50201
   312
      Markup.SKOLEM -> "skolem variable",
wenzelm@50201
   313
      Markup.BOUND -> "bound variable",
wenzelm@50201
   314
      Markup.VAR -> "schematic variable",
wenzelm@50201
   315
      Markup.TFREE -> "free type variable",
wenzelm@50201
   316
      Markup.TVAR -> "schematic type variable",
wenzelm@50201
   317
      Markup.ML_SOURCE -> "ML source",
wenzelm@50201
   318
      Markup.DOCUMENT_SOURCE -> "document source")
wenzelm@49356
   319
wenzelm@49356
   320
  private val tooltip_elements =
wenzelm@51588
   321
    Set(Markup.TIMING, Markup.ENTITY, Markup.SORTING, Markup.TYPING,
wenzelm@51588
   322
      Markup.ML_TYPING, Markup.PATH) ++ tooltips.keys
wenzelm@49356
   323
wenzelm@49700
   324
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@49700
   325
    Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
wenzelm@49356
   326
wenzelm@51588
   327
  private val timing_threshold: Double = options.real("jedit_timing_threshold")
wenzelm@51588
   328
wenzelm@51496
   329
  def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@49356
   330
  {
wenzelm@51588
   331
    def add(prev: Text.Info[(Timing, List[(Boolean, XML.Tree)])],
wenzelm@51588
   332
      r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, List[(Boolean, XML.Tree)])] =
wenzelm@51496
   333
    {
wenzelm@51496
   334
      val r = snapshot.convert(r0)
wenzelm@51588
   335
      val (t, info) = prev.info
wenzelm@51588
   336
      if (prev.range == r) Text.Info(r, (t, p :: info)) else Text.Info(r, (t, List(p)))
wenzelm@51496
   337
    }
wenzelm@49356
   338
wenzelm@51496
   339
    val results =
wenzelm@51588
   340
      snapshot.cumulate_markup[Text.Info[(Timing, List[(Boolean, XML.Tree)])]](
wenzelm@51588
   341
        range, Text.Info(range, (Timing.zero, Nil)), Some(tooltip_elements), _ =>
wenzelm@49356
   342
        {
wenzelm@51588
   343
          case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
wenzelm@52889
   344
            Some(Text.Info(r, (t1 + t2, info)))
wenzelm@50201
   345
          case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
wenzelm@49356
   346
            val kind1 = space_explode('_', kind).mkString(" ")
wenzelm@51588
   347
            val txt1 = kind1 + " " + quote(name)
wenzelm@51588
   348
            val t = prev.info._1
wenzelm@51588
   349
            val txt2 =
wenzelm@51588
   350
              if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
wenzelm@51588
   351
                "\n" + t.message
wenzelm@51588
   352
              else ""
wenzelm@52889
   353
            Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
wenzelm@50201
   354
          case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
wenzelm@49356
   355
          if Path.is_ok(name) =>
wenzelm@50205
   356
            val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
wenzelm@52889
   357
            Some(add(prev, r, (true, XML.Text("file " + quote(jedit_file)))))
wenzelm@49674
   358
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@50201
   359
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@52889
   360
            Some(add(prev, r, (true, pretty_typing("::", body))))
wenzelm@50201
   361
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@52889
   362
            Some(add(prev, r, (false, pretty_typing("ML:", body))))
wenzelm@52889
   363
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
wenzelm@52889
   364
            if (tooltips.isDefinedAt(name))
wenzelm@52889
   365
              Some(add(prev, r, (true, XML.Text(tooltips(name)))))
wenzelm@52889
   366
            else None
wenzelm@51496
   367
        }).toList.map(_.info)
wenzelm@49356
   368
wenzelm@51588
   369
    results.map(_.info).flatMap(_._2) match {
wenzelm@51496
   370
      case Nil => None
wenzelm@51496
   371
      case tips =>
wenzelm@51496
   372
        val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@51496
   373
        val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@51496
   374
        Some(Text.Info(r, Library.separate(Pretty.FBreak, all_tips)))
wenzelm@51496
   375
    }
wenzelm@49356
   376
  }
wenzelm@49356
   377
wenzelm@50554
   378
  val tooltip_margin: Int = options.int("jedit_tooltip_margin")
wenzelm@50554
   379
  val tooltip_bounds: Double = (options.real("jedit_tooltip_bounds") max 0.2) min 0.8
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@49356
   425
  def squiggly_underline(range: Text.Range): Stream[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@52889
   430
          case (pri, Text.Info(_, msg @ XML.Elem(markup, _))) =>
wenzelm@52889
   431
            if (Protocol.is_information(msg))
wenzelm@52889
   432
              Some(pri max Rendering.information_pri)
wenzelm@52889
   433
            else if (squiggly_include.contains(markup.name))
wenzelm@52889
   434
              Some(pri max Rendering.message_pri(markup.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@52650
   460
          case (pri, Text.Info(_, XML.Elem(Markup(Markup.INFORMATION, _), _))) =>
wenzelm@52889
   461
            Some(pri max Rendering.information_pri)
wenzelm@52889
   462
          case (pri, Text.Info(_, XML.Elem(Markup(name, _), _))) =>
wenzelm@52889
   463
            if (name == Markup.WRITELN_MESSAGE || name == Markup.TRACING_MESSAGE ||
wenzelm@52889
   464
                name == Markup.WARNING_MESSAGE || name == Markup.ERROR_MESSAGE)
wenzelm@52889
   465
              Some(pri max Rendering.message_pri(name))
wenzelm@52889
   466
            else None
wenzelm@49474
   467
        })
wenzelm@49474
   468
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@49474
   469
wenzelm@49474
   470
    val is_separator = pri > 0 &&
wenzelm@50500
   471
      snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)), _ =>
wenzelm@49473
   472
        {
wenzelm@52889
   473
          case (_, Text.Info(_, XML.Elem(Markup(Markup.SEPARATOR, _), _))) => Some(true)
wenzelm@52889
   474
          case _ => None
wenzelm@49473
   475
        }).exists(_.info)
wenzelm@49473
   476
wenzelm@49474
   477
    message_colors.get(pri).map((_, is_separator))
wenzelm@49473
   478
  }
wenzelm@49356
   479
wenzelm@49492
   480
wenzelm@50500
   481
  def output_messages(st: Command.State): List[XML.Tree] =
wenzelm@50500
   482
  {
wenzelm@50507
   483
    st.results.entries.map(_._2).filterNot(Protocol.is_result(_)).toList
wenzelm@50500
   484
  }
wenzelm@50500
   485
wenzelm@50500
   486
wenzelm@49492
   487
  private val background1_include =
wenzelm@50201
   488
    Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
wenzelm@52650
   489
      Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY ++
wenzelm@52650
   490
      active_include
wenzelm@49492
   491
wenzelm@49356
   492
  def background1(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@49356
   493
  {
wenzelm@49356
   494
    if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
wenzelm@49356
   495
    else
wenzelm@49356
   496
      for {
wenzelm@49356
   497
        Text.Info(r, result) <-
wenzelm@50500
   498
          snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
wenzelm@50500
   499
            range, (Some(Protocol.Status.init), None), Some(background1_include), command_state =>
wenzelm@49356
   500
            {
wenzelm@50500
   501
              case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@49356
   502
              if (Protocol.command_status_markup(markup.name)) =>
wenzelm@52889
   503
                Some((Some(Protocol.command_status(status, markup)), color))
wenzelm@50500
   504
              case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
wenzelm@52889
   505
                Some((None, Some(bad_color)))
wenzelm@50500
   506
              case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
wenzelm@52889
   507
                Some((None, Some(intensify_color)))
wenzelm@52081
   508
              case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
wenzelm@52081
   509
                command_state.results.get(serial) match {
wenzelm@52081
   510
                  case Some(Protocol.Dialog_Result(res)) if res == result =>
wenzelm@52889
   511
                    Some((None, Some(active_result_color)))
wenzelm@52081
   512
                  case _ =>
wenzelm@52889
   513
                    Some((None, Some(active_color)))
wenzelm@50500
   514
                }
wenzelm@52889
   515
              case (_, Text.Info(_, XML.Elem(markup, _))) =>
wenzelm@52889
   516
                if (active_include(markup.name)) Some((None, Some(active_color)))
wenzelm@52889
   517
                else None
wenzelm@49356
   518
            })
wenzelm@49356
   519
        color <-
wenzelm@49356
   520
          (result match {
wenzelm@50500
   521
            case (Some(status), opt_color) =>
wenzelm@49356
   522
              if (status.is_unprocessed) Some(unprocessed1_color)
wenzelm@49356
   523
              else if (status.is_running) Some(running1_color)
wenzelm@49356
   524
              else opt_color
wenzelm@50500
   525
            case (_, opt_color) => opt_color
wenzelm@49356
   526
          })
wenzelm@49356
   527
      } yield Text.Info(r, color)
wenzelm@49356
   528
  }
wenzelm@49356
   529
wenzelm@49356
   530
wenzelm@49356
   531
  def background2(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@50500
   532
    snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)), _ =>
wenzelm@49356
   533
      {
wenzelm@52889
   534
        case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => Some(light_color)
wenzelm@52889
   535
        case _ => None
wenzelm@49356
   536
      })
wenzelm@49356
   537
wenzelm@49356
   538
wenzelm@51574
   539
  def bullet(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@51574
   540
    snapshot.select_markup(range, Some(Set(Markup.BULLET)), _ =>
wenzelm@51574
   541
      {
wenzelm@52889
   542
        case Text.Info(_, XML.Elem(Markup(Markup.BULLET, _), _)) => Some(bullet_color)
wenzelm@52889
   543
        case _ => None
wenzelm@51574
   544
      })
wenzelm@51574
   545
wenzelm@51574
   546
wenzelm@52101
   547
  private val foreground_elements =
wenzelm@52101
   548
    Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM, Markup.ANTIQ)
wenzelm@52101
   549
wenzelm@49356
   550
  def foreground(range: Text.Range): Stream[Text.Info[Color]] =
wenzelm@52101
   551
    snapshot.select_markup(range, Some(foreground_elements), _ =>
wenzelm@49356
   552
      {
wenzelm@52889
   553
        case Text.Info(_, XML.Elem(Markup(Markup.STRING, _), _)) => Some(quoted_color)
wenzelm@52889
   554
        case Text.Info(_, XML.Elem(Markup(Markup.ALTSTRING, _), _)) => Some(quoted_color)
wenzelm@52889
   555
        case Text.Info(_, XML.Elem(Markup(Markup.VERBATIM, _), _)) => Some(quoted_color)
wenzelm@52889
   556
        case Text.Info(_, XML.Elem(Markup(Markup.ANTIQ, _), _)) => Some(antiquoted_color)
wenzelm@52889
   557
        case _ => None
wenzelm@49356
   558
      })
wenzelm@49356
   559
wenzelm@49356
   560
wenzelm@49356
   561
  private val text_colors: Map[String, Color] = Map(
wenzelm@50201
   562
      Markup.KEYWORD1 -> keyword1_color,
wenzelm@50201
   563
      Markup.KEYWORD2 -> keyword2_color,
wenzelm@50201
   564
      Markup.STRING -> Color.BLACK,
wenzelm@50201
   565
      Markup.ALTSTRING -> Color.BLACK,
wenzelm@50201
   566
      Markup.VERBATIM -> Color.BLACK,
wenzelm@50201
   567
      Markup.LITERAL -> keyword1_color,
wenzelm@50201
   568
      Markup.DELIMITER -> Color.BLACK,
wenzelm@50201
   569
      Markup.TFREE -> tfree_color,
wenzelm@50201
   570
      Markup.TVAR -> tvar_color,
wenzelm@50201
   571
      Markup.FREE -> free_color,
wenzelm@50201
   572
      Markup.SKOLEM -> skolem_color,
wenzelm@50201
   573
      Markup.BOUND -> bound_color,
wenzelm@50201
   574
      Markup.VAR -> var_color,
wenzelm@50201
   575
      Markup.INNER_STRING -> inner_quoted_color,
wenzelm@50201
   576
      Markup.INNER_COMMENT -> inner_comment_color,
wenzelm@50201
   577
      Markup.DYNAMIC_FACT -> dynamic_color,
wenzelm@50201
   578
      Markup.ML_KEYWORD -> keyword1_color,
wenzelm@50201
   579
      Markup.ML_DELIMITER -> Color.BLACK,
wenzelm@50201
   580
      Markup.ML_NUMERAL -> inner_numeral_color,
wenzelm@50201
   581
      Markup.ML_CHAR -> inner_quoted_color,
wenzelm@50201
   582
      Markup.ML_STRING -> inner_quoted_color,
wenzelm@52101
   583
      Markup.ML_COMMENT -> inner_comment_color)
wenzelm@49356
   584
wenzelm@52102
   585
  private val text_color_elements = text_colors.keySet
wenzelm@49356
   586
wenzelm@49356
   587
  def text_color(range: Text.Range, color: Color)
wenzelm@49356
   588
      : Stream[Text.Info[Color]] =
wenzelm@49356
   589
  {
wenzelm@50196
   590
    if (color == Token_Markup.hidden_color) Stream(Text.Info(range, color))
wenzelm@50196
   591
    else
wenzelm@50500
   592
      snapshot.cumulate_markup(range, color, Some(text_color_elements), _ =>
wenzelm@50196
   593
        {
wenzelm@52889
   594
          case (_, Text.Info(_, XML.Elem(markup, _))) => text_colors.get(markup.name)
wenzelm@50196
   595
        })
wenzelm@49356
   596
  }
wenzelm@50542
   597
wenzelm@50542
   598
wenzelm@50542
   599
  /* nested text structure -- folds */
wenzelm@50542
   600
wenzelm@50545
   601
  private val fold_depth_include = Set(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
wenzelm@50542
   602
wenzelm@50542
   603
  def fold_depth(range: Text.Range): Stream[Text.Info[Int]] =
wenzelm@50542
   604
    snapshot.cumulate_markup[Int](range, 0, Some(fold_depth_include), _ =>
wenzelm@50542
   605
      {
wenzelm@52889
   606
        case (depth, Text.Info(_, XML.Elem(markup, _))) =>
wenzelm@52889
   607
          if (fold_depth_include(markup.name)) Some(depth + 1) else None
wenzelm@50542
   608
      })
wenzelm@49356
   609
}