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