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