src/Tools/jEdit/src/plugin.scala
author wenzelm
Tue Sep 29 15:37:37 2015 +0200 (2015-09-29)
changeset 61277 c9152a195899
parent 61024 7b7f01afab71
child 61278 4d2ea32e0f75
permissions -rw-r--r--
build session within running jEdit;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/plugin.scala
wenzelm@36760
     2
    Author:     Makarius
wenzelm@36760
     3
wenzelm@50362
     4
Main plumbing for PIDE infrastructure as jEdit plugin.
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@44573
    12
import javax.swing.JOptionPane
wenzelm@34318
    13
wenzelm@49246
    14
import scala.swing.{ListView, ScrollPane}
immler@34406
    15
wenzelm@60205
    16
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, View, Debug, PerspectiveManager}
wenzelm@53487
    17
import org.gjt.sp.jedit.gui.AboutDialog
wenzelm@39043
    18
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@59077
    19
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@50362
    20
import org.gjt.sp.jedit.syntax.ModeProvider
wenzelm@49100
    21
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@34429
    22
wenzelm@43443
    23
import org.gjt.sp.util.SyntaxUtilities
wenzelm@39241
    24
wenzelm@34318
    25
wenzelm@50205
    26
object PIDE
wenzelm@34618
    27
{
wenzelm@34784
    28
  /* plugin instance */
wenzelm@34784
    29
wenzelm@49245
    30
  val options = new JEdit_Options
wenzelm@53337
    31
  val completion_history = new Completion.History_Variable
wenzelm@56558
    32
  val spell_checker = new Spell_Checker_Variable
wenzelm@49245
    33
wenzelm@49099
    34
  @volatile var startup_failure: Option[Throwable] = None
wenzelm@49099
    35
  @volatile var startup_notified = false
wenzelm@34784
    36
wenzelm@49101
    37
  @volatile var plugin: Plugin = null
wenzelm@60835
    38
  @volatile var session: Session = new Session(JEdit_Resources.empty)
wenzelm@49101
    39
wenzelm@55791
    40
  def options_changed() { plugin.options_changed() }
wenzelm@56316
    41
  def deps_changed() { plugin.deps_changed() }
wenzelm@52815
    42
wenzelm@56208
    43
  def resources(): JEdit_Resources =
wenzelm@56208
    44
    session.resources.asInstanceOf[JEdit_Resources]
wenzelm@44955
    45
wenzelm@52971
    46
  lazy val editor = new JEdit_Editor
wenzelm@52971
    47
wenzelm@34784
    48
wenzelm@53244
    49
  /* popups */
wenzelm@53244
    50
wenzelm@53244
    51
  def dismissed_popups(view: View): Boolean =
wenzelm@53244
    52
  {
wenzelm@53272
    53
    var dismissed = false
wenzelm@53272
    54
wenzelm@53274
    55
    JEdit_Lib.jedit_text_areas(view).foreach(text_area =>
wenzelm@53274
    56
      if (Completion_Popup.Text_Area.dismissed(text_area)) dismissed = true)
wenzelm@53272
    57
wenzelm@53272
    58
    if (Pretty_Tooltip.dismissed_all()) dismissed = true
wenzelm@53272
    59
wenzelm@53272
    60
    dismissed
wenzelm@53244
    61
  }
wenzelm@53244
    62
wenzelm@53244
    63
wenzelm@43449
    64
  /* document model and view */
wenzelm@43449
    65
wenzelm@59077
    66
  def document_model(buffer: JEditBuffer): Option[Document_Model] =
wenzelm@59077
    67
    buffer match {
wenzelm@59077
    68
      case b: Buffer => Document_Model(b)
wenzelm@59077
    69
      case _ => None
wenzelm@59077
    70
    }
wenzelm@53274
    71
wenzelm@55712
    72
  def document_view(text_area: TextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
    73
wenzelm@44379
    74
  def document_views(buffer: Buffer): List[Document_View] =
wenzelm@44379
    75
    for {
wenzelm@49406
    76
      text_area <- JEdit_Lib.jedit_text_areas(buffer).toList
wenzelm@54529
    77
      doc_view <- document_view(text_area)
wenzelm@54529
    78
    } yield doc_view
wenzelm@44379
    79
wenzelm@54522
    80
  def document_models(): List[Document_Model] =
wenzelm@54522
    81
    for {
wenzelm@54522
    82
      buffer <- JEdit_Lib.jedit_buffers().toList
wenzelm@54522
    83
      model <- document_model(buffer)
wenzelm@54522
    84
    } yield model
wenzelm@54522
    85
wenzelm@54522
    86
  def document_blobs(): Document.Blobs =
wenzelm@55783
    87
    Document.Blobs(
wenzelm@55783
    88
      (for {
wenzelm@55783
    89
        buffer <- JEdit_Lib.jedit_buffers()
wenzelm@55783
    90
        model <- document_model(buffer)
wenzelm@55783
    91
        blob <- model.get_blob()
wenzelm@55783
    92
      } yield (model.node_name -> blob)).toMap)
wenzelm@54522
    93
wenzelm@50344
    94
  def exit_models(buffers: List[Buffer])
wenzelm@47058
    95
  {
wenzelm@57612
    96
    GUI_Thread.now {
wenzelm@54461
    97
      PIDE.editor.flush()
wenzelm@50344
    98
      buffers.foreach(buffer =>
wenzelm@50344
    99
        JEdit_Lib.buffer_lock(buffer) {
wenzelm@50344
   100
          JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@50344
   101
          Document_Model.exit(buffer)
wenzelm@50344
   102
        })
wenzelm@50344
   103
      }
wenzelm@47058
   104
  }
wenzelm@47058
   105
wenzelm@55791
   106
  def init_models()
wenzelm@43510
   107
  {
wenzelm@57612
   108
    GUI_Thread.now {
wenzelm@54461
   109
      PIDE.editor.flush()
wenzelm@55791
   110
wenzelm@55791
   111
      for {
wenzelm@55791
   112
        buffer <- JEdit_Lib.jedit_buffers()
wenzelm@55791
   113
        if buffer != null && !buffer.isLoading && !buffer.getBooleanProperty(Buffer.GZIPPED)
wenzelm@55791
   114
      } {
wenzelm@55791
   115
        JEdit_Lib.buffer_lock(buffer) {
wenzelm@56208
   116
          val node_name = resources.node_name(buffer)
wenzelm@59077
   117
          val model = Document_Model.init(session, buffer, node_name, document_model(buffer))
wenzelm@55791
   118
          for {
wenzelm@55791
   119
            text_area <- JEdit_Lib.jedit_text_areas(buffer)
wenzelm@55791
   120
            if document_view(text_area).map(_.model) != Some(model)
wenzelm@55791
   121
          } Document_View.init(model, text_area)
wenzelm@43510
   122
        }
wenzelm@55791
   123
      }
wenzelm@55791
   124
wenzelm@55791
   125
      PIDE.editor.invoke()
wenzelm@43510
   126
    }
wenzelm@43510
   127
  }
wenzelm@43510
   128
wenzelm@56774
   129
  def init_view(buffer: Buffer, text_area: JEditTextArea): Unit =
wenzelm@57612
   130
    GUI_Thread.now {
wenzelm@56774
   131
      JEdit_Lib.buffer_lock(buffer) {
wenzelm@56774
   132
        document_model(buffer) match {
wenzelm@56774
   133
          case Some(model) => Document_View.init(model, text_area)
wenzelm@56774
   134
          case None =>
wenzelm@56774
   135
        }
wenzelm@43510
   136
      }
wenzelm@43510
   137
    }
wenzelm@43510
   138
wenzelm@56774
   139
  def exit_view(buffer: Buffer, text_area: JEditTextArea): Unit =
wenzelm@57612
   140
    GUI_Thread.now {
wenzelm@56774
   141
      JEdit_Lib.buffer_lock(buffer) {
wenzelm@56774
   142
        Document_View.exit(text_area)
wenzelm@56774
   143
      }
wenzelm@43510
   144
    }
wenzelm@55621
   145
wenzelm@55621
   146
wenzelm@55621
   147
  /* current document content */
wenzelm@55621
   148
wenzelm@60272
   149
  def snapshot(view: View): Document.Snapshot =
wenzelm@55621
   150
  {
wenzelm@55621
   151
    val buffer = view.getBuffer
wenzelm@55621
   152
    document_model(buffer) match {
wenzelm@55621
   153
      case Some(model) => model.snapshot
wenzelm@55621
   154
      case None => error("No document model for current buffer")
wenzelm@55621
   155
    }
wenzelm@55621
   156
  }
wenzelm@55621
   157
wenzelm@57612
   158
  def rendering(view: View): Rendering = GUI_Thread.now
wenzelm@55621
   159
  {
wenzelm@55621
   160
    val text_area = view.getTextArea
wenzelm@55621
   161
    document_view(text_area) match {
wenzelm@55621
   162
      case Some(doc_view) => doc_view.get_rendering()
wenzelm@55621
   163
      case None => error("No document view for current text area")
wenzelm@55621
   164
    }
wenzelm@55621
   165
  }
wenzelm@34318
   166
}
wenzelm@34318
   167
wenzelm@34429
   168
wenzelm@34618
   169
class Plugin extends EBPlugin
wenzelm@34618
   170
{
wenzelm@56316
   171
  /* global changes */
wenzelm@55791
   172
wenzelm@56316
   173
  def options_changed()
wenzelm@56316
   174
  {
wenzelm@56715
   175
    PIDE.session.global_options.post(Session.Global_Options(PIDE.options.value))
wenzelm@56770
   176
    delay_load.invoke()
wenzelm@55791
   177
  }
wenzelm@55791
   178
wenzelm@56316
   179
  def deps_changed()
wenzelm@56316
   180
  {
wenzelm@56770
   181
    delay_load.invoke()
wenzelm@56316
   182
  }
wenzelm@56316
   183
wenzelm@55791
   184
wenzelm@44577
   185
  /* theory files */
wenzelm@44574
   186
wenzelm@50344
   187
  private lazy val delay_init =
wenzelm@57612
   188
    GUI_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@50344
   189
    {
wenzelm@55791
   190
      PIDE.init_models()
wenzelm@50344
   191
    }
wenzelm@50344
   192
wenzelm@57582
   193
  private val delay_load_active = Synchronized(false)
wenzelm@57582
   194
  private def delay_load_activated(): Boolean =
wenzelm@57582
   195
    delay_load_active.guarded_access(a => Some((!a, true)))
wenzelm@57582
   196
wenzelm@44573
   197
  private lazy val delay_load =
wenzelm@57612
   198
    GUI_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@44574
   199
    {
wenzelm@60205
   200
      if (Isabelle.continuous_checking && delay_load_activated() &&
wenzelm@60205
   201
          PerspectiveManager.isPerspectiveEnabled)
wenzelm@60205
   202
      {
wenzelm@57582
   203
        try {
wenzelm@57582
   204
          val view = jEdit.getActiveView()
wenzelm@44963
   205
wenzelm@57582
   206
          val buffers = JEdit_Lib.jedit_buffers().toList
wenzelm@57582
   207
          if (buffers.forall(_.isLoaded)) {
wenzelm@57582
   208
            def loaded_buffer(name: String): Boolean =
wenzelm@57582
   209
              buffers.exists(buffer => JEdit_Lib.buffer_name(buffer) == name)
wenzelm@44574
   210
wenzelm@57582
   211
            val thys =
wenzelm@57582
   212
              for {
wenzelm@57582
   213
                buffer <- buffers
wenzelm@57582
   214
                model <- PIDE.document_model(buffer)
wenzelm@57582
   215
                if model.is_theory
wenzelm@57582
   216
              } yield (model.node_name, Position.none)
wenzelm@46761
   217
wenzelm@57582
   218
            val thy_info = new Thy_Info(PIDE.resources)
wenzelm@60916
   219
            val thy_files = thy_info.dependencies("", thys).deps.map(_.name.node)
wenzelm@60916
   220
wenzelm@60916
   221
            val aux_files =
wenzelm@60916
   222
              if (PIDE.options.bool("jedit_auto_resolve")) {
wenzelm@60933
   223
                PIDE.editor.stable_tip_version() match {
wenzelm@61023
   224
                  case Some(version) => PIDE.resources.undefined_blobs(version.nodes).map(_.node)
wenzelm@60933
   225
                  case None => Nil
wenzelm@60933
   226
                }
wenzelm@60916
   227
              }
wenzelm@60916
   228
              else Nil
wenzelm@60916
   229
wenzelm@60916
   230
            val files =
wenzelm@60916
   231
              (thy_files ::: aux_files).filter(file =>
wenzelm@60916
   232
                !loaded_buffer(file) && PIDE.resources.check_file(file))
wenzelm@44574
   233
wenzelm@59319
   234
            if (files.nonEmpty) {
wenzelm@57582
   235
              if (PIDE.options.bool("jedit_auto_load")) {
wenzelm@57582
   236
                files.foreach(file => jEdit.openFile(null: View, file))
wenzelm@57582
   237
              }
wenzelm@57582
   238
              else {
wenzelm@57582
   239
                val files_list = new ListView(files.sorted)
wenzelm@57582
   240
                for (i <- 0 until files.length)
wenzelm@57582
   241
                  files_list.selection.indices += i
wenzelm@44606
   242
wenzelm@57582
   243
                val answer =
wenzelm@57582
   244
                  GUI.confirm_dialog(view,
wenzelm@57582
   245
                    "Auto loading of required files",
wenzelm@57582
   246
                    JOptionPane.YES_NO_OPTION,
wenzelm@57582
   247
                    "The following files are required to resolve theory imports.",
wenzelm@57582
   248
                    "Reload selected files now?",
wenzelm@57582
   249
                    new ScrollPane(files_list),
wenzelm@57582
   250
                    new Isabelle.Continuous_Checking)
wenzelm@57582
   251
                if (answer == 0) {
wenzelm@57582
   252
                  files.foreach(file =>
wenzelm@57582
   253
                    if (files_list.selection.items.contains(file))
wenzelm@57582
   254
                      jEdit.openFile(null: View, file))
wenzelm@57582
   255
                }
wenzelm@53715
   256
              }
wenzelm@53074
   257
            }
wenzelm@46761
   258
          }
wenzelm@46761
   259
        }
wenzelm@57582
   260
        finally { delay_load_active.change(_ => false) }
wenzelm@44606
   261
      }
wenzelm@44574
   262
    }
wenzelm@44573
   263
wenzelm@44574
   264
wenzelm@61277
   265
  /* session build */
wenzelm@61277
   266
wenzelm@61277
   267
  def session_build(): Int =
wenzelm@61277
   268
  {
wenzelm@61277
   269
    val system_dialog = new System_Dialog
wenzelm@61277
   270
wenzelm@61277
   271
    try {
wenzelm@61277
   272
      val mode = Isabelle_System.getenv("JEDIT_BUILD_MODE")
wenzelm@61277
   273
      if (mode == "none")
wenzelm@61277
   274
        system_dialog.return_code(0)
wenzelm@61277
   275
      else {
wenzelm@61277
   276
        val system_mode = mode == "" || mode == "system"
wenzelm@61277
   277
        val dirs = Path.split(Isabelle_System.getenv("JEDIT_SESSION_DIRS"))
wenzelm@61277
   278
        val session = Isabelle_System.default_logic(
wenzelm@61277
   279
          Isabelle_System.getenv("JEDIT_LOGIC"),
wenzelm@61277
   280
          PIDE.options.string("jedit_logic"))
wenzelm@61277
   281
wenzelm@61277
   282
        if (Build.build(options = PIDE.options.value, build_heap = true, no_build = true,
wenzelm@61277
   283
            dirs = dirs, system_mode = system_mode, sessions = List(session)) == 0)
wenzelm@61277
   284
          system_dialog.return_code(0)
wenzelm@61277
   285
        else {
wenzelm@61277
   286
          system_dialog.title("Isabelle build (" +
wenzelm@61277
   287
            Isabelle_System.getenv("ML_IDENTIFIER") + " / " +
wenzelm@61277
   288
            "jdk-" + Platform.jvm_version + "_" + Platform.jvm_platform + ")")
wenzelm@61277
   289
          system_dialog.echo("Build started for Isabelle/" + session + " ...")
wenzelm@61277
   290
wenzelm@61277
   291
          val (out, rc) =
wenzelm@61277
   292
            try {
wenzelm@61277
   293
              ("",
wenzelm@61277
   294
                Build.build(options = PIDE.options.value, progress = system_dialog,
wenzelm@61277
   295
                  build_heap = true, dirs = dirs, system_mode = system_mode,
wenzelm@61277
   296
                  sessions = List(session)))
wenzelm@61277
   297
            }
wenzelm@61277
   298
            catch {
wenzelm@61277
   299
              case exn: Throwable =>
wenzelm@61277
   300
                (Output.error_text(Exn.message(exn)) + "\n", Exn.return_code(exn, 2))
wenzelm@61277
   301
            }
wenzelm@61277
   302
wenzelm@61277
   303
          system_dialog.echo(out + (if (rc == 0) "OK\n" else "Return code: " + rc + "\n"))
wenzelm@61277
   304
          system_dialog.return_code(rc)
wenzelm@61277
   305
        }
wenzelm@61277
   306
      }
wenzelm@61277
   307
    }
wenzelm@61277
   308
    catch {
wenzelm@61277
   309
      case exn: Throwable =>
wenzelm@61277
   310
        GUI.dialog(null, "Isabelle", GUI.scrollable_text(Exn.message(exn)))
wenzelm@61277
   311
        system_dialog.return_code(Exn.return_code(exn, 2))
wenzelm@61277
   312
    }
wenzelm@61277
   313
wenzelm@61277
   314
    system_dialog.join()
wenzelm@61277
   315
  }
wenzelm@61277
   316
wenzelm@61277
   317
wenzelm@56715
   318
  /* session phase */
wenzelm@44573
   319
wenzelm@56715
   320
  private val session_phase =
wenzelm@56715
   321
    Session.Consumer[Session.Phase](getClass.getName) {
wenzelm@56715
   322
      case Session.Inactive | Session.Failed =>
wenzelm@57612
   323
        GUI_Thread.later {
wenzelm@56715
   324
          GUI.error_dialog(jEdit.getActiveView, "Prover process terminated",
wenzelm@56775
   325
            "Isabelle Syslog", GUI.scrollable_text(PIDE.session.syslog_content()))
wenzelm@56715
   326
        }
wenzelm@39735
   327
wenzelm@56715
   328
      case Session.Ready =>
wenzelm@60910
   329
        Debugger.init_session(PIDE.session)
wenzelm@60075
   330
        PIDE.session.update_options(PIDE.options.value)
wenzelm@56715
   331
        PIDE.init_models()
wenzelm@57640
   332
wenzelm@57640
   333
        if (!Isabelle.continuous_checking) {
wenzelm@57640
   334
          GUI_Thread.later {
wenzelm@57640
   335
            val answer =
wenzelm@57640
   336
              GUI.confirm_dialog(jEdit.getActiveView,
wenzelm@57640
   337
                "Continuous checking of PIDE document",
wenzelm@57640
   338
                JOptionPane.YES_NO_OPTION,
wenzelm@57640
   339
                "Continuous checking is presently disabled:",
wenzelm@57640
   340
                "editor buffers will remain inactive!",
wenzelm@57640
   341
                "Enable continuous checking now?")
wenzelm@57640
   342
            if (answer == 0) Isabelle.continuous_checking = true
wenzelm@57640
   343
          }
wenzelm@57640
   344
        }
wenzelm@57640
   345
wenzelm@56770
   346
        delay_load.invoke()
wenzelm@44573
   347
wenzelm@56715
   348
      case Session.Shutdown =>
wenzelm@56715
   349
        PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@56770
   350
        delay_load.revoke()
wenzelm@46740
   351
wenzelm@56715
   352
      case _ =>
wenzelm@39630
   353
    }
wenzelm@39630
   354
wenzelm@39630
   355
wenzelm@34618
   356
  /* main plugin plumbing */
wenzelm@34433
   357
wenzelm@34767
   358
  override def handleMessage(message: EBMessage)
wenzelm@34618
   359
  {
wenzelm@57612
   360
    GUI_Thread.assert {}
wenzelm@39630
   361
wenzelm@50205
   362
    if (PIDE.startup_failure.isDefined && !PIDE.startup_notified) {
wenzelm@49099
   363
      message match {
wenzelm@49100
   364
        case msg: EditorStarted =>
wenzelm@51616
   365
          GUI.error_dialog(null, "Isabelle plugin startup failure",
wenzelm@51616
   366
            GUI.scrollable_text(Exn.message(PIDE.startup_failure.get)),
wenzelm@49099
   367
            "Prover IDE inactive!")
wenzelm@50205
   368
          PIDE.startup_notified = true
wenzelm@49099
   369
        case _ =>
wenzelm@49099
   370
      }
wenzelm@49099
   371
    }
wenzelm@49099
   372
wenzelm@50205
   373
    if (PIDE.startup_failure.isEmpty) {
wenzelm@49099
   374
      message match {
wenzelm@49099
   375
        case msg: EditorStarted =>
wenzelm@57649
   376
          if (Distribution.is_identified && !Distribution.is_official) {
wenzelm@57680
   377
            GUI.warning_dialog(jEdit.getActiveView, "Isabelle version for testing",
wenzelm@60206
   378
              "This is " + Distribution.version + ".",
wenzelm@57649
   379
              "It is for testing only, not for production use.")
wenzelm@57649
   380
          }
wenzelm@57649
   381
wenzelm@61277
   382
          Simple_Thread.fork("session_build") {
wenzelm@61277
   383
            val rc = session_build()
wenzelm@61277
   384
            if (rc == 0) PIDE.session.start("Isabelle", Isabelle_Logic.session_args())
wenzelm@61277
   385
          }
wenzelm@61277
   386
wenzelm@49099
   387
        case msg: BufferUpdate
wenzelm@57611
   388
        if msg.getWhat == BufferUpdate.LOADED ||
wenzelm@57611
   389
          msg.getWhat == BufferUpdate.PROPERTIES_CHANGED ||
wenzelm@57611
   390
          msg.getWhat == BufferUpdate.CLOSING =>
wenzelm@57611
   391
wenzelm@57611
   392
          if (msg.getWhat == BufferUpdate.CLOSING) {
wenzelm@57611
   393
            val buffer = msg.getBuffer
wenzelm@57611
   394
            if (buffer != null) PIDE.editor.remove_node(PIDE.resources.node_name(msg.getBuffer))
wenzelm@57611
   395
          }
wenzelm@50205
   396
          if (PIDE.session.is_ready) {
wenzelm@55791
   397
            delay_init.invoke()
wenzelm@55791
   398
            delay_load.invoke()
wenzelm@49099
   399
          }
wenzelm@34784
   400
wenzelm@49099
   401
        case msg: EditPaneUpdate
wenzelm@60215
   402
        if msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@49099
   403
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   404
            msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@60215
   405
            msg.getWhat == EditPaneUpdate.DESTROYED =>
wenzelm@49099
   406
          val edit_pane = msg.getEditPane
wenzelm@49099
   407
          val buffer = edit_pane.getBuffer
wenzelm@49099
   408
          val text_area = edit_pane.getTextArea
wenzelm@49099
   409
wenzelm@49099
   410
          if (buffer != null && text_area != null) {
wenzelm@49099
   411
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   412
                msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@50205
   413
              if (PIDE.session.is_ready)
wenzelm@50205
   414
                PIDE.init_view(buffer, text_area)
wenzelm@49099
   415
            }
wenzelm@52867
   416
            else {
wenzelm@53244
   417
              PIDE.dismissed_popups(text_area.getView)
wenzelm@52867
   418
              PIDE.exit_view(buffer, text_area)
wenzelm@52867
   419
            }
wenzelm@53274
   420
wenzelm@53274
   421
            if (msg.getWhat == EditPaneUpdate.CREATED)
wenzelm@53274
   422
              Completion_Popup.Text_Area.init(text_area)
wenzelm@53274
   423
wenzelm@53274
   424
            if (msg.getWhat == EditPaneUpdate.DESTROYED)
wenzelm@53274
   425
              Completion_Popup.Text_Area.exit(text_area)
wenzelm@43510
   426
          }
wenzelm@34784
   427
wenzelm@49099
   428
        case msg: PropertiesChanged =>
wenzelm@56558
   429
          PIDE.spell_checker.update(PIDE.options.value)
wenzelm@52084
   430
          PIDE.session.update_options(PIDE.options.value)
wenzelm@34784
   431
wenzelm@49099
   432
        case _ =>
wenzelm@49099
   433
      }
wenzelm@34318
   434
    }
wenzelm@34318
   435
  }
wenzelm@34318
   436
wenzelm@34618
   437
  override def start()
wenzelm@49099
   438
  {
wenzelm@49099
   439
    try {
wenzelm@50709
   440
      Debug.DISABLE_SEARCH_DIALOG_POOL = true
wenzelm@50709
   441
wenzelm@50205
   442
      PIDE.plugin = this
wenzelm@49099
   443
      Isabelle_System.init()
wenzelm@57908
   444
      GUI.install_fonts()
wenzelm@49099
   445
wenzelm@53338
   446
      PIDE.options.update(Options.init())
wenzelm@53337
   447
      PIDE.completion_history.load()
wenzelm@56558
   448
      PIDE.spell_checker.update(PIDE.options.value)
wenzelm@49245
   449
wenzelm@49099
   450
      SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@49099
   451
      if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@49099
   452
        ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@48870
   453
wenzelm@53274
   454
      JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.init _)
wenzelm@53274
   455
wenzelm@49246
   456
      val content = Isabelle_Logic.session_content(false)
wenzelm@56801
   457
      val resources =
wenzelm@56801
   458
        new JEdit_Resources(content.loaded_theories, content.known_theories, content.syntax)
wenzelm@49524
   459
wenzelm@56208
   460
      PIDE.session = new Session(resources) {
wenzelm@50207
   461
        override def output_delay = PIDE.options.seconds("editor_output_delay")
wenzelm@57867
   462
        override def prune_delay = PIDE.options.seconds("editor_prune_delay")
wenzelm@57974
   463
        override def syslog_limit = PIDE.options.int("editor_syslog_limit")
wenzelm@50205
   464
        override def reparse_limit = PIDE.options.int("editor_reparse_limit")
wenzelm@49288
   465
      }
wenzelm@48870
   466
wenzelm@56715
   467
      PIDE.session.phase_changed += session_phase
wenzelm@50205
   468
      PIDE.startup_failure = None
wenzelm@49099
   469
    }
wenzelm@49099
   470
    catch {
wenzelm@49099
   471
      case exn: Throwable =>
wenzelm@50205
   472
        PIDE.startup_failure = Some(exn)
wenzelm@50205
   473
        PIDE.startup_notified = false
wenzelm@49099
   474
    }
wenzelm@34318
   475
  }
wenzelm@34618
   476
wenzelm@39628
   477
  override def stop()
wenzelm@34618
   478
  {
wenzelm@53274
   479
    JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.exit _)
wenzelm@53274
   480
wenzelm@53337
   481
    if (PIDE.startup_failure.isEmpty) {
wenzelm@50205
   482
      PIDE.options.value.save_prefs()
wenzelm@53337
   483
      PIDE.completion_history.value.save()
wenzelm@53337
   484
    }
wenzelm@49245
   485
wenzelm@56715
   486
    PIDE.session.phase_changed -= session_phase
wenzelm@50344
   487
    PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@50205
   488
    PIDE.session.stop()
wenzelm@34318
   489
  }
wenzelm@34318
   490
}