src/Pure/PIDE/rendering.scala
author wenzelm
Tue Mar 07 17:56:57 2017 +0100 (2017-03-07)
changeset 65144 b5782e996651
parent 65143 36cd85caf09a
child 65145 576d52aa0a78
permissions -rw-r--r--
more generic colors;
wenzelm@64622
     1
/*  Title:      Pure/PIDE/rendering.scala
wenzelm@64622
     2
    Author:     Makarius
wenzelm@64622
     3
wenzelm@64622
     4
Isabelle-specific implementation of quasi-abstract rendering and
wenzelm@64622
     5
markup interpretation.
wenzelm@64622
     6
*/
wenzelm@64622
     7
wenzelm@64622
     8
package isabelle
wenzelm@64622
     9
wenzelm@64622
    10
wenzelm@64622
    11
object Rendering
wenzelm@64622
    12
{
wenzelm@65101
    13
  /* color */
wenzelm@65101
    14
wenzelm@65101
    15
  object Color extends Enumeration
wenzelm@65101
    16
  {
wenzelm@65104
    17
    // background
wenzelm@65101
    18
    val unprocessed1 = Value("unprocessed1")
wenzelm@65101
    19
    val running1 = Value("running1")
wenzelm@65101
    20
    val bad = Value("bad")
wenzelm@65101
    21
    val intensify = Value("intensify")
wenzelm@65101
    22
    val entity = Value("entity")
wenzelm@65101
    23
    val active = Value("active")
wenzelm@65101
    24
    val active_result = Value("active_result")
wenzelm@65101
    25
    val markdown_item1 = Value("markdown_item1")
wenzelm@65101
    26
    val markdown_item2 = Value("markdown_item2")
wenzelm@65101
    27
    val markdown_item3 = Value("markdown_item3")
wenzelm@65101
    28
    val markdown_item4 = Value("markdown_item4")
wenzelm@65143
    29
    val background_colors = values
wenzelm@65104
    30
wenzelm@65104
    31
    // foreground
wenzelm@65104
    32
    val quoted = Value("quoted")
wenzelm@65104
    33
    val antiquoted = Value("antiquoted")
wenzelm@65143
    34
    val foreground_colors = values -- background_colors
wenzelm@65121
    35
wenzelm@65124
    36
    // message underline
wenzelm@65121
    37
    val writeln = Value("writeln")
wenzelm@65121
    38
    val information = Value("information")
wenzelm@65121
    39
    val warning = Value("warning")
wenzelm@65121
    40
    val legacy = Value("legacy")
wenzelm@65121
    41
    val error = Value("error")
wenzelm@65143
    42
    val message_underline_colors = values -- background_colors -- foreground_colors
wenzelm@65124
    43
wenzelm@65124
    44
    // message background
wenzelm@65124
    45
    val writeln_message = Value("writeln_message")
wenzelm@65124
    46
    val information_message = Value("information_message")
wenzelm@65124
    47
    val tracing_message = Value("tracing_message")
wenzelm@65124
    48
    val warning_message = Value("warning_message")
wenzelm@65124
    49
    val legacy_message = Value("legacy_message")
wenzelm@65124
    50
    val error_message = Value("error_message")
wenzelm@65143
    51
    val message_background_colors =
wenzelm@65143
    52
      values -- background_colors -- foreground_colors -- message_underline_colors
wenzelm@65144
    53
wenzelm@65144
    54
    // text
wenzelm@65144
    55
    val text = Value("text")
wenzelm@65144
    56
    val keyword1 = Value("keyword1")
wenzelm@65144
    57
    val keyword2 = Value("keyword2")
wenzelm@65144
    58
    val keyword3 = Value("keyword3")
wenzelm@65144
    59
    val quasi_keyword = Value("quasi_keyword")
wenzelm@65144
    60
    val improper = Value("improper")
wenzelm@65144
    61
    val operator = Value("operator")
wenzelm@65144
    62
    val tfree = Value("tfree")
wenzelm@65144
    63
    val tvar = Value("tvar")
wenzelm@65144
    64
    val free = Value("free")
wenzelm@65144
    65
    val skolem = Value("skolem")
wenzelm@65144
    66
    val bound = Value("bound")
wenzelm@65144
    67
    val var_ = Value("var")
wenzelm@65144
    68
    val inner_numeral = Value("inner_numeral")
wenzelm@65144
    69
    val inner_quoted = Value("inner_quoted")
wenzelm@65144
    70
    val inner_cartouche = Value("inner_cartouche")
wenzelm@65144
    71
    val inner_comment = Value("inner_comment")
wenzelm@65144
    72
    val dynamic = Value("dynamic")
wenzelm@65144
    73
    val class_parameter = Value("class_parameter")
wenzelm@65144
    74
    val antiquote = Value("antiquote")
wenzelm@65144
    75
    val text_colors =
wenzelm@65144
    76
      values -- background_colors -- foreground_colors -- message_underline_colors --
wenzelm@65144
    77
      message_background_colors
wenzelm@65101
    78
  }
wenzelm@65101
    79
wenzelm@65101
    80
wenzelm@64676
    81
  /* message priorities */
wenzelm@64676
    82
wenzelm@64676
    83
  val state_pri = 1
wenzelm@64676
    84
  val writeln_pri = 2
wenzelm@64676
    85
  val information_pri = 3
wenzelm@64676
    86
  val tracing_pri = 4
wenzelm@64676
    87
  val warning_pri = 5
wenzelm@64676
    88
  val legacy_pri = 6
wenzelm@64676
    89
  val error_pri = 7
wenzelm@64676
    90
wenzelm@64676
    91
  val message_pri = Map(
wenzelm@64676
    92
    Markup.STATE -> state_pri,
wenzelm@64676
    93
    Markup.STATE_MESSAGE -> state_pri,
wenzelm@64676
    94
    Markup.WRITELN -> writeln_pri,
wenzelm@64676
    95
    Markup.WRITELN_MESSAGE -> writeln_pri,
wenzelm@64676
    96
    Markup.INFORMATION -> information_pri,
wenzelm@64676
    97
    Markup.INFORMATION_MESSAGE -> information_pri,
wenzelm@64676
    98
    Markup.TRACING -> tracing_pri,
wenzelm@64676
    99
    Markup.TRACING_MESSAGE -> tracing_pri,
wenzelm@64676
   100
    Markup.WARNING -> warning_pri,
wenzelm@64676
   101
    Markup.WARNING_MESSAGE -> warning_pri,
wenzelm@64676
   102
    Markup.LEGACY -> legacy_pri,
wenzelm@64676
   103
    Markup.LEGACY_MESSAGE -> legacy_pri,
wenzelm@64676
   104
    Markup.ERROR -> error_pri,
wenzelm@65133
   105
    Markup.ERROR_MESSAGE -> error_pri
wenzelm@65133
   106
  ).withDefaultValue(0)
wenzelm@64676
   107
wenzelm@65121
   108
  val message_underline_color = Map(
wenzelm@65121
   109
    writeln_pri -> Color.writeln,
wenzelm@65121
   110
    information_pri -> Color.information,
wenzelm@65121
   111
    warning_pri -> Color.warning,
wenzelm@65121
   112
    legacy_pri -> Color.legacy,
wenzelm@65121
   113
    error_pri -> Color.error)
wenzelm@65121
   114
wenzelm@65124
   115
  val message_background_color = Map(
wenzelm@65124
   116
    writeln_pri -> Color.writeln_message,
wenzelm@65124
   117
    information_pri -> Color.information_message,
wenzelm@65124
   118
    tracing_pri -> Color.tracing_message,
wenzelm@65124
   119
    warning_pri -> Color.warning_message,
wenzelm@65124
   120
    legacy_pri -> Color.legacy_message,
wenzelm@65124
   121
    error_pri -> Color.error_message)
wenzelm@65124
   122
wenzelm@64676
   123
wenzelm@65144
   124
  /* text color */
wenzelm@65144
   125
wenzelm@65144
   126
  val text_color = Map(
wenzelm@65144
   127
    Markup.KEYWORD1 -> Color.keyword1,
wenzelm@65144
   128
    Markup.KEYWORD2 -> Color.keyword2,
wenzelm@65144
   129
    Markup.KEYWORD3 -> Color.keyword3,
wenzelm@65144
   130
    Markup.QUASI_KEYWORD -> Color.quasi_keyword,
wenzelm@65144
   131
    Markup.IMPROPER -> Color.improper,
wenzelm@65144
   132
    Markup.OPERATOR -> Color.operator,
wenzelm@65144
   133
    Markup.STRING -> Color.text,
wenzelm@65144
   134
    Markup.ALT_STRING -> Color.text,
wenzelm@65144
   135
    Markup.VERBATIM -> Color.text,
wenzelm@65144
   136
    Markup.CARTOUCHE -> Color.text,
wenzelm@65144
   137
    Markup.LITERAL -> Color.keyword1,
wenzelm@65144
   138
    Markup.DELIMITER -> Color.text,
wenzelm@65144
   139
    Markup.TFREE -> Color.tfree,
wenzelm@65144
   140
    Markup.TVAR -> Color.tvar,
wenzelm@65144
   141
    Markup.FREE -> Color.free,
wenzelm@65144
   142
    Markup.SKOLEM -> Color.skolem,
wenzelm@65144
   143
    Markup.BOUND -> Color.bound,
wenzelm@65144
   144
    Markup.VAR -> Color.var_,
wenzelm@65144
   145
    Markup.INNER_STRING -> Color.inner_quoted,
wenzelm@65144
   146
    Markup.INNER_CARTOUCHE -> Color.inner_cartouche,
wenzelm@65144
   147
    Markup.INNER_COMMENT -> Color.inner_comment,
wenzelm@65144
   148
    Markup.DYNAMIC_FACT -> Color.dynamic,
wenzelm@65144
   149
    Markup.CLASS_PARAMETER -> Color.class_parameter,
wenzelm@65144
   150
    Markup.ANTIQUOTE -> Color.antiquote,
wenzelm@65144
   151
    Markup.ML_KEYWORD1 -> Color.keyword1,
wenzelm@65144
   152
    Markup.ML_KEYWORD2 -> Color.keyword2,
wenzelm@65144
   153
    Markup.ML_KEYWORD3 -> Color.keyword3,
wenzelm@65144
   154
    Markup.ML_DELIMITER -> Color.text,
wenzelm@65144
   155
    Markup.ML_NUMERAL -> Color.inner_numeral,
wenzelm@65144
   156
    Markup.ML_CHAR -> Color.inner_quoted,
wenzelm@65144
   157
    Markup.ML_STRING -> Color.inner_quoted,
wenzelm@65144
   158
    Markup.ML_COMMENT -> Color.inner_comment,
wenzelm@65144
   159
    Markup.SML_STRING -> Color.inner_quoted,
wenzelm@65144
   160
    Markup.SML_COMMENT -> Color.inner_comment)
wenzelm@65144
   161
wenzelm@65144
   162
wenzelm@64676
   163
  /* markup elements */
wenzelm@64676
   164
wenzelm@65101
   165
  val active_elements =
wenzelm@65101
   166
    Markup.Elements(Markup.DIALOG, Markup.BROWSER, Markup.GRAPHVIEW,
wenzelm@65101
   167
      Markup.SENDBACK, Markup.JEDIT_ACTION, Markup.SIMP_TRACE_PANEL)
wenzelm@65101
   168
wenzelm@65101
   169
  private val background_elements =
wenzelm@65101
   170
    Protocol.proper_status_elements + Markup.WRITELN_MESSAGE +
wenzelm@65101
   171
      Markup.STATE_MESSAGE + Markup.INFORMATION_MESSAGE +
wenzelm@65101
   172
      Markup.TRACING_MESSAGE + Markup.WARNING_MESSAGE +
wenzelm@65101
   173
      Markup.LEGACY_MESSAGE + Markup.ERROR_MESSAGE +
wenzelm@65101
   174
      Markup.BAD + Markup.INTENSIFY + Markup.ENTITY +
wenzelm@65101
   175
      Markup.Markdown_Item.name ++ active_elements
wenzelm@65101
   176
wenzelm@65101
   177
  private val foreground_elements =
wenzelm@65101
   178
    Markup.Elements(Markup.STRING, Markup.ALT_STRING, Markup.VERBATIM,
wenzelm@65101
   179
      Markup.CARTOUCHE, Markup.ANTIQUOTED)
wenzelm@65101
   180
wenzelm@64877
   181
  private val semantic_completion_elements =
wenzelm@64877
   182
    Markup.Elements(Markup.COMPLETION, Markup.NO_COMPLETION)
wenzelm@64877
   183
wenzelm@64622
   184
  private val tooltip_descriptions =
wenzelm@64622
   185
    Map(
wenzelm@64622
   186
      Markup.CITATION -> "citation",
wenzelm@64622
   187
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@64622
   188
      Markup.FREE -> "free variable",
wenzelm@64622
   189
      Markup.SKOLEM -> "skolem variable",
wenzelm@64622
   190
      Markup.BOUND -> "bound variable",
wenzelm@64622
   191
      Markup.VAR -> "schematic variable",
wenzelm@64622
   192
      Markup.TFREE -> "free type variable",
wenzelm@64622
   193
      Markup.TVAR -> "schematic type variable")
wenzelm@64622
   194
wenzelm@65129
   195
  val tooltip_elements =
wenzelm@64622
   196
    Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
wenzelm@64622
   197
      Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
wenzelm@64622
   198
      Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
wenzelm@64622
   199
      Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
wenzelm@64622
   200
wenzelm@65129
   201
  val tooltip_message_elements =
wenzelm@65129
   202
    Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR,
wenzelm@65129
   203
      Markup.BAD)
wenzelm@65129
   204
wenzelm@64622
   205
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@64622
   206
    Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
wenzelm@64767
   207
wenzelm@64767
   208
  val caret_focus_elements = Markup.Elements(Markup.ENTITY)
wenzelm@65144
   209
wenzelm@65144
   210
  val text_color_elements = Markup.Elements(text_color.keySet)
wenzelm@64622
   211
}
wenzelm@64622
   212
wenzelm@64622
   213
abstract class Rendering(
wenzelm@64622
   214
  val snapshot: Document.Snapshot,
wenzelm@64622
   215
  val options: Options,
wenzelm@64622
   216
  val resources: Resources)
wenzelm@64622
   217
{
wenzelm@64622
   218
  override def toString: String = "Rendering(" + snapshot.toString + ")"
wenzelm@64622
   219
wenzelm@64622
   220
wenzelm@64877
   221
  /* completion */
wenzelm@64877
   222
wenzelm@64877
   223
  def semantic_completion(completed_range: Option[Text.Range], range: Text.Range)
wenzelm@64877
   224
      : Option[Text.Info[Completion.Semantic]] =
wenzelm@64877
   225
    if (snapshot.is_outdated) None
wenzelm@64877
   226
    else {
wenzelm@64877
   227
      snapshot.select(range, Rendering.semantic_completion_elements, _ =>
wenzelm@64877
   228
        {
wenzelm@64877
   229
          case Completion.Semantic.Info(info) =>
wenzelm@64877
   230
            completed_range match {
wenzelm@64877
   231
              case Some(range0) if range0.contains(info.range) && range0 != info.range => None
wenzelm@64877
   232
              case _ => Some(info)
wenzelm@64877
   233
            }
wenzelm@64877
   234
          case _ => None
wenzelm@64877
   235
        }).headOption.map(_.info)
wenzelm@64877
   236
    }
wenzelm@64877
   237
wenzelm@64877
   238
wenzelm@65139
   239
  /* spell checker */
wenzelm@65139
   240
wenzelm@65139
   241
  private lazy val spell_checker_elements =
wenzelm@65139
   242
    Markup.Elements(space_explode(',', options.string("spell_checker_elements")): _*)
wenzelm@65139
   243
wenzelm@65139
   244
  def spell_checker_ranges(range: Text.Range): List[Text.Range] =
wenzelm@65139
   245
    snapshot.select(range, spell_checker_elements, _ => _ => Some(())).map(_.range)
wenzelm@65139
   246
wenzelm@65139
   247
  def spell_checker_point(range: Text.Range): Option[Text.Range] =
wenzelm@65139
   248
    snapshot.select(range, spell_checker_elements, _ =>
wenzelm@65139
   249
      {
wenzelm@65139
   250
        case info => Some(snapshot.convert(info.range))
wenzelm@65139
   251
      }).headOption.map(_.info)
wenzelm@65139
   252
wenzelm@65139
   253
wenzelm@64622
   254
  /* tooltips */
wenzelm@64622
   255
wenzelm@64622
   256
  def timing_threshold: Double
wenzelm@64622
   257
wenzelm@65129
   258
  private sealed case class Tooltip_Info(
wenzelm@65129
   259
    range: Text.Range,
wenzelm@65129
   260
    timing: Timing = Timing.zero,
wenzelm@65129
   261
    messages: List[Command.Results.Entry] = Nil,
wenzelm@65129
   262
    rev_infos: List[(Boolean, XML.Tree)] = Nil)
wenzelm@64622
   263
  {
wenzelm@65129
   264
    def + (t: Timing): Tooltip_Info = copy(timing = timing + t)
wenzelm@65129
   265
    def + (r0: Text.Range, serial: Long, tree: XML.Tree): Tooltip_Info =
wenzelm@65129
   266
    {
wenzelm@65129
   267
      val r = snapshot.convert(r0)
wenzelm@65129
   268
      if (range == r) copy(messages = (serial -> tree) :: messages)
wenzelm@65129
   269
      else copy(range = r, messages = List(serial -> tree))
wenzelm@65129
   270
    }
wenzelm@65129
   271
    def + (r0: Text.Range, important: Boolean, tree: XML.Tree): Tooltip_Info =
wenzelm@64622
   272
    {
wenzelm@64622
   273
      val r = snapshot.convert(r0)
wenzelm@65129
   274
      if (range == r) copy(rev_infos = (important -> tree) :: rev_infos)
wenzelm@65129
   275
      else copy (range = r, rev_infos = List(important -> tree))
wenzelm@64622
   276
    }
wenzelm@65129
   277
    def infos(important: Boolean): List[XML.Tree] =
wenzelm@65129
   278
      rev_infos.filter(p => p._1 == important).reverse.map(_._2)
wenzelm@65129
   279
  }
wenzelm@64622
   280
wenzelm@65129
   281
  def tooltips(elements: Markup.Elements, range: Text.Range): Option[Text.Info[List[XML.Tree]]] =
wenzelm@65129
   282
  {
wenzelm@64622
   283
    val results =
wenzelm@65129
   284
      snapshot.cumulate[Tooltip_Info](range, Tooltip_Info(range), elements, _ =>
wenzelm@64622
   285
        {
wenzelm@65129
   286
          case (info, Text.Info(_, XML.Elem(Markup.Timing(t), _))) => Some(info + t)
wenzelm@64622
   287
wenzelm@65129
   288
          case (info, Text.Info(r0, XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
wenzelm@65129
   289
          if Rendering.tooltip_message_elements(name) && body.nonEmpty =>
wenzelm@65129
   290
            Some(info + (r0, serial, XML.Elem(Markup(Markup.message(name), props), body)))
wenzelm@65129
   291
wenzelm@65129
   292
          case (info, Text.Info(r0, XML.Elem(Markup.Entity(kind, name), _)))
wenzelm@64660
   293
          if kind != "" && kind != Markup.ML_DEF =>
wenzelm@64622
   294
            val kind1 = Word.implode(Word.explode('_', kind))
wenzelm@64622
   295
            val txt1 =
wenzelm@64622
   296
              if (name == "") kind1
wenzelm@64622
   297
              else if (kind1 == "") quote(name)
wenzelm@64622
   298
              else kind1 + " " + quote(name)
wenzelm@64622
   299
            val txt2 =
wenzelm@65129
   300
              if (kind == Markup.COMMAND && info.timing.elapsed.seconds >= timing_threshold)
wenzelm@65129
   301
                "\n" + info.timing.message
wenzelm@64622
   302
              else ""
wenzelm@65129
   303
            Some(info + (r0, true, XML.Text(txt1 + txt2)))
wenzelm@64622
   304
wenzelm@65129
   305
          case (info, Text.Info(r0, XML.Elem(Markup.Path(name), _))) =>
wenzelm@64654
   306
            val file = resources.append_file(snapshot.node_name.master_dir, name)
wenzelm@64622
   307
            val text =
wenzelm@64622
   308
              if (name == file) "file " + quote(file)
wenzelm@64622
   309
              else "path " + quote(name) + "\nfile " + quote(file)
wenzelm@65129
   310
            Some(info + (r0, true, XML.Text(text)))
wenzelm@64622
   311
wenzelm@65129
   312
          case (info, Text.Info(r0, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@64622
   313
            val text = "doc " + quote(name)
wenzelm@65129
   314
            Some(info + (r0, true, XML.Text(text)))
wenzelm@64622
   315
wenzelm@65129
   316
          case (info, Text.Info(r0, XML.Elem(Markup.Url(name), _))) =>
wenzelm@65129
   317
            Some(info + (r0, true, XML.Text("URL " + quote(name))))
wenzelm@64622
   318
wenzelm@65129
   319
          case (info, Text.Info(r0, XML.Elem(Markup(name, _), body)))
wenzelm@64622
   320
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@65129
   321
            Some(info + (r0, true, Rendering.pretty_typing("::", body)))
wenzelm@64622
   322
wenzelm@65129
   323
          case (info, Text.Info(r0, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
wenzelm@65129
   324
            Some(info + (r0, true, Pretty.block(0, body)))
wenzelm@64622
   325
wenzelm@65129
   326
          case (info, Text.Info(r0, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@65129
   327
            Some(info + (r0, false, Rendering.pretty_typing("ML:", body)))
wenzelm@64622
   328
wenzelm@65129
   329
          case (info, Text.Info(r0, Protocol.ML_Breakpoint(breakpoint))) =>
wenzelm@64622
   330
            val text =
wenzelm@64622
   331
              if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
wenzelm@64622
   332
              else "breakpoint (disabled)"
wenzelm@65129
   333
            Some(info + (r0, true, XML.Text(text)))
wenzelm@64622
   334
wenzelm@65129
   335
          case (info, Text.Info(r0, XML.Elem(Markup.Language(language, _, _, _), _))) =>
wenzelm@64622
   336
            val lang = Word.implode(Word.explode('_', language))
wenzelm@65129
   337
            Some(info + (r0, true, XML.Text("language: " + lang)))
wenzelm@64622
   338
wenzelm@65129
   339
          case (info, Text.Info(r0, XML.Elem(Markup.Expression(kind), _))) =>
wenzelm@64622
   340
            val descr = if (kind == "") "expression" else "expression: " + kind
wenzelm@65129
   341
            Some(info + (r0, true, XML.Text(descr)))
wenzelm@64622
   342
wenzelm@65129
   343
          case (info, Text.Info(r0, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
wenzelm@65129
   344
            Some(info + (r0, true, XML.Text("Markdown: paragraph")))
wenzelm@65129
   345
          case (info, Text.Info(r0, XML.Elem(Markup.Markdown_List(kind), _))) =>
wenzelm@65129
   346
            Some(info + (r0, true, XML.Text("Markdown: " + kind)))
wenzelm@64622
   347
wenzelm@65129
   348
          case (info, Text.Info(r0, XML.Elem(Markup(name, _), _))) =>
wenzelm@65129
   349
            Rendering.tooltip_descriptions.get(name).map(desc => info + (r0, true, XML.Text(desc)))
wenzelm@64622
   350
        }).map(_.info)
wenzelm@64622
   351
wenzelm@65129
   352
    if (results.isEmpty) None
wenzelm@65129
   353
    else {
wenzelm@65129
   354
      val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@65129
   355
      val all_tips =
wenzelm@65129
   356
        Command.Results.make(results.flatMap(_.messages)).iterator.map(_._2).toList :::
wenzelm@65129
   357
        results.flatMap(res => res.infos(true)) :::
wenzelm@65129
   358
        results.flatMap(res => res.infos(false)).lastOption.toList
wenzelm@65129
   359
      if (all_tips.isEmpty) None else Some(Text.Info(r, all_tips))
wenzelm@64622
   360
    }
wenzelm@64622
   361
  }
wenzelm@64767
   362
wenzelm@64767
   363
wenzelm@65101
   364
  /* text background */
wenzelm@65101
   365
wenzelm@65101
   366
  def background(range: Text.Range, focus: Set[Long]): List[Text.Info[Rendering.Color.Value]] =
wenzelm@65101
   367
  {
wenzelm@65101
   368
    for {
wenzelm@65101
   369
      Text.Info(r, result) <-
wenzelm@65101
   370
        snapshot.cumulate[(List[Markup], Option[Rendering.Color.Value])](
wenzelm@65101
   371
          range, (List(Markup.Empty), None), Rendering.background_elements,
wenzelm@65101
   372
          command_states =>
wenzelm@65101
   373
            {
wenzelm@65101
   374
              case (((markups, color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@65101
   375
              if markups.nonEmpty && Protocol.proper_status_elements(markup.name) =>
wenzelm@65101
   376
                Some((markup :: markups, color))
wenzelm@65101
   377
              case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
wenzelm@65101
   378
                Some((Nil, Some(Rendering.Color.bad)))
wenzelm@65101
   379
              case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
wenzelm@65101
   380
                Some((Nil, Some(Rendering.Color.intensify)))
wenzelm@65101
   381
              case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@65101
   382
                props match {
wenzelm@65101
   383
                  case Markup.Entity.Def(i) if focus(i) => Some((Nil, Some(Rendering.Color.entity)))
wenzelm@65101
   384
                  case Markup.Entity.Ref(i) if focus(i) => Some((Nil, Some(Rendering.Color.entity)))
wenzelm@65101
   385
                  case _ => None
wenzelm@65101
   386
                }
wenzelm@65101
   387
              case (_, Text.Info(_, XML.Elem(Markup.Markdown_Item(depth), _))) =>
wenzelm@65101
   388
                val color =
wenzelm@65101
   389
                  depth match {
wenzelm@65101
   390
                    case 1 => Rendering.Color.markdown_item1
wenzelm@65101
   391
                    case 2 => Rendering.Color.markdown_item2
wenzelm@65101
   392
                    case 3 => Rendering.Color.markdown_item3
wenzelm@65101
   393
                    case _ => Rendering.Color.markdown_item4
wenzelm@65101
   394
                  }
wenzelm@65101
   395
                Some((Nil, Some(color)))
wenzelm@65101
   396
              case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
wenzelm@65101
   397
                command_states.collectFirst(
wenzelm@65101
   398
                  { case st if st.results.defined(serial) => st.results.get(serial).get }) match
wenzelm@65101
   399
                {
wenzelm@65101
   400
                  case Some(Protocol.Dialog_Result(res)) if res == result =>
wenzelm@65101
   401
                    Some((Nil, Some(Rendering.Color.active_result)))
wenzelm@65101
   402
                  case _ =>
wenzelm@65101
   403
                    Some((Nil, Some(Rendering.Color.active)))
wenzelm@65101
   404
                }
wenzelm@65101
   405
              case (_, Text.Info(_, elem)) =>
wenzelm@65101
   406
                if (Rendering.active_elements(elem.name))
wenzelm@65101
   407
                  Some((Nil, Some(Rendering.Color.active)))
wenzelm@65101
   408
                else None
wenzelm@65101
   409
            })
wenzelm@65101
   410
      color <-
wenzelm@65101
   411
        (result match {
wenzelm@65101
   412
          case (markups, opt_color) if markups.nonEmpty =>
wenzelm@65101
   413
            val status = Protocol.Status.make(markups.iterator)
wenzelm@65101
   414
            if (status.is_unprocessed) Some(Rendering.Color.unprocessed1)
wenzelm@65101
   415
            else if (status.is_running) Some(Rendering.Color.running1)
wenzelm@65101
   416
            else opt_color
wenzelm@65101
   417
          case (_, opt_color) => opt_color
wenzelm@65101
   418
        })
wenzelm@65101
   419
    } yield Text.Info(r, color)
wenzelm@65101
   420
  }
wenzelm@65101
   421
wenzelm@65101
   422
wenzelm@65101
   423
  /* text foreground */
wenzelm@65101
   424
wenzelm@65101
   425
  def foreground(range: Text.Range): List[Text.Info[Rendering.Color.Value]] =
wenzelm@65101
   426
    snapshot.select(range, Rendering.foreground_elements, _ =>
wenzelm@65101
   427
      {
wenzelm@65101
   428
        case Text.Info(_, elem) =>
wenzelm@65101
   429
          if (elem.name == Markup.ANTIQUOTED) Some(Rendering.Color.antiquoted)
wenzelm@65101
   430
          else Some(Rendering.Color.quoted)
wenzelm@65101
   431
      })
wenzelm@65101
   432
wenzelm@65101
   433
wenzelm@64767
   434
  /* caret focus */
wenzelm@64767
   435
wenzelm@64767
   436
  private def entity_focus(range: Text.Range,
wenzelm@64767
   437
    check: (Boolean, Long) => Boolean = (is_def: Boolean, i: Long) => is_def): Set[Long] =
wenzelm@64767
   438
  {
wenzelm@64767
   439
    val results =
wenzelm@64767
   440
      snapshot.cumulate[Set[Long]](range, Set.empty, Rendering.caret_focus_elements, _ =>
wenzelm@64767
   441
          {
wenzelm@64767
   442
            case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64767
   443
              props match {
wenzelm@64767
   444
                case Markup.Entity.Def(i) if check(true, i) => Some(serials + i)
wenzelm@64767
   445
                case Markup.Entity.Ref(i) if check(false, i) => Some(serials + i)
wenzelm@64767
   446
                case _ => None
wenzelm@64767
   447
              }
wenzelm@64767
   448
            case _ => None
wenzelm@64767
   449
          })
wenzelm@64767
   450
    (Set.empty[Long] /: results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
wenzelm@64767
   451
  }
wenzelm@64767
   452
wenzelm@64767
   453
  def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
wenzelm@64767
   454
  {
wenzelm@64767
   455
    val focus_defs = entity_focus(caret_range)
wenzelm@64767
   456
    if (focus_defs.nonEmpty) focus_defs
wenzelm@64767
   457
    else {
wenzelm@64767
   458
      val visible_defs = entity_focus(visible_range)
wenzelm@64767
   459
      entity_focus(caret_range, (is_def: Boolean, i: Long) => !is_def && visible_defs.contains(i))
wenzelm@64767
   460
    }
wenzelm@64767
   461
  }
wenzelm@64767
   462
wenzelm@64767
   463
  def caret_focus_ranges(caret_range: Text.Range, visible_range: Text.Range): List[Text.Range] =
wenzelm@64767
   464
  {
wenzelm@64767
   465
    val focus = caret_focus(caret_range, visible_range)
wenzelm@64767
   466
    if (focus.nonEmpty) {
wenzelm@64767
   467
      val results =
wenzelm@64767
   468
        snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
wenzelm@64767
   469
          {
wenzelm@64767
   470
            case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64767
   471
              props match {
wenzelm@64767
   472
                case Markup.Entity.Def(i) if focus(i) => Some(true)
wenzelm@64767
   473
                case Markup.Entity.Ref(i) if focus(i) => Some(true)
wenzelm@64767
   474
                case _ => None
wenzelm@64767
   475
              }
wenzelm@64767
   476
          })
wenzelm@64767
   477
      for (info <- results if info.info) yield info.range
wenzelm@64767
   478
    }
wenzelm@64767
   479
    else Nil
wenzelm@64767
   480
  }
wenzelm@65121
   481
wenzelm@65121
   482
wenzelm@65121
   483
  /* message underline color */
wenzelm@65121
   484
wenzelm@65129
   485
  def message_underline_color(elements: Markup.Elements, range: Text.Range)
wenzelm@65129
   486
    : List[Text.Info[Rendering.Color.Value]] =
wenzelm@65121
   487
  {
wenzelm@65121
   488
    val results =
wenzelm@65121
   489
      snapshot.cumulate[Int](range, 0, elements, _ =>
wenzelm@65121
   490
        {
wenzelm@65121
   491
          case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
wenzelm@65121
   492
        })
wenzelm@65121
   493
    for {
wenzelm@65121
   494
      Text.Info(r, pri) <- results
wenzelm@65121
   495
      color <- Rendering.message_underline_color.get(pri)
wenzelm@65121
   496
    } yield Text.Info(r, color)
wenzelm@65121
   497
  }
wenzelm@64622
   498
}