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