src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Sat May 22 22:30:37 2010 +0200 (2010-05-22)
changeset 37068 07936a4efe93
parent 37019 8f747cee4e27
child 37164 8b4617ad1593
permissions -rw-r--r--
access statically typed dockable windows;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/plugin.scala
wenzelm@36760
     2
    Author:     Johannes Hölzl, TU Munich
wenzelm@36760
     3
    Author:     Fabian Immler, TU Munich
wenzelm@36760
     4
    Author:     Makarius
wenzelm@36760
     5
wenzelm@36760
     6
Main Isabelle/jEdit plugin setup.
wenzelm@36760
     7
*/
wenzelm@34407
     8
wenzelm@34318
     9
package isabelle.jedit
wenzelm@34318
    10
wenzelm@34429
    11
wenzelm@36015
    12
import isabelle._
wenzelm@36015
    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@37068
    24
import org.gjt.sp.jedit.gui.DockableWindowManager
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 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@36814
    46
    def apply(name: String): String =
wenzelm@36814
    47
      jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@36814
    48
    def apply(name: String, default: String): String =
wenzelm@36814
    49
      jEdit.getProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    50
    def update(name: String, value: String) =
wenzelm@36814
    51
      jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    52
  }
wenzelm@34433
    53
wenzelm@34618
    54
  object Boolean_Property
wenzelm@34618
    55
  {
wenzelm@36814
    56
    def apply(name: String): Boolean =
wenzelm@36814
    57
      jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@36814
    58
    def apply(name: String, default: Boolean): Boolean =
wenzelm@36814
    59
      jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    60
    def update(name: String, value: Boolean) =
wenzelm@36814
    61
      jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    62
  }
wenzelm@34618
    63
wenzelm@34618
    64
  object Int_Property
wenzelm@34618
    65
  {
wenzelm@36814
    66
    def apply(name: String): Int =
wenzelm@36814
    67
      jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@36814
    68
    def apply(name: String, default: Int): Int =
wenzelm@36814
    69
      jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    70
    def update(name: String, value: Int) =
wenzelm@36814
    71
      jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    72
  }
wenzelm@34618
    73
wenzelm@37019
    74
  def font_size(): Float =
wenzelm@37019
    75
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
    76
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
    77
wenzelm@34618
    78
wenzelm@34618
    79
  /* settings */
wenzelm@34618
    80
wenzelm@34782
    81
  def default_logic(): String =
wenzelm@34782
    82
  {
wenzelm@34782
    83
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
    84
    if (logic != "") logic
wenzelm@34782
    85
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
    86
  }
wenzelm@34782
    87
wenzelm@34782
    88
  def isabelle_args(): List[String] =
wenzelm@34618
    89
  {
wenzelm@34780
    90
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
    91
    val logic = {
wenzelm@34784
    92
      val logic = Property("logic")
wenzelm@34782
    93
      if (logic != null && logic != "") logic
wenzelm@34782
    94
      else default_logic()
wenzelm@34780
    95
    }
wenzelm@34780
    96
    modes ++ List(logic)
wenzelm@34502
    97
  }
wenzelm@34502
    98
wenzelm@34618
    99
wenzelm@34784
   100
  /* main jEdit components */  // FIXME ownership!?
wenzelm@34784
   101
wenzelm@34784
   102
  def jedit_buffers(): Iterator[Buffer] = Iterator.fromArray(jEdit.getBuffers())
wenzelm@34784
   103
wenzelm@34784
   104
  def jedit_views(): Iterator[View] = Iterator.fromArray(jEdit.getViews())
wenzelm@34784
   105
wenzelm@34784
   106
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@34784
   107
    Iterator.fromArray(view.getEditPanes).map(_.getTextArea)
wenzelm@34784
   108
wenzelm@34784
   109
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   110
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   111
wenzelm@34784
   112
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   113
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   114
wenzelm@34784
   115
wenzelm@37068
   116
  /* dockable windows */
wenzelm@37068
   117
wenzelm@37068
   118
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   119
wenzelm@37068
   120
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   121
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   122
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   123
      case _ => None
wenzelm@37068
   124
    }
wenzelm@37068
   125
wenzelm@37068
   126
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   127
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   128
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   129
      case _ => None
wenzelm@37068
   130
    }
wenzelm@37068
   131
wenzelm@37068
   132
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   133
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   134
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   135
      case _ => None
wenzelm@37068
   136
    }
wenzelm@37068
   137
wenzelm@37068
   138
wenzelm@34820
   139
  /* manage prover */
wenzelm@34820
   140
wenzelm@34820
   141
  private def prover_started(view: View): Boolean =
wenzelm@34820
   142
  {
wenzelm@34820
   143
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@34820
   144
    session.start(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   145
      case Some(err) =>
wenzelm@34822
   146
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   147
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   148
        false
wenzelm@34820
   149
      case None => true
wenzelm@34820
   150
    }
wenzelm@34820
   151
  }
wenzelm@34820
   152
wenzelm@34820
   153
wenzelm@34784
   154
  /* activation */
wenzelm@34784
   155
wenzelm@34820
   156
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   157
  {
wenzelm@34820
   158
    if (prover_started(view)) {
wenzelm@34820
   159
      val model = Document_Model.init(session, buffer)
wenzelm@34820
   160
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   161
        Document_View.init(model, text_area)
wenzelm@34820
   162
    }
wenzelm@34784
   163
  }
wenzelm@34784
   164
wenzelm@34784
   165
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   166
  {
wenzelm@34784
   167
    session.stop()  // FIXME not yet
wenzelm@34784
   168
wenzelm@34784
   169
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   170
      Document_View.exit(text_area)
wenzelm@34784
   171
    Document_Model.exit(buffer)
wenzelm@34784
   172
  }
wenzelm@34784
   173
wenzelm@34784
   174
  def switch_active(view: View) =
wenzelm@34784
   175
  {
wenzelm@34784
   176
    val buffer = view.getBuffer
wenzelm@34788
   177
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   178
    else activate_buffer(view, buffer)
wenzelm@34784
   179
  }
wenzelm@34784
   180
wenzelm@34784
   181
  def is_active(view: View): Boolean =
wenzelm@34788
   182
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   183
}
wenzelm@34318
   184
wenzelm@34429
   185
wenzelm@34618
   186
class Plugin extends EBPlugin
wenzelm@34618
   187
{
wenzelm@34618
   188
  /* main plugin plumbing */
wenzelm@34433
   189
wenzelm@34767
   190
  override def handleMessage(message: EBMessage)
wenzelm@34618
   191
  {
wenzelm@34767
   192
    message match {
wenzelm@34767
   193
      case msg: EditPaneUpdate =>
wenzelm@34784
   194
        val edit_pane = msg.getEditPane
wenzelm@34784
   195
        val buffer = edit_pane.getBuffer
wenzelm@34784
   196
        val text_area = edit_pane.getTextArea
wenzelm@34784
   197
wenzelm@34787
   198
        def init_view()
wenzelm@34787
   199
        {
wenzelm@34788
   200
          Document_Model(buffer) match {
wenzelm@34787
   201
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   202
            case None =>
wenzelm@34787
   203
          }
wenzelm@34787
   204
        }
wenzelm@34787
   205
        def exit_view()
wenzelm@34787
   206
        {
wenzelm@34788
   207
          if (Document_View(text_area).isDefined)
wenzelm@34787
   208
            Document_View.exit(text_area)
wenzelm@34787
   209
        }
wenzelm@34767
   210
        msg.getWhat match {
wenzelm@34787
   211
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   212
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   213
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   214
          case _ =>
immler@34671
   215
        }
wenzelm@34784
   216
wenzelm@34777
   217
      case msg: PropertiesChanged =>
wenzelm@34791
   218
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   219
wenzelm@34318
   220
      case _ =>
wenzelm@34318
   221
    }
wenzelm@34318
   222
  }
wenzelm@34318
   223
wenzelm@34618
   224
  override def start()
wenzelm@34618
   225
  {
wenzelm@34615
   226
    Isabelle.system = new Isabelle_System
wenzelm@34774
   227
    Isabelle.system.install_fonts()
wenzelm@34777
   228
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   229
  }
wenzelm@34618
   230
wenzelm@34618
   231
  override def stop()
wenzelm@34618
   232
  {
wenzelm@34777
   233
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   234
    Isabelle.session = null
wenzelm@34441
   235
    Isabelle.system = null
wenzelm@34318
   236
  }
wenzelm@34318
   237
}