added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
authorwenzelm
Mon, 20 Jun 2011 22:43:56 +0200
changeset 43482 ebb90ff55b79
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
--- a/src/Tools/jEdit/patches/extended_styles	Mon Jun 20 12:13:43 2011 +0200
+++ b/src/Tools/jEdit/patches/extended_styles	Mon Jun 20 22:43:56 2011 +0200
@@ -1,7 +1,8 @@
-diff -ru jEdit/org/gjt/sp/jedit/Buffer.java jEdit-patched/org/gjt/sp/jedit/Buffer.java
---- jEdit/org/gjt/sp/jedit/Buffer.java	2010-05-09 14:29:25.000000000 +0200
-+++ jEdit-patched/org/gjt/sp/jedit/Buffer.java	2011-06-18 18:28:19.000000000 +0200
-@@ -2232,7 +2232,7 @@
+Only in jEdit-patched: build
+diff -ru jEdit/org/gjt/sp/jedit/Buffer.java jEdit-patched/org/gjt/sp/jedit/Buffer.java
+--- jEdit/org/gjt/sp/jedit/Buffer.java	2010-05-09 14:29:25.000000000 +0200
++++ jEdit-patched/org/gjt/sp/jedit/Buffer.java	2011-06-18 18:28:19.000000000 +0200
+@@ -2232,7 +2232,7 @@
  			start = next;
  			token = token.next;
  		}
@@ -10,10 +11,10 @@
  		{
  			JOptionPane.showMessageDialog(jEdit.getActiveView(),
  				jEdit.getProperty("syntax-style-no-token.message"),
-diff -ru jEdit/org/gjt/sp/jedit/syntax/Token.java jEdit-patched/org/gjt/sp/jedit/syntax/Token.java
---- jEdit/org/gjt/sp/jedit/syntax/Token.java	2010-05-09 14:29:24.000000000 +0200
-+++ jEdit-patched/org/gjt/sp/jedit/syntax/Token.java	2011-06-18 18:28:10.000000000 +0200
-@@ -57,7 +57,7 @@
+diff -ru jEdit/org/gjt/sp/jedit/syntax/Token.java jEdit-patched/org/gjt/sp/jedit/syntax/Token.java
+--- jEdit/org/gjt/sp/jedit/syntax/Token.java	2010-05-09 14:29:24.000000000 +0200
++++ jEdit-patched/org/gjt/sp/jedit/syntax/Token.java	2011-06-18 18:28:10.000000000 +0200
+@@ -57,7 +57,7 @@
  	 */
  	public static String tokenToString(byte token)
  	{
@@ -22,68 +23,41 @@
  	} //}}}
  
  	//{{{ Token types
-diff -ru jEdit/org/gjt/sp/util/SyntaxUtilities.java jEdit-patched/org/gjt/sp/util/SyntaxUtilities.java
---- jEdit/org/gjt/sp/util/SyntaxUtilities.java	2010-05-09 14:29:29.000000000 +0200
-+++ jEdit-patched/org/gjt/sp/util/SyntaxUtilities.java	2011-06-19 21:24:41.000000000 +0200
-@@ -26,6 +26,7 @@
- //{{{ Imports
- import java.awt.Color;
- import java.awt.Font;
-+import java.awt.font.TextAttribute;
- import java.util.Locale;
- import java.util.StringTokenizer;
- import org.gjt.sp.jedit.syntax.SyntaxStyle;
-@@ -194,6 +195,22 @@
+diff -ru jEdit/org/gjt/sp/util/SyntaxUtilities.java jEdit-patched/org/gjt/sp/util/SyntaxUtilities.java
+--- jEdit/org/gjt/sp/util/SyntaxUtilities.java	2010-05-09 14:29:29.000000000 +0200
++++ jEdit-patched/org/gjt/sp/util/SyntaxUtilities.java	2011-06-20 21:30:58.000000000 +0200
+@@ -194,6 +194,23 @@
  	}
  	
  	/**
-+	 * Style with sub/superscript font attribute.
++	 * Extended styles derived from the user-specified style array.
 +	 */
-+	public static SyntaxStyle scriptStyle(SyntaxStyle style, int script)
++
++	public static class StyleExtender
 +	{
-+		java.util.Map attributes = new java.util.HashMap();
-+		attributes.put(TextAttribute.SUPERSCRIPT, new Integer(script));
-+		return new SyntaxStyle(style.getForegroundColor(), style.getBackgroundColor(),
-+                  style.getFont().deriveFont(attributes));
++		public SyntaxStyle[] extendStyles(SyntaxStyle[] styles)
++		{
++			return styles;
++		}
++	}
++	volatile private static StyleExtender _styleExtender = new StyleExtender();
++	public static void setStyleExtender(StyleExtender ext)
++	{
++		_styleExtender = ext;
 +	}
 +
-+	public static SyntaxStyle boldStyle(SyntaxStyle style) {
-+		return new SyntaxStyle(style.getForegroundColor(), style.getBackgroundColor(),
-+                  style.getFont().deriveFont(Font.BOLD));
-+	}
-+	
 +	/**
  	 * Loads the syntax styles from the properties, giving them the specified
  	 * base font family and size.
  	 * @param family The font family
-@@ -203,10 +220,10 @@
- 	 */
- 	public static SyntaxStyle[] loadStyles(String family, int size, boolean color)
- 	{
--		SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT];
-+		SyntaxStyle[] styles = new SyntaxStyle[4 * Token.ID_COUNT + 1];
- 
- 		// start at 1 not 0 to skip Token.NULL
--		for(int i = 1; i < styles.length; i++)
-+		for(int i = 1; i < Token.ID_COUNT; i++)
- 		{
- 			try
- 			{
-@@ -223,6 +240,17 @@
+@@ -222,8 +239,9 @@
+ 				Log.log(Log.ERROR,StandardUtilities.class,e);
  			}
  		}
++		styles[0] = new SyntaxStyle(Color.black, null, new Font(family, 0, size));
  
-+		styles[0] = new SyntaxStyle(Color.black, null, new Font(family, 0, size));
-+		for(int i = 0; i < Token.ID_COUNT; i++)
-+		{
-+			styles[i + Token.ID_COUNT] = scriptStyle(styles[i], -1);
-+			styles[i + 2 * Token.ID_COUNT] = scriptStyle(styles[i], 1);
-+			styles[i + 3 * Token.ID_COUNT] = boldStyle(styles[i]);
-+		}
-+		styles[0] = null;
-+		styles[4 * Token.ID_COUNT] =
-+			new SyntaxStyle(Color.white, null, new Font(family, 0, 1));
-+
- 		return styles;
+-		return styles;
++		return _styleExtender.extendStyles(styles);
  	} //}}}
  	
+ 	private SyntaxUtilities(){}
--- a/src/Tools/jEdit/src/plugin.scala	Mon Jun 20 12:13:43 2011 +0200
+++ b/src/Tools/jEdit/src/plugin.scala	Mon Jun 20 22:43:56 2011 +0200
@@ -39,11 +39,6 @@
   var session: Session = null
 
 
-  /* extended syntax styles */
-
-  def extended_styles: Boolean = plugin != null && plugin._extended_styles
-
-
   /* properties */
 
   val OPTION_PREFIX = "options.isabelle."
@@ -270,19 +265,6 @@
 
 class Plugin extends EBPlugin
 {
-  /* extended syntax styles */
-
-  @volatile var _extended_styles: Boolean = false
-
-  private def check_extended_styles()
-  {
-    val family = jEdit.getProperty("view.font")
-    val size = jEdit.getIntegerProperty("view.fontsize", 12)
-    val styles = SyntaxUtilities.loadStyles(family, size)
-    _extended_styles = (styles.length == JEditToken.ID_COUNT * 4 + 1)
-  }
-
-
   /* session management */
 
   private def init_model(buffer: Buffer)
@@ -373,7 +355,6 @@
     message match {
       case msg: EditorStarted =>
       Isabelle.check_jvm()
-      check_extended_styles()
       if (Isabelle.Boolean_Property("auto-start")) Isabelle.start_session()
 
       case msg: BufferUpdate
@@ -408,15 +389,15 @@
     }
   }
 
-
   override def start()
   {
-    ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
     Isabelle.plugin = this
     Isabelle.setup_tooltips()
     Isabelle.system = new Isabelle_System
     Isabelle.system.install_fonts()
     Isabelle.session = new Session(Isabelle.system)
+    SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
+    ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
     Isabelle.session.phase_changed += session_manager
   }
 
--- a/src/Tools/jEdit/src/token_markup.scala	Mon Jun 20 12:13:43 2011 +0200
+++ b/src/Tools/jEdit/src/token_markup.scala	Mon Jun 20 22:43:56 2011 +0200
@@ -9,9 +9,13 @@
 
 import isabelle._
 
+import java.awt.{Font, Color}
+import java.awt.font.TextAttribute
+
+import org.gjt.sp.util.SyntaxUtilities
 import org.gjt.sp.jedit.Mode
 import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
-  ParserRuleSet, ModeProvider, XModeHandler}
+  ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
 
 import javax.swing.text.Segment
 
@@ -21,6 +25,7 @@
   /* extended syntax styles */
 
   private val plain_range: Int = JEditToken.ID_COUNT
+  private val full_range: Int = 4 * plain_range + 1
   private def check_range(i: Int) { require(0 <= i && i < plain_range) }
 
   def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
@@ -28,38 +33,64 @@
   def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
   val hidden: Byte = (4 * plain_range).toByte
 
+  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
+  {
+    import scala.collection.JavaConversions._
+    val font = style.getFont.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
+    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, font)
+  }
+
+  private def bold_style(style: SyntaxStyle): SyntaxStyle =
+    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor,
+      style.getFont.deriveFont(Font.BOLD))
+
+  class Style_Extender extends SyntaxUtilities.StyleExtender
+  {
+    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
+    {
+      val new_styles = new Array[SyntaxStyle](full_range)
+      for (i <- 0 until plain_range) {
+        val style = styles(i)
+        new_styles(i) = style
+        new_styles(subscript(i.toByte)) = script_style(style, -1)
+        new_styles(superscript(i.toByte)) = script_style(style, 1)
+        new_styles(bold(i.toByte)) = bold_style(style)
+      }
+      new_styles(hidden) =
+        new SyntaxStyle(Color.white, null, new Font(styles(0).getFont.getFamily, 0, 1))
+      new_styles
+    }
+  }
+
   private def extended_styles(symbols: Symbol.Interpretation, text: CharSequence)
     : Map[Text.Offset, Byte => Byte] =
   {
-    if (Isabelle.extended_styles) {
-      // FIXME \\<^bsub> \\<^esub> \\<^bsup> \\<^esup>
-      def ctrl_style(sym: String): Option[Byte => Byte] =
-        if (symbols.is_subscript_decoded(sym)) Some(subscript(_))
-        else if (symbols.is_superscript_decoded(sym)) Some(superscript(_))
-        else if (symbols.is_bold_decoded(sym)) Some(bold(_))
-        else None
+    // FIXME \\<^bsub> \\<^esub> \\<^bsup> \\<^esup>
+    def ctrl_style(sym: String): Option[Byte => Byte] =
+      if (symbols.is_subscript_decoded(sym)) Some(subscript(_))
+      else if (symbols.is_superscript_decoded(sym)) Some(superscript(_))
+      else if (symbols.is_bold_decoded(sym)) Some(bold(_))
+      else None
 
-      var result = Map[Text.Offset, Byte => Byte]()
-      def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
-      {
-        for (i <- start until stop) result += (i -> style)
+    var result = Map[Text.Offset, Byte => Byte]()
+    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
+    {
+      for (i <- start until stop) result += (i -> style)
+    }
+    var offset = 0
+    var ctrl = ""
+    for (sym <- Symbol.iterator(text).map(_.toString)) {
+      if (ctrl_style(sym).isDefined) ctrl = sym
+      else if (ctrl != "") {
+        if (symbols.is_controllable(sym) && sym != "\"") {
+          mark(offset - ctrl.length, offset, _ => hidden)
+          mark(offset, offset + sym.length, ctrl_style(ctrl).get)
+        }
+        ctrl = ""
       }
-      var offset = 0
-      var ctrl = ""
-      for (sym <- Symbol.iterator(text).map(_.toString)) {
-        if (ctrl_style(sym).isDefined) ctrl = sym
-        else if (ctrl != "") {
-          if (symbols.is_controllable(sym) && sym != "\"") {
-            mark(offset - ctrl.length, offset, _ => hidden)
-            mark(offset, offset + sym.length, ctrl_style(ctrl).get)
-          }
-          ctrl = ""
-        }
-        offset += sym.length
-      }
-      result
+      offset += sym.length
     }
-    else Map.empty
+    result
   }