src/Tools/jEdit/src/plugin.scala
author wenzelm
Sat Jun 18 17:33:27 2011 +0200 (2011-06-18)
changeset 43443 5d9693c2337e
parent 43390 7ee98a3802af
child 43449 591598bc52bc
permissions -rw-r--r--
basic support for extended syntax styles: sub/superscript;
     1 /*  Title:      Tools/jEdit/src/plugin.scala
     2     Author:     Makarius
     3 
     4 Main Isabelle/jEdit plugin setup.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import java.io.{FileInputStream, IOException}
    13 import java.awt.Font
    14 
    15 import scala.collection.mutable
    16 import scala.swing.ComboBox
    17 
    18 import org.gjt.sp.jedit.{jEdit, GUIUtilities, EBMessage, EBPlugin,
    19   Buffer, EditPane, ServiceManager, View}
    20 import org.gjt.sp.jedit.buffer.JEditBuffer
    21 import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
    22 import org.gjt.sp.jedit.syntax.{Token => JEditToken}
    23 import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
    24 import org.gjt.sp.jedit.gui.DockableWindowManager
    25 
    26 import org.gjt.sp.util.SyntaxUtilities
    27 import org.gjt.sp.util.Log
    28 
    29 import scala.actors.Actor
    30 import Actor._
    31 
    32 
    33 object Isabelle
    34 {
    35   /* plugin instance */
    36 
    37   var plugin: Plugin = null
    38   var system: Isabelle_System = null
    39   var session: Session = null
    40 
    41 
    42   /* extended syntax styles */
    43 
    44   def extended_styles: Boolean = plugin != null && plugin._extended_styles
    45 
    46 
    47   /* properties */
    48 
    49   val OPTION_PREFIX = "options.isabelle."
    50 
    51   object Property
    52   {
    53     def apply(name: String): String =
    54       jEdit.getProperty(OPTION_PREFIX + name)
    55     def apply(name: String, default: String): String =
    56       jEdit.getProperty(OPTION_PREFIX + name, default)
    57     def update(name: String, value: String) =
    58       jEdit.setProperty(OPTION_PREFIX + name, value)
    59   }
    60 
    61   object Boolean_Property
    62   {
    63     def apply(name: String): Boolean =
    64       jEdit.getBooleanProperty(OPTION_PREFIX + name)
    65     def apply(name: String, default: Boolean): Boolean =
    66       jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
    67     def update(name: String, value: Boolean) =
    68       jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
    69   }
    70 
    71   object Int_Property
    72   {
    73     def apply(name: String): Int =
    74       jEdit.getIntegerProperty(OPTION_PREFIX + name)
    75     def apply(name: String, default: Int): Int =
    76       jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
    77     def update(name: String, value: Int) =
    78       jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
    79   }
    80 
    81   object Double_Property
    82   {
    83     def apply(name: String): Double =
    84       jEdit.getDoubleProperty(OPTION_PREFIX + name, 0.0)
    85     def apply(name: String, default: Double): Double =
    86       jEdit.getDoubleProperty(OPTION_PREFIX + name, default)
    87     def update(name: String, value: Double) =
    88       jEdit.setDoubleProperty(OPTION_PREFIX + name, value)
    89   }
    90 
    91   object Time_Property
    92   {
    93     def apply(name: String): Time =
    94       Time.seconds(Double_Property(name))
    95     def apply(name: String, default: Time): Time =
    96       Time.seconds(Double_Property(name, default.seconds))
    97     def update(name: String, value: Time) =
    98       Double_Property.update(name, value.seconds)
    99   }
   100 
   101 
   102   /* font */
   103 
   104   def font_family(): String = jEdit.getProperty("view.font")
   105 
   106   def font_size(): Float =
   107     (jEdit.getIntegerProperty("view.fontsize", 16) *
   108       Int_Property("relative-font-size", 100)).toFloat / 100
   109 
   110 
   111   /* text area ranges */
   112 
   113   case class Gfx_Range(val x: Int, val y: Int, val length: Int)
   114 
   115   def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
   116   {
   117     val p = text_area.offsetToXY(range.start)
   118     val q = text_area.offsetToXY(range.stop)
   119     if (p != null && q != null && p.y == q.y) Some(new Gfx_Range(p.x, p.y, q.x - p.x))
   120     else None
   121   }
   122 
   123 
   124   /* tooltip markup */
   125 
   126   def tooltip(text: String): String =
   127     "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
   128         Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
   129       HTML.encode(text) + "</pre></html>"
   130 
   131   def tooltip_dismiss_delay(): Time =
   132     Time_Property("tooltip-dismiss-delay", Time.seconds(8.0)) max Time.seconds(0.5)
   133 
   134   def setup_tooltips()
   135   {
   136     Swing_Thread.now {
   137       val manager = javax.swing.ToolTipManager.sharedInstance
   138       manager.setDismissDelay(tooltip_dismiss_delay().ms.toInt)
   139     }
   140   }
   141 
   142 
   143   /* icons */
   144 
   145   def load_icon(name: String): javax.swing.Icon =
   146   {
   147     val icon = GUIUtilities.loadIcon(name)
   148     if (icon.getIconWidth < 0 || icon.getIconHeight < 0)
   149       Log.log(Log.ERROR, icon, "Bad icon: " + name)
   150     icon
   151   }
   152 
   153 
   154   /* check JVM */
   155 
   156   def check_jvm()
   157   {
   158     if (!Platform.is_hotspot) {
   159       Library.warning_dialog(jEdit.getActiveView, "Bad Java Virtual Machine",
   160         "This is " + Platform.jvm_name,
   161         "Isabelle/jEdit requires Java Hotspot from Sun/Oracle/Apple!")
   162     }
   163   }
   164 
   165 
   166   /* main jEdit components */
   167 
   168   def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
   169 
   170   def jedit_views(): Iterator[View] = jEdit.getViews().iterator
   171 
   172   def jedit_text_areas(view: View): Iterator[JEditTextArea] =
   173     view.getEditPanes().iterator.map(_.getTextArea)
   174 
   175   def jedit_text_areas(): Iterator[JEditTextArea] =
   176     jedit_views().flatMap(jedit_text_areas(_))
   177 
   178   def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
   179     jedit_text_areas().filter(_.getBuffer == buffer)
   180 
   181   def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
   182   {
   183     try { buffer.readLock(); body }
   184     finally { buffer.readUnlock() }
   185   }
   186 
   187   def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
   188     Swing_Thread.now { buffer_lock(buffer) { body } }
   189 
   190   def buffer_text(buffer: JEditBuffer): String =
   191     buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
   192 
   193 
   194   /* dockable windows */
   195 
   196   private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
   197 
   198   def docked_session(view: View): Option[Session_Dockable] =
   199     wm(view).getDockableWindow("isabelle-session") match {
   200       case dockable: Session_Dockable => Some(dockable)
   201       case _ => None
   202     }
   203 
   204   def docked_output(view: View): Option[Output_Dockable] =
   205     wm(view).getDockableWindow("isabelle-output") match {
   206       case dockable: Output_Dockable => Some(dockable)
   207       case _ => None
   208     }
   209 
   210   def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
   211     wm(view).getDockableWindow("isabelle-raw-output") match {
   212       case dockable: Raw_Output_Dockable => Some(dockable)
   213       case _ => None
   214     }
   215 
   216   def docked_protocol(view: View): Option[Protocol_Dockable] =
   217     wm(view).getDockableWindow("isabelle-protocol") match {
   218       case dockable: Protocol_Dockable => Some(dockable)
   219       case _ => None
   220     }
   221 
   222 
   223   /* logic image */
   224 
   225   def default_logic(): String =
   226   {
   227     val logic = system.getenv("JEDIT_LOGIC")
   228     if (logic != "") logic
   229     else system.getenv_strict("ISABELLE_LOGIC")
   230   }
   231 
   232   class Logic_Entry(val name: String, val description: String)
   233   {
   234     override def toString = description
   235   }
   236 
   237   def logic_selector(logic: String): ComboBox[Logic_Entry] =
   238   {
   239     val entries =
   240       new Logic_Entry("", "default (" + default_logic() + ")") ::
   241         system.find_logics().map(name => new Logic_Entry(name, name))
   242     val component = new ComboBox(entries)
   243     entries.find(_.name == logic) match {
   244       case None =>
   245       case Some(entry) => component.selection.item = entry
   246     }
   247     component.tooltip = "Isabelle logic image"
   248     component
   249   }
   250 
   251   def start_session()
   252   {
   253     val timeout = Time_Property("startup-timeout", Time.seconds(10)) max Time.seconds(5)
   254     val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
   255     val logic = {
   256       val logic = Property("logic")
   257       if (logic != null && logic != "") logic
   258       else Isabelle.default_logic()
   259     }
   260     session.start(timeout, modes ::: List(logic))
   261   }
   262 }
   263 
   264 
   265 class Plugin extends EBPlugin
   266 {
   267   /* extended syntax styles */
   268 
   269   @volatile var _extended_styles: Boolean = false
   270 
   271   private def check_extended_styles()
   272   {
   273     val family = jEdit.getProperty("view.font")
   274     val size = jEdit.getIntegerProperty("view.fontsize", 12)
   275     val styles = SyntaxUtilities.loadStyles(family, size)
   276     _extended_styles = (styles.length == JEditToken.ID_COUNT * 3 + 1)
   277   }
   278 
   279 
   280   /* session management */
   281 
   282   private def init_model(buffer: Buffer)
   283   {
   284     Isabelle.swing_buffer_lock(buffer) {
   285       val opt_model =
   286         Document_Model(buffer) match {
   287           case Some(model) => model.refresh; Some(model)
   288           case None =>
   289             Thy_Header.split_thy_path(Isabelle.system.posix_path(buffer.getPath)) match {
   290               case Some((dir, thy_name)) =>
   291                 Some(Document_Model.init(Isabelle.session, buffer, dir + "/" + thy_name))
   292               case None => None
   293             }
   294         }
   295       if (opt_model.isDefined) {
   296         for (text_area <- Isabelle.jedit_text_areas(buffer)) {
   297           if (Document_View(text_area).map(_.model) != opt_model)
   298             Document_View.init(opt_model.get, text_area)
   299         }
   300       }
   301     }
   302   }
   303 
   304   private def exit_model(buffer: Buffer)
   305   {
   306     Isabelle.swing_buffer_lock(buffer) {
   307       Isabelle.jedit_text_areas(buffer).foreach(Document_View.exit)
   308       Document_Model.exit(buffer)
   309     }
   310   }
   311 
   312   private case class Init_Model(buffer: Buffer)
   313   private case class Exit_Model(buffer: Buffer)
   314   private case class Init_View(buffer: Buffer, text_area: JEditTextArea)
   315   private case class Exit_View(buffer: Buffer, text_area: JEditTextArea)
   316 
   317   private val session_manager = actor {
   318     var ready = false
   319     loop {
   320       react {
   321         case phase: Session.Phase =>
   322           ready = phase == Session.Ready
   323           phase match {
   324             case Session.Failed =>
   325               Swing_Thread.now {
   326                 val text = new scala.swing.TextArea(Isabelle.session.syslog())
   327                 text.editable = false
   328                 Library.error_dialog(jEdit.getActiveView, "Failed to start Isabelle process", text)
   329               }
   330 
   331             case Session.Ready => Isabelle.jedit_buffers.foreach(init_model)
   332             case Session.Shutdown => Isabelle.jedit_buffers.foreach(exit_model)
   333             case _ =>
   334           }
   335 
   336         case Init_Model(buffer) =>
   337           if (ready) init_model(buffer)
   338 
   339         case Exit_Model(buffer) =>
   340           exit_model(buffer)
   341 
   342         case Init_View(buffer, text_area) =>
   343           if (ready) {
   344             Isabelle.swing_buffer_lock(buffer) {
   345               Document_Model(buffer) match {
   346                 case Some(model) => Document_View.init(model, text_area)
   347                 case None =>
   348               }
   349             }
   350           }
   351 
   352         case Exit_View(buffer, text_area) =>
   353           Isabelle.swing_buffer_lock(buffer) {
   354             Document_View.exit(text_area)
   355           }
   356 
   357         case bad => System.err.println("session_manager: ignoring bad message " + bad)
   358       }
   359     }
   360   }
   361 
   362 
   363   /* main plugin plumbing */
   364 
   365   override def handleMessage(message: EBMessage)
   366   {
   367     message match {
   368       case msg: EditorStarted =>
   369       Isabelle.check_jvm()
   370       check_extended_styles()
   371       if (Isabelle.Boolean_Property("auto-start")) Isabelle.start_session()
   372 
   373       case msg: BufferUpdate
   374       if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
   375 
   376         val buffer = msg.getBuffer
   377         if (buffer != null) session_manager ! Init_Model(buffer)
   378 
   379       case msg: EditPaneUpdate
   380       if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
   381           msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   382           msg.getWhat == EditPaneUpdate.CREATED ||
   383           msg.getWhat == EditPaneUpdate.DESTROYED) =>
   384 
   385         val edit_pane = msg.getEditPane
   386         val buffer = edit_pane.getBuffer
   387         val text_area = edit_pane.getTextArea
   388 
   389         if (buffer != null && text_area != null) {
   390           if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   391               msg.getWhat == EditPaneUpdate.CREATED)
   392             session_manager ! Init_View(buffer, text_area)
   393           else
   394             session_manager ! Exit_View(buffer, text_area)
   395         }
   396 
   397       case msg: PropertiesChanged =>
   398         Swing_Thread.now { Isabelle.setup_tooltips() }
   399         Isabelle.session.global_settings.event(Session.Global_Settings)
   400 
   401       case _ =>
   402     }
   403   }
   404 
   405   override def start()
   406   {
   407     Isabelle.plugin = this
   408     Isabelle.setup_tooltips()
   409     Isabelle.system = new Isabelle_System
   410     Isabelle.system.install_fonts()
   411     Isabelle.session = new Session(Isabelle.system)
   412     Isabelle.session.phase_changed += session_manager
   413   }
   414 
   415   override def stop()
   416   {
   417     Isabelle.session.stop()
   418     Isabelle.session.phase_changed -= session_manager
   419   }
   420 }