src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Fri May 28 21:40:32 2010 +0200 (2010-05-28)
changeset 37177 17331ca75044
parent 37164 8b4617ad1593
child 37201 8517a650cfdc
permissions -rw-r--r--
avoid deprecated Iterator.fromArray;
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@37164
    74
wenzelm@37164
    75
  /* font */
wenzelm@37164
    76
wenzelm@37164
    77
  def font_family(): String = jEdit.getProperty("view.font")
wenzelm@37164
    78
wenzelm@37019
    79
  def font_size(): Float =
wenzelm@37019
    80
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
    81
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
    82
wenzelm@34618
    83
wenzelm@34618
    84
  /* settings */
wenzelm@34618
    85
wenzelm@34782
    86
  def default_logic(): String =
wenzelm@34782
    87
  {
wenzelm@34782
    88
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
    89
    if (logic != "") logic
wenzelm@34782
    90
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
    91
  }
wenzelm@34782
    92
wenzelm@34782
    93
  def isabelle_args(): List[String] =
wenzelm@34618
    94
  {
wenzelm@34780
    95
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
    96
    val logic = {
wenzelm@34784
    97
      val logic = Property("logic")
wenzelm@34782
    98
      if (logic != null && logic != "") logic
wenzelm@34782
    99
      else default_logic()
wenzelm@34780
   100
    }
wenzelm@34780
   101
    modes ++ List(logic)
wenzelm@34502
   102
  }
wenzelm@34502
   103
wenzelm@34618
   104
wenzelm@34784
   105
  /* main jEdit components */  // FIXME ownership!?
wenzelm@34784
   106
wenzelm@37177
   107
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   108
wenzelm@37177
   109
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   110
wenzelm@34784
   111
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   112
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   113
wenzelm@34784
   114
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   115
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   116
wenzelm@34784
   117
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   118
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   119
wenzelm@34784
   120
wenzelm@37068
   121
  /* dockable windows */
wenzelm@37068
   122
wenzelm@37068
   123
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   124
wenzelm@37068
   125
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   126
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   127
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   128
      case _ => None
wenzelm@37068
   129
    }
wenzelm@37068
   130
wenzelm@37068
   131
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   132
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   133
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   134
      case _ => None
wenzelm@37068
   135
    }
wenzelm@37068
   136
wenzelm@37068
   137
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   138
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   139
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   140
      case _ => None
wenzelm@37068
   141
    }
wenzelm@37068
   142
wenzelm@37068
   143
wenzelm@34820
   144
  /* manage prover */
wenzelm@34820
   145
wenzelm@34820
   146
  private def prover_started(view: View): Boolean =
wenzelm@34820
   147
  {
wenzelm@34820
   148
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@34820
   149
    session.start(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   150
      case Some(err) =>
wenzelm@34822
   151
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   152
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   153
        false
wenzelm@34820
   154
      case None => true
wenzelm@34820
   155
    }
wenzelm@34820
   156
  }
wenzelm@34820
   157
wenzelm@34820
   158
wenzelm@34784
   159
  /* activation */
wenzelm@34784
   160
wenzelm@34820
   161
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   162
  {
wenzelm@34820
   163
    if (prover_started(view)) {
wenzelm@34820
   164
      val model = Document_Model.init(session, buffer)
wenzelm@34820
   165
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   166
        Document_View.init(model, text_area)
wenzelm@34820
   167
    }
wenzelm@34784
   168
  }
wenzelm@34784
   169
wenzelm@34784
   170
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   171
  {
wenzelm@34784
   172
    session.stop()  // FIXME not yet
wenzelm@34784
   173
wenzelm@34784
   174
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   175
      Document_View.exit(text_area)
wenzelm@34784
   176
    Document_Model.exit(buffer)
wenzelm@34784
   177
  }
wenzelm@34784
   178
wenzelm@34784
   179
  def switch_active(view: View) =
wenzelm@34784
   180
  {
wenzelm@34784
   181
    val buffer = view.getBuffer
wenzelm@34788
   182
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   183
    else activate_buffer(view, buffer)
wenzelm@34784
   184
  }
wenzelm@34784
   185
wenzelm@34784
   186
  def is_active(view: View): Boolean =
wenzelm@34788
   187
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   188
}
wenzelm@34318
   189
wenzelm@34429
   190
wenzelm@34618
   191
class Plugin extends EBPlugin
wenzelm@34618
   192
{
wenzelm@34618
   193
  /* main plugin plumbing */
wenzelm@34433
   194
wenzelm@34767
   195
  override def handleMessage(message: EBMessage)
wenzelm@34618
   196
  {
wenzelm@34767
   197
    message match {
wenzelm@34767
   198
      case msg: EditPaneUpdate =>
wenzelm@34784
   199
        val edit_pane = msg.getEditPane
wenzelm@34784
   200
        val buffer = edit_pane.getBuffer
wenzelm@34784
   201
        val text_area = edit_pane.getTextArea
wenzelm@34784
   202
wenzelm@34787
   203
        def init_view()
wenzelm@34787
   204
        {
wenzelm@34788
   205
          Document_Model(buffer) match {
wenzelm@34787
   206
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   207
            case None =>
wenzelm@34787
   208
          }
wenzelm@34787
   209
        }
wenzelm@34787
   210
        def exit_view()
wenzelm@34787
   211
        {
wenzelm@34788
   212
          if (Document_View(text_area).isDefined)
wenzelm@34787
   213
            Document_View.exit(text_area)
wenzelm@34787
   214
        }
wenzelm@34767
   215
        msg.getWhat match {
wenzelm@34787
   216
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   217
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   218
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   219
          case _ =>
immler@34671
   220
        }
wenzelm@34784
   221
wenzelm@34777
   222
      case msg: PropertiesChanged =>
wenzelm@34791
   223
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   224
wenzelm@34318
   225
      case _ =>
wenzelm@34318
   226
    }
wenzelm@34318
   227
  }
wenzelm@34318
   228
wenzelm@34618
   229
  override def start()
wenzelm@34618
   230
  {
wenzelm@34615
   231
    Isabelle.system = new Isabelle_System
wenzelm@34774
   232
    Isabelle.system.install_fonts()
wenzelm@34777
   233
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   234
  }
wenzelm@34618
   235
wenzelm@34618
   236
  override def stop()
wenzelm@34618
   237
  {
wenzelm@34777
   238
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   239
    Isabelle.session = null
wenzelm@34441
   240
    Isabelle.system = null
wenzelm@34318
   241
  }
wenzelm@34318
   242
}