src/Tools/jEdit/src/plugin.scala
author wenzelm
Thu, 31 Aug 2017 16:35:09 +0200
changeset 66572 1e5ae735e026
parent 66459 b578ef1a8b40
child 66590 8e1aac4eed11
permissions -rw-r--r--
tolerate errors in session structure, although this may lead to confusion about theory imports later on;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43282
5d294220ca43 moved sources -- eliminated Netbeans artifact of jedit package directory;
wenzelm
parents: 41537
diff changeset
     1
/*  Title:      Tools/jEdit/src/plugin.scala
36760
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     2
    Author:     Makarius
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     3
50362
wenzelm
parents: 50344
diff changeset
     4
Main plumbing for PIDE infrastructure as jEdit plugin.
36760
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     5
*/
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     6
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     8
34429
wenzelm
parents: 34422
diff changeset
     9
36015
6111de7c916a adapted to Scala 2.8.0 Beta 1;
wenzelm
parents: 34871
diff changeset
    10
import isabelle._
6111de7c916a adapted to Scala 2.8.0 Beta 1;
wenzelm
parents: 34871
diff changeset
    11
44573
51f8895b9ad9 some dialog for auto loading of required files (still inactive);
wenzelm
parents: 44434
diff changeset
    12
import javax.swing.JOptionPane
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    13
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
    14
import java.io.{File => JFile}
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
    15
60205
9ee125c3bff7 avoid auto-load dialog while exit/closeAllBuffers is active: the perspective manager happens to indicate this precisely in jEdit 5.2.0;
wenzelm
parents: 60075
diff changeset
    16
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, View, Debug, PerspectiveManager}
65248
wenzelm
parents: 65247
diff changeset
    17
import org.gjt.sp.jedit.textarea.JEditTextArea
50362
wenzelm
parents: 50344
diff changeset
    18
import org.gjt.sp.jedit.syntax.ModeProvider
49100
e7aecf2a5fc4 prefer old startup dialog scheme (cf. 514bb82514df);
wenzelm
parents: 49099
diff changeset
    19
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
43443
5d9693c2337e basic support for extended syntax styles: sub/superscript;
wenzelm
parents: 43390
diff changeset
    20
import org.gjt.sp.util.SyntaxUtilities
62228
6dfe5b12c5b2 report error on internal channel as well: startup_failure dialog may be too late;
wenzelm
parents: 62227
diff changeset
    21
import org.gjt.sp.util.Log
39241
e9a442606db3 Isabelle.load_icon with some sanity checks;
wenzelm
parents: 39167
diff changeset
    22
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    23
50205
788c8263e634 renamed main plugin object to PIDE;
wenzelm
parents: 50183
diff changeset
    24
object PIDE
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    25
{
65248
wenzelm
parents: 65247
diff changeset
    26
  /* semantic document content */
55621
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    27
62062
ee610059b0e9 fewer use of GUI_Thread.now to reduce danger of deadlock on shutdown;
wenzelm
parents: 62051
diff changeset
    28
  def snapshot(view: View): Document.Snapshot = GUI_Thread.now
55621
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    29
  {
64813
7283f41d05ab manage buffer models as explicit global state;
wenzelm
parents: 64621
diff changeset
    30
    Document_Model.get(view.getBuffer) match {
55621
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    31
      case Some(model) => model.snapshot
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    32
      case None => error("No document model for current buffer")
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    33
    }
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    34
  }
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    35
64621
7116f2634e32 clarified module name;
wenzelm
parents: 64602
diff changeset
    36
  def rendering(view: View): JEdit_Rendering = GUI_Thread.now
55621
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    37
  {
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    38
    val text_area = view.getTextArea
64882
c3b42ac0cf81 tuned signature;
wenzelm
parents: 64858
diff changeset
    39
    Document_View.get(text_area) match {
55621
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    40
      case Some(doc_view) => doc_view.get_rendering()
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    41
      case None => error("No document view for current text area")
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    42
    }
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    43
  }
65277
wenzelm
parents: 65275
diff changeset
    44
wenzelm
parents: 65275
diff changeset
    45
wenzelm
parents: 65275
diff changeset
    46
  /* plugin instance */
wenzelm
parents: 65275
diff changeset
    47
wenzelm
parents: 65275
diff changeset
    48
  @volatile var _plugin: Plugin = null
wenzelm
parents: 65275
diff changeset
    49
wenzelm
parents: 65275
diff changeset
    50
  def plugin: Plugin =
wenzelm
parents: 65275
diff changeset
    51
    if (_plugin == null) error("Uninitialized Isabelle/jEdit plugin")
wenzelm
parents: 65275
diff changeset
    52
    else _plugin
wenzelm
parents: 65275
diff changeset
    53
wenzelm
parents: 65275
diff changeset
    54
  def options: JEdit_Options = plugin.options
wenzelm
parents: 65275
diff changeset
    55
  def resources: JEdit_Resources = plugin.resources
wenzelm
parents: 65275
diff changeset
    56
  def session: Session = plugin.session
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
    57
66083
wenzelm
parents: 66082
diff changeset
    58
  object editor extends JEdit_Editor
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    59
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    60
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    61
class Plugin extends EBPlugin
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    62
{
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    63
  /* options */
65238
02037d14cb42 more robust early initialization;
wenzelm
parents: 65236
diff changeset
    64
65257
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
    65
  private var _options: JEdit_Options = null
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    66
  private def init_options(): Unit = _options = new JEdit_Options(Options.init())
65257
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
    67
  def options: JEdit_Options = _options
65239
509a9b0ad02e avoid global variables with implicit initialization;
wenzelm
parents: 65238
diff changeset
    68
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    69
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    70
  /* resources */
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    71
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    72
  private var _resources: JEdit_Resources = null
66572
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
    73
  private def init_resources(): Unit = _resources = JEdit_Resources(options.value)
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    74
  def resources: JEdit_Resources = _resources
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    75
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    76
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    77
  /* session */
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    78
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    79
  private var _session: Session = null
65264
7e6ecd04b5fe dynamic session_options for tuning parameters and initial prover options;
wenzelm
parents: 65263
diff changeset
    80
  private def init_session(): Unit = _session = new Session(options.value, resources)
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    81
  def session: Session = _session
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    82
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    83
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    84
  /* misc support */
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    85
65239
509a9b0ad02e avoid global variables with implicit initialization;
wenzelm
parents: 65238
diff changeset
    86
  val completion_history = new Completion.History_Variable
509a9b0ad02e avoid global variables with implicit initialization;
wenzelm
parents: 65238
diff changeset
    87
  val spell_checker = new Spell_Checker_Variable
509a9b0ad02e avoid global variables with implicit initialization;
wenzelm
parents: 65238
diff changeset
    88
65238
02037d14cb42 more robust early initialization;
wenzelm
parents: 65236
diff changeset
    89
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
    90
  /* global changes */
55791
5821b1937fa5 clarified init_models: simultaneous initialization of all document models, before flushing edits by regular means (via PIDE.editor.invoke) -- important for consolidated doc_blobs when determining initial edits;
wenzelm
parents: 55784
diff changeset
    91
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
    92
  def options_changed()
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
    93
  {
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    94
    session.global_options.post(Session.Global_Options(options.value))
56770
e160ae47db94 mane delayed events outside of Swing thread -- triggers no longer require Swing_Thread.later;
wenzelm
parents: 56715
diff changeset
    95
    delay_load.invoke()
55791
5821b1937fa5 clarified init_models: simultaneous initialization of all document models, before flushing edits by regular means (via PIDE.editor.invoke) -- important for consolidated doc_blobs when determining initial edits;
wenzelm
parents: 55784
diff changeset
    96
  }
5821b1937fa5 clarified init_models: simultaneous initialization of all document models, before flushing edits by regular means (via PIDE.editor.invoke) -- important for consolidated doc_blobs when determining initial edits;
wenzelm
parents: 55784
diff changeset
    97
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
    98
  def deps_changed()
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
    99
  {
56770
e160ae47db94 mane delayed events outside of Swing thread -- triggers no longer require Swing_Thread.later;
wenzelm
parents: 56715
diff changeset
   100
    delay_load.invoke()
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
   101
  }
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
   102
55791
5821b1937fa5 clarified init_models: simultaneous initialization of all document models, before flushing edits by regular means (via PIDE.editor.invoke) -- important for consolidated doc_blobs when determining initial edits;
wenzelm
parents: 55784
diff changeset
   103
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents: 44574
diff changeset
   104
  /* theory files */
44574
24444588fddd actual auto loading of required files;
wenzelm
parents: 44573
diff changeset
   105
62068
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   106
  lazy val delay_init =
65249
c3ee88b9c3cb tuned signature;
wenzelm
parents: 65248
diff changeset
   107
    GUI_Thread.delay_last(options.seconds("editor_load_delay"))
50344
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50209
diff changeset
   108
    {
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   109
      init_models()
50344
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50209
diff changeset
   110
    }
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50209
diff changeset
   111
57582
b79b75f92604 avoid delay_load overrun;
wenzelm
parents: 56834
diff changeset
   112
  private val delay_load_active = Synchronized(false)
b79b75f92604 avoid delay_load overrun;
wenzelm
parents: 56834
diff changeset
   113
  private def delay_load_activated(): Boolean =
b79b75f92604 avoid delay_load overrun;
wenzelm
parents: 56834
diff changeset
   114
    delay_load_active.guarded_access(a => Some((!a, true)))
62068
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   115
  private def delay_load_action()
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   116
  {
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   117
    if (Isabelle.continuous_checking && delay_load_activated() &&
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   118
        PerspectiveManager.isPerspectiveEnabled)
44574
24444588fddd actual auto loading of required files;
wenzelm
parents: 44573
diff changeset
   119
    {
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   120
      if (JEdit_Lib.jedit_buffers().exists(_.isLoading)) delay_load.invoke()
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   121
      else {
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   122
        val required_files =
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   123
        {
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   124
          val models = Document_Model.get_models()
44574
24444588fddd actual auto loading of required files;
wenzelm
parents: 44573
diff changeset
   125
62068
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   126
          val thys =
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   127
            (for ((node_name, model) <- models.iterator if model.is_theory)
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   128
              yield (node_name, Position.none)).toList
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65329
diff changeset
   129
          val thy_files = resources.thy_info.dependencies(thys).deps.map(_.name)
46761
b0a797158e34 avoid buffer loading overrun;
wenzelm
parents: 46740
diff changeset
   130
64839
842163abfc0d tuned signature;
wenzelm
parents: 64838
diff changeset
   131
          val aux_files =
65249
c3ee88b9c3cb tuned signature;
wenzelm
parents: 65248
diff changeset
   132
            if (options.bool("jedit_auto_resolve")) {
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   133
              val stable_tip_version =
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   134
                if (models.forall(p => p._2.is_stable))
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   135
                  session.current_state().stable_tip_version
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   136
                else None
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   137
              stable_tip_version match {
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   138
                case Some(version) => resources.undefined_blobs(version.nodes)
64478
812c22e556b9 more robust jedit_auto_resolve: avoid losing events deps_changed() / delay_load.invoke();
wenzelm
parents: 63754
diff changeset
   139
                case None => delay_load.invoke(); Nil
57582
b79b75f92604 avoid delay_load overrun;
wenzelm
parents: 56834
diff changeset
   140
              }
62068
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   141
            }
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   142
            else Nil
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   143
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   144
          (thy_files ::: aux_files).filterNot(models.isDefinedAt(_))
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   145
        }
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   146
        if (required_files.nonEmpty) {
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   147
          try {
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   148
            Standard_Thread.fork("resolve_dependencies") {
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   149
              val loaded_files =
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   150
                for {
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   151
                  name <- required_files
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   152
                  text <- resources.read_file_content(name)
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   153
                } yield (name, text)
62068
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   154
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   155
              GUI_Thread.later {
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   156
                try {
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   157
                  Document_Model.provide_files(session, loaded_files)
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   158
                  delay_init.invoke()
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   159
                }
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   160
                finally { delay_load_active.change(_ => false) }
53715
68c664737d04 added option "jedit_auto_load";
wenzelm
parents: 53487
diff changeset
   161
              }
53074
e62c7a4b6697 load_theories if continuous_checking;
wenzelm
parents: 52974
diff changeset
   162
            }
46761
b0a797158e34 avoid buffer loading overrun;
wenzelm
parents: 46740
diff changeset
   163
          }
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   164
          catch { case _: Throwable => delay_load_active.change(_ => false) }
46761
b0a797158e34 avoid buffer loading overrun;
wenzelm
parents: 46740
diff changeset
   165
        }
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   166
        else delay_load_active.change(_ => false)
44606
b625650aa2db improved auto loading: selectable file list;
wenzelm
parents: 44580
diff changeset
   167
      }
44574
24444588fddd actual auto loading of required files;
wenzelm
parents: 44573
diff changeset
   168
    }
62068
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   169
  }
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   170
64478
812c22e556b9 more robust jedit_auto_resolve: avoid losing events deps_changed() / delay_load.invoke();
wenzelm
parents: 63754
diff changeset
   171
  private lazy val delay_load =
65249
c3ee88b9c3cb tuned signature;
wenzelm
parents: 65248
diff changeset
   172
    GUI_Thread.delay_last(options.seconds("editor_load_delay")) { delay_load_action() }
44573
51f8895b9ad9 some dialog for auto loading of required files (still inactive);
wenzelm
parents: 44434
diff changeset
   173
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
   174
  private def file_watcher_action(changed: Set[JFile]): Unit =
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
   175
    if (Document_Model.sync_files(changed)) PIDE.editor.invoke_generated()
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
   176
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
   177
  lazy val file_watcher: File_Watcher =
65249
c3ee88b9c3cb tuned signature;
wenzelm
parents: 65248
diff changeset
   178
    File_Watcher(file_watcher_action _, options.seconds("editor_load_delay"))
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
   179
44574
24444588fddd actual auto loading of required files;
wenzelm
parents: 44573
diff changeset
   180
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   181
  /* session phase */
44573
51f8895b9ad9 some dialog for auto loading of required files (still inactive);
wenzelm
parents: 44434
diff changeset
   182
65226
wenzelm
parents: 65222
diff changeset
   183
  val session_phase_changed: Session.Phase => Unit =
wenzelm
parents: 65222
diff changeset
   184
  {
65317
b9f5cd845616 more informative session result;
wenzelm
parents: 65277
diff changeset
   185
    case Session.Terminated(result) if !result.ok =>
65226
wenzelm
parents: 65222
diff changeset
   186
      GUI_Thread.later {
65267
7e427dff15dd clarified message;
wenzelm
parents: 65266
diff changeset
   187
        GUI.error_dialog(jEdit.getActiveView, "Prover process terminated with error",
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   188
          "Isabelle Syslog", GUI.scrollable_text(session.syslog_content()))
65226
wenzelm
parents: 65222
diff changeset
   189
      }
39735
969ede84aac0 more uniform init/exit model/view in session_manager, trading race wrt. session.phase for race wrt. global editor state;
wenzelm
parents: 39702
diff changeset
   190
65226
wenzelm
parents: 65222
diff changeset
   191
    case Session.Ready =>
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   192
      init_models()
57640
0a28cf866d5d reconfirm continuous checking on startup, to address common trap of disabling it accidentally;
wenzelm
parents: 57612
diff changeset
   193
65226
wenzelm
parents: 65222
diff changeset
   194
      if (!Isabelle.continuous_checking) {
wenzelm
parents: 65222
diff changeset
   195
        GUI_Thread.later {
wenzelm
parents: 65222
diff changeset
   196
          val answer =
wenzelm
parents: 65222
diff changeset
   197
            GUI.confirm_dialog(jEdit.getActiveView,
wenzelm
parents: 65222
diff changeset
   198
              "Continuous checking of PIDE document",
wenzelm
parents: 65222
diff changeset
   199
              JOptionPane.YES_NO_OPTION,
wenzelm
parents: 65222
diff changeset
   200
              "Continuous checking is presently disabled:",
wenzelm
parents: 65222
diff changeset
   201
              "editor buffers will remain inactive!",
wenzelm
parents: 65222
diff changeset
   202
              "Enable continuous checking now?")
wenzelm
parents: 65222
diff changeset
   203
          if (answer == 0) Isabelle.continuous_checking = true
57640
0a28cf866d5d reconfirm continuous checking on startup, to address common trap of disabling it accidentally;
wenzelm
parents: 57612
diff changeset
   204
        }
65226
wenzelm
parents: 65222
diff changeset
   205
      }
57640
0a28cf866d5d reconfirm continuous checking on startup, to address common trap of disabling it accidentally;
wenzelm
parents: 57612
diff changeset
   206
65226
wenzelm
parents: 65222
diff changeset
   207
      delay_load.invoke()
44573
51f8895b9ad9 some dialog for auto loading of required files (still inactive);
wenzelm
parents: 44434
diff changeset
   208
65226
wenzelm
parents: 65222
diff changeset
   209
    case Session.Shutdown =>
wenzelm
parents: 65222
diff changeset
   210
      GUI_Thread.later {
wenzelm
parents: 65222
diff changeset
   211
        delay_load.revoke()
wenzelm
parents: 65222
diff changeset
   212
        delay_init.revoke()
66458
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66351
diff changeset
   213
        PIDE.editor.shutdown()
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   214
        exit_models(JEdit_Lib.jedit_buffers().toList)
65226
wenzelm
parents: 65222
diff changeset
   215
      }
46740
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46204
diff changeset
   216
65226
wenzelm
parents: 65222
diff changeset
   217
    case _ =>
wenzelm
parents: 65222
diff changeset
   218
  }
39630
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 39628
diff changeset
   219
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 39628
diff changeset
   220
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   221
  /* document model and view */
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   222
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   223
  def exit_models(buffers: List[Buffer])
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   224
  {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   225
    GUI_Thread.now {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   226
      buffers.foreach(buffer =>
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   227
        JEdit_Lib.buffer_lock(buffer) {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   228
          JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   229
          Document_Model.exit(buffer)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   230
        })
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   231
      }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   232
  }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   233
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   234
  def init_models()
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   235
  {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   236
    GUI_Thread.now {
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
   237
      PIDE.editor.flush()
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   238
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   239
      for {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   240
        buffer <- JEdit_Lib.jedit_buffers()
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   241
        if buffer != null && !buffer.getBooleanProperty(Buffer.GZIPPED)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   242
      } {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   243
        if (buffer.isLoaded) {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   244
          JEdit_Lib.buffer_lock(buffer) {
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   245
            val node_name = resources.node_name(buffer)
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   246
            val model = Document_Model.init(session, node_name, buffer)
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   247
            for {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   248
              text_area <- JEdit_Lib.jedit_text_areas(buffer)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   249
              if Document_View.get(text_area).map(_.model) != Some(model)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   250
            } Document_View.init(model, text_area)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   251
          }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   252
        }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   253
        else delay_init.invoke()
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   254
      }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   255
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
   256
      PIDE.editor.invoke_generated()
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   257
    }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   258
  }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   259
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   260
  def init_view(buffer: Buffer, text_area: JEditTextArea): Unit =
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   261
    GUI_Thread.now {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   262
      JEdit_Lib.buffer_lock(buffer) {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   263
        Document_Model.get(buffer) match {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   264
          case Some(model) => Document_View.init(model, text_area)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   265
          case None =>
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   266
        }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   267
      }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   268
    }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   269
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   270
  def exit_view(buffer: Buffer, text_area: JEditTextArea): Unit =
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   271
    GUI_Thread.now {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   272
      JEdit_Lib.buffer_lock(buffer) {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   273
        Document_View.exit(text_area)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   274
      }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   275
    }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   276
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   277
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   278
  /* main plugin plumbing */
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
   279
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   280
  @volatile private var startup_failure: Option[Throwable] = None
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   281
  @volatile private var startup_notified = false
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   282
66459
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   283
  private def init_view(view: View)
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   284
  {
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   285
    Session_Build.check_dialog(view)
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   286
    Keymap_Merge.check_dialog(view)
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   287
  }
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   288
34767
8dd19c5f8c56 added properties_changed event bus;
wenzelm
parents: 34764
diff changeset
   289
  override def handleMessage(message: EBMessage)
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   290
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57611
diff changeset
   291
    GUI_Thread.assert {}
39630
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 39628
diff changeset
   292
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   293
    if (startup_failure.isDefined && !startup_notified) {
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   294
      message match {
49100
e7aecf2a5fc4 prefer old startup dialog scheme (cf. 514bb82514df);
wenzelm
parents: 49099
diff changeset
   295
        case msg: EditorStarted =>
51616
949e2cf02a3d tuned signature -- concentrate GUI tools;
wenzelm
parents: 51614
diff changeset
   296
          GUI.error_dialog(null, "Isabelle plugin startup failure",
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   297
            GUI.scrollable_text(Exn.message(startup_failure.get)),
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   298
            "Prover IDE inactive!")
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   299
          startup_notified = true
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   300
        case _ =>
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   301
      }
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   302
    }
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   303
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   304
    if (startup_failure.isEmpty) {
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   305
      message match {
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   306
        case msg: EditorStarted =>
57649
a43898f76ae9 further distinction of Isabelle distribution: alert for identified release candidates;
wenzelm
parents: 57640
diff changeset
   307
          if (Distribution.is_identified && !Distribution.is_official) {
57680
ba206aa2ad39 tuned message;
wenzelm
parents: 57649
diff changeset
   308
            GUI.warning_dialog(jEdit.getActiveView, "Isabelle version for testing",
60206
18267ceb10b5 tuned whitespace;
wenzelm
parents: 60205
diff changeset
   309
              "This is " + Distribution.version + ".",
57649
a43898f76ae9 further distinction of Isabelle distribution: alert for identified release candidates;
wenzelm
parents: 57640
diff changeset
   310
              "It is for testing only, not for production use.")
a43898f76ae9 further distinction of Isabelle distribution: alert for identified release candidates;
wenzelm
parents: 57640
diff changeset
   311
          }
a43898f76ae9 further distinction of Isabelle distribution: alert for identified release candidates;
wenzelm
parents: 57640
diff changeset
   312
66572
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
   313
          if (resources.session_errors.nonEmpty) {
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
   314
            GUI.warning_dialog(jEdit.getActiveView,
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
   315
              "Bad session structure: may cause problems with theory imports",
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
   316
              GUI.scrollable_text(cat_lines(resources.session_errors)))
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
   317
          }
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
   318
64602
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 64524
diff changeset
   319
          val view = jEdit.getActiveView()
66459
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   320
          init_view(view)
64602
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 64524
diff changeset
   321
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
   322
          PIDE.editor.hyperlink_position(true, Document.Snapshot.init,
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65249
diff changeset
   323
            JEdit_Sessions.session_info(options.value).open_root).foreach(_.follow(view))
61277
c9152a195899 build session within running jEdit;
wenzelm
parents: 61024
diff changeset
   324
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   325
        case msg: BufferUpdate
64818
67a0a563d2b3 clarified buffer events: exit model while loading;
wenzelm
parents: 64817
diff changeset
   326
        if msg.getWhat == BufferUpdate.LOAD_STARTED || msg.getWhat == BufferUpdate.CLOSING =>
64838
ae6c51dcba9c avoid immediate editor.flush on buffer events;
wenzelm
parents: 64835
diff changeset
   327
          if (msg.getBuffer != null) {
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   328
            exit_models(List(msg.getBuffer))
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
   329
            PIDE.editor.invoke_generated()
64838
ae6c51dcba9c avoid immediate editor.flush on buffer events;
wenzelm
parents: 64835
diff changeset
   330
          }
57611
b6256ea3b7c5 proper change of perspective for removed nodes (stemming from closed buffers);
wenzelm
parents: 57582
diff changeset
   331
64818
67a0a563d2b3 clarified buffer events: exit model while loading;
wenzelm
parents: 64817
diff changeset
   332
        case msg: BufferUpdate
67a0a563d2b3 clarified buffer events: exit model while loading;
wenzelm
parents: 64817
diff changeset
   333
        if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED || msg.getWhat == BufferUpdate.LOADED =>
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   334
          if (session.is_ready) {
55791
5821b1937fa5 clarified init_models: simultaneous initialization of all document models, before flushing edits by regular means (via PIDE.editor.invoke) -- important for consolidated doc_blobs when determining initial edits;
wenzelm
parents: 55784
diff changeset
   335
            delay_init.invoke()
5821b1937fa5 clarified init_models: simultaneous initialization of all document models, before flushing edits by regular means (via PIDE.editor.invoke) -- important for consolidated doc_blobs when determining initial edits;
wenzelm
parents: 55784
diff changeset
   336
            delay_load.invoke()
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   337
          }
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34782
diff changeset
   338
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   339
        case msg: EditPaneUpdate
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 60206
diff changeset
   340
        if msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   341
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   342
            msg.getWhat == EditPaneUpdate.CREATED ||
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 60206
diff changeset
   343
            msg.getWhat == EditPaneUpdate.DESTROYED =>
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   344
          val edit_pane = msg.getEditPane
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   345
          val buffer = edit_pane.getBuffer
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   346
          val text_area = edit_pane.getTextArea
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   347
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   348
          if (buffer != null && text_area != null) {
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   349
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   350
                msg.getWhat == EditPaneUpdate.CREATED) {
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   351
              if (session.is_ready)
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   352
                init_view(buffer, text_area)
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   353
            }
52867
8d8cb75ab20a more central Pretty_Tooltip.dismissed_all -- avoid spurious crash of Rich_Text_Area.robust_body in asynchronous mouse_motion_listener;
wenzelm
parents: 52816
diff changeset
   354
            else {
65240
fe5a96240749 clarified modules;
wenzelm
parents: 65239
diff changeset
   355
              Isabelle.dismissed_popups(text_area.getView)
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   356
              exit_view(buffer, text_area)
52867
8d8cb75ab20a more central Pretty_Tooltip.dismissed_all -- avoid spurious crash of Rich_Text_Area.robust_body in asynchronous mouse_motion_listener;
wenzelm
parents: 52816
diff changeset
   357
            }
53274
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   358
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   359
            if (msg.getWhat == EditPaneUpdate.CREATED)
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   360
              Completion_Popup.Text_Area.init(text_area)
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   361
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   362
            if (msg.getWhat == EditPaneUpdate.DESTROYED)
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   363
              Completion_Popup.Text_Area.exit(text_area)
43510
17d431c92575 init/exit model/view synchronously within the swing thread -- EditBus.send in jedit-4.4.1 always runs there;
wenzelm
parents: 43487
diff changeset
   364
          }
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34782
diff changeset
   365
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   366
        case msg: PropertiesChanged =>
62264
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   367
          for {
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   368
            view <- JEdit_Lib.jedit_views
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   369
            edit_pane <- JEdit_Lib.jedit_edit_panes(view)
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   370
          } {
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   371
            val buffer = edit_pane.getBuffer
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   372
            val text_area = edit_pane.getTextArea
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   373
            if (buffer != null && text_area != null) init_view(buffer, text_area)
62264
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   374
          }
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   375
65249
c3ee88b9c3cb tuned signature;
wenzelm
parents: 65248
diff changeset
   376
          spell_checker.update(options.value)
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   377
          session.update_options(options.value)
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34782
diff changeset
   378
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   379
        case _ =>
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   380
      }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   381
    }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   382
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   383
65260
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   384
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   385
  /* mode provider */
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   386
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   387
  private var orig_mode_provider: ModeProvider = null
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   388
  private var pide_mode_provider: ModeProvider = null
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   389
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   390
  def init_mode_provider()
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   391
  {
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   392
    orig_mode_provider = ModeProvider.instance
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   393
    if (orig_mode_provider.isInstanceOf[ModeProvider]) {
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   394
      pide_mode_provider = new Token_Markup.Mode_Provider(orig_mode_provider)
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   395
      ModeProvider.instance = pide_mode_provider
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   396
    }
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   397
  }
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   398
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   399
  def exit_mode_provider()
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   400
  {
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   401
    if (ModeProvider.instance == pide_mode_provider)
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   402
      ModeProvider.instance = orig_mode_provider
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   403
  }
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   404
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   405
66019
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   406
  /* HTTP server */
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   407
66351
95847ffa62dc tuned signature;
wenzelm
parents: 66083
diff changeset
   408
  val http_root: String = "/" + Library.UUID()
66019
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   409
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   410
  val http_server: HTTP.Server = HTTP.server(Document_Model.http_handlers(http_root))
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   411
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   412
65260
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   413
  /* start and stop */
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   414
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   415
  override def start()
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   416
  {
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   417
    Debug.DISABLE_SEARCH_DIALOG_POOL = true
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   418
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   419
65257
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   420
    /* strict initialization */
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   421
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   422
    init_options()
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   423
    init_resources()
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   424
    init_session()
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   425
    PIDE._plugin = this
65257
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   426
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   427
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   428
    /* non-strict initialization */
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   429
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   430
    try {
65242
wenzelm
parents: 65241
diff changeset
   431
      completion_history.load()
65249
c3ee88b9c3cb tuned signature;
wenzelm
parents: 65248
diff changeset
   432
      spell_checker.update(options.value)
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 49195
diff changeset
   433
65261
034c49653a8e clarified initialization;
wenzelm
parents: 65260
diff changeset
   434
      SyntaxUtilities.setStyleExtender(Syntax_Style.Extender)
65260
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   435
      init_mode_provider()
53274
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   436
      JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.init _)
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   437
66019
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   438
      http_server.start
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   439
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   440
      startup_failure = None
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   441
    }
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   442
    catch {
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   443
      case exn: Throwable =>
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   444
        startup_failure = Some(exn)
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   445
        startup_notified = false
62228
6dfe5b12c5b2 report error on internal channel as well: startup_failure dialog may be too late;
wenzelm
parents: 62227
diff changeset
   446
        Log.log(Log.ERROR, this, exn)
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   447
    }
66459
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   448
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   449
    val view = jEdit.getActiveView()
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   450
    if (view != null) init_view(view)
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   451
  }
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   452
39628
f6e82967b5cd Plugin.stop: refrain from invalidating Isabelle.session -- some actors/dockables out there might still refer to it;
wenzelm
parents: 39518
diff changeset
   453
  override def stop()
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   454
  {
66019
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   455
    http_server.stop
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   456
65261
034c49653a8e clarified initialization;
wenzelm
parents: 65260
diff changeset
   457
    exit_mode_provider()
53274
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   458
    JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.exit _)
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   459
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   460
    if (startup_failure.isEmpty) {
65249
c3ee88b9c3cb tuned signature;
wenzelm
parents: 65248
diff changeset
   461
      options.value.save_prefs()
65242
wenzelm
parents: 65241
diff changeset
   462
      completion_history.value.save()
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53277
diff changeset
   463
    }
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 49195
diff changeset
   464
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   465
    exit_models(JEdit_Lib.jedit_buffers().toList)
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   466
    session.stop()
65245
e955b33f432c proper plugin access;
wenzelm
parents: 65244
diff changeset
   467
    file_watcher.shutdown()
66458
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66351
diff changeset
   468
    PIDE.editor.shutdown()
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   469
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   470
}