src/Tools/jEdit/src/token_markup.scala
author wenzelm
Tue Jun 21 15:43:27 2011 +0200 (2011-06-21)
changeset 43491 7b7baa283434
parent 43489 132f99cc0a43
child 43502 736183a22fa4
permissions -rw-r--r--
hidden font: full height makes cursor more visible;
wenzelm@43414
     1
/*  Title:      Tools/jEdit/src/token_markup.scala
wenzelm@43414
     2
    Author:     Makarius
wenzelm@43414
     3
wenzelm@43414
     4
Outer syntax token markup.
wenzelm@43414
     5
*/
wenzelm@43414
     6
wenzelm@43414
     7
package isabelle.jedit
wenzelm@43414
     8
wenzelm@43414
     9
wenzelm@43414
    10
import isabelle._
wenzelm@43414
    11
wenzelm@43482
    12
import java.awt.{Font, Color}
wenzelm@43491
    13
import java.awt.font.{TextAttribute, TransformAttribute}
wenzelm@43491
    14
import java.awt.geom.AffineTransform
wenzelm@43482
    15
wenzelm@43482
    16
import org.gjt.sp.util.SyntaxUtilities
wenzelm@43452
    17
import org.gjt.sp.jedit.Mode
wenzelm@43452
    18
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
wenzelm@43482
    19
  ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
wenzelm@43452
    20
wenzelm@43414
    21
import javax.swing.text.Segment
wenzelm@43414
    22
wenzelm@43414
    23
wenzelm@43414
    24
object Token_Markup
wenzelm@43414
    25
{
wenzelm@43443
    26
  /* extended syntax styles */
wenzelm@43414
    27
wenzelm@43440
    28
  private val plain_range: Int = JEditToken.ID_COUNT
wenzelm@43488
    29
  private val full_range = 6 * plain_range + 1
wenzelm@43414
    30
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@43414
    31
wenzelm@43414
    32
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@43414
    33
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@43460
    34
  def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
wenzelm@43487
    35
  def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
wenzelm@43487
    36
  val hidden: Byte = (6 * plain_range).toByte
wenzelm@43487
    37
wenzelm@43487
    38
  private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle =
wenzelm@43487
    39
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont))
wenzelm@43414
    40
wenzelm@43482
    41
  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
wenzelm@43482
    42
  {
wenzelm@43482
    43
    import scala.collection.JavaConversions._
wenzelm@43487
    44
    font_style(style, font =>
wenzelm@43487
    45
      style.getFont.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i))))
wenzelm@43482
    46
  }
wenzelm@43482
    47
wenzelm@43482
    48
  private def bold_style(style: SyntaxStyle): SyntaxStyle =
wenzelm@43487
    49
    font_style(style, _.deriveFont(Font.BOLD))
wenzelm@43482
    50
wenzelm@43491
    51
  private def hidden_font(font: Font): Font =
wenzelm@43491
    52
  {
wenzelm@43491
    53
    import scala.collection.JavaConversions._
wenzelm@43491
    54
    val base_font = new Font(font.getFamily, 0, 1)
wenzelm@43491
    55
    val transform =
wenzelm@43491
    56
      new TransformAttribute(AffineTransform.getScaleInstance(1.0, font.getSize.toDouble))
wenzelm@43491
    57
    base_font.deriveFont(Map(TextAttribute.TRANSFORM -> transform))
wenzelm@43491
    58
  }
wenzelm@43491
    59
wenzelm@43487
    60
  class Style_Extender(symbols: Symbol.Interpretation) extends SyntaxUtilities.StyleExtender
wenzelm@43482
    61
  {
wenzelm@43487
    62
    if (symbols.font_names.length > 2)
wenzelm@43487
    63
      error("Too many user symbol fonts (max 2 permitted): " + symbols.font_names.mkString(", "))
wenzelm@43487
    64
wenzelm@43482
    65
    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
wenzelm@43482
    66
    {
wenzelm@43482
    67
      val new_styles = new Array[SyntaxStyle](full_range)
wenzelm@43482
    68
      for (i <- 0 until plain_range) {
wenzelm@43482
    69
        val style = styles(i)
wenzelm@43482
    70
        new_styles(i) = style
wenzelm@43482
    71
        new_styles(subscript(i.toByte)) = script_style(style, -1)
wenzelm@43482
    72
        new_styles(superscript(i.toByte)) = script_style(style, 1)
wenzelm@43482
    73
        new_styles(bold(i.toByte)) = bold_style(style)
wenzelm@43487
    74
        for ((family, idx) <- symbols.font_index) {
wenzelm@43487
    75
          // FIXME adjust size
wenzelm@43487
    76
          new_styles(user_font(idx, i.toByte)) =
wenzelm@43487
    77
            font_style(style, font => new Font(family, font.getStyle, font.getSize))
wenzelm@43487
    78
        }
wenzelm@43482
    79
      }
wenzelm@43491
    80
      new_styles(hidden) = new SyntaxStyle(Color.white, null, hidden_font(styles(0).getFont))
wenzelm@43482
    81
      new_styles
wenzelm@43482
    82
    }
wenzelm@43482
    83
  }
wenzelm@43482
    84
wenzelm@43487
    85
  def extended_styles(symbols: Symbol.Interpretation, text: CharSequence)
wenzelm@43443
    86
    : Map[Text.Offset, Byte => Byte] =
wenzelm@43443
    87
  {
wenzelm@43482
    88
    // FIXME \\<^bsub> \\<^esub> \\<^bsup> \\<^esup>
wenzelm@43482
    89
    def ctrl_style(sym: String): Option[Byte => Byte] =
wenzelm@43482
    90
      if (symbols.is_subscript_decoded(sym)) Some(subscript(_))
wenzelm@43482
    91
      else if (symbols.is_superscript_decoded(sym)) Some(superscript(_))
wenzelm@43482
    92
      else if (symbols.is_bold_decoded(sym)) Some(bold(_))
wenzelm@43482
    93
      else None
wenzelm@43455
    94
wenzelm@43482
    95
    var result = Map[Text.Offset, Byte => Byte]()
wenzelm@43482
    96
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
wenzelm@43482
    97
    {
wenzelm@43482
    98
      for (i <- start until stop) result += (i -> style)
wenzelm@43482
    99
    }
wenzelm@43482
   100
    var offset = 0
wenzelm@43482
   101
    var ctrl = ""
wenzelm@43489
   102
    for (sym <- Symbol.iterator_string(text)) {
wenzelm@43482
   103
      if (ctrl_style(sym).isDefined) ctrl = sym
wenzelm@43482
   104
      else if (ctrl != "") {
wenzelm@43487
   105
        if (symbols.is_controllable(sym) && sym != "\"" && !symbols.fonts.isDefinedAt(sym)) {
wenzelm@43482
   106
          mark(offset - ctrl.length, offset, _ => hidden)
wenzelm@43482
   107
          mark(offset, offset + sym.length, ctrl_style(ctrl).get)
wenzelm@43482
   108
        }
wenzelm@43482
   109
        ctrl = ""
wenzelm@43443
   110
      }
wenzelm@43488
   111
      symbols.lookup_font(sym) match {
wenzelm@43488
   112
        case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
wenzelm@43487
   113
        case _ =>
wenzelm@43487
   114
      }
wenzelm@43482
   115
      offset += sym.length
wenzelm@43443
   116
    }
wenzelm@43482
   117
    result
wenzelm@43443
   118
  }
wenzelm@43443
   119
wenzelm@43414
   120
wenzelm@43414
   121
  /* token marker */
wenzelm@43414
   122
wenzelm@43414
   123
  private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
wenzelm@43414
   124
wenzelm@43429
   125
  private class Line_Context(val context: Scan.Context)
wenzelm@43429
   126
    extends TokenMarker.LineContext(isabelle_rules, null)
wenzelm@43414
   127
  {
wenzelm@43414
   128
    override def hashCode: Int = context.hashCode
wenzelm@43414
   129
    override def equals(that: Any): Boolean =
wenzelm@43414
   130
      that match {
wenzelm@43414
   131
        case other: Line_Context => context == other.context
wenzelm@43414
   132
        case _ => false
wenzelm@43414
   133
      }
wenzelm@43414
   134
  }
wenzelm@43414
   135
wenzelm@43452
   136
  class Marker extends TokenMarker
wenzelm@43452
   137
  {
wenzelm@43452
   138
    override def markTokens(context: TokenMarker.LineContext,
wenzelm@43452
   139
        handler: TokenHandler, line: Segment): TokenMarker.LineContext =
wenzelm@43452
   140
    {
wenzelm@43452
   141
      val symbols = Isabelle.system.symbols
wenzelm@43452
   142
      val syntax = Isabelle.session.current_syntax()
wenzelm@43414
   143
wenzelm@43452
   144
      val ctxt =
wenzelm@43452
   145
        context match {
wenzelm@43452
   146
          case c: Line_Context => c.context
wenzelm@43452
   147
          case _ => Scan.Finished
wenzelm@43452
   148
        }
wenzelm@43452
   149
      val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
wenzelm@43452
   150
      val context1 = new Line_Context(ctxt1)
wenzelm@43452
   151
wenzelm@43452
   152
      val extended = extended_styles(symbols, line)
wenzelm@43443
   153
wenzelm@43452
   154
      var offset = 0
wenzelm@43452
   155
      for (token <- tokens) {
wenzelm@43452
   156
        val style = Isabelle_Markup.token_markup(syntax, token)
wenzelm@43452
   157
        val length = token.source.length
wenzelm@43452
   158
        val end_offset = offset + length
wenzelm@43452
   159
        if ((offset until end_offset) exists extended.isDefinedAt) {
wenzelm@43452
   160
          for (i <- offset until end_offset) {
wenzelm@43452
   161
            val style1 =
wenzelm@43452
   162
              extended.get(i) match {
wenzelm@43452
   163
                case None => style
wenzelm@43452
   164
                case Some(ext) => ext(style)
wenzelm@43452
   165
              }
wenzelm@43452
   166
            handler.handleToken(line, style1, i, 1, context1)
wenzelm@43443
   167
          }
wenzelm@43414
   168
        }
wenzelm@43452
   169
        else handler.handleToken(line, style, offset, length, context1)
wenzelm@43452
   170
        offset += length
wenzelm@43452
   171
      }
wenzelm@43452
   172
      handler.handleToken(line, JEditToken.END, line.count, 0, context1)
wenzelm@43452
   173
wenzelm@43452
   174
      val context2 = context1.intern
wenzelm@43452
   175
      handler.setLineContext(context2)
wenzelm@43452
   176
      context2
wenzelm@43452
   177
    }
wenzelm@43452
   178
  }
wenzelm@43452
   179
wenzelm@43416
   180
wenzelm@43452
   181
  /* mode provider */
wenzelm@43452
   182
wenzelm@43452
   183
  class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
wenzelm@43452
   184
  {
wenzelm@43452
   185
    for (mode <- orig_provider.getModes) addMode(mode)
wenzelm@43452
   186
wenzelm@43452
   187
    val isabelle_token_marker = new Token_Markup.Marker
wenzelm@43452
   188
wenzelm@43452
   189
    override def loadMode(mode: Mode, xmh: XModeHandler)
wenzelm@43452
   190
    {
wenzelm@43452
   191
      super.loadMode(mode, xmh)
wenzelm@43452
   192
      if (mode.getName == "isabelle")
wenzelm@43452
   193
        mode.setTokenMarker(isabelle_token_marker)
wenzelm@43414
   194
    }
wenzelm@43452
   195
  }
wenzelm@43414
   196
}
wenzelm@43414
   197