src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Sat Sep 18 14:28:42 2010 +0200 (2010-09-18 ago)
changeset 39515 57ceabb0bb8e
parent 39241 e9a442606db3
child 39517 e036c67448e6
permissions -rw-r--r--
basic setup for prover session panel;
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@39241
    18
import org.gjt.sp.jedit.{jEdit, GUIUtilities, EBMessage, EBPlugin,
wenzelm@39241
    19
  Buffer, EditPane, ServiceManager, View}
immler@34406
    20
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@39043
    21
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@37557
    22
import org.gjt.sp.jedit.msg.{BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@37068
    23
import org.gjt.sp.jedit.gui.DockableWindowManager
wenzelm@34429
    24
wenzelm@39241
    25
import org.gjt.sp.util.Log
wenzelm@39241
    26
wenzelm@34318
    27
wenzelm@34618
    28
object Isabelle
wenzelm@34618
    29
{
wenzelm@34784
    30
  /* plugin instance */
wenzelm@34784
    31
wenzelm@34784
    32
  var system: Isabelle_System = null
wenzelm@34784
    33
  var session: Session = null
wenzelm@34784
    34
wenzelm@34784
    35
wenzelm@34618
    36
  /* properties */
wenzelm@34618
    37
wenzelm@34618
    38
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    39
wenzelm@34618
    40
  object Property
wenzelm@34618
    41
  {
wenzelm@36814
    42
    def apply(name: String): String =
wenzelm@36814
    43
      jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@36814
    44
    def apply(name: String, default: String): String =
wenzelm@36814
    45
      jEdit.getProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    46
    def update(name: String, value: String) =
wenzelm@36814
    47
      jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    48
  }
wenzelm@34433
    49
wenzelm@34618
    50
  object Boolean_Property
wenzelm@34618
    51
  {
wenzelm@36814
    52
    def apply(name: String): Boolean =
wenzelm@36814
    53
      jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@36814
    54
    def apply(name: String, default: Boolean): Boolean =
wenzelm@36814
    55
      jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    56
    def update(name: String, value: Boolean) =
wenzelm@36814
    57
      jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    58
  }
wenzelm@34618
    59
wenzelm@34618
    60
  object Int_Property
wenzelm@34618
    61
  {
wenzelm@36814
    62
    def apply(name: String): Int =
wenzelm@36814
    63
      jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@36814
    64
    def apply(name: String, default: Int): Int =
wenzelm@36814
    65
      jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    66
    def update(name: String, value: Int) =
wenzelm@36814
    67
      jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    68
  }
wenzelm@34618
    69
wenzelm@37164
    70
wenzelm@37164
    71
  /* font */
wenzelm@37164
    72
wenzelm@37164
    73
  def font_family(): String = jEdit.getProperty("view.font")
wenzelm@37164
    74
wenzelm@37019
    75
  def font_size(): Float =
wenzelm@37019
    76
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
    77
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
    78
wenzelm@34618
    79
wenzelm@39043
    80
  /* text area ranges */
wenzelm@39043
    81
wenzelm@39043
    82
  case class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@39043
    83
wenzelm@39043
    84
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@39043
    85
  {
wenzelm@39043
    86
    val p = text_area.offsetToXY(range.start)
wenzelm@39043
    87
    val q = text_area.offsetToXY(range.stop)
wenzelm@39043
    88
    if (p != null && q != null && p.y == q.y) Some(new Gfx_Range(p.x, p.y, q.x - p.x))
wenzelm@39043
    89
    else None
wenzelm@39043
    90
  }
wenzelm@39043
    91
wenzelm@39043
    92
wenzelm@37201
    93
  /* tooltip markup */
wenzelm@37201
    94
wenzelm@37201
    95
  def tooltip(text: String): String =
wenzelm@37203
    96
    "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
wenzelm@37203
    97
        Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
wenzelm@37203
    98
      HTML.encode(text) + "</pre></html>"
wenzelm@37201
    99
wenzelm@38854
   100
  def tooltip_dismiss_delay(): Int =
wenzelm@38854
   101
    Int_Property("tooltip-dismiss-delay", 8000) max 500
wenzelm@38854
   102
wenzelm@38854
   103
  def setup_tooltips()
wenzelm@38854
   104
  {
wenzelm@38854
   105
    Swing_Thread.now {
wenzelm@38854
   106
      val manager = javax.swing.ToolTipManager.sharedInstance
wenzelm@38854
   107
      manager.setDismissDelay(tooltip_dismiss_delay())
wenzelm@38854
   108
    }
wenzelm@38854
   109
  }
wenzelm@38854
   110
wenzelm@37201
   111
wenzelm@39241
   112
  /* icons */
wenzelm@39241
   113
wenzelm@39241
   114
  def load_icon(name: String): javax.swing.Icon =
wenzelm@39241
   115
  {
wenzelm@39241
   116
    val icon = GUIUtilities.loadIcon(name)
wenzelm@39241
   117
    if (icon.getIconWidth < 0 || icon.getIconHeight < 0)
wenzelm@39241
   118
      Log.log(Log.ERROR, icon, "Bad icon: " + name);
wenzelm@39241
   119
    icon
wenzelm@39241
   120
  }
wenzelm@39241
   121
wenzelm@39241
   122
wenzelm@34618
   123
  /* settings */
wenzelm@34618
   124
wenzelm@34782
   125
  def default_logic(): String =
wenzelm@34782
   126
  {
wenzelm@34782
   127
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
   128
    if (logic != "") logic
wenzelm@34782
   129
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
   130
  }
wenzelm@34782
   131
wenzelm@34782
   132
  def isabelle_args(): List[String] =
wenzelm@34618
   133
  {
wenzelm@34780
   134
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
   135
    val logic = {
wenzelm@34784
   136
      val logic = Property("logic")
wenzelm@34782
   137
      if (logic != null && logic != "") logic
wenzelm@34782
   138
      else default_logic()
wenzelm@34780
   139
    }
wenzelm@34780
   140
    modes ++ List(logic)
wenzelm@34502
   141
  }
wenzelm@34502
   142
wenzelm@34618
   143
wenzelm@38221
   144
  /* main jEdit components */
wenzelm@34784
   145
wenzelm@37177
   146
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   147
wenzelm@37177
   148
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   149
wenzelm@34784
   150
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   151
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   152
wenzelm@34784
   153
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   154
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   155
wenzelm@34784
   156
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   157
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   158
wenzelm@38843
   159
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38640
   160
  {
wenzelm@38640
   161
    try { buffer.readLock(); body }
wenzelm@38640
   162
    finally { buffer.readUnlock() }
wenzelm@38640
   163
  }
wenzelm@38640
   164
wenzelm@38843
   165
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38843
   166
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@38843
   167
wenzelm@34784
   168
wenzelm@37068
   169
  /* dockable windows */
wenzelm@37068
   170
wenzelm@37068
   171
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   172
wenzelm@39515
   173
  def docked_session(view: View): Option[Session_Dockable] =
wenzelm@39515
   174
    wm(view).getDockableWindow("isabelle-session") match {
wenzelm@39515
   175
      case dockable: Session_Dockable => Some(dockable)
wenzelm@39515
   176
      case _ => None
wenzelm@39515
   177
    }
wenzelm@39515
   178
wenzelm@37068
   179
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   180
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   181
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   182
      case _ => None
wenzelm@37068
   183
    }
wenzelm@37068
   184
wenzelm@37068
   185
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   186
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   187
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   188
      case _ => None
wenzelm@37068
   189
    }
wenzelm@37068
   190
wenzelm@37068
   191
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   192
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   193
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   194
      case _ => None
wenzelm@37068
   195
    }
wenzelm@37068
   196
wenzelm@37068
   197
wenzelm@38221
   198
  /* manage prover */  // FIXME async!?
wenzelm@34820
   199
wenzelm@34820
   200
  private def prover_started(view: View): Boolean =
wenzelm@34820
   201
  {
wenzelm@34820
   202
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@38221
   203
    session.started(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   204
      case Some(err) =>
wenzelm@34822
   205
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   206
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   207
        false
wenzelm@34820
   208
      case None => true
wenzelm@34820
   209
    }
wenzelm@34820
   210
  }
wenzelm@34820
   211
wenzelm@34820
   212
wenzelm@34784
   213
  /* activation */
wenzelm@34784
   214
wenzelm@34820
   215
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   216
  {
wenzelm@34820
   217
    if (prover_started(view)) {
wenzelm@38222
   218
      // FIXME proper error handling
wenzelm@38222
   219
      val (_, thy_name) = Thy_Header.split_thy_path(Isabelle.system.posix_path(buffer.getPath))
wenzelm@38222
   220
wenzelm@38222
   221
      val model = Document_Model.init(session, buffer, thy_name)
wenzelm@34820
   222
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   223
        Document_View.init(model, text_area)
wenzelm@34820
   224
    }
wenzelm@34784
   225
  }
wenzelm@34784
   226
wenzelm@34784
   227
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   228
  {
wenzelm@34784
   229
    session.stop()  // FIXME not yet
wenzelm@34784
   230
wenzelm@34784
   231
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   232
      Document_View.exit(text_area)
wenzelm@34784
   233
    Document_Model.exit(buffer)
wenzelm@34784
   234
  }
wenzelm@34784
   235
wenzelm@34784
   236
  def switch_active(view: View) =
wenzelm@34784
   237
  {
wenzelm@34784
   238
    val buffer = view.getBuffer
wenzelm@34788
   239
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   240
    else activate_buffer(view, buffer)
wenzelm@34784
   241
  }
wenzelm@34784
   242
wenzelm@34784
   243
  def is_active(view: View): Boolean =
wenzelm@34788
   244
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   245
}
wenzelm@34318
   246
wenzelm@34429
   247
wenzelm@34618
   248
class Plugin extends EBPlugin
wenzelm@34618
   249
{
wenzelm@34618
   250
  /* main plugin plumbing */
wenzelm@34433
   251
wenzelm@34767
   252
  override def handleMessage(message: EBMessage)
wenzelm@34618
   253
  {
wenzelm@34767
   254
    message match {
wenzelm@37557
   255
      case msg: BufferUpdate
wenzelm@37557
   256
        if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@37557
   257
        Document_Model(msg.getBuffer) match {
wenzelm@37557
   258
          case Some(model) => model.refresh()
wenzelm@37557
   259
          case _ =>
wenzelm@37557
   260
        }
wenzelm@37557
   261
wenzelm@34767
   262
      case msg: EditPaneUpdate =>
wenzelm@34784
   263
        val edit_pane = msg.getEditPane
wenzelm@34784
   264
        val buffer = edit_pane.getBuffer
wenzelm@34784
   265
        val text_area = edit_pane.getTextArea
wenzelm@34784
   266
wenzelm@34787
   267
        def init_view()
wenzelm@34787
   268
        {
wenzelm@34788
   269
          Document_Model(buffer) match {
wenzelm@34787
   270
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   271
            case None =>
wenzelm@34787
   272
          }
wenzelm@34787
   273
        }
wenzelm@34787
   274
        def exit_view()
wenzelm@34787
   275
        {
wenzelm@34788
   276
          if (Document_View(text_area).isDefined)
wenzelm@34787
   277
            Document_View.exit(text_area)
wenzelm@34787
   278
        }
wenzelm@34767
   279
        msg.getWhat match {
wenzelm@34787
   280
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   281
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   282
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   283
          case _ =>
immler@34671
   284
        }
wenzelm@34784
   285
wenzelm@34777
   286
      case msg: PropertiesChanged =>
wenzelm@37241
   287
        Swing_Thread.now {
wenzelm@37241
   288
          for (text_area <- Isabelle.jedit_text_areas if Document_View(text_area).isDefined)
wenzelm@37241
   289
            Document_View(text_area).get.extend_styles()
wenzelm@38854
   290
wenzelm@38854
   291
          Isabelle.setup_tooltips()
wenzelm@37241
   292
        }
wenzelm@37241
   293
wenzelm@34791
   294
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   295
wenzelm@34318
   296
      case _ =>
wenzelm@34318
   297
    }
wenzelm@34318
   298
  }
wenzelm@34318
   299
wenzelm@34618
   300
  override def start()
wenzelm@34618
   301
  {
wenzelm@34615
   302
    Isabelle.system = new Isabelle_System
wenzelm@34774
   303
    Isabelle.system.install_fonts()
wenzelm@34777
   304
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@38854
   305
wenzelm@38854
   306
    Isabelle.setup_tooltips()
wenzelm@34318
   307
  }
wenzelm@34618
   308
wenzelm@39167
   309
  override def stop()  // FIXME fragile
wenzelm@34618
   310
  {
wenzelm@34777
   311
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   312
    Isabelle.session = null
wenzelm@34318
   313
  }
wenzelm@34318
   314
}