src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Tue Dec 08 21:48:12 2009 +0100 (2009-12-08 ago)
changeset 34767 8dd19c5f8c56
parent 34764 581e919c8730
child 34772 1a79c9b9af82
permissions -rw-r--r--
added properties_changed event bus;
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@34760
     6
 * @author Makarius
wenzelm@34407
     7
 */
wenzelm@34407
     8
wenzelm@34318
     9
package isabelle.jedit
wenzelm@34318
    10
wenzelm@34429
    11
wenzelm@34429
    12
import java.io.{FileInputStream, IOException}
wenzelm@34318
    13
import java.awt.Font
immler@34406
    14
import javax.swing.JScrollPane
wenzelm@34318
    15
wenzelm@34497
    16
import scala.collection.mutable
immler@34406
    17
wenzelm@34760
    18
import isabelle.proofdocument.{Command, Proof_Document, Prover}
wenzelm@34615
    19
import isabelle.Isabelle_System
wenzelm@34429
    20
wenzelm@34429
    21
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, EditPane, ServiceManager, View}
immler@34406
    22
import org.gjt.sp.jedit.buffer.JEditBuffer
immler@34406
    23
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@34429
    24
import org.gjt.sp.jedit.msg.{EditPaneUpdate, PropertiesChanged}
wenzelm@34429
    25
wenzelm@34318
    26
wenzelm@34618
    27
object Isabelle
wenzelm@34618
    28
{
wenzelm@34618
    29
  /* name */
wenzelm@34618
    30
wenzelm@34318
    31
  val NAME = "Isabelle"
wenzelm@34623
    32
wenzelm@34618
    33
wenzelm@34618
    34
  /* properties */
wenzelm@34618
    35
wenzelm@34618
    36
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    37
wenzelm@34618
    38
  object Property
wenzelm@34618
    39
  {
wenzelm@34618
    40
    def apply(name: String): String = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34468
    41
    def update(name: String, value: String) = jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    42
  }
wenzelm@34433
    43
wenzelm@34618
    44
  object Boolean_Property
wenzelm@34618
    45
  {
wenzelm@34618
    46
    def apply(name: String): Boolean = jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@34618
    47
    def update(name: String, value: Boolean) = jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    48
  }
wenzelm@34618
    49
wenzelm@34618
    50
  object Int_Property
wenzelm@34618
    51
  {
wenzelm@34618
    52
    def apply(name: String): Int = jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@34618
    53
    def update(name: String, value: Int) = jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    54
  }
wenzelm@34618
    55
wenzelm@34618
    56
wenzelm@34618
    57
  /* Isabelle system instance */
wenzelm@34618
    58
wenzelm@34615
    59
  var system: Isabelle_System = null
wenzelm@34487
    60
  def symbols = system.symbols
wenzelm@34612
    61
  lazy val completion = new Completion + symbols
wenzelm@34440
    62
wenzelm@34618
    63
wenzelm@34618
    64
  /* settings */
wenzelm@34618
    65
wenzelm@34696
    66
  def default_logic(): String =
wenzelm@34618
    67
  {
wenzelm@34502
    68
    val logic = Isabelle.Property("logic")
wenzelm@34703
    69
    if (logic != null) logic
wenzelm@34703
    70
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34502
    71
  }
wenzelm@34502
    72
wenzelm@34618
    73
wenzelm@34618
    74
  /* plugin instance */
wenzelm@34618
    75
wenzelm@34440
    76
  var plugin: Plugin = null
wenzelm@34433
    77
wenzelm@34618
    78
wenzelm@34618
    79
  /* running provers */
wenzelm@34618
    80
immler@34475
    81
  def prover_setup(buffer: JEditBuffer) = plugin.prover_setup(buffer)
wenzelm@34318
    82
}
wenzelm@34318
    83
wenzelm@34429
    84
wenzelm@34618
    85
class Plugin extends EBPlugin
wenzelm@34618
    86
{
immler@34685
    87
  /* event buses */
immler@34685
    88
wenzelm@34720
    89
  val state_update = new Event_Bus[Command]
wenzelm@34767
    90
  val properties_changed = new Event_Bus[Unit]
wenzelm@34433
    91
wenzelm@34703
    92
immler@34669
    93
  /* selected state */
immler@34669
    94
immler@34669
    95
  private var _selected_state: Command = null
immler@34669
    96
  def selected_state = _selected_state
immler@34669
    97
  def selected_state_=(state: Command) { _selected_state = state; state_update.event(state) }
immler@34669
    98
immler@34669
    99
wenzelm@34618
   100
  /* mapping buffer <-> prover */
wenzelm@34433
   101
wenzelm@34760
   102
  private val mapping = new mutable.HashMap[JEditBuffer, Prover_Setup]
immler@34406
   103
wenzelm@34618
   104
  private def install(view: View)
wenzelm@34618
   105
  {
immler@34406
   106
    val buffer = view.getBuffer
wenzelm@34760
   107
    val prover_setup = new Prover_Setup(buffer)
immler@34463
   108
    mapping.update(buffer, prover_setup)
immler@34463
   109
    prover_setup.activate(view)
wenzelm@34318
   110
  }
immler@34406
   111
immler@34463
   112
  private def uninstall(view: View) =
immler@34463
   113
    mapping.removeKey(view.getBuffer).get.deactivate
immler@34463
   114
wenzelm@34764
   115
  def switch_active(view: View) =
immler@34463
   116
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
immler@34463
   117
    else install(view)
immler@34406
   118
wenzelm@34760
   119
  def prover_setup(buffer: JEditBuffer): Option[Prover_Setup] = mapping.get(buffer)
wenzelm@34618
   120
  def is_active (buffer: JEditBuffer) = mapping.isDefinedAt(buffer)
wenzelm@34618
   121
wenzelm@34618
   122
wenzelm@34618
   123
  /* main plugin plumbing */
wenzelm@34433
   124
wenzelm@34767
   125
  override def handleMessage(message: EBMessage)
wenzelm@34618
   126
  {
wenzelm@34767
   127
    message match {
wenzelm@34767
   128
      case msg: EditPaneUpdate =>
wenzelm@34767
   129
        val buffer = msg.getEditPane.getBuffer
wenzelm@34767
   130
        msg.getWhat match {
immler@34671
   131
          case EditPaneUpdate.BUFFER_CHANGED =>
immler@34671
   132
            (mapping get buffer) map (_.theory_view.activate)
immler@34671
   133
          case EditPaneUpdate.BUFFER_CHANGING =>
immler@34671
   134
            if (buffer != null)
immler@34671
   135
              (mapping get buffer) map (_.theory_view.deactivate)
immler@34671
   136
          case _ =>
immler@34671
   137
        }
wenzelm@34767
   138
      case msg: PropertiesChanged => properties_changed.event(())
wenzelm@34318
   139
      case _ =>
wenzelm@34318
   140
    }
wenzelm@34318
   141
  }
wenzelm@34318
   142
wenzelm@34618
   143
  override def start()
wenzelm@34618
   144
  {
wenzelm@34751
   145
    Isabelle.plugin = this
wenzelm@34615
   146
    Isabelle.system = new Isabelle_System
wenzelm@34762
   147
    Isabelle.system.register_fonts()
wenzelm@34318
   148
  }
wenzelm@34618
   149
wenzelm@34618
   150
  override def stop()
wenzelm@34618
   151
  {
wenzelm@34429
   152
    // TODO: proper cleanup
wenzelm@34441
   153
    Isabelle.system = null
wenzelm@34440
   154
    Isabelle.plugin = null
wenzelm@34318
   155
  }
wenzelm@34318
   156
}