src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Thu Sep 23 18:44:26 2010 +0200 (2010-09-23)
changeset 39630 44181423183a
parent 39628 f6e82967b5cd
child 39631 cad7a5b7f641
permissions -rw-r--r--
explicit Session.Phase indication with associated event bus;
asynchronous Session.start();
synchronous Session.stop();
added Plugin.session_manager on top of basic Session;
eliminated separate isabelle.activate action;
misc tuning;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/plugin.scala
wenzelm@36760
     2
    Author:     Makarius
wenzelm@36760
     3
wenzelm@36760
     4
Main Isabelle/jEdit plugin setup.
wenzelm@36760
     5
*/
wenzelm@34407
     6
wenzelm@34318
     7
package isabelle.jedit
wenzelm@34318
     8
wenzelm@34429
     9
wenzelm@36015
    10
import isabelle._
wenzelm@36015
    11
wenzelm@34429
    12
import java.io.{FileInputStream, IOException}
wenzelm@34318
    13
import java.awt.Font
wenzelm@34318
    14
wenzelm@34497
    15
import scala.collection.mutable
wenzelm@39517
    16
import scala.swing.ComboBox
immler@34406
    17
wenzelm@39241
    18
import org.gjt.sp.jedit.{jEdit, GUIUtilities, EBMessage, EBPlugin,
wenzelm@39241
    19
  Buffer, EditPane, ServiceManager, View}
immler@34406
    20
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@39043
    21
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@39630
    22
import org.gjt.sp.jedit.msg.{BufferUpdate, EditPaneUpdate, PropertiesChanged, ViewUpdate}
wenzelm@37068
    23
import org.gjt.sp.jedit.gui.DockableWindowManager
wenzelm@34429
    24
wenzelm@39241
    25
import org.gjt.sp.util.Log
wenzelm@39241
    26
wenzelm@39630
    27
import scala.actors.Actor
wenzelm@39630
    28
import Actor._
wenzelm@39630
    29
wenzelm@34318
    30
wenzelm@34618
    31
object Isabelle
wenzelm@34618
    32
{
wenzelm@34784
    33
  /* plugin instance */
wenzelm@34784
    34
wenzelm@34784
    35
  var system: Isabelle_System = null
wenzelm@34784
    36
  var session: Session = null
wenzelm@34784
    37
wenzelm@34784
    38
wenzelm@34618
    39
  /* properties */
wenzelm@34618
    40
wenzelm@34618
    41
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    42
wenzelm@34618
    43
  object Property
wenzelm@34618
    44
  {
wenzelm@36814
    45
    def apply(name: String): String =
wenzelm@36814
    46
      jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@36814
    47
    def apply(name: String, default: String): String =
wenzelm@36814
    48
      jEdit.getProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    49
    def update(name: String, value: String) =
wenzelm@36814
    50
      jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    51
  }
wenzelm@34433
    52
wenzelm@34618
    53
  object Boolean_Property
wenzelm@34618
    54
  {
wenzelm@36814
    55
    def apply(name: String): Boolean =
wenzelm@36814
    56
      jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@36814
    57
    def apply(name: String, default: Boolean): Boolean =
wenzelm@36814
    58
      jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    59
    def update(name: String, value: Boolean) =
wenzelm@36814
    60
      jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    61
  }
wenzelm@34618
    62
wenzelm@34618
    63
  object Int_Property
wenzelm@34618
    64
  {
wenzelm@36814
    65
    def apply(name: String): Int =
wenzelm@36814
    66
      jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@36814
    67
    def apply(name: String, default: Int): Int =
wenzelm@36814
    68
      jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    69
    def update(name: String, value: Int) =
wenzelm@36814
    70
      jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    71
  }
wenzelm@34618
    72
wenzelm@37164
    73
wenzelm@37164
    74
  /* font */
wenzelm@37164
    75
wenzelm@37164
    76
  def font_family(): String = jEdit.getProperty("view.font")
wenzelm@37164
    77
wenzelm@37019
    78
  def font_size(): Float =
wenzelm@37019
    79
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
    80
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
    81
wenzelm@34618
    82
wenzelm@39043
    83
  /* text area ranges */
wenzelm@39043
    84
wenzelm@39043
    85
  case class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@39043
    86
wenzelm@39043
    87
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@39043
    88
  {
wenzelm@39043
    89
    val p = text_area.offsetToXY(range.start)
wenzelm@39043
    90
    val q = text_area.offsetToXY(range.stop)
wenzelm@39043
    91
    if (p != null && q != null && p.y == q.y) Some(new Gfx_Range(p.x, p.y, q.x - p.x))
wenzelm@39043
    92
    else None
wenzelm@39043
    93
  }
wenzelm@39043
    94
wenzelm@39043
    95
wenzelm@37201
    96
  /* tooltip markup */
wenzelm@37201
    97
wenzelm@37201
    98
  def tooltip(text: String): String =
wenzelm@37203
    99
    "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
wenzelm@37203
   100
        Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
wenzelm@37203
   101
      HTML.encode(text) + "</pre></html>"
wenzelm@37201
   102
wenzelm@38854
   103
  def tooltip_dismiss_delay(): Int =
wenzelm@38854
   104
    Int_Property("tooltip-dismiss-delay", 8000) max 500
wenzelm@38854
   105
wenzelm@38854
   106
  def setup_tooltips()
wenzelm@38854
   107
  {
wenzelm@38854
   108
    Swing_Thread.now {
wenzelm@38854
   109
      val manager = javax.swing.ToolTipManager.sharedInstance
wenzelm@38854
   110
      manager.setDismissDelay(tooltip_dismiss_delay())
wenzelm@38854
   111
    }
wenzelm@38854
   112
  }
wenzelm@38854
   113
wenzelm@37201
   114
wenzelm@39241
   115
  /* icons */
wenzelm@39241
   116
wenzelm@39241
   117
  def load_icon(name: String): javax.swing.Icon =
wenzelm@39241
   118
  {
wenzelm@39241
   119
    val icon = GUIUtilities.loadIcon(name)
wenzelm@39241
   120
    if (icon.getIconWidth < 0 || icon.getIconHeight < 0)
wenzelm@39630
   121
      Log.log(Log.ERROR, icon, "Bad icon: " + name)
wenzelm@39241
   122
    icon
wenzelm@39241
   123
  }
wenzelm@39241
   124
wenzelm@39241
   125
wenzelm@38221
   126
  /* main jEdit components */
wenzelm@34784
   127
wenzelm@37177
   128
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   129
wenzelm@37177
   130
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   131
wenzelm@34784
   132
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   133
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   134
wenzelm@34784
   135
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   136
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   137
wenzelm@34784
   138
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   139
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   140
wenzelm@38843
   141
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38640
   142
  {
wenzelm@38640
   143
    try { buffer.readLock(); body }
wenzelm@38640
   144
    finally { buffer.readUnlock() }
wenzelm@38640
   145
  }
wenzelm@38640
   146
wenzelm@38843
   147
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38843
   148
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@38843
   149
wenzelm@34784
   150
wenzelm@37068
   151
  /* dockable windows */
wenzelm@37068
   152
wenzelm@37068
   153
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   154
wenzelm@39515
   155
  def docked_session(view: View): Option[Session_Dockable] =
wenzelm@39515
   156
    wm(view).getDockableWindow("isabelle-session") match {
wenzelm@39515
   157
      case dockable: Session_Dockable => Some(dockable)
wenzelm@39515
   158
      case _ => None
wenzelm@39515
   159
    }
wenzelm@39515
   160
wenzelm@37068
   161
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   162
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   163
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   164
      case _ => None
wenzelm@37068
   165
    }
wenzelm@37068
   166
wenzelm@37068
   167
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   168
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   169
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   170
      case _ => None
wenzelm@37068
   171
    }
wenzelm@37068
   172
wenzelm@37068
   173
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   174
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   175
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   176
      case _ => None
wenzelm@37068
   177
    }
wenzelm@37068
   178
wenzelm@37068
   179
wenzelm@39517
   180
  /* logic image */
wenzelm@39517
   181
wenzelm@39517
   182
  def default_logic(): String =
wenzelm@39517
   183
  {
wenzelm@39517
   184
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@39517
   185
    if (logic != "") logic
wenzelm@39517
   186
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@39517
   187
  }
wenzelm@39517
   188
wenzelm@39517
   189
  class Logic_Entry(val name: String, val description: String)
wenzelm@39517
   190
  {
wenzelm@39517
   191
    override def toString = description
wenzelm@39517
   192
  }
wenzelm@39517
   193
wenzelm@39517
   194
  def logic_selector(logic: String): ComboBox[Logic_Entry] =
wenzelm@39517
   195
  {
wenzelm@39517
   196
    val entries =
wenzelm@39517
   197
      new Logic_Entry("", "default (" + default_logic() + ")") ::
wenzelm@39517
   198
        system.find_logics().map(name => new Logic_Entry(name, name))
wenzelm@39517
   199
    val component = new ComboBox(entries)
wenzelm@39517
   200
    entries.find(_.name == logic) match {
wenzelm@39517
   201
      case None =>
wenzelm@39517
   202
      case Some(entry) => component.selection.item = entry
wenzelm@39517
   203
    }
wenzelm@39517
   204
    component
wenzelm@39517
   205
  }
wenzelm@34318
   206
}
wenzelm@34318
   207
wenzelm@34429
   208
wenzelm@34618
   209
class Plugin extends EBPlugin
wenzelm@34618
   210
{
wenzelm@39630
   211
  /* session management */
wenzelm@39630
   212
wenzelm@39630
   213
  private def session_startup()
wenzelm@39630
   214
  {
wenzelm@39630
   215
    val timeout = Isabelle.Int_Property("startup-timeout") max 1000
wenzelm@39630
   216
    val modes = Isabelle.system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@39630
   217
    val logic = {
wenzelm@39630
   218
      val logic = Isabelle.Property("logic")
wenzelm@39630
   219
      if (logic != null && logic != "") logic
wenzelm@39630
   220
      else Isabelle.default_logic()
wenzelm@39630
   221
    }
wenzelm@39630
   222
    Isabelle.session.start(timeout, modes ::: List(logic))
wenzelm@39630
   223
  }
wenzelm@39630
   224
wenzelm@39630
   225
  private def activate_buffer(buffer: Buffer)
wenzelm@39630
   226
  {
wenzelm@39630
   227
    Isabelle.swing_buffer_lock(buffer) {
wenzelm@39630
   228
      Thy_Header.split_thy_path(Isabelle.system.posix_path(buffer.getPath)) match {
wenzelm@39630
   229
        case None =>
wenzelm@39630
   230
        case Some((_, thy_name)) =>
wenzelm@39630
   231
          val model = Document_Model.init(Isabelle.session, buffer, thy_name)
wenzelm@39630
   232
          for (text_area <- Isabelle.jedit_text_areas(buffer))
wenzelm@39630
   233
            Document_View.init(model, text_area)
wenzelm@39630
   234
      }
wenzelm@39630
   235
    }
wenzelm@39630
   236
  }
wenzelm@39630
   237
wenzelm@39630
   238
  private def deactivate_buffer(buffer: Buffer)
wenzelm@39630
   239
  {
wenzelm@39630
   240
    Isabelle.swing_buffer_lock(buffer) {
wenzelm@39630
   241
      for (text_area <- Isabelle.jedit_text_areas(buffer)) {
wenzelm@39630
   242
        if (Document_View(text_area).isDefined) Document_View.exit(text_area)
wenzelm@39630
   243
      }
wenzelm@39630
   244
      if (Document_Model(buffer).isDefined) Document_Model.exit(buffer)
wenzelm@39630
   245
    }
wenzelm@39630
   246
  }
wenzelm@39630
   247
wenzelm@39630
   248
  private val session_manager = Simple_Thread.actor("session_manager", daemon = true) {
wenzelm@39630
   249
    var finished = false
wenzelm@39630
   250
    while (!finished) {
wenzelm@39630
   251
      receive {
wenzelm@39630
   252
        case (Session.Startup, Session.Exit) =>
wenzelm@39630
   253
          val text = new scala.swing.TextArea(Isabelle.session.syslog())
wenzelm@39630
   254
          text.editable = false
wenzelm@39630
   255
          // FIXME proper view!?
wenzelm@39630
   256
          Library.error_dialog(null, null, "Failed to start Isabelle process", text)
wenzelm@39630
   257
          finished = true
wenzelm@39630
   258
wenzelm@39630
   259
        case (_, Session.Ready) => Isabelle.jedit_buffers.foreach(activate_buffer)
wenzelm@39630
   260
        case (_, Session.Shutdown) => Isabelle.jedit_buffers.foreach(deactivate_buffer)
wenzelm@39630
   261
        case (_, Session.Terminated) => finished = true
wenzelm@39630
   262
      }
wenzelm@39630
   263
    }
wenzelm@39630
   264
  }
wenzelm@39630
   265
wenzelm@39630
   266
wenzelm@34618
   267
  /* main plugin plumbing */
wenzelm@34433
   268
wenzelm@34767
   269
  override def handleMessage(message: EBMessage)
wenzelm@34618
   270
  {
wenzelm@34767
   271
    message match {
wenzelm@39630
   272
      case msg: ViewUpdate
wenzelm@39630
   273
        if msg.getWhat == ViewUpdate.ACTIVATED =>
wenzelm@39630
   274
        session_startup()
wenzelm@39630
   275
wenzelm@37557
   276
      case msg: BufferUpdate
wenzelm@37557
   277
        if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@37557
   278
        Document_Model(msg.getBuffer) match {
wenzelm@37557
   279
          case Some(model) => model.refresh()
wenzelm@37557
   280
          case _ =>
wenzelm@37557
   281
        }
wenzelm@37557
   282
wenzelm@34767
   283
      case msg: EditPaneUpdate =>
wenzelm@34784
   284
        val edit_pane = msg.getEditPane
wenzelm@34784
   285
        val buffer = edit_pane.getBuffer
wenzelm@34784
   286
        val text_area = edit_pane.getTextArea
wenzelm@34784
   287
wenzelm@34787
   288
        def init_view()
wenzelm@34787
   289
        {
wenzelm@34788
   290
          Document_Model(buffer) match {
wenzelm@34787
   291
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   292
            case None =>
wenzelm@34787
   293
          }
wenzelm@34787
   294
        }
wenzelm@34787
   295
        def exit_view()
wenzelm@34787
   296
        {
wenzelm@34788
   297
          if (Document_View(text_area).isDefined)
wenzelm@34787
   298
            Document_View.exit(text_area)
wenzelm@34787
   299
        }
wenzelm@34767
   300
        msg.getWhat match {
wenzelm@34787
   301
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   302
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   303
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   304
          case _ =>
immler@34671
   305
        }
wenzelm@34784
   306
wenzelm@34777
   307
      case msg: PropertiesChanged =>
wenzelm@37241
   308
        Swing_Thread.now {
wenzelm@39630
   309
          Isabelle.setup_tooltips()
wenzelm@37241
   310
          for (text_area <- Isabelle.jedit_text_areas if Document_View(text_area).isDefined)
wenzelm@37241
   311
            Document_View(text_area).get.extend_styles()
wenzelm@37241
   312
        }
wenzelm@34791
   313
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   314
wenzelm@34318
   315
      case _ =>
wenzelm@34318
   316
    }
wenzelm@34318
   317
  }
wenzelm@34318
   318
wenzelm@34618
   319
  override def start()
wenzelm@34618
   320
  {
wenzelm@39630
   321
    Isabelle.setup_tooltips()
wenzelm@34615
   322
    Isabelle.system = new Isabelle_System
wenzelm@34774
   323
    Isabelle.system.install_fonts()
wenzelm@39628
   324
    Isabelle.session = new Session(Isabelle.system)
wenzelm@39630
   325
    Isabelle.session.phase_changed += session_manager._2
wenzelm@34318
   326
  }
wenzelm@34618
   327
wenzelm@39628
   328
  override def stop()
wenzelm@34618
   329
  {
wenzelm@39628
   330
    Isabelle.session.stop()
wenzelm@39630
   331
    session_manager._1.join
wenzelm@39630
   332
    Isabelle.session.phase_changed -= session_manager._2
wenzelm@34318
   333
  }
wenzelm@34318
   334
}