src/Tools/jEdit/src/plugin.scala
author wenzelm
Wed Jul 31 19:59:14 2013 +0200 (2013-07-31)
changeset 52815 eaad5fe7bb1b
parent 52807 b859a180936b
child 52816 c608e0ade554
permissions -rw-r--r--
actions and shortcuts to change node_required status, with painter that looks like CheckBox (non-clickable);
just one module for Isabelle/jEdit actions;
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@50709
    16
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, View, Debug}
wenzelm@39043
    17
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@50362
    18
import org.gjt.sp.jedit.syntax.ModeProvider
wenzelm@49100
    19
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@34429
    20
wenzelm@43443
    21
import org.gjt.sp.util.SyntaxUtilities
wenzelm@39241
    22
wenzelm@44721
    23
import scala.actors.Actor._
wenzelm@39630
    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@49245
    31
wenzelm@49099
    32
  @volatile var startup_failure: Option[Throwable] = None
wenzelm@49099
    33
  @volatile var startup_notified = false
wenzelm@34784
    34
wenzelm@49101
    35
  @volatile var plugin: Plugin = null
wenzelm@49101
    36
  @volatile var session: Session = new Session(new JEdit_Thy_Load(Set.empty, Outer_Syntax.empty))
wenzelm@49101
    37
wenzelm@52815
    38
  def options_changed() { session.global_options.event(Session.Global_Options(options.value)) }
wenzelm@52815
    39
wenzelm@48870
    40
  def thy_load(): JEdit_Thy_Load =
wenzelm@48870
    41
    session.thy_load.asInstanceOf[JEdit_Thy_Load]
wenzelm@44955
    42
wenzelm@48884
    43
  def get_recent_syntax(): Option[Outer_Syntax] =
wenzelm@48884
    44
  {
wenzelm@48884
    45
    val current_session = session
wenzelm@49101
    46
    if (current_session.recent_syntax == Outer_Syntax.empty) None
wenzelm@49101
    47
    else Some(current_session.recent_syntax)
wenzelm@48884
    48
  }
wenzelm@48884
    49
wenzelm@34784
    50
wenzelm@43449
    51
  /* document model and view */
wenzelm@43449
    52
wenzelm@43449
    53
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@43449
    54
  def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
    55
wenzelm@44379
    56
  def document_views(buffer: Buffer): List[Document_View] =
wenzelm@44379
    57
    for {
wenzelm@49406
    58
      text_area <- JEdit_Lib.jedit_text_areas(buffer).toList
wenzelm@46997
    59
      doc_view = document_view(text_area)
wenzelm@44379
    60
      if doc_view.isDefined
wenzelm@44379
    61
    } yield doc_view.get
wenzelm@44379
    62
wenzelm@50344
    63
  def exit_models(buffers: List[Buffer])
wenzelm@47058
    64
  {
wenzelm@50344
    65
    Swing_Thread.now {
wenzelm@50344
    66
      buffers.foreach(buffer =>
wenzelm@50344
    67
        JEdit_Lib.buffer_lock(buffer) {
wenzelm@50344
    68
          JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@50344
    69
          Document_Model.exit(buffer)
wenzelm@50344
    70
        })
wenzelm@50344
    71
      }
wenzelm@47058
    72
  }
wenzelm@47058
    73
wenzelm@50344
    74
  def init_models(buffers: List[Buffer])
wenzelm@43510
    75
  {
wenzelm@50344
    76
    Swing_Thread.now {
wenzelm@50344
    77
      val init_edits =
wenzelm@50344
    78
        (List.empty[Document.Edit_Text] /: buffers) { case (edits, buffer) =>
wenzelm@50344
    79
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@50344
    80
            val (model_edits, opt_model) =
wenzelm@50566
    81
              thy_load.buffer_node_name(buffer) match {
wenzelm@50344
    82
                case Some(node_name) =>
wenzelm@50344
    83
                  document_model(buffer) match {
wenzelm@50344
    84
                    case Some(model) if model.name == node_name => (Nil, Some(model))
wenzelm@50344
    85
                    case _ =>
wenzelm@50344
    86
                      val model = Document_Model.init(session, buffer, node_name)
wenzelm@50344
    87
                      (model.init_edits(), Some(model))
wenzelm@50344
    88
                  }
wenzelm@50344
    89
                case None => (Nil, None)
wenzelm@50344
    90
              }
wenzelm@50344
    91
            if (opt_model.isDefined) {
wenzelm@50344
    92
              for (text_area <- JEdit_Lib.jedit_text_areas(buffer)) {
wenzelm@50344
    93
                if (document_view(text_area).map(_.model) != opt_model)
wenzelm@50344
    94
                  Document_View.init(opt_model.get, text_area)
wenzelm@50344
    95
              }
wenzelm@43510
    96
            }
wenzelm@50344
    97
            model_edits ::: edits
wenzelm@50344
    98
          }
wenzelm@43510
    99
        }
wenzelm@50566
   100
      session.update(init_edits)
wenzelm@43510
   101
    }
wenzelm@43510
   102
  }
wenzelm@43510
   103
wenzelm@43510
   104
  def init_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   105
  {
wenzelm@49406
   106
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
   107
      document_model(buffer) match {
wenzelm@43510
   108
        case Some(model) => Document_View.init(model, text_area)
wenzelm@43510
   109
        case None =>
wenzelm@43510
   110
      }
wenzelm@43510
   111
    }
wenzelm@43510
   112
  }
wenzelm@43510
   113
wenzelm@43510
   114
  def exit_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   115
  {
wenzelm@49406
   116
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
   117
      Document_View.exit(text_area)
wenzelm@43510
   118
    }
wenzelm@43510
   119
  }
wenzelm@34318
   120
}
wenzelm@34318
   121
wenzelm@34429
   122
wenzelm@34618
   123
class Plugin extends EBPlugin
wenzelm@34618
   124
{
wenzelm@44577
   125
  /* theory files */
wenzelm@44574
   126
wenzelm@50344
   127
  private lazy val delay_init =
wenzelm@50344
   128
    Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@50344
   129
    {
wenzelm@50344
   130
      PIDE.init_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@50344
   131
    }
wenzelm@50344
   132
wenzelm@44573
   133
  private lazy val delay_load =
wenzelm@50207
   134
    Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@44574
   135
    {
wenzelm@44963
   136
      val view = jEdit.getActiveView()
wenzelm@44963
   137
wenzelm@49406
   138
      val buffers = JEdit_Lib.jedit_buffers().toList
wenzelm@46761
   139
      if (buffers.forall(_.isLoaded)) {
wenzelm@46761
   140
        def loaded_buffer(name: String): Boolean =
wenzelm@49406
   141
          buffers.exists(buffer => JEdit_Lib.buffer_name(buffer) == name)
wenzelm@44574
   142
wenzelm@46761
   143
        val thys =
wenzelm@50205
   144
          for (buffer <- buffers; model <- PIDE.document_model(buffer))
wenzelm@46761
   145
            yield model.name
wenzelm@46761
   146
wenzelm@50205
   147
        val thy_info = new Thy_Info(PIDE.thy_load)
wenzelm@46761
   148
        // FIXME avoid I/O in Swing thread!?!
wenzelm@51294
   149
        val files = thy_info.dependencies(thys).deps.map(_.name.node).
wenzelm@50205
   150
          filter(file => !loaded_buffer(file) && PIDE.thy_load.check_file(view, file))
wenzelm@44574
   151
wenzelm@46761
   152
        if (!files.isEmpty) {
wenzelm@46761
   153
          val files_list = new ListView(files.sorted)
wenzelm@46761
   154
          for (i <- 0 until files.length)
wenzelm@46761
   155
            files_list.selection.indices += i
wenzelm@44606
   156
wenzelm@46761
   157
          val answer =
wenzelm@51616
   158
            GUI.confirm_dialog(view,
wenzelm@46761
   159
              "Auto loading of required files",
wenzelm@46761
   160
              JOptionPane.YES_NO_OPTION,
wenzelm@46761
   161
              "The following files are required to resolve theory imports.",
wenzelm@46761
   162
              "Reload selected files now?",
wenzelm@46761
   163
              new ScrollPane(files_list))
wenzelm@46761
   164
          if (answer == 0) {
wenzelm@46761
   165
            files.foreach(file =>
wenzelm@46761
   166
              if (files_list.selection.items.contains(file))
wenzelm@46761
   167
                jEdit.openFile(null: View, file))
wenzelm@46761
   168
          }
wenzelm@46761
   169
        }
wenzelm@44606
   170
      }
wenzelm@44574
   171
    }
wenzelm@44573
   172
wenzelm@44574
   173
wenzelm@44574
   174
  /* session manager */
wenzelm@44573
   175
wenzelm@39633
   176
  private val session_manager = actor {
wenzelm@39633
   177
    loop {
wenzelm@39633
   178
      react {
wenzelm@39735
   179
        case phase: Session.Phase =>
wenzelm@39735
   180
          phase match {
wenzelm@50808
   181
            case Session.Inactive | Session.Failed =>
wenzelm@46918
   182
              Swing_Thread.later {
wenzelm@51616
   183
                GUI.error_dialog(jEdit.getActiveView, "Prover process terminated",
wenzelm@51616
   184
                    "Isabelle Syslog", GUI.scrollable_text(PIDE.session.current_syslog()))
wenzelm@39735
   185
              }
wenzelm@39735
   186
wenzelm@44573
   187
            case Session.Ready =>
wenzelm@52084
   188
              PIDE.session.update_options(PIDE.options.value)
wenzelm@50344
   189
              PIDE.init_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@49195
   190
              Swing_Thread.later { delay_load.invoke() }
wenzelm@44573
   191
wenzelm@46740
   192
            case Session.Shutdown =>
wenzelm@50344
   193
              PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@49195
   194
              Swing_Thread.later { delay_load.revoke() }
wenzelm@46740
   195
wenzelm@39735
   196
            case _ =>
wenzelm@39735
   197
          }
wenzelm@49406
   198
        case bad => java.lang.System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   199
      }
wenzelm@39630
   200
    }
wenzelm@39630
   201
  }
wenzelm@39630
   202
wenzelm@39630
   203
wenzelm@51071
   204
  /* Mac OS X application hooks */
wenzelm@51071
   205
wenzelm@51071
   206
  def handle_quit(): Boolean =
wenzelm@51071
   207
  {
wenzelm@51071
   208
    jEdit.exit(jEdit.getActiveView(), true)
wenzelm@51071
   209
    false
wenzelm@51071
   210
  }
wenzelm@51071
   211
wenzelm@51071
   212
wenzelm@34618
   213
  /* main plugin plumbing */
wenzelm@34433
   214
wenzelm@34767
   215
  override def handleMessage(message: EBMessage)
wenzelm@34618
   216
  {
wenzelm@43510
   217
    Swing_Thread.assert()
wenzelm@39630
   218
wenzelm@50205
   219
    if (PIDE.startup_failure.isDefined && !PIDE.startup_notified) {
wenzelm@49099
   220
      message match {
wenzelm@49100
   221
        case msg: EditorStarted =>
wenzelm@51616
   222
          GUI.error_dialog(null, "Isabelle plugin startup failure",
wenzelm@51616
   223
            GUI.scrollable_text(Exn.message(PIDE.startup_failure.get)),
wenzelm@49099
   224
            "Prover IDE inactive!")
wenzelm@50205
   225
          PIDE.startup_notified = true
wenzelm@49099
   226
        case _ =>
wenzelm@49099
   227
      }
wenzelm@49099
   228
    }
wenzelm@49099
   229
wenzelm@50205
   230
    if (PIDE.startup_failure.isEmpty) {
wenzelm@49099
   231
      message match {
wenzelm@49099
   232
        case msg: EditorStarted =>
wenzelm@50408
   233
          PIDE.session.start(Isabelle_Logic.session_args())
wenzelm@37557
   234
wenzelm@49099
   235
        case msg: BufferUpdate
wenzelm@49099
   236
        if msg.getWhat == BufferUpdate.LOADED || msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@50205
   237
          if (PIDE.session.is_ready) {
wenzelm@49099
   238
            val buffer = msg.getBuffer
wenzelm@50344
   239
            if (buffer != null && !buffer.isLoading) delay_init.invoke()
wenzelm@49195
   240
            Swing_Thread.later { delay_load.invoke() }
wenzelm@49099
   241
          }
wenzelm@34784
   242
wenzelm@49099
   243
        case msg: EditPaneUpdate
wenzelm@49099
   244
        if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@49099
   245
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   246
            msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@49099
   247
            msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@49099
   248
          val edit_pane = msg.getEditPane
wenzelm@49099
   249
          val buffer = edit_pane.getBuffer
wenzelm@49099
   250
          val text_area = edit_pane.getTextArea
wenzelm@49099
   251
wenzelm@49099
   252
          if (buffer != null && text_area != null) {
wenzelm@49099
   253
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   254
                msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@50205
   255
              if (PIDE.session.is_ready)
wenzelm@50205
   256
                PIDE.init_view(buffer, text_area)
wenzelm@49099
   257
            }
wenzelm@50205
   258
            else PIDE.exit_view(buffer, text_area)
wenzelm@43510
   259
          }
wenzelm@34784
   260
wenzelm@49099
   261
        case msg: PropertiesChanged =>
wenzelm@52084
   262
          PIDE.session.update_options(PIDE.options.value)
wenzelm@34784
   263
wenzelm@49099
   264
        case _ =>
wenzelm@49099
   265
      }
wenzelm@34318
   266
    }
wenzelm@34318
   267
  }
wenzelm@34318
   268
wenzelm@34618
   269
  override def start()
wenzelm@49099
   270
  {
wenzelm@49099
   271
    try {
wenzelm@50709
   272
      Debug.DISABLE_SEARCH_DIALOG_POOL = true
wenzelm@50709
   273
wenzelm@50205
   274
      PIDE.plugin = this
wenzelm@49099
   275
      Isabelle_System.init()
wenzelm@51614
   276
      Isabelle_Font.install_fonts()
wenzelm@49099
   277
wenzelm@49245
   278
      val init_options = Options.init()
wenzelm@50205
   279
      Swing_Thread.now { PIDE.options.update(init_options)  }
wenzelm@49245
   280
wenzelm@51071
   281
      if (Platform.is_macos && PIDE.options.bool("jedit_mac_adapter"))
wenzelm@51071
   282
        OSX_Adapter.set_quit_handler(this, this.getClass.getDeclaredMethod("handle_quit"))
wenzelm@51071
   283
wenzelm@49099
   284
      SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@49099
   285
      if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@49099
   286
        ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@48870
   287
wenzelm@49246
   288
      val content = Isabelle_Logic.session_content(false)
wenzelm@49099
   289
      val thy_load = new JEdit_Thy_Load(content.loaded_theories, content.syntax)
wenzelm@49524
   290
wenzelm@50205
   291
      PIDE.session = new Session(thy_load) {
wenzelm@50207
   292
        override def output_delay = PIDE.options.seconds("editor_output_delay")
wenzelm@50205
   293
        override def reparse_limit = PIDE.options.int("editor_reparse_limit")
wenzelm@49288
   294
      }
wenzelm@48870
   295
wenzelm@50205
   296
      PIDE.session.phase_changed += session_manager
wenzelm@50205
   297
      PIDE.startup_failure = None
wenzelm@49099
   298
    }
wenzelm@49099
   299
    catch {
wenzelm@49099
   300
      case exn: Throwable =>
wenzelm@50205
   301
        PIDE.startup_failure = Some(exn)
wenzelm@50205
   302
        PIDE.startup_notified = false
wenzelm@49099
   303
    }
wenzelm@34318
   304
  }
wenzelm@34618
   305
wenzelm@39628
   306
  override def stop()
wenzelm@34618
   307
  {
wenzelm@50205
   308
    if (PIDE.startup_failure.isEmpty)
wenzelm@50205
   309
      PIDE.options.value.save_prefs()
wenzelm@49245
   310
wenzelm@50205
   311
    PIDE.session.phase_changed -= session_manager
wenzelm@50344
   312
    PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@50205
   313
    PIDE.session.stop()
wenzelm@34318
   314
  }
wenzelm@34318
   315
}