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