src/Tools/jEdit/src/rendering.scala
author wenzelm
Wed Jun 22 16:04:03 2016 +0200 (2016-06-22 ago)
changeset 63347 e344dc82f6c2
parent 63340 29d7ac9bdcb1
child 63441 4c3fa4dba79f
permissions -rw-r--r--
report class parameters within instantiation;
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@61175
    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@59184
    30
  private val state_pri = 1
wenzelm@59184
    31
  private val writeln_pri = 2
wenzelm@59184
    32
  private val information_pri = 3
wenzelm@59184
    33
  private val tracing_pri = 4
wenzelm@59184
    34
  private val warning_pri = 5
wenzelm@59184
    35
  private val legacy_pri = 6
wenzelm@59184
    36
  private val error_pri = 7
wenzelm@49474
    37
wenzelm@49474
    38
  private val message_pri = Map(
wenzelm@59184
    39
    Markup.STATE -> state_pri,
wenzelm@59184
    40
    Markup.STATE_MESSAGE -> state_pri,
wenzelm@59184
    41
    Markup.WRITELN -> writeln_pri,
wenzelm@59184
    42
    Markup.WRITELN_MESSAGE -> writeln_pri,
wenzelm@59184
    43
    Markup.INFORMATION -> information_pri,
wenzelm@59184
    44
    Markup.INFORMATION_MESSAGE -> information_pri,
wenzelm@59184
    45
    Markup.TRACING -> tracing_pri,
wenzelm@59184
    46
    Markup.TRACING_MESSAGE -> tracing_pri,
wenzelm@59184
    47
    Markup.WARNING -> warning_pri,
wenzelm@59184
    48
    Markup.WARNING_MESSAGE -> warning_pri,
wenzelm@59203
    49
    Markup.LEGACY -> legacy_pri,
wenzelm@59203
    50
    Markup.LEGACY_MESSAGE -> legacy_pri,
wenzelm@59184
    51
    Markup.ERROR -> error_pri,
wenzelm@59184
    52
    Markup.ERROR_MESSAGE -> error_pri)
wenzelm@39178
    53
wenzelm@49406
    54
wenzelm@53230
    55
  /* popup window bounds */
wenzelm@53230
    56
wenzelm@53230
    57
  def popup_bounds: Double = (PIDE.options.real("jedit_popup_bounds") max 0.2) min 0.8
wenzelm@53230
    58
wenzelm@53230
    59
wenzelm@55501
    60
  /* Isabelle/Isar token markup */
wenzelm@39179
    61
wenzelm@39179
    62
  private val command_style: Map[String, Byte] =
wenzelm@39179
    63
  {
wenzelm@43414
    64
    import JEditToken._
wenzelm@39179
    65
    Map[String, Byte](
wenzelm@39179
    66
      Keyword.THY_END -> KEYWORD2,
wenzelm@39179
    67
      Keyword.PRF_ASM -> KEYWORD3,
wenzelm@59125
    68
      Keyword.PRF_ASM_GOAL -> KEYWORD3
wenzelm@39179
    69
    ).withDefaultValue(KEYWORD1)
wenzelm@39179
    70
  }
wenzelm@39179
    71
wenzelm@43414
    72
  private val token_style: Map[Token.Kind.Value, Byte] =
wenzelm@39179
    73
  {
wenzelm@43414
    74
    import JEditToken._
wenzelm@43414
    75
    Map[Token.Kind.Value, Byte](
wenzelm@43414
    76
      Token.Kind.KEYWORD -> KEYWORD2,
wenzelm@43414
    77
      Token.Kind.IDENT -> NULL,
wenzelm@43414
    78
      Token.Kind.LONG_IDENT -> NULL,
wenzelm@43414
    79
      Token.Kind.SYM_IDENT -> NULL,
wenzelm@43414
    80
      Token.Kind.VAR -> NULL,
wenzelm@43414
    81
      Token.Kind.TYPE_IDENT -> NULL,
wenzelm@43414
    82
      Token.Kind.TYPE_VAR -> NULL,
wenzelm@43414
    83
      Token.Kind.NAT -> NULL,
wenzelm@43414
    84
      Token.Kind.FLOAT -> NULL,
wenzelm@59081
    85
      Token.Kind.SPACE -> NULL,
wenzelm@43431
    86
      Token.Kind.STRING -> LITERAL1,
wenzelm@43431
    87
      Token.Kind.ALT_STRING -> LITERAL2,
wenzelm@43414
    88
      Token.Kind.VERBATIM -> COMMENT3,
wenzelm@55033
    89
      Token.Kind.CARTOUCHE -> COMMENT4,
wenzelm@43414
    90
      Token.Kind.COMMENT -> COMMENT1,
wenzelm@48754
    91
      Token.Kind.ERROR -> INVALID
wenzelm@39179
    92
    ).withDefaultValue(NULL)
wenzelm@39179
    93
  }
wenzelm@39179
    94
wenzelm@43414
    95
  def token_markup(syntax: Outer_Syntax, token: Token): Byte =
wenzelm@58901
    96
    if (token.is_command) command_style(syntax.keywords.command_kind(token.content).getOrElse(""))
wenzelm@61579
    97
    else if (token.is_keyword && token.source == Symbol.comment_decoded) JEditToken.NULL
wenzelm@55505
    98
    else if (token.is_delimiter) JEditToken.OPERATOR
wenzelm@43414
    99
    else token_style(token.kind)
wenzelm@55500
   100
wenzelm@55501
   101
wenzelm@55501
   102
  /* Isabelle/ML token markup */
wenzelm@55501
   103
wenzelm@55500
   104
  private val ml_token_style: Map[ML_Lex.Kind.Value, Byte] =
wenzelm@55500
   105
  {
wenzelm@55500
   106
    import JEditToken._
wenzelm@55500
   107
    Map[ML_Lex.Kind.Value, Byte](
wenzelm@55505
   108
      ML_Lex.Kind.KEYWORD -> NULL,
wenzelm@55500
   109
      ML_Lex.Kind.IDENT -> NULL,
wenzelm@55500
   110
      ML_Lex.Kind.LONG_IDENT -> NULL,
wenzelm@55500
   111
      ML_Lex.Kind.TYPE_VAR -> NULL,
wenzelm@55503
   112
      ML_Lex.Kind.WORD -> DIGIT,
wenzelm@55503
   113
      ML_Lex.Kind.INT -> DIGIT,
wenzelm@55503
   114
      ML_Lex.Kind.REAL -> DIGIT,
wenzelm@55500
   115
      ML_Lex.Kind.CHAR -> LITERAL2,
wenzelm@55500
   116
      ML_Lex.Kind.STRING -> LITERAL1,
wenzelm@55500
   117
      ML_Lex.Kind.SPACE -> NULL,
wenzelm@55500
   118
      ML_Lex.Kind.COMMENT -> COMMENT1,
wenzelm@55512
   119
      ML_Lex.Kind.ANTIQ -> NULL,
wenzelm@55512
   120
      ML_Lex.Kind.ANTIQ_START -> LITERAL4,
wenzelm@55512
   121
      ML_Lex.Kind.ANTIQ_STOP -> LITERAL4,
wenzelm@55512
   122
      ML_Lex.Kind.ANTIQ_OTHER -> NULL,
wenzelm@55514
   123
      ML_Lex.Kind.ANTIQ_STRING -> NULL,
wenzelm@55514
   124
      ML_Lex.Kind.ANTIQ_ALT_STRING -> NULL,
wenzelm@55514
   125
      ML_Lex.Kind.ANTIQ_CARTOUCHE -> NULL,
wenzelm@55500
   126
      ML_Lex.Kind.ERROR -> INVALID
wenzelm@55500
   127
    ).withDefaultValue(NULL)
wenzelm@55500
   128
  }
wenzelm@55500
   129
wenzelm@55500
   130
  def ml_token_markup(token: ML_Lex.Token): Byte =
wenzelm@55501
   131
    if (!token.is_keyword) ml_token_style(token.kind)
wenzelm@55505
   132
    else if (token.is_delimiter) JEditToken.OPERATOR
wenzelm@55505
   133
    else if (ML_Lex.keywords2(token.source)) JEditToken.KEYWORD2
wenzelm@55505
   134
    else if (ML_Lex.keywords3(token.source)) JEditToken.KEYWORD3
wenzelm@55501
   135
    else JEditToken.KEYWORD1
wenzelm@55647
   136
wenzelm@55647
   137
wenzelm@55647
   138
  /* markup elements */
wenzelm@55647
   139
wenzelm@56173
   140
  private val semantic_completion_elements =
wenzelm@56743
   141
    Markup.Elements(Markup.COMPLETION, Markup.NO_COMPLETION)
wenzelm@55674
   142
wenzelm@56173
   143
  private val language_context_elements =
wenzelm@59081
   144
    Markup.Elements(Markup.STRING, Markup.ALT_STRING, Markup.VERBATIM,
wenzelm@55647
   145
      Markup.CARTOUCHE, Markup.COMMENT, Markup.LANGUAGE,
wenzelm@61598
   146
      Markup.ML_STRING, Markup.ML_COMMENT)
wenzelm@55647
   147
wenzelm@56843
   148
  private val language_elements = Markup.Elements(Markup.LANGUAGE)
wenzelm@56843
   149
wenzelm@58592
   150
  private val citation_elements = Markup.Elements(Markup.CITATION)
wenzelm@58592
   151
wenzelm@60878
   152
  private val breakpoint_elements = Markup.Elements(Markup.ML_BREAKPOINT)
wenzelm@60878
   153
wenzelm@62986
   154
  private val caret_focus_elements = Markup.Elements(Markup.ENTITY)
wenzelm@62986
   155
wenzelm@55647
   156
  private val highlight_elements =
wenzelm@58545
   157
    Markup.Elements(Markup.EXPRESSION, Markup.CITATION, Markup.LANGUAGE, Markup.ML_TYPING,
wenzelm@61718
   158
      Markup.TOKEN_RANGE, Markup.ENTITY, Markup.PATH, Markup.DOC, Markup.URL, Markup.SORTING,
wenzelm@63347
   159
      Markup.TYPING, Markup.CLASS_PARAMETER, Markup.FREE, Markup.SKOLEM, Markup.BOUND,
wenzelm@61450
   160
      Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_BREAKPOINT,
wenzelm@61450
   161
      Markup.MARKDOWN_PARAGRAPH, Markup.Markdown_List.name)
wenzelm@55647
   162
wenzelm@55647
   163
  private val hyperlink_elements =
wenzelm@61660
   164
    Markup.Elements(Markup.ENTITY, Markup.PATH, Markup.DOC, Markup.POSITION,
wenzelm@61660
   165
      Markup.CITATION, Markup.URL)
wenzelm@55647
   166
wenzelm@55647
   167
  private val active_elements =
wenzelm@56743
   168
    Markup.Elements(Markup.DIALOG, Markup.BROWSER, Markup.GRAPHVIEW,
wenzelm@57598
   169
      Markup.SENDBACK, Markup.SIMP_TRACE_PANEL)
wenzelm@55647
   170
wenzelm@55647
   171
  private val tooltip_message_elements =
wenzelm@59203
   172
    Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR,
wenzelm@59203
   173
      Markup.BAD)
wenzelm@55647
   174
wenzelm@55647
   175
  private val tooltip_descriptions =
wenzelm@55647
   176
    Map(
wenzelm@58545
   177
      Markup.CITATION -> "citation",
wenzelm@55647
   178
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@55647
   179
      Markup.FREE -> "free variable",
wenzelm@55647
   180
      Markup.SKOLEM -> "skolem variable",
wenzelm@55647
   181
      Markup.BOUND -> "bound variable",
wenzelm@55647
   182
      Markup.VAR -> "schematic variable",
wenzelm@55647
   183
      Markup.TFREE -> "free type variable",
wenzelm@55647
   184
      Markup.TVAR -> "schematic type variable")
wenzelm@55647
   185
wenzelm@55647
   186
  private val tooltip_elements =
wenzelm@62806
   187
    Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
wenzelm@63347
   188
      Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
wenzelm@63347
   189
      Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
wenzelm@63347
   190
      Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
wenzelm@55647
   191
wenzelm@55647
   192
  private val gutter_elements =
wenzelm@59203
   193
    Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR)
wenzelm@55647
   194
wenzelm@55647
   195
  private val squiggly_elements =
wenzelm@59203
   196
    Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR)
wenzelm@55647
   197
wenzelm@55647
   198
  private val line_background_elements =
wenzelm@59184
   199
    Markup.Elements(Markup.WRITELN_MESSAGE, Markup.STATE_MESSAGE, Markup.INFORMATION_MESSAGE,
wenzelm@59203
   200
      Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE, Markup.LEGACY_MESSAGE,
wenzelm@59203
   201
      Markup.ERROR_MESSAGE)
wenzelm@55647
   202
wenzelm@55820
   203
  private val separator_elements =
wenzelm@56743
   204
    Markup.Elements(Markup.SEPARATOR)
wenzelm@55647
   205
wenzelm@55790
   206
  private val background_elements =
wenzelm@56395
   207
    Protocol.proper_status_elements + Markup.WRITELN_MESSAGE +
wenzelm@59184
   208
      Markup.STATE_MESSAGE + Markup.INFORMATION_MESSAGE +
wenzelm@55647
   209
      Markup.TRACING_MESSAGE + Markup.WARNING_MESSAGE +
wenzelm@59203
   210
      Markup.LEGACY_MESSAGE + Markup.ERROR_MESSAGE +
wenzelm@62986
   211
      Markup.BAD + Markup.INTENSIFY + Markup.ENTITY +
wenzelm@62986
   212
      Markup.Markdown_Item.name ++ active_elements
wenzelm@55647
   213
wenzelm@55647
   214
  private val foreground_elements =
wenzelm@59081
   215
    Markup.Elements(Markup.STRING, Markup.ALT_STRING, Markup.VERBATIM,
wenzelm@55647
   216
      Markup.CARTOUCHE, Markup.ANTIQUOTED)
wenzelm@55647
   217
wenzelm@55820
   218
  private val bullet_elements =
wenzelm@60913
   219
    Markup.Elements(Markup.BULLET, Markup.ML_BREAKPOINT)
wenzelm@55647
   220
wenzelm@55647
   221
  private val fold_depth_elements =
wenzelm@56743
   222
    Markup.Elements(Markup.TEXT_FOLD, Markup.GOAL, Markup.SUBGOAL)
wenzelm@39178
   223
}
wenzelm@49356
   224
wenzelm@49356
   225
wenzelm@50199
   226
class Rendering private(val snapshot: Document.Snapshot, val options: Options)
wenzelm@49356
   227
{
wenzelm@55710
   228
  override def toString: String = "Rendering(" + snapshot.toString + ")"
wenzelm@55710
   229
wenzelm@55710
   230
wenzelm@49356
   231
  /* colors */
wenzelm@49356
   232
wenzelm@49356
   233
  def color_value(s: String): Color = Color_Value(options.string(s))
wenzelm@49356
   234
wenzelm@49356
   235
  val outdated_color = color_value("outdated_color")
wenzelm@49356
   236
  val unprocessed_color = color_value("unprocessed_color")
wenzelm@49356
   237
  val unprocessed1_color = color_value("unprocessed1_color")
wenzelm@49356
   238
  val running_color = color_value("running_color")
wenzelm@49356
   239
  val running1_color = color_value("running1_color")
wenzelm@51574
   240
  val bullet_color = color_value("bullet_color")
wenzelm@49706
   241
  val tooltip_color = color_value("tooltip_color")
wenzelm@49356
   242
  val writeln_color = color_value("writeln_color")
wenzelm@52650
   243
  val information_color = color_value("information_color")
wenzelm@49356
   244
  val warning_color = color_value("warning_color")
wenzelm@59203
   245
  val legacy_color = color_value("legacy_color")
wenzelm@49356
   246
  val error_color = color_value("error_color")
wenzelm@49418
   247
  val writeln_message_color = color_value("writeln_message_color")
wenzelm@52650
   248
  val information_message_color = color_value("information_message_color")
wenzelm@49418
   249
  val tracing_message_color = color_value("tracing_message_color")
wenzelm@49418
   250
  val warning_message_color = color_value("warning_message_color")
wenzelm@59203
   251
  val legacy_message_color = color_value("legacy_message_color")
wenzelm@49418
   252
  val error_message_color = color_value("error_message_color")
wenzelm@56550
   253
  val spell_checker_color = color_value("spell_checker_color")
wenzelm@49356
   254
  val bad_color = color_value("bad_color")
wenzelm@49358
   255
  val intensify_color = color_value("intensify_color")
wenzelm@62988
   256
  val entity_color = color_value("entity_color")
wenzelm@62991
   257
  val entity_ref_color = color_value("entity_ref_color")
wenzelm@60913
   258
  val breakpoint_disabled_color = color_value("breakpoint_disabled_color")
wenzelm@60913
   259
  val breakpoint_enabled_color = color_value("breakpoint_enabled_color")
wenzelm@61011
   260
  val caret_debugger_color = color_value("caret_debugger_color")
wenzelm@49356
   261
  val quoted_color = color_value("quoted_color")
wenzelm@52101
   262
  val antiquoted_color = color_value("antiquoted_color")
wenzelm@55526
   263
  val antiquote_color = color_value("antiquote_color")
wenzelm@49358
   264
  val highlight_color = color_value("highlight_color")
wenzelm@49356
   265
  val hyperlink_color = color_value("hyperlink_color")
wenzelm@50450
   266
  val active_color = color_value("active_color")
wenzelm@50450
   267
  val active_hover_color = color_value("active_hover_color")
wenzelm@50498
   268
  val active_result_color = color_value("active_result_color")
wenzelm@49356
   269
  val keyword1_color = color_value("keyword1_color")
wenzelm@49356
   270
  val keyword2_color = color_value("keyword2_color")
wenzelm@55505
   271
  val keyword3_color = color_value("keyword3_color")
wenzelm@55919
   272
  val quasi_keyword_color = color_value("quasi_keyword_color")
wenzelm@56202
   273
  val improper_color = color_value("improper_color")
wenzelm@56064
   274
  val operator_color = color_value("operator_color")
wenzelm@55713
   275
  val caret_invisible_color = color_value("caret_invisible_color")
wenzelm@55747
   276
  val completion_color = color_value("completion_color")
wenzelm@56883
   277
  val search_color = color_value("search_color")
wenzelm@49356
   278
wenzelm@49356
   279
  val tfree_color = color_value("tfree_color")
wenzelm@49356
   280
  val tvar_color = color_value("tvar_color")
wenzelm@49356
   281
  val free_color = color_value("free_color")
wenzelm@49356
   282
  val skolem_color = color_value("skolem_color")
wenzelm@49356
   283
  val bound_color = color_value("bound_color")
wenzelm@49356
   284
  val var_color = color_value("var_color")
wenzelm@49356
   285
  val inner_numeral_color = color_value("inner_numeral_color")
wenzelm@49356
   286
  val inner_quoted_color = color_value("inner_quoted_color")
wenzelm@55033
   287
  val inner_cartouche_color = color_value("inner_cartouche_color")
wenzelm@49356
   288
  val inner_comment_color = color_value("inner_comment_color")
wenzelm@49356
   289
  val dynamic_color = color_value("dynamic_color")
wenzelm@63347
   290
  val class_parameter_color = color_value("class_parameter_color")
wenzelm@49356
   291
wenzelm@61449
   292
  val markdown_item_color1 = color_value("markdown_item_color1")
wenzelm@61449
   293
  val markdown_item_color2 = color_value("markdown_item_color2")
wenzelm@61449
   294
  val markdown_item_color3 = color_value("markdown_item_color3")
wenzelm@61449
   295
  val markdown_item_color4 = color_value("markdown_item_color4")
wenzelm@61449
   296
wenzelm@49356
   297
wenzelm@55674
   298
  /* completion */
wenzelm@55674
   299
wenzelm@61601
   300
  def semantic_completion(completed_range: Option[Text.Range], range: Text.Range)
wenzelm@61601
   301
      : Option[Text.Info[Completion.Semantic]] =
wenzelm@55690
   302
    if (snapshot.is_outdated) None
wenzelm@55690
   303
    else {
wenzelm@56173
   304
      snapshot.select(range, Rendering.semantic_completion_elements, _ =>
wenzelm@55688
   305
        {
wenzelm@61601
   306
          case Completion.Semantic.Info(info) =>
wenzelm@61601
   307
            completed_range match {
wenzelm@61601
   308
              case Some(range0) if range0.contains(info.range) && range0 != info.range => None
wenzelm@61601
   309
              case _ => Some(info)
wenzelm@61601
   310
            }
wenzelm@55688
   311
          case _ => None
wenzelm@55688
   312
        }).headOption.map(_.info)
wenzelm@55674
   313
    }
wenzelm@55615
   314
wenzelm@56173
   315
  def language_context(range: Text.Range): Option[Completion.Language_Context] =
wenzelm@56173
   316
    snapshot.select(range, Rendering.language_context_elements, _ =>
wenzelm@55690
   317
      {
wenzelm@55828
   318
        case Text.Info(_, XML.Elem(Markup.Language(language, symbols, antiquotes, delimited), _)) =>
wenzelm@55828
   319
          if (delimited) Some(Completion.Language_Context(language, symbols, antiquotes))
wenzelm@55828
   320
          else None
wenzelm@55690
   321
        case Text.Info(_, elem)
wenzelm@61598
   322
        if elem.name == Markup.ML_STRING || elem.name == Markup.ML_COMMENT =>
wenzelm@55749
   323
          Some(Completion.Language_Context.ML_inner)
wenzelm@55690
   324
        case Text.Info(_, _) =>
wenzelm@55749
   325
          Some(Completion.Language_Context.inner)
wenzelm@55690
   326
      }).headOption.map(_.info)
wenzelm@55615
   327
wenzelm@56843
   328
  def language_path(range: Text.Range): Option[Text.Range] =
wenzelm@56843
   329
    snapshot.select(range, Rendering.language_elements, _ =>
wenzelm@56843
   330
      {
wenzelm@56843
   331
        case Text.Info(info_range, XML.Elem(Markup.Language(Markup.Language.PATH, _, _, _), _)) =>
wenzelm@56843
   332
          Some(snapshot.convert(info_range))
wenzelm@56843
   333
        case _ => None
wenzelm@56843
   334
      }).headOption.map(_.info)
wenzelm@56843
   335
wenzelm@58592
   336
  def citation(range: Text.Range): Option[Text.Info[String]] =
wenzelm@58592
   337
    snapshot.select(range, Rendering.citation_elements, _ =>
wenzelm@58592
   338
      {
wenzelm@58592
   339
        case Text.Info(info_range, XML.Elem(Markup.Citation(name), _)) =>
wenzelm@58592
   340
          Some(Text.Info(snapshot.convert(info_range), name))
wenzelm@58592
   341
        case _ => None
wenzelm@58592
   342
      }).headOption.map(_.info)
wenzelm@58592
   343
wenzelm@55615
   344
wenzelm@56551
   345
  /* spell checker */
wenzelm@56548
   346
wenzelm@56551
   347
  private lazy val spell_checker_elements =
wenzelm@56743
   348
    Markup.Elements(space_explode(',', options.string("spell_checker_elements")): _*)
wenzelm@56551
   349
wenzelm@56551
   350
  def spell_checker_ranges(range: Text.Range): List[Text.Range] =
wenzelm@56551
   351
    snapshot.select(range, spell_checker_elements, _ => _ => Some(())).map(_.range)
wenzelm@56548
   352
wenzelm@56564
   353
  def spell_checker_point(range: Text.Range): Option[Text.Range] =
wenzelm@56564
   354
    snapshot.select(range, spell_checker_elements, _ =>
wenzelm@56564
   355
      {
wenzelm@56564
   356
        case info => Some(snapshot.convert(info.range))
wenzelm@56564
   357
      }).headOption.map(_.info)
wenzelm@56564
   358
wenzelm@56548
   359
wenzelm@60878
   360
  /* breakpoints */
wenzelm@60878
   361
wenzelm@60880
   362
  def breakpoint(range: Text.Range): Option[(Command, Long)] =
wenzelm@60880
   363
    if (snapshot.is_outdated) None
wenzelm@60880
   364
    else
wenzelm@60880
   365
      snapshot.select(range, Rendering.breakpoint_elements, command_states =>
wenzelm@60880
   366
        {
wenzelm@60882
   367
          case Text.Info(_, Protocol.ML_Breakpoint(breakpoint)) =>
wenzelm@60880
   368
            command_states match {
wenzelm@60882
   369
              case st :: _ => Some((st.command, breakpoint))
wenzelm@60880
   370
              case _ => None
wenzelm@60880
   371
            }
wenzelm@60880
   372
          case _ => None
wenzelm@60880
   373
        }).headOption.map(_.info)
wenzelm@60878
   374
wenzelm@60878
   375
wenzelm@55626
   376
  /* command status overview */
wenzelm@49356
   377
wenzelm@49356
   378
  def overview_color(range: Text.Range): Option[Color] =
wenzelm@49356
   379
  {
wenzelm@49356
   380
    if (snapshot.is_outdated) None
wenzelm@49356
   381
    else {
wenzelm@49356
   382
      val results =
wenzelm@56395
   383
        snapshot.cumulate[List[Markup]](range, Nil, Protocol.liberal_status_elements, _ =>
wenzelm@49356
   384
          {
wenzelm@56395
   385
            case (status, Text.Info(_, elem)) => Some(elem.markup :: status)
wenzelm@55651
   386
          }, status = true)
wenzelm@49356
   387
      if (results.isEmpty) None
wenzelm@49356
   388
      else {
wenzelm@56395
   389
        val status = Protocol.Status.make(results.iterator.flatMap(_.info))
wenzelm@49356
   390
wenzelm@50895
   391
        if (status.is_running) Some(running_color)
wenzelm@56980
   392
        else if (status.is_failed) Some(error_color)
wenzelm@56395
   393
        else if (status.is_warned) Some(warning_color)
wenzelm@49356
   394
        else if (status.is_unprocessed) Some(unprocessed_color)
wenzelm@49356
   395
        else None
wenzelm@49356
   396
      }
wenzelm@49356
   397
    }
wenzelm@49356
   398
  }
wenzelm@49356
   399
wenzelm@49356
   400
wenzelm@62986
   401
  /* caret focus */
wenzelm@62986
   402
wenzelm@62986
   403
  def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
wenzelm@62986
   404
  {
wenzelm@63234
   405
    def focus(is_def: Boolean): Set[Long] =
wenzelm@63234
   406
    {
wenzelm@63234
   407
      val results =
wenzelm@63234
   408
        snapshot.cumulate[Set[Long]](caret_range, Set.empty, Rendering.caret_focus_elements, _ =>
wenzelm@63234
   409
            {
wenzelm@63234
   410
              case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@63234
   411
                props match {
wenzelm@63234
   412
                  case Markup.Entity.Def(i) if is_def => Some(serials + i)
wenzelm@63234
   413
                  case Markup.Entity.Ref(i) if !is_def => Some(serials + i)
wenzelm@63234
   414
                  case _ => None
wenzelm@63234
   415
                }
wenzelm@63234
   416
              case _ => None
wenzelm@63234
   417
            })
wenzelm@63234
   418
      (Set.empty[Long] /: results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
wenzelm@63234
   419
    }
wenzelm@63234
   420
wenzelm@63234
   421
    val focus_defs = focus(true)
wenzelm@63235
   422
wenzelm@63234
   423
    if (focus_defs.nonEmpty) focus_defs
wenzelm@63234
   424
    else {
wenzelm@63234
   425
      val focus_refs = focus(false)
wenzelm@63235
   426
      def visible_def: Boolean =
wenzelm@63235
   427
        snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
wenzelm@62986
   428
          {
wenzelm@63234
   429
            case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@62986
   430
              props match {
wenzelm@63234
   431
                case Markup.Entity.Def(i) if focus_refs(i) => Some(true)
wenzelm@62986
   432
                case _ => None
wenzelm@62986
   433
              }
wenzelm@63235
   434
          }).exists(info => info.info)
wenzelm@63235
   435
      if (focus_refs.nonEmpty && visible_def) focus_refs
wenzelm@63234
   436
      else Set.empty[Long]
wenzelm@63234
   437
    }
wenzelm@62986
   438
  }
wenzelm@62986
   439
wenzelm@63236
   440
  def caret_focus_ranges(caret_range: Text.Range, visible_range: Text.Range): List[Text.Range] =
wenzelm@63236
   441
  {
wenzelm@63236
   442
    val focus = caret_focus(caret_range, visible_range)
wenzelm@63236
   443
    if (focus.nonEmpty) {
wenzelm@63236
   444
      val results =
wenzelm@63236
   445
        snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
wenzelm@63236
   446
          {
wenzelm@63236
   447
            case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@63236
   448
              props match {
wenzelm@63236
   449
                case Markup.Entity.Def(i) if focus(i) => Some(true)
wenzelm@63236
   450
                case Markup.Entity.Ref(i) if focus(i) => Some(true)
wenzelm@63236
   451
                case _ => None
wenzelm@63236
   452
              }
wenzelm@63236
   453
          })
wenzelm@63236
   454
      for (info <- results if info.info) yield info.range
wenzelm@63236
   455
    }
wenzelm@63236
   456
    else Nil
wenzelm@63236
   457
  }
wenzelm@63236
   458
wenzelm@62991
   459
  def entity_ref(range: Text.Range, focus: Set[Long]): List[Text.Info[Color]] =
wenzelm@62988
   460
    snapshot.select(range, Rendering.caret_focus_elements, _ =>
wenzelm@62988
   461
      {
wenzelm@62991
   462
        case Text.Info(_, XML.Elem(Markup(Markup.ENTITY, Markup.Entity.Ref(i)), _)) if focus(i) =>
wenzelm@62991
   463
          Some(entity_ref_color)
wenzelm@62988
   464
        case _ => None
wenzelm@62988
   465
      })
wenzelm@62988
   466
wenzelm@62986
   467
wenzelm@55626
   468
  /* highlighted area */
wenzelm@49356
   469
wenzelm@49358
   470
  def highlight(range: Text.Range): Option[Text.Info[Color]] =
wenzelm@55651
   471
    snapshot.select(range, Rendering.highlight_elements, _ =>
wenzelm@55620
   472
      {
wenzelm@55620
   473
        case info => Some(Text.Info(snapshot.convert(info.range), highlight_color))
wenzelm@55689
   474
      }).headOption.map(_.info)
wenzelm@49356
   475
wenzelm@49356
   476
wenzelm@55626
   477
  /* hyperlinks */
wenzelm@55626
   478
wenzelm@56458
   479
  private def jedit_file(name: String): String =
wenzelm@56458
   480
    if (Path.is_valid(name))
wenzelm@56458
   481
      PIDE.resources.append(snapshot.node_name.master_dir, Path.explode(name))
wenzelm@56458
   482
    else name
wenzelm@56458
   483
wenzelm@52980
   484
  def hyperlink(range: Text.Range): Option[Text.Info[PIDE.editor.Hyperlink]] =
wenzelm@49356
   485
  {
wenzelm@55651
   486
    snapshot.cumulate[Vector[Text.Info[PIDE.editor.Hyperlink]]](
wenzelm@55647
   487
      range, Vector.empty, Rendering.hyperlink_elements, _ =>
wenzelm@49356
   488
        {
wenzelm@56458
   489
          case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _))) =>
wenzelm@60893
   490
            val link = PIDE.editor.hyperlink_file(true, jedit_file(name))
wenzelm@55622
   491
            Some(links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@49356
   492
wenzelm@61660
   493
          case (links, Text.Info(info_range, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@61660
   494
            PIDE.editor.hyperlink_doc(name).map(link =>
wenzelm@61660
   495
              (links :+ Text.Info(snapshot.convert(info_range), link)))
wenzelm@61660
   496
wenzelm@54702
   497
          case (links, Text.Info(info_range, XML.Elem(Markup.Url(name), _))) =>
wenzelm@54702
   498
            val link = PIDE.editor.hyperlink_url(name)
wenzelm@55622
   499
            Some(links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@54702
   500
wenzelm@50201
   501
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
wenzelm@49815
   502
          if !props.exists(
wenzelm@50201
   503
            { case (Markup.KIND, Markup.ML_OPEN) => true
wenzelm@55837
   504
              case (Markup.KIND, Markup.ML_STRUCTURE) => true
wenzelm@49815
   505
              case _ => false }) =>
wenzelm@60893
   506
            val opt_link = PIDE.editor.hyperlink_def_position(true, snapshot, props)
wenzelm@60215
   507
            opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@49356
   508
wenzelm@55547
   509
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
wenzelm@60893
   510
            val opt_link = PIDE.editor.hyperlink_position(true, snapshot, props)
wenzelm@60215
   511
            opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@52889
   512
wenzelm@58545
   513
          case (links, Text.Info(info_range, XML.Elem(Markup.Citation(name), _))) =>
wenzelm@58545
   514
            val opt_link =
wenzelm@58545
   515
              Bibtex_JEdit.entries_iterator.collectFirst(
wenzelm@58545
   516
                { case (a, buffer, offset) if a == name =>
wenzelm@60893
   517
                    PIDE.editor.hyperlink_buffer(true, buffer, offset) })
wenzelm@60215
   518
            opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@58545
   519
wenzelm@52889
   520
          case _ => None
wenzelm@55622
   521
        }) match { case Text.Info(_, _ :+ info) :: _ => Some(info) case _ => None }
wenzelm@49356
   522
  }
wenzelm@49356
   523
wenzelm@49356
   524
wenzelm@55626
   525
  /* active elements */
wenzelm@55626
   526
wenzelm@50450
   527
  def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
wenzelm@56354
   528
    snapshot.select(range, Rendering.active_elements, command_states =>
wenzelm@55620
   529
      {
wenzelm@55620
   530
        case Text.Info(info_range, elem) =>
wenzelm@55623
   531
          if (elem.name == Markup.DIALOG) {
wenzelm@55623
   532
            elem match {
wenzelm@55623
   533
              case Protocol.Dialog(_, serial, _)
wenzelm@56354
   534
              if !command_states.exists(st => st.results.defined(serial)) =>
wenzelm@55623
   535
                Some(Text.Info(snapshot.convert(info_range), elem))
wenzelm@55623
   536
              case _ => None
wenzelm@55623
   537
            }
wenzelm@55623
   538
          }
wenzelm@55623
   539
          else Some(Text.Info(snapshot.convert(info_range), elem))
wenzelm@55689
   540
      }).headOption.map(_.info)
wenzelm@49492
   541
wenzelm@50502
   542
  def command_results(range: Text.Range): Command.Results =
wenzelm@56354
   543
    Command.State.merge_results(
wenzelm@56743
   544
      snapshot.select[List[Command.State]](range, Markup.Elements.full, command_states =>
wenzelm@56354
   545
        { case _ => Some(command_states) }).flatMap(_.info))
wenzelm@50502
   546
wenzelm@55626
   547
wenzelm@55626
   548
  /* tooltip messages */
wenzelm@55626
   549
wenzelm@51496
   550
  def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@49356
   551
  {
wenzelm@51496
   552
    val results =
wenzelm@55651
   553
      snapshot.cumulate[List[Text.Info[Command.Results.Entry]]](
wenzelm@55647
   554
        range, Nil, Rendering.tooltip_message_elements, _ =>
wenzelm@51496
   555
        {
wenzelm@51496
   556
          case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
wenzelm@59319
   557
          if body.nonEmpty =>
wenzelm@52530
   558
            val entry: Command.Results.Entry = (Document_ID.make(), msg)
wenzelm@52889
   559
            Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
wenzelm@52889
   560
wenzelm@59184
   561
          case (msgs, Text.Info(info_range,
wenzelm@59184
   562
            XML.Elem(Markup(name, props @ Markup.Serial(serial)), body))) =>
wenzelm@59184
   563
            val entry: Command.Results.Entry =
wenzelm@60215
   564
              serial -> XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@59184
   565
            Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
wenzelm@59184
   566
wenzelm@52889
   567
          case _ => None
wenzelm@52900
   568
        }).flatMap(_.info)
wenzelm@51496
   569
    if (results.isEmpty) None
wenzelm@51496
   570
    else {
wenzelm@51496
   571
      val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@51496
   572
      val msgs = Command.Results.make(results.map(_.info))
wenzelm@56372
   573
      Some(Text.Info(r, Pretty.separate(msgs.iterator.map(_._2).toList)))
wenzelm@51496
   574
    }
wenzelm@49356
   575
  }
wenzelm@49356
   576
wenzelm@49356
   577
wenzelm@55626
   578
  /* tooltips */
wenzelm@55626
   579
wenzelm@49700
   580
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@61873
   581
    Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
wenzelm@49356
   582
wenzelm@55647
   583
  private def timing_threshold: Double = options.real("jedit_timing_threshold")
wenzelm@51588
   584
wenzelm@51496
   585
  def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@49356
   586
  {
wenzelm@55622
   587
    def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
wenzelm@55622
   588
      r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
wenzelm@51496
   589
    {
wenzelm@51496
   590
      val r = snapshot.convert(r0)
wenzelm@51588
   591
      val (t, info) = prev.info
wenzelm@55622
   592
      if (prev.range == r)
wenzelm@55622
   593
        Text.Info(r, (t, info :+ p))
wenzelm@55622
   594
      else Text.Info(r, (t, Vector(p)))
wenzelm@51496
   595
    }
wenzelm@49356
   596
wenzelm@51496
   597
    val results =
wenzelm@55651
   598
      snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
wenzelm@55647
   599
        range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
wenzelm@49356
   600
        {
wenzelm@51588
   601
          case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
wenzelm@52889
   602
            Some(Text.Info(r, (t1 + t2, info)))
wenzelm@61450
   603
wenzelm@63340
   604
          case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
wenzelm@63340
   605
          if kind != "" &&
wenzelm@63340
   606
            kind != Markup.ML_DEF &&
wenzelm@63340
   607
            kind != Markup.ML_OPEN &&
wenzelm@63340
   608
            kind != Markup.ML_STRUCTURE =>
wenzelm@56600
   609
            val kind1 = Word.implode(Word.explode('_', kind))
wenzelm@58048
   610
            val txt1 =
wenzelm@58048
   611
              if (name == "") kind1
wenzelm@63337
   612
              else if (kind1 == "") quote(name)
wenzelm@58048
   613
              else kind1 + " " + quote(name)
wenzelm@51588
   614
            val t = prev.info._1
wenzelm@51588
   615
            val txt2 =
wenzelm@51588
   616
              if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
wenzelm@51588
   617
                "\n" + t.message
wenzelm@51588
   618
              else ""
wenzelm@52889
   619
            Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
wenzelm@61450
   620
wenzelm@56458
   621
          case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
wenzelm@56458
   622
            val file = jedit_file(name)
wenzelm@56458
   623
            val text =
wenzelm@56458
   624
              if (name == file) "file " + quote(file)
wenzelm@56458
   625
              else "path " + quote(name) + "\nfile " + quote(file)
wenzelm@56134
   626
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@61450
   627
wenzelm@61718
   628
          case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@61718
   629
            val text = "doc " + quote(name)
wenzelm@61718
   630
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@61718
   631
wenzelm@54702
   632
          case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
wenzelm@54702
   633
            Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
wenzelm@61450
   634
wenzelm@49674
   635
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@50201
   636
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@52889
   637
            Some(add(prev, r, (true, pretty_typing("::", body))))
wenzelm@61450
   638
wenzelm@63347
   639
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
wenzelm@63347
   640
            Some(add(prev, r, (true, Pretty.block(0, body))))
wenzelm@63347
   641
wenzelm@50201
   642
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@52889
   643
            Some(add(prev, r, (false, pretty_typing("ML:", body))))
wenzelm@61450
   644
wenzelm@60882
   645
          case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
wenzelm@60882
   646
            val text =
wenzelm@60882
   647
              if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
wenzelm@60882
   648
              else "breakpoint (disabled)"
wenzelm@60882
   649
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@62806
   650
wenzelm@55828
   651
          case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
wenzelm@62772
   652
            val lang = Word.implode(Word.explode('_', language))
wenzelm@62772
   653
            Some(add(prev, r, (true, XML.Text("language: " + lang))))
wenzelm@61450
   654
wenzelm@62806
   655
          case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
wenzelm@62806
   656
            val descr = if (kind == "") "expression" else "expression: " + kind
wenzelm@62806
   657
            Some(add(prev, r, (true, XML.Text(descr))))
wenzelm@62806
   658
wenzelm@61450
   659
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
wenzelm@61450
   660
            Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
wenzelm@61450
   661
          case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
wenzelm@61450
   662
            Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
wenzelm@61450
   663
wenzelm@52889
   664
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
wenzelm@55647
   665
            Rendering.tooltip_descriptions.get(name).
wenzelm@55647
   666
              map(descr => add(prev, r, (true, XML.Text(descr))))
wenzelm@52900
   667
        }).map(_.info)
wenzelm@49356
   668
wenzelm@55622
   669
    results.map(_.info).flatMap(res => res._2.toList) match {
wenzelm@51496
   670
      case Nil => None
wenzelm@51496
   671
      case tips =>
wenzelm@51496
   672
        val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@51496
   673
        val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@61873
   674
        Some(Text.Info(r, Library.separate(Pretty.fbrk, all_tips)))
wenzelm@51496
   675
    }
wenzelm@49356
   676
  }
wenzelm@49356
   677
wenzelm@55647
   678
  def tooltip_margin: Int = options.int("jedit_tooltip_margin")
wenzelm@50554
   679
wenzelm@52873
   680
  lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
wenzelm@52873
   681
  lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
wenzelm@52472
   682
wenzelm@52472
   683
wenzelm@56495
   684
  /* gutter */
wenzelm@56495
   685
wenzelm@56495
   686
  private def gutter_message_pri(msg: XML.Tree): Int =
wenzelm@56495
   687
    if (Protocol.is_error(msg)) Rendering.error_pri
wenzelm@56495
   688
    else if (Protocol.is_legacy(msg)) Rendering.legacy_pri
wenzelm@56495
   689
    else if (Protocol.is_warning(msg)) Rendering.warning_pri
wenzelm@56495
   690
    else if (Protocol.is_information(msg)) Rendering.information_pri
wenzelm@56495
   691
    else 0
wenzelm@55626
   692
wenzelm@59129
   693
  private lazy val gutter_message_content = Map(
wenzelm@59129
   694
    Rendering.information_pri ->
wenzelm@59129
   695
      (JEdit_Lib.load_icon(options.string("gutter_information_icon")), information_message_color),
wenzelm@59129
   696
    Rendering.warning_pri ->
wenzelm@59129
   697
      (JEdit_Lib.load_icon(options.string("gutter_warning_icon")), warning_message_color),
wenzelm@59129
   698
    Rendering.legacy_pri ->
wenzelm@59129
   699
      (JEdit_Lib.load_icon(options.string("gutter_legacy_icon")), warning_message_color),
wenzelm@59129
   700
    Rendering.error_pri ->
wenzelm@59129
   701
      (JEdit_Lib.load_icon(options.string("gutter_error_icon")), error_message_color))
wenzelm@49356
   702
wenzelm@59129
   703
  def gutter_content(range: Text.Range): Option[(Icon, Color)] =
wenzelm@49356
   704
  {
wenzelm@56495
   705
    val pris =
wenzelm@55651
   706
      snapshot.cumulate[Int](range, 0, Rendering.gutter_elements, _ =>
wenzelm@49356
   707
        {
wenzelm@56495
   708
          case (pri, Text.Info(_, msg @ XML.Elem(Markup(_, Markup.Serial(serial)), _))) =>
wenzelm@56495
   709
            Some(pri max gutter_message_pri(msg))
wenzelm@52889
   710
          case _ => None
wenzelm@56495
   711
        }).map(_.info)
wenzelm@56495
   712
wenzelm@59129
   713
    gutter_message_content.get((0 /: pris)(_ max _))
wenzelm@49356
   714
  }
wenzelm@49356
   715
wenzelm@49356
   716
wenzelm@55626
   717
  /* squiggly underline */
wenzelm@55626
   718
wenzelm@55647
   719
  private lazy val squiggly_colors = Map(
wenzelm@50199
   720
    Rendering.writeln_pri -> writeln_color,
wenzelm@52650
   721
    Rendering.information_pri -> information_color,
wenzelm@50199
   722
    Rendering.warning_pri -> warning_color,
wenzelm@59203
   723
    Rendering.legacy_pri -> legacy_color,
wenzelm@50199
   724
    Rendering.error_pri -> error_color)
wenzelm@49356
   725
wenzelm@52900
   726
  def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
wenzelm@49356
   727
  {
wenzelm@49356
   728
    val results =
wenzelm@55651
   729
      snapshot.cumulate[Int](range, 0, Rendering.squiggly_elements, _ =>
wenzelm@49356
   730
        {
wenzelm@59184
   731
          case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
wenzelm@49356
   732
        })
wenzelm@49356
   733
    for {
wenzelm@49356
   734
      Text.Info(r, pri) <- results
wenzelm@49356
   735
      color <- squiggly_colors.get(pri)
wenzelm@49356
   736
    } yield Text.Info(r, color)
wenzelm@49356
   737
  }
wenzelm@49356
   738
wenzelm@49474
   739
wenzelm@55626
   740
  /* message output */
wenzelm@55626
   741
wenzelm@55647
   742
  private lazy val message_colors = Map(
wenzelm@50199
   743
    Rendering.writeln_pri -> writeln_message_color,
wenzelm@52650
   744
    Rendering.information_pri -> information_message_color,
wenzelm@50199
   745
    Rendering.tracing_pri -> tracing_message_color,
wenzelm@50199
   746
    Rendering.warning_pri -> warning_message_color,
wenzelm@59203
   747
    Rendering.legacy_pri -> legacy_message_color,
wenzelm@50199
   748
    Rendering.error_pri -> error_message_color)
wenzelm@49474
   749
wenzelm@49473
   750
  def line_background(range: Text.Range): Option[(Color, Boolean)] =
wenzelm@49473
   751
  {
wenzelm@49474
   752
    val results =
wenzelm@55651
   753
      snapshot.cumulate[Int](range, 0, Rendering.line_background_elements, _ =>
wenzelm@49473
   754
        {
wenzelm@59184
   755
          case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
wenzelm@49474
   756
        })
wenzelm@49474
   757
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@49474
   758
wenzelm@61719
   759
    message_colors.get(pri).map(message_color =>
wenzelm@61719
   760
      {
wenzelm@61719
   761
        val is_separator =
wenzelm@61719
   762
          snapshot.cumulate[Boolean](range, false, Rendering.separator_elements, _ =>
wenzelm@61719
   763
            {
wenzelm@61719
   764
              case _ => Some(true)
wenzelm@61719
   765
            }).exists(_.info)
wenzelm@61719
   766
        (message_color, is_separator)
wenzelm@61719
   767
      })
wenzelm@49473
   768
  }
wenzelm@49356
   769
wenzelm@56299
   770
  def output_messages(results: Command.Results): List[XML.Tree] =
wenzelm@57691
   771
  {
wenzelm@57691
   772
    val (states, other) =
wenzelm@57691
   773
      results.iterator.map(_._2).filterNot(Protocol.is_result(_)).toList
wenzelm@57691
   774
        .partition(Protocol.is_state(_))
wenzelm@61727
   775
    states ::: other
wenzelm@57691
   776
  }
wenzelm@50500
   777
wenzelm@50500
   778
wenzelm@55626
   779
  /* text background */
wenzelm@55626
   780
wenzelm@62986
   781
  def background(range: Text.Range, focus: Set[Long]): List[Text.Info[Color]] =
wenzelm@49356
   782
  {
wenzelm@61722
   783
    for {
wenzelm@61722
   784
      Text.Info(r, result) <-
wenzelm@61722
   785
        snapshot.cumulate[(List[Markup], Option[Color])](
wenzelm@61722
   786
          range, (List(Markup.Empty), None), Rendering.background_elements,
wenzelm@61722
   787
          command_states =>
wenzelm@61722
   788
            {
wenzelm@61722
   789
              case (((status, color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@61722
   790
              if status.nonEmpty && Protocol.proper_status_elements(markup.name) =>
wenzelm@61722
   791
                Some((markup :: status, color))
wenzelm@61722
   792
              case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
wenzelm@61722
   793
                Some((Nil, Some(bad_color)))
wenzelm@61722
   794
              case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
wenzelm@61722
   795
                Some((Nil, Some(intensify_color)))
wenzelm@62986
   796
              case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@62986
   797
                props match {
wenzelm@62988
   798
                  case Markup.Entity.Def(i) if focus(i) => Some((Nil, Some(entity_color)))
wenzelm@62988
   799
                  case Markup.Entity.Ref(i) if focus(i) => Some((Nil, Some(entity_color)))
wenzelm@62986
   800
                  case _ => None
wenzelm@62986
   801
                }
wenzelm@61722
   802
              case (_, Text.Info(_, XML.Elem(Markup.Markdown_Item(depth), _))) =>
wenzelm@61722
   803
                val color =
wenzelm@61722
   804
                  depth match {
wenzelm@61722
   805
                    case 1 => markdown_item_color1
wenzelm@61722
   806
                    case 2 => markdown_item_color2
wenzelm@61722
   807
                    case 3 => markdown_item_color3
wenzelm@61722
   808
                    case _ => markdown_item_color4
wenzelm@55647
   809
                  }
wenzelm@61722
   810
                Some((Nil, Some(color)))
wenzelm@61722
   811
              case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
wenzelm@61722
   812
                command_states.collectFirst(
wenzelm@61722
   813
                  { case st if st.results.defined(serial) => st.results.get(serial).get }) match
wenzelm@61722
   814
                {
wenzelm@61722
   815
                  case Some(Protocol.Dialog_Result(res)) if res == result =>
wenzelm@61722
   816
                    Some((Nil, Some(active_result_color)))
wenzelm@61722
   817
                  case _ =>
wenzelm@61722
   818
                    Some((Nil, Some(active_color)))
wenzelm@61722
   819
                }
wenzelm@61722
   820
              case (_, Text.Info(_, elem)) =>
wenzelm@61722
   821
                if (Rendering.active_elements(elem.name)) Some((Nil, Some(active_color)))
wenzelm@61722
   822
                else None
wenzelm@61722
   823
            })
wenzelm@61722
   824
      color <-
wenzelm@61722
   825
        (result match {
wenzelm@61722
   826
          case (markups, opt_color) if markups.nonEmpty =>
wenzelm@61722
   827
            val status = Protocol.Status.make(markups.iterator)
wenzelm@61722
   828
            if (status.is_unprocessed) Some(unprocessed1_color)
wenzelm@61722
   829
            else if (status.is_running) Some(running1_color)
wenzelm@61722
   830
            else opt_color
wenzelm@61722
   831
          case (_, opt_color) => opt_color
wenzelm@61722
   832
        })
wenzelm@61722
   833
    } yield Text.Info(r, color)
wenzelm@49356
   834
  }
wenzelm@49356
   835
wenzelm@49356
   836
wenzelm@55626
   837
  /* text foreground */
wenzelm@51574
   838
wenzelm@52900
   839
  def foreground(range: Text.Range): List[Text.Info[Color]] =
wenzelm@55651
   840
    snapshot.select(range, Rendering.foreground_elements, _ =>
wenzelm@49356
   841
      {
wenzelm@52890
   842
        case Text.Info(_, elem) =>
wenzelm@55620
   843
          if (elem.name == Markup.ANTIQUOTED) Some(antiquoted_color) else Some(quoted_color)
wenzelm@49356
   844
      })
wenzelm@49356
   845
wenzelm@49356
   846
wenzelm@55626
   847
  /* text color */
wenzelm@55626
   848
wenzelm@61175
   849
  val foreground_color = jEdit.getColorProperty("view.fgColor")
wenzelm@61175
   850
wenzelm@55647
   851
  private lazy val text_colors: Map[String, Color] = Map(
wenzelm@50201
   852
      Markup.KEYWORD1 -> keyword1_color,
wenzelm@50201
   853
      Markup.KEYWORD2 -> keyword2_color,
wenzelm@55765
   854
      Markup.KEYWORD3 -> keyword3_color,
wenzelm@55919
   855
      Markup.QUASI_KEYWORD -> quasi_keyword_color,
wenzelm@56202
   856
      Markup.IMPROPER -> improper_color,
wenzelm@56064
   857
      Markup.OPERATOR -> operator_color,
wenzelm@61175
   858
      Markup.STRING -> foreground_color,
wenzelm@61175
   859
      Markup.ALT_STRING -> foreground_color,
wenzelm@61175
   860
      Markup.VERBATIM -> foreground_color,
wenzelm@61175
   861
      Markup.CARTOUCHE -> foreground_color,
wenzelm@50201
   862
      Markup.LITERAL -> keyword1_color,
wenzelm@61175
   863
      Markup.DELIMITER -> foreground_color,
wenzelm@50201
   864
      Markup.TFREE -> tfree_color,
wenzelm@50201
   865
      Markup.TVAR -> tvar_color,
wenzelm@50201
   866
      Markup.FREE -> free_color,
wenzelm@50201
   867
      Markup.SKOLEM -> skolem_color,
wenzelm@50201
   868
      Markup.BOUND -> bound_color,
wenzelm@50201
   869
      Markup.VAR -> var_color,
wenzelm@50201
   870
      Markup.INNER_STRING -> inner_quoted_color,
wenzelm@55033
   871
      Markup.INNER_CARTOUCHE -> inner_cartouche_color,
wenzelm@50201
   872
      Markup.INNER_COMMENT -> inner_comment_color,
wenzelm@50201
   873
      Markup.DYNAMIC_FACT -> dynamic_color,
wenzelm@63347
   874
      Markup.CLASS_PARAMETER -> class_parameter_color,
wenzelm@55526
   875
      Markup.ANTIQUOTE -> antiquote_color,
wenzelm@55505
   876
      Markup.ML_KEYWORD1 -> keyword1_color,
wenzelm@55505
   877
      Markup.ML_KEYWORD2 -> keyword2_color,
wenzelm@55505
   878
      Markup.ML_KEYWORD3 -> keyword3_color,
wenzelm@61175
   879
      Markup.ML_DELIMITER -> foreground_color,
wenzelm@50201
   880
      Markup.ML_NUMERAL -> inner_numeral_color,
wenzelm@50201
   881
      Markup.ML_CHAR -> inner_quoted_color,
wenzelm@50201
   882
      Markup.ML_STRING -> inner_quoted_color,
wenzelm@56278
   883
      Markup.ML_COMMENT -> inner_comment_color,
wenzelm@56278
   884
      Markup.SML_STRING -> inner_quoted_color,
wenzelm@56278
   885
      Markup.SML_COMMENT -> inner_comment_color)
wenzelm@49356
   886
wenzelm@55820
   887
  private lazy val text_color_elements =
wenzelm@56743
   888
    Markup.Elements(text_colors.keySet)
wenzelm@49356
   889
wenzelm@52900
   890
  def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
wenzelm@49356
   891
  {
wenzelm@52900
   892
    if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
wenzelm@50196
   893
    else
wenzelm@55651
   894
      snapshot.cumulate(range, color, text_color_elements, _ =>
wenzelm@50196
   895
        {
wenzelm@52890
   896
          case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
wenzelm@50196
   897
        })
wenzelm@49356
   898
  }
wenzelm@50542
   899
wenzelm@50542
   900
wenzelm@55626
   901
  /* virtual bullets */
wenzelm@55626
   902
wenzelm@55626
   903
  def bullet(range: Text.Range): List[Text.Info[Color]] =
wenzelm@60913
   904
    snapshot.select(range, Rendering.bullet_elements, _ =>
wenzelm@60913
   905
      {
wenzelm@60913
   906
        case Text.Info(_, Protocol.ML_Breakpoint(breakpoint)) =>
wenzelm@60913
   907
          Debugger.active_breakpoint_state(breakpoint).map(b =>
wenzelm@60913
   908
            if (b) breakpoint_enabled_color else breakpoint_disabled_color)
wenzelm@60913
   909
        case _ => Some(bullet_color)
wenzelm@60913
   910
      })
wenzelm@55626
   911
wenzelm@55626
   912
wenzelm@55626
   913
  /* text folds */
wenzelm@50542
   914
wenzelm@52900
   915
  def fold_depth(range: Text.Range): List[Text.Info[Int]] =
wenzelm@55651
   916
    snapshot.cumulate[Int](range, 0, Rendering.fold_depth_elements, _ =>
wenzelm@50542
   917
      {
wenzelm@55620
   918
        case (depth, _) => Some(depth + 1)
wenzelm@50542
   919
      })
wenzelm@49356
   920
}