src/Tools/jEdit/src/plugin.scala
author wenzelm
Sat Jul 09 12:56:51 2011 +0200 (2011-07-09)
changeset 43714 3749d1e6dde9
parent 43697 77ce24aa1770
child 44160 8848867501fb
permissions -rw-r--r--
tuned signature;
     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.{File, 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, MiscUtilities, 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 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   sealed 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   def init_model(buffer: Buffer)
   195   {
   196     swing_buffer_lock(buffer) {
   197       val opt_model =
   198         document_model(buffer) match {
   199           case Some(model) => Some(model)
   200           case None =>
   201             // FIXME strip protocol prefix of URL
   202             {
   203               try {
   204                 Some(Thy_Header.split_thy_path(
   205                   Path.explode(Isabelle_System.posix_path(buffer.getPath))))
   206               }
   207               catch { case _ => None }
   208             } map {
   209               case (master_dir, thy_name) =>
   210                 Document_Model.init(session, buffer, master_dir, thy_name)
   211             }
   212         }
   213       if (opt_model.isDefined) {
   214         for (text_area <- jedit_text_areas(buffer)) {
   215           if (document_view(text_area).map(_.model) != opt_model)
   216             Document_View.init(opt_model.get, text_area)
   217         }
   218       }
   219     }
   220   }
   221 
   222   def exit_model(buffer: Buffer)
   223   {
   224     swing_buffer_lock(buffer) {
   225       jedit_text_areas(buffer).foreach(Document_View.exit)
   226       Document_Model.exit(buffer)
   227     }
   228   }
   229 
   230   def init_view(buffer: Buffer, text_area: JEditTextArea)
   231   {
   232     swing_buffer_lock(buffer) {
   233       document_model(buffer) match {
   234         case Some(model) => Document_View.init(model, text_area)
   235         case None =>
   236       }
   237     }
   238   }
   239 
   240   def exit_view(buffer: Buffer, text_area: JEditTextArea)
   241   {
   242     swing_buffer_lock(buffer) {
   243       Document_View.exit(text_area)
   244     }
   245   }
   246 
   247 
   248   /* dockable windows */
   249 
   250   private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
   251 
   252   def docked_session(view: View): Option[Session_Dockable] =
   253     wm(view).getDockableWindow("isabelle-session") match {
   254       case dockable: Session_Dockable => Some(dockable)
   255       case _ => None
   256     }
   257 
   258   def docked_output(view: View): Option[Output_Dockable] =
   259     wm(view).getDockableWindow("isabelle-output") match {
   260       case dockable: Output_Dockable => Some(dockable)
   261       case _ => None
   262     }
   263 
   264   def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
   265     wm(view).getDockableWindow("isabelle-raw-output") match {
   266       case dockable: Raw_Output_Dockable => Some(dockable)
   267       case _ => None
   268     }
   269 
   270   def docked_protocol(view: View): Option[Protocol_Dockable] =
   271     wm(view).getDockableWindow("isabelle-protocol") match {
   272       case dockable: Protocol_Dockable => Some(dockable)
   273       case _ => None
   274     }
   275 
   276 
   277   /* logic image */
   278 
   279   def default_logic(): String =
   280   {
   281     val logic = Isabelle_System.getenv("JEDIT_LOGIC")
   282     if (logic != "") logic
   283     else Isabelle_System.getenv_strict("ISABELLE_LOGIC")
   284   }
   285 
   286   class Logic_Entry(val name: String, val description: String)
   287   {
   288     override def toString = description
   289   }
   290 
   291   def logic_selector(logic: String): ComboBox[Logic_Entry] =
   292   {
   293     val entries =
   294       new Logic_Entry("", "default (" + default_logic() + ")") ::
   295         Isabelle_System.find_logics().map(name => new Logic_Entry(name, name))
   296     val component = new ComboBox(entries)
   297     entries.find(_.name == logic) match {
   298       case None =>
   299       case Some(entry) => component.selection.item = entry
   300     }
   301     component.tooltip = "Isabelle logic image"
   302     component
   303   }
   304 
   305   def start_session()
   306   {
   307     val timeout = Time_Property("startup-timeout", Time.seconds(10)) max Time.seconds(5)
   308     val modes = space_explode(',', Isabelle_System.getenv("JEDIT_PRINT_MODE")).map("-m" + _)
   309     val logic = {
   310       val logic = Property("logic")
   311       if (logic != null && logic != "") logic
   312       else Isabelle.default_logic()
   313     }
   314     session.start(timeout, modes ::: List(logic))
   315   }
   316 }
   317 
   318 
   319 class Plugin extends EBPlugin
   320 {
   321   /* editor file store */
   322 
   323   private val file_store = new Session.File_Store
   324   {
   325     def read(path: Path): String =
   326     {
   327       val platform_path = Isabelle_System.platform_path(path)
   328       val canonical_path = MiscUtilities.resolveSymlinks(platform_path)
   329 
   330       Isabelle.jedit_buffers().find(buffer =>
   331           MiscUtilities.resolveSymlinks(buffer.getPath) == canonical_path) match {
   332         case Some(buffer) => Isabelle.buffer_text(buffer)
   333         case None => Standard_System.read_file(new File(platform_path))
   334       }
   335     }
   336   }
   337 
   338 
   339   /* session manager */
   340 
   341   private val session_manager = actor {
   342     loop {
   343       react {
   344         case phase: Session.Phase =>
   345           phase match {
   346             case Session.Failed =>
   347               Swing_Thread.now {
   348                 val text = new scala.swing.TextArea(Isabelle.session.syslog())
   349                 text.editable = false
   350                 Library.error_dialog(jEdit.getActiveView, "Failed to start Isabelle process", text)
   351               }
   352 
   353             case Session.Ready => Isabelle.jedit_buffers.foreach(Isabelle.init_model)
   354             case Session.Shutdown => Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
   355             case _ =>
   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     Swing_Thread.assert()
   368     message match {
   369       case msg: EditorStarted =>
   370         Isabelle.check_jvm()
   371         if (Isabelle.Boolean_Property("auto-start"))
   372           Isabelle.start_session()
   373 
   374       case msg: BufferUpdate
   375       if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
   376 
   377         val buffer = msg.getBuffer
   378         if (buffer != null && Isabelle.session.is_ready)
   379           Isabelle.init_model(buffer)
   380 
   381       case msg: EditPaneUpdate
   382       if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
   383           msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   384           msg.getWhat == EditPaneUpdate.CREATED ||
   385           msg.getWhat == EditPaneUpdate.DESTROYED) =>
   386 
   387         val edit_pane = msg.getEditPane
   388         val buffer = edit_pane.getBuffer
   389         val text_area = edit_pane.getTextArea
   390 
   391         if (buffer != null && text_area != null) {
   392           if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   393               msg.getWhat == EditPaneUpdate.CREATED) {
   394             if (Isabelle.session.is_ready)
   395               Isabelle.init_view(buffer, text_area)
   396           }
   397           else Isabelle.exit_view(buffer, text_area)
   398         }
   399 
   400       case msg: PropertiesChanged =>
   401         Swing_Thread.now { Isabelle.setup_tooltips() }
   402         Isabelle.session.global_settings.event(Session.Global_Settings)
   403 
   404       case _ =>
   405     }
   406   }
   407 
   408   override def start()
   409   {
   410     Isabelle.plugin = this
   411     Isabelle.setup_tooltips()
   412     Isabelle_System.init()
   413     Isabelle_System.install_fonts()
   414     Isabelle.session = new Session(file_store)
   415     SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
   416     if (ModeProvider.instance.isInstanceOf[ModeProvider])
   417       ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
   418     Isabelle.session.phase_changed += session_manager
   419   }
   420 
   421   override def stop()
   422   {
   423     Isabelle.session.phase_changed -= session_manager
   424     Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
   425     Isabelle.session.stop()
   426   }
   427 }