src/Tools/jEdit/src/jedit/scala_console.scala
author wenzelm
Sat May 08 21:08:30 2010 +0200 (2010-05-08)
changeset 36760 b82a698ef6c9
parent 36015 6111de7c916a
child 37175 be764a7adb10
permissions -rw-r--r--
tuned headers;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/scala_console.scala
wenzelm@36760
     2
    Author:     Makarius
wenzelm@36760
     3
wenzelm@36760
     4
Scala instance of Console plugin.
wenzelm@36760
     5
*/
wenzelm@34841
     6
wenzelm@34841
     7
package isabelle.jedit
wenzelm@34841
     8
wenzelm@34841
     9
wenzelm@36015
    10
import isabelle._
wenzelm@36015
    11
wenzelm@34844
    12
import console.{Console, ConsolePane, Shell, Output}
wenzelm@34841
    13
wenzelm@34845
    14
import org.gjt.sp.jedit.{jEdit, JARClassLoader}
wenzelm@34841
    15
import org.gjt.sp.jedit.MiscUtilities
wenzelm@34841
    16
wenzelm@34850
    17
import java.io.{File, OutputStream, Writer, PrintWriter}
wenzelm@34841
    18
wenzelm@34841
    19
import scala.tools.nsc.{Interpreter, GenericRunnerSettings, NewLinePrintWriter, ConsoleWriter}
wenzelm@34846
    20
import scala.collection.mutable
wenzelm@34841
    21
wenzelm@34841
    22
wenzelm@34841
    23
class Scala_Console extends Shell("Scala")
wenzelm@34841
    24
{
wenzelm@34850
    25
  /* reconstructed jEdit/plugin classpath */
wenzelm@34850
    26
wenzelm@34850
    27
  private def reconstruct_classpath(): String =
wenzelm@34850
    28
  {
wenzelm@34850
    29
    def find_jars(start: String): List[String] =
wenzelm@34850
    30
      if (start != null)
wenzelm@34850
    31
        Standard_System.find_files(new File(start),
wenzelm@34850
    32
          entry => entry.isFile && entry.getName.endsWith(".jar")).map(_.getAbsolutePath)
wenzelm@34850
    33
      else Nil
wenzelm@34850
    34
    val path = find_jars(jEdit.getSettingsDirectory) ::: find_jars(jEdit.getJEditHome)
wenzelm@34850
    35
    path.mkString(File.pathSeparator)
wenzelm@34850
    36
  }
wenzelm@34850
    37
wenzelm@34850
    38
wenzelm@34849
    39
  /* global state -- owned by Swing thread */
wenzelm@34849
    40
wenzelm@34841
    41
  private var interpreters = Map[Console, Interpreter]()
wenzelm@34841
    42
wenzelm@34841
    43
  private var global_console: Console = null
wenzelm@34841
    44
  private var global_out: Output = null
wenzelm@34841
    45
  private var global_err: Output = null
wenzelm@34841
    46
wenzelm@34850
    47
  private val console_stream = new OutputStream
wenzelm@34850
    48
  {
wenzelm@34850
    49
    val buf = new StringBuilder
wenzelm@34850
    50
    override def flush()
wenzelm@34850
    51
    {
wenzelm@34850
    52
      val str = Standard_System.decode_permissive_utf8(buf.toString)
wenzelm@34850
    53
      buf.clear
wenzelm@34850
    54
      if (global_out == null) System.out.print(str)
wenzelm@34850
    55
      else Swing_Thread.now { global_out.writeAttrs(null, str) }
wenzelm@34850
    56
    }
wenzelm@34850
    57
    override def close() { flush () }
wenzelm@34850
    58
    def write(byte: Int) { buf.append(byte.toChar) }
wenzelm@34850
    59
  }
wenzelm@34850
    60
wenzelm@34850
    61
  private val console_writer = new Writer
wenzelm@34850
    62
  {
wenzelm@34850
    63
    def flush() {}
wenzelm@34850
    64
    def close() {}
wenzelm@34850
    65
wenzelm@34850
    66
    def write(cbuf: Array[Char], off: Int, len: Int)
wenzelm@34850
    67
    {
wenzelm@36015
    68
      if (len > 0) write(new String(cbuf.slice(off, off + len)))
wenzelm@34850
    69
    }
wenzelm@34850
    70
wenzelm@34850
    71
    override def write(str: String)
wenzelm@34850
    72
    {
wenzelm@34850
    73
      if (global_out == null) System.out.println(str)
wenzelm@34850
    74
      else global_out.print(null, str)
wenzelm@34850
    75
    }
wenzelm@34850
    76
  }
wenzelm@34850
    77
wenzelm@34844
    78
  private def with_console[A](console: Console, out: Output, err: Output)(e: => A): A =
wenzelm@34844
    79
  {
wenzelm@34844
    80
    global_console = console
wenzelm@34844
    81
    global_out = out
wenzelm@34844
    82
    global_err = if (err == null) out else err
wenzelm@34850
    83
    val res = Exn.capture { scala.Console.withOut(console_stream)(e) }
wenzelm@34850
    84
    console_stream.flush
wenzelm@34844
    85
    global_console = null
wenzelm@34844
    86
    global_out = null
wenzelm@34844
    87
    global_err = null
wenzelm@34844
    88
    Exn.release(res)
wenzelm@34844
    89
  }
wenzelm@34844
    90
wenzelm@34841
    91
  private def report_error(str: String)
wenzelm@34841
    92
  {
wenzelm@34841
    93
    if (global_console == null || global_err == null) System.err.println(str)
wenzelm@34850
    94
    else Swing_Thread.now { global_err.print(global_console.getErrorColor, str) }
wenzelm@34846
    95
  }
wenzelm@34846
    96
wenzelm@34841
    97
wenzelm@34850
    98
  /* jEdit console methods */
wenzelm@34841
    99
wenzelm@34841
   100
  override def openConsole(console: Console)
wenzelm@34841
   101
  {
wenzelm@34841
   102
    val settings = new GenericRunnerSettings(report_error)
wenzelm@34850
   103
    settings.classpath.value = reconstruct_classpath()
wenzelm@34849
   104
wenzelm@34850
   105
    val interp = new Interpreter(settings, new PrintWriter(console_writer, true))
wenzelm@34845
   106
    {
wenzelm@34845
   107
      override def parentClassLoader = new JARClassLoader
wenzelm@34845
   108
    }
wenzelm@34845
   109
    interp.setContextClassLoader
wenzelm@34846
   110
    interp.bind("view", "org.gjt.sp.jedit.View", console.getView)
wenzelm@34852
   111
    interp.bind("console", "console.Console", console)
wenzelm@34851
   112
    interp.interpret("import isabelle.jedit.Isabelle")
wenzelm@34850
   113
wenzelm@34845
   114
    interpreters += (console -> interp)
wenzelm@34841
   115
  }
wenzelm@34841
   116
wenzelm@34841
   117
  override def closeConsole(console: Console)
wenzelm@34841
   118
  {
wenzelm@34841
   119
    interpreters -= console
wenzelm@34841
   120
  }
wenzelm@34841
   121
wenzelm@34849
   122
  override def printInfoMessage(out: Output)
wenzelm@34849
   123
  {
wenzelm@34849
   124
    out.print(null,
wenzelm@34849
   125
     "This shell evaluates Isabelle/Scala expressions.\n\n" +
wenzelm@34849
   126
     "The following special toplevel bindings are provided:\n" +
wenzelm@34851
   127
     "  view      -- current jEdit/Swing view (e.g. view.getBuffer, view.getTextArea)\n" +
wenzelm@34852
   128
     "  console   -- jEdit Console plugin instance\n" +
wenzelm@34852
   129
     "  Isabelle  -- Isabelle plugin instance (e.g. Isabelle.system, Isabelle.session)\n")
wenzelm@34849
   130
  }
wenzelm@34849
   131
wenzelm@34841
   132
  override def printPrompt(console: Console, out: Output)
wenzelm@34841
   133
	{
wenzelm@34844
   134
    out.writeAttrs(ConsolePane.colorAttributes(console.getInfoColor), "scala>")
wenzelm@34845
   135
		out.writeAttrs(ConsolePane.colorAttributes(console.getPlainColor), " ")
wenzelm@34841
   136
	}
wenzelm@34841
   137
wenzelm@34841
   138
  override def execute(console: Console, input: String, out: Output, err: Output, command: String)
wenzelm@34841
   139
  {
wenzelm@34846
   140
    val interp = interpreters(console)
wenzelm@34846
   141
    with_console(console, out, err) { interp.interpret(command) }
wenzelm@34844
   142
    if (err != null) err.commandDone()
wenzelm@34844
   143
		out.commandDone()
wenzelm@34841
   144
  }
wenzelm@34849
   145
wenzelm@34849
   146
  override def stop(console: Console)
wenzelm@34849
   147
  {
wenzelm@34849
   148
    closeConsole(console)
wenzelm@34849
   149
    console.clear
wenzelm@34849
   150
    openConsole(console)
wenzelm@34849
   151
    val out = console.getOutput
wenzelm@34849
   152
    out.commandDone
wenzelm@34849
   153
    printPrompt(console, out)
wenzelm@34849
   154
  }
wenzelm@34841
   155
}