src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Wed Dec 16 21:11:04 2009 +0100 (2009-12-16 ago)
changeset 34791 b97d5b38dea4
parent 34788 3779c54a2d21
child 34808 e462572536e9
permissions -rw-r--r--
explicit object Session.Global_Settings;
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@34788
   125
    if (Document_Model(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@34788
   130
    Document_Model(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@34787
   146
        def init_view()
wenzelm@34787
   147
        {
wenzelm@34788
   148
          Document_Model(buffer) match {
wenzelm@34787
   149
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   150
            case None =>
wenzelm@34787
   151
          }
wenzelm@34787
   152
        }
wenzelm@34787
   153
        def exit_view()
wenzelm@34787
   154
        {
wenzelm@34788
   155
          if (Document_View(text_area).isDefined)
wenzelm@34787
   156
            Document_View.exit(text_area)
wenzelm@34787
   157
        }
wenzelm@34767
   158
        msg.getWhat match {
wenzelm@34787
   159
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   160
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   161
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   162
          case _ =>
immler@34671
   163
        }
wenzelm@34784
   164
wenzelm@34777
   165
      case msg: PropertiesChanged =>
wenzelm@34791
   166
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   167
wenzelm@34318
   168
      case _ =>
wenzelm@34318
   169
    }
wenzelm@34318
   170
  }
wenzelm@34318
   171
wenzelm@34618
   172
  override def start()
wenzelm@34618
   173
  {
wenzelm@34751
   174
    Isabelle.plugin = this
wenzelm@34615
   175
    Isabelle.system = new Isabelle_System
wenzelm@34774
   176
    Isabelle.system.install_fonts()
wenzelm@34777
   177
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   178
  }
wenzelm@34618
   179
wenzelm@34618
   180
  override def stop()
wenzelm@34618
   181
  {
wenzelm@34777
   182
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   183
    Isabelle.session = null
wenzelm@34441
   184
    Isabelle.system = null
wenzelm@34440
   185
    Isabelle.plugin = null
wenzelm@34318
   186
  }
wenzelm@34318
   187
}