src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Thu Sep 02 23:27:41 2010 +0200 (2010-09-02)
changeset 39043 a0d7e9b580ec
parent 38854 eb6a35be18ca
child 39167 803431dcc7fb
permissions -rw-r--r--
added gfx_range convenience;
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
wenzelm@39043
    20
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
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@39043
    77
  /* text area ranges */
wenzelm@39043
    78
wenzelm@39043
    79
  case class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@39043
    80
wenzelm@39043
    81
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@39043
    82
  {
wenzelm@39043
    83
    val p = text_area.offsetToXY(range.start)
wenzelm@39043
    84
    val q = text_area.offsetToXY(range.stop)
wenzelm@39043
    85
    if (p != null && q != null && p.y == q.y) Some(new Gfx_Range(p.x, p.y, q.x - p.x))
wenzelm@39043
    86
    else None
wenzelm@39043
    87
  }
wenzelm@39043
    88
wenzelm@39043
    89
wenzelm@37201
    90
  /* tooltip markup */
wenzelm@37201
    91
wenzelm@37201
    92
  def tooltip(text: String): String =
wenzelm@37203
    93
    "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
wenzelm@37203
    94
        Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
wenzelm@37203
    95
      HTML.encode(text) + "</pre></html>"
wenzelm@37201
    96
wenzelm@38854
    97
  def tooltip_dismiss_delay(): Int =
wenzelm@38854
    98
    Int_Property("tooltip-dismiss-delay", 8000) max 500
wenzelm@38854
    99
wenzelm@38854
   100
  def setup_tooltips()
wenzelm@38854
   101
  {
wenzelm@38854
   102
    Swing_Thread.now {
wenzelm@38854
   103
      val manager = javax.swing.ToolTipManager.sharedInstance
wenzelm@38854
   104
      manager.setDismissDelay(tooltip_dismiss_delay())
wenzelm@38854
   105
    }
wenzelm@38854
   106
  }
wenzelm@38854
   107
wenzelm@37201
   108
wenzelm@34618
   109
  /* settings */
wenzelm@34618
   110
wenzelm@34782
   111
  def default_logic(): String =
wenzelm@34782
   112
  {
wenzelm@34782
   113
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
   114
    if (logic != "") logic
wenzelm@34782
   115
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
   116
  }
wenzelm@34782
   117
wenzelm@34782
   118
  def isabelle_args(): List[String] =
wenzelm@34618
   119
  {
wenzelm@34780
   120
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
   121
    val logic = {
wenzelm@34784
   122
      val logic = Property("logic")
wenzelm@34782
   123
      if (logic != null && logic != "") logic
wenzelm@34782
   124
      else default_logic()
wenzelm@34780
   125
    }
wenzelm@34780
   126
    modes ++ List(logic)
wenzelm@34502
   127
  }
wenzelm@34502
   128
wenzelm@34618
   129
wenzelm@38221
   130
  /* main jEdit components */
wenzelm@34784
   131
wenzelm@37177
   132
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   133
wenzelm@37177
   134
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   135
wenzelm@34784
   136
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   137
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   138
wenzelm@34784
   139
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   140
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   141
wenzelm@34784
   142
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   143
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   144
wenzelm@38843
   145
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38640
   146
  {
wenzelm@38640
   147
    try { buffer.readLock(); body }
wenzelm@38640
   148
    finally { buffer.readUnlock() }
wenzelm@38640
   149
  }
wenzelm@38640
   150
wenzelm@38843
   151
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38843
   152
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@38843
   153
wenzelm@34784
   154
wenzelm@37068
   155
  /* dockable windows */
wenzelm@37068
   156
wenzelm@37068
   157
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   158
wenzelm@37068
   159
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   160
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   161
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   162
      case _ => None
wenzelm@37068
   163
    }
wenzelm@37068
   164
wenzelm@37068
   165
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   166
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   167
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   168
      case _ => None
wenzelm@37068
   169
    }
wenzelm@37068
   170
wenzelm@37068
   171
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   172
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   173
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   174
      case _ => None
wenzelm@37068
   175
    }
wenzelm@37068
   176
wenzelm@37068
   177
wenzelm@38221
   178
  /* manage prover */  // FIXME async!?
wenzelm@34820
   179
wenzelm@34820
   180
  private def prover_started(view: View): Boolean =
wenzelm@34820
   181
  {
wenzelm@34820
   182
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@38221
   183
    session.started(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   184
      case Some(err) =>
wenzelm@34822
   185
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   186
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   187
        false
wenzelm@34820
   188
      case None => true
wenzelm@34820
   189
    }
wenzelm@34820
   190
  }
wenzelm@34820
   191
wenzelm@34820
   192
wenzelm@34784
   193
  /* activation */
wenzelm@34784
   194
wenzelm@34820
   195
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   196
  {
wenzelm@34820
   197
    if (prover_started(view)) {
wenzelm@38222
   198
      // FIXME proper error handling
wenzelm@38222
   199
      val (_, thy_name) = Thy_Header.split_thy_path(Isabelle.system.posix_path(buffer.getPath))
wenzelm@38222
   200
wenzelm@38222
   201
      val model = Document_Model.init(session, buffer, thy_name)
wenzelm@34820
   202
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   203
        Document_View.init(model, text_area)
wenzelm@34820
   204
    }
wenzelm@34784
   205
  }
wenzelm@34784
   206
wenzelm@34784
   207
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   208
  {
wenzelm@34784
   209
    session.stop()  // FIXME not yet
wenzelm@34784
   210
wenzelm@34784
   211
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   212
      Document_View.exit(text_area)
wenzelm@34784
   213
    Document_Model.exit(buffer)
wenzelm@34784
   214
  }
wenzelm@34784
   215
wenzelm@34784
   216
  def switch_active(view: View) =
wenzelm@34784
   217
  {
wenzelm@34784
   218
    val buffer = view.getBuffer
wenzelm@34788
   219
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   220
    else activate_buffer(view, buffer)
wenzelm@34784
   221
  }
wenzelm@34784
   222
wenzelm@34784
   223
  def is_active(view: View): Boolean =
wenzelm@34788
   224
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   225
}
wenzelm@34318
   226
wenzelm@34429
   227
wenzelm@34618
   228
class Plugin extends EBPlugin
wenzelm@34618
   229
{
wenzelm@34618
   230
  /* main plugin plumbing */
wenzelm@34433
   231
wenzelm@34767
   232
  override def handleMessage(message: EBMessage)
wenzelm@34618
   233
  {
wenzelm@34767
   234
    message match {
wenzelm@37557
   235
      case msg: BufferUpdate
wenzelm@37557
   236
        if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@37557
   237
        Document_Model(msg.getBuffer) match {
wenzelm@37557
   238
          case Some(model) => model.refresh()
wenzelm@37557
   239
          case _ =>
wenzelm@37557
   240
        }
wenzelm@37557
   241
wenzelm@34767
   242
      case msg: EditPaneUpdate =>
wenzelm@34784
   243
        val edit_pane = msg.getEditPane
wenzelm@34784
   244
        val buffer = edit_pane.getBuffer
wenzelm@34784
   245
        val text_area = edit_pane.getTextArea
wenzelm@34784
   246
wenzelm@34787
   247
        def init_view()
wenzelm@34787
   248
        {
wenzelm@34788
   249
          Document_Model(buffer) match {
wenzelm@34787
   250
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   251
            case None =>
wenzelm@34787
   252
          }
wenzelm@34787
   253
        }
wenzelm@34787
   254
        def exit_view()
wenzelm@34787
   255
        {
wenzelm@34788
   256
          if (Document_View(text_area).isDefined)
wenzelm@34787
   257
            Document_View.exit(text_area)
wenzelm@34787
   258
        }
wenzelm@34767
   259
        msg.getWhat match {
wenzelm@34787
   260
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   261
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   262
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   263
          case _ =>
immler@34671
   264
        }
wenzelm@34784
   265
wenzelm@34777
   266
      case msg: PropertiesChanged =>
wenzelm@37241
   267
        Swing_Thread.now {
wenzelm@37241
   268
          for (text_area <- Isabelle.jedit_text_areas if Document_View(text_area).isDefined)
wenzelm@37241
   269
            Document_View(text_area).get.extend_styles()
wenzelm@38854
   270
wenzelm@38854
   271
          Isabelle.setup_tooltips()
wenzelm@37241
   272
        }
wenzelm@37241
   273
wenzelm@34791
   274
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   275
wenzelm@34318
   276
      case _ =>
wenzelm@34318
   277
    }
wenzelm@34318
   278
  }
wenzelm@34318
   279
wenzelm@34618
   280
  override def start()
wenzelm@34618
   281
  {
wenzelm@34615
   282
    Isabelle.system = new Isabelle_System
wenzelm@34774
   283
    Isabelle.system.install_fonts()
wenzelm@34777
   284
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@38854
   285
wenzelm@38854
   286
    Isabelle.setup_tooltips()
wenzelm@34318
   287
  }
wenzelm@34618
   288
wenzelm@34618
   289
  override def stop()
wenzelm@34618
   290
  {
wenzelm@34777
   291
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   292
    Isabelle.session = null
wenzelm@34441
   293
    Isabelle.system = null
wenzelm@34318
   294
  }
wenzelm@34318
   295
}