src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Sat Aug 28 20:24:41 2010 +0200 (2010-08-28 ago)
changeset 38843 d95522496593
parent 38640 105d1f112da5
child 38854 eb6a35be18ca
permissions -rw-r--r--
more careful locking of jEdit buffer;
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@38843
   121
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38640
   122
  {
wenzelm@38640
   123
    try { buffer.readLock(); body }
wenzelm@38640
   124
    finally { buffer.readUnlock() }
wenzelm@38640
   125
  }
wenzelm@38640
   126
wenzelm@38843
   127
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38843
   128
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@38843
   129
wenzelm@34784
   130
wenzelm@37068
   131
  /* dockable windows */
wenzelm@37068
   132
wenzelm@37068
   133
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   134
wenzelm@37068
   135
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   136
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   137
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   138
      case _ => None
wenzelm@37068
   139
    }
wenzelm@37068
   140
wenzelm@37068
   141
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   142
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   143
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   144
      case _ => None
wenzelm@37068
   145
    }
wenzelm@37068
   146
wenzelm@37068
   147
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   148
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   149
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   150
      case _ => None
wenzelm@37068
   151
    }
wenzelm@37068
   152
wenzelm@37068
   153
wenzelm@38221
   154
  /* manage prover */  // FIXME async!?
wenzelm@34820
   155
wenzelm@34820
   156
  private def prover_started(view: View): Boolean =
wenzelm@34820
   157
  {
wenzelm@34820
   158
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@38221
   159
    session.started(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   160
      case Some(err) =>
wenzelm@34822
   161
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   162
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   163
        false
wenzelm@34820
   164
      case None => true
wenzelm@34820
   165
    }
wenzelm@34820
   166
  }
wenzelm@34820
   167
wenzelm@34820
   168
wenzelm@34784
   169
  /* activation */
wenzelm@34784
   170
wenzelm@34820
   171
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   172
  {
wenzelm@34820
   173
    if (prover_started(view)) {
wenzelm@38222
   174
      // FIXME proper error handling
wenzelm@38222
   175
      val (_, thy_name) = Thy_Header.split_thy_path(Isabelle.system.posix_path(buffer.getPath))
wenzelm@38222
   176
wenzelm@38222
   177
      val model = Document_Model.init(session, buffer, thy_name)
wenzelm@34820
   178
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   179
        Document_View.init(model, text_area)
wenzelm@34820
   180
    }
wenzelm@34784
   181
  }
wenzelm@34784
   182
wenzelm@34784
   183
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   184
  {
wenzelm@34784
   185
    session.stop()  // FIXME not yet
wenzelm@34784
   186
wenzelm@34784
   187
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   188
      Document_View.exit(text_area)
wenzelm@34784
   189
    Document_Model.exit(buffer)
wenzelm@34784
   190
  }
wenzelm@34784
   191
wenzelm@34784
   192
  def switch_active(view: View) =
wenzelm@34784
   193
  {
wenzelm@34784
   194
    val buffer = view.getBuffer
wenzelm@34788
   195
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   196
    else activate_buffer(view, buffer)
wenzelm@34784
   197
  }
wenzelm@34784
   198
wenzelm@34784
   199
  def is_active(view: View): Boolean =
wenzelm@34788
   200
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   201
}
wenzelm@34318
   202
wenzelm@34429
   203
wenzelm@34618
   204
class Plugin extends EBPlugin
wenzelm@34618
   205
{
wenzelm@34618
   206
  /* main plugin plumbing */
wenzelm@34433
   207
wenzelm@34767
   208
  override def handleMessage(message: EBMessage)
wenzelm@34618
   209
  {
wenzelm@34767
   210
    message match {
wenzelm@37557
   211
      case msg: BufferUpdate
wenzelm@37557
   212
        if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@37557
   213
        Document_Model(msg.getBuffer) match {
wenzelm@37557
   214
          case Some(model) => model.refresh()
wenzelm@37557
   215
          case _ =>
wenzelm@37557
   216
        }
wenzelm@37557
   217
wenzelm@34767
   218
      case msg: EditPaneUpdate =>
wenzelm@34784
   219
        val edit_pane = msg.getEditPane
wenzelm@34784
   220
        val buffer = edit_pane.getBuffer
wenzelm@34784
   221
        val text_area = edit_pane.getTextArea
wenzelm@34784
   222
wenzelm@34787
   223
        def init_view()
wenzelm@34787
   224
        {
wenzelm@34788
   225
          Document_Model(buffer) match {
wenzelm@34787
   226
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   227
            case None =>
wenzelm@34787
   228
          }
wenzelm@34787
   229
        }
wenzelm@34787
   230
        def exit_view()
wenzelm@34787
   231
        {
wenzelm@34788
   232
          if (Document_View(text_area).isDefined)
wenzelm@34787
   233
            Document_View.exit(text_area)
wenzelm@34787
   234
        }
wenzelm@34767
   235
        msg.getWhat match {
wenzelm@34787
   236
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   237
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   238
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   239
          case _ =>
immler@34671
   240
        }
wenzelm@34784
   241
wenzelm@34777
   242
      case msg: PropertiesChanged =>
wenzelm@37241
   243
        Swing_Thread.now {
wenzelm@37241
   244
          for (text_area <- Isabelle.jedit_text_areas if Document_View(text_area).isDefined)
wenzelm@37241
   245
            Document_View(text_area).get.extend_styles()
wenzelm@37241
   246
        }
wenzelm@37241
   247
wenzelm@34791
   248
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   249
wenzelm@34318
   250
      case _ =>
wenzelm@34318
   251
    }
wenzelm@34318
   252
  }
wenzelm@34318
   253
wenzelm@34618
   254
  override def start()
wenzelm@34618
   255
  {
wenzelm@34615
   256
    Isabelle.system = new Isabelle_System
wenzelm@34774
   257
    Isabelle.system.install_fonts()
wenzelm@34777
   258
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   259
  }
wenzelm@34618
   260
wenzelm@34618
   261
  override def stop()
wenzelm@34618
   262
  {
wenzelm@34777
   263
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   264
    Isabelle.session = null
wenzelm@34441
   265
    Isabelle.system = null
wenzelm@34318
   266
  }
wenzelm@34318
   267
}