src/Tools/jEdit/src/plugin.scala
author wenzelm
Wed Aug 17 16:01:27 2011 +0200 (2011-08-17)
changeset 44238 36120feb70ed
parent 44225 a8f921e6484f
child 44379 1079ab6b342b
permissions -rw-r--r--
some convenience actions/shortcuts for control symbols;
     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 import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
    27 
    28 import org.gjt.sp.util.SyntaxUtilities
    29 import org.gjt.sp.util.Log
    30 
    31 import scala.actors.Actor
    32 import Actor._
    33 
    34 
    35 object Isabelle
    36 {
    37   /* plugin instance */
    38 
    39   var plugin: Plugin = null
    40   var session: Session = null
    41 
    42 
    43   /* properties */
    44 
    45   val OPTION_PREFIX = "options.isabelle."
    46 
    47   object Property
    48   {
    49     def apply(name: String): String =
    50       jEdit.getProperty(OPTION_PREFIX + name)
    51     def apply(name: String, default: String): String =
    52       jEdit.getProperty(OPTION_PREFIX + name, default)
    53     def update(name: String, value: String) =
    54       jEdit.setProperty(OPTION_PREFIX + name, value)
    55   }
    56 
    57   object Boolean_Property
    58   {
    59     def apply(name: String): Boolean =
    60       jEdit.getBooleanProperty(OPTION_PREFIX + name)
    61     def apply(name: String, default: Boolean): Boolean =
    62       jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
    63     def update(name: String, value: Boolean) =
    64       jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
    65   }
    66 
    67   object Int_Property
    68   {
    69     def apply(name: String): Int =
    70       jEdit.getIntegerProperty(OPTION_PREFIX + name)
    71     def apply(name: String, default: Int): Int =
    72       jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
    73     def update(name: String, value: Int) =
    74       jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
    75   }
    76 
    77   object Double_Property
    78   {
    79     def apply(name: String): Double =
    80       jEdit.getDoubleProperty(OPTION_PREFIX + name, 0.0)
    81     def apply(name: String, default: Double): Double =
    82       jEdit.getDoubleProperty(OPTION_PREFIX + name, default)
    83     def update(name: String, value: Double) =
    84       jEdit.setDoubleProperty(OPTION_PREFIX + name, value)
    85   }
    86 
    87   object Time_Property
    88   {
    89     def apply(name: String): Time =
    90       Time.seconds(Double_Property(name))
    91     def apply(name: String, default: Time): Time =
    92       Time.seconds(Double_Property(name, default.seconds))
    93     def update(name: String, value: Time) =
    94       Double_Property.update(name, value.seconds)
    95   }
    96 
    97 
    98   /* font */
    99 
   100   def font_family(): String = jEdit.getProperty("view.font")
   101 
   102   def font_size(): Float =
   103     (jEdit.getIntegerProperty("view.fontsize", 16) *
   104       Int_Property("relative-font-size", 100)).toFloat / 100
   105 
   106 
   107   /* text area ranges */
   108 
   109   sealed case class Gfx_Range(val x: Int, val y: Int, val length: Int)
   110 
   111   def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
   112   {
   113     val p = text_area.offsetToXY(range.start)
   114     val q = text_area.offsetToXY(range.stop)
   115     if (p != null && q != null && p.y == q.y) Some(new Gfx_Range(p.x, p.y, q.x - p.x))
   116     else None
   117   }
   118 
   119 
   120   /* tooltip markup */
   121 
   122   def tooltip(text: String): String =
   123     "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
   124         Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
   125       HTML.encode(text) + "</pre></html>"
   126 
   127   def tooltip_dismiss_delay(): Time =
   128     Time_Property("tooltip-dismiss-delay", Time.seconds(8.0)) max Time.seconds(0.5)
   129 
   130   def setup_tooltips()
   131   {
   132     Swing_Thread.now {
   133       val manager = javax.swing.ToolTipManager.sharedInstance
   134       manager.setDismissDelay(tooltip_dismiss_delay().ms.toInt)
   135     }
   136   }
   137 
   138 
   139   /* icons */
   140 
   141   def load_icon(name: String): javax.swing.Icon =
   142   {
   143     val icon = GUIUtilities.loadIcon(name)
   144     if (icon.getIconWidth < 0 || icon.getIconHeight < 0)
   145       Log.log(Log.ERROR, icon, "Bad icon: " + name)
   146     icon
   147   }
   148 
   149 
   150   /* check JVM */
   151 
   152   def check_jvm()
   153   {
   154     if (!Platform.is_hotspot) {
   155       Library.warning_dialog(jEdit.getActiveView, "Bad Java Virtual Machine",
   156         "This is " + Platform.jvm_name,
   157         "Isabelle/jEdit requires Java Hotspot from Sun/Oracle/Apple!")
   158     }
   159   }
   160 
   161 
   162   /* main jEdit components */
   163 
   164   def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
   165 
   166   def jedit_views(): Iterator[View] = jEdit.getViews().iterator
   167 
   168   def jedit_text_areas(view: View): Iterator[JEditTextArea] =
   169     view.getEditPanes().iterator.map(_.getTextArea)
   170 
   171   def jedit_text_areas(): Iterator[JEditTextArea] =
   172     jedit_views().flatMap(jedit_text_areas(_))
   173 
   174   def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
   175     jedit_text_areas().filter(_.getBuffer == buffer)
   176 
   177   def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
   178   {
   179     try { buffer.readLock(); body }
   180     finally { buffer.readUnlock() }
   181   }
   182 
   183   def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
   184     Swing_Thread.now { buffer_lock(buffer) { body } }
   185 
   186   def buffer_text(buffer: JEditBuffer): String =
   187     buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
   188 
   189   def buffer_path(buffer: Buffer): (String, String) =
   190   {
   191     val master_dir = buffer.getDirectory
   192     val path = buffer.getSymlinkPath
   193     (master_dir, path)
   194   }
   195 
   196 
   197   /* document model and view */
   198 
   199   def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
   200   def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
   201 
   202   def init_model(buffer: Buffer)
   203   {
   204     swing_buffer_lock(buffer) {
   205       val opt_model =
   206         document_model(buffer) match {
   207           case Some(model) => Some(model)
   208           case None =>
   209             val (master_dir, path) = buffer_path(buffer)
   210             Thy_Header.thy_name(path) match {
   211               case Some(name) =>
   212                 Some(Document_Model.init(session, buffer, master_dir, path, name))
   213               case None => None
   214             }
   215         }
   216       if (opt_model.isDefined) {
   217         for (text_area <- jedit_text_areas(buffer)) {
   218           if (document_view(text_area).map(_.model) != opt_model)
   219             Document_View.init(opt_model.get, text_area)
   220         }
   221       }
   222     }
   223   }
   224 
   225   def exit_model(buffer: Buffer)
   226   {
   227     swing_buffer_lock(buffer) {
   228       jedit_text_areas(buffer).foreach(Document_View.exit)
   229       Document_Model.exit(buffer)
   230     }
   231   }
   232 
   233   def init_view(buffer: Buffer, text_area: JEditTextArea)
   234   {
   235     swing_buffer_lock(buffer) {
   236       document_model(buffer) match {
   237         case Some(model) => Document_View.init(model, text_area)
   238         case None =>
   239       }
   240     }
   241   }
   242 
   243   def exit_view(buffer: Buffer, text_area: JEditTextArea)
   244   {
   245     swing_buffer_lock(buffer) {
   246       Document_View.exit(text_area)
   247     }
   248   }
   249 
   250 
   251   /* dockable windows */
   252 
   253   private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
   254 
   255   def docked_session(view: View): Option[Session_Dockable] =
   256     wm(view).getDockableWindow("isabelle-session") match {
   257       case dockable: Session_Dockable => Some(dockable)
   258       case _ => None
   259     }
   260 
   261   def docked_output(view: View): Option[Output_Dockable] =
   262     wm(view).getDockableWindow("isabelle-output") match {
   263       case dockable: Output_Dockable => Some(dockable)
   264       case _ => None
   265     }
   266 
   267   def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
   268     wm(view).getDockableWindow("isabelle-raw-output") match {
   269       case dockable: Raw_Output_Dockable => Some(dockable)
   270       case _ => None
   271     }
   272 
   273   def docked_protocol(view: View): Option[Protocol_Dockable] =
   274     wm(view).getDockableWindow("isabelle-protocol") match {
   275       case dockable: Protocol_Dockable => Some(dockable)
   276       case _ => None
   277     }
   278 
   279 
   280   /* logic image */
   281 
   282   def default_logic(): String =
   283   {
   284     val logic = Isabelle_System.getenv("JEDIT_LOGIC")
   285     if (logic != "") logic
   286     else Isabelle_System.getenv_strict("ISABELLE_LOGIC")
   287   }
   288 
   289   class Logic_Entry(val name: String, val description: String)
   290   {
   291     override def toString = description
   292   }
   293 
   294   def logic_selector(logic: String): ComboBox[Logic_Entry] =
   295   {
   296     val entries =
   297       new Logic_Entry("", "default (" + default_logic() + ")") ::
   298         Isabelle_System.find_logics().map(name => new Logic_Entry(name, name))
   299     val component = new ComboBox(entries)
   300     entries.find(_.name == logic) match {
   301       case None =>
   302       case Some(entry) => component.selection.item = entry
   303     }
   304     component.tooltip = "Isabelle logic image"
   305     component
   306   }
   307 
   308   def start_session()
   309   {
   310     val timeout = Time_Property("startup-timeout", Time.seconds(10)) max Time.seconds(5)
   311     val modes = space_explode(',', Isabelle_System.getenv("JEDIT_PRINT_MODE")).map("-m" + _)
   312     val logic = {
   313       val logic = Property("logic")
   314       if (logic != null && logic != "") logic
   315       else Isabelle.default_logic()
   316     }
   317     session.start(timeout, modes ::: List(logic))
   318   }
   319 
   320 
   321   /* convenience actions */
   322 
   323   private def user_input(text_area: JEditTextArea, s1: String, s2: String = "")
   324   {
   325     s1.foreach(text_area.userInput(_))
   326     s2.foreach(text_area.userInput(_))
   327     s2.foreach(_ => text_area.goToPrevCharacter(false))
   328   }
   329 
   330   def input_sub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.sub_decoded)
   331   def input_sup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.sup_decoded)
   332   def input_isub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.isub_decoded)
   333   def input_isup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.isup_decoded)
   334   def input_bsub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bsub_decoded, Symbol.esub_decoded)
   335   def input_bsup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bsup_decoded, Symbol.esup_decoded)
   336   def input_bold(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bold_decoded)
   337 }
   338 
   339 
   340 class Plugin extends EBPlugin
   341 {
   342   /* editor file store */
   343 
   344   private val file_store = new Session.File_Store
   345   {
   346     def append(master_dir: String, source_path: Path): String =
   347     {
   348       val path = source_path.expand
   349       if (path.is_absolute) Isabelle_System.platform_path(path)
   350       else {
   351         val vfs = VFSManager.getVFSForPath(master_dir)
   352         if (vfs.isInstanceOf[FileVFS])
   353           MiscUtilities.resolveSymlinks(
   354             vfs.constructPath(master_dir, Isabelle_System.platform_path(path)))
   355         else vfs.constructPath(master_dir, Isabelle_System.standard_path(path))
   356       }
   357     }
   358 
   359     def require(canonical_name: String)
   360     {
   361       Swing_Thread.later {
   362         if (!Isabelle.jedit_buffers().exists(_.getSymlinkPath == canonical_name))
   363           jEdit.openFile(null: View, canonical_name)
   364       }
   365     }
   366   }
   367 
   368 
   369   /* session manager */
   370 
   371   private val session_manager = actor {
   372     loop {
   373       react {
   374         case phase: Session.Phase =>
   375           phase match {
   376             case Session.Failed =>
   377               Swing_Thread.now {
   378                 val text = new scala.swing.TextArea(Isabelle.session.syslog())
   379                 text.editable = false
   380                 Library.error_dialog(jEdit.getActiveView, "Failed to start Isabelle process", text)
   381               }
   382 
   383             case Session.Ready => Isabelle.jedit_buffers.foreach(Isabelle.init_model)
   384             case Session.Shutdown => Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
   385             case _ =>
   386           }
   387         case bad => System.err.println("session_manager: ignoring bad message " + bad)
   388       }
   389     }
   390   }
   391 
   392 
   393   /* main plugin plumbing */
   394 
   395   override def handleMessage(message: EBMessage)
   396   {
   397     Swing_Thread.assert()
   398     message match {
   399       case msg: EditorStarted =>
   400         Isabelle.check_jvm()
   401         if (Isabelle.Boolean_Property("auto-start"))
   402           Isabelle.start_session()
   403 
   404       case msg: BufferUpdate
   405       if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
   406 
   407         val buffer = msg.getBuffer
   408         if (buffer != null && Isabelle.session.is_ready)
   409           Isabelle.init_model(buffer)
   410 
   411       case msg: EditPaneUpdate
   412       if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
   413           msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   414           msg.getWhat == EditPaneUpdate.CREATED ||
   415           msg.getWhat == EditPaneUpdate.DESTROYED) =>
   416 
   417         val edit_pane = msg.getEditPane
   418         val buffer = edit_pane.getBuffer
   419         val text_area = edit_pane.getTextArea
   420 
   421         if (buffer != null && text_area != null) {
   422           if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   423               msg.getWhat == EditPaneUpdate.CREATED) {
   424             if (Isabelle.session.is_ready)
   425               Isabelle.init_view(buffer, text_area)
   426           }
   427           else Isabelle.exit_view(buffer, text_area)
   428         }
   429 
   430       case msg: PropertiesChanged =>
   431         Swing_Thread.now { Isabelle.setup_tooltips() }
   432         Isabelle.session.global_settings.event(Session.Global_Settings)
   433 
   434       case _ =>
   435     }
   436   }
   437 
   438   override def start()
   439   {
   440     Isabelle.plugin = this
   441     Isabelle.setup_tooltips()
   442     Isabelle_System.init()
   443     Isabelle_System.install_fonts()
   444     Isabelle.session = new Session(file_store)
   445     SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
   446     if (ModeProvider.instance.isInstanceOf[ModeProvider])
   447       ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
   448     Isabelle.session.phase_changed += session_manager
   449   }
   450 
   451   override def stop()
   452   {
   453     Isabelle.session.phase_changed -= session_manager
   454     Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
   455     Isabelle.session.stop()
   456   }
   457 }