src/Tools/jEdit/src/rendering.scala
author wenzelm
Thu Apr 14 22:55:53 2016 +0200 (2016-04-14)
changeset 62986 9d2fae6b31cc
parent 62806 de9bf8171626
child 62988 224e8d8a4fb8
permissions -rw-r--r--
background color for entity def/ref focus;
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@55647
   159
      Markup.TYPING, 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@57595
   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@62806
   188
      Markup.SORTING, Markup.TYPING, Markup.ML_TYPING, Markup.ML_BREAKPOINT, Markup.PATH,
wenzelm@62806
   189
      Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH, Markup.Markdown_List.name) ++
wenzelm@56743
   190
    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@62986
   256
  val entity_def_color = color_value("entity_def_color")
wenzelm@62986
   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@49356
   290
wenzelm@61449
   291
  val markdown_item_color1 = color_value("markdown_item_color1")
wenzelm@61449
   292
  val markdown_item_color2 = color_value("markdown_item_color2")
wenzelm@61449
   293
  val markdown_item_color3 = color_value("markdown_item_color3")
wenzelm@61449
   294
  val markdown_item_color4 = color_value("markdown_item_color4")
wenzelm@61449
   295
wenzelm@49356
   296
wenzelm@55674
   297
  /* completion */
wenzelm@55674
   298
wenzelm@61601
   299
  def semantic_completion(completed_range: Option[Text.Range], range: Text.Range)
wenzelm@61601
   300
      : Option[Text.Info[Completion.Semantic]] =
wenzelm@55690
   301
    if (snapshot.is_outdated) None
wenzelm@55690
   302
    else {
wenzelm@56173
   303
      snapshot.select(range, Rendering.semantic_completion_elements, _ =>
wenzelm@55688
   304
        {
wenzelm@61601
   305
          case Completion.Semantic.Info(info) =>
wenzelm@61601
   306
            completed_range match {
wenzelm@61601
   307
              case Some(range0) if range0.contains(info.range) && range0 != info.range => None
wenzelm@61601
   308
              case _ => Some(info)
wenzelm@61601
   309
            }
wenzelm@55688
   310
          case _ => None
wenzelm@55688
   311
        }).headOption.map(_.info)
wenzelm@55674
   312
    }
wenzelm@55615
   313
wenzelm@56173
   314
  def language_context(range: Text.Range): Option[Completion.Language_Context] =
wenzelm@56173
   315
    snapshot.select(range, Rendering.language_context_elements, _ =>
wenzelm@55690
   316
      {
wenzelm@55828
   317
        case Text.Info(_, XML.Elem(Markup.Language(language, symbols, antiquotes, delimited), _)) =>
wenzelm@55828
   318
          if (delimited) Some(Completion.Language_Context(language, symbols, antiquotes))
wenzelm@55828
   319
          else None
wenzelm@55690
   320
        case Text.Info(_, elem)
wenzelm@61598
   321
        if elem.name == Markup.ML_STRING || elem.name == Markup.ML_COMMENT =>
wenzelm@55749
   322
          Some(Completion.Language_Context.ML_inner)
wenzelm@55690
   323
        case Text.Info(_, _) =>
wenzelm@55749
   324
          Some(Completion.Language_Context.inner)
wenzelm@55690
   325
      }).headOption.map(_.info)
wenzelm@55615
   326
wenzelm@56843
   327
  def language_path(range: Text.Range): Option[Text.Range] =
wenzelm@56843
   328
    snapshot.select(range, Rendering.language_elements, _ =>
wenzelm@56843
   329
      {
wenzelm@56843
   330
        case Text.Info(info_range, XML.Elem(Markup.Language(Markup.Language.PATH, _, _, _), _)) =>
wenzelm@56843
   331
          Some(snapshot.convert(info_range))
wenzelm@56843
   332
        case _ => None
wenzelm@56843
   333
      }).headOption.map(_.info)
wenzelm@56843
   334
wenzelm@58592
   335
  def citation(range: Text.Range): Option[Text.Info[String]] =
wenzelm@58592
   336
    snapshot.select(range, Rendering.citation_elements, _ =>
wenzelm@58592
   337
      {
wenzelm@58592
   338
        case Text.Info(info_range, XML.Elem(Markup.Citation(name), _)) =>
wenzelm@58592
   339
          Some(Text.Info(snapshot.convert(info_range), name))
wenzelm@58592
   340
        case _ => None
wenzelm@58592
   341
      }).headOption.map(_.info)
wenzelm@58592
   342
wenzelm@55615
   343
wenzelm@56551
   344
  /* spell checker */
wenzelm@56548
   345
wenzelm@56551
   346
  private lazy val spell_checker_elements =
wenzelm@56743
   347
    Markup.Elements(space_explode(',', options.string("spell_checker_elements")): _*)
wenzelm@56551
   348
wenzelm@56551
   349
  def spell_checker_ranges(range: Text.Range): List[Text.Range] =
wenzelm@56551
   350
    snapshot.select(range, spell_checker_elements, _ => _ => Some(())).map(_.range)
wenzelm@56548
   351
wenzelm@56564
   352
  def spell_checker_point(range: Text.Range): Option[Text.Range] =
wenzelm@56564
   353
    snapshot.select(range, spell_checker_elements, _ =>
wenzelm@56564
   354
      {
wenzelm@56564
   355
        case info => Some(snapshot.convert(info.range))
wenzelm@56564
   356
      }).headOption.map(_.info)
wenzelm@56564
   357
wenzelm@56548
   358
wenzelm@60878
   359
  /* breakpoints */
wenzelm@60878
   360
wenzelm@60880
   361
  def breakpoint(range: Text.Range): Option[(Command, Long)] =
wenzelm@60880
   362
    if (snapshot.is_outdated) None
wenzelm@60880
   363
    else
wenzelm@60880
   364
      snapshot.select(range, Rendering.breakpoint_elements, command_states =>
wenzelm@60880
   365
        {
wenzelm@60882
   366
          case Text.Info(_, Protocol.ML_Breakpoint(breakpoint)) =>
wenzelm@60880
   367
            command_states match {
wenzelm@60882
   368
              case st :: _ => Some((st.command, breakpoint))
wenzelm@60880
   369
              case _ => None
wenzelm@60880
   370
            }
wenzelm@60880
   371
          case _ => None
wenzelm@60880
   372
        }).headOption.map(_.info)
wenzelm@60878
   373
wenzelm@60878
   374
wenzelm@55626
   375
  /* command status overview */
wenzelm@49356
   376
wenzelm@49356
   377
  def overview_color(range: Text.Range): Option[Color] =
wenzelm@49356
   378
  {
wenzelm@49356
   379
    if (snapshot.is_outdated) None
wenzelm@49356
   380
    else {
wenzelm@49356
   381
      val results =
wenzelm@56395
   382
        snapshot.cumulate[List[Markup]](range, Nil, Protocol.liberal_status_elements, _ =>
wenzelm@49356
   383
          {
wenzelm@56395
   384
            case (status, Text.Info(_, elem)) => Some(elem.markup :: status)
wenzelm@55651
   385
          }, status = true)
wenzelm@49356
   386
      if (results.isEmpty) None
wenzelm@49356
   387
      else {
wenzelm@56395
   388
        val status = Protocol.Status.make(results.iterator.flatMap(_.info))
wenzelm@49356
   389
wenzelm@50895
   390
        if (status.is_running) Some(running_color)
wenzelm@56980
   391
        else if (status.is_failed) Some(error_color)
wenzelm@56395
   392
        else if (status.is_warned) Some(warning_color)
wenzelm@49356
   393
        else if (status.is_unprocessed) Some(unprocessed_color)
wenzelm@49356
   394
        else None
wenzelm@49356
   395
      }
wenzelm@49356
   396
    }
wenzelm@49356
   397
  }
wenzelm@49356
   398
wenzelm@49356
   399
wenzelm@62986
   400
  /* caret focus */
wenzelm@62986
   401
wenzelm@62986
   402
  def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
wenzelm@62986
   403
  {
wenzelm@62986
   404
    val focus_results =
wenzelm@62986
   405
      snapshot.cumulate[Set[Long]](caret_range, Set.empty, Rendering.caret_focus_elements, _ =>
wenzelm@62986
   406
          {
wenzelm@62986
   407
            case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@62986
   408
              props match {
wenzelm@62986
   409
                case Markup.Entity.Def(i) => Some(serials + i)
wenzelm@62986
   410
                case Markup.Entity.Ref(i) => Some(serials + i)
wenzelm@62986
   411
                case _ => None
wenzelm@62986
   412
              }
wenzelm@62986
   413
            case _ => None
wenzelm@62986
   414
          })
wenzelm@62986
   415
    val focus = (Set.empty[Long] /: focus_results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
wenzelm@62986
   416
wenzelm@62986
   417
    val visible =
wenzelm@62986
   418
      focus.nonEmpty &&
wenzelm@62986
   419
        snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
wenzelm@62986
   420
            {
wenzelm@62986
   421
              case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@62986
   422
                props match {
wenzelm@62986
   423
                  case Markup.Entity.Def(i) if focus(i) => Some(true)
wenzelm@62986
   424
                  case _ => None
wenzelm@62986
   425
                }
wenzelm@62986
   426
            }).exists({ case Text.Info(_, visible) => visible })
wenzelm@62986
   427
    if (visible) focus else Set.empty[Long]
wenzelm@62986
   428
  }
wenzelm@62986
   429
wenzelm@62986
   430
wenzelm@55626
   431
  /* highlighted area */
wenzelm@49356
   432
wenzelm@49358
   433
  def highlight(range: Text.Range): Option[Text.Info[Color]] =
wenzelm@55651
   434
    snapshot.select(range, Rendering.highlight_elements, _ =>
wenzelm@55620
   435
      {
wenzelm@55620
   436
        case info => Some(Text.Info(snapshot.convert(info.range), highlight_color))
wenzelm@55689
   437
      }).headOption.map(_.info)
wenzelm@49356
   438
wenzelm@49356
   439
wenzelm@55626
   440
  /* hyperlinks */
wenzelm@55626
   441
wenzelm@56458
   442
  private def jedit_file(name: String): String =
wenzelm@56458
   443
    if (Path.is_valid(name))
wenzelm@56458
   444
      PIDE.resources.append(snapshot.node_name.master_dir, Path.explode(name))
wenzelm@56458
   445
    else name
wenzelm@56458
   446
wenzelm@52980
   447
  def hyperlink(range: Text.Range): Option[Text.Info[PIDE.editor.Hyperlink]] =
wenzelm@49356
   448
  {
wenzelm@55651
   449
    snapshot.cumulate[Vector[Text.Info[PIDE.editor.Hyperlink]]](
wenzelm@55647
   450
      range, Vector.empty, Rendering.hyperlink_elements, _ =>
wenzelm@49356
   451
        {
wenzelm@56458
   452
          case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _))) =>
wenzelm@60893
   453
            val link = PIDE.editor.hyperlink_file(true, jedit_file(name))
wenzelm@55622
   454
            Some(links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@49356
   455
wenzelm@61660
   456
          case (links, Text.Info(info_range, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@61660
   457
            PIDE.editor.hyperlink_doc(name).map(link =>
wenzelm@61660
   458
              (links :+ Text.Info(snapshot.convert(info_range), link)))
wenzelm@61660
   459
wenzelm@54702
   460
          case (links, Text.Info(info_range, XML.Elem(Markup.Url(name), _))) =>
wenzelm@54702
   461
            val link = PIDE.editor.hyperlink_url(name)
wenzelm@55622
   462
            Some(links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@54702
   463
wenzelm@50201
   464
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
wenzelm@49815
   465
          if !props.exists(
wenzelm@50201
   466
            { case (Markup.KIND, Markup.ML_OPEN) => true
wenzelm@55837
   467
              case (Markup.KIND, Markup.ML_STRUCTURE) => true
wenzelm@49815
   468
              case _ => false }) =>
wenzelm@60893
   469
            val opt_link = PIDE.editor.hyperlink_def_position(true, snapshot, props)
wenzelm@60215
   470
            opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@49356
   471
wenzelm@55545
   472
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
wenzelm@60893
   473
            val opt_link = PIDE.editor.hyperlink_position(true, snapshot, props)
wenzelm@60215
   474
            opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@52889
   475
wenzelm@58545
   476
          case (links, Text.Info(info_range, XML.Elem(Markup.Citation(name), _))) =>
wenzelm@58545
   477
            val opt_link =
wenzelm@58545
   478
              Bibtex_JEdit.entries_iterator.collectFirst(
wenzelm@58545
   479
                { case (a, buffer, offset) if a == name =>
wenzelm@60893
   480
                    PIDE.editor.hyperlink_buffer(true, buffer, offset) })
wenzelm@60215
   481
            opt_link.map(link => links :+ Text.Info(snapshot.convert(info_range), link))
wenzelm@58545
   482
wenzelm@52889
   483
          case _ => None
wenzelm@55622
   484
        }) match { case Text.Info(_, _ :+ info) :: _ => Some(info) case _ => None }
wenzelm@49356
   485
  }
wenzelm@49356
   486
wenzelm@49356
   487
wenzelm@55626
   488
  /* active elements */
wenzelm@55626
   489
wenzelm@50450
   490
  def active(range: Text.Range): Option[Text.Info[XML.Elem]] =
wenzelm@56354
   491
    snapshot.select(range, Rendering.active_elements, command_states =>
wenzelm@55620
   492
      {
wenzelm@55620
   493
        case Text.Info(info_range, elem) =>
wenzelm@55623
   494
          if (elem.name == Markup.DIALOG) {
wenzelm@55623
   495
            elem match {
wenzelm@55623
   496
              case Protocol.Dialog(_, serial, _)
wenzelm@56354
   497
              if !command_states.exists(st => st.results.defined(serial)) =>
wenzelm@55623
   498
                Some(Text.Info(snapshot.convert(info_range), elem))
wenzelm@55623
   499
              case _ => None
wenzelm@55623
   500
            }
wenzelm@55623
   501
          }
wenzelm@55623
   502
          else Some(Text.Info(snapshot.convert(info_range), elem))
wenzelm@55689
   503
      }).headOption.map(_.info)
wenzelm@49492
   504
wenzelm@50502
   505
  def command_results(range: Text.Range): Command.Results =
wenzelm@56354
   506
    Command.State.merge_results(
wenzelm@56743
   507
      snapshot.select[List[Command.State]](range, Markup.Elements.full, command_states =>
wenzelm@56354
   508
        { case _ => Some(command_states) }).flatMap(_.info))
wenzelm@50502
   509
wenzelm@55626
   510
wenzelm@55626
   511
  /* tooltip messages */
wenzelm@55626
   512
wenzelm@51496
   513
  def tooltip_message(range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@49356
   514
  {
wenzelm@51496
   515
    val results =
wenzelm@55651
   516
      snapshot.cumulate[List[Text.Info[Command.Results.Entry]]](
wenzelm@55647
   517
        range, Nil, Rendering.tooltip_message_elements, _ =>
wenzelm@51496
   518
        {
wenzelm@51496
   519
          case (msgs, Text.Info(info_range, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
wenzelm@59319
   520
          if body.nonEmpty =>
wenzelm@52530
   521
            val entry: Command.Results.Entry = (Document_ID.make(), msg)
wenzelm@52889
   522
            Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
wenzelm@52889
   523
wenzelm@59184
   524
          case (msgs, Text.Info(info_range,
wenzelm@59184
   525
            XML.Elem(Markup(name, props @ Markup.Serial(serial)), body))) =>
wenzelm@59184
   526
            val entry: Command.Results.Entry =
wenzelm@60215
   527
              serial -> XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@59184
   528
            Some(Text.Info(snapshot.convert(info_range), entry) :: msgs)
wenzelm@59184
   529
wenzelm@52889
   530
          case _ => None
wenzelm@52900
   531
        }).flatMap(_.info)
wenzelm@51496
   532
    if (results.isEmpty) None
wenzelm@51496
   533
    else {
wenzelm@51496
   534
      val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@51496
   535
      val msgs = Command.Results.make(results.map(_.info))
wenzelm@56372
   536
      Some(Text.Info(r, Pretty.separate(msgs.iterator.map(_._2).toList)))
wenzelm@51496
   537
    }
wenzelm@49356
   538
  }
wenzelm@49356
   539
wenzelm@49356
   540
wenzelm@55626
   541
  /* tooltips */
wenzelm@55626
   542
wenzelm@49700
   543
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@61871
   544
    Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
wenzelm@49356
   545
wenzelm@55647
   546
  private def timing_threshold: Double = options.real("jedit_timing_threshold")
wenzelm@51588
   547
wenzelm@51496
   548
  def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@49356
   549
  {
wenzelm@55622
   550
    def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
wenzelm@55622
   551
      r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
wenzelm@51496
   552
    {
wenzelm@51496
   553
      val r = snapshot.convert(r0)
wenzelm@51588
   554
      val (t, info) = prev.info
wenzelm@55622
   555
      if (prev.range == r)
wenzelm@55622
   556
        Text.Info(r, (t, info :+ p))
wenzelm@55622
   557
      else Text.Info(r, (t, Vector(p)))
wenzelm@51496
   558
    }
wenzelm@49356
   559
wenzelm@51496
   560
    val results =
wenzelm@55651
   561
      snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
wenzelm@55647
   562
        range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
wenzelm@49356
   563
        {
wenzelm@51588
   564
          case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
wenzelm@52889
   565
            Some(Text.Info(r, (t1 + t2, info)))
wenzelm@61450
   566
wenzelm@50201
   567
          case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
wenzelm@56600
   568
            val kind1 = Word.implode(Word.explode('_', kind))
wenzelm@58048
   569
            val txt1 =
wenzelm@58048
   570
              if (name == "") kind1
wenzelm@58048
   571
              else kind1 + " " + quote(name)
wenzelm@51588
   572
            val t = prev.info._1
wenzelm@51588
   573
            val txt2 =
wenzelm@51588
   574
              if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
wenzelm@51588
   575
                "\n" + t.message
wenzelm@51588
   576
              else ""
wenzelm@52889
   577
            Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
wenzelm@61450
   578
wenzelm@56458
   579
          case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
wenzelm@56458
   580
            val file = jedit_file(name)
wenzelm@56458
   581
            val text =
wenzelm@56458
   582
              if (name == file) "file " + quote(file)
wenzelm@56458
   583
              else "path " + quote(name) + "\nfile " + quote(file)
wenzelm@56134
   584
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@61450
   585
wenzelm@61718
   586
          case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@61718
   587
            val text = "doc " + quote(name)
wenzelm@61718
   588
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@61718
   589
wenzelm@54702
   590
          case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
wenzelm@54702
   591
            Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
wenzelm@61450
   592
wenzelm@49674
   593
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@50201
   594
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@52889
   595
            Some(add(prev, r, (true, pretty_typing("::", body))))
wenzelm@61450
   596
wenzelm@50201
   597
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@52889
   598
            Some(add(prev, r, (false, pretty_typing("ML:", body))))
wenzelm@61450
   599
wenzelm@60882
   600
          case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
wenzelm@60882
   601
            val text =
wenzelm@60882
   602
              if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
wenzelm@60882
   603
              else "breakpoint (disabled)"
wenzelm@60882
   604
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@62806
   605
wenzelm@55828
   606
          case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
wenzelm@62772
   607
            val lang = Word.implode(Word.explode('_', language))
wenzelm@62772
   608
            Some(add(prev, r, (true, XML.Text("language: " + lang))))
wenzelm@61450
   609
wenzelm@62806
   610
          case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
wenzelm@62806
   611
            val descr = if (kind == "") "expression" else "expression: " + kind
wenzelm@62806
   612
            Some(add(prev, r, (true, XML.Text(descr))))
wenzelm@62806
   613
wenzelm@61450
   614
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
wenzelm@61450
   615
            Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
wenzelm@61450
   616
          case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
wenzelm@61450
   617
            Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
wenzelm@61450
   618
wenzelm@52889
   619
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
wenzelm@55647
   620
            Rendering.tooltip_descriptions.get(name).
wenzelm@55647
   621
              map(descr => add(prev, r, (true, XML.Text(descr))))
wenzelm@52900
   622
        }).map(_.info)
wenzelm@49356
   623
wenzelm@55622
   624
    results.map(_.info).flatMap(res => res._2.toList) match {
wenzelm@51496
   625
      case Nil => None
wenzelm@51496
   626
      case tips =>
wenzelm@51496
   627
        val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@51496
   628
        val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@61871
   629
        Some(Text.Info(r, Library.separate(Pretty.fbrk, all_tips)))
wenzelm@51496
   630
    }
wenzelm@49356
   631
  }
wenzelm@49356
   632
wenzelm@55647
   633
  def tooltip_margin: Int = options.int("jedit_tooltip_margin")
wenzelm@50554
   634
wenzelm@52873
   635
  lazy val tooltip_close_icon = JEdit_Lib.load_icon(options.string("tooltip_close_icon"))
wenzelm@52873
   636
  lazy val tooltip_detach_icon = JEdit_Lib.load_icon(options.string("tooltip_detach_icon"))
wenzelm@52472
   637
wenzelm@52472
   638
wenzelm@56495
   639
  /* gutter */
wenzelm@56495
   640
wenzelm@56495
   641
  private def gutter_message_pri(msg: XML.Tree): Int =
wenzelm@56495
   642
    if (Protocol.is_error(msg)) Rendering.error_pri
wenzelm@56495
   643
    else if (Protocol.is_legacy(msg)) Rendering.legacy_pri
wenzelm@56495
   644
    else if (Protocol.is_warning(msg)) Rendering.warning_pri
wenzelm@56495
   645
    else if (Protocol.is_information(msg)) Rendering.information_pri
wenzelm@56495
   646
    else 0
wenzelm@55626
   647
wenzelm@59129
   648
  private lazy val gutter_message_content = Map(
wenzelm@59129
   649
    Rendering.information_pri ->
wenzelm@59129
   650
      (JEdit_Lib.load_icon(options.string("gutter_information_icon")), information_message_color),
wenzelm@59129
   651
    Rendering.warning_pri ->
wenzelm@59129
   652
      (JEdit_Lib.load_icon(options.string("gutter_warning_icon")), warning_message_color),
wenzelm@59129
   653
    Rendering.legacy_pri ->
wenzelm@59129
   654
      (JEdit_Lib.load_icon(options.string("gutter_legacy_icon")), warning_message_color),
wenzelm@59129
   655
    Rendering.error_pri ->
wenzelm@59129
   656
      (JEdit_Lib.load_icon(options.string("gutter_error_icon")), error_message_color))
wenzelm@49356
   657
wenzelm@59129
   658
  def gutter_content(range: Text.Range): Option[(Icon, Color)] =
wenzelm@49356
   659
  {
wenzelm@56495
   660
    val pris =
wenzelm@55651
   661
      snapshot.cumulate[Int](range, 0, Rendering.gutter_elements, _ =>
wenzelm@49356
   662
        {
wenzelm@56495
   663
          case (pri, Text.Info(_, msg @ XML.Elem(Markup(_, Markup.Serial(serial)), _))) =>
wenzelm@56495
   664
            Some(pri max gutter_message_pri(msg))
wenzelm@52889
   665
          case _ => None
wenzelm@56495
   666
        }).map(_.info)
wenzelm@56495
   667
wenzelm@59129
   668
    gutter_message_content.get((0 /: pris)(_ max _))
wenzelm@49356
   669
  }
wenzelm@49356
   670
wenzelm@49356
   671
wenzelm@55626
   672
  /* squiggly underline */
wenzelm@55626
   673
wenzelm@55647
   674
  private lazy val squiggly_colors = Map(
wenzelm@50199
   675
    Rendering.writeln_pri -> writeln_color,
wenzelm@52650
   676
    Rendering.information_pri -> information_color,
wenzelm@50199
   677
    Rendering.warning_pri -> warning_color,
wenzelm@59203
   678
    Rendering.legacy_pri -> legacy_color,
wenzelm@50199
   679
    Rendering.error_pri -> error_color)
wenzelm@49356
   680
wenzelm@52900
   681
  def squiggly_underline(range: Text.Range): List[Text.Info[Color]] =
wenzelm@49356
   682
  {
wenzelm@49356
   683
    val results =
wenzelm@55651
   684
      snapshot.cumulate[Int](range, 0, Rendering.squiggly_elements, _ =>
wenzelm@49356
   685
        {
wenzelm@59184
   686
          case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
wenzelm@49356
   687
        })
wenzelm@49356
   688
    for {
wenzelm@49356
   689
      Text.Info(r, pri) <- results
wenzelm@49356
   690
      color <- squiggly_colors.get(pri)
wenzelm@49356
   691
    } yield Text.Info(r, color)
wenzelm@49356
   692
  }
wenzelm@49356
   693
wenzelm@49474
   694
wenzelm@55626
   695
  /* message output */
wenzelm@55626
   696
wenzelm@55647
   697
  private lazy val message_colors = Map(
wenzelm@50199
   698
    Rendering.writeln_pri -> writeln_message_color,
wenzelm@52650
   699
    Rendering.information_pri -> information_message_color,
wenzelm@50199
   700
    Rendering.tracing_pri -> tracing_message_color,
wenzelm@50199
   701
    Rendering.warning_pri -> warning_message_color,
wenzelm@59203
   702
    Rendering.legacy_pri -> legacy_message_color,
wenzelm@50199
   703
    Rendering.error_pri -> error_message_color)
wenzelm@49474
   704
wenzelm@49473
   705
  def line_background(range: Text.Range): Option[(Color, Boolean)] =
wenzelm@49473
   706
  {
wenzelm@49474
   707
    val results =
wenzelm@55651
   708
      snapshot.cumulate[Int](range, 0, Rendering.line_background_elements, _ =>
wenzelm@49473
   709
        {
wenzelm@59184
   710
          case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
wenzelm@49474
   711
        })
wenzelm@49474
   712
    val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
wenzelm@49474
   713
wenzelm@61719
   714
    message_colors.get(pri).map(message_color =>
wenzelm@61719
   715
      {
wenzelm@61719
   716
        val is_separator =
wenzelm@61719
   717
          snapshot.cumulate[Boolean](range, false, Rendering.separator_elements, _ =>
wenzelm@61719
   718
            {
wenzelm@61719
   719
              case _ => Some(true)
wenzelm@61719
   720
            }).exists(_.info)
wenzelm@61719
   721
        (message_color, is_separator)
wenzelm@61719
   722
      })
wenzelm@49473
   723
  }
wenzelm@49356
   724
wenzelm@56299
   725
  def output_messages(results: Command.Results): List[XML.Tree] =
wenzelm@57691
   726
  {
wenzelm@57691
   727
    val (states, other) =
wenzelm@57691
   728
      results.iterator.map(_._2).filterNot(Protocol.is_result(_)).toList
wenzelm@57691
   729
        .partition(Protocol.is_state(_))
wenzelm@61727
   730
    states ::: other
wenzelm@57691
   731
  }
wenzelm@50500
   732
wenzelm@50500
   733
wenzelm@55626
   734
  /* text background */
wenzelm@55626
   735
wenzelm@62986
   736
  def background(range: Text.Range, focus: Set[Long]): List[Text.Info[Color]] =
wenzelm@49356
   737
  {
wenzelm@61722
   738
    for {
wenzelm@61722
   739
      Text.Info(r, result) <-
wenzelm@61722
   740
        snapshot.cumulate[(List[Markup], Option[Color])](
wenzelm@61722
   741
          range, (List(Markup.Empty), None), Rendering.background_elements,
wenzelm@61722
   742
          command_states =>
wenzelm@61722
   743
            {
wenzelm@61722
   744
              case (((status, color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@61722
   745
              if status.nonEmpty && Protocol.proper_status_elements(markup.name) =>
wenzelm@61722
   746
                Some((markup :: status, color))
wenzelm@61722
   747
              case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
wenzelm@61722
   748
                Some((Nil, Some(bad_color)))
wenzelm@61722
   749
              case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
wenzelm@61722
   750
                Some((Nil, Some(intensify_color)))
wenzelm@62986
   751
              case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@62986
   752
                props match {
wenzelm@62986
   753
                  case Markup.Entity.Def(i) if focus(i) =>
wenzelm@62986
   754
                    Some((Nil, Some(entity_def_color)))
wenzelm@62986
   755
                  case Markup.Entity.Ref(i) if focus(i) =>
wenzelm@62986
   756
                    Some((Nil, Some(entity_ref_color)))
wenzelm@62986
   757
                  case _ => None
wenzelm@62986
   758
                }
wenzelm@61722
   759
              case (_, Text.Info(_, XML.Elem(Markup.Markdown_Item(depth), _))) =>
wenzelm@61722
   760
                val color =
wenzelm@61722
   761
                  depth match {
wenzelm@61722
   762
                    case 1 => markdown_item_color1
wenzelm@61722
   763
                    case 2 => markdown_item_color2
wenzelm@61722
   764
                    case 3 => markdown_item_color3
wenzelm@61722
   765
                    case _ => markdown_item_color4
wenzelm@55647
   766
                  }
wenzelm@61722
   767
                Some((Nil, Some(color)))
wenzelm@61722
   768
              case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
wenzelm@61722
   769
                command_states.collectFirst(
wenzelm@61722
   770
                  { case st if st.results.defined(serial) => st.results.get(serial).get }) match
wenzelm@61722
   771
                {
wenzelm@61722
   772
                  case Some(Protocol.Dialog_Result(res)) if res == result =>
wenzelm@61722
   773
                    Some((Nil, Some(active_result_color)))
wenzelm@61722
   774
                  case _ =>
wenzelm@61722
   775
                    Some((Nil, Some(active_color)))
wenzelm@61722
   776
                }
wenzelm@61722
   777
              case (_, Text.Info(_, elem)) =>
wenzelm@61722
   778
                if (Rendering.active_elements(elem.name)) Some((Nil, Some(active_color)))
wenzelm@61722
   779
                else None
wenzelm@61722
   780
            })
wenzelm@61722
   781
      color <-
wenzelm@61722
   782
        (result match {
wenzelm@61722
   783
          case (markups, opt_color) if markups.nonEmpty =>
wenzelm@61722
   784
            val status = Protocol.Status.make(markups.iterator)
wenzelm@61722
   785
            if (status.is_unprocessed) Some(unprocessed1_color)
wenzelm@61722
   786
            else if (status.is_running) Some(running1_color)
wenzelm@61722
   787
            else opt_color
wenzelm@61722
   788
          case (_, opt_color) => opt_color
wenzelm@61722
   789
        })
wenzelm@61722
   790
    } yield Text.Info(r, color)
wenzelm@49356
   791
  }
wenzelm@49356
   792
wenzelm@49356
   793
wenzelm@55626
   794
  /* text foreground */
wenzelm@51574
   795
wenzelm@52900
   796
  def foreground(range: Text.Range): List[Text.Info[Color]] =
wenzelm@55651
   797
    snapshot.select(range, Rendering.foreground_elements, _ =>
wenzelm@49356
   798
      {
wenzelm@52890
   799
        case Text.Info(_, elem) =>
wenzelm@55620
   800
          if (elem.name == Markup.ANTIQUOTED) Some(antiquoted_color) else Some(quoted_color)
wenzelm@49356
   801
      })
wenzelm@49356
   802
wenzelm@49356
   803
wenzelm@55626
   804
  /* text color */
wenzelm@55626
   805
wenzelm@61175
   806
  val foreground_color = jEdit.getColorProperty("view.fgColor")
wenzelm@61175
   807
wenzelm@55647
   808
  private lazy val text_colors: Map[String, Color] = Map(
wenzelm@50201
   809
      Markup.KEYWORD1 -> keyword1_color,
wenzelm@50201
   810
      Markup.KEYWORD2 -> keyword2_color,
wenzelm@55765
   811
      Markup.KEYWORD3 -> keyword3_color,
wenzelm@55919
   812
      Markup.QUASI_KEYWORD -> quasi_keyword_color,
wenzelm@56202
   813
      Markup.IMPROPER -> improper_color,
wenzelm@56064
   814
      Markup.OPERATOR -> operator_color,
wenzelm@61175
   815
      Markup.STRING -> foreground_color,
wenzelm@61175
   816
      Markup.ALT_STRING -> foreground_color,
wenzelm@61175
   817
      Markup.VERBATIM -> foreground_color,
wenzelm@61175
   818
      Markup.CARTOUCHE -> foreground_color,
wenzelm@50201
   819
      Markup.LITERAL -> keyword1_color,
wenzelm@61175
   820
      Markup.DELIMITER -> foreground_color,
wenzelm@50201
   821
      Markup.TFREE -> tfree_color,
wenzelm@50201
   822
      Markup.TVAR -> tvar_color,
wenzelm@50201
   823
      Markup.FREE -> free_color,
wenzelm@50201
   824
      Markup.SKOLEM -> skolem_color,
wenzelm@50201
   825
      Markup.BOUND -> bound_color,
wenzelm@50201
   826
      Markup.VAR -> var_color,
wenzelm@50201
   827
      Markup.INNER_STRING -> inner_quoted_color,
wenzelm@55033
   828
      Markup.INNER_CARTOUCHE -> inner_cartouche_color,
wenzelm@50201
   829
      Markup.INNER_COMMENT -> inner_comment_color,
wenzelm@50201
   830
      Markup.DYNAMIC_FACT -> dynamic_color,
wenzelm@55526
   831
      Markup.ANTIQUOTE -> antiquote_color,
wenzelm@55505
   832
      Markup.ML_KEYWORD1 -> keyword1_color,
wenzelm@55505
   833
      Markup.ML_KEYWORD2 -> keyword2_color,
wenzelm@55505
   834
      Markup.ML_KEYWORD3 -> keyword3_color,
wenzelm@61175
   835
      Markup.ML_DELIMITER -> foreground_color,
wenzelm@50201
   836
      Markup.ML_NUMERAL -> inner_numeral_color,
wenzelm@50201
   837
      Markup.ML_CHAR -> inner_quoted_color,
wenzelm@50201
   838
      Markup.ML_STRING -> inner_quoted_color,
wenzelm@56278
   839
      Markup.ML_COMMENT -> inner_comment_color,
wenzelm@56278
   840
      Markup.SML_STRING -> inner_quoted_color,
wenzelm@56278
   841
      Markup.SML_COMMENT -> inner_comment_color)
wenzelm@49356
   842
wenzelm@55820
   843
  private lazy val text_color_elements =
wenzelm@56743
   844
    Markup.Elements(text_colors.keySet)
wenzelm@49356
   845
wenzelm@52900
   846
  def text_color(range: Text.Range, color: Color): List[Text.Info[Color]] =
wenzelm@49356
   847
  {
wenzelm@52900
   848
    if (color == Token_Markup.hidden_color) List(Text.Info(range, color))
wenzelm@50196
   849
    else
wenzelm@55651
   850
      snapshot.cumulate(range, color, text_color_elements, _ =>
wenzelm@50196
   851
        {
wenzelm@52890
   852
          case (_, Text.Info(_, elem)) => text_colors.get(elem.name)
wenzelm@50196
   853
        })
wenzelm@49356
   854
  }
wenzelm@50542
   855
wenzelm@50542
   856
wenzelm@55626
   857
  /* virtual bullets */
wenzelm@55626
   858
wenzelm@55626
   859
  def bullet(range: Text.Range): List[Text.Info[Color]] =
wenzelm@60913
   860
    snapshot.select(range, Rendering.bullet_elements, _ =>
wenzelm@60913
   861
      {
wenzelm@60913
   862
        case Text.Info(_, Protocol.ML_Breakpoint(breakpoint)) =>
wenzelm@60913
   863
          Debugger.active_breakpoint_state(breakpoint).map(b =>
wenzelm@60913
   864
            if (b) breakpoint_enabled_color else breakpoint_disabled_color)
wenzelm@60913
   865
        case _ => Some(bullet_color)
wenzelm@60913
   866
      })
wenzelm@55626
   867
wenzelm@55626
   868
wenzelm@55626
   869
  /* text folds */
wenzelm@50542
   870
wenzelm@52900
   871
  def fold_depth(range: Text.Range): List[Text.Info[Int]] =
wenzelm@55651
   872
    snapshot.cumulate[Int](range, 0, Rendering.fold_depth_elements, _ =>
wenzelm@50542
   873
      {
wenzelm@55620
   874
        case (depth, _) => Some(depth + 1)
wenzelm@50542
   875
      })
wenzelm@49356
   876
}