src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Tue Jun 01 17:27:38 2010 +0200 (2010-06-01)
changeset 37241 04d2521e79b0
parent 37203 c4261f3bbdd7
child 37557 1ae272fd4082
permissions -rw-r--r--
basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/plugin.scala
wenzelm@36760
     2
    Author:     Johannes Hölzl, TU Munich
wenzelm@36760
     3
    Author:     Fabian Immler, TU Munich
wenzelm@36760
     4
    Author:     Makarius
wenzelm@36760
     5
wenzelm@36760
     6
Main Isabelle/jEdit plugin setup.
wenzelm@36760
     7
*/
wenzelm@34407
     8
wenzelm@34318
     9
package isabelle.jedit
wenzelm@34318
    10
wenzelm@34429
    11
wenzelm@36015
    12
import isabelle._
wenzelm@36015
    13
wenzelm@34429
    14
import java.io.{FileInputStream, IOException}
wenzelm@34318
    15
import java.awt.Font
wenzelm@34822
    16
import javax.swing.JTextArea
wenzelm@34318
    17
wenzelm@34497
    18
import scala.collection.mutable
immler@34406
    19
wenzelm@34429
    20
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, EditPane, ServiceManager, View}
immler@34406
    21
import org.gjt.sp.jedit.buffer.JEditBuffer
immler@34406
    22
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@34429
    23
import org.gjt.sp.jedit.msg.{EditPaneUpdate, PropertiesChanged}
wenzelm@37068
    24
import org.gjt.sp.jedit.gui.DockableWindowManager
wenzelm@34429
    25
wenzelm@34318
    26
wenzelm@34618
    27
object Isabelle
wenzelm@34618
    28
{
wenzelm@34784
    29
  /* plugin instance */
wenzelm@34784
    30
wenzelm@34784
    31
  var system: Isabelle_System = null
wenzelm@34784
    32
  var session: Session = null
wenzelm@34784
    33
wenzelm@34784
    34
wenzelm@34618
    35
  /* name */
wenzelm@34618
    36
wenzelm@34318
    37
  val NAME = "Isabelle"
wenzelm@34623
    38
wenzelm@34618
    39
wenzelm@34618
    40
  /* properties */
wenzelm@34618
    41
wenzelm@34618
    42
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    43
wenzelm@34618
    44
  object Property
wenzelm@34618
    45
  {
wenzelm@36814
    46
    def apply(name: String): String =
wenzelm@36814
    47
      jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@36814
    48
    def apply(name: String, default: String): String =
wenzelm@36814
    49
      jEdit.getProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    50
    def update(name: String, value: String) =
wenzelm@36814
    51
      jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    52
  }
wenzelm@34433
    53
wenzelm@34618
    54
  object Boolean_Property
wenzelm@34618
    55
  {
wenzelm@36814
    56
    def apply(name: String): Boolean =
wenzelm@36814
    57
      jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@36814
    58
    def apply(name: String, default: Boolean): Boolean =
wenzelm@36814
    59
      jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    60
    def update(name: String, value: Boolean) =
wenzelm@36814
    61
      jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    62
  }
wenzelm@34618
    63
wenzelm@34618
    64
  object Int_Property
wenzelm@34618
    65
  {
wenzelm@36814
    66
    def apply(name: String): Int =
wenzelm@36814
    67
      jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@36814
    68
    def apply(name: String, default: Int): Int =
wenzelm@36814
    69
      jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    70
    def update(name: String, value: Int) =
wenzelm@36814
    71
      jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    72
  }
wenzelm@34618
    73
wenzelm@37164
    74
wenzelm@37164
    75
  /* font */
wenzelm@37164
    76
wenzelm@37164
    77
  def font_family(): String = jEdit.getProperty("view.font")
wenzelm@37164
    78
wenzelm@37019
    79
  def font_size(): Float =
wenzelm@37019
    80
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
    81
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
    82
wenzelm@34618
    83
wenzelm@37201
    84
  /* tooltip markup */
wenzelm@37201
    85
wenzelm@37201
    86
  def tooltip(text: String): String =
wenzelm@37203
    87
    "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
wenzelm@37203
    88
        Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
wenzelm@37203
    89
      HTML.encode(text) + "</pre></html>"
wenzelm@37201
    90
wenzelm@37201
    91
wenzelm@34618
    92
  /* settings */
wenzelm@34618
    93
wenzelm@34782
    94
  def default_logic(): String =
wenzelm@34782
    95
  {
wenzelm@34782
    96
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
    97
    if (logic != "") logic
wenzelm@34782
    98
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
    99
  }
wenzelm@34782
   100
wenzelm@34782
   101
  def isabelle_args(): List[String] =
wenzelm@34618
   102
  {
wenzelm@34780
   103
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
   104
    val logic = {
wenzelm@34784
   105
      val logic = Property("logic")
wenzelm@34782
   106
      if (logic != null && logic != "") logic
wenzelm@34782
   107
      else default_logic()
wenzelm@34780
   108
    }
wenzelm@34780
   109
    modes ++ List(logic)
wenzelm@34502
   110
  }
wenzelm@34502
   111
wenzelm@34618
   112
wenzelm@34784
   113
  /* main jEdit components */  // FIXME ownership!?
wenzelm@34784
   114
wenzelm@37177
   115
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   116
wenzelm@37177
   117
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   118
wenzelm@34784
   119
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   120
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   121
wenzelm@34784
   122
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   123
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   124
wenzelm@34784
   125
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   126
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   127
wenzelm@34784
   128
wenzelm@37068
   129
  /* dockable windows */
wenzelm@37068
   130
wenzelm@37068
   131
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   132
wenzelm@37068
   133
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   134
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   135
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   136
      case _ => None
wenzelm@37068
   137
    }
wenzelm@37068
   138
wenzelm@37068
   139
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   140
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   141
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   142
      case _ => None
wenzelm@37068
   143
    }
wenzelm@37068
   144
wenzelm@37068
   145
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   146
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   147
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   148
      case _ => None
wenzelm@37068
   149
    }
wenzelm@37068
   150
wenzelm@37068
   151
wenzelm@34820
   152
  /* manage prover */
wenzelm@34820
   153
wenzelm@34820
   154
  private def prover_started(view: View): Boolean =
wenzelm@34820
   155
  {
wenzelm@34820
   156
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@34820
   157
    session.start(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   158
      case Some(err) =>
wenzelm@34822
   159
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   160
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   161
        false
wenzelm@34820
   162
      case None => true
wenzelm@34820
   163
    }
wenzelm@34820
   164
  }
wenzelm@34820
   165
wenzelm@34820
   166
wenzelm@34784
   167
  /* activation */
wenzelm@34784
   168
wenzelm@34820
   169
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   170
  {
wenzelm@34820
   171
    if (prover_started(view)) {
wenzelm@34820
   172
      val model = Document_Model.init(session, buffer)
wenzelm@34820
   173
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   174
        Document_View.init(model, text_area)
wenzelm@34820
   175
    }
wenzelm@34784
   176
  }
wenzelm@34784
   177
wenzelm@34784
   178
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   179
  {
wenzelm@34784
   180
    session.stop()  // FIXME not yet
wenzelm@34784
   181
wenzelm@34784
   182
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   183
      Document_View.exit(text_area)
wenzelm@34784
   184
    Document_Model.exit(buffer)
wenzelm@34784
   185
  }
wenzelm@34784
   186
wenzelm@34784
   187
  def switch_active(view: View) =
wenzelm@34784
   188
  {
wenzelm@34784
   189
    val buffer = view.getBuffer
wenzelm@34788
   190
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   191
    else activate_buffer(view, buffer)
wenzelm@34784
   192
  }
wenzelm@34784
   193
wenzelm@34784
   194
  def is_active(view: View): Boolean =
wenzelm@34788
   195
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   196
}
wenzelm@34318
   197
wenzelm@34429
   198
wenzelm@34618
   199
class Plugin extends EBPlugin
wenzelm@34618
   200
{
wenzelm@34618
   201
  /* main plugin plumbing */
wenzelm@34433
   202
wenzelm@34767
   203
  override def handleMessage(message: EBMessage)
wenzelm@34618
   204
  {
wenzelm@34767
   205
    message match {
wenzelm@34767
   206
      case msg: EditPaneUpdate =>
wenzelm@34784
   207
        val edit_pane = msg.getEditPane
wenzelm@34784
   208
        val buffer = edit_pane.getBuffer
wenzelm@34784
   209
        val text_area = edit_pane.getTextArea
wenzelm@34784
   210
wenzelm@34787
   211
        def init_view()
wenzelm@34787
   212
        {
wenzelm@34788
   213
          Document_Model(buffer) match {
wenzelm@34787
   214
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   215
            case None =>
wenzelm@34787
   216
          }
wenzelm@34787
   217
        }
wenzelm@34787
   218
        def exit_view()
wenzelm@34787
   219
        {
wenzelm@34788
   220
          if (Document_View(text_area).isDefined)
wenzelm@34787
   221
            Document_View.exit(text_area)
wenzelm@34787
   222
        }
wenzelm@34767
   223
        msg.getWhat match {
wenzelm@34787
   224
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   225
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   226
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   227
          case _ =>
immler@34671
   228
        }
wenzelm@34784
   229
wenzelm@34777
   230
      case msg: PropertiesChanged =>
wenzelm@37241
   231
        Swing_Thread.now {
wenzelm@37241
   232
          for (text_area <- Isabelle.jedit_text_areas if Document_View(text_area).isDefined)
wenzelm@37241
   233
            Document_View(text_area).get.extend_styles()
wenzelm@37241
   234
        }
wenzelm@37241
   235
wenzelm@34791
   236
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   237
wenzelm@34318
   238
      case _ =>
wenzelm@34318
   239
    }
wenzelm@34318
   240
  }
wenzelm@34318
   241
wenzelm@34618
   242
  override def start()
wenzelm@34618
   243
  {
wenzelm@34615
   244
    Isabelle.system = new Isabelle_System
wenzelm@34774
   245
    Isabelle.system.install_fonts()
wenzelm@34777
   246
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   247
  }
wenzelm@34618
   248
wenzelm@34618
   249
  override def stop()
wenzelm@34618
   250
  {
wenzelm@34777
   251
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   252
    Isabelle.session = null
wenzelm@34441
   253
    Isabelle.system = null
wenzelm@34318
   254
  }
wenzelm@34318
   255
}