added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
authorwenzelm
Mon Jun 20 22:43:56 2011 +0200 (2011-06-20)
changeset 43482ebb90ff55b79
parent 43481 51857e7fa64b
child 43483 a7a8496d3bfc
added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
src/Tools/jEdit/patches/extended_styles
src/Tools/jEdit/src/plugin.scala
src/Tools/jEdit/src/token_markup.scala
     1.1 --- a/src/Tools/jEdit/patches/extended_styles	Mon Jun 20 12:13:43 2011 +0200
     1.2 +++ b/src/Tools/jEdit/patches/extended_styles	Mon Jun 20 22:43:56 2011 +0200
     1.3 @@ -1,7 +1,8 @@
     1.4 -diff -ru jEdit/org/gjt/sp/jedit/Buffer.java jEdit-patched/org/gjt/sp/jedit/Buffer.java
     1.5 ---- jEdit/org/gjt/sp/jedit/Buffer.java	2010-05-09 14:29:25.000000000 +0200
     1.6 -+++ jEdit-patched/org/gjt/sp/jedit/Buffer.java	2011-06-18 18:28:19.000000000 +0200
     1.7 -@@ -2232,7 +2232,7 @@
     1.8 +Only in jEdit-patched: build
     1.9 +diff -ru jEdit/org/gjt/sp/jedit/Buffer.java jEdit-patched/org/gjt/sp/jedit/Buffer.java
    1.10 +--- jEdit/org/gjt/sp/jedit/Buffer.java	2010-05-09 14:29:25.000000000 +0200
    1.11 ++++ jEdit-patched/org/gjt/sp/jedit/Buffer.java	2011-06-18 18:28:19.000000000 +0200
    1.12 +@@ -2232,7 +2232,7 @@
    1.13   			start = next;
    1.14   			token = token.next;
    1.15   		}
    1.16 @@ -10,10 +11,10 @@
    1.17   		{
    1.18   			JOptionPane.showMessageDialog(jEdit.getActiveView(),
    1.19   				jEdit.getProperty("syntax-style-no-token.message"),
    1.20 -diff -ru jEdit/org/gjt/sp/jedit/syntax/Token.java jEdit-patched/org/gjt/sp/jedit/syntax/Token.java
    1.21 ---- jEdit/org/gjt/sp/jedit/syntax/Token.java	2010-05-09 14:29:24.000000000 +0200
    1.22 -+++ jEdit-patched/org/gjt/sp/jedit/syntax/Token.java	2011-06-18 18:28:10.000000000 +0200
    1.23 -@@ -57,7 +57,7 @@
    1.24 +diff -ru jEdit/org/gjt/sp/jedit/syntax/Token.java jEdit-patched/org/gjt/sp/jedit/syntax/Token.java
    1.25 +--- jEdit/org/gjt/sp/jedit/syntax/Token.java	2010-05-09 14:29:24.000000000 +0200
    1.26 ++++ jEdit-patched/org/gjt/sp/jedit/syntax/Token.java	2011-06-18 18:28:10.000000000 +0200
    1.27 +@@ -57,7 +57,7 @@
    1.28   	 */
    1.29   	public static String tokenToString(byte token)
    1.30   	{
    1.31 @@ -22,68 +23,41 @@
    1.32   	} //}}}
    1.33   
    1.34   	//{{{ Token types
    1.35 -diff -ru jEdit/org/gjt/sp/util/SyntaxUtilities.java jEdit-patched/org/gjt/sp/util/SyntaxUtilities.java
    1.36 ---- jEdit/org/gjt/sp/util/SyntaxUtilities.java	2010-05-09 14:29:29.000000000 +0200
    1.37 -+++ jEdit-patched/org/gjt/sp/util/SyntaxUtilities.java	2011-06-19 21:24:41.000000000 +0200
    1.38 -@@ -26,6 +26,7 @@
    1.39 - //{{{ Imports
    1.40 - import java.awt.Color;
    1.41 - import java.awt.Font;
    1.42 -+import java.awt.font.TextAttribute;
    1.43 - import java.util.Locale;
    1.44 - import java.util.StringTokenizer;
    1.45 - import org.gjt.sp.jedit.syntax.SyntaxStyle;
    1.46 -@@ -194,6 +195,22 @@
    1.47 +diff -ru jEdit/org/gjt/sp/util/SyntaxUtilities.java jEdit-patched/org/gjt/sp/util/SyntaxUtilities.java
    1.48 +--- jEdit/org/gjt/sp/util/SyntaxUtilities.java	2010-05-09 14:29:29.000000000 +0200
    1.49 ++++ jEdit-patched/org/gjt/sp/util/SyntaxUtilities.java	2011-06-20 21:30:58.000000000 +0200
    1.50 +@@ -194,6 +194,23 @@
    1.51   	}
    1.52   	
    1.53   	/**
    1.54 -+	 * Style with sub/superscript font attribute.
    1.55 ++	 * Extended styles derived from the user-specified style array.
    1.56  +	 */
    1.57 -+	public static SyntaxStyle scriptStyle(SyntaxStyle style, int script)
    1.58 ++
    1.59 ++	public static class StyleExtender
    1.60  +	{
    1.61 -+		java.util.Map attributes = new java.util.HashMap();
    1.62 -+		attributes.put(TextAttribute.SUPERSCRIPT, new Integer(script));
    1.63 -+		return new SyntaxStyle(style.getForegroundColor(), style.getBackgroundColor(),
    1.64 -+                  style.getFont().deriveFont(attributes));
    1.65 ++		public SyntaxStyle[] extendStyles(SyntaxStyle[] styles)
    1.66 ++		{
    1.67 ++			return styles;
    1.68 ++		}
    1.69 ++	}
    1.70 ++	volatile private static StyleExtender _styleExtender = new StyleExtender();
    1.71 ++	public static void setStyleExtender(StyleExtender ext)
    1.72 ++	{
    1.73 ++		_styleExtender = ext;
    1.74  +	}
    1.75  +
    1.76 -+	public static SyntaxStyle boldStyle(SyntaxStyle style) {
    1.77 -+		return new SyntaxStyle(style.getForegroundColor(), style.getBackgroundColor(),
    1.78 -+                  style.getFont().deriveFont(Font.BOLD));
    1.79 -+	}
    1.80 -+	
    1.81  +	/**
    1.82   	 * Loads the syntax styles from the properties, giving them the specified
    1.83   	 * base font family and size.
    1.84   	 * @param family The font family
    1.85 -@@ -203,10 +220,10 @@
    1.86 - 	 */
    1.87 - 	public static SyntaxStyle[] loadStyles(String family, int size, boolean color)
    1.88 - 	{
    1.89 --		SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT];
    1.90 -+		SyntaxStyle[] styles = new SyntaxStyle[4 * Token.ID_COUNT + 1];
    1.91 - 
    1.92 - 		// start at 1 not 0 to skip Token.NULL
    1.93 --		for(int i = 1; i < styles.length; i++)
    1.94 -+		for(int i = 1; i < Token.ID_COUNT; i++)
    1.95 - 		{
    1.96 - 			try
    1.97 - 			{
    1.98 -@@ -223,6 +240,17 @@
    1.99 +@@ -222,8 +239,9 @@
   1.100 + 				Log.log(Log.ERROR,StandardUtilities.class,e);
   1.101   			}
   1.102   		}
   1.103 ++		styles[0] = new SyntaxStyle(Color.black, null, new Font(family, 0, size));
   1.104   
   1.105 -+		styles[0] = new SyntaxStyle(Color.black, null, new Font(family, 0, size));
   1.106 -+		for(int i = 0; i < Token.ID_COUNT; i++)
   1.107 -+		{
   1.108 -+			styles[i + Token.ID_COUNT] = scriptStyle(styles[i], -1);
   1.109 -+			styles[i + 2 * Token.ID_COUNT] = scriptStyle(styles[i], 1);
   1.110 -+			styles[i + 3 * Token.ID_COUNT] = boldStyle(styles[i]);
   1.111 -+		}
   1.112 -+		styles[0] = null;
   1.113 -+		styles[4 * Token.ID_COUNT] =
   1.114 -+			new SyntaxStyle(Color.white, null, new Font(family, 0, 1));
   1.115 -+
   1.116 - 		return styles;
   1.117 +-		return styles;
   1.118 ++		return _styleExtender.extendStyles(styles);
   1.119   	} //}}}
   1.120   	
   1.121 + 	private SyntaxUtilities(){}
     2.1 --- a/src/Tools/jEdit/src/plugin.scala	Mon Jun 20 12:13:43 2011 +0200
     2.2 +++ b/src/Tools/jEdit/src/plugin.scala	Mon Jun 20 22:43:56 2011 +0200
     2.3 @@ -39,11 +39,6 @@
     2.4    var session: Session = null
     2.5  
     2.6  
     2.7 -  /* extended syntax styles */
     2.8 -
     2.9 -  def extended_styles: Boolean = plugin != null && plugin._extended_styles
    2.10 -
    2.11 -
    2.12    /* properties */
    2.13  
    2.14    val OPTION_PREFIX = "options.isabelle."
    2.15 @@ -270,19 +265,6 @@
    2.16  
    2.17  class Plugin extends EBPlugin
    2.18  {
    2.19 -  /* extended syntax styles */
    2.20 -
    2.21 -  @volatile var _extended_styles: Boolean = false
    2.22 -
    2.23 -  private def check_extended_styles()
    2.24 -  {
    2.25 -    val family = jEdit.getProperty("view.font")
    2.26 -    val size = jEdit.getIntegerProperty("view.fontsize", 12)
    2.27 -    val styles = SyntaxUtilities.loadStyles(family, size)
    2.28 -    _extended_styles = (styles.length == JEditToken.ID_COUNT * 4 + 1)
    2.29 -  }
    2.30 -
    2.31 -
    2.32    /* session management */
    2.33  
    2.34    private def init_model(buffer: Buffer)
    2.35 @@ -373,7 +355,6 @@
    2.36      message match {
    2.37        case msg: EditorStarted =>
    2.38        Isabelle.check_jvm()
    2.39 -      check_extended_styles()
    2.40        if (Isabelle.Boolean_Property("auto-start")) Isabelle.start_session()
    2.41  
    2.42        case msg: BufferUpdate
    2.43 @@ -408,15 +389,15 @@
    2.44      }
    2.45    }
    2.46  
    2.47 -
    2.48    override def start()
    2.49    {
    2.50 -    ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
    2.51      Isabelle.plugin = this
    2.52      Isabelle.setup_tooltips()
    2.53      Isabelle.system = new Isabelle_System
    2.54      Isabelle.system.install_fonts()
    2.55      Isabelle.session = new Session(Isabelle.system)
    2.56 +    SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
    2.57 +    ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
    2.58      Isabelle.session.phase_changed += session_manager
    2.59    }
    2.60  
     3.1 --- a/src/Tools/jEdit/src/token_markup.scala	Mon Jun 20 12:13:43 2011 +0200
     3.2 +++ b/src/Tools/jEdit/src/token_markup.scala	Mon Jun 20 22:43:56 2011 +0200
     3.3 @@ -9,9 +9,13 @@
     3.4  
     3.5  import isabelle._
     3.6  
     3.7 +import java.awt.{Font, Color}
     3.8 +import java.awt.font.TextAttribute
     3.9 +
    3.10 +import org.gjt.sp.util.SyntaxUtilities
    3.11  import org.gjt.sp.jedit.Mode
    3.12  import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
    3.13 -  ParserRuleSet, ModeProvider, XModeHandler}
    3.14 +  ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
    3.15  
    3.16  import javax.swing.text.Segment
    3.17  
    3.18 @@ -21,6 +25,7 @@
    3.19    /* extended syntax styles */
    3.20  
    3.21    private val plain_range: Int = JEditToken.ID_COUNT
    3.22 +  private val full_range: Int = 4 * plain_range + 1
    3.23    private def check_range(i: Int) { require(0 <= i && i < plain_range) }
    3.24  
    3.25    def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
    3.26 @@ -28,38 +33,64 @@
    3.27    def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
    3.28    val hidden: Byte = (4 * plain_range).toByte
    3.29  
    3.30 +  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
    3.31 +  {
    3.32 +    import scala.collection.JavaConversions._
    3.33 +    val font = style.getFont.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
    3.34 +    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, font)
    3.35 +  }
    3.36 +
    3.37 +  private def bold_style(style: SyntaxStyle): SyntaxStyle =
    3.38 +    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor,
    3.39 +      style.getFont.deriveFont(Font.BOLD))
    3.40 +
    3.41 +  class Style_Extender extends SyntaxUtilities.StyleExtender
    3.42 +  {
    3.43 +    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
    3.44 +    {
    3.45 +      val new_styles = new Array[SyntaxStyle](full_range)
    3.46 +      for (i <- 0 until plain_range) {
    3.47 +        val style = styles(i)
    3.48 +        new_styles(i) = style
    3.49 +        new_styles(subscript(i.toByte)) = script_style(style, -1)
    3.50 +        new_styles(superscript(i.toByte)) = script_style(style, 1)
    3.51 +        new_styles(bold(i.toByte)) = bold_style(style)
    3.52 +      }
    3.53 +      new_styles(hidden) =
    3.54 +        new SyntaxStyle(Color.white, null, new Font(styles(0).getFont.getFamily, 0, 1))
    3.55 +      new_styles
    3.56 +    }
    3.57 +  }
    3.58 +
    3.59    private def extended_styles(symbols: Symbol.Interpretation, text: CharSequence)
    3.60      : Map[Text.Offset, Byte => Byte] =
    3.61    {
    3.62 -    if (Isabelle.extended_styles) {
    3.63 -      // FIXME \\<^bsub> \\<^esub> \\<^bsup> \\<^esup>
    3.64 -      def ctrl_style(sym: String): Option[Byte => Byte] =
    3.65 -        if (symbols.is_subscript_decoded(sym)) Some(subscript(_))
    3.66 -        else if (symbols.is_superscript_decoded(sym)) Some(superscript(_))
    3.67 -        else if (symbols.is_bold_decoded(sym)) Some(bold(_))
    3.68 -        else None
    3.69 +    // FIXME \\<^bsub> \\<^esub> \\<^bsup> \\<^esup>
    3.70 +    def ctrl_style(sym: String): Option[Byte => Byte] =
    3.71 +      if (symbols.is_subscript_decoded(sym)) Some(subscript(_))
    3.72 +      else if (symbols.is_superscript_decoded(sym)) Some(superscript(_))
    3.73 +      else if (symbols.is_bold_decoded(sym)) Some(bold(_))
    3.74 +      else None
    3.75  
    3.76 -      var result = Map[Text.Offset, Byte => Byte]()
    3.77 -      def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
    3.78 -      {
    3.79 -        for (i <- start until stop) result += (i -> style)
    3.80 +    var result = Map[Text.Offset, Byte => Byte]()
    3.81 +    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
    3.82 +    {
    3.83 +      for (i <- start until stop) result += (i -> style)
    3.84 +    }
    3.85 +    var offset = 0
    3.86 +    var ctrl = ""
    3.87 +    for (sym <- Symbol.iterator(text).map(_.toString)) {
    3.88 +      if (ctrl_style(sym).isDefined) ctrl = sym
    3.89 +      else if (ctrl != "") {
    3.90 +        if (symbols.is_controllable(sym) && sym != "\"") {
    3.91 +          mark(offset - ctrl.length, offset, _ => hidden)
    3.92 +          mark(offset, offset + sym.length, ctrl_style(ctrl).get)
    3.93 +        }
    3.94 +        ctrl = ""
    3.95        }
    3.96 -      var offset = 0
    3.97 -      var ctrl = ""
    3.98 -      for (sym <- Symbol.iterator(text).map(_.toString)) {
    3.99 -        if (ctrl_style(sym).isDefined) ctrl = sym
   3.100 -        else if (ctrl != "") {
   3.101 -          if (symbols.is_controllable(sym) && sym != "\"") {
   3.102 -            mark(offset - ctrl.length, offset, _ => hidden)
   3.103 -            mark(offset, offset + sym.length, ctrl_style(ctrl).get)
   3.104 -          }
   3.105 -          ctrl = ""
   3.106 -        }
   3.107 -        offset += sym.length
   3.108 -      }
   3.109 -      result
   3.110 +      offset += sym.length
   3.111      }
   3.112 -    else Map.empty
   3.113 +    result
   3.114    }
   3.115  
   3.116