src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Thu Sep 09 21:30:33 2010 +0200 (2010-09-09 ago)
changeset 39241 e9a442606db3
parent 39167 803431dcc7fb
child 39515 57ceabb0bb8e
permissions -rw-r--r--
Isabelle.load_icon with some sanity checks;
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@37068
   173
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   174
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   175
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   176
      case _ => None
wenzelm@37068
   177
    }
wenzelm@37068
   178
wenzelm@37068
   179
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   180
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   181
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   182
      case _ => None
wenzelm@37068
   183
    }
wenzelm@37068
   184
wenzelm@37068
   185
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   186
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   187
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   188
      case _ => None
wenzelm@37068
   189
    }
wenzelm@37068
   190
wenzelm@37068
   191
wenzelm@38221
   192
  /* manage prover */  // FIXME async!?
wenzelm@34820
   193
wenzelm@34820
   194
  private def prover_started(view: View): Boolean =
wenzelm@34820
   195
  {
wenzelm@34820
   196
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@38221
   197
    session.started(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   198
      case Some(err) =>
wenzelm@34822
   199
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   200
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   201
        false
wenzelm@34820
   202
      case None => true
wenzelm@34820
   203
    }
wenzelm@34820
   204
  }
wenzelm@34820
   205
wenzelm@34820
   206
wenzelm@34784
   207
  /* activation */
wenzelm@34784
   208
wenzelm@34820
   209
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   210
  {
wenzelm@34820
   211
    if (prover_started(view)) {
wenzelm@38222
   212
      // FIXME proper error handling
wenzelm@38222
   213
      val (_, thy_name) = Thy_Header.split_thy_path(Isabelle.system.posix_path(buffer.getPath))
wenzelm@38222
   214
wenzelm@38222
   215
      val model = Document_Model.init(session, buffer, thy_name)
wenzelm@34820
   216
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   217
        Document_View.init(model, text_area)
wenzelm@34820
   218
    }
wenzelm@34784
   219
  }
wenzelm@34784
   220
wenzelm@34784
   221
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   222
  {
wenzelm@34784
   223
    session.stop()  // FIXME not yet
wenzelm@34784
   224
wenzelm@34784
   225
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   226
      Document_View.exit(text_area)
wenzelm@34784
   227
    Document_Model.exit(buffer)
wenzelm@34784
   228
  }
wenzelm@34784
   229
wenzelm@34784
   230
  def switch_active(view: View) =
wenzelm@34784
   231
  {
wenzelm@34784
   232
    val buffer = view.getBuffer
wenzelm@34788
   233
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   234
    else activate_buffer(view, buffer)
wenzelm@34784
   235
  }
wenzelm@34784
   236
wenzelm@34784
   237
  def is_active(view: View): Boolean =
wenzelm@34788
   238
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   239
}
wenzelm@34318
   240
wenzelm@34429
   241
wenzelm@34618
   242
class Plugin extends EBPlugin
wenzelm@34618
   243
{
wenzelm@34618
   244
  /* main plugin plumbing */
wenzelm@34433
   245
wenzelm@34767
   246
  override def handleMessage(message: EBMessage)
wenzelm@34618
   247
  {
wenzelm@34767
   248
    message match {
wenzelm@37557
   249
      case msg: BufferUpdate
wenzelm@37557
   250
        if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@37557
   251
        Document_Model(msg.getBuffer) match {
wenzelm@37557
   252
          case Some(model) => model.refresh()
wenzelm@37557
   253
          case _ =>
wenzelm@37557
   254
        }
wenzelm@37557
   255
wenzelm@34767
   256
      case msg: EditPaneUpdate =>
wenzelm@34784
   257
        val edit_pane = msg.getEditPane
wenzelm@34784
   258
        val buffer = edit_pane.getBuffer
wenzelm@34784
   259
        val text_area = edit_pane.getTextArea
wenzelm@34784
   260
wenzelm@34787
   261
        def init_view()
wenzelm@34787
   262
        {
wenzelm@34788
   263
          Document_Model(buffer) match {
wenzelm@34787
   264
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   265
            case None =>
wenzelm@34787
   266
          }
wenzelm@34787
   267
        }
wenzelm@34787
   268
        def exit_view()
wenzelm@34787
   269
        {
wenzelm@34788
   270
          if (Document_View(text_area).isDefined)
wenzelm@34787
   271
            Document_View.exit(text_area)
wenzelm@34787
   272
        }
wenzelm@34767
   273
        msg.getWhat match {
wenzelm@34787
   274
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   275
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   276
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   277
          case _ =>
immler@34671
   278
        }
wenzelm@34784
   279
wenzelm@34777
   280
      case msg: PropertiesChanged =>
wenzelm@37241
   281
        Swing_Thread.now {
wenzelm@37241
   282
          for (text_area <- Isabelle.jedit_text_areas if Document_View(text_area).isDefined)
wenzelm@37241
   283
            Document_View(text_area).get.extend_styles()
wenzelm@38854
   284
wenzelm@38854
   285
          Isabelle.setup_tooltips()
wenzelm@37241
   286
        }
wenzelm@37241
   287
wenzelm@34791
   288
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   289
wenzelm@34318
   290
      case _ =>
wenzelm@34318
   291
    }
wenzelm@34318
   292
  }
wenzelm@34318
   293
wenzelm@34618
   294
  override def start()
wenzelm@34618
   295
  {
wenzelm@34615
   296
    Isabelle.system = new Isabelle_System
wenzelm@34774
   297
    Isabelle.system.install_fonts()
wenzelm@34777
   298
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@38854
   299
wenzelm@38854
   300
    Isabelle.setup_tooltips()
wenzelm@34318
   301
  }
wenzelm@34618
   302
wenzelm@39167
   303
  override def stop()  // FIXME fragile
wenzelm@34618
   304
  {
wenzelm@34777
   305
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   306
    Isabelle.session = null
wenzelm@34318
   307
  }
wenzelm@34318
   308
}