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