src/Tools/jEdit/src/jedit/Plugin.scala
author wenzelm
Tue Jan 27 18:58:16 2009 +0100 (2009-01-27 ago)
changeset 34502 7d1d13750890
parent 34497 184fda8cce04
child 34503 7d0726f19d04
permissions -rw-r--r--
added Isabelle.default_logic;
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@34487
    18
import isabelle.IsabelleSystem
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@34440
    26
object Isabelle {
wenzelm@34433
    27
  // name
wenzelm@34318
    28
  val NAME = "Isabelle"
wenzelm@34337
    29
  val VFS_PREFIX = "isabelle:"
wenzelm@34433
    30
wenzelm@34433
    31
  // properties
wenzelm@34468
    32
  object Property {
wenzelm@34468
    33
    private val OPTION_PREFIX = "options.isabelle."
wenzelm@34468
    34
    def apply(name: String) = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34468
    35
    def update(name: String, value: String) = jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    36
  }
wenzelm@34433
    37
wenzelm@34487
    38
  // Isabelle system instance
wenzelm@34441
    39
  var system: IsabelleSystem = null
wenzelm@34487
    40
  def symbols = system.symbols
wenzelm@34440
    41
wenzelm@34502
    42
  // settings
wenzelm@34502
    43
  def default_logic = {
wenzelm@34502
    44
    val logic = Isabelle.Property("logic")
wenzelm@34502
    45
    if (logic != null) logic else Isabelle.system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34502
    46
  }
wenzelm@34502
    47
wenzelm@34440
    48
  // plugin instance
wenzelm@34440
    49
  var plugin: Plugin = null
wenzelm@34433
    50
wenzelm@34487
    51
  // running provers
immler@34475
    52
  def prover_setup(buffer: JEditBuffer) = plugin.prover_setup(buffer)
wenzelm@34318
    53
}
wenzelm@34318
    54
wenzelm@34429
    55
wenzelm@34318
    56
class Plugin extends EBPlugin {
wenzelm@34433
    57
wenzelm@34433
    58
  // Isabelle font
wenzelm@34433
    59
wenzelm@34433
    60
  var font: Font = null
wenzelm@34456
    61
  val font_changed = new EventBus[Font]
immler@34406
    62
wenzelm@34433
    63
  def set_font(path: String, size: Float) {
wenzelm@34456
    64
    font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path)).
wenzelm@34456
    65
      deriveFont(Font.PLAIN, size)
wenzelm@34456
    66
    font_changed.event(font)
wenzelm@34433
    67
  }
wenzelm@34433
    68
wenzelm@34433
    69
wenzelm@34449
    70
  /* unique ids */  // FIXME specific to "session" (!??)
wenzelm@34449
    71
wenzelm@34449
    72
  private var id_count: BigInt = 0
wenzelm@34449
    73
  def id() : String = synchronized { id_count += 1; "editor:" + id_count }
wenzelm@34449
    74
wenzelm@34449
    75
wenzelm@34433
    76
  // mapping buffer <-> prover
wenzelm@34433
    77
wenzelm@34497
    78
  private val mapping = new mutable.HashMap[JEditBuffer, ProverSetup]
immler@34406
    79
immler@34463
    80
  private def install(view: View) {
immler@34406
    81
    val buffer = view.getBuffer
immler@34463
    82
    val prover_setup = new ProverSetup(buffer)
immler@34463
    83
    mapping.update(buffer, prover_setup)
immler@34463
    84
    prover_setup.activate(view)
wenzelm@34318
    85
  }
immler@34406
    86
immler@34463
    87
  private def uninstall(view: View) =
immler@34463
    88
    mapping.removeKey(view.getBuffer).get.deactivate
immler@34463
    89
immler@34463
    90
  def switch_active (view : View) =
immler@34463
    91
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
immler@34463
    92
    else install(view)
immler@34406
    93
immler@34406
    94
  def prover_setup (buffer : JEditBuffer) : Option[ProverSetup] = mapping.get(buffer)
immler@34462
    95
  def is_active (buffer : JEditBuffer) = mapping.isDefinedAt(buffer)
immler@34463
    96
  
wenzelm@34318
    97
  
wenzelm@34433
    98
  // main plugin plumbing
wenzelm@34433
    99
wenzelm@34429
   100
  override def handleMessage(msg: EBMessage) = msg match {
wenzelm@34433
   101
    case epu: EditPaneUpdate => epu.getWhat match {
wenzelm@34318
   102
      case EditPaneUpdate.BUFFER_CHANGED =>
immler@34406
   103
        mapping get epu.getEditPane.getBuffer match {
immler@34406
   104
          //only activate 'isabelle'-buffers!
immler@34406
   105
          case None =>
immler@34406
   106
          case Some(prover_setup) => 
immler@34406
   107
            prover_setup.theory_view.activate
wenzelm@34422
   108
            val dockable = epu.getEditPane.getView.getDockableWindowManager.getDockable("isabelle-output")
immler@34406
   109
            if(dockable != null) {
immler@34406
   110
              val output_dockable = dockable.asInstanceOf[OutputDockable]
immler@34406
   111
              if(output_dockable.getComponent(0) != prover_setup.output_text_view ) {
immler@34406
   112
                output_dockable.asInstanceOf[OutputDockable].removeAll
immler@34406
   113
                output_dockable.asInstanceOf[OutputDockable].add(new JScrollPane(prover_setup.output_text_view))
immler@34406
   114
                output_dockable.revalidate
immler@34406
   115
              }
immler@34406
   116
            }
immler@34406
   117
        }
wenzelm@34318
   118
      case EditPaneUpdate.BUFFER_CHANGING =>
immler@34406
   119
        val buffer = epu.getEditPane.getBuffer
immler@34406
   120
        if(buffer != null) mapping get buffer match {
immler@34406
   121
          //only deactivate 'isabelle'-buffers!
immler@34406
   122
          case None =>
immler@34406
   123
          case Some(prover_setup) => prover_setup.theory_view.deactivate
immler@34406
   124
        }
wenzelm@34318
   125
      case _ =>
wenzelm@34318
   126
    }
wenzelm@34318
   127
    case _ =>
wenzelm@34318
   128
  }
wenzelm@34318
   129
wenzelm@34318
   130
  override def start() {
wenzelm@34441
   131
    Isabelle.system = new IsabelleSystem
wenzelm@34440
   132
    Isabelle.plugin = this
immler@34406
   133
    
wenzelm@34468
   134
    if (Isabelle.Property("font-path") != null && Isabelle.Property("font-size") != null)
wenzelm@34318
   135
      try {
wenzelm@34468
   136
        set_font(Isabelle.Property("font-path"), Isabelle.Property("font-size").toFloat)
wenzelm@34318
   137
      }
wenzelm@34318
   138
      catch {
wenzelm@34429
   139
        case e: NumberFormatException =>
wenzelm@34318
   140
      }
wenzelm@34318
   141
  }
wenzelm@34318
   142
  
wenzelm@34318
   143
  override def stop() {
wenzelm@34429
   144
    // TODO: proper cleanup
wenzelm@34441
   145
    Isabelle.system = null
wenzelm@34440
   146
    Isabelle.plugin = null
wenzelm@34318
   147
  }
wenzelm@34318
   148
}