src/Tools/jEdit/src/token_markup.scala
author wenzelm
Wed Aug 17 16:01:27 2011 +0200 (2011-08-17)
changeset 44238 36120feb70ed
parent 43695 5130dfe1b7be
child 44355 9c38bdc6d755
permissions -rw-r--r--
some convenience actions/shortcuts for control symbols;
     1 /*  Title:      Tools/jEdit/src/token_markup.scala
     2     Author:     Makarius
     3 
     4 Outer syntax token markup.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import java.awt.{Font, Color}
    13 import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
    14 import java.awt.geom.AffineTransform
    15 
    16 import org.gjt.sp.util.SyntaxUtilities
    17 import org.gjt.sp.jedit.Mode
    18 import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
    19   ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
    20 
    21 import javax.swing.text.Segment
    22 
    23 
    24 object Token_Markup
    25 {
    26   /* font operations */
    27 
    28   private def font_metrics(font: Font): LineMetrics =
    29     font.getLineMetrics("", new FontRenderContext(null, false, false))
    30 
    31   private def imitate_font(family: String, font: Font): Font =
    32   {
    33     val font1 = new Font (family, font.getStyle, font.getSize)
    34     font1.deriveFont(font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize)
    35   }
    36 
    37   private def transform_font(font: Font, transform: AffineTransform): Font =
    38   {
    39     import scala.collection.JavaConversions._
    40     font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform)))
    41   }
    42 
    43 
    44   /* extended syntax styles */
    45 
    46   private val plain_range: Int = JEditToken.ID_COUNT
    47   private val full_range = 6 * plain_range + 1
    48   private def check_range(i: Int) { require(0 <= i && i < plain_range) }
    49 
    50   def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
    51   def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
    52   def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
    53   def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
    54   val hidden: Byte = (6 * plain_range).toByte
    55 
    56   private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle =
    57     new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont))
    58 
    59   private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
    60   {
    61     font_style(style, font0 =>
    62       {
    63         import scala.collection.JavaConversions._
    64         val font1 = font0.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
    65 
    66         def shift(y: Float): Font =
    67           transform_font(font1, AffineTransform.getTranslateInstance(0.0, y.toDouble))
    68 
    69         val m0 = font_metrics(font0)
    70         val m1 = font_metrics(font1)
    71         val a = m1.getAscent - m0.getAscent
    72         val b = (m1.getDescent + m1.getLeading) - (m0.getDescent + m0.getLeading)
    73         if (a > 0.0f) shift(a)
    74         else if (b > 0.0f) shift(- b)
    75         else font1
    76       })
    77   }
    78 
    79   private def bold_style(style: SyntaxStyle): SyntaxStyle =
    80     font_style(style, _.deriveFont(Font.BOLD))
    81 
    82   class Style_Extender extends SyntaxUtilities.StyleExtender
    83   {
    84     if (Symbol.font_names.length > 2)
    85       error("Too many user symbol fonts (max 2 permitted): " + Symbol.font_names.mkString(", "))
    86 
    87     override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
    88     {
    89       val new_styles = new Array[SyntaxStyle](full_range)
    90       for (i <- 0 until plain_range) {
    91         val style = styles(i)
    92         new_styles(i) = style
    93         new_styles(subscript(i.toByte)) = script_style(style, -1)
    94         new_styles(superscript(i.toByte)) = script_style(style, 1)
    95         new_styles(bold(i.toByte)) = bold_style(style)
    96         for ((family, idx) <- Symbol.font_index)
    97           new_styles(user_font(idx, i.toByte)) = font_style(style, imitate_font(family, _))
    98       }
    99       new_styles(hidden) =
   100         new SyntaxStyle(Color.white, null,
   101           { val font = styles(0).getFont
   102             transform_font(new Font(font.getFamily, 0, 1),
   103               AffineTransform.getScaleInstance(1.0, font.getSize.toDouble)) })
   104       new_styles
   105     }
   106   }
   107 
   108   def extended_styles(text: CharSequence): Map[Text.Offset, Byte => Byte] =
   109   {
   110     // FIXME Symbol.bsub_decoded etc.
   111     def ctrl_style(sym: String): Option[Byte => Byte] =
   112       if (sym == Symbol.sub_decoded || sym == Symbol.isub_decoded) Some(subscript(_))
   113       else if (sym == Symbol.sup_decoded || sym == Symbol.isup_decoded) Some(superscript(_))
   114       else if (sym == Symbol.bold_decoded) Some(bold(_))
   115       else None
   116 
   117     var result = Map[Text.Offset, Byte => Byte]()
   118     def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
   119     {
   120       for (i <- start until stop) result += (i -> style)
   121     }
   122     var offset = 0
   123     var ctrl = ""
   124     for (sym <- Symbol.iterator(text)) {
   125       if (ctrl_style(sym).isDefined) ctrl = sym
   126       else if (ctrl != "") {
   127         if (Symbol.is_controllable(sym) && sym != "\"" && !Symbol.fonts.isDefinedAt(sym)) {
   128           mark(offset - ctrl.length, offset, _ => hidden)
   129           mark(offset, offset + sym.length, ctrl_style(ctrl).get)
   130         }
   131         ctrl = ""
   132       }
   133       Symbol.lookup_font(sym) match {
   134         case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
   135         case _ =>
   136       }
   137       offset += sym.length
   138     }
   139     result
   140   }
   141 
   142 
   143   /* token marker */
   144 
   145   private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
   146 
   147   private class Line_Context(val context: Scan.Context)
   148     extends TokenMarker.LineContext(isabelle_rules, null)
   149   {
   150     override def hashCode: Int = context.hashCode
   151     override def equals(that: Any): Boolean =
   152       that match {
   153         case other: Line_Context => context == other.context
   154         case _ => false
   155       }
   156   }
   157 
   158   class Marker extends TokenMarker
   159   {
   160     override def markTokens(context: TokenMarker.LineContext,
   161         handler: TokenHandler, line: Segment): TokenMarker.LineContext =
   162     {
   163       val context1 =
   164         if (Isabelle.session.is_ready) {
   165           val syntax = Isabelle.session.current_syntax()
   166     
   167           val ctxt =
   168             context match {
   169               case c: Line_Context => c.context
   170               case _ => Scan.Finished
   171             }
   172           val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
   173           val context1 = new Line_Context(ctxt1)
   174     
   175           val extended = extended_styles(line)
   176     
   177           var offset = 0
   178           for (token <- tokens) {
   179             val style = Isabelle_Markup.token_markup(syntax, token)
   180             val length = token.source.length
   181             val end_offset = offset + length
   182             if ((offset until end_offset) exists extended.isDefinedAt) {
   183               for (i <- offset until end_offset) {
   184                 val style1 =
   185                   extended.get(i) match {
   186                     case None => style
   187                     case Some(ext) => ext(style)
   188                   }
   189                 handler.handleToken(line, style1, i, 1, context1)
   190               }
   191             }
   192             else handler.handleToken(line, style, offset, length, context1)
   193             offset += length
   194           }
   195           handler.handleToken(line, JEditToken.END, line.count, 0, context1)
   196           context1
   197         }
   198         else {
   199           val context1 = new Line_Context(Scan.Finished)
   200           handler.handleToken(line, JEditToken.NULL, 0, line.count, context1)
   201           handler.handleToken(line, JEditToken.END, line.count, 0, context1)
   202           context1
   203         }
   204       val context2 = context1.intern
   205       handler.setLineContext(context2)
   206       context2
   207     }
   208   }
   209 
   210 
   211   /* mode provider */
   212 
   213   class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
   214   {
   215     for (mode <- orig_provider.getModes) addMode(mode)
   216 
   217     val isabelle_token_marker = new Token_Markup.Marker
   218 
   219     override def loadMode(mode: Mode, xmh: XModeHandler)
   220     {
   221       super.loadMode(mode, xmh)
   222       if (mode.getName == "isabelle")
   223         mode.setTokenMarker(isabelle_token_marker)
   224     }
   225   }
   226 }
   227