src/Tools/jEdit/src/plugin.scala
author wenzelm
Fri Mar 02 21:22:42 2012 +0100 (2012-03-02)
changeset 46761 b0a797158e34
parent 46740 852baa599351
child 46771 06a9b24c4a36
permissions -rw-r--r--
avoid buffer loading overrun;
tuned;
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@34318
    13
import java.awt.Font
wenzelm@44573
    14
import javax.swing.JOptionPane
wenzelm@34318
    15
wenzelm@34497
    16
import scala.collection.mutable
wenzelm@44606
    17
import scala.swing.{ComboBox, ListView, ScrollPane}
immler@34406
    18
wenzelm@39241
    19
import org.gjt.sp.jedit.{jEdit, GUIUtilities, EBMessage, EBPlugin,
wenzelm@44577
    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@44721
    30
import scala.actors.Actor._
wenzelm@39630
    31
wenzelm@34318
    32
wenzelm@34618
    33
object Isabelle
wenzelm@34618
    34
{
wenzelm@34784
    35
  /* plugin instance */
wenzelm@34784
    36
wenzelm@43443
    37
  var plugin: Plugin = null
wenzelm@34784
    38
  var session: Session = null
wenzelm@34784
    39
wenzelm@44955
    40
  val thy_load = new JEdit_Thy_Load
wenzelm@44955
    41
  val thy_info = new Thy_Info(thy_load)
wenzelm@44955
    42
wenzelm@34784
    43
wenzelm@34618
    44
  /* properties */
wenzelm@34618
    45
wenzelm@34618
    46
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    47
wenzelm@34618
    48
  object Property
wenzelm@34618
    49
  {
wenzelm@36814
    50
    def apply(name: String): String =
wenzelm@36814
    51
      jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@36814
    52
    def apply(name: String, default: String): String =
wenzelm@36814
    53
      jEdit.getProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    54
    def update(name: String, value: String) =
wenzelm@36814
    55
      jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    56
  }
wenzelm@34433
    57
wenzelm@34618
    58
  object Boolean_Property
wenzelm@34618
    59
  {
wenzelm@36814
    60
    def apply(name: String): Boolean =
wenzelm@36814
    61
      jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@36814
    62
    def apply(name: String, default: Boolean): Boolean =
wenzelm@36814
    63
      jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    64
    def update(name: String, value: Boolean) =
wenzelm@36814
    65
      jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    66
  }
wenzelm@34618
    67
wenzelm@34618
    68
  object Int_Property
wenzelm@34618
    69
  {
wenzelm@36814
    70
    def apply(name: String): Int =
wenzelm@36814
    71
      jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@36814
    72
    def apply(name: String, default: Int): Int =
wenzelm@36814
    73
      jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    74
    def update(name: String, value: Int) =
wenzelm@36814
    75
      jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    76
  }
wenzelm@34618
    77
wenzelm@40848
    78
  object Double_Property
wenzelm@40848
    79
  {
wenzelm@40848
    80
    def apply(name: String): Double =
wenzelm@40848
    81
      jEdit.getDoubleProperty(OPTION_PREFIX + name, 0.0)
wenzelm@40848
    82
    def apply(name: String, default: Double): Double =
wenzelm@40848
    83
      jEdit.getDoubleProperty(OPTION_PREFIX + name, default)
wenzelm@40848
    84
    def update(name: String, value: Double) =
wenzelm@40848
    85
      jEdit.setDoubleProperty(OPTION_PREFIX + name, value)
wenzelm@40848
    86
  }
wenzelm@40848
    87
wenzelm@40850
    88
  object Time_Property
wenzelm@40850
    89
  {
wenzelm@40850
    90
    def apply(name: String): Time =
wenzelm@40850
    91
      Time.seconds(Double_Property(name))
wenzelm@40850
    92
    def apply(name: String, default: Time): Time =
wenzelm@40850
    93
      Time.seconds(Double_Property(name, default.seconds))
wenzelm@40850
    94
    def update(name: String, value: Time) =
wenzelm@40850
    95
      Double_Property.update(name, value.seconds)
wenzelm@40850
    96
  }
wenzelm@40850
    97
wenzelm@40848
    98
wenzelm@37164
    99
  /* font */
wenzelm@37164
   100
wenzelm@37164
   101
  def font_family(): String = jEdit.getProperty("view.font")
wenzelm@37164
   102
wenzelm@37019
   103
  def font_size(): Float =
wenzelm@37019
   104
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
   105
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
   106
wenzelm@34618
   107
wenzelm@37201
   108
  /* tooltip markup */
wenzelm@37201
   109
wenzelm@37201
   110
  def tooltip(text: String): String =
wenzelm@37203
   111
    "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
wenzelm@37203
   112
        Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
wenzelm@37203
   113
      HTML.encode(text) + "</pre></html>"
wenzelm@37201
   114
wenzelm@44699
   115
  private val tooltip_lb = Time.seconds(0.5)
wenzelm@44699
   116
  private val tooltip_ub = Time.seconds(60.0)
wenzelm@40849
   117
  def tooltip_dismiss_delay(): Time =
wenzelm@44699
   118
    Time_Property("tooltip-dismiss-delay", Time.seconds(8.0)) max tooltip_lb min tooltip_ub
wenzelm@38854
   119
wenzelm@38854
   120
  def setup_tooltips()
wenzelm@38854
   121
  {
wenzelm@38854
   122
    Swing_Thread.now {
wenzelm@38854
   123
      val manager = javax.swing.ToolTipManager.sharedInstance
wenzelm@40849
   124
      manager.setDismissDelay(tooltip_dismiss_delay().ms.toInt)
wenzelm@38854
   125
    }
wenzelm@38854
   126
  }
wenzelm@38854
   127
wenzelm@37201
   128
wenzelm@39241
   129
  /* icons */
wenzelm@39241
   130
wenzelm@39241
   131
  def load_icon(name: String): javax.swing.Icon =
wenzelm@39241
   132
  {
wenzelm@39241
   133
    val icon = GUIUtilities.loadIcon(name)
wenzelm@39241
   134
    if (icon.getIconWidth < 0 || icon.getIconHeight < 0)
wenzelm@39630
   135
      Log.log(Log.ERROR, icon, "Bad icon: " + name)
wenzelm@39241
   136
    icon
wenzelm@39241
   137
  }
wenzelm@39241
   138
wenzelm@39241
   139
wenzelm@44580
   140
  /* buffers */
wenzelm@44580
   141
wenzelm@44580
   142
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@44580
   143
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@44580
   144
wenzelm@44580
   145
  def buffer_text(buffer: JEditBuffer): String =
wenzelm@44580
   146
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
wenzelm@44580
   147
wenzelm@44580
   148
  def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
wenzelm@44580
   149
wenzelm@44580
   150
wenzelm@38221
   151
  /* main jEdit components */
wenzelm@34784
   152
wenzelm@37177
   153
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   154
wenzelm@44580
   155
  def jedit_buffer(name: String): Option[Buffer] =
wenzelm@44580
   156
    jedit_buffers().find(buffer => buffer_name(buffer) == name)
wenzelm@44580
   157
wenzelm@37177
   158
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   159
wenzelm@34784
   160
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   161
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   162
wenzelm@34784
   163
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   164
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   165
wenzelm@34784
   166
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   167
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   168
wenzelm@38843
   169
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38640
   170
  {
wenzelm@38640
   171
    try { buffer.readLock(); body }
wenzelm@38640
   172
    finally { buffer.readUnlock() }
wenzelm@38640
   173
  }
wenzelm@38640
   174
wenzelm@34784
   175
wenzelm@43449
   176
  /* document model and view */
wenzelm@43449
   177
wenzelm@43449
   178
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@43449
   179
  def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
   180
wenzelm@44379
   181
  def document_views(buffer: Buffer): List[Document_View] =
wenzelm@44379
   182
    for {
wenzelm@44379
   183
      text_area <- jedit_text_areas(buffer).toList
wenzelm@44379
   184
      val doc_view = document_view(text_area)
wenzelm@44379
   185
      if doc_view.isDefined
wenzelm@44379
   186
    } yield doc_view.get
wenzelm@44379
   187
wenzelm@43510
   188
  def init_model(buffer: Buffer)
wenzelm@43510
   189
  {
wenzelm@43510
   190
    swing_buffer_lock(buffer) {
wenzelm@43510
   191
      val opt_model =
wenzelm@43510
   192
        document_model(buffer) match {
wenzelm@43510
   193
          case Some(model) => Some(model)
wenzelm@43510
   194
          case None =>
wenzelm@44615
   195
            val name = buffer_name(buffer)
wenzelm@44615
   196
            Thy_Header.thy_name(name) match {
wenzelm@44615
   197
              case Some(theory) =>
wenzelm@44615
   198
                val node_name = Document.Node.Name(name, buffer.getDirectory, theory)
wenzelm@44615
   199
                Some(Document_Model.init(session, buffer, node_name))
wenzelm@44160
   200
              case None => None
wenzelm@43510
   201
            }
wenzelm@43510
   202
        }
wenzelm@43510
   203
      if (opt_model.isDefined) {
wenzelm@43510
   204
        for (text_area <- jedit_text_areas(buffer)) {
wenzelm@43510
   205
          if (document_view(text_area).map(_.model) != opt_model)
wenzelm@43510
   206
            Document_View.init(opt_model.get, text_area)
wenzelm@43510
   207
        }
wenzelm@43510
   208
      }
wenzelm@43510
   209
    }
wenzelm@43510
   210
  }
wenzelm@43510
   211
wenzelm@43510
   212
  def exit_model(buffer: Buffer)
wenzelm@43510
   213
  {
wenzelm@43510
   214
    swing_buffer_lock(buffer) {
wenzelm@43510
   215
      jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@43510
   216
      Document_Model.exit(buffer)
wenzelm@43510
   217
    }
wenzelm@43510
   218
  }
wenzelm@43510
   219
wenzelm@43510
   220
  def init_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   221
  {
wenzelm@43510
   222
    swing_buffer_lock(buffer) {
wenzelm@43510
   223
      document_model(buffer) match {
wenzelm@43510
   224
        case Some(model) => Document_View.init(model, text_area)
wenzelm@43510
   225
        case None =>
wenzelm@43510
   226
      }
wenzelm@43510
   227
    }
wenzelm@43510
   228
  }
wenzelm@43510
   229
wenzelm@43510
   230
  def exit_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   231
  {
wenzelm@43510
   232
    swing_buffer_lock(buffer) {
wenzelm@43510
   233
      Document_View.exit(text_area)
wenzelm@43510
   234
    }
wenzelm@43510
   235
  }
wenzelm@43510
   236
wenzelm@43449
   237
wenzelm@37068
   238
  /* dockable windows */
wenzelm@37068
   239
wenzelm@37068
   240
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   241
wenzelm@39515
   242
  def docked_session(view: View): Option[Session_Dockable] =
wenzelm@39515
   243
    wm(view).getDockableWindow("isabelle-session") match {
wenzelm@39515
   244
      case dockable: Session_Dockable => Some(dockable)
wenzelm@39515
   245
      case _ => None
wenzelm@39515
   246
    }
wenzelm@39515
   247
wenzelm@37068
   248
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   249
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   250
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   251
      case _ => None
wenzelm@37068
   252
    }
wenzelm@37068
   253
wenzelm@37068
   254
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   255
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   256
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   257
      case _ => None
wenzelm@37068
   258
    }
wenzelm@37068
   259
wenzelm@37068
   260
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   261
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   262
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   263
      case _ => None
wenzelm@37068
   264
    }
wenzelm@37068
   265
wenzelm@37068
   266
wenzelm@39517
   267
  /* logic image */
wenzelm@39517
   268
wenzelm@39517
   269
  def default_logic(): String =
wenzelm@39517
   270
  {
wenzelm@43661
   271
    val logic = Isabelle_System.getenv("JEDIT_LOGIC")
wenzelm@39517
   272
    if (logic != "") logic
wenzelm@43661
   273
    else Isabelle_System.getenv_strict("ISABELLE_LOGIC")
wenzelm@39517
   274
  }
wenzelm@39517
   275
wenzelm@39517
   276
  class Logic_Entry(val name: String, val description: String)
wenzelm@39517
   277
  {
wenzelm@39517
   278
    override def toString = description
wenzelm@39517
   279
  }
wenzelm@39517
   280
wenzelm@39517
   281
  def logic_selector(logic: String): ComboBox[Logic_Entry] =
wenzelm@39517
   282
  {
wenzelm@39517
   283
    val entries =
wenzelm@39517
   284
      new Logic_Entry("", "default (" + default_logic() + ")") ::
wenzelm@43661
   285
        Isabelle_System.find_logics().map(name => new Logic_Entry(name, name))
wenzelm@39517
   286
    val component = new ComboBox(entries)
wenzelm@39517
   287
    entries.find(_.name == logic) match {
wenzelm@39517
   288
      case None =>
wenzelm@39517
   289
      case Some(entry) => component.selection.item = entry
wenzelm@39517
   290
    }
wenzelm@39702
   291
    component.tooltip = "Isabelle logic image"
wenzelm@39517
   292
    component
wenzelm@39517
   293
  }
wenzelm@39702
   294
wenzelm@39702
   295
  def start_session()
wenzelm@39702
   296
  {
wenzelm@45075
   297
    val timeout = Time_Property("startup-timeout", Time.seconds(25)) max Time.seconds(5)
wenzelm@43670
   298
    val modes = space_explode(',', Isabelle_System.getenv("JEDIT_PRINT_MODE")).map("-m" + _)
wenzelm@39702
   299
    val logic = {
wenzelm@39702
   300
      val logic = Property("logic")
wenzelm@39702
   301
      if (logic != null && logic != "") logic
wenzelm@39702
   302
      else Isabelle.default_logic()
wenzelm@39702
   303
    }
wenzelm@45158
   304
    session.start(timeout, modes ::: List(logic))
wenzelm@39702
   305
  }
wenzelm@44238
   306
wenzelm@44238
   307
wenzelm@44238
   308
  /* convenience actions */
wenzelm@44238
   309
wenzelm@44238
   310
  private def user_input(text_area: JEditTextArea, s1: String, s2: String = "")
wenzelm@44238
   311
  {
wenzelm@44238
   312
    s1.foreach(text_area.userInput(_))
wenzelm@44238
   313
    s2.foreach(text_area.userInput(_))
wenzelm@44238
   314
    s2.foreach(_ => text_area.goToPrevCharacter(false))
wenzelm@44238
   315
  }
wenzelm@44238
   316
wenzelm@44238
   317
  def input_sub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.sub_decoded)
wenzelm@44238
   318
  def input_sup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.sup_decoded)
wenzelm@44238
   319
  def input_isub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.isub_decoded)
wenzelm@44238
   320
  def input_isup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.isup_decoded)
wenzelm@44238
   321
  def input_bsub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bsub_decoded, Symbol.esub_decoded)
wenzelm@44238
   322
  def input_bsup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bsup_decoded, Symbol.esup_decoded)
wenzelm@44238
   323
  def input_bold(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bold_decoded)
wenzelm@44864
   324
wenzelm@44864
   325
  def check_buffer(buffer: Buffer)
wenzelm@44864
   326
  {
wenzelm@44864
   327
    document_model(buffer) match {
wenzelm@44864
   328
      case None =>
wenzelm@44864
   329
      case Some(model) => model.full_perspective()
wenzelm@44864
   330
    }
wenzelm@44864
   331
  }
wenzelm@44864
   332
wenzelm@44864
   333
  def cancel_execution() { session.cancel_execution() }
wenzelm@34318
   334
}
wenzelm@34318
   335
wenzelm@34429
   336
wenzelm@34618
   337
class Plugin extends EBPlugin
wenzelm@34618
   338
{
wenzelm@44577
   339
  /* theory files */
wenzelm@44574
   340
wenzelm@44573
   341
  private lazy val delay_load =
wenzelm@44574
   342
    Swing_Thread.delay_last(Isabelle.session.load_delay)
wenzelm@44574
   343
    {
wenzelm@44963
   344
      val view = jEdit.getActiveView()
wenzelm@44963
   345
wenzelm@44574
   346
      val buffers = Isabelle.jedit_buffers().toList
wenzelm@46761
   347
      if (buffers.forall(_.isLoaded)) {
wenzelm@46761
   348
        def loaded_buffer(name: String): Boolean =
wenzelm@46761
   349
          buffers.exists(buffer => Isabelle.buffer_name(buffer) == name)
wenzelm@44574
   350
wenzelm@46761
   351
        val thys =
wenzelm@46761
   352
          for (buffer <- buffers; model <- Isabelle.document_model(buffer))
wenzelm@46761
   353
            yield model.name
wenzelm@46761
   354
wenzelm@46761
   355
        // FIXME avoid I/O in Swing thread!?!
wenzelm@46761
   356
        val files = Isabelle.thy_info.dependencies(thys).map(_._1.node).
wenzelm@46761
   357
          filter(file => !loaded_buffer(file) && Isabelle.thy_load.check_file(view, file))
wenzelm@44574
   358
wenzelm@46761
   359
        if (!files.isEmpty) {
wenzelm@46761
   360
          val files_list = new ListView(files.sorted)
wenzelm@46761
   361
          for (i <- 0 until files.length)
wenzelm@46761
   362
            files_list.selection.indices += i
wenzelm@44606
   363
wenzelm@46761
   364
          val answer =
wenzelm@46761
   365
            Library.confirm_dialog(view,
wenzelm@46761
   366
              "Auto loading of required files",
wenzelm@46761
   367
              JOptionPane.YES_NO_OPTION,
wenzelm@46761
   368
              "The following files are required to resolve theory imports.",
wenzelm@46761
   369
              "Reload selected files now?",
wenzelm@46761
   370
              new ScrollPane(files_list))
wenzelm@46761
   371
          if (answer == 0) {
wenzelm@46761
   372
            files.foreach(file =>
wenzelm@46761
   373
              if (files_list.selection.items.contains(file))
wenzelm@46761
   374
                jEdit.openFile(null: View, file))
wenzelm@46761
   375
          }
wenzelm@46761
   376
        }
wenzelm@44606
   377
      }
wenzelm@44574
   378
    }
wenzelm@44573
   379
wenzelm@44574
   380
wenzelm@44574
   381
  /* session manager */
wenzelm@44573
   382
wenzelm@39633
   383
  private val session_manager = actor {
wenzelm@39633
   384
    loop {
wenzelm@39633
   385
      react {
wenzelm@39735
   386
        case phase: Session.Phase =>
wenzelm@39735
   387
          phase match {
wenzelm@39735
   388
            case Session.Failed =>
wenzelm@39735
   389
              Swing_Thread.now {
wenzelm@39735
   390
                val text = new scala.swing.TextArea(Isabelle.session.syslog())
wenzelm@39735
   391
                text.editable = false
wenzelm@39735
   392
                Library.error_dialog(jEdit.getActiveView, "Failed to start Isabelle process", text)
wenzelm@39735
   393
              }
wenzelm@39735
   394
wenzelm@44573
   395
            case Session.Ready =>
wenzelm@44573
   396
              Isabelle.jedit_buffers.foreach(Isabelle.init_model)
wenzelm@46740
   397
              delay_load(true)
wenzelm@44573
   398
wenzelm@46740
   399
            case Session.Shutdown =>
wenzelm@46740
   400
              Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@46740
   401
              delay_load(false)
wenzelm@46740
   402
wenzelm@39735
   403
            case _ =>
wenzelm@39735
   404
          }
wenzelm@39735
   405
        case bad => System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   406
      }
wenzelm@39630
   407
    }
wenzelm@39630
   408
  }
wenzelm@39630
   409
wenzelm@39630
   410
wenzelm@34618
   411
  /* main plugin plumbing */
wenzelm@34433
   412
wenzelm@34767
   413
  override def handleMessage(message: EBMessage)
wenzelm@34618
   414
  {
wenzelm@43510
   415
    Swing_Thread.assert()
wenzelm@34767
   416
    message match {
wenzelm@41383
   417
      case msg: EditorStarted =>
wenzelm@43510
   418
        if (Isabelle.Boolean_Property("auto-start"))
wenzelm@43510
   419
          Isabelle.start_session()
wenzelm@39630
   420
wenzelm@37557
   421
      case msg: BufferUpdate
wenzelm@44434
   422
      if msg.getWhat == BufferUpdate.LOADED =>
wenzelm@44573
   423
        if (Isabelle.session.is_ready) {
wenzelm@44573
   424
          val buffer = msg.getBuffer
wenzelm@44573
   425
          if (buffer != null) Isabelle.init_model(buffer)
wenzelm@46740
   426
          delay_load(true)
wenzelm@44573
   427
        }
wenzelm@37557
   428
wenzelm@39634
   429
      case msg: EditPaneUpdate
wenzelm@39735
   430
      if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@39637
   431
          msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@39634
   432
          msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@39634
   433
          msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@34784
   434
        val edit_pane = msg.getEditPane
wenzelm@34784
   435
        val buffer = edit_pane.getBuffer
wenzelm@34784
   436
        val text_area = edit_pane.getTextArea
wenzelm@34784
   437
wenzelm@39637
   438
        if (buffer != null && text_area != null) {
wenzelm@39735
   439
          if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@43510
   440
              msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@43643
   441
            if (Isabelle.session.is_ready)
wenzelm@43510
   442
              Isabelle.init_view(buffer, text_area)
wenzelm@43510
   443
          }
wenzelm@43510
   444
          else Isabelle.exit_view(buffer, text_area)
immler@34671
   445
        }
wenzelm@34784
   446
wenzelm@34777
   447
      case msg: PropertiesChanged =>
wenzelm@43390
   448
        Swing_Thread.now { Isabelle.setup_tooltips() }
wenzelm@34791
   449
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   450
wenzelm@34318
   451
      case _ =>
wenzelm@34318
   452
    }
wenzelm@34318
   453
  }
wenzelm@34318
   454
wenzelm@34618
   455
  override def start()
wenzelm@34618
   456
  {
wenzelm@43443
   457
    Isabelle.plugin = this
wenzelm@39630
   458
    Isabelle.setup_tooltips()
wenzelm@43661
   459
    Isabelle_System.init()
wenzelm@43661
   460
    Isabelle_System.install_fonts()
wenzelm@44955
   461
    Isabelle.session = new Session(Isabelle.thy_load)
wenzelm@43661
   462
    SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@43513
   463
    if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@43513
   464
      ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@39633
   465
    Isabelle.session.phase_changed += session_manager
wenzelm@34318
   466
  }
wenzelm@34618
   467
wenzelm@39628
   468
  override def stop()
wenzelm@34618
   469
  {
wenzelm@43513
   470
    Isabelle.session.phase_changed -= session_manager
wenzelm@43513
   471
    Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@39628
   472
    Isabelle.session.stop()
wenzelm@34318
   473
  }
wenzelm@34318
   474
}