src/Tools/jEdit/src/token_markup.scala
author wenzelm
Wed Jul 23 11:19:24 2014 +0200 (2014-07-23 ago)
changeset 57612 990ffb84489b
parent 57126 3a928dffc37f
child 58529 cd4439d8799c
permissions -rw-r--r--
clarified module name: facilitate alternative GUI frameworks;
     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
    14 import java.awt.geom.AffineTransform
    15 
    16 import org.gjt.sp.util.SyntaxUtilities
    17 import org.gjt.sp.jedit.{jEdit, Mode}
    18 import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
    19   ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
    20 import org.gjt.sp.jedit.textarea.{TextArea, Selection}
    21 import org.gjt.sp.jedit.buffer.JEditBuffer
    22 
    23 import javax.swing.text.Segment
    24 
    25 
    26 object Token_Markup
    27 {
    28   /* editing support for control symbols */
    29 
    30   val is_control_style =
    31     Set(Symbol.sub_decoded, Symbol.sup_decoded, Symbol.bold_decoded)
    32 
    33   def update_control_style(control: String, text: String): String =
    34   {
    35     val result = new StringBuilder
    36     for (sym <- Symbol.iterator(text) if !is_control_style(sym)) {
    37       if (Symbol.is_controllable(sym)) result ++= control
    38       result ++= sym
    39     }
    40     result.toString
    41   }
    42 
    43   def edit_control_style(text_area: TextArea, control: String)
    44   {
    45     GUI_Thread.assert {}
    46 
    47     val buffer = text_area.getBuffer
    48 
    49     text_area.getSelection.foreach(sel => {
    50       val before = JEdit_Lib.point_range(buffer, sel.getStart - 1)
    51       JEdit_Lib.try_get_text(buffer, before) match {
    52         case Some(s) if is_control_style(s) =>
    53           text_area.extendSelection(before.start, before.stop)
    54         case _ =>
    55       }
    56     })
    57 
    58     text_area.getSelection.toList match {
    59       case Nil =>
    60         text_area.setSelectedText(control)
    61       case sels =>
    62         JEdit_Lib.buffer_edit(buffer) {
    63           sels.foreach(sel =>
    64             text_area.setSelectedText(sel,
    65               update_control_style(control, text_area.getSelectedText(sel))))
    66         }
    67     }
    68   }
    69 
    70 
    71   /* extended syntax styles */
    72 
    73   private val plain_range: Int = JEditToken.ID_COUNT
    74   private val full_range = 6 * plain_range + 1
    75   private def check_range(i: Int) { require(0 <= i && i < plain_range) }
    76 
    77   def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
    78   def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
    79   def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
    80   def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
    81   val hidden: Byte = (6 * plain_range).toByte
    82 
    83   private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle =
    84     new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont))
    85 
    86   private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
    87   {
    88     font_style(style, font0 =>
    89       {
    90         import scala.collection.JavaConversions._
    91         val font1 = font0.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
    92 
    93         def shift(y: Float): Font =
    94           GUI.transform_font(font1, AffineTransform.getTranslateInstance(0.0, y.toDouble))
    95 
    96         val m0 = GUI.font_metrics(font0)
    97         val m1 = GUI.font_metrics(font1)
    98         val a = m1.getAscent - m0.getAscent
    99         val b = (m1.getDescent + m1.getLeading) - (m0.getDescent + m0.getLeading)
   100         if (a > 0.0f) shift(a)
   101         else if (b > 0.0f) shift(- b)
   102         else font1
   103       })
   104   }
   105 
   106   private def bold_style(style: SyntaxStyle): SyntaxStyle =
   107     font_style(style, font => font.deriveFont(if (font.isBold) Font.PLAIN else Font.BOLD))
   108 
   109   val hidden_color: Color = new Color(255, 255, 255, 0)
   110 
   111   class Style_Extender extends SyntaxUtilities.StyleExtender
   112   {
   113     val max_user_fonts = 2
   114     if (Symbol.font_names.length > max_user_fonts)
   115       error("Too many user symbol fonts (max " + max_user_fonts + " permitted): " +
   116         Symbol.font_names.mkString(", "))
   117 
   118     override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
   119     {
   120       val new_styles = new Array[SyntaxStyle](full_range)
   121       for (i <- 0 until plain_range) {
   122         val style = styles(i)
   123         new_styles(i) = style
   124         new_styles(subscript(i.toByte)) = script_style(style, -1)
   125         new_styles(superscript(i.toByte)) = script_style(style, 1)
   126         new_styles(bold(i.toByte)) = bold_style(style)
   127         for (idx <- 0 until max_user_fonts)
   128           new_styles(user_font(idx, i.toByte)) = style
   129         for ((family, idx) <- Symbol.font_index)
   130           new_styles(user_font(idx, i.toByte)) = font_style(style, GUI.imitate_font(family, _))
   131       }
   132       new_styles(hidden) =
   133         new SyntaxStyle(hidden_color, null,
   134           { val font = styles(0).getFont
   135             GUI.transform_font(new Font(font.getFamily, 0, 1),
   136               AffineTransform.getScaleInstance(1.0, font.getSize.toDouble)) })
   137       new_styles
   138     }
   139   }
   140 
   141   def extended_styles(text: CharSequence): Map[Text.Offset, Byte => Byte] =
   142   {
   143     // FIXME Symbol.bsub_decoded etc.
   144     def control_style(sym: String): Option[Byte => Byte] =
   145       if (sym == Symbol.sub_decoded) Some(subscript(_))
   146       else if (sym == Symbol.sup_decoded) Some(superscript(_))
   147       else if (sym == Symbol.bold_decoded) Some(bold(_))
   148       else None
   149 
   150     var result = Map[Text.Offset, Byte => Byte]()
   151     def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
   152     {
   153       for (i <- start until stop) result += (i -> style)
   154     }
   155     var offset = 0
   156     var control = ""
   157     for (sym <- Symbol.iterator(text)) {
   158       if (control_style(sym).isDefined) control = sym
   159       else if (control != "") {
   160         if (Symbol.is_controllable(sym) && sym != "\"" && !Symbol.fonts.isDefinedAt(sym)) {
   161           mark(offset - control.length, offset, _ => hidden)
   162           mark(offset, offset + sym.length, control_style(control).get)
   163         }
   164         control = ""
   165       }
   166       Symbol.lookup_font(sym) match {
   167         case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
   168         case _ =>
   169       }
   170       offset += sym.length
   171     }
   172     result
   173   }
   174 
   175 
   176   /* token marker */
   177 
   178   private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
   179 
   180   private class Line_Context(val context: Option[Scan.Line_Context])
   181     extends TokenMarker.LineContext(isabelle_rules, null)
   182   {
   183     override def hashCode: Int = context.hashCode
   184     override def equals(that: Any): Boolean =
   185       that match {
   186         case other: Line_Context => context == other.context
   187         case _ => false
   188       }
   189   }
   190 
   191   class Marker(mode: String) extends TokenMarker
   192   {
   193     override def markTokens(context: TokenMarker.LineContext,
   194         handler: TokenHandler, raw_line: Segment): TokenMarker.LineContext =
   195     {
   196       val line_ctxt =
   197         context match {
   198           case c: Line_Context => c.context
   199           case _ => Some(Scan.Finished)
   200         }
   201       val line = if (raw_line == null) new Segment else raw_line
   202 
   203       val context1 =
   204       {
   205         val (styled_tokens, context1) =
   206           if (mode == "isabelle-ml" || mode == "sml") {
   207             val (tokens, ctxt1) = ML_Lex.tokenize_line(mode == "sml", line, line_ctxt.get)
   208             val styled_tokens = tokens.map(tok => (Rendering.ml_token_markup(tok), tok.source))
   209             (styled_tokens, new Line_Context(Some(ctxt1)))
   210           }
   211           else {
   212             Isabelle.mode_syntax(mode) match {
   213               case Some(syntax) if syntax.has_tokens && line_ctxt.isDefined =>
   214                 val (tokens, ctxt1) = syntax.scan_line(line, line_ctxt.get)
   215                 val styled_tokens =
   216                   tokens.map(tok => (Rendering.token_markup(syntax, tok), tok.source))
   217                 (styled_tokens, new Line_Context(Some(ctxt1)))
   218               case _ =>
   219                 val styled_token = (JEditToken.NULL, line.subSequence(0, line.count).toString)
   220                 (List(styled_token), new Line_Context(None))
   221             }
   222           }
   223 
   224         val extended = extended_styles(line)
   225 
   226         var offset = 0
   227         for ((style, token) <- styled_tokens) {
   228           val length = token.length
   229           val end_offset = offset + length
   230           if ((offset until end_offset) exists
   231               (i => extended.isDefinedAt(i) || line.charAt(i) == '\t')) {
   232             for (i <- offset until end_offset) {
   233               val style1 =
   234                 extended.get(i) match {
   235                   case None => style
   236                   case Some(ext) => ext(style)
   237                 }
   238               handler.handleToken(line, style1, i, 1, context1)
   239             }
   240           }
   241           else handler.handleToken(line, style, offset, length, context1)
   242           offset += length
   243         }
   244         handler.handleToken(line, JEditToken.END, line.count, 0, context1)
   245         context1
   246       }
   247       val context2 = context1.intern
   248       handler.setLineContext(context2)
   249       context2
   250     }
   251   }
   252 
   253 
   254   /* mode provider */
   255 
   256   class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
   257   {
   258     for (mode <- orig_provider.getModes) addMode(mode)
   259 
   260     override def loadMode(mode: Mode, xmh: XModeHandler)
   261     {
   262       super.loadMode(mode, xmh)
   263       Isabelle.token_marker(mode.getName).foreach(mode.setTokenMarker _)
   264     }
   265   }
   266 
   267   def refresh_buffer(buffer: JEditBuffer)
   268   {
   269     Isabelle.token_marker(JEdit_Lib.buffer_mode(buffer)) match {
   270       case None =>
   271       case Some(marker) =>
   272         buffer.setTokenMarker(jEdit.getMode("text").getTokenMarker)
   273         buffer.setTokenMarker(marker)
   274     }
   275   }
   276 }
   277