src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Tue, 08 Dec 2009 20:13:07 +0100
changeset 34764 581e919c8730
parent 34762 0974378d235a
child 34767 8dd19c5f8c56
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     1
/*
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     2
 * Main Isabelle/jEdit plugin setup
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     3
 *
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     4
 * @author Johannes Hölzl, TU Munich
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     5
 * @author Fabian Immler, TU Munich
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
     6
 * @author Makarius
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     7
 */
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     8
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     9
package isabelle.jedit
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    10
34429
wenzelm
parents: 34422
diff changeset
    11
wenzelm
parents: 34422
diff changeset
    12
import java.io.{FileInputStream, IOException}
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    13
import java.awt.Font
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    14
import javax.swing.JScrollPane
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    15
34497
184fda8cce04 more explicit indication of mutable collections;
wenzelm
parents: 34487
diff changeset
    16
import scala.collection.mutable
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    17
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
    18
import isabelle.proofdocument.{Command, Proof_Document, Prover}
34615
5e61055bf35b renamed IsabelleProcess to Isabelle_Process;
wenzelm
parents: 34612
diff changeset
    19
import isabelle.Isabelle_System
34429
wenzelm
parents: 34422
diff changeset
    20
wenzelm
parents: 34422
diff changeset
    21
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, EditPane, ServiceManager, View}
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    22
import org.gjt.sp.jedit.buffer.JEditBuffer
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    23
import org.gjt.sp.jedit.textarea.JEditTextArea
34429
wenzelm
parents: 34422
diff changeset
    24
import org.gjt.sp.jedit.msg.{EditPaneUpdate, PropertiesChanged}
wenzelm
parents: 34422
diff changeset
    25
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    26
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    27
object Isabelle
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    28
{
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    29
  /* name */
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    30
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    31
  val NAME = "Isabelle"
34623
a356a8ee6f00 renamed UTF-8-isabelle to UTF-8-Isabelle;
wenzelm
parents: 34618
diff changeset
    32
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    33
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    34
  /* properties */
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    35
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    36
  val OPTION_PREFIX = "options.isabelle."
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    37
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    38
  object Property
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    39
  {
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    40
    def apply(name: String): String = jEdit.getProperty(OPTION_PREFIX + name)
34468
9d4b4f290676 maintain Isabelle properties via object Isabelle.Property with apply/update methods;
wenzelm
parents: 34463
diff changeset
    41
    def update(name: String, value: String) = jEdit.setProperty(OPTION_PREFIX + name, value)
9d4b4f290676 maintain Isabelle properties via object Isabelle.Property with apply/update methods;
wenzelm
parents: 34463
diff changeset
    42
  }
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    43
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    44
  object Boolean_Property
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    45
  {
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    46
    def apply(name: String): Boolean = jEdit.getBooleanProperty(OPTION_PREFIX + name)
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    47
    def update(name: String, value: Boolean) = jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    48
  }
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    49
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    50
  object Int_Property
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    51
  {
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    52
    def apply(name: String): Int = jEdit.getIntegerProperty(OPTION_PREFIX + name)
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    53
    def update(name: String, value: Int) = jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    54
  }
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    55
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    56
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    57
  /* Isabelle system instance */
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    58
34615
5e61055bf35b renamed IsabelleProcess to Isabelle_Process;
wenzelm
parents: 34612
diff changeset
    59
  var system: Isabelle_System = null
34487
b88ee385308d use Symbol.Interpretation from IsabelleSystem instance;
wenzelm
parents: 34475
diff changeset
    60
  def symbols = system.symbols
34612
5a03dc7a19e1 fall back on Isabelle system completion (symbols only);
wenzelm
parents: 34503
diff changeset
    61
  lazy val completion = new Completion + symbols
34440
561a6d19bd95 renamed object Plugin to Isabelle;
wenzelm
parents: 34433
diff changeset
    62
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    63
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    64
  /* settings */
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    65
34696
wenzelm
parents: 34692
diff changeset
    66
  def default_logic(): String =
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    67
  {
34502
7d1d13750890 added Isabelle.default_logic;
wenzelm
parents: 34497
diff changeset
    68
    val logic = Isabelle.Property("logic")
34703
ff037c17332a minor tuning;
wenzelm
parents: 34696
diff changeset
    69
    if (logic != null) logic
ff037c17332a minor tuning;
wenzelm
parents: 34696
diff changeset
    70
    else system.getenv_strict("ISABELLE_LOGIC")
34502
7d1d13750890 added Isabelle.default_logic;
wenzelm
parents: 34497
diff changeset
    71
  }
7d1d13750890 added Isabelle.default_logic;
wenzelm
parents: 34497
diff changeset
    72
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    73
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    74
  /* plugin instance */
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    75
34440
561a6d19bd95 renamed object Plugin to Isabelle;
wenzelm
parents: 34433
diff changeset
    76
  var plugin: Plugin = null
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    77
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    78
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    79
  /* running provers */
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    80
34475
f963335dbc6b implemented IsabelleSideKickParser.complete
immler@in.tum.de
parents: 34468
diff changeset
    81
  def prover_setup(buffer: JEditBuffer) = plugin.prover_setup(buffer)
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    82
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    83
34429
wenzelm
parents: 34422
diff changeset
    84
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    85
class Plugin extends EBPlugin
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    86
{
34685
93f4978fe2a8 global event buses for changes concerning commands and document edits
immler@in.tum.de
parents: 34671
diff changeset
    87
  /* event buses */
93f4978fe2a8 global event buses for changes concerning commands and document edits
immler@in.tum.de
parents: 34671
diff changeset
    88
34720
ac61bdd7f598 modernized Event_Bus -- based on actors;
wenzelm
parents: 34703
diff changeset
    89
  val state_update = new Event_Bus[Command]
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    90
34703
ff037c17332a minor tuning;
wenzelm
parents: 34696
diff changeset
    91
34669
73727c7eec64 state_update global in Plugin
immler@in.tum.de
parents: 34627
diff changeset
    92
  /* selected state */
73727c7eec64 state_update global in Plugin
immler@in.tum.de
parents: 34627
diff changeset
    93
73727c7eec64 state_update global in Plugin
immler@in.tum.de
parents: 34627
diff changeset
    94
  private var _selected_state: Command = null
73727c7eec64 state_update global in Plugin
immler@in.tum.de
parents: 34627
diff changeset
    95
  def selected_state = _selected_state
73727c7eec64 state_update global in Plugin
immler@in.tum.de
parents: 34627
diff changeset
    96
  def selected_state_=(state: Command) { _selected_state = state; state_update.event(state) }
73727c7eec64 state_update global in Plugin
immler@in.tum.de
parents: 34627
diff changeset
    97
73727c7eec64 state_update global in Plugin
immler@in.tum.de
parents: 34627
diff changeset
    98
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
    99
  /* mapping buffer <-> prover */
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
   100
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   101
  private val mapping = new mutable.HashMap[JEditBuffer, Prover_Setup]
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   102
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   103
  private def install(view: View)
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   104
  {
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   105
    val buffer = view.getBuffer
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   106
    val prover_setup = new Prover_Setup(buffer)
34463
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
   107
    mapping.update(buffer, prover_setup)
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
   108
    prover_setup.activate(view)
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   109
  }
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   110
34463
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
   111
  private def uninstall(view: View) =
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
   112
    mapping.removeKey(view.getBuffer).get.deactivate
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
   113
34764
wenzelm
parents: 34762
diff changeset
   114
  def switch_active(view: View) =
34463
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
   115
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
   116
    else install(view)
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   117
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   118
  def prover_setup(buffer: JEditBuffer): Option[Prover_Setup] = mapping.get(buffer)
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   119
  def is_active (buffer: JEditBuffer) = mapping.isDefinedAt(buffer)
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   120
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   121
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   122
  /* main plugin plumbing */
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
   123
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   124
  override def handleMessage(msg: EBMessage)
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   125
  {
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   126
    msg match {
34671
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   127
      case epu: EditPaneUpdate =>
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   128
        val buffer = epu.getEditPane.getBuffer
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   129
        epu.getWhat match {
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   130
          case EditPaneUpdate.BUFFER_CHANGED =>
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   131
            (mapping get buffer) map (_.theory_view.activate)
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   132
          case EditPaneUpdate.BUFFER_CHANGING =>
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   133
            if (buffer != null)
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   134
              (mapping get buffer) map (_.theory_view.deactivate)
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   135
          case _ =>
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
   136
        }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   137
      case _ =>
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   138
    }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   139
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   140
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   141
  override def start()
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   142
  {
34751
6ed1b3701459 simplified treatment of Isabelle fonts, via Isabelle_System.register_fonts (requires Java 1.6);
wenzelm
parents: 34732
diff changeset
   143
    Isabelle.plugin = this
34615
5e61055bf35b renamed IsabelleProcess to Isabelle_Process;
wenzelm
parents: 34612
diff changeset
   144
    Isabelle.system = new Isabelle_System
34762
0974378d235a simplified register_fonts;
wenzelm
parents: 34760
diff changeset
   145
    Isabelle.system.register_fonts()
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   146
  }
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   147
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   148
  override def stop()
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   149
  {
34429
wenzelm
parents: 34422
diff changeset
   150
    // TODO: proper cleanup
34441
ff3b7ae2b12a replaced static IsabelleSystem by Isabelle.system;
wenzelm
parents: 34440
diff changeset
   151
    Isabelle.system = null
34440
561a6d19bd95 renamed object Plugin to Isabelle;
wenzelm
parents: 34433
diff changeset
   152
    Isabelle.plugin = null
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   153
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   154
}