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