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