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