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