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