src/Tools/jEdit/src/plugin.scala
author wenzelm
Sun Jun 19 21:34:55 2011 +0200 (2011-06-19)
changeset 43460 2852f309174a
parent 43452 5cf548485529
child 43482 ebb90ff55b79
permissions -rw-r--r--
support for bold style within text buffer;
hidden: white foreground;
     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, ModeProvider}
    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   /* document model and view */
   195 
   196   def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
   197   def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
   198 
   199 
   200   /* dockable windows */
   201 
   202   private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
   203 
   204   def docked_session(view: View): Option[Session_Dockable] =
   205     wm(view).getDockableWindow("isabelle-session") match {
   206       case dockable: Session_Dockable => Some(dockable)
   207       case _ => None
   208     }
   209 
   210   def docked_output(view: View): Option[Output_Dockable] =
   211     wm(view).getDockableWindow("isabelle-output") match {
   212       case dockable: Output_Dockable => Some(dockable)
   213       case _ => None
   214     }
   215 
   216   def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
   217     wm(view).getDockableWindow("isabelle-raw-output") match {
   218       case dockable: Raw_Output_Dockable => Some(dockable)
   219       case _ => None
   220     }
   221 
   222   def docked_protocol(view: View): Option[Protocol_Dockable] =
   223     wm(view).getDockableWindow("isabelle-protocol") match {
   224       case dockable: Protocol_Dockable => Some(dockable)
   225       case _ => None
   226     }
   227 
   228 
   229   /* logic image */
   230 
   231   def default_logic(): String =
   232   {
   233     val logic = system.getenv("JEDIT_LOGIC")
   234     if (logic != "") logic
   235     else system.getenv_strict("ISABELLE_LOGIC")
   236   }
   237 
   238   class Logic_Entry(val name: String, val description: String)
   239   {
   240     override def toString = description
   241   }
   242 
   243   def logic_selector(logic: String): ComboBox[Logic_Entry] =
   244   {
   245     val entries =
   246       new Logic_Entry("", "default (" + default_logic() + ")") ::
   247         system.find_logics().map(name => new Logic_Entry(name, name))
   248     val component = new ComboBox(entries)
   249     entries.find(_.name == logic) match {
   250       case None =>
   251       case Some(entry) => component.selection.item = entry
   252     }
   253     component.tooltip = "Isabelle logic image"
   254     component
   255   }
   256 
   257   def start_session()
   258   {
   259     val timeout = Time_Property("startup-timeout", Time.seconds(10)) max Time.seconds(5)
   260     val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
   261     val logic = {
   262       val logic = Property("logic")
   263       if (logic != null && logic != "") logic
   264       else Isabelle.default_logic()
   265     }
   266     session.start(timeout, modes ::: List(logic))
   267   }
   268 }
   269 
   270 
   271 class Plugin extends EBPlugin
   272 {
   273   /* extended syntax styles */
   274 
   275   @volatile var _extended_styles: Boolean = false
   276 
   277   private def check_extended_styles()
   278   {
   279     val family = jEdit.getProperty("view.font")
   280     val size = jEdit.getIntegerProperty("view.fontsize", 12)
   281     val styles = SyntaxUtilities.loadStyles(family, size)
   282     _extended_styles = (styles.length == JEditToken.ID_COUNT * 4 + 1)
   283   }
   284 
   285 
   286   /* session management */
   287 
   288   private def init_model(buffer: Buffer)
   289   {
   290     Isabelle.swing_buffer_lock(buffer) {
   291       val opt_model =
   292         Document_Model(buffer) match {
   293           case Some(model) => Some(model)
   294           case None =>
   295             Thy_Header.split_thy_path(Isabelle.system.posix_path(buffer.getPath)) match {
   296               case Some((dir, thy_name)) =>
   297                 Some(Document_Model.init(Isabelle.session, buffer, dir + "/" + thy_name))
   298               case None => None
   299             }
   300         }
   301       if (opt_model.isDefined) {
   302         for (text_area <- Isabelle.jedit_text_areas(buffer)) {
   303           if (Document_View(text_area).map(_.model) != opt_model)
   304             Document_View.init(opt_model.get, text_area)
   305         }
   306       }
   307     }
   308   }
   309 
   310   private def exit_model(buffer: Buffer)
   311   {
   312     Isabelle.swing_buffer_lock(buffer) {
   313       Isabelle.jedit_text_areas(buffer).foreach(Document_View.exit)
   314       Document_Model.exit(buffer)
   315     }
   316   }
   317 
   318   private case class Init_Model(buffer: Buffer)
   319   private case class Exit_Model(buffer: Buffer)
   320   private case class Init_View(buffer: Buffer, text_area: JEditTextArea)
   321   private case class Exit_View(buffer: Buffer, text_area: JEditTextArea)
   322 
   323   private val session_manager = actor {
   324     var ready = false
   325     loop {
   326       react {
   327         case phase: Session.Phase =>
   328           ready = phase == Session.Ready
   329           phase match {
   330             case Session.Failed =>
   331               Swing_Thread.now {
   332                 val text = new scala.swing.TextArea(Isabelle.session.syslog())
   333                 text.editable = false
   334                 Library.error_dialog(jEdit.getActiveView, "Failed to start Isabelle process", text)
   335               }
   336 
   337             case Session.Ready => Isabelle.jedit_buffers.foreach(init_model)
   338             case Session.Shutdown => Isabelle.jedit_buffers.foreach(exit_model)
   339             case _ =>
   340           }
   341 
   342         case Init_Model(buffer) =>
   343           if (ready) init_model(buffer)
   344 
   345         case Exit_Model(buffer) =>
   346           exit_model(buffer)
   347 
   348         case Init_View(buffer, text_area) =>
   349           if (ready) {
   350             Isabelle.swing_buffer_lock(buffer) {
   351               Document_Model(buffer) match {
   352                 case Some(model) => Document_View.init(model, text_area)
   353                 case None =>
   354               }
   355             }
   356           }
   357 
   358         case Exit_View(buffer, text_area) =>
   359           Isabelle.swing_buffer_lock(buffer) {
   360             Document_View.exit(text_area)
   361           }
   362 
   363         case bad => System.err.println("session_manager: ignoring bad message " + bad)
   364       }
   365     }
   366   }
   367 
   368 
   369   /* main plugin plumbing */
   370 
   371   override def handleMessage(message: EBMessage)
   372   {
   373     message match {
   374       case msg: EditorStarted =>
   375       Isabelle.check_jvm()
   376       check_extended_styles()
   377       if (Isabelle.Boolean_Property("auto-start")) Isabelle.start_session()
   378 
   379       case msg: BufferUpdate
   380       if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
   381 
   382         val buffer = msg.getBuffer
   383         if (buffer != null) session_manager ! Init_Model(buffer)
   384 
   385       case msg: EditPaneUpdate
   386       if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
   387           msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   388           msg.getWhat == EditPaneUpdate.CREATED ||
   389           msg.getWhat == EditPaneUpdate.DESTROYED) =>
   390 
   391         val edit_pane = msg.getEditPane
   392         val buffer = edit_pane.getBuffer
   393         val text_area = edit_pane.getTextArea
   394 
   395         if (buffer != null && text_area != null) {
   396           if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   397               msg.getWhat == EditPaneUpdate.CREATED)
   398             session_manager ! Init_View(buffer, text_area)
   399           else
   400             session_manager ! Exit_View(buffer, text_area)
   401         }
   402 
   403       case msg: PropertiesChanged =>
   404         Swing_Thread.now { Isabelle.setup_tooltips() }
   405         Isabelle.session.global_settings.event(Session.Global_Settings)
   406 
   407       case _ =>
   408     }
   409   }
   410 
   411 
   412   override def start()
   413   {
   414     ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
   415     Isabelle.plugin = this
   416     Isabelle.setup_tooltips()
   417     Isabelle.system = new Isabelle_System
   418     Isabelle.system.install_fonts()
   419     Isabelle.session = new Session(Isabelle.system)
   420     Isabelle.session.phase_changed += session_manager
   421   }
   422 
   423   override def stop()
   424   {
   425     Isabelle.session.stop()
   426     Isabelle.session.phase_changed -= session_manager
   427   }
   428 }