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