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