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