src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Tue Dec 15 19:50:55 2009 +0100 (2009-12-15)
changeset 34784 02959dcea756
parent 34782 fcd6a41326a6
child 34787 0feac35069c6
permissions -rw-r--r--
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
incorporate Document_Overview into Document_View;
eliminated Plugin.mapping in favour of direct association via generic properties;
support several views per model;
misc tuning;
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@34784
    12
import isabelle.proofdocument.Session
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@34784
    28
  /* plugin instance */
wenzelm@34784
    29
wenzelm@34784
    30
  var plugin: Plugin = null
wenzelm@34784
    31
  var system: Isabelle_System = null
wenzelm@34784
    32
  var session: Session = null
wenzelm@34784
    33
wenzelm@34784
    34
wenzelm@34618
    35
  /* name */
wenzelm@34618
    36
wenzelm@34318
    37
  val NAME = "Isabelle"
wenzelm@34623
    38
wenzelm@34618
    39
wenzelm@34618
    40
  /* properties */
wenzelm@34618
    41
wenzelm@34618
    42
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    43
wenzelm@34618
    44
  object Property
wenzelm@34618
    45
  {
wenzelm@34618
    46
    def apply(name: String): String = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34468
    47
    def update(name: String, value: String) = jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    48
  }
wenzelm@34433
    49
wenzelm@34618
    50
  object Boolean_Property
wenzelm@34618
    51
  {
wenzelm@34618
    52
    def apply(name: String): Boolean = jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@34618
    53
    def update(name: String, value: Boolean) = jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    54
  }
wenzelm@34618
    55
wenzelm@34618
    56
  object Int_Property
wenzelm@34618
    57
  {
wenzelm@34618
    58
    def apply(name: String): Int = jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@34618
    59
    def update(name: String, value: Int) = jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    60
  }
wenzelm@34618
    61
wenzelm@34618
    62
wenzelm@34618
    63
  /* settings */
wenzelm@34618
    64
wenzelm@34782
    65
  def default_logic(): String =
wenzelm@34782
    66
  {
wenzelm@34782
    67
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
    68
    if (logic != "") logic
wenzelm@34782
    69
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
    70
  }
wenzelm@34782
    71
wenzelm@34782
    72
  def isabelle_args(): List[String] =
wenzelm@34618
    73
  {
wenzelm@34780
    74
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
    75
    val logic = {
wenzelm@34784
    76
      val logic = Property("logic")
wenzelm@34782
    77
      if (logic != null && logic != "") logic
wenzelm@34782
    78
      else default_logic()
wenzelm@34780
    79
    }
wenzelm@34780
    80
    modes ++ List(logic)
wenzelm@34502
    81
  }
wenzelm@34502
    82
wenzelm@34618
    83
wenzelm@34784
    84
  /* main jEdit components */  // FIXME ownership!?
wenzelm@34784
    85
wenzelm@34784
    86
  def jedit_buffers(): Iterator[Buffer] = Iterator.fromArray(jEdit.getBuffers())
wenzelm@34784
    87
wenzelm@34784
    88
  def jedit_views(): Iterator[View] = Iterator.fromArray(jEdit.getViews())
wenzelm@34784
    89
wenzelm@34784
    90
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@34784
    91
    Iterator.fromArray(view.getEditPanes).map(_.getTextArea)
wenzelm@34784
    92
wenzelm@34784
    93
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
    94
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
    95
wenzelm@34784
    96
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
    97
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
    98
wenzelm@34784
    99
wenzelm@34784
   100
  /* activation */
wenzelm@34784
   101
wenzelm@34784
   102
  def activate_buffer(buffer: Buffer)
wenzelm@34784
   103
  {
wenzelm@34784
   104
    val model = Document_Model.init(session, buffer)
wenzelm@34784
   105
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   106
      Document_View.init(model, text_area)
wenzelm@34618
   107
wenzelm@34784
   108
    session.start(Isabelle.isabelle_args())
wenzelm@34784
   109
    // FIXME theory_view.activate()
wenzelm@34784
   110
    session.begin_document(buffer.getName)
wenzelm@34784
   111
  }
wenzelm@34784
   112
wenzelm@34784
   113
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   114
  {
wenzelm@34784
   115
    session.stop()  // FIXME not yet
wenzelm@34784
   116
wenzelm@34784
   117
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   118
      Document_View.exit(text_area)
wenzelm@34784
   119
    Document_Model.exit(buffer)
wenzelm@34784
   120
  }
wenzelm@34784
   121
wenzelm@34784
   122
  def switch_active(view: View) =
wenzelm@34784
   123
  {
wenzelm@34784
   124
    val buffer = view.getBuffer
wenzelm@34784
   125
    if (Document_Model.get(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34784
   126
    else activate_buffer(buffer)
wenzelm@34784
   127
  }
wenzelm@34784
   128
wenzelm@34784
   129
  def is_active(view: View): Boolean =
wenzelm@34784
   130
    Document_Model.get(view.getBuffer).isDefined
wenzelm@34318
   131
}
wenzelm@34318
   132
wenzelm@34429
   133
wenzelm@34618
   134
class Plugin extends EBPlugin
wenzelm@34618
   135
{
wenzelm@34618
   136
  /* main plugin plumbing */
wenzelm@34433
   137
wenzelm@34767
   138
  override def handleMessage(message: EBMessage)
wenzelm@34618
   139
  {
wenzelm@34767
   140
    message match {
wenzelm@34767
   141
      case msg: EditPaneUpdate =>
wenzelm@34784
   142
        val edit_pane = msg.getEditPane
wenzelm@34784
   143
        val buffer = edit_pane.getBuffer
wenzelm@34784
   144
        val text_area = edit_pane.getTextArea
wenzelm@34784
   145
wenzelm@34767
   146
        msg.getWhat match {
immler@34671
   147
          case EditPaneUpdate.BUFFER_CHANGED =>
wenzelm@34784
   148
            if (Document_View.get(text_area).isDefined)
wenzelm@34784
   149
              Document_View.exit(text_area)
wenzelm@34784
   150
            Document_Model.get(buffer) match {
wenzelm@34784
   151
              case Some(model) => Document_View.init(model, text_area)
wenzelm@34784
   152
              case None =>
wenzelm@34784
   153
            }
wenzelm@34784
   154
wenzelm@34784
   155
          case EditPaneUpdate.DESTROYED =>
wenzelm@34784
   156
            if (Document_View.get(text_area).isDefined)
wenzelm@34784
   157
              Document_View.exit(text_area)
wenzelm@34784
   158
immler@34671
   159
          case _ =>
immler@34671
   160
        }
wenzelm@34784
   161
wenzelm@34777
   162
      case msg: PropertiesChanged =>
wenzelm@34777
   163
        Isabelle.session.global_settings.event(())
wenzelm@34784
   164
wenzelm@34318
   165
      case _ =>
wenzelm@34318
   166
    }
wenzelm@34318
   167
  }
wenzelm@34318
   168
wenzelm@34618
   169
  override def start()
wenzelm@34618
   170
  {
wenzelm@34751
   171
    Isabelle.plugin = this
wenzelm@34615
   172
    Isabelle.system = new Isabelle_System
wenzelm@34774
   173
    Isabelle.system.install_fonts()
wenzelm@34777
   174
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   175
  }
wenzelm@34618
   176
wenzelm@34618
   177
  override def stop()
wenzelm@34618
   178
  {
wenzelm@34777
   179
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   180
    Isabelle.session = null
wenzelm@34441
   181
    Isabelle.system = null
wenzelm@34440
   182
    Isabelle.plugin = null
wenzelm@34318
   183
  }
wenzelm@34318
   184
}