src/Tools/jEdit/src/jedit/Plugin.scala
author wenzelm
Tue, 23 Jun 2009 20:49:56 +0200
changeset 34612 5a03dc7a19e1
parent 34503 7d0726f19d04
child 34615 5e61055bf35b
permissions -rw-r--r--
fall back on Isabelle system completion (symbols only); 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
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     6
 */
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     7
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     8
package isabelle.jedit
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     9
34429
wenzelm
parents: 34422
diff changeset
    10
wenzelm
parents: 34422
diff changeset
    11
import java.io.{FileInputStream, IOException}
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    12
import java.awt.Font
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    13
import javax.swing.JScrollPane
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    14
34497
184fda8cce04 more explicit indication of mutable collections;
wenzelm
parents: 34487
diff changeset
    15
import scala.collection.mutable
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    16
34429
wenzelm
parents: 34422
diff changeset
    17
import isabelle.prover.{Prover, Command}
34487
b88ee385308d use Symbol.Interpretation from IsabelleSystem instance;
wenzelm
parents: 34475
diff changeset
    18
import isabelle.IsabelleSystem
34429
wenzelm
parents: 34422
diff changeset
    19
wenzelm
parents: 34422
diff changeset
    20
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
    21
import org.gjt.sp.jedit.buffer.JEditBuffer
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    22
import org.gjt.sp.jedit.textarea.JEditTextArea
34429
wenzelm
parents: 34422
diff changeset
    23
import org.gjt.sp.jedit.msg.{EditPaneUpdate, PropertiesChanged}
wenzelm
parents: 34422
diff changeset
    24
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    25
34440
561a6d19bd95 renamed object Plugin to Isabelle;
wenzelm
parents: 34433
diff changeset
    26
object Isabelle {
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    27
  // name
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    28
  val NAME = "Isabelle"
34337
5d5b69f2956b renamed VFS protocol prefix from "isa:" to "isabelle:";
wenzelm
parents: 34318
diff changeset
    29
  val VFS_PREFIX = "isabelle:"
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    30
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    31
  // properties
34468
9d4b4f290676 maintain Isabelle properties via object Isabelle.Property with apply/update methods;
wenzelm
parents: 34463
diff changeset
    32
  object Property {
9d4b4f290676 maintain Isabelle properties via object Isabelle.Property with apply/update methods;
wenzelm
parents: 34463
diff changeset
    33
    private val OPTION_PREFIX = "options.isabelle."
9d4b4f290676 maintain Isabelle properties via object Isabelle.Property with apply/update methods;
wenzelm
parents: 34463
diff changeset
    34
    def apply(name: String) = jEdit.getProperty(OPTION_PREFIX + name)
9d4b4f290676 maintain Isabelle properties via object Isabelle.Property with apply/update methods;
wenzelm
parents: 34463
diff changeset
    35
    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
    36
  }
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    37
34487
b88ee385308d use Symbol.Interpretation from IsabelleSystem instance;
wenzelm
parents: 34475
diff changeset
    38
  // Isabelle system instance
34441
ff3b7ae2b12a replaced static IsabelleSystem by Isabelle.system;
wenzelm
parents: 34440
diff changeset
    39
  var system: IsabelleSystem = null
34487
b88ee385308d use Symbol.Interpretation from IsabelleSystem instance;
wenzelm
parents: 34475
diff changeset
    40
  def symbols = system.symbols
34612
5a03dc7a19e1 fall back on Isabelle system completion (symbols only);
wenzelm
parents: 34503
diff changeset
    41
  lazy val completion = new Completion + symbols
34440
561a6d19bd95 renamed object Plugin to Isabelle;
wenzelm
parents: 34433
diff changeset
    42
34502
7d1d13750890 added Isabelle.default_logic;
wenzelm
parents: 34497
diff changeset
    43
  // settings
7d1d13750890 added Isabelle.default_logic;
wenzelm
parents: 34497
diff changeset
    44
  def default_logic = {
7d1d13750890 added Isabelle.default_logic;
wenzelm
parents: 34497
diff changeset
    45
    val logic = Isabelle.Property("logic")
7d1d13750890 added Isabelle.default_logic;
wenzelm
parents: 34497
diff changeset
    46
    if (logic != null) logic else Isabelle.system.getenv_strict("ISABELLE_LOGIC")
7d1d13750890 added Isabelle.default_logic;
wenzelm
parents: 34497
diff changeset
    47
  }
7d1d13750890 added Isabelle.default_logic;
wenzelm
parents: 34497
diff changeset
    48
34440
561a6d19bd95 renamed object Plugin to Isabelle;
wenzelm
parents: 34433
diff changeset
    49
  // plugin instance
561a6d19bd95 renamed object Plugin to Isabelle;
wenzelm
parents: 34433
diff changeset
    50
  var plugin: Plugin = null
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    51
34487
b88ee385308d use Symbol.Interpretation from IsabelleSystem instance;
wenzelm
parents: 34475
diff changeset
    52
  // running provers
34475
f963335dbc6b implemented IsabelleSideKickParser.complete
immler@in.tum.de
parents: 34468
diff changeset
    53
  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
    54
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    55
34429
wenzelm
parents: 34422
diff changeset
    56
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    57
class Plugin extends EBPlugin {
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    58
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    59
  // Isabelle font
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    60
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    61
  var font: Font = null
34456
14367c0715e8 replaced EventSource by EventBus;
wenzelm
parents: 34449
diff changeset
    62
  val font_changed = new EventBus[Font]
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    63
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    64
  def set_font(path: String, size: Float) {
34456
14367c0715e8 replaced EventSource by EventBus;
wenzelm
parents: 34449
diff changeset
    65
    font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path)).
14367c0715e8 replaced EventSource by EventBus;
wenzelm
parents: 34449
diff changeset
    66
      deriveFont(Font.PLAIN, size)
14367c0715e8 replaced EventSource by EventBus;
wenzelm
parents: 34449
diff changeset
    67
    font_changed.event(font)
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    68
  }
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    69
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    70
34449
57fce8528a22 added central id generator;
wenzelm
parents: 34443
diff changeset
    71
  /* unique ids */  // FIXME specific to "session" (!??)
57fce8528a22 added central id generator;
wenzelm
parents: 34443
diff changeset
    72
57fce8528a22 added central id generator;
wenzelm
parents: 34443
diff changeset
    73
  private var id_count: BigInt = 0
57fce8528a22 added central id generator;
wenzelm
parents: 34443
diff changeset
    74
  def id() : String = synchronized { id_count += 1; "editor:" + id_count }
57fce8528a22 added central id generator;
wenzelm
parents: 34443
diff changeset
    75
57fce8528a22 added central id generator;
wenzelm
parents: 34443
diff changeset
    76
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    77
  // mapping buffer <-> prover
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    78
34497
184fda8cce04 more explicit indication of mutable collections;
wenzelm
parents: 34487
diff changeset
    79
  private val mapping = new mutable.HashMap[JEditBuffer, ProverSetup]
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    80
34463
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    81
  private def install(view: View) {
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    82
    val buffer = view.getBuffer
34463
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    83
    val prover_setup = new ProverSetup(buffer)
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    84
    mapping.update(buffer, prover_setup)
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    85
    prover_setup.activate(view)
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    86
  }
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    87
34463
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    88
  private def uninstall(view: View) =
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    89
    mapping.removeKey(view.getBuffer).get.deactivate
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    90
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    91
  def switch_active (view : View) =
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    92
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    93
    else install(view)
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    94
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
    95
  def prover_setup (buffer : JEditBuffer) : Option[ProverSetup] = mapping.get(buffer)
34462
immler@in.tum.de
parents: 34460 34456
diff changeset
    96
  def is_active (buffer : JEditBuffer) = mapping.isDefinedAt(buffer)
34463
b510b7d88de2 changed install/uninstall prover on view to private
immler@in.tum.de
parents: 34462
diff changeset
    97
  
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    98
  
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
    99
  // main plugin plumbing
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
   100
34429
wenzelm
parents: 34422
diff changeset
   101
  override def handleMessage(msg: EBMessage) = msg match {
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
   102
    case epu: EditPaneUpdate => epu.getWhat match {
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   103
      case EditPaneUpdate.BUFFER_CHANGED =>
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   104
        mapping get epu.getEditPane.getBuffer match {
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   105
          //only activate 'isabelle'-buffers!
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   106
          case None =>
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   107
          case Some(prover_setup) => 
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   108
            prover_setup.theory_view.activate
34422
d5a41da986c3 more conventional action names;
wenzelm
parents: 34407
diff changeset
   109
            val dockable = epu.getEditPane.getView.getDockableWindowManager.getDockable("isabelle-output")
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34502
diff changeset
   110
            if (dockable != null) {
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   111
              val output_dockable = dockable.asInstanceOf[OutputDockable]
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34502
diff changeset
   112
              if (output_dockable.getComponent(0) != prover_setup.output_text_view ) {
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   113
                output_dockable.asInstanceOf[OutputDockable].removeAll
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   114
                output_dockable.asInstanceOf[OutputDockable].add(new JScrollPane(prover_setup.output_text_view))
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   115
                output_dockable.revalidate
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   116
              }
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   117
            }
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   118
        }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   119
      case EditPaneUpdate.BUFFER_CHANGING =>
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   120
        val buffer = epu.getEditPane.getBuffer
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34502
diff changeset
   121
        if (buffer != null) mapping get buffer match {
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   122
          //only deactivate 'isabelle'-buffers!
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   123
          case None =>
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   124
          case Some(prover_setup) => prover_setup.theory_view.deactivate
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   125
        }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   126
      case _ =>
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   127
    }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   128
    case _ =>
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   129
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   130
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   131
  override def start() {
34441
ff3b7ae2b12a replaced static IsabelleSystem by Isabelle.system;
wenzelm
parents: 34440
diff changeset
   132
    Isabelle.system = new IsabelleSystem
34440
561a6d19bd95 renamed object Plugin to Isabelle;
wenzelm
parents: 34433
diff changeset
   133
    Isabelle.plugin = this
34406
f81cd75ae331 restructured: independent provers in different buffers
immler@in.tum.de
parents: 34337
diff changeset
   134
    
34468
9d4b4f290676 maintain Isabelle properties via object Isabelle.Property with apply/update methods;
wenzelm
parents: 34463
diff changeset
   135
    if (Isabelle.Property("font-path") != null && Isabelle.Property("font-size") != null)
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   136
      try {
34468
9d4b4f290676 maintain Isabelle properties via object Isabelle.Property with apply/update methods;
wenzelm
parents: 34463
diff changeset
   137
        set_font(Isabelle.Property("font-path"), Isabelle.Property("font-size").toFloat)
34318
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
      catch {
34429
wenzelm
parents: 34422
diff changeset
   140
        case e: NumberFormatException =>
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   141
      }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   142
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   143
  
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   144
  override def stop() {
34429
wenzelm
parents: 34422
diff changeset
   145
    // TODO: proper cleanup
34441
ff3b7ae2b12a replaced static IsabelleSystem by Isabelle.system;
wenzelm
parents: 34440
diff changeset
   146
    Isabelle.system = null
34440
561a6d19bd95 renamed object Plugin to Isabelle;
wenzelm
parents: 34433
diff changeset
   147
    Isabelle.plugin = null
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   148
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   149
}