src/Tools/jEdit/src/jedit/Plugin.scala
author wenzelm
Fri Jun 26 18:24:03 2009 +0200 (2009-06-26)
changeset 34623 a356a8ee6f00
parent 34618 e45052ff7233
child 34624 5e4f33d033ba
permissions -rw-r--r--
renamed UTF-8-isabelle to UTF-8-Isabelle;
wenzelm@34407
     1
/*
wenzelm@34407
     2
 * Main Isabelle/jEdit plugin setup
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Johannes Hölzl, TU Munich
wenzelm@34407
     5
 * @author Fabian Immler, TU Munich
wenzelm@34407
     6
 */
wenzelm@34407
     7
wenzelm@34318
     8
package isabelle.jedit
wenzelm@34318
     9
wenzelm@34429
    10
wenzelm@34429
    11
import java.io.{FileInputStream, IOException}
wenzelm@34318
    12
import java.awt.Font
immler@34406
    13
import javax.swing.JScrollPane
wenzelm@34318
    14
wenzelm@34497
    15
import scala.collection.mutable
immler@34406
    16
wenzelm@34429
    17
import isabelle.prover.{Prover, Command}
wenzelm@34615
    18
import isabelle.Isabelle_System
wenzelm@34429
    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@34429
    24
wenzelm@34318
    25
wenzelm@34618
    26
object Isabelle
wenzelm@34618
    27
{
wenzelm@34618
    28
  /* name */
wenzelm@34618
    29
wenzelm@34318
    30
  val NAME = "Isabelle"
wenzelm@34337
    31
  val VFS_PREFIX = "isabelle:"
wenzelm@34433
    32
wenzelm@34623
    33
  val ISABELLE_ENCODING = "UTF-8-Isabelle"
wenzelm@34623
    34
wenzelm@34618
    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@34618
    42
    def apply(name: String): String = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34468
    43
    def update(name: String, value: String) = jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    44
  }
wenzelm@34433
    45
wenzelm@34618
    46
  object Boolean_Property
wenzelm@34618
    47
  {
wenzelm@34618
    48
    def apply(name: String): Boolean = jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@34618
    49
    def update(name: String, value: Boolean) = jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    50
  }
wenzelm@34618
    51
wenzelm@34618
    52
  object Int_Property
wenzelm@34618
    53
  {
wenzelm@34618
    54
    def apply(name: String): Int = jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@34618
    55
    def update(name: String, value: Int) = jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    56
  }
wenzelm@34618
    57
wenzelm@34618
    58
wenzelm@34618
    59
  /* Isabelle system instance */
wenzelm@34618
    60
wenzelm@34615
    61
  var system: Isabelle_System = null
wenzelm@34487
    62
  def symbols = system.symbols
wenzelm@34612
    63
  lazy val completion = new Completion + symbols
wenzelm@34440
    64
wenzelm@34618
    65
wenzelm@34618
    66
  /* settings */
wenzelm@34618
    67
wenzelm@34618
    68
  def default_logic =
wenzelm@34618
    69
  {
wenzelm@34502
    70
    val logic = Isabelle.Property("logic")
wenzelm@34502
    71
    if (logic != null) logic else Isabelle.system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34502
    72
  }
wenzelm@34502
    73
wenzelm@34618
    74
wenzelm@34618
    75
  /* plugin instance */
wenzelm@34618
    76
wenzelm@34440
    77
  var plugin: Plugin = null
wenzelm@34433
    78
wenzelm@34618
    79
wenzelm@34618
    80
  /* running provers */
wenzelm@34618
    81
immler@34475
    82
  def prover_setup(buffer: JEditBuffer) = plugin.prover_setup(buffer)
wenzelm@34318
    83
}
wenzelm@34318
    84
wenzelm@34429
    85
wenzelm@34618
    86
class Plugin extends EBPlugin
wenzelm@34618
    87
{
wenzelm@34618
    88
  /* Isabelle font */
wenzelm@34433
    89
wenzelm@34433
    90
  var font: Font = null
wenzelm@34456
    91
  val font_changed = new EventBus[Font]
immler@34406
    92
wenzelm@34618
    93
  def set_font(size: Int)
wenzelm@34618
    94
  {
wenzelm@34618
    95
    font = Font.createFont(Font.TRUETYPE_FONT,
wenzelm@34618
    96
        new FileInputStream(Isabelle.system.platform_path("~~/lib/fonts/IsabelleMono.ttf"))).
wenzelm@34618
    97
      deriveFont(Font.PLAIN, size max 1)
wenzelm@34456
    98
    font_changed.event(font)
wenzelm@34433
    99
  }
wenzelm@34433
   100
wenzelm@34433
   101
wenzelm@34618
   102
  /* mapping buffer <-> prover */
wenzelm@34433
   103
wenzelm@34497
   104
  private val mapping = new mutable.HashMap[JEditBuffer, ProverSetup]
immler@34406
   105
wenzelm@34618
   106
  private def install(view: View)
wenzelm@34618
   107
  {
immler@34406
   108
    val buffer = view.getBuffer
immler@34463
   109
    val prover_setup = new ProverSetup(buffer)
immler@34463
   110
    mapping.update(buffer, prover_setup)
immler@34463
   111
    prover_setup.activate(view)
wenzelm@34318
   112
  }
immler@34406
   113
immler@34463
   114
  private def uninstall(view: View) =
immler@34463
   115
    mapping.removeKey(view.getBuffer).get.deactivate
immler@34463
   116
wenzelm@34618
   117
  def switch_active (view: View) =
immler@34463
   118
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
immler@34463
   119
    else install(view)
immler@34406
   120
wenzelm@34618
   121
  def prover_setup(buffer: JEditBuffer): Option[ProverSetup] = mapping.get(buffer)
wenzelm@34618
   122
  def is_active (buffer: JEditBuffer) = mapping.isDefinedAt(buffer)
wenzelm@34618
   123
wenzelm@34618
   124
wenzelm@34618
   125
  /* main plugin plumbing */
wenzelm@34433
   126
wenzelm@34618
   127
  override def handleMessage(msg: EBMessage)
wenzelm@34618
   128
  {
wenzelm@34618
   129
    msg match {
wenzelm@34618
   130
      case epu: EditPaneUpdate => epu.getWhat match {
wenzelm@34618
   131
        case EditPaneUpdate.BUFFER_CHANGED =>
wenzelm@34618
   132
          mapping get epu.getEditPane.getBuffer match {
wenzelm@34618
   133
            // only activate 'isabelle' buffers
wenzelm@34618
   134
            case None =>
wenzelm@34618
   135
            case Some(prover_setup) =>
wenzelm@34618
   136
              prover_setup.theory_view.activate
wenzelm@34618
   137
              val dockable =
wenzelm@34618
   138
                epu.getEditPane.getView.getDockableWindowManager.getDockable("isabelle-output")
wenzelm@34618
   139
              if (dockable != null) {
wenzelm@34618
   140
                val output_dockable = dockable.asInstanceOf[OutputDockable]
wenzelm@34618
   141
                if (output_dockable.getComponent(0) != prover_setup.output_text_view ) {
wenzelm@34618
   142
                  output_dockable.asInstanceOf[OutputDockable].removeAll
wenzelm@34618
   143
                  output_dockable.asInstanceOf[OutputDockable].
wenzelm@34618
   144
                    add(new JScrollPane(prover_setup.output_text_view))
wenzelm@34618
   145
                  output_dockable.revalidate
wenzelm@34618
   146
                }
immler@34406
   147
              }
wenzelm@34618
   148
          }
wenzelm@34618
   149
        case EditPaneUpdate.BUFFER_CHANGING =>
wenzelm@34618
   150
          val buffer = epu.getEditPane.getBuffer
wenzelm@34618
   151
          if (buffer != null) mapping get buffer match {
wenzelm@34618
   152
            // only deactivate 'isabelle' buffers
wenzelm@34618
   153
            case None =>
wenzelm@34618
   154
            case Some(prover_setup) => prover_setup.theory_view.deactivate
wenzelm@34618
   155
          }
wenzelm@34618
   156
        case _ =>
wenzelm@34618
   157
      }
wenzelm@34318
   158
      case _ =>
wenzelm@34318
   159
    }
wenzelm@34318
   160
  }
wenzelm@34318
   161
wenzelm@34618
   162
  override def start()
wenzelm@34618
   163
  {
wenzelm@34615
   164
    Isabelle.system = new Isabelle_System
wenzelm@34440
   165
    Isabelle.plugin = this
wenzelm@34618
   166
    set_font(Isabelle.Int_Property("font-size"))
wenzelm@34318
   167
  }
wenzelm@34618
   168
wenzelm@34618
   169
  override def stop()
wenzelm@34618
   170
  {
wenzelm@34429
   171
    // TODO: proper cleanup
wenzelm@34441
   172
    Isabelle.system = null
wenzelm@34440
   173
    Isabelle.plugin = null
wenzelm@34318
   174
  }
wenzelm@34618
   175
wenzelm@34318
   176
}