src/Tools/jEdit/src/plugin.scala
author wenzelm
Sat Jul 02 20:54:38 2011 +0200 (2011-07-02)
changeset 43643 474745a899ea
parent 43520 cec9b95fa35d
child 43645 ac886d096c11
permissions -rw-r--r--
eliminated redundant session_ready;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/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@43520
    12
import java.lang.System
wenzelm@34429
    13
import java.io.{FileInputStream, IOException}
wenzelm@34318
    14
import java.awt.Font
wenzelm@34318
    15
wenzelm@34497
    16
import scala.collection.mutable
wenzelm@39517
    17
import scala.swing.ComboBox
immler@34406
    18
wenzelm@39241
    19
import org.gjt.sp.jedit.{jEdit, GUIUtilities, EBMessage, EBPlugin,
wenzelm@39241
    20
  Buffer, EditPane, ServiceManager, View}
immler@34406
    21
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@39043
    22
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@43452
    23
import org.gjt.sp.jedit.syntax.{Token => JEditToken, ModeProvider}
wenzelm@39633
    24
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@37068
    25
import org.gjt.sp.jedit.gui.DockableWindowManager
wenzelm@34429
    26
wenzelm@43443
    27
import org.gjt.sp.util.SyntaxUtilities
wenzelm@39241
    28
import org.gjt.sp.util.Log
wenzelm@39241
    29
wenzelm@39630
    30
import scala.actors.Actor
wenzelm@39630
    31
import Actor._
wenzelm@39630
    32
wenzelm@34318
    33
wenzelm@34618
    34
object Isabelle
wenzelm@34618
    35
{
wenzelm@34784
    36
  /* plugin instance */
wenzelm@34784
    37
wenzelm@43443
    38
  var plugin: Plugin = null
wenzelm@34784
    39
  var system: Isabelle_System = null
wenzelm@34784
    40
  var session: Session = null
wenzelm@34784
    41
wenzelm@34784
    42
wenzelm@34618
    43
  /* properties */
wenzelm@34618
    44
wenzelm@34618
    45
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    46
wenzelm@34618
    47
  object Property
wenzelm@34618
    48
  {
wenzelm@36814
    49
    def apply(name: String): String =
wenzelm@36814
    50
      jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@36814
    51
    def apply(name: String, default: String): String =
wenzelm@36814
    52
      jEdit.getProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    53
    def update(name: String, value: String) =
wenzelm@36814
    54
      jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    55
  }
wenzelm@34433
    56
wenzelm@34618
    57
  object Boolean_Property
wenzelm@34618
    58
  {
wenzelm@36814
    59
    def apply(name: String): Boolean =
wenzelm@36814
    60
      jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@36814
    61
    def apply(name: String, default: Boolean): Boolean =
wenzelm@36814
    62
      jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    63
    def update(name: String, value: Boolean) =
wenzelm@36814
    64
      jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    65
  }
wenzelm@34618
    66
wenzelm@34618
    67
  object Int_Property
wenzelm@34618
    68
  {
wenzelm@36814
    69
    def apply(name: String): Int =
wenzelm@36814
    70
      jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@36814
    71
    def apply(name: String, default: Int): Int =
wenzelm@36814
    72
      jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    73
    def update(name: String, value: Int) =
wenzelm@36814
    74
      jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    75
  }
wenzelm@34618
    76
wenzelm@40848
    77
  object Double_Property
wenzelm@40848
    78
  {
wenzelm@40848
    79
    def apply(name: String): Double =
wenzelm@40848
    80
      jEdit.getDoubleProperty(OPTION_PREFIX + name, 0.0)
wenzelm@40848
    81
    def apply(name: String, default: Double): Double =
wenzelm@40848
    82
      jEdit.getDoubleProperty(OPTION_PREFIX + name, default)
wenzelm@40848
    83
    def update(name: String, value: Double) =
wenzelm@40848
    84
      jEdit.setDoubleProperty(OPTION_PREFIX + name, value)
wenzelm@40848
    85
  }
wenzelm@40848
    86
wenzelm@40850
    87
  object Time_Property
wenzelm@40850
    88
  {
wenzelm@40850
    89
    def apply(name: String): Time =
wenzelm@40850
    90
      Time.seconds(Double_Property(name))
wenzelm@40850
    91
    def apply(name: String, default: Time): Time =
wenzelm@40850
    92
      Time.seconds(Double_Property(name, default.seconds))
wenzelm@40850
    93
    def update(name: String, value: Time) =
wenzelm@40850
    94
      Double_Property.update(name, value.seconds)
wenzelm@40850
    95
  }
wenzelm@40850
    96
wenzelm@40848
    97
wenzelm@37164
    98
  /* font */
wenzelm@37164
    99
wenzelm@37164
   100
  def font_family(): String = jEdit.getProperty("view.font")
wenzelm@37164
   101
wenzelm@37019
   102
  def font_size(): Float =
wenzelm@37019
   103
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
   104
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
   105
wenzelm@34618
   106
wenzelm@39043
   107
  /* text area ranges */
wenzelm@39043
   108
wenzelm@39043
   109
  case class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@39043
   110
wenzelm@39043
   111
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@39043
   112
  {
wenzelm@39043
   113
    val p = text_area.offsetToXY(range.start)
wenzelm@39043
   114
    val q = text_area.offsetToXY(range.stop)
wenzelm@39043
   115
    if (p != null && q != null && p.y == q.y) Some(new Gfx_Range(p.x, p.y, q.x - p.x))
wenzelm@39043
   116
    else None
wenzelm@39043
   117
  }
wenzelm@39043
   118
wenzelm@39043
   119
wenzelm@37201
   120
  /* tooltip markup */
wenzelm@37201
   121
wenzelm@37201
   122
  def tooltip(text: String): String =
wenzelm@37203
   123
    "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
wenzelm@37203
   124
        Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
wenzelm@37203
   125
      HTML.encode(text) + "</pre></html>"
wenzelm@37201
   126
wenzelm@40849
   127
  def tooltip_dismiss_delay(): Time =
wenzelm@40852
   128
    Time_Property("tooltip-dismiss-delay", Time.seconds(8.0)) max Time.seconds(0.5)
wenzelm@38854
   129
wenzelm@38854
   130
  def setup_tooltips()
wenzelm@38854
   131
  {
wenzelm@38854
   132
    Swing_Thread.now {
wenzelm@38854
   133
      val manager = javax.swing.ToolTipManager.sharedInstance
wenzelm@40849
   134
      manager.setDismissDelay(tooltip_dismiss_delay().ms.toInt)
wenzelm@38854
   135
    }
wenzelm@38854
   136
  }
wenzelm@38854
   137
wenzelm@37201
   138
wenzelm@39241
   139
  /* icons */
wenzelm@39241
   140
wenzelm@39241
   141
  def load_icon(name: String): javax.swing.Icon =
wenzelm@39241
   142
  {
wenzelm@39241
   143
    val icon = GUIUtilities.loadIcon(name)
wenzelm@39241
   144
    if (icon.getIconWidth < 0 || icon.getIconHeight < 0)
wenzelm@39630
   145
      Log.log(Log.ERROR, icon, "Bad icon: " + name)
wenzelm@39241
   146
    icon
wenzelm@39241
   147
  }
wenzelm@39241
   148
wenzelm@39241
   149
wenzelm@41382
   150
  /* check JVM */
wenzelm@41382
   151
wenzelm@41382
   152
  def check_jvm()
wenzelm@41382
   153
  {
wenzelm@41382
   154
    if (!Platform.is_hotspot) {
wenzelm@41382
   155
      Library.warning_dialog(jEdit.getActiveView, "Bad Java Virtual Machine",
wenzelm@41382
   156
        "This is " + Platform.jvm_name,
wenzelm@41382
   157
        "Isabelle/jEdit requires Java Hotspot from Sun/Oracle/Apple!")
wenzelm@41382
   158
    }
wenzelm@41382
   159
  }
wenzelm@41382
   160
wenzelm@41382
   161
wenzelm@38221
   162
  /* main jEdit components */
wenzelm@34784
   163
wenzelm@37177
   164
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   165
wenzelm@37177
   166
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   167
wenzelm@34784
   168
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   169
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   170
wenzelm@34784
   171
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   172
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   173
wenzelm@34784
   174
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   175
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   176
wenzelm@38843
   177
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38640
   178
  {
wenzelm@38640
   179
    try { buffer.readLock(); body }
wenzelm@38640
   180
    finally { buffer.readUnlock() }
wenzelm@38640
   181
  }
wenzelm@38640
   182
wenzelm@38843
   183
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38843
   184
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@38843
   185
wenzelm@40474
   186
  def buffer_text(buffer: JEditBuffer): String =
wenzelm@40474
   187
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
wenzelm@40474
   188
wenzelm@34784
   189
wenzelm@43449
   190
  /* document model and view */
wenzelm@43449
   191
wenzelm@43449
   192
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@43449
   193
  def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
   194
wenzelm@43510
   195
  def init_model(buffer: Buffer)
wenzelm@43510
   196
  {
wenzelm@43510
   197
    swing_buffer_lock(buffer) {
wenzelm@43510
   198
      val opt_model =
wenzelm@43510
   199
        document_model(buffer) match {
wenzelm@43510
   200
          case Some(model) => Some(model)
wenzelm@43510
   201
          case None =>
wenzelm@43510
   202
            // FIXME strip protocol prefix of URL
wenzelm@43510
   203
            Thy_Header.split_thy_path(system.posix_path(buffer.getPath)) match {
wenzelm@43510
   204
              case Some((dir, thy_name)) =>
wenzelm@43510
   205
                Some(Document_Model.init(session, buffer, dir + "/" + thy_name))
wenzelm@43510
   206
              case None => None
wenzelm@43510
   207
            }
wenzelm@43510
   208
        }
wenzelm@43510
   209
      if (opt_model.isDefined) {
wenzelm@43510
   210
        for (text_area <- jedit_text_areas(buffer)) {
wenzelm@43510
   211
          if (document_view(text_area).map(_.model) != opt_model)
wenzelm@43510
   212
            Document_View.init(opt_model.get, text_area)
wenzelm@43510
   213
        }
wenzelm@43510
   214
      }
wenzelm@43510
   215
    }
wenzelm@43510
   216
  }
wenzelm@43510
   217
wenzelm@43510
   218
  def exit_model(buffer: Buffer)
wenzelm@43510
   219
  {
wenzelm@43510
   220
    swing_buffer_lock(buffer) {
wenzelm@43510
   221
      jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@43510
   222
      Document_Model.exit(buffer)
wenzelm@43510
   223
    }
wenzelm@43510
   224
  }
wenzelm@43510
   225
wenzelm@43510
   226
  def init_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   227
  {
wenzelm@43510
   228
    swing_buffer_lock(buffer) {
wenzelm@43510
   229
      document_model(buffer) match {
wenzelm@43510
   230
        case Some(model) => Document_View.init(model, text_area)
wenzelm@43510
   231
        case None =>
wenzelm@43510
   232
      }
wenzelm@43510
   233
    }
wenzelm@43510
   234
  }
wenzelm@43510
   235
wenzelm@43510
   236
  def exit_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   237
  {
wenzelm@43510
   238
    swing_buffer_lock(buffer) {
wenzelm@43510
   239
      Document_View.exit(text_area)
wenzelm@43510
   240
    }
wenzelm@43510
   241
  }
wenzelm@43510
   242
wenzelm@43449
   243
wenzelm@37068
   244
  /* dockable windows */
wenzelm@37068
   245
wenzelm@37068
   246
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   247
wenzelm@39515
   248
  def docked_session(view: View): Option[Session_Dockable] =
wenzelm@39515
   249
    wm(view).getDockableWindow("isabelle-session") match {
wenzelm@39515
   250
      case dockable: Session_Dockable => Some(dockable)
wenzelm@39515
   251
      case _ => None
wenzelm@39515
   252
    }
wenzelm@39515
   253
wenzelm@37068
   254
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   255
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   256
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   257
      case _ => None
wenzelm@37068
   258
    }
wenzelm@37068
   259
wenzelm@37068
   260
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   261
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   262
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   263
      case _ => None
wenzelm@37068
   264
    }
wenzelm@37068
   265
wenzelm@37068
   266
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   267
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   268
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   269
      case _ => None
wenzelm@37068
   270
    }
wenzelm@37068
   271
wenzelm@37068
   272
wenzelm@39517
   273
  /* logic image */
wenzelm@39517
   274
wenzelm@39517
   275
  def default_logic(): String =
wenzelm@39517
   276
  {
wenzelm@39517
   277
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@39517
   278
    if (logic != "") logic
wenzelm@39517
   279
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@39517
   280
  }
wenzelm@39517
   281
wenzelm@39517
   282
  class Logic_Entry(val name: String, val description: String)
wenzelm@39517
   283
  {
wenzelm@39517
   284
    override def toString = description
wenzelm@39517
   285
  }
wenzelm@39517
   286
wenzelm@39517
   287
  def logic_selector(logic: String): ComboBox[Logic_Entry] =
wenzelm@39517
   288
  {
wenzelm@39517
   289
    val entries =
wenzelm@39517
   290
      new Logic_Entry("", "default (" + default_logic() + ")") ::
wenzelm@39517
   291
        system.find_logics().map(name => new Logic_Entry(name, name))
wenzelm@39517
   292
    val component = new ComboBox(entries)
wenzelm@39517
   293
    entries.find(_.name == logic) match {
wenzelm@39517
   294
      case None =>
wenzelm@39517
   295
      case Some(entry) => component.selection.item = entry
wenzelm@39517
   296
    }
wenzelm@39702
   297
    component.tooltip = "Isabelle logic image"
wenzelm@39517
   298
    component
wenzelm@39517
   299
  }
wenzelm@39702
   300
wenzelm@39702
   301
  def start_session()
wenzelm@39702
   302
  {
wenzelm@40852
   303
    val timeout = Time_Property("startup-timeout", Time.seconds(10)) max Time.seconds(5)
wenzelm@39702
   304
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@39702
   305
    val logic = {
wenzelm@39702
   306
      val logic = Property("logic")
wenzelm@39702
   307
      if (logic != null && logic != "") logic
wenzelm@39702
   308
      else Isabelle.default_logic()
wenzelm@39702
   309
    }
wenzelm@40850
   310
    session.start(timeout, modes ::: List(logic))
wenzelm@39702
   311
  }
wenzelm@34318
   312
}
wenzelm@34318
   313
wenzelm@34429
   314
wenzelm@34618
   315
class Plugin extends EBPlugin
wenzelm@34618
   316
{
wenzelm@39630
   317
  /* session management */
wenzelm@39630
   318
wenzelm@39633
   319
  private val session_manager = actor {
wenzelm@39633
   320
    loop {
wenzelm@39633
   321
      react {
wenzelm@39735
   322
        case phase: Session.Phase =>
wenzelm@39735
   323
          phase match {
wenzelm@39735
   324
            case Session.Failed =>
wenzelm@39735
   325
              Swing_Thread.now {
wenzelm@39735
   326
                val text = new scala.swing.TextArea(Isabelle.session.syslog())
wenzelm@39735
   327
                text.editable = false
wenzelm@39735
   328
                Library.error_dialog(jEdit.getActiveView, "Failed to start Isabelle process", text)
wenzelm@39735
   329
              }
wenzelm@39735
   330
wenzelm@43510
   331
            case Session.Ready => Isabelle.jedit_buffers.foreach(Isabelle.init_model)
wenzelm@43510
   332
            case Session.Shutdown => Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@39735
   333
            case _ =>
wenzelm@39735
   334
          }
wenzelm@39735
   335
        case bad => System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   336
      }
wenzelm@39630
   337
    }
wenzelm@39630
   338
  }
wenzelm@39630
   339
wenzelm@39630
   340
wenzelm@34618
   341
  /* main plugin plumbing */
wenzelm@34433
   342
wenzelm@34767
   343
  override def handleMessage(message: EBMessage)
wenzelm@34618
   344
  {
wenzelm@43510
   345
    Swing_Thread.assert()
wenzelm@34767
   346
    message match {
wenzelm@41383
   347
      case msg: EditorStarted =>
wenzelm@43510
   348
        Isabelle.check_jvm()
wenzelm@43510
   349
        if (Isabelle.Boolean_Property("auto-start"))
wenzelm@43510
   350
          Isabelle.start_session()
wenzelm@39630
   351
wenzelm@37557
   352
      case msg: BufferUpdate
wenzelm@39735
   353
      if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@39634
   354
wenzelm@39633
   355
        val buffer = msg.getBuffer
wenzelm@43643
   356
        if (buffer != null && Isabelle.session.is_ready)
wenzelm@43510
   357
          Isabelle.init_model(buffer)
wenzelm@37557
   358
wenzelm@39634
   359
      case msg: EditPaneUpdate
wenzelm@39735
   360
      if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@39637
   361
          msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@39634
   362
          msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@39634
   363
          msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@39634
   364
wenzelm@34784
   365
        val edit_pane = msg.getEditPane
wenzelm@34784
   366
        val buffer = edit_pane.getBuffer
wenzelm@34784
   367
        val text_area = edit_pane.getTextArea
wenzelm@34784
   368
wenzelm@39637
   369
        if (buffer != null && text_area != null) {
wenzelm@39735
   370
          if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@43510
   371
              msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@43643
   372
            if (Isabelle.session.is_ready)
wenzelm@43510
   373
              Isabelle.init_view(buffer, text_area)
wenzelm@43510
   374
          }
wenzelm@43510
   375
          else Isabelle.exit_view(buffer, text_area)
immler@34671
   376
        }
wenzelm@34784
   377
wenzelm@34777
   378
      case msg: PropertiesChanged =>
wenzelm@43390
   379
        Swing_Thread.now { Isabelle.setup_tooltips() }
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@43443
   388
    Isabelle.plugin = this
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@43487
   393
    SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender(Isabelle.system.symbols))
wenzelm@43513
   394
    if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@43513
   395
      ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@39633
   396
    Isabelle.session.phase_changed += session_manager
wenzelm@34318
   397
  }
wenzelm@34618
   398
wenzelm@39628
   399
  override def stop()
wenzelm@34618
   400
  {
wenzelm@43513
   401
    Isabelle.session.phase_changed -= session_manager
wenzelm@43513
   402
    Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@39628
   403
    Isabelle.session.stop()
wenzelm@34318
   404
  }
wenzelm@34318
   405
}