src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Thu Dec 10 22:15:19 2009 +0100 (2009-12-10 ago)
changeset 34777 91d6089cef88
parent 34774 1fa466333361
child 34779 d1040b77b189
permissions -rw-r--r--
class Session models full session, with or without prover process (cf. heaps, browser_info);
replaced Prover by Session, with a singleton instance in Isabelle plugin (shared by all active buffers);
misc cleanup of Session vs. Plugin instance;
eliminated Prover_Setup -- maintain mapping of JEditBuffer <-> Theory_View directly;
misc tuning and simplification;
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@34777
    12
import isabelle.proofdocument.{Session, Theory_View}
wenzelm@34777
    13
wenzelm@34429
    14
import java.io.{FileInputStream, IOException}
wenzelm@34318
    15
import java.awt.Font
immler@34406
    16
import javax.swing.JScrollPane
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@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
  /* settings */
wenzelm@34618
    57
wenzelm@34777
    58
  def get_logic(): String =
wenzelm@34618
    59
  {
wenzelm@34502
    60
    val logic = Isabelle.Property("logic")
wenzelm@34703
    61
    if (logic != null) logic
wenzelm@34703
    62
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34502
    63
  }
wenzelm@34502
    64
wenzelm@34618
    65
wenzelm@34618
    66
  /* plugin instance */
wenzelm@34618
    67
wenzelm@34440
    68
  var plugin: Plugin = null
wenzelm@34777
    69
  var system: Isabelle_System = null
wenzelm@34777
    70
  var session: Session = null
wenzelm@34318
    71
}
wenzelm@34318
    72
wenzelm@34429
    73
wenzelm@34618
    74
class Plugin extends EBPlugin
wenzelm@34618
    75
{
wenzelm@34777
    76
  /* mapping buffer <-> theory view */
immler@34669
    77
wenzelm@34777
    78
  private var mapping = Map[JEditBuffer, Theory_View]()
immler@34406
    79
wenzelm@34618
    80
  private def install(view: View)
wenzelm@34618
    81
  {
wenzelm@34777
    82
    val text_area = view.getTextArea
immler@34406
    83
    val buffer = view.getBuffer
wenzelm@34777
    84
wenzelm@34777
    85
 
wenzelm@34777
    86
    val theory_view = new Theory_View(Isabelle.session, text_area)   // FIXME multiple text areas!?
wenzelm@34777
    87
    mapping += (buffer -> theory_view)
wenzelm@34777
    88
wenzelm@34777
    89
    Isabelle.session.start(Isabelle.get_logic())
wenzelm@34777
    90
    theory_view.activate()
wenzelm@34777
    91
    Isabelle.session.begin_document(buffer.getName)
wenzelm@34318
    92
  }
immler@34406
    93
wenzelm@34777
    94
  private def uninstall(view: View)
wenzelm@34777
    95
  {
wenzelm@34777
    96
    val buffer = view.getBuffer
wenzelm@34777
    97
    mapping(buffer).deactivate
wenzelm@34777
    98
    mapping -= buffer
wenzelm@34777
    99
  }
immler@34463
   100
wenzelm@34764
   101
  def switch_active(view: View) =
immler@34463
   102
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
immler@34463
   103
    else install(view)
immler@34406
   104
wenzelm@34777
   105
  def theory_view(buffer: JEditBuffer): Option[Theory_View] = mapping.get(buffer)
wenzelm@34777
   106
  def is_active(buffer: JEditBuffer) = mapping.isDefinedAt(buffer)
wenzelm@34618
   107
wenzelm@34618
   108
wenzelm@34618
   109
  /* main plugin plumbing */
wenzelm@34433
   110
wenzelm@34767
   111
  override def handleMessage(message: EBMessage)
wenzelm@34618
   112
  {
wenzelm@34767
   113
    message match {
wenzelm@34767
   114
      case msg: EditPaneUpdate =>
wenzelm@34767
   115
        val buffer = msg.getEditPane.getBuffer
wenzelm@34767
   116
        msg.getWhat match {
immler@34671
   117
          case EditPaneUpdate.BUFFER_CHANGED =>
wenzelm@34777
   118
            theory_view(buffer)map(_.activate)
immler@34671
   119
          case EditPaneUpdate.BUFFER_CHANGING =>
immler@34671
   120
            if (buffer != null)
wenzelm@34777
   121
              theory_view(buffer).map(_.deactivate)
immler@34671
   122
          case _ =>
immler@34671
   123
        }
wenzelm@34777
   124
      case msg: PropertiesChanged =>
wenzelm@34777
   125
        Isabelle.session.global_settings.event(())
wenzelm@34318
   126
      case _ =>
wenzelm@34318
   127
    }
wenzelm@34318
   128
  }
wenzelm@34318
   129
wenzelm@34618
   130
  override def start()
wenzelm@34618
   131
  {
wenzelm@34751
   132
    Isabelle.plugin = this
wenzelm@34615
   133
    Isabelle.system = new Isabelle_System
wenzelm@34774
   134
    Isabelle.system.install_fonts()
wenzelm@34777
   135
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   136
  }
wenzelm@34618
   137
wenzelm@34618
   138
  override def stop()
wenzelm@34618
   139
  {
wenzelm@34777
   140
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   141
    Isabelle.session = null
wenzelm@34441
   142
    Isabelle.system = null
wenzelm@34440
   143
    Isabelle.plugin = null
wenzelm@34318
   144
  }
wenzelm@34318
   145
}