src/Tools/jEdit/src/plugin.scala
author wenzelm
Tue Aug 21 12:15:25 2012 +0200 (2012-08-21)
changeset 48870 4accee106f0f
parent 48791 9e8f30bfbdca
child 48872 6124e0d1120a
permissions -rw-r--r--
clarified initialization of Thy_Load, Thy_Info, Session;
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@48870
    40
  def thy_load(): JEdit_Thy_Load =
wenzelm@48870
    41
    session.thy_load.asInstanceOf[JEdit_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@48718
   150
  def buffer_node_dummy(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@48718
   151
    Some(Document.Node.Name(buffer_name(buffer), buffer.getDirectory, buffer.getName))
wenzelm@48718
   152
wenzelm@48718
   153
  def buffer_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@48718
   154
  {
wenzelm@48718
   155
    val name = buffer_name(buffer)
wenzelm@48718
   156
    Thy_Header.thy_name(name).map(theory => Document.Node.Name(name, buffer.getDirectory, theory))
wenzelm@48718
   157
  }
wenzelm@48718
   158
wenzelm@44580
   159
wenzelm@38221
   160
  /* main jEdit components */
wenzelm@34784
   161
wenzelm@37177
   162
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   163
wenzelm@44580
   164
  def jedit_buffer(name: String): Option[Buffer] =
wenzelm@44580
   165
    jedit_buffers().find(buffer => buffer_name(buffer) == name)
wenzelm@44580
   166
wenzelm@37177
   167
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   168
wenzelm@34784
   169
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   170
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   171
wenzelm@34784
   172
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   173
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   174
wenzelm@34784
   175
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   176
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   177
wenzelm@38843
   178
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38640
   179
  {
wenzelm@38640
   180
    try { buffer.readLock(); body }
wenzelm@38640
   181
    finally { buffer.readUnlock() }
wenzelm@38640
   182
  }
wenzelm@38640
   183
wenzelm@34784
   184
wenzelm@43449
   185
  /* document model and view */
wenzelm@43449
   186
wenzelm@43449
   187
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@43449
   188
  def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
   189
wenzelm@44379
   190
  def document_views(buffer: Buffer): List[Document_View] =
wenzelm@44379
   191
    for {
wenzelm@44379
   192
      text_area <- jedit_text_areas(buffer).toList
wenzelm@46997
   193
      doc_view = document_view(text_area)
wenzelm@44379
   194
      if doc_view.isDefined
wenzelm@44379
   195
    } yield doc_view.get
wenzelm@44379
   196
wenzelm@47058
   197
  def exit_model(buffer: Buffer)
wenzelm@47058
   198
  {
wenzelm@47058
   199
    swing_buffer_lock(buffer) {
wenzelm@47058
   200
      jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@47058
   201
      Document_Model.exit(buffer)
wenzelm@47058
   202
    }
wenzelm@47058
   203
  }
wenzelm@47058
   204
wenzelm@43510
   205
  def init_model(buffer: Buffer)
wenzelm@43510
   206
  {
wenzelm@43510
   207
    swing_buffer_lock(buffer) {
wenzelm@43510
   208
      val opt_model =
wenzelm@48717
   209
        buffer_node_name(buffer) match {
wenzelm@48717
   210
          case Some(node_name) =>
wenzelm@47058
   211
            document_model(buffer) match {
wenzelm@47058
   212
              case Some(model) if model.name == node_name => Some(model)
wenzelm@47058
   213
              case _ => Some(Document_Model.init(session, buffer, node_name))
wenzelm@43510
   214
            }
wenzelm@47058
   215
          case None => None
wenzelm@43510
   216
        }
wenzelm@43510
   217
      if (opt_model.isDefined) {
wenzelm@43510
   218
        for (text_area <- jedit_text_areas(buffer)) {
wenzelm@43510
   219
          if (document_view(text_area).map(_.model) != opt_model)
wenzelm@43510
   220
            Document_View.init(opt_model.get, text_area)
wenzelm@43510
   221
        }
wenzelm@43510
   222
      }
wenzelm@43510
   223
    }
wenzelm@43510
   224
  }
wenzelm@43510
   225
wenzelm@43510
   226
  def init_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   227
  {
wenzelm@43510
   228
    swing_buffer_lock(buffer) {
wenzelm@43510
   229
      document_model(buffer) match {
wenzelm@43510
   230
        case Some(model) => Document_View.init(model, text_area)
wenzelm@43510
   231
        case None =>
wenzelm@43510
   232
      }
wenzelm@43510
   233
    }
wenzelm@43510
   234
  }
wenzelm@43510
   235
wenzelm@43510
   236
  def exit_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   237
  {
wenzelm@43510
   238
    swing_buffer_lock(buffer) {
wenzelm@43510
   239
      Document_View.exit(text_area)
wenzelm@43510
   240
    }
wenzelm@43510
   241
  }
wenzelm@43510
   242
wenzelm@43449
   243
wenzelm@37068
   244
  /* dockable windows */
wenzelm@37068
   245
wenzelm@37068
   246
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   247
wenzelm@39515
   248
  def docked_session(view: View): Option[Session_Dockable] =
wenzelm@39515
   249
    wm(view).getDockableWindow("isabelle-session") match {
wenzelm@39515
   250
      case dockable: Session_Dockable => Some(dockable)
wenzelm@39515
   251
      case _ => None
wenzelm@39515
   252
    }
wenzelm@39515
   253
wenzelm@37068
   254
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   255
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   256
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   257
      case _ => None
wenzelm@37068
   258
    }
wenzelm@37068
   259
wenzelm@37068
   260
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   261
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   262
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   263
      case _ => None
wenzelm@37068
   264
    }
wenzelm@37068
   265
wenzelm@37068
   266
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   267
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   268
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   269
      case _ => None
wenzelm@37068
   270
    }
wenzelm@37068
   271
wenzelm@37068
   272
wenzelm@39517
   273
  /* logic image */
wenzelm@39517
   274
wenzelm@39517
   275
  def default_logic(): String =
wenzelm@39517
   276
  {
wenzelm@43661
   277
    val logic = Isabelle_System.getenv("JEDIT_LOGIC")
wenzelm@39517
   278
    if (logic != "") logic
wenzelm@43661
   279
    else Isabelle_System.getenv_strict("ISABELLE_LOGIC")
wenzelm@39517
   280
  }
wenzelm@39517
   281
wenzelm@39517
   282
  class Logic_Entry(val name: String, val description: String)
wenzelm@39517
   283
  {
wenzelm@39517
   284
    override def toString = description
wenzelm@39517
   285
  }
wenzelm@39517
   286
wenzelm@39517
   287
  def logic_selector(logic: String): ComboBox[Logic_Entry] =
wenzelm@39517
   288
  {
wenzelm@39517
   289
    val entries =
wenzelm@39517
   290
      new Logic_Entry("", "default (" + default_logic() + ")") ::
wenzelm@43661
   291
        Isabelle_System.find_logics().map(name => new Logic_Entry(name, name))
wenzelm@39517
   292
    val component = new ComboBox(entries)
wenzelm@39517
   293
    entries.find(_.name == logic) match {
wenzelm@39517
   294
      case None =>
wenzelm@39517
   295
      case Some(entry) => component.selection.item = entry
wenzelm@39517
   296
    }
wenzelm@39702
   297
    component.tooltip = "Isabelle logic image"
wenzelm@39517
   298
    component
wenzelm@39517
   299
  }
wenzelm@39702
   300
wenzelm@48870
   301
  def session_args(): List[String] =
wenzelm@39702
   302
  {
wenzelm@43670
   303
    val modes = space_explode(',', Isabelle_System.getenv("JEDIT_PRINT_MODE")).map("-m" + _)
wenzelm@39702
   304
    val logic = {
wenzelm@39702
   305
      val logic = Property("logic")
wenzelm@39702
   306
      if (logic != null && logic != "") logic
wenzelm@39702
   307
      else Isabelle.default_logic()
wenzelm@39702
   308
    }
wenzelm@48870
   309
    modes ::: List(logic)
wenzelm@48870
   310
  }
wenzelm@48870
   311
wenzelm@48870
   312
  def session_content(): Build.Session_Content =
wenzelm@48870
   313
  {
wenzelm@48870
   314
    val dirs = Path.split(Isabelle_System.getenv("JEDIT_SESSION_DIRS"))
wenzelm@48870
   315
    val name = Path.explode(session_args().last).base.implode  // FIXME more robust
wenzelm@48870
   316
    Build.session_content(dirs, name).check_errors
wenzelm@39702
   317
  }
wenzelm@44238
   318
wenzelm@44238
   319
wenzelm@44238
   320
  /* convenience actions */
wenzelm@44238
   321
wenzelm@44238
   322
  private def user_input(text_area: JEditTextArea, s1: String, s2: String = "")
wenzelm@44238
   323
  {
wenzelm@44238
   324
    s1.foreach(text_area.userInput(_))
wenzelm@44238
   325
    s2.foreach(text_area.userInput(_))
wenzelm@44238
   326
    s2.foreach(_ => text_area.goToPrevCharacter(false))
wenzelm@44238
   327
  }
wenzelm@44238
   328
wenzelm@44238
   329
  def input_sub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.sub_decoded)
wenzelm@44238
   330
  def input_sup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.sup_decoded)
wenzelm@44238
   331
  def input_isub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.isub_decoded)
wenzelm@44238
   332
  def input_isup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.isup_decoded)
wenzelm@44238
   333
  def input_bsub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bsub_decoded, Symbol.esub_decoded)
wenzelm@44238
   334
  def input_bsup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bsup_decoded, Symbol.esup_decoded)
wenzelm@44238
   335
  def input_bold(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bold_decoded)
wenzelm@44864
   336
wenzelm@44864
   337
  def check_buffer(buffer: Buffer)
wenzelm@44864
   338
  {
wenzelm@44864
   339
    document_model(buffer) match {
wenzelm@44864
   340
      case None =>
wenzelm@44864
   341
      case Some(model) => model.full_perspective()
wenzelm@44864
   342
    }
wenzelm@44864
   343
  }
wenzelm@44864
   344
wenzelm@44864
   345
  def cancel_execution() { session.cancel_execution() }
wenzelm@34318
   346
}
wenzelm@34318
   347
wenzelm@34429
   348
wenzelm@34618
   349
class Plugin extends EBPlugin
wenzelm@34618
   350
{
wenzelm@44577
   351
  /* theory files */
wenzelm@44574
   352
wenzelm@44573
   353
  private lazy val delay_load =
wenzelm@44574
   354
    Swing_Thread.delay_last(Isabelle.session.load_delay)
wenzelm@44574
   355
    {
wenzelm@44963
   356
      val view = jEdit.getActiveView()
wenzelm@44963
   357
wenzelm@44574
   358
      val buffers = Isabelle.jedit_buffers().toList
wenzelm@46761
   359
      if (buffers.forall(_.isLoaded)) {
wenzelm@46761
   360
        def loaded_buffer(name: String): Boolean =
wenzelm@46761
   361
          buffers.exists(buffer => Isabelle.buffer_name(buffer) == name)
wenzelm@44574
   362
wenzelm@46761
   363
        val thys =
wenzelm@46761
   364
          for (buffer <- buffers; model <- Isabelle.document_model(buffer))
wenzelm@46761
   365
            yield model.name
wenzelm@46761
   366
wenzelm@48870
   367
        val thy_info = new Thy_Info(Isabelle.thy_load)
wenzelm@46761
   368
        // FIXME avoid I/O in Swing thread!?!
wenzelm@48870
   369
        val files = thy_info.dependencies(thys).map(_._1.node).
wenzelm@46761
   370
          filter(file => !loaded_buffer(file) && Isabelle.thy_load.check_file(view, file))
wenzelm@44574
   371
wenzelm@46761
   372
        if (!files.isEmpty) {
wenzelm@46761
   373
          val files_list = new ListView(files.sorted)
wenzelm@46761
   374
          for (i <- 0 until files.length)
wenzelm@46761
   375
            files_list.selection.indices += i
wenzelm@44606
   376
wenzelm@46761
   377
          val answer =
wenzelm@46761
   378
            Library.confirm_dialog(view,
wenzelm@46761
   379
              "Auto loading of required files",
wenzelm@46761
   380
              JOptionPane.YES_NO_OPTION,
wenzelm@46761
   381
              "The following files are required to resolve theory imports.",
wenzelm@46761
   382
              "Reload selected files now?",
wenzelm@46761
   383
              new ScrollPane(files_list))
wenzelm@46761
   384
          if (answer == 0) {
wenzelm@46761
   385
            files.foreach(file =>
wenzelm@46761
   386
              if (files_list.selection.items.contains(file))
wenzelm@46761
   387
                jEdit.openFile(null: View, file))
wenzelm@46761
   388
          }
wenzelm@46761
   389
        }
wenzelm@44606
   390
      }
wenzelm@44574
   391
    }
wenzelm@44573
   392
wenzelm@44574
   393
wenzelm@44574
   394
  /* session manager */
wenzelm@44573
   395
wenzelm@39633
   396
  private val session_manager = actor {
wenzelm@39633
   397
    loop {
wenzelm@39633
   398
      react {
wenzelm@39735
   399
        case phase: Session.Phase =>
wenzelm@39735
   400
          phase match {
wenzelm@39735
   401
            case Session.Failed =>
wenzelm@46918
   402
              Swing_Thread.later {
wenzelm@48022
   403
                Library.error_dialog(jEdit.getActiveView, "Prover process failure",
wenzelm@48022
   404
                    "Isabelle Syslog", Library.scrollable_text(Isabelle.session.current_syslog()))
wenzelm@39735
   405
              }
wenzelm@39735
   406
wenzelm@44573
   407
            case Session.Ready =>
wenzelm@44573
   408
              Isabelle.jedit_buffers.foreach(Isabelle.init_model)
wenzelm@46740
   409
              delay_load(true)
wenzelm@44573
   410
wenzelm@46740
   411
            case Session.Shutdown =>
wenzelm@46740
   412
              Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@46740
   413
              delay_load(false)
wenzelm@46740
   414
wenzelm@39735
   415
            case _ =>
wenzelm@39735
   416
          }
wenzelm@39735
   417
        case bad => System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   418
      }
wenzelm@39630
   419
    }
wenzelm@39630
   420
  }
wenzelm@39630
   421
wenzelm@39630
   422
wenzelm@34618
   423
  /* main plugin plumbing */
wenzelm@34433
   424
wenzelm@34767
   425
  override def handleMessage(message: EBMessage)
wenzelm@34618
   426
  {
wenzelm@43510
   427
    Swing_Thread.assert()
wenzelm@34767
   428
    message match {
wenzelm@41383
   429
      case msg: EditorStarted =>
wenzelm@43510
   430
        if (Isabelle.Boolean_Property("auto-start"))
wenzelm@48870
   431
          Isabelle.session.start(Isabelle.session_args())
wenzelm@39630
   432
wenzelm@37557
   433
      case msg: BufferUpdate
wenzelm@47058
   434
      if msg.getWhat == BufferUpdate.LOADED || msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@44573
   435
        if (Isabelle.session.is_ready) {
wenzelm@44573
   436
          val buffer = msg.getBuffer
wenzelm@47058
   437
          if (buffer != null && !buffer.isLoading) Isabelle.init_model(buffer)
wenzelm@46740
   438
          delay_load(true)
wenzelm@44573
   439
        }
wenzelm@37557
   440
wenzelm@39634
   441
      case msg: EditPaneUpdate
wenzelm@39735
   442
      if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@39637
   443
          msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@39634
   444
          msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@39634
   445
          msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@34784
   446
        val edit_pane = msg.getEditPane
wenzelm@34784
   447
        val buffer = edit_pane.getBuffer
wenzelm@34784
   448
        val text_area = edit_pane.getTextArea
wenzelm@34784
   449
wenzelm@39637
   450
        if (buffer != null && text_area != null) {
wenzelm@39735
   451
          if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@43510
   452
              msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@43643
   453
            if (Isabelle.session.is_ready)
wenzelm@43510
   454
              Isabelle.init_view(buffer, text_area)
wenzelm@43510
   455
          }
wenzelm@43510
   456
          else Isabelle.exit_view(buffer, text_area)
immler@34671
   457
        }
wenzelm@34784
   458
wenzelm@34777
   459
      case msg: PropertiesChanged =>
wenzelm@46918
   460
        Isabelle.setup_tooltips()
wenzelm@34791
   461
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   462
wenzelm@34318
   463
      case _ =>
wenzelm@34318
   464
    }
wenzelm@34318
   465
  }
wenzelm@34318
   466
wenzelm@34618
   467
  override def start()
wenzelm@48870
   468
  { // FIXME more robust error handling
wenzelm@43443
   469
    Isabelle.plugin = this
wenzelm@39630
   470
    Isabelle.setup_tooltips()
wenzelm@43661
   471
    Isabelle_System.init()
wenzelm@43661
   472
    Isabelle_System.install_fonts()
wenzelm@48870
   473
wenzelm@48870
   474
    val content = Isabelle.session_content()
wenzelm@48870
   475
    val thy_load = new JEdit_Thy_Load(content.loaded_theories, content.syntax)
wenzelm@48870
   476
    Isabelle.session = new Session(thy_load)
wenzelm@48870
   477
wenzelm@43661
   478
    SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@43513
   479
    if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@43513
   480
      ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@39633
   481
    Isabelle.session.phase_changed += session_manager
wenzelm@34318
   482
  }
wenzelm@34618
   483
wenzelm@39628
   484
  override def stop()
wenzelm@34618
   485
  {
wenzelm@43513
   486
    Isabelle.session.phase_changed -= session_manager
wenzelm@43513
   487
    Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@39628
   488
    Isabelle.session.stop()
wenzelm@34318
   489
  }
wenzelm@34318
   490
}