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