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