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