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