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