src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Thu May 20 23:20:01 2010 +0200 (2010-05-20)
changeset 37019 8f747cee4e27
parent 36814 dc85664dbf6d
child 37068 07936a4efe93
permissions -rw-r--r--
zoom font size;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/plugin.scala
wenzelm@36760
     2
    Author:     Johannes Hölzl, TU Munich
wenzelm@36760
     3
    Author:     Fabian Immler, TU Munich
wenzelm@36760
     4
    Author:     Makarius
wenzelm@36760
     5
wenzelm@36760
     6
Main Isabelle/jEdit plugin setup.
wenzelm@36760
     7
*/
wenzelm@34407
     8
wenzelm@34318
     9
package isabelle.jedit
wenzelm@34318
    10
wenzelm@34429
    11
wenzelm@36015
    12
import isabelle._
wenzelm@36015
    13
wenzelm@34429
    14
import java.io.{FileInputStream, IOException}
wenzelm@34318
    15
import java.awt.Font
wenzelm@34822
    16
import javax.swing.JTextArea
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 system: Isabelle_System = null
wenzelm@34784
    31
  var session: Session = null
wenzelm@34784
    32
wenzelm@34784
    33
wenzelm@34618
    34
  /* name */
wenzelm@34618
    35
wenzelm@34318
    36
  val NAME = "Isabelle"
wenzelm@34623
    37
wenzelm@34618
    38
wenzelm@34618
    39
  /* properties */
wenzelm@34618
    40
wenzelm@34618
    41
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34618
    42
wenzelm@34618
    43
  object Property
wenzelm@34618
    44
  {
wenzelm@36814
    45
    def apply(name: String): String =
wenzelm@36814
    46
      jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@36814
    47
    def apply(name: String, default: String): String =
wenzelm@36814
    48
      jEdit.getProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    49
    def update(name: String, value: String) =
wenzelm@36814
    50
      jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    51
  }
wenzelm@34433
    52
wenzelm@34618
    53
  object Boolean_Property
wenzelm@34618
    54
  {
wenzelm@36814
    55
    def apply(name: String): Boolean =
wenzelm@36814
    56
      jEdit.getBooleanProperty(OPTION_PREFIX + name)
wenzelm@36814
    57
    def apply(name: String, default: Boolean): Boolean =
wenzelm@36814
    58
      jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    59
    def update(name: String, value: Boolean) =
wenzelm@36814
    60
      jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    61
  }
wenzelm@34618
    62
wenzelm@34618
    63
  object Int_Property
wenzelm@34618
    64
  {
wenzelm@36814
    65
    def apply(name: String): Int =
wenzelm@36814
    66
      jEdit.getIntegerProperty(OPTION_PREFIX + name)
wenzelm@36814
    67
    def apply(name: String, default: Int): Int =
wenzelm@36814
    68
      jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
wenzelm@36814
    69
    def update(name: String, value: Int) =
wenzelm@36814
    70
      jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
wenzelm@34618
    71
  }
wenzelm@34618
    72
wenzelm@37019
    73
  def font_size(): Float =
wenzelm@37019
    74
    (jEdit.getIntegerProperty("view.fontsize", 16) *
wenzelm@37019
    75
      Int_Property("relative-font-size", 100)).toFloat / 100
wenzelm@36814
    76
wenzelm@34618
    77
wenzelm@34618
    78
  /* settings */
wenzelm@34618
    79
wenzelm@34782
    80
  def default_logic(): String =
wenzelm@34782
    81
  {
wenzelm@34782
    82
    val logic = system.getenv("JEDIT_LOGIC")
wenzelm@34782
    83
    if (logic != "") logic
wenzelm@34782
    84
    else system.getenv_strict("ISABELLE_LOGIC")
wenzelm@34782
    85
  }
wenzelm@34782
    86
wenzelm@34782
    87
  def isabelle_args(): List[String] =
wenzelm@34618
    88
  {
wenzelm@34780
    89
    val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
wenzelm@34780
    90
    val logic = {
wenzelm@34784
    91
      val logic = Property("logic")
wenzelm@34782
    92
      if (logic != null && logic != "") logic
wenzelm@34782
    93
      else default_logic()
wenzelm@34780
    94
    }
wenzelm@34780
    95
    modes ++ List(logic)
wenzelm@34502
    96
  }
wenzelm@34502
    97
wenzelm@34618
    98
wenzelm@34784
    99
  /* main jEdit components */  // FIXME ownership!?
wenzelm@34784
   100
wenzelm@34784
   101
  def jedit_buffers(): Iterator[Buffer] = Iterator.fromArray(jEdit.getBuffers())
wenzelm@34784
   102
wenzelm@34784
   103
  def jedit_views(): Iterator[View] = Iterator.fromArray(jEdit.getViews())
wenzelm@34784
   104
wenzelm@34784
   105
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@34784
   106
    Iterator.fromArray(view.getEditPanes).map(_.getTextArea)
wenzelm@34784
   107
wenzelm@34784
   108
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@34784
   109
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@34784
   110
wenzelm@34784
   111
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@34784
   112
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@34784
   113
wenzelm@34784
   114
wenzelm@34820
   115
  /* manage prover */
wenzelm@34820
   116
wenzelm@34820
   117
  private def prover_started(view: View): Boolean =
wenzelm@34820
   118
  {
wenzelm@34820
   119
    val timeout = Int_Property("startup-timeout") max 1000
wenzelm@34820
   120
    session.start(timeout, Isabelle.isabelle_args()) match {
wenzelm@34820
   121
      case Some(err) =>
wenzelm@34822
   122
        val text = new JTextArea(err); text.setEditable(false)
wenzelm@34822
   123
        Library.error_dialog(view, null, "Failed to start Isabelle process", text)
wenzelm@34820
   124
        false
wenzelm@34820
   125
      case None => true
wenzelm@34820
   126
    }
wenzelm@34820
   127
  }
wenzelm@34820
   128
wenzelm@34820
   129
wenzelm@34784
   130
  /* activation */
wenzelm@34784
   131
wenzelm@34820
   132
  def activate_buffer(view: View, buffer: Buffer)
wenzelm@34784
   133
  {
wenzelm@34820
   134
    if (prover_started(view)) {
wenzelm@34820
   135
      val model = Document_Model.init(session, buffer)
wenzelm@34820
   136
      for (text_area <- jedit_text_areas(buffer))
wenzelm@34820
   137
        Document_View.init(model, text_area)
wenzelm@34820
   138
    }
wenzelm@34784
   139
  }
wenzelm@34784
   140
wenzelm@34784
   141
  def deactivate_buffer(buffer: Buffer)
wenzelm@34784
   142
  {
wenzelm@34784
   143
    session.stop()  // FIXME not yet
wenzelm@34784
   144
wenzelm@34784
   145
    for (text_area <- jedit_text_areas(buffer))
wenzelm@34784
   146
      Document_View.exit(text_area)
wenzelm@34784
   147
    Document_Model.exit(buffer)
wenzelm@34784
   148
  }
wenzelm@34784
   149
wenzelm@34784
   150
  def switch_active(view: View) =
wenzelm@34784
   151
  {
wenzelm@34784
   152
    val buffer = view.getBuffer
wenzelm@34788
   153
    if (Document_Model(buffer).isDefined) deactivate_buffer(buffer)
wenzelm@34820
   154
    else activate_buffer(view, buffer)
wenzelm@34784
   155
  }
wenzelm@34784
   156
wenzelm@34784
   157
  def is_active(view: View): Boolean =
wenzelm@34788
   158
    Document_Model(view.getBuffer).isDefined
wenzelm@34318
   159
}
wenzelm@34318
   160
wenzelm@34429
   161
wenzelm@34618
   162
class Plugin extends EBPlugin
wenzelm@34618
   163
{
wenzelm@34618
   164
  /* main plugin plumbing */
wenzelm@34433
   165
wenzelm@34767
   166
  override def handleMessage(message: EBMessage)
wenzelm@34618
   167
  {
wenzelm@34767
   168
    message match {
wenzelm@34767
   169
      case msg: EditPaneUpdate =>
wenzelm@34784
   170
        val edit_pane = msg.getEditPane
wenzelm@34784
   171
        val buffer = edit_pane.getBuffer
wenzelm@34784
   172
        val text_area = edit_pane.getTextArea
wenzelm@34784
   173
wenzelm@34787
   174
        def init_view()
wenzelm@34787
   175
        {
wenzelm@34788
   176
          Document_Model(buffer) match {
wenzelm@34787
   177
            case Some(model) => Document_View.init(model, text_area)
wenzelm@34787
   178
            case None =>
wenzelm@34787
   179
          }
wenzelm@34787
   180
        }
wenzelm@34787
   181
        def exit_view()
wenzelm@34787
   182
        {
wenzelm@34788
   183
          if (Document_View(text_area).isDefined)
wenzelm@34787
   184
            Document_View.exit(text_area)
wenzelm@34787
   185
        }
wenzelm@34767
   186
        msg.getWhat match {
wenzelm@34787
   187
          case EditPaneUpdate.BUFFER_CHANGED => exit_view(); init_view()
wenzelm@34787
   188
          case EditPaneUpdate.CREATED => init_view()
wenzelm@34787
   189
          case EditPaneUpdate.DESTROYED => exit_view()
immler@34671
   190
          case _ =>
immler@34671
   191
        }
wenzelm@34784
   192
wenzelm@34777
   193
      case msg: PropertiesChanged =>
wenzelm@34791
   194
        Isabelle.session.global_settings.event(Session.Global_Settings)
wenzelm@34784
   195
wenzelm@34318
   196
      case _ =>
wenzelm@34318
   197
    }
wenzelm@34318
   198
  }
wenzelm@34318
   199
wenzelm@34618
   200
  override def start()
wenzelm@34618
   201
  {
wenzelm@34615
   202
    Isabelle.system = new Isabelle_System
wenzelm@34774
   203
    Isabelle.system.install_fonts()
wenzelm@34777
   204
    Isabelle.session = new Session(Isabelle.system)  // FIXME dialog!?
wenzelm@34318
   205
  }
wenzelm@34618
   206
wenzelm@34618
   207
  override def stop()
wenzelm@34618
   208
  {
wenzelm@34777
   209
    Isabelle.session.stop()  // FIXME dialog!?
wenzelm@34777
   210
    Isabelle.session = null
wenzelm@34441
   211
    Isabelle.system = null
wenzelm@34318
   212
  }
wenzelm@34318
   213
}