src/Tools/jEdit/src/token_markup.scala
author wenzelm
Sun, 21 Aug 2011 20:25:49 +0200
changeset 44356 f6a2e5ce2ce5
parent 44355 9c38bdc6d755
child 44358 2a2df4de1bbe
permissions -rw-r--r--
avoid actual Color.white, which would be turned into Color.black by org.gjt.sp.jedit.print.BufferPrintable;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/token_markup.scala
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     3
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     4
Outer syntax token markup.
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     5
*/
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     6
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     8
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     9
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    10
import isabelle._
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    11
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    12
import java.awt.{Font, Color}
43502
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    13
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
43491
7b7baa283434 hidden font: full height makes cursor more visible;
wenzelm
parents: 43489
diff changeset
    14
import java.awt.geom.AffineTransform
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    15
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    16
import org.gjt.sp.util.SyntaxUtilities
43452
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
    17
import org.gjt.sp.jedit.Mode
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
    18
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    19
  ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
43452
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
    20
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    21
import javax.swing.text.Segment
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    22
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    23
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    24
object Token_Markup
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    25
{
43502
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    26
  /* font operations */
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    27
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    28
  private def font_metrics(font: Font): LineMetrics =
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    29
    font.getLineMetrics("", new FontRenderContext(null, false, false))
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    30
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    31
  private def imitate_font(family: String, font: Font): Font =
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    32
  {
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    33
    val font1 = new Font (family, font.getStyle, font.getSize)
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    34
    font1.deriveFont(font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize)
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    35
  }
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    36
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    37
  private def transform_font(font: Font, transform: AffineTransform): Font =
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    38
  {
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    39
    import scala.collection.JavaConversions._
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    40
    font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform)))
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    41
  }
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    42
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    43
43443
5d9693c2337e basic support for extended syntax styles: sub/superscript;
wenzelm
parents: 43440
diff changeset
    44
  /* extended syntax styles */
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    45
43440
a1db9a251a03 tuned signature;
wenzelm
parents: 43429
diff changeset
    46
  private val plain_range: Int = JEditToken.ID_COUNT
43488
39035276927c Symbol.is_ctrl: handle decoded version as well;
wenzelm
parents: 43487
diff changeset
    47
  private val full_range = 6 * plain_range + 1
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    48
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    49
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    50
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    51
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
43460
2852f309174a support for bold style within text buffer;
wenzelm
parents: 43458
diff changeset
    52
  def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
43487
98cd7e83fc5b some support for user symbol fonts;
wenzelm
parents: 43482
diff changeset
    53
  def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
98cd7e83fc5b some support for user symbol fonts;
wenzelm
parents: 43482
diff changeset
    54
  val hidden: Byte = (6 * plain_range).toByte
98cd7e83fc5b some support for user symbol fonts;
wenzelm
parents: 43482
diff changeset
    55
98cd7e83fc5b some support for user symbol fonts;
wenzelm
parents: 43482
diff changeset
    56
  private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle =
98cd7e83fc5b some support for user symbol fonts;
wenzelm
parents: 43482
diff changeset
    57
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont))
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    58
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    59
  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    60
  {
43502
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    61
    font_style(style, font0 =>
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    62
      {
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    63
        import scala.collection.JavaConversions._
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    64
        val font1 = font0.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    65
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    66
        def shift(y: Float): Font =
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    67
          transform_font(font1, AffineTransform.getTranslateInstance(0.0, y.toDouble))
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    68
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    69
        val m0 = font_metrics(font0)
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    70
        val m1 = font_metrics(font1)
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    71
        val a = m1.getAscent - m0.getAscent
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    72
        val b = (m1.getDescent + m1.getLeading) - (m0.getDescent + m0.getLeading)
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    73
        if (a > 0.0f) shift(a)
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    74
        else if (b > 0.0f) shift(- b)
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    75
        else font1
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
    76
      })
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    77
  }
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    78
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    79
  private def bold_style(style: SyntaxStyle): SyntaxStyle =
43487
98cd7e83fc5b some support for user symbol fonts;
wenzelm
parents: 43482
diff changeset
    80
    font_style(style, _.deriveFont(Font.BOLD))
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    81
44356
f6a2e5ce2ce5 avoid actual Color.white, which would be turned into Color.black by org.gjt.sp.jedit.print.BufferPrintable;
wenzelm
parents: 44355
diff changeset
    82
  private def hidden_color: Color = new Color(255, 255, 255, 0)
f6a2e5ce2ce5 avoid actual Color.white, which would be turned into Color.black by org.gjt.sp.jedit.print.BufferPrintable;
wenzelm
parents: 44355
diff changeset
    83
43661
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43553
diff changeset
    84
  class Style_Extender extends SyntaxUtilities.StyleExtender
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    85
  {
44355
9c38bdc6d755 default style for user fonts -- to prevent org.gjt.sp.jedit.print.BufferPrintable from choking on null;
wenzelm
parents: 44238
diff changeset
    86
    val max_user_fonts = 2
9c38bdc6d755 default style for user fonts -- to prevent org.gjt.sp.jedit.print.BufferPrintable from choking on null;
wenzelm
parents: 44238
diff changeset
    87
    if (Symbol.font_names.length > max_user_fonts)
9c38bdc6d755 default style for user fonts -- to prevent org.gjt.sp.jedit.print.BufferPrintable from choking on null;
wenzelm
parents: 44238
diff changeset
    88
      error("Too many user symbol fonts (max " + max_user_fonts + " permitted): " +
9c38bdc6d755 default style for user fonts -- to prevent org.gjt.sp.jedit.print.BufferPrintable from choking on null;
wenzelm
parents: 44238
diff changeset
    89
        Symbol.font_names.mkString(", "))
43487
98cd7e83fc5b some support for user symbol fonts;
wenzelm
parents: 43482
diff changeset
    90
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    91
    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    92
    {
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    93
      val new_styles = new Array[SyntaxStyle](full_range)
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    94
      for (i <- 0 until plain_range) {
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    95
        val style = styles(i)
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    96
        new_styles(i) = style
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    97
        new_styles(subscript(i.toByte)) = script_style(style, -1)
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    98
        new_styles(superscript(i.toByte)) = script_style(style, 1)
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
    99
        new_styles(bold(i.toByte)) = bold_style(style)
44355
9c38bdc6d755 default style for user fonts -- to prevent org.gjt.sp.jedit.print.BufferPrintable from choking on null;
wenzelm
parents: 44238
diff changeset
   100
        for (idx <- 0 until max_user_fonts)
9c38bdc6d755 default style for user fonts -- to prevent org.gjt.sp.jedit.print.BufferPrintable from choking on null;
wenzelm
parents: 44238
diff changeset
   101
          new_styles(user_font(idx, i.toByte)) = style
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   102
        for ((family, idx) <- Symbol.font_index)
43502
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
   103
          new_styles(user_font(idx, i.toByte)) = font_style(style, imitate_font(family, _))
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   104
      }
43502
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
   105
      new_styles(hidden) =
44356
f6a2e5ce2ce5 avoid actual Color.white, which would be turned into Color.black by org.gjt.sp.jedit.print.BufferPrintable;
wenzelm
parents: 44355
diff changeset
   106
        new SyntaxStyle(hidden_color, null,
43502
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
   107
          { val font = styles(0).getFont
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
   108
            transform_font(new Font(font.getFamily, 0, 1),
736183a22fa4 more precise font transformations: shift sub/superscript, adjust size for user fonts;
wenzelm
parents: 43491
diff changeset
   109
              AffineTransform.getScaleInstance(1.0, font.getSize.toDouble)) })
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   110
      new_styles
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   111
    }
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   112
  }
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   113
43661
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43553
diff changeset
   114
  def extended_styles(text: CharSequence): Map[Text.Offset, Byte => Byte] =
43443
5d9693c2337e basic support for extended syntax styles: sub/superscript;
wenzelm
parents: 43440
diff changeset
   115
  {
44238
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 43695
diff changeset
   116
    // FIXME Symbol.bsub_decoded etc.
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   117
    def ctrl_style(sym: String): Option[Byte => Byte] =
44238
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 43695
diff changeset
   118
      if (sym == Symbol.sub_decoded || sym == Symbol.isub_decoded) Some(subscript(_))
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 43695
diff changeset
   119
      else if (sym == Symbol.sup_decoded || sym == Symbol.isup_decoded) Some(superscript(_))
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 43695
diff changeset
   120
      else if (sym == Symbol.bold_decoded) Some(bold(_))
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   121
      else None
43455
4b4b93672f15 some unicode chars for special control symbols;
wenzelm
parents: 43452
diff changeset
   122
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   123
    var result = Map[Text.Offset, Byte => Byte]()
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   124
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   125
    {
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   126
      for (i <- start until stop) result += (i -> style)
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   127
    }
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   128
    var offset = 0
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   129
    var ctrl = ""
43675
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43661
diff changeset
   130
    for (sym <- Symbol.iterator(text)) {
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   131
      if (ctrl_style(sym).isDefined) ctrl = sym
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   132
      else if (ctrl != "") {
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   133
        if (Symbol.is_controllable(sym) && sym != "\"" && !Symbol.fonts.isDefinedAt(sym)) {
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   134
          mark(offset - ctrl.length, offset, _ => hidden)
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   135
          mark(offset, offset + sym.length, ctrl_style(ctrl).get)
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   136
        }
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   137
        ctrl = ""
43443
5d9693c2337e basic support for extended syntax styles: sub/superscript;
wenzelm
parents: 43440
diff changeset
   138
      }
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   139
      Symbol.lookup_font(sym) match {
43488
39035276927c Symbol.is_ctrl: handle decoded version as well;
wenzelm
parents: 43487
diff changeset
   140
        case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
43487
98cd7e83fc5b some support for user symbol fonts;
wenzelm
parents: 43482
diff changeset
   141
        case _ =>
98cd7e83fc5b some support for user symbol fonts;
wenzelm
parents: 43482
diff changeset
   142
      }
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   143
      offset += sym.length
43443
5d9693c2337e basic support for extended syntax styles: sub/superscript;
wenzelm
parents: 43440
diff changeset
   144
    }
43482
ebb90ff55b79 added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
wenzelm
parents: 43464
diff changeset
   145
    result
43443
5d9693c2337e basic support for extended syntax styles: sub/superscript;
wenzelm
parents: 43440
diff changeset
   146
  }
5d9693c2337e basic support for extended syntax styles: sub/superscript;
wenzelm
parents: 43440
diff changeset
   147
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   148
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   149
  /* token marker */
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   150
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   151
  private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   152
43429
095f90f8dca3 simplified Line_Context (again);
wenzelm
parents: 43416
diff changeset
   153
  private class Line_Context(val context: Scan.Context)
095f90f8dca3 simplified Line_Context (again);
wenzelm
parents: 43416
diff changeset
   154
    extends TokenMarker.LineContext(isabelle_rules, null)
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   155
  {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   156
    override def hashCode: Int = context.hashCode
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   157
    override def equals(that: Any): Boolean =
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   158
      that match {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   159
        case other: Line_Context => context == other.context
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   160
        case _ => false
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   161
      }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   162
  }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   163
43452
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   164
  class Marker extends TokenMarker
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   165
  {
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   166
    override def markTokens(context: TokenMarker.LineContext,
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   167
        handler: TokenHandler, line: Segment): TokenMarker.LineContext =
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   168
    {
43553
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   169
      val context1 =
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   170
        if (Isabelle.session.is_ready) {
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   171
          val syntax = Isabelle.session.current_syntax()
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   172
    
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   173
          val ctxt =
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   174
            context match {
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   175
              case c: Line_Context => c.context
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   176
              case _ => Scan.Finished
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   177
            }
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   178
          val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   179
          val context1 = new Line_Context(ctxt1)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   180
    
43661
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43553
diff changeset
   181
          val extended = extended_styles(line)
43553
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   182
    
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   183
          var offset = 0
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   184
          for (token <- tokens) {
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   185
            val style = Isabelle_Markup.token_markup(syntax, token)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   186
            val length = token.source.length
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   187
            val end_offset = offset + length
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   188
            if ((offset until end_offset) exists extended.isDefinedAt) {
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   189
              for (i <- offset until end_offset) {
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   190
                val style1 =
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   191
                  extended.get(i) match {
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   192
                    case None => style
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   193
                    case Some(ext) => ext(style)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   194
                  }
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   195
                handler.handleToken(line, style1, i, 1, context1)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   196
              }
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   197
            }
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   198
            else handler.handleToken(line, style, offset, length, context1)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   199
            offset += length
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   200
          }
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   201
          handler.handleToken(line, JEditToken.END, line.count, 0, context1)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   202
          context1
43452
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   203
        }
43553
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   204
        else {
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   205
          val context1 = new Line_Context(Scan.Finished)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   206
          handler.handleToken(line, JEditToken.NULL, 0, line.count, context1)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   207
          handler.handleToken(line, JEditToken.END, line.count, 0, context1)
df80747342cb proper tokens only if session is ready;
wenzelm
parents: 43511
diff changeset
   208
          context1
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   209
        }
43452
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   210
      val context2 = context1.intern
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   211
      handler.setLineContext(context2)
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   212
      context2
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   213
    }
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   214
  }
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   215
43416
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
   216
43452
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   217
  /* mode provider */
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   218
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   219
  class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   220
  {
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   221
    for (mode <- orig_provider.getModes) addMode(mode)
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   222
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   223
    val isabelle_token_marker = new Token_Markup.Marker
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   224
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   225
    override def loadMode(mode: Mode, xmh: XModeHandler)
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   226
    {
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   227
      super.loadMode(mode, xmh)
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   228
      if (mode.getName == "isabelle")
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   229
        mode.setTokenMarker(isabelle_token_marker)
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   230
    }
43452
5cf548485529 avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents: 43443
diff changeset
   231
  }
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   232
}
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
   233