author | wenzelm |
Sat, 09 Jan 2010 18:25:48 +0100 | |
changeset 34846 | ca76b3978540 |
parent 34845 | 6d64de27efa5 |
child 34849 | 96bcb91b45ce |
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 |
||
34846
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
15 |
import java.io.{File, 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 |
{ |
|
23 |
private var interpreters = Map[Console, Interpreter]() |
|
24 |
||
25 |
private var global_console: Console = null |
|
26 |
private var global_out: Output = null |
|
27 |
private var global_err: Output = null |
|
28 |
||
34844 | 29 |
private def with_console[A](console: Console, out: Output, err: Output)(e: => A): A = |
30 |
{ |
|
31 |
global_console = console |
|
32 |
global_out = out |
|
33 |
global_err = if (err == null) out else err |
|
34 |
val res = Exn.capture { e } |
|
35 |
global_console = null |
|
36 |
global_out = null |
|
37 |
global_err = null |
|
38 |
Exn.release(res) |
|
39 |
} |
|
40 |
||
34841 | 41 |
private def report_error(str: String) |
42 |
{ |
|
43 |
if (global_console == null || global_err == null) System.err.println(str) |
|
44 |
else global_err.print(global_console.getErrorColor, str) |
|
45 |
} |
|
46 |
||
34846
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
47 |
private def construct_classpath(): String = |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
48 |
{ |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
49 |
def find_jars(start: String): List[String] = |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
50 |
if (start != null) |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
51 |
Standard_System.find_files(new File(start), |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
52 |
entry => entry.isFile && entry.getName.endsWith(".jar")).map(_.getAbsolutePath) |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
53 |
else Nil |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
54 |
val path = |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
55 |
(jEdit.getJEditHome + File.separator + "jedit.jar") :: |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
56 |
(find_jars(jEdit.getSettingsDirectory) ::: find_jars(jEdit.getJEditHome)) |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
57 |
path.mkString(File.pathSeparator) |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
58 |
} |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
59 |
|
34841 | 60 |
private class Console_Writer extends Writer |
61 |
{ |
|
62 |
def close {} |
|
63 |
def flush {} |
|
64 |
||
65 |
def write(cbuf: Array[Char], off: Int, len: Int) |
|
66 |
{ |
|
67 |
if (len > 0) write(new String(cbuf.subArray(off, off + len))) |
|
68 |
} |
|
69 |
||
70 |
override def write(str: String) |
|
71 |
{ |
|
72 |
if (global_out == null) System.out.println(str) |
|
73 |
else global_out.print(null, str) |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
override def openConsole(console: Console) |
|
78 |
{ |
|
79 |
val settings = new GenericRunnerSettings(report_error) |
|
34846
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
80 |
settings.classpath.value = construct_classpath() |
34841 | 81 |
val printer = new PrintWriter(new Console_Writer, true) |
34845 | 82 |
val interp = new Interpreter(settings, printer) |
83 |
{ |
|
84 |
override def parentClassLoader = new JARClassLoader |
|
85 |
} |
|
86 |
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
|
87 |
interp.bind("view", "org.gjt.sp.jedit.View", console.getView) |
34845 | 88 |
interpreters += (console -> interp) |
34841 | 89 |
} |
90 |
||
91 |
override def closeConsole(console: Console) |
|
92 |
{ |
|
93 |
interpreters -= console |
|
94 |
} |
|
95 |
||
96 |
override def printPrompt(console: Console, out: Output) |
|
97 |
{ |
|
34844 | 98 |
out.writeAttrs(ConsolePane.colorAttributes(console.getInfoColor), "scala>") |
34845 | 99 |
out.writeAttrs(ConsolePane.colorAttributes(console.getPlainColor), " ") |
34841 | 100 |
} |
101 |
||
102 |
override def execute(console: Console, input: String, out: Output, err: Output, command: String) |
|
103 |
{ |
|
34846
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
104 |
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
|
105 |
with_console(console, out, err) { interp.interpret(command) } |
34844 | 106 |
if (err != null) err.commandDone() |
107 |
out.commandDone() |
|
34841 | 108 |
} |
109 |
} |