src/Tools/jEdit/src/plugin.scala
author wenzelm
Mon Aug 29 21:55:49 2011 +0200 (2011-08-29)
changeset 44574 24444588fddd
parent 44573 51f8895b9ad9
child 44577 96b6388d06c4
permissions -rw-r--r--
actual auto loading of required files;
eliminated File_Store in favour of Thy_Load;
tuned;
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@43645
    13
import java.io.{File, FileInputStream, IOException}
wenzelm@34318
    14
import java.awt.Font
wenzelm@44573
    15
import javax.swing.JOptionPane
wenzelm@44574
    16
import javax.swing.text.Segment
wenzelm@34318
    17
wenzelm@34497
    18
import scala.collection.mutable
wenzelm@39517
    19
import scala.swing.ComboBox
wenzelm@44574
    20
import scala.util.Sorting
immler@34406
    21
wenzelm@39241
    22
import org.gjt.sp.jedit.{jEdit, GUIUtilities, EBMessage, EBPlugin,
wenzelm@43645
    23
  Buffer, EditPane, MiscUtilities, ServiceManager, View}
immler@34406
    24
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@39043
    25
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@43452
    26
import org.gjt.sp.jedit.syntax.{Token => JEditToken, ModeProvider}
wenzelm@39633
    27
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@37068
    28
import org.gjt.sp.jedit.gui.DockableWindowManager
wenzelm@44160
    29
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
wenzelm@34429
    30
wenzelm@43443
    31
import org.gjt.sp.util.SyntaxUtilities
wenzelm@39241
    32
import org.gjt.sp.util.Log
wenzelm@39241
    33
wenzelm@39630
    34
import scala.actors.Actor
wenzelm@39630
    35
import Actor._
wenzelm@39630
    36
wenzelm@34318
    37
wenzelm@34618
    38
object Isabelle
wenzelm@34618
    39
{
wenzelm@34784
    40
  /* plugin instance */
wenzelm@34784
    41
wenzelm@43443
    42
  var plugin: Plugin = null
wenzelm@34784
    43
  var session: Session = null
wenzelm@34784
    44
wenzelm@34784
    45
wenzelm@34618
    46
  /* properties */
wenzelm@34618
    47
wenzelm@34618
    48
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    49
wenzelm@34618
    50
  object Property
wenzelm@34618
    51
  {
wenzelm@36814
    52
    def apply(name: String): String =
wenzelm@36814
    53
      jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@36814
    54
    def apply(name: String, default: String): String =
wenzelm@36814
    55
      jEdit.getProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    56
    def update(name: String, value: String) =
wenzelm@36814
    57
      jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    58
  }
wenzelm@34433
    59
wenzelm@34618
    60
  object Boolean_Property
wenzelm@34618
    61
  {
wenzelm@36814
    62
    def apply(name: String): Boolean =
wenzelm@36814
    63
      jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@36814
    64
    def apply(name: String, default: Boolean): Boolean =
wenzelm@36814
    65
      jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    66
    def update(name: String, value: Boolean) =
wenzelm@36814
    67
      jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    68
  }
wenzelm@34618
    69
wenzelm@34618
    70
  object Int_Property
wenzelm@34618
    71
  {
wenzelm@36814
    72
    def apply(name: String): Int =
wenzelm@36814
    73
      jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@36814
    74
    def apply(name: String, default: Int): Int =
wenzelm@36814
    75
      jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    76
    def update(name: String, value: Int) =
wenzelm@36814
    77
      jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    78
  }
wenzelm@34618
    79
wenzelm@40848
    80
  object Double_Property
wenzelm@40848
    81
  {
wenzelm@40848
    82
    def apply(name: String): Double =
wenzelm@40848
    83
      jEdit.getDoubleProperty(OPTION_PREFIX + name, 0.0)
wenzelm@40848
    84
    def apply(name: String, default: Double): Double =
wenzelm@40848
    85
      jEdit.getDoubleProperty(OPTION_PREFIX + name, default)
wenzelm@40848
    86
    def update(name: String, value: Double) =
wenzelm@40848
    87
      jEdit.setDoubleProperty(OPTION_PREFIX + name, value)
wenzelm@40848
    88
  }
wenzelm@40848
    89
wenzelm@40850
    90
  object Time_Property
wenzelm@40850
    91
  {
wenzelm@40850
    92
    def apply(name: String): Time =
wenzelm@40850
    93
      Time.seconds(Double_Property(name))
wenzelm@40850
    94
    def apply(name: String, default: Time): Time =
wenzelm@40850
    95
      Time.seconds(Double_Property(name, default.seconds))
wenzelm@40850
    96
    def update(name: String, value: Time) =
wenzelm@40850
    97
      Double_Property.update(name, value.seconds)
wenzelm@40850
    98
  }
wenzelm@40850
    99
wenzelm@40848
   100
wenzelm@37164
   101
  /* font */
wenzelm@37164
   102
wenzelm@37164
   103
  def font_family(): String = jEdit.getProperty("view.font")
wenzelm@37164
   104
wenzelm@37019
   105
  def font_size(): Float =
wenzelm@37019
   106
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
   107
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
   108
wenzelm@34618
   109
wenzelm@39043
   110
  /* text area ranges */
wenzelm@39043
   111
wenzelm@43714
   112
  sealed case class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@39043
   113
wenzelm@39043
   114
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@39043
   115
  {
wenzelm@39043
   116
    val p = text_area.offsetToXY(range.start)
wenzelm@39043
   117
    val q = text_area.offsetToXY(range.stop)
wenzelm@39043
   118
    if (p != null && q != null && p.y == q.y) Some(new Gfx_Range(p.x, p.y, q.x - p.x))
wenzelm@39043
   119
    else None
wenzelm@39043
   120
  }
wenzelm@39043
   121
wenzelm@39043
   122
wenzelm@37201
   123
  /* tooltip markup */
wenzelm@37201
   124
wenzelm@37201
   125
  def tooltip(text: String): String =
wenzelm@37203
   126
    "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
wenzelm@37203
   127
        Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
wenzelm@37203
   128
      HTML.encode(text) + "</pre></html>"
wenzelm@37201
   129
wenzelm@40849
   130
  def tooltip_dismiss_delay(): Time =
wenzelm@40852
   131
    Time_Property("tooltip-dismiss-delay", Time.seconds(8.0)) max Time.seconds(0.5)
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@38221
   165
  /* main jEdit components */
wenzelm@34784
   166
wenzelm@37177
   167
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   168
wenzelm@37177
   169
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   170
wenzelm@34784
   171
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   172
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   173
wenzelm@34784
   174
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   175
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   176
wenzelm@34784
   177
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   178
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   179
wenzelm@38843
   180
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38640
   181
  {
wenzelm@38640
   182
    try { buffer.readLock(); body }
wenzelm@38640
   183
    finally { buffer.readUnlock() }
wenzelm@38640
   184
  }
wenzelm@38640
   185
wenzelm@38843
   186
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38843
   187
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@38843
   188
wenzelm@40474
   189
  def buffer_text(buffer: JEditBuffer): String =
wenzelm@40474
   190
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
wenzelm@40474
   191
wenzelm@44574
   192
  def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
wenzelm@44574
   193
wenzelm@44160
   194
  def buffer_path(buffer: Buffer): (String, String) =
wenzelm@44574
   195
    (buffer.getDirectory, buffer_name(buffer))
wenzelm@44160
   196
wenzelm@34784
   197
wenzelm@43449
   198
  /* document model and view */
wenzelm@43449
   199
wenzelm@43449
   200
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@43449
   201
  def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
   202
wenzelm@44379
   203
  def document_views(buffer: Buffer): List[Document_View] =
wenzelm@44379
   204
    for {
wenzelm@44379
   205
      text_area <- jedit_text_areas(buffer).toList
wenzelm@44379
   206
      val doc_view = document_view(text_area)
wenzelm@44379
   207
      if doc_view.isDefined
wenzelm@44379
   208
    } yield doc_view.get
wenzelm@44379
   209
wenzelm@43510
   210
  def init_model(buffer: Buffer)
wenzelm@43510
   211
  {
wenzelm@43510
   212
    swing_buffer_lock(buffer) {
wenzelm@43510
   213
      val opt_model =
wenzelm@43510
   214
        document_model(buffer) match {
wenzelm@43510
   215
          case Some(model) => Some(model)
wenzelm@43510
   216
          case None =>
wenzelm@44160
   217
            val (master_dir, path) = buffer_path(buffer)
wenzelm@44160
   218
            Thy_Header.thy_name(path) match {
wenzelm@44222
   219
              case Some(name) =>
wenzelm@44222
   220
                Some(Document_Model.init(session, buffer, master_dir, path, name))
wenzelm@44160
   221
              case None => None
wenzelm@43510
   222
            }
wenzelm@43510
   223
        }
wenzelm@43510
   224
      if (opt_model.isDefined) {
wenzelm@43510
   225
        for (text_area <- jedit_text_areas(buffer)) {
wenzelm@43510
   226
          if (document_view(text_area).map(_.model) != opt_model)
wenzelm@43510
   227
            Document_View.init(opt_model.get, text_area)
wenzelm@43510
   228
        }
wenzelm@43510
   229
      }
wenzelm@43510
   230
    }
wenzelm@43510
   231
  }
wenzelm@43510
   232
wenzelm@43510
   233
  def exit_model(buffer: Buffer)
wenzelm@43510
   234
  {
wenzelm@43510
   235
    swing_buffer_lock(buffer) {
wenzelm@43510
   236
      jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@43510
   237
      Document_Model.exit(buffer)
wenzelm@43510
   238
    }
wenzelm@43510
   239
  }
wenzelm@43510
   240
wenzelm@43510
   241
  def init_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   242
  {
wenzelm@43510
   243
    swing_buffer_lock(buffer) {
wenzelm@43510
   244
      document_model(buffer) match {
wenzelm@43510
   245
        case Some(model) => Document_View.init(model, text_area)
wenzelm@43510
   246
        case None =>
wenzelm@43510
   247
      }
wenzelm@43510
   248
    }
wenzelm@43510
   249
  }
wenzelm@43510
   250
wenzelm@43510
   251
  def exit_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   252
  {
wenzelm@43510
   253
    swing_buffer_lock(buffer) {
wenzelm@43510
   254
      Document_View.exit(text_area)
wenzelm@43510
   255
    }
wenzelm@43510
   256
  }
wenzelm@43510
   257
wenzelm@43449
   258
wenzelm@37068
   259
  /* dockable windows */
wenzelm@37068
   260
wenzelm@37068
   261
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   262
wenzelm@39515
   263
  def docked_session(view: View): Option[Session_Dockable] =
wenzelm@39515
   264
    wm(view).getDockableWindow("isabelle-session") match {
wenzelm@39515
   265
      case dockable: Session_Dockable => Some(dockable)
wenzelm@39515
   266
      case _ => None
wenzelm@39515
   267
    }
wenzelm@39515
   268
wenzelm@37068
   269
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   270
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   271
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   272
      case _ => None
wenzelm@37068
   273
    }
wenzelm@37068
   274
wenzelm@37068
   275
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   276
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   277
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   278
      case _ => None
wenzelm@37068
   279
    }
wenzelm@37068
   280
wenzelm@37068
   281
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   282
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   283
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   284
      case _ => None
wenzelm@37068
   285
    }
wenzelm@37068
   286
wenzelm@37068
   287
wenzelm@39517
   288
  /* logic image */
wenzelm@39517
   289
wenzelm@39517
   290
  def default_logic(): String =
wenzelm@39517
   291
  {
wenzelm@43661
   292
    val logic = Isabelle_System.getenv("JEDIT_LOGIC")
wenzelm@39517
   293
    if (logic != "") logic
wenzelm@43661
   294
    else Isabelle_System.getenv_strict("ISABELLE_LOGIC")
wenzelm@39517
   295
  }
wenzelm@39517
   296
wenzelm@39517
   297
  class Logic_Entry(val name: String, val description: String)
wenzelm@39517
   298
  {
wenzelm@39517
   299
    override def toString = description
wenzelm@39517
   300
  }
wenzelm@39517
   301
wenzelm@39517
   302
  def logic_selector(logic: String): ComboBox[Logic_Entry] =
wenzelm@39517
   303
  {
wenzelm@39517
   304
    val entries =
wenzelm@39517
   305
      new Logic_Entry("", "default (" + default_logic() + ")") ::
wenzelm@43661
   306
        Isabelle_System.find_logics().map(name => new Logic_Entry(name, name))
wenzelm@39517
   307
    val component = new ComboBox(entries)
wenzelm@39517
   308
    entries.find(_.name == logic) match {
wenzelm@39517
   309
      case None =>
wenzelm@39517
   310
      case Some(entry) => component.selection.item = entry
wenzelm@39517
   311
    }
wenzelm@39702
   312
    component.tooltip = "Isabelle logic image"
wenzelm@39517
   313
    component
wenzelm@39517
   314
  }
wenzelm@39702
   315
wenzelm@39702
   316
  def start_session()
wenzelm@39702
   317
  {
wenzelm@40852
   318
    val timeout = Time_Property("startup-timeout", Time.seconds(10)) max Time.seconds(5)
wenzelm@43670
   319
    val modes = space_explode(',', Isabelle_System.getenv("JEDIT_PRINT_MODE")).map("-m" + _)
wenzelm@39702
   320
    val logic = {
wenzelm@39702
   321
      val logic = Property("logic")
wenzelm@39702
   322
      if (logic != null && logic != "") logic
wenzelm@39702
   323
      else Isabelle.default_logic()
wenzelm@39702
   324
    }
wenzelm@40850
   325
    session.start(timeout, modes ::: List(logic))
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@34318
   345
}
wenzelm@34318
   346
wenzelm@34429
   347
wenzelm@34618
   348
class Plugin extends EBPlugin
wenzelm@34618
   349
{
wenzelm@44574
   350
  /* theory files via editor document model */
wenzelm@44574
   351
wenzelm@44574
   352
  val thy_load = new Thy_Load
wenzelm@44574
   353
  {
wenzelm@44574
   354
    private var loaded_theories: Set[String] = Set()
wenzelm@43645
   355
wenzelm@44574
   356
    def register_thy(thy_name: String)
wenzelm@44574
   357
    {
wenzelm@44574
   358
      synchronized { loaded_theories += thy_name }
wenzelm@44574
   359
    }
wenzelm@44574
   360
    def is_loaded(thy_name: String): Boolean =
wenzelm@44574
   361
      synchronized { loaded_theories.contains(thy_name) }
wenzelm@44574
   362
wenzelm@44185
   363
    def append(master_dir: String, source_path: Path): String =
wenzelm@43645
   364
    {
wenzelm@44185
   365
      val path = source_path.expand
wenzelm@44185
   366
      if (path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@44185
   367
      else {
wenzelm@44185
   368
        val vfs = VFSManager.getVFSForPath(master_dir)
wenzelm@44185
   369
        if (vfs.isInstanceOf[FileVFS])
wenzelm@44222
   370
          MiscUtilities.resolveSymlinks(
wenzelm@44222
   371
            vfs.constructPath(master_dir, Isabelle_System.platform_path(path)))
wenzelm@44185
   372
        else vfs.constructPath(master_dir, Isabelle_System.standard_path(path))
wenzelm@44185
   373
      }
wenzelm@44163
   374
    }
wenzelm@43645
   375
wenzelm@44574
   376
    def check_thy(node_name: String): Thy_Header =
wenzelm@44163
   377
    {
wenzelm@44574
   378
      Swing_Thread.now {
wenzelm@44574
   379
        Isabelle.jedit_buffers().find(buffer => Isabelle.buffer_name(buffer) == node_name) match {
wenzelm@44574
   380
          case Some(buffer) =>
wenzelm@44574
   381
            Isabelle.buffer_lock(buffer) {
wenzelm@44574
   382
              val text = new Segment
wenzelm@44574
   383
              buffer.getText(0, buffer.getLength, text)
wenzelm@44574
   384
              Some(Thy_Header.read(text))
wenzelm@44574
   385
            }
wenzelm@44574
   386
          case None => None
wenzelm@44574
   387
        }
wenzelm@44574
   388
      } getOrElse {
wenzelm@44574
   389
        val file = new File(node_name)  // FIXME load URL via jEdit VFS (!?)
wenzelm@44574
   390
        if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
wenzelm@44574
   391
        Thy_Header.read(file)
wenzelm@43645
   392
      }
wenzelm@43645
   393
    }
wenzelm@43645
   394
  }
wenzelm@43645
   395
wenzelm@44574
   396
  val thy_info = new Thy_Info(thy_load)
wenzelm@39630
   397
wenzelm@44573
   398
  private lazy val delay_load =
wenzelm@44574
   399
    Swing_Thread.delay_last(Isabelle.session.load_delay)
wenzelm@44574
   400
    {
wenzelm@44574
   401
      val buffers = Isabelle.jedit_buffers().toList
wenzelm@44574
   402
      def loaded_buffer(name: String): Boolean =
wenzelm@44574
   403
        buffers.exists(buffer => Isabelle.buffer_name(buffer) == name)
wenzelm@44574
   404
wenzelm@44574
   405
      val thys =
wenzelm@44574
   406
        for (buffer <- buffers; model <- Isabelle.document_model(buffer))
wenzelm@44574
   407
          yield (model.master_dir, model.thy_name)
wenzelm@44574
   408
      val files = thy_info.dependencies(thys).map(_._1).filterNot(loaded_buffer _)
wenzelm@44574
   409
wenzelm@44574
   410
      val do_load = !files.isEmpty &&
wenzelm@44573
   411
        {
wenzelm@44574
   412
          val files_sorted = { val a = files.toArray; Sorting.quickSort(a); a.toList }
wenzelm@44574
   413
          val files_text = new scala.swing.TextArea(files_sorted.mkString("\n"))
wenzelm@44574
   414
          files_text.editable = false
wenzelm@44574
   415
          Library.confirm_dialog(jEdit.getActiveView(),
wenzelm@44573
   416
            "Auto loading of required files",
wenzelm@44573
   417
            JOptionPane.YES_NO_OPTION,
wenzelm@44574
   418
            "The following files are required to resolve theory imports.  Reload now?",
wenzelm@44574
   419
            files_text) == 0
wenzelm@44573
   420
        }
wenzelm@44574
   421
      if (do_load)
wenzelm@44574
   422
        for (file <- files if !loaded_buffer(file))
wenzelm@44574
   423
          jEdit.openFile(null: View, file)
wenzelm@44574
   424
    }
wenzelm@44573
   425
wenzelm@44574
   426
wenzelm@44574
   427
  /* session manager */
wenzelm@44573
   428
wenzelm@39633
   429
  private val session_manager = actor {
wenzelm@39633
   430
    loop {
wenzelm@39633
   431
      react {
wenzelm@39735
   432
        case phase: Session.Phase =>
wenzelm@39735
   433
          phase match {
wenzelm@39735
   434
            case Session.Failed =>
wenzelm@39735
   435
              Swing_Thread.now {
wenzelm@39735
   436
                val text = new scala.swing.TextArea(Isabelle.session.syslog())
wenzelm@39735
   437
                text.editable = false
wenzelm@39735
   438
                Library.error_dialog(jEdit.getActiveView, "Failed to start Isabelle process", text)
wenzelm@39735
   439
              }
wenzelm@39735
   440
wenzelm@44573
   441
            case Session.Ready =>
wenzelm@44573
   442
              Isabelle.jedit_buffers.foreach(Isabelle.init_model)
wenzelm@44573
   443
              delay_load()
wenzelm@44573
   444
wenzelm@43510
   445
            case Session.Shutdown => Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@39735
   446
            case _ =>
wenzelm@39735
   447
          }
wenzelm@39735
   448
        case bad => System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   449
      }
wenzelm@39630
   450
    }
wenzelm@39630
   451
  }
wenzelm@39630
   452
wenzelm@39630
   453
wenzelm@34618
   454
  /* main plugin plumbing */
wenzelm@34433
   455
wenzelm@34767
   456
  override def handleMessage(message: EBMessage)
wenzelm@34618
   457
  {
wenzelm@43510
   458
    Swing_Thread.assert()
wenzelm@34767
   459
    message match {
wenzelm@41383
   460
      case msg: EditorStarted =>
wenzelm@43510
   461
        Isabelle.check_jvm()
wenzelm@43510
   462
        if (Isabelle.Boolean_Property("auto-start"))
wenzelm@43510
   463
          Isabelle.start_session()
wenzelm@39630
   464
wenzelm@37557
   465
      case msg: BufferUpdate
wenzelm@44434
   466
      if msg.getWhat == BufferUpdate.LOADED =>
wenzelm@44573
   467
        if (Isabelle.session.is_ready) {
wenzelm@44573
   468
          val buffer = msg.getBuffer
wenzelm@44573
   469
          if (buffer != null) Isabelle.init_model(buffer)
wenzelm@44573
   470
          delay_load()
wenzelm@44573
   471
        }
wenzelm@37557
   472
wenzelm@39634
   473
      case msg: EditPaneUpdate
wenzelm@39735
   474
      if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@39637
   475
          msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@39634
   476
          msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@39634
   477
          msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@34784
   478
        val edit_pane = msg.getEditPane
wenzelm@34784
   479
        val buffer = edit_pane.getBuffer
wenzelm@34784
   480
        val text_area = edit_pane.getTextArea
wenzelm@34784
   481
wenzelm@39637
   482
        if (buffer != null && text_area != null) {
wenzelm@39735
   483
          if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@43510
   484
              msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@43643
   485
            if (Isabelle.session.is_ready)
wenzelm@43510
   486
              Isabelle.init_view(buffer, text_area)
wenzelm@43510
   487
          }
wenzelm@43510
   488
          else Isabelle.exit_view(buffer, text_area)
immler@34671
   489
        }
wenzelm@34784
   490
wenzelm@34777
   491
      case msg: PropertiesChanged =>
wenzelm@43390
   492
        Swing_Thread.now { Isabelle.setup_tooltips() }
wenzelm@34791
   493
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   494
wenzelm@34318
   495
      case _ =>
wenzelm@34318
   496
    }
wenzelm@34318
   497
  }
wenzelm@34318
   498
wenzelm@34618
   499
  override def start()
wenzelm@34618
   500
  {
wenzelm@43443
   501
    Isabelle.plugin = this
wenzelm@39630
   502
    Isabelle.setup_tooltips()
wenzelm@43661
   503
    Isabelle_System.init()
wenzelm@43661
   504
    Isabelle_System.install_fonts()
wenzelm@44574
   505
    Isabelle.session = new Session(thy_load)
wenzelm@43661
   506
    SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@43513
   507
    if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@43513
   508
      ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@39633
   509
    Isabelle.session.phase_changed += session_manager
wenzelm@34318
   510
  }
wenzelm@34618
   511
wenzelm@39628
   512
  override def stop()
wenzelm@34618
   513
  {
wenzelm@43513
   514
    Isabelle.session.phase_changed -= session_manager
wenzelm@43513
   515
    Isabelle.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@39628
   516
    Isabelle.session.stop()
wenzelm@34318
   517
  }
wenzelm@34318
   518
}