src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Tue Dec 15 00:21:21 2009 +0100 (2009-12-15)
changeset 34782 fcd6a41326a6
parent 34781 6c2372c4aefb
child 34784 02959dcea756
permissions -rw-r--r--
refined treatment of default logic concerning property and GUI;
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@34782
    58
  def default_logic(): String =
wenzelm@34782
    59
  {
wenzelm@34782
    60
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
    61
    if (logic != "") logic
wenzelm@34782
    62
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
    63
  }
wenzelm@34782
    64
wenzelm@34782
    65
  def isabelle_args(): List[String] =
wenzelm@34618
    66
  {
wenzelm@34780
    67
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
    68
    val logic = {
wenzelm@34782
    69
      val logic = Isabelle.Property("logic")
wenzelm@34782
    70
      if (logic != null && logic != "") logic
wenzelm@34782
    71
      else default_logic()
wenzelm@34780
    72
    }
wenzelm@34780
    73
    modes ++ List(logic)
wenzelm@34502
    74
  }
wenzelm@34502
    75
wenzelm@34618
    76
wenzelm@34618
    77
  /* plugin instance */
wenzelm@34618
    78
wenzelm@34440
    79
  var plugin: Plugin = null
wenzelm@34777
    80
  var system: Isabelle_System = null
wenzelm@34777
    81
  var session: Session = null
wenzelm@34318
    82
}
wenzelm@34318
    83
wenzelm@34429
    84
wenzelm@34618
    85
class Plugin extends EBPlugin
wenzelm@34618
    86
{
wenzelm@34777
    87
  /* mapping buffer <-> theory view */
immler@34669
    88
wenzelm@34777
    89
  private var mapping = Map[JEditBuffer, Theory_View]()
immler@34406
    90
wenzelm@34618
    91
  private def install(view: View)
wenzelm@34618
    92
  {
wenzelm@34777
    93
    val text_area = view.getTextArea
immler@34406
    94
    val buffer = view.getBuffer
wenzelm@34777
    95
wenzelm@34777
    96
 
wenzelm@34777
    97
    val theory_view = new Theory_View(Isabelle.session, text_area)   // FIXME multiple text areas!?
wenzelm@34777
    98
    mapping += (buffer -> theory_view)
wenzelm@34777
    99
wenzelm@34782
   100
    Isabelle.session.start(Isabelle.isabelle_args())
wenzelm@34777
   101
    theory_view.activate()
wenzelm@34777
   102
    Isabelle.session.begin_document(buffer.getName)
wenzelm@34318
   103
  }
immler@34406
   104
wenzelm@34777
   105
  private def uninstall(view: View)
wenzelm@34777
   106
  {
wenzelm@34777
   107
    val buffer = view.getBuffer
wenzelm@34779
   108
    Isabelle.session.stop()
wenzelm@34779
   109
    mapping(buffer).deactivate()
wenzelm@34777
   110
    mapping -= buffer
wenzelm@34777
   111
  }
immler@34463
   112
wenzelm@34764
   113
  def switch_active(view: View) =
immler@34463
   114
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
immler@34463
   115
    else install(view)
immler@34406
   116
wenzelm@34777
   117
  def theory_view(buffer: JEditBuffer): Option[Theory_View] = mapping.get(buffer)
wenzelm@34777
   118
  def is_active(buffer: JEditBuffer) = mapping.isDefinedAt(buffer)
wenzelm@34618
   119
wenzelm@34618
   120
wenzelm@34618
   121
  /* main plugin plumbing */
wenzelm@34433
   122
wenzelm@34767
   123
  override def handleMessage(message: EBMessage)
wenzelm@34618
   124
  {
wenzelm@34767
   125
    message match {
wenzelm@34767
   126
      case msg: EditPaneUpdate =>
wenzelm@34767
   127
        val buffer = msg.getEditPane.getBuffer
wenzelm@34767
   128
        msg.getWhat match {
immler@34671
   129
          case EditPaneUpdate.BUFFER_CHANGED =>
wenzelm@34777
   130
            theory_view(buffer)map(_.activate)
immler@34671
   131
          case EditPaneUpdate.BUFFER_CHANGING =>
immler@34671
   132
            if (buffer != null)
wenzelm@34777
   133
              theory_view(buffer).map(_.deactivate)
immler@34671
   134
          case _ =>
immler@34671
   135
        }
wenzelm@34777
   136
      case msg: PropertiesChanged =>
wenzelm@34777
   137
        Isabelle.session.global_settings.event(())
wenzelm@34318
   138
      case _ =>
wenzelm@34318
   139
    }
wenzelm@34318
   140
  }
wenzelm@34318
   141
wenzelm@34618
   142
  override def start()
wenzelm@34618
   143
  {
wenzelm@34751
   144
    Isabelle.plugin = this
wenzelm@34615
   145
    Isabelle.system = new Isabelle_System
wenzelm@34774
   146
    Isabelle.system.install_fonts()
wenzelm@34777
   147
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   148
  }
wenzelm@34618
   149
wenzelm@34618
   150
  override def stop()
wenzelm@34618
   151
  {
wenzelm@34777
   152
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   153
    Isabelle.session = null
wenzelm@34441
   154
    Isabelle.system = null
wenzelm@34440
   155
    Isabelle.plugin = null
wenzelm@34318
   156
  }
wenzelm@34318
   157
}