src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Sun May 30 23:42:03 2010 +0200 (2010-05-30 ago)
changeset 37201 8517a650cfdc
parent 37177 17331ca75044
child 37203 c4261f3bbdd7
permissions -rw-r--r--
control tooltip font via Swing HTML, with tooltip-font-size property;
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
  // raw font markup
wenzelm@37201
    87
wenzelm@37201
    88
  def font(name: String, size: Int, body: List[XML.Tree]): XML.Tree =
wenzelm@37201
    89
    XML.Elem("font", List(("face", name), ("size", size.toString)), body)
wenzelm@37201
    90
wenzelm@37201
    91
wenzelm@37201
    92
  def tooltip(text: String): String =
wenzelm@37201
    93
    "<html><font face=\"" + font_family() + "\" size=\"" +
wenzelm@37201
    94
        Int_Property("tooltip-font-size", 4).toString + "\">" +
wenzelm@37201
    95
      HTML.encode(text) + "</font></html>"
wenzelm@37201
    96
wenzelm@37201
    97
wenzelm@34618
    98
  /* settings */
wenzelm@34618
    99
wenzelm@34782
   100
  def default_logic(): String =
wenzelm@34782
   101
  {
wenzelm@34782
   102
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
   103
    if (logic != "") logic
wenzelm@34782
   104
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
   105
  }
wenzelm@34782
   106
wenzelm@34782
   107
  def isabelle_args(): List[String] =
wenzelm@34618
   108
  {
wenzelm@34780
   109
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
   110
    val logic = {
wenzelm@34784
   111
      val logic = Property("logic")
wenzelm@34782
   112
      if (logic != null && logic != "") logic
wenzelm@34782
   113
      else default_logic()
wenzelm@34780
   114
    }
wenzelm@34780
   115
    modes ++ List(logic)
wenzelm@34502
   116
  }
wenzelm@34502
   117
wenzelm@34618
   118
wenzelm@34784
   119
  /* main jEdit components */  // FIXME ownership!?
wenzelm@34784
   120
wenzelm@37177
   121
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@34784
   122
wenzelm@37177
   123
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@34784
   124
wenzelm@34784
   125
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@37177
   126
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@34784
   127
wenzelm@34784
   128
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   129
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   130
wenzelm@34784
   131
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   132
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   133
wenzelm@34784
   134
wenzelm@37068
   135
  /* dockable windows */
wenzelm@37068
   136
wenzelm@37068
   137
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   138
wenzelm@37068
   139
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   140
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   141
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   142
      case _ => None
wenzelm@37068
   143
    }
wenzelm@37068
   144
wenzelm@37068
   145
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   146
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   147
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   148
      case _ => None
wenzelm@37068
   149
    }
wenzelm@37068
   150
wenzelm@37068
   151
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   152
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   153
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   154
      case _ => None
wenzelm@37068
   155
    }
wenzelm@37068
   156
wenzelm@37068
   157
wenzelm@34820
   158
  /* manage prover */
wenzelm@34820
   159
wenzelm@34820
   160
  private def prover_started(view: View): Boolean =
wenzelm@34820
   161
  {
wenzelm@34820
   162
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@34820
   163
    session.start(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   164
      case Some(err) =>
wenzelm@34822
   165
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   166
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   167
        false
wenzelm@34820
   168
      case None => true
wenzelm@34820
   169
    }
wenzelm@34820
   170
  }
wenzelm@34820
   171
wenzelm@34820
   172
wenzelm@34784
   173
  /* activation */
wenzelm@34784
   174
wenzelm@34820
   175
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   176
  {
wenzelm@34820
   177
    if (prover_started(view)) {
wenzelm@34820
   178
      val model = Document_Model.init(session, buffer)
wenzelm@34820
   179
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   180
        Document_View.init(model, text_area)
wenzelm@34820
   181
    }
wenzelm@34784
   182
  }
wenzelm@34784
   183
wenzelm@34784
   184
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   185
  {
wenzelm@34784
   186
    session.stop()  // FIXME not yet
wenzelm@34784
   187
wenzelm@34784
   188
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   189
      Document_View.exit(text_area)
wenzelm@34784
   190
    Document_Model.exit(buffer)
wenzelm@34784
   191
  }
wenzelm@34784
   192
wenzelm@34784
   193
  def switch_active(view: View) =
wenzelm@34784
   194
  {
wenzelm@34784
   195
    val buffer = view.getBuffer
wenzelm@34788
   196
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   197
    else activate_buffer(view, buffer)
wenzelm@34784
   198
  }
wenzelm@34784
   199
wenzelm@34784
   200
  def is_active(view: View): Boolean =
wenzelm@34788
   201
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   202
}
wenzelm@34318
   203
wenzelm@34429
   204
wenzelm@34618
   205
class Plugin extends EBPlugin
wenzelm@34618
   206
{
wenzelm@34618
   207
  /* main plugin plumbing */
wenzelm@34433
   208
wenzelm@34767
   209
  override def handleMessage(message: EBMessage)
wenzelm@34618
   210
  {
wenzelm@34767
   211
    message match {
wenzelm@34767
   212
      case msg: EditPaneUpdate =>
wenzelm@34784
   213
        val edit_pane = msg.getEditPane
wenzelm@34784
   214
        val buffer = edit_pane.getBuffer
wenzelm@34784
   215
        val text_area = edit_pane.getTextArea
wenzelm@34784
   216
wenzelm@34787
   217
        def init_view()
wenzelm@34787
   218
        {
wenzelm@34788
   219
          Document_Model(buffer) match {
wenzelm@34787
   220
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   221
            case None =>
wenzelm@34787
   222
          }
wenzelm@34787
   223
        }
wenzelm@34787
   224
        def exit_view()
wenzelm@34787
   225
        {
wenzelm@34788
   226
          if (Document_View(text_area).isDefined)
wenzelm@34787
   227
            Document_View.exit(text_area)
wenzelm@34787
   228
        }
wenzelm@34767
   229
        msg.getWhat match {
wenzelm@34787
   230
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   231
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   232
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   233
          case _ =>
immler@34671
   234
        }
wenzelm@34784
   235
wenzelm@34777
   236
      case msg: PropertiesChanged =>
wenzelm@34791
   237
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   238
wenzelm@34318
   239
      case _ =>
wenzelm@34318
   240
    }
wenzelm@34318
   241
  }
wenzelm@34318
   242
wenzelm@34618
   243
  override def start()
wenzelm@34618
   244
  {
wenzelm@34615
   245
    Isabelle.system = new Isabelle_System
wenzelm@34774
   246
    Isabelle.system.install_fonts()
wenzelm@34777
   247
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   248
  }
wenzelm@34618
   249
wenzelm@34618
   250
  override def stop()
wenzelm@34618
   251
  {
wenzelm@34777
   252
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   253
    Isabelle.session = null
wenzelm@34441
   254
    Isabelle.system = null
wenzelm@34318
   255
  }
wenzelm@34318
   256
}