src/Tools/jEdit/src/plugin.scala
author wenzelm
Sun Nov 25 21:23:20 2012 +0100 (2012-11-25)
changeset 50207 54be125d8cdc
parent 50206 6626bc5ed053
child 50208 1382ad6d4774
permissions -rw-r--r--
tuned signature;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/plugin.scala
wenzelm@36760
     2
    Author:     Makarius
wenzelm@36760
     3
wenzelm@36760
     4
Main Isabelle/jEdit plugin setup.
wenzelm@36760
     5
*/
wenzelm@34407
     6
wenzelm@34318
     7
package isabelle.jedit
wenzelm@34318
     8
wenzelm@34429
     9
wenzelm@36015
    10
import isabelle._
wenzelm@36015
    11
wenzelm@44573
    12
import javax.swing.JOptionPane
wenzelm@34318
    13
wenzelm@49246
    14
import scala.swing.{ListView, ScrollPane}
immler@34406
    15
wenzelm@49406
    16
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, View}
wenzelm@39043
    17
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@43452
    18
import org.gjt.sp.jedit.syntax.{Token => JEditToken, ModeProvider}
wenzelm@49100
    19
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@37068
    20
import org.gjt.sp.jedit.gui.DockableWindowManager
wenzelm@34429
    21
wenzelm@43443
    22
import org.gjt.sp.util.SyntaxUtilities
wenzelm@39241
    23
wenzelm@44721
    24
import scala.actors.Actor._
wenzelm@39630
    25
wenzelm@34318
    26
wenzelm@50205
    27
object PIDE
wenzelm@34618
    28
{
wenzelm@34784
    29
  /* plugin instance */
wenzelm@34784
    30
wenzelm@49245
    31
  val options = new JEdit_Options
wenzelm@49245
    32
wenzelm@49099
    33
  @volatile var startup_failure: Option[Throwable] = None
wenzelm@49099
    34
  @volatile var startup_notified = false
wenzelm@34784
    35
wenzelm@49101
    36
  @volatile var plugin: Plugin = null
wenzelm@49101
    37
  @volatile var session: Session = new Session(new JEdit_Thy_Load(Set.empty, Outer_Syntax.empty))
wenzelm@49101
    38
wenzelm@48870
    39
  def thy_load(): JEdit_Thy_Load =
wenzelm@48870
    40
    session.thy_load.asInstanceOf[JEdit_Thy_Load]
wenzelm@44955
    41
wenzelm@48884
    42
  def get_recent_syntax(): Option[Outer_Syntax] =
wenzelm@48884
    43
  {
wenzelm@48884
    44
    val current_session = session
wenzelm@49101
    45
    if (current_session.recent_syntax == Outer_Syntax.empty) None
wenzelm@49101
    46
    else Some(current_session.recent_syntax)
wenzelm@48884
    47
  }
wenzelm@48884
    48
wenzelm@34784
    49
wenzelm@43449
    50
  /* document model and view */
wenzelm@43449
    51
wenzelm@43449
    52
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@43449
    53
  def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
    54
wenzelm@44379
    55
  def document_views(buffer: Buffer): List[Document_View] =
wenzelm@44379
    56
    for {
wenzelm@49406
    57
      text_area <- JEdit_Lib.jedit_text_areas(buffer).toList
wenzelm@46997
    58
      doc_view = document_view(text_area)
wenzelm@44379
    59
      if doc_view.isDefined
wenzelm@44379
    60
    } yield doc_view.get
wenzelm@44379
    61
wenzelm@47058
    62
  def exit_model(buffer: Buffer)
wenzelm@47058
    63
  {
wenzelm@49406
    64
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@49406
    65
      JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@47058
    66
      Document_Model.exit(buffer)
wenzelm@47058
    67
    }
wenzelm@47058
    68
  }
wenzelm@47058
    69
wenzelm@43510
    70
  def init_model(buffer: Buffer)
wenzelm@43510
    71
  {
wenzelm@49406
    72
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
    73
      val opt_model =
wenzelm@49406
    74
        JEdit_Lib.buffer_node_name(buffer) match {
wenzelm@48717
    75
          case Some(node_name) =>
wenzelm@47058
    76
            document_model(buffer) match {
wenzelm@47058
    77
              case Some(model) if model.name == node_name => Some(model)
wenzelm@47058
    78
              case _ => Some(Document_Model.init(session, buffer, node_name))
wenzelm@43510
    79
            }
wenzelm@47058
    80
          case None => None
wenzelm@43510
    81
        }
wenzelm@43510
    82
      if (opt_model.isDefined) {
wenzelm@49406
    83
        for (text_area <- JEdit_Lib.jedit_text_areas(buffer)) {
wenzelm@43510
    84
          if (document_view(text_area).map(_.model) != opt_model)
wenzelm@43510
    85
            Document_View.init(opt_model.get, text_area)
wenzelm@43510
    86
        }
wenzelm@43510
    87
      }
wenzelm@43510
    88
    }
wenzelm@43510
    89
  }
wenzelm@43510
    90
wenzelm@43510
    91
  def init_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
    92
  {
wenzelm@49406
    93
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
    94
      document_model(buffer) match {
wenzelm@43510
    95
        case Some(model) => Document_View.init(model, text_area)
wenzelm@43510
    96
        case None =>
wenzelm@43510
    97
      }
wenzelm@43510
    98
    }
wenzelm@43510
    99
  }
wenzelm@43510
   100
wenzelm@43510
   101
  def exit_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   102
  {
wenzelm@49406
   103
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
   104
      Document_View.exit(text_area)
wenzelm@43510
   105
    }
wenzelm@43510
   106
  }
wenzelm@43510
   107
wenzelm@43449
   108
wenzelm@37068
   109
  /* dockable windows */
wenzelm@37068
   110
wenzelm@37068
   111
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   112
wenzelm@39515
   113
  def docked_session(view: View): Option[Session_Dockable] =
wenzelm@39515
   114
    wm(view).getDockableWindow("isabelle-session") match {
wenzelm@39515
   115
      case dockable: Session_Dockable => Some(dockable)
wenzelm@39515
   116
      case _ => None
wenzelm@39515
   117
    }
wenzelm@39515
   118
wenzelm@37068
   119
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   120
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   121
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   122
      case _ => None
wenzelm@37068
   123
    }
wenzelm@37068
   124
wenzelm@37068
   125
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   126
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   127
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   128
      case _ => None
wenzelm@37068
   129
    }
wenzelm@37068
   130
wenzelm@37068
   131
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   132
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   133
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   134
      case _ => None
wenzelm@37068
   135
    }
wenzelm@34318
   136
}
wenzelm@34318
   137
wenzelm@34429
   138
wenzelm@34618
   139
class Plugin extends EBPlugin
wenzelm@34618
   140
{
wenzelm@44577
   141
  /* theory files */
wenzelm@44574
   142
wenzelm@44573
   143
  private lazy val delay_load =
wenzelm@50207
   144
    Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@44574
   145
    {
wenzelm@44963
   146
      val view = jEdit.getActiveView()
wenzelm@44963
   147
wenzelm@49406
   148
      val buffers = JEdit_Lib.jedit_buffers().toList
wenzelm@46761
   149
      if (buffers.forall(_.isLoaded)) {
wenzelm@46761
   150
        def loaded_buffer(name: String): Boolean =
wenzelm@49406
   151
          buffers.exists(buffer => JEdit_Lib.buffer_name(buffer) == name)
wenzelm@44574
   152
wenzelm@46761
   153
        val thys =
wenzelm@50205
   154
          for (buffer <- buffers; model <- PIDE.document_model(buffer))
wenzelm@46761
   155
            yield model.name
wenzelm@46761
   156
wenzelm@50205
   157
        val thy_info = new Thy_Info(PIDE.thy_load)
wenzelm@46761
   158
        // FIXME avoid I/O in Swing thread!?!
wenzelm@49098
   159
        val files = thy_info.dependencies(true, thys).deps.map(_._1.node).
wenzelm@50205
   160
          filter(file => !loaded_buffer(file) && PIDE.thy_load.check_file(view, file))
wenzelm@44574
   161
wenzelm@46761
   162
        if (!files.isEmpty) {
wenzelm@46761
   163
          val files_list = new ListView(files.sorted)
wenzelm@46761
   164
          for (i <- 0 until files.length)
wenzelm@46761
   165
            files_list.selection.indices += i
wenzelm@44606
   166
wenzelm@46761
   167
          val answer =
wenzelm@46761
   168
            Library.confirm_dialog(view,
wenzelm@46761
   169
              "Auto loading of required files",
wenzelm@46761
   170
              JOptionPane.YES_NO_OPTION,
wenzelm@46761
   171
              "The following files are required to resolve theory imports.",
wenzelm@46761
   172
              "Reload selected files now?",
wenzelm@46761
   173
              new ScrollPane(files_list))
wenzelm@46761
   174
          if (answer == 0) {
wenzelm@46761
   175
            files.foreach(file =>
wenzelm@46761
   176
              if (files_list.selection.items.contains(file))
wenzelm@46761
   177
                jEdit.openFile(null: View, file))
wenzelm@46761
   178
          }
wenzelm@46761
   179
        }
wenzelm@44606
   180
      }
wenzelm@44574
   181
    }
wenzelm@44573
   182
wenzelm@44574
   183
wenzelm@44574
   184
  /* session manager */
wenzelm@44573
   185
wenzelm@39633
   186
  private val session_manager = actor {
wenzelm@39633
   187
    loop {
wenzelm@39633
   188
      react {
wenzelm@39735
   189
        case phase: Session.Phase =>
wenzelm@39735
   190
          phase match {
wenzelm@39735
   191
            case Session.Failed =>
wenzelm@46918
   192
              Swing_Thread.later {
wenzelm@48022
   193
                Library.error_dialog(jEdit.getActiveView, "Prover process failure",
wenzelm@50205
   194
                    "Isabelle Syslog", Library.scrollable_text(PIDE.session.current_syslog()))
wenzelm@39735
   195
              }
wenzelm@39735
   196
wenzelm@44573
   197
            case Session.Ready =>
wenzelm@50205
   198
              PIDE.session.global_options.event(Session.Global_Options(PIDE.options.value))
wenzelm@50205
   199
              JEdit_Lib.jedit_buffers.foreach(PIDE.init_model)
wenzelm@49195
   200
              Swing_Thread.later { delay_load.invoke() }
wenzelm@44573
   201
wenzelm@46740
   202
            case Session.Shutdown =>
wenzelm@50205
   203
              JEdit_Lib.jedit_buffers.foreach(PIDE.exit_model)
wenzelm@49195
   204
              Swing_Thread.later { delay_load.revoke() }
wenzelm@46740
   205
wenzelm@39735
   206
            case _ =>
wenzelm@39735
   207
          }
wenzelm@49406
   208
        case bad => java.lang.System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   209
      }
wenzelm@39630
   210
    }
wenzelm@39630
   211
  }
wenzelm@39630
   212
wenzelm@39630
   213
wenzelm@34618
   214
  /* main plugin plumbing */
wenzelm@34433
   215
wenzelm@34767
   216
  override def handleMessage(message: EBMessage)
wenzelm@34618
   217
  {
wenzelm@43510
   218
    Swing_Thread.assert()
wenzelm@39630
   219
wenzelm@50205
   220
    if (PIDE.startup_failure.isDefined && !PIDE.startup_notified) {
wenzelm@49099
   221
      message match {
wenzelm@49100
   222
        case msg: EditorStarted =>
wenzelm@49100
   223
          Library.error_dialog(null, "Isabelle plugin startup failure",
wenzelm@50205
   224
            Library.scrollable_text(Exn.message(PIDE.startup_failure.get)),
wenzelm@49099
   225
            "Prover IDE inactive!")
wenzelm@50205
   226
          PIDE.startup_notified = true
wenzelm@49099
   227
        case _ =>
wenzelm@49099
   228
      }
wenzelm@49099
   229
    }
wenzelm@49099
   230
wenzelm@50205
   231
    if (PIDE.startup_failure.isEmpty) {
wenzelm@49099
   232
      message match {
wenzelm@49099
   233
        case msg: EditorStarted =>
wenzelm@50205
   234
          if (PIDE.options.bool("jedit_auto_start"))
wenzelm@50205
   235
            PIDE.session.start(Isabelle_Logic.session_args())
wenzelm@37557
   236
wenzelm@49099
   237
        case msg: BufferUpdate
wenzelm@49099
   238
        if msg.getWhat == BufferUpdate.LOADED || msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@50205
   239
          if (PIDE.session.is_ready) {
wenzelm@49099
   240
            val buffer = msg.getBuffer
wenzelm@50205
   241
            if (buffer != null && !buffer.isLoading) PIDE.init_model(buffer)
wenzelm@49195
   242
            Swing_Thread.later { delay_load.invoke() }
wenzelm@49099
   243
          }
wenzelm@34784
   244
wenzelm@49099
   245
        case msg: EditPaneUpdate
wenzelm@49099
   246
        if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@49099
   247
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   248
            msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@49099
   249
            msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@49099
   250
          val edit_pane = msg.getEditPane
wenzelm@49099
   251
          val buffer = edit_pane.getBuffer
wenzelm@49099
   252
          val text_area = edit_pane.getTextArea
wenzelm@49099
   253
wenzelm@49099
   254
          if (buffer != null && text_area != null) {
wenzelm@49099
   255
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   256
                msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@50205
   257
              if (PIDE.session.is_ready)
wenzelm@50205
   258
                PIDE.init_view(buffer, text_area)
wenzelm@49099
   259
            }
wenzelm@50205
   260
            else PIDE.exit_view(buffer, text_area)
wenzelm@43510
   261
          }
wenzelm@34784
   262
wenzelm@49099
   263
        case msg: PropertiesChanged =>
wenzelm@50205
   264
          PIDE.session.global_options.event(Session.Global_Options(PIDE.options.value))
wenzelm@34784
   265
wenzelm@49099
   266
        case _ =>
wenzelm@49099
   267
      }
wenzelm@34318
   268
    }
wenzelm@34318
   269
  }
wenzelm@34318
   270
wenzelm@34618
   271
  override def start()
wenzelm@49099
   272
  {
wenzelm@49099
   273
    try {
wenzelm@50205
   274
      PIDE.plugin = this
wenzelm@49099
   275
      Isabelle_System.init()
wenzelm@49099
   276
      Isabelle_System.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@49099
   281
      SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@49099
   282
      if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@49099
   283
        ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@48870
   284
wenzelm@49246
   285
      val content = Isabelle_Logic.session_content(false)
wenzelm@49099
   286
      val thy_load = new JEdit_Thy_Load(content.loaded_theories, content.syntax)
wenzelm@49524
   287
wenzelm@50205
   288
      PIDE.session = new Session(thy_load) {
wenzelm@50207
   289
        override def output_delay = PIDE.options.seconds("editor_output_delay")
wenzelm@50205
   290
        override def reparse_limit = PIDE.options.int("editor_reparse_limit")
wenzelm@49288
   291
      }
wenzelm@48870
   292
wenzelm@50205
   293
      PIDE.session.phase_changed += session_manager
wenzelm@50205
   294
      PIDE.startup_failure = None
wenzelm@49099
   295
    }
wenzelm@49099
   296
    catch {
wenzelm@49099
   297
      case exn: Throwable =>
wenzelm@50205
   298
        PIDE.startup_failure = Some(exn)
wenzelm@50205
   299
        PIDE.startup_notified = false
wenzelm@49099
   300
    }
wenzelm@34318
   301
  }
wenzelm@34618
   302
wenzelm@39628
   303
  override def stop()
wenzelm@34618
   304
  {
wenzelm@50205
   305
    if (PIDE.startup_failure.isEmpty)
wenzelm@50205
   306
      PIDE.options.value.save_prefs()
wenzelm@49245
   307
wenzelm@50205
   308
    PIDE.session.phase_changed -= session_manager
wenzelm@50205
   309
    JEdit_Lib.jedit_buffers.foreach(PIDE.exit_model)
wenzelm@50205
   310
    PIDE.session.stop()
wenzelm@34318
   311
  }
wenzelm@34318
   312
}