src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Sat Aug 07 13:19:48 2010 +0200 (2010-08-07)
changeset 38221 e0f00f0945b4
parent 37557 1ae272fd4082
child 38222 dac5fa0ac971
permissions -rw-r--r--
misc tuning and clarification;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/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@34429
    12
import java.io.{FileInputStream, IOException}
wenzelm@34318
    13
import java.awt.Font
wenzelm@34822
    14
import javax.swing.JTextArea
wenzelm@34318
    15
wenzelm@34497
    16
import scala.collection.mutable
immler@34406
    17
wenzelm@34429
    18
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, EditPane, ServiceManager, View}
immler@34406
    19
import org.gjt.sp.jedit.buffer.JEditBuffer
immler@34406
    20
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@37557
    21
import org.gjt.sp.jedit.msg.{BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@37068
    22
import org.gjt.sp.jedit.gui.DockableWindowManager
wenzelm@34429
    23
wenzelm@34318
    24
wenzelm@34618
    25
object Isabelle
wenzelm@34618
    26
{
wenzelm@34784
    27
  /* plugin instance */
wenzelm@34784
    28
wenzelm@34784
    29
  var system: Isabelle_System = null
wenzelm@34784
    30
  var session: Session = null
wenzelm@34784
    31
wenzelm@34784
    32
wenzelm@34618
    33
  /* properties */
wenzelm@34618
    34
wenzelm@34618
    35
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    36
wenzelm@34618
    37
  object Property
wenzelm@34618
    38
  {
wenzelm@36814
    39
    def apply(name: String): String =
wenzelm@36814
    40
      jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@36814
    41
    def apply(name: String, default: String): String =
wenzelm@36814
    42
      jEdit.getProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    43
    def update(name: String, value: String) =
wenzelm@36814
    44
      jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    45
  }
wenzelm@34433
    46
wenzelm@34618
    47
  object Boolean_Property
wenzelm@34618
    48
  {
wenzelm@36814
    49
    def apply(name: String): Boolean =
wenzelm@36814
    50
      jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@36814
    51
    def apply(name: String, default: Boolean): Boolean =
wenzelm@36814
    52
      jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    53
    def update(name: String, value: Boolean) =
wenzelm@36814
    54
      jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    55
  }
wenzelm@34618
    56
wenzelm@34618
    57
  object Int_Property
wenzelm@34618
    58
  {
wenzelm@36814
    59
    def apply(name: String): Int =
wenzelm@36814
    60
      jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@36814
    61
    def apply(name: String, default: Int): Int =
wenzelm@36814
    62
      jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    63
    def update(name: String, value: Int) =
wenzelm@36814
    64
      jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    65
  }
wenzelm@34618
    66
wenzelm@37164
    67
wenzelm@37164
    68
  /* font */
wenzelm@37164
    69
wenzelm@37164
    70
  def font_family(): String = jEdit.getProperty("view.font")
wenzelm@37164
    71
wenzelm@37019
    72
  def font_size(): Float =
wenzelm@37019
    73
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
    74
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
    75
wenzelm@34618
    76
wenzelm@37201
    77
  /* tooltip markup */
wenzelm@37201
    78
wenzelm@37201
    79
  def tooltip(text: String): String =
wenzelm@37203
    80
    "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
wenzelm@37203
    81
        Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
wenzelm@37203
    82
      HTML.encode(text) + "</pre></html>"
wenzelm@37201
    83
wenzelm@37201
    84
wenzelm@34618
    85
  /* settings */
wenzelm@34618
    86
wenzelm@34782
    87
  def default_logic(): String =
wenzelm@34782
    88
  {
wenzelm@34782
    89
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
    90
    if (logic != "") logic
wenzelm@34782
    91
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
    92
  }
wenzelm@34782
    93
wenzelm@34782
    94
  def isabelle_args(): List[String] =
wenzelm@34618
    95
  {
wenzelm@34780
    96
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
    97
    val logic = {
wenzelm@34784
    98
      val logic = Property("logic")
wenzelm@34782
    99
      if (logic != null && logic != "") logic
wenzelm@34782
   100
      else default_logic()
wenzelm@34780
   101
    }
wenzelm@34780
   102
    modes ++ List(logic)
wenzelm@34502
   103
  }
wenzelm@34502
   104
wenzelm@34618
   105
wenzelm@38221
   106
  /* main jEdit components */
wenzelm@34784
   107
wenzelm@37177
   108
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   109
wenzelm@37177
   110
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   111
wenzelm@34784
   112
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   113
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   114
wenzelm@34784
   115
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   116
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   117
wenzelm@34784
   118
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   119
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   120
wenzelm@34784
   121
wenzelm@37068
   122
  /* dockable windows */
wenzelm@37068
   123
wenzelm@37068
   124
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   125
wenzelm@37068
   126
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   127
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   128
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   129
      case _ => None
wenzelm@37068
   130
    }
wenzelm@37068
   131
wenzelm@37068
   132
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   133
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   134
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   135
      case _ => None
wenzelm@37068
   136
    }
wenzelm@37068
   137
wenzelm@37068
   138
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   139
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   140
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   141
      case _ => None
wenzelm@37068
   142
    }
wenzelm@37068
   143
wenzelm@37068
   144
wenzelm@38221
   145
  /* manage prover */  // FIXME async!?
wenzelm@34820
   146
wenzelm@34820
   147
  private def prover_started(view: View): Boolean =
wenzelm@34820
   148
  {
wenzelm@34820
   149
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@38221
   150
    session.started(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   151
      case Some(err) =>
wenzelm@34822
   152
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   153
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   154
        false
wenzelm@34820
   155
      case None => true
wenzelm@34820
   156
    }
wenzelm@34820
   157
  }
wenzelm@34820
   158
wenzelm@34820
   159
wenzelm@34784
   160
  /* activation */
wenzelm@34784
   161
wenzelm@34820
   162
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   163
  {
wenzelm@34820
   164
    if (prover_started(view)) {
wenzelm@34820
   165
      val model = Document_Model.init(session, buffer)
wenzelm@34820
   166
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   167
        Document_View.init(model, text_area)
wenzelm@34820
   168
    }
wenzelm@34784
   169
  }
wenzelm@34784
   170
wenzelm@34784
   171
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   172
  {
wenzelm@34784
   173
    session.stop()  // FIXME not yet
wenzelm@34784
   174
wenzelm@34784
   175
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   176
      Document_View.exit(text_area)
wenzelm@34784
   177
    Document_Model.exit(buffer)
wenzelm@34784
   178
  }
wenzelm@34784
   179
wenzelm@34784
   180
  def switch_active(view: View) =
wenzelm@34784
   181
  {
wenzelm@34784
   182
    val buffer = view.getBuffer
wenzelm@34788
   183
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   184
    else activate_buffer(view, buffer)
wenzelm@34784
   185
  }
wenzelm@34784
   186
wenzelm@34784
   187
  def is_active(view: View): Boolean =
wenzelm@34788
   188
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   189
}
wenzelm@34318
   190
wenzelm@34429
   191
wenzelm@34618
   192
class Plugin extends EBPlugin
wenzelm@34618
   193
{
wenzelm@34618
   194
  /* main plugin plumbing */
wenzelm@34433
   195
wenzelm@34767
   196
  override def handleMessage(message: EBMessage)
wenzelm@34618
   197
  {
wenzelm@34767
   198
    message match {
wenzelm@37557
   199
      case msg: BufferUpdate
wenzelm@37557
   200
        if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@37557
   201
        Document_Model(msg.getBuffer) match {
wenzelm@37557
   202
          case Some(model) => model.refresh()
wenzelm@37557
   203
          case _ =>
wenzelm@37557
   204
        }
wenzelm@37557
   205
wenzelm@34767
   206
      case msg: EditPaneUpdate =>
wenzelm@34784
   207
        val edit_pane = msg.getEditPane
wenzelm@34784
   208
        val buffer = edit_pane.getBuffer
wenzelm@34784
   209
        val text_area = edit_pane.getTextArea
wenzelm@34784
   210
wenzelm@34787
   211
        def init_view()
wenzelm@34787
   212
        {
wenzelm@34788
   213
          Document_Model(buffer) match {
wenzelm@34787
   214
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   215
            case None =>
wenzelm@34787
   216
          }
wenzelm@34787
   217
        }
wenzelm@34787
   218
        def exit_view()
wenzelm@34787
   219
        {
wenzelm@34788
   220
          if (Document_View(text_area).isDefined)
wenzelm@34787
   221
            Document_View.exit(text_area)
wenzelm@34787
   222
        }
wenzelm@34767
   223
        msg.getWhat match {
wenzelm@34787
   224
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   225
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   226
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   227
          case _ =>
immler@34671
   228
        }
wenzelm@34784
   229
wenzelm@34777
   230
      case msg: PropertiesChanged =>
wenzelm@37241
   231
        Swing_Thread.now {
wenzelm@37241
   232
          for (text_area <- Isabelle.jedit_text_areas if Document_View(text_area).isDefined)
wenzelm@37241
   233
            Document_View(text_area).get.extend_styles()
wenzelm@37241
   234
        }
wenzelm@37241
   235
wenzelm@34791
   236
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   237
wenzelm@34318
   238
      case _ =>
wenzelm@34318
   239
    }
wenzelm@34318
   240
  }
wenzelm@34318
   241
wenzelm@34618
   242
  override def start()
wenzelm@34618
   243
  {
wenzelm@34615
   244
    Isabelle.system = new Isabelle_System
wenzelm@34774
   245
    Isabelle.system.install_fonts()
wenzelm@34777
   246
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   247
  }
wenzelm@34618
   248
wenzelm@34618
   249
  override def stop()
wenzelm@34618
   250
  {
wenzelm@34777
   251
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   252
    Isabelle.session = null
wenzelm@34441
   253
    Isabelle.system = null
wenzelm@34318
   254
  }
wenzelm@34318
   255
}