src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Thu Sep 23 16:48:12 2010 +0200 (2010-09-23 ago)
changeset 39628 f6e82967b5cd
parent 39518 96180281c3b2
child 39630 44181423183a
permissions -rw-r--r--
Plugin.stop: refrain from invalidating Isabelle.session -- some actors/dockables out there might still refer to it;
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@34318
    14
wenzelm@34497
    15
import scala.collection.mutable
wenzelm@39517
    16
import scala.swing.ComboBox
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@38221
   123
  /* main jEdit components */
wenzelm@34784
   124
wenzelm@37177
   125
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   126
wenzelm@37177
   127
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   128
wenzelm@34784
   129
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   130
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   131
wenzelm@34784
   132
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   133
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   134
wenzelm@34784
   135
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   136
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   137
wenzelm@38843
   138
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38640
   139
  {
wenzelm@38640
   140
    try { buffer.readLock(); body }
wenzelm@38640
   141
    finally { buffer.readUnlock() }
wenzelm@38640
   142
  }
wenzelm@38640
   143
wenzelm@38843
   144
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@38843
   145
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@38843
   146
wenzelm@34784
   147
wenzelm@37068
   148
  /* dockable windows */
wenzelm@37068
   149
wenzelm@37068
   150
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   151
wenzelm@39515
   152
  def docked_session(view: View): Option[Session_Dockable] =
wenzelm@39515
   153
    wm(view).getDockableWindow("isabelle-session") match {
wenzelm@39515
   154
      case dockable: Session_Dockable => Some(dockable)
wenzelm@39515
   155
      case _ => None
wenzelm@39515
   156
    }
wenzelm@39515
   157
wenzelm@37068
   158
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   159
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   160
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   161
      case _ => None
wenzelm@37068
   162
    }
wenzelm@37068
   163
wenzelm@37068
   164
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   165
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   166
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   167
      case _ => None
wenzelm@37068
   168
    }
wenzelm@37068
   169
wenzelm@37068
   170
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   171
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   172
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   173
      case _ => None
wenzelm@37068
   174
    }
wenzelm@37068
   175
wenzelm@37068
   176
wenzelm@39517
   177
  /* logic image */
wenzelm@39517
   178
wenzelm@39517
   179
  def default_logic(): String =
wenzelm@39517
   180
  {
wenzelm@39517
   181
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@39517
   182
    if (logic != "") logic
wenzelm@39517
   183
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@39517
   184
  }
wenzelm@39517
   185
wenzelm@39517
   186
  class Logic_Entry(val name: String, val description: String)
wenzelm@39517
   187
  {
wenzelm@39517
   188
    override def toString = description
wenzelm@39517
   189
  }
wenzelm@39517
   190
wenzelm@39517
   191
  def logic_selector(logic: String): ComboBox[Logic_Entry] =
wenzelm@39517
   192
  {
wenzelm@39517
   193
    val entries =
wenzelm@39517
   194
      new Logic_Entry("", "default (" + default_logic() + ")") ::
wenzelm@39517
   195
        system.find_logics().map(name => new Logic_Entry(name, name))
wenzelm@39517
   196
    val component = new ComboBox(entries)
wenzelm@39517
   197
    entries.find(_.name == logic) match {
wenzelm@39517
   198
      case None =>
wenzelm@39517
   199
      case Some(entry) => component.selection.item = entry
wenzelm@39517
   200
    }
wenzelm@39517
   201
    component
wenzelm@39517
   202
  }
wenzelm@39517
   203
wenzelm@39517
   204
  def isabelle_args(): List[String] =
wenzelm@39517
   205
  {
wenzelm@39517
   206
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@39517
   207
    val logic = {
wenzelm@39517
   208
      val logic = Property("logic")
wenzelm@39517
   209
      if (logic != null && logic != "") logic
wenzelm@39517
   210
      else default_logic()
wenzelm@39517
   211
    }
wenzelm@39517
   212
    modes ++ List(logic)
wenzelm@39517
   213
  }
wenzelm@39517
   214
wenzelm@39517
   215
wenzelm@38221
   216
  /* manage prover */  // FIXME async!?
wenzelm@34820
   217
wenzelm@34820
   218
  private def prover_started(view: View): Boolean =
wenzelm@34820
   219
  {
wenzelm@34820
   220
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@38221
   221
    session.started(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   222
      case Some(err) =>
wenzelm@39518
   223
        val text = new scala.swing.TextArea(err)
wenzelm@39518
   224
        text.editable = false
wenzelm@34822
   225
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   226
        false
wenzelm@34820
   227
      case None => true
wenzelm@34820
   228
    }
wenzelm@34820
   229
  }
wenzelm@34820
   230
wenzelm@34820
   231
wenzelm@34784
   232
  /* activation */
wenzelm@34784
   233
wenzelm@34820
   234
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   235
  {
wenzelm@34820
   236
    if (prover_started(view)) {
wenzelm@38222
   237
      // FIXME proper error handling
wenzelm@38222
   238
      val (_, thy_name) = Thy_Header.split_thy_path(Isabelle.system.posix_path(buffer.getPath))
wenzelm@38222
   239
wenzelm@38222
   240
      val model = Document_Model.init(session, buffer, thy_name)
wenzelm@34820
   241
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   242
        Document_View.init(model, text_area)
wenzelm@34820
   243
    }
wenzelm@34784
   244
  }
wenzelm@34784
   245
wenzelm@34784
   246
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   247
  {
wenzelm@34784
   248
    session.stop()  // FIXME not yet
wenzelm@34784
   249
wenzelm@34784
   250
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   251
      Document_View.exit(text_area)
wenzelm@34784
   252
    Document_Model.exit(buffer)
wenzelm@34784
   253
  }
wenzelm@34784
   254
wenzelm@34784
   255
  def switch_active(view: View) =
wenzelm@34784
   256
  {
wenzelm@34784
   257
    val buffer = view.getBuffer
wenzelm@34788
   258
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   259
    else activate_buffer(view, buffer)
wenzelm@34784
   260
  }
wenzelm@34784
   261
wenzelm@34784
   262
  def is_active(view: View): Boolean =
wenzelm@34788
   263
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   264
}
wenzelm@34318
   265
wenzelm@34429
   266
wenzelm@34618
   267
class Plugin extends EBPlugin
wenzelm@34618
   268
{
wenzelm@34618
   269
  /* main plugin plumbing */
wenzelm@34433
   270
wenzelm@34767
   271
  override def handleMessage(message: EBMessage)
wenzelm@34618
   272
  {
wenzelm@34767
   273
    message match {
wenzelm@37557
   274
      case msg: BufferUpdate
wenzelm@37557
   275
        if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@37557
   276
        Document_Model(msg.getBuffer) match {
wenzelm@37557
   277
          case Some(model) => model.refresh()
wenzelm@37557
   278
          case _ =>
wenzelm@37557
   279
        }
wenzelm@37557
   280
wenzelm@34767
   281
      case msg: EditPaneUpdate =>
wenzelm@34784
   282
        val edit_pane = msg.getEditPane
wenzelm@34784
   283
        val buffer = edit_pane.getBuffer
wenzelm@34784
   284
        val text_area = edit_pane.getTextArea
wenzelm@34784
   285
wenzelm@34787
   286
        def init_view()
wenzelm@34787
   287
        {
wenzelm@34788
   288
          Document_Model(buffer) match {
wenzelm@34787
   289
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   290
            case None =>
wenzelm@34787
   291
          }
wenzelm@34787
   292
        }
wenzelm@34787
   293
        def exit_view()
wenzelm@34787
   294
        {
wenzelm@34788
   295
          if (Document_View(text_area).isDefined)
wenzelm@34787
   296
            Document_View.exit(text_area)
wenzelm@34787
   297
        }
wenzelm@34767
   298
        msg.getWhat match {
wenzelm@34787
   299
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   300
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   301
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   302
          case _ =>
immler@34671
   303
        }
wenzelm@34784
   304
wenzelm@34777
   305
      case msg: PropertiesChanged =>
wenzelm@37241
   306
        Swing_Thread.now {
wenzelm@37241
   307
          for (text_area <- Isabelle.jedit_text_areas if Document_View(text_area).isDefined)
wenzelm@37241
   308
            Document_View(text_area).get.extend_styles()
wenzelm@38854
   309
wenzelm@38854
   310
          Isabelle.setup_tooltips()
wenzelm@37241
   311
        }
wenzelm@37241
   312
wenzelm@34791
   313
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   314
wenzelm@34318
   315
      case _ =>
wenzelm@34318
   316
    }
wenzelm@34318
   317
  }
wenzelm@34318
   318
wenzelm@34618
   319
  override def start()
wenzelm@34618
   320
  {
wenzelm@34615
   321
    Isabelle.system = new Isabelle_System
wenzelm@34774
   322
    Isabelle.system.install_fonts()
wenzelm@39628
   323
    Isabelle.session = new Session(Isabelle.system)
wenzelm@38854
   324
    Isabelle.setup_tooltips()
wenzelm@34318
   325
  }
wenzelm@34618
   326
wenzelm@39628
   327
  override def stop()
wenzelm@34618
   328
  {
wenzelm@39628
   329
    Isabelle.session.stop()
wenzelm@34318
   330
  }
wenzelm@34318
   331
}