src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Thu Dec 31 00:38:15 2009 +0100 (2009-12-31 ago)
changeset 34820 a8ba6cde13e9
parent 34808 e462572536e9
child 34821 ba83272dc4ca
permissions -rw-r--r--
basic setup for synchronous / modal (!) prover startup;
wenzelm@34407
     1
/*
wenzelm@34407
     2
 * Main Isabelle/jEdit plugin setup
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Johannes Hölzl, TU Munich
wenzelm@34407
     5
 * @author Fabian Immler, TU Munich
wenzelm@34760
     6
 * @author Makarius
wenzelm@34407
     7
 */
wenzelm@34407
     8
wenzelm@34318
     9
package isabelle.jedit
wenzelm@34318
    10
wenzelm@34429
    11
wenzelm@34784
    12
import isabelle.proofdocument.Session
wenzelm@34777
    13
wenzelm@34429
    14
import java.io.{FileInputStream, IOException}
wenzelm@34318
    15
import java.awt.Font
immler@34406
    16
import javax.swing.JScrollPane
wenzelm@34318
    17
wenzelm@34497
    18
import scala.collection.mutable
immler@34406
    19
wenzelm@34429
    20
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, EditPane, ServiceManager, View}
immler@34406
    21
import org.gjt.sp.jedit.buffer.JEditBuffer
immler@34406
    22
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@34429
    23
import org.gjt.sp.jedit.msg.{EditPaneUpdate, PropertiesChanged}
wenzelm@34820
    24
import org.gjt.sp.jedit.gui.EnhancedDialog
wenzelm@34429
    25
wenzelm@34318
    26
wenzelm@34618
    27
object Isabelle
wenzelm@34618
    28
{
wenzelm@34784
    29
  /* plugin instance */
wenzelm@34784
    30
wenzelm@34784
    31
  var plugin: Plugin = null
wenzelm@34784
    32
  var system: Isabelle_System = null
wenzelm@34784
    33
  var session: Session = null
wenzelm@34784
    34
wenzelm@34784
    35
wenzelm@34618
    36
  /* name */
wenzelm@34618
    37
wenzelm@34318
    38
  val NAME = "Isabelle"
wenzelm@34623
    39
wenzelm@34618
    40
wenzelm@34618
    41
  /* properties */
wenzelm@34618
    42
wenzelm@34618
    43
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    44
wenzelm@34618
    45
  object Property
wenzelm@34618
    46
  {
wenzelm@34618
    47
    def apply(name: String): String = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34468
    48
    def update(name: String, value: String) = jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    49
  }
wenzelm@34433
    50
wenzelm@34618
    51
  object Boolean_Property
wenzelm@34618
    52
  {
wenzelm@34618
    53
    def apply(name: String): Boolean = jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@34618
    54
    def update(name: String, value: Boolean) = jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    55
  }
wenzelm@34618
    56
wenzelm@34618
    57
  object Int_Property
wenzelm@34618
    58
  {
wenzelm@34618
    59
    def apply(name: String): Int = jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@34618
    60
    def update(name: String, value: Int) = jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    61
  }
wenzelm@34618
    62
wenzelm@34618
    63
wenzelm@34618
    64
  /* settings */
wenzelm@34618
    65
wenzelm@34782
    66
  def default_logic(): String =
wenzelm@34782
    67
  {
wenzelm@34782
    68
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
    69
    if (logic != "") logic
wenzelm@34782
    70
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
    71
  }
wenzelm@34782
    72
wenzelm@34782
    73
  def isabelle_args(): List[String] =
wenzelm@34618
    74
  {
wenzelm@34780
    75
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
    76
    val logic = {
wenzelm@34784
    77
      val logic = Property("logic")
wenzelm@34782
    78
      if (logic != null && logic != "") logic
wenzelm@34782
    79
      else default_logic()
wenzelm@34780
    80
    }
wenzelm@34780
    81
    modes ++ List(logic)
wenzelm@34502
    82
  }
wenzelm@34502
    83
wenzelm@34618
    84
wenzelm@34784
    85
  /* main jEdit components */  // FIXME ownership!?
wenzelm@34784
    86
wenzelm@34784
    87
  def jedit_buffers(): Iterator[Buffer] = Iterator.fromArray(jEdit.getBuffers())
wenzelm@34784
    88
wenzelm@34784
    89
  def jedit_views(): Iterator[View] = Iterator.fromArray(jEdit.getViews())
wenzelm@34784
    90
wenzelm@34784
    91
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@34784
    92
    Iterator.fromArray(view.getEditPanes).map(_.getTextArea)
wenzelm@34784
    93
wenzelm@34784
    94
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
    95
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
    96
wenzelm@34784
    97
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
    98
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
    99
wenzelm@34784
   100
wenzelm@34820
   101
  /* manage prover */
wenzelm@34820
   102
wenzelm@34820
   103
  private def prover_started(view: View): Boolean =
wenzelm@34820
   104
  {
wenzelm@34820
   105
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@34820
   106
    session.start(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   107
      case Some(err) =>
wenzelm@34820
   108
        // FIXME proper dialog
wenzelm@34820
   109
        val dialog = new EnhancedDialog(view, "Failed to start prover:\n" + err, true) {
wenzelm@34820
   110
          def ok { dispose }
wenzelm@34820
   111
          def cancel { dispose }
wenzelm@34820
   112
        }
wenzelm@34820
   113
        dialog.setVisible(true)
wenzelm@34820
   114
        false
wenzelm@34820
   115
      case None => true
wenzelm@34820
   116
    }
wenzelm@34820
   117
  }
wenzelm@34820
   118
wenzelm@34820
   119
wenzelm@34784
   120
  /* activation */
wenzelm@34784
   121
wenzelm@34820
   122
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   123
  {
wenzelm@34820
   124
    if (prover_started(view)) {
wenzelm@34820
   125
      val model = Document_Model.init(session, buffer)
wenzelm@34820
   126
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   127
        Document_View.init(model, text_area)
wenzelm@34820
   128
    }
wenzelm@34784
   129
  }
wenzelm@34784
   130
wenzelm@34784
   131
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   132
  {
wenzelm@34784
   133
    session.stop()  // FIXME not yet
wenzelm@34784
   134
wenzelm@34784
   135
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   136
      Document_View.exit(text_area)
wenzelm@34784
   137
    Document_Model.exit(buffer)
wenzelm@34784
   138
  }
wenzelm@34784
   139
wenzelm@34784
   140
  def switch_active(view: View) =
wenzelm@34784
   141
  {
wenzelm@34784
   142
    val buffer = view.getBuffer
wenzelm@34788
   143
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   144
    else activate_buffer(view, buffer)
wenzelm@34784
   145
  }
wenzelm@34784
   146
wenzelm@34784
   147
  def is_active(view: View): Boolean =
wenzelm@34788
   148
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   149
}
wenzelm@34318
   150
wenzelm@34429
   151
wenzelm@34618
   152
class Plugin extends EBPlugin
wenzelm@34618
   153
{
wenzelm@34618
   154
  /* main plugin plumbing */
wenzelm@34433
   155
wenzelm@34767
   156
  override def handleMessage(message: EBMessage)
wenzelm@34618
   157
  {
wenzelm@34767
   158
    message match {
wenzelm@34767
   159
      case msg: EditPaneUpdate =>
wenzelm@34784
   160
        val edit_pane = msg.getEditPane
wenzelm@34784
   161
        val buffer = edit_pane.getBuffer
wenzelm@34784
   162
        val text_area = edit_pane.getTextArea
wenzelm@34784
   163
wenzelm@34787
   164
        def init_view()
wenzelm@34787
   165
        {
wenzelm@34788
   166
          Document_Model(buffer) match {
wenzelm@34787
   167
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   168
            case None =>
wenzelm@34787
   169
          }
wenzelm@34787
   170
        }
wenzelm@34787
   171
        def exit_view()
wenzelm@34787
   172
        {
wenzelm@34788
   173
          if (Document_View(text_area).isDefined)
wenzelm@34787
   174
            Document_View.exit(text_area)
wenzelm@34787
   175
        }
wenzelm@34767
   176
        msg.getWhat match {
wenzelm@34787
   177
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   178
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   179
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   180
          case _ =>
immler@34671
   181
        }
wenzelm@34784
   182
wenzelm@34777
   183
      case msg: PropertiesChanged =>
wenzelm@34791
   184
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   185
wenzelm@34318
   186
      case _ =>
wenzelm@34318
   187
    }
wenzelm@34318
   188
  }
wenzelm@34318
   189
wenzelm@34618
   190
  override def start()
wenzelm@34618
   191
  {
wenzelm@34751
   192
    Isabelle.plugin = this
wenzelm@34615
   193
    Isabelle.system = new Isabelle_System
wenzelm@34774
   194
    Isabelle.system.install_fonts()
wenzelm@34777
   195
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   196
  }
wenzelm@34618
   197
wenzelm@34618
   198
  override def stop()
wenzelm@34618
   199
  {
wenzelm@34777
   200
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   201
    Isabelle.session = null
wenzelm@34441
   202
    Isabelle.system = null
wenzelm@34440
   203
    Isabelle.plugin = null
wenzelm@34318
   204
  }
wenzelm@34318
   205
}