src/Tools/jEdit/src/jedit/Plugin.scala
author immler@in.tum.de
Thu Aug 27 10:51:09 2009 +0200 (2009-08-27)
changeset 34671 d179fcb04cbc
parent 34670 c99878d7bec7
child 34685 93f4978fe2a8
permissions -rw-r--r--
output_info specific to prover
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@34623
    31
wenzelm@34618
    32
wenzelm@34618
    33
  /* properties */
wenzelm@34618
    34
wenzelm@34618
    35
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    36
wenzelm@34618
    37
  object Property
wenzelm@34618
    38
  {
wenzelm@34618
    39
    def apply(name: String): String = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34468
    40
    def update(name: String, value: String) = jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    41
  }
wenzelm@34433
    42
wenzelm@34618
    43
  object Boolean_Property
wenzelm@34618
    44
  {
wenzelm@34618
    45
    def apply(name: String): Boolean = jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@34618
    46
    def update(name: String, value: Boolean) = jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    47
  }
wenzelm@34618
    48
wenzelm@34618
    49
  object Int_Property
wenzelm@34618
    50
  {
wenzelm@34618
    51
    def apply(name: String): Int = jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@34618
    52
    def update(name: String, value: Int) = jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    53
  }
wenzelm@34618
    54
wenzelm@34618
    55
wenzelm@34618
    56
  /* Isabelle system instance */
wenzelm@34618
    57
wenzelm@34615
    58
  var system: Isabelle_System = null
wenzelm@34487
    59
  def symbols = system.symbols
wenzelm@34612
    60
  lazy val completion = new Completion + symbols
wenzelm@34440
    61
wenzelm@34618
    62
wenzelm@34618
    63
  /* settings */
wenzelm@34618
    64
wenzelm@34618
    65
  def default_logic =
wenzelm@34618
    66
  {
wenzelm@34502
    67
    val logic = Isabelle.Property("logic")
wenzelm@34502
    68
    if (logic != null) logic else Isabelle.system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34502
    69
  }
wenzelm@34502
    70
wenzelm@34618
    71
wenzelm@34618
    72
  /* plugin instance */
wenzelm@34618
    73
wenzelm@34440
    74
  var plugin: Plugin = null
wenzelm@34433
    75
wenzelm@34618
    76
wenzelm@34618
    77
  /* running provers */
wenzelm@34618
    78
immler@34475
    79
  def prover_setup(buffer: JEditBuffer) = plugin.prover_setup(buffer)
wenzelm@34318
    80
}
wenzelm@34318
    81
wenzelm@34429
    82
wenzelm@34618
    83
class Plugin extends EBPlugin
wenzelm@34618
    84
{
wenzelm@34618
    85
  /* Isabelle font */
wenzelm@34433
    86
wenzelm@34433
    87
  var font: Font = null
wenzelm@34456
    88
  val font_changed = new EventBus[Font]
immler@34406
    89
wenzelm@34618
    90
  def set_font(size: Int)
wenzelm@34618
    91
  {
wenzelm@34618
    92
    font = Font.createFont(Font.TRUETYPE_FONT,
wenzelm@34627
    93
        Isabelle.system.platform_file("~~/lib/fonts/IsabelleMono.ttf")).
wenzelm@34627
    94
      deriveFont(Font.PLAIN, (size max 1).toFloat)
wenzelm@34456
    95
    font_changed.event(font)
wenzelm@34433
    96
  }
wenzelm@34433
    97
wenzelm@34433
    98
immler@34669
    99
  /* selected state */
immler@34669
   100
immler@34669
   101
  val state_update = new EventBus[Command]
immler@34669
   102
immler@34669
   103
  private var _selected_state: Command = null
immler@34669
   104
  def selected_state = _selected_state
immler@34669
   105
  def selected_state_=(state: Command) { _selected_state = state; state_update.event(state) }
immler@34669
   106
immler@34669
   107
wenzelm@34618
   108
  /* mapping buffer <-> prover */
wenzelm@34433
   109
wenzelm@34497
   110
  private val mapping = new mutable.HashMap[JEditBuffer, ProverSetup]
immler@34406
   111
wenzelm@34618
   112
  private def install(view: View)
wenzelm@34618
   113
  {
immler@34406
   114
    val buffer = view.getBuffer
immler@34463
   115
    val prover_setup = new ProverSetup(buffer)
immler@34463
   116
    mapping.update(buffer, prover_setup)
immler@34463
   117
    prover_setup.activate(view)
wenzelm@34318
   118
  }
immler@34406
   119
immler@34463
   120
  private def uninstall(view: View) =
immler@34463
   121
    mapping.removeKey(view.getBuffer).get.deactivate
immler@34463
   122
wenzelm@34618
   123
  def switch_active (view: View) =
immler@34463
   124
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
immler@34463
   125
    else install(view)
immler@34406
   126
wenzelm@34618
   127
  def prover_setup(buffer: JEditBuffer): Option[ProverSetup] = mapping.get(buffer)
wenzelm@34618
   128
  def is_active (buffer: JEditBuffer) = mapping.isDefinedAt(buffer)
wenzelm@34618
   129
wenzelm@34618
   130
wenzelm@34618
   131
  /* main plugin plumbing */
wenzelm@34433
   132
wenzelm@34618
   133
  override def handleMessage(msg: EBMessage)
wenzelm@34618
   134
  {
wenzelm@34618
   135
    msg match {
immler@34671
   136
      case epu: EditPaneUpdate =>
immler@34671
   137
        val buffer = epu.getEditPane.getBuffer
immler@34671
   138
        epu.getWhat match {
immler@34671
   139
          case EditPaneUpdate.BUFFER_CHANGED =>
immler@34671
   140
            (mapping get buffer) map (_.theory_view.activate)
immler@34671
   141
          case EditPaneUpdate.BUFFER_CHANGING =>
immler@34671
   142
            if (buffer != null)
immler@34671
   143
              (mapping get buffer) map (_.theory_view.deactivate)
immler@34671
   144
          case _ =>
immler@34671
   145
        }
wenzelm@34318
   146
      case _ =>
wenzelm@34318
   147
    }
wenzelm@34318
   148
  }
wenzelm@34318
   149
wenzelm@34618
   150
  override def start()
wenzelm@34618
   151
  {
wenzelm@34615
   152
    Isabelle.system = new Isabelle_System
wenzelm@34440
   153
    Isabelle.plugin = this
wenzelm@34618
   154
    set_font(Isabelle.Int_Property("font-size"))
wenzelm@34318
   155
  }
wenzelm@34618
   156
wenzelm@34618
   157
  override def stop()
wenzelm@34618
   158
  {
wenzelm@34429
   159
    // TODO: proper cleanup
wenzelm@34441
   160
    Isabelle.system = null
wenzelm@34440
   161
    Isabelle.plugin = null
wenzelm@34318
   162
  }
wenzelm@34618
   163
wenzelm@34318
   164
}