author | wenzelm |
Mon, 30 Jul 2012 20:43:07 +0200 | |
changeset 48613 | 232652ac346e |
parent 48412 | dbd75cbb84ba |
child 50203 | 00d8ad713e32 |
permissions | -rw-r--r-- |
43282
5d294220ca43
moved sources -- eliminated Netbeans artifact of jedit package directory;
wenzelm
parents:
37175
diff
changeset
|
1 |
/* Title: Tools/jEdit/src/scala_console.scala |
36760 | 2 |
Author: Makarius |
3 |
||
4 |
Scala instance of Console plugin. |
|
5 |
*/ |
|
34841 | 6 |
|
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
36015 | 10 |
import isabelle._ |
11 |
||
34844 | 12 |
import console.{Console, ConsolePane, Shell, Output} |
34841 | 13 |
|
34845 | 14 |
import org.gjt.sp.jedit.{jEdit, JARClassLoader} |
34841 | 15 |
import org.gjt.sp.jedit.MiscUtilities |
16 |
||
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43282
diff
changeset
|
17 |
import java.lang.System |
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
18 |
import java.io.{File => JFile, FileFilter, OutputStream, Writer, PrintWriter} |
34841 | 19 |
|
47992
7700f0e9618c
avoid scala.tools.nsc.Interpreter -- deprecated in scala-2.9.0;
wenzelm
parents:
45580
diff
changeset
|
20 |
import scala.tools.nsc.{GenericRunnerSettings, NewLinePrintWriter, ConsoleWriter} |
7700f0e9618c
avoid scala.tools.nsc.Interpreter -- deprecated in scala-2.9.0;
wenzelm
parents:
45580
diff
changeset
|
21 |
import scala.tools.nsc.interpreter.IMain |
34846
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
22 |
import scala.collection.mutable |
34841 | 23 |
|
24 |
||
25 |
class Scala_Console extends Shell("Scala") |
|
26 |
{ |
|
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
27 |
/* reconstructed jEdit/plugin classpath */ |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
28 |
|
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
29 |
private def reconstruct_classpath(): String = |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
30 |
{ |
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
31 |
def find_files(start: JFile, ok: JFile => Boolean = _ => true): List[JFile] = |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
32 |
{ |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
33 |
val files = new mutable.ListBuffer[JFile] |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
34 |
val filter = new FileFilter { def accept(entry: JFile) = entry.isDirectory || ok(entry) } |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
35 |
def find_entry(entry: JFile) |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
36 |
{ |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
37 |
if (ok(entry)) files += entry |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
38 |
if (entry.isDirectory) entry.listFiles(filter).foreach(find_entry) |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
39 |
} |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
40 |
find_entry(start) |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
41 |
files.toList |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
42 |
} |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
43 |
|
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
44 |
def find_jars(start: String): List[String] = |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
45 |
if (start != null) |
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48412
diff
changeset
|
46 |
find_files(new JFile(start), |
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
47 |
entry => entry.isFile && entry.getName.endsWith(".jar")).map(_.getAbsolutePath) |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
48 |
else Nil |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
49 |
val path = find_jars(jEdit.getSettingsDirectory) ::: find_jars(jEdit.getJEditHome) |
48409 | 50 |
path.mkString(JFile.pathSeparator) |
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
51 |
} |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
52 |
|
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
53 |
|
34849 | 54 |
/* global state -- owned by Swing thread */ |
55 |
||
47992
7700f0e9618c
avoid scala.tools.nsc.Interpreter -- deprecated in scala-2.9.0;
wenzelm
parents:
45580
diff
changeset
|
56 |
private var interpreters = Map[Console, IMain]() |
34841 | 57 |
|
58 |
private var global_console: Console = null |
|
59 |
private var global_out: Output = null |
|
60 |
private var global_err: Output = null |
|
61 |
||
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
62 |
private val console_stream = new OutputStream |
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 |
val buf = new StringBuilder |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
65 |
override def flush() |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
66 |
{ |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
67 |
val str = Standard_System.decode_permissive_utf8(buf.toString) |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
68 |
buf.clear |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
69 |
if (global_out == null) System.out.print(str) |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
70 |
else Swing_Thread.now { global_out.writeAttrs(null, str) } |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
71 |
} |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
72 |
override def close() { flush () } |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
73 |
def write(byte: Int) { buf.append(byte.toChar) } |
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 |
|
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
76 |
private val console_writer = new Writer |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
77 |
{ |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
78 |
def flush() {} |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
79 |
def close() {} |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
80 |
|
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
81 |
def write(cbuf: Array[Char], off: Int, len: Int) |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
82 |
{ |
36015 | 83 |
if (len > 0) write(new String(cbuf.slice(off, off + len))) |
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
84 |
} |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
85 |
|
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
86 |
override def write(str: String) |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
87 |
{ |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
88 |
if (global_out == null) System.out.println(str) |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
89 |
else global_out.print(null, str) |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
90 |
} |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
91 |
} |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
92 |
|
34844 | 93 |
private def with_console[A](console: Console, out: Output, err: Output)(e: => A): A = |
94 |
{ |
|
95 |
global_console = console |
|
96 |
global_out = out |
|
97 |
global_err = if (err == null) out else err |
|
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
98 |
val res = Exn.capture { scala.Console.withOut(console_stream)(e) } |
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
99 |
console_stream.flush |
34844 | 100 |
global_console = null |
101 |
global_out = null |
|
102 |
global_err = null |
|
103 |
Exn.release(res) |
|
104 |
} |
|
105 |
||
34841 | 106 |
private def report_error(str: String) |
107 |
{ |
|
108 |
if (global_console == null || global_err == null) System.err.println(str) |
|
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
109 |
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
|
110 |
} |
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
111 |
|
34841 | 112 |
|
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
113 |
/* jEdit console methods */ |
34841 | 114 |
|
115 |
override def openConsole(console: Console) |
|
116 |
{ |
|
117 |
val settings = new GenericRunnerSettings(report_error) |
|
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
118 |
settings.classpath.value = reconstruct_classpath() |
34849 | 119 |
|
47992
7700f0e9618c
avoid scala.tools.nsc.Interpreter -- deprecated in scala-2.9.0;
wenzelm
parents:
45580
diff
changeset
|
120 |
val interp = new IMain(settings, new PrintWriter(console_writer, true)) |
34845 | 121 |
{ |
122 |
override def parentClassLoader = new JARClassLoader |
|
123 |
} |
|
124 |
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
|
125 |
interp.bind("view", "org.gjt.sp.jedit.View", console.getView) |
34852 | 126 |
interp.bind("console", "console.Console", console) |
34851
304a86164dd2
provide global "Isabelle" within interpreter loop -- using import instead of val avoids pontential conflicts with later import isabelle.jedit._;
wenzelm
parents:
34850
diff
changeset
|
127 |
interp.interpret("import isabelle.jedit.Isabelle") |
34850
fdd560e80264
redirect scala.Console output during interpretation;
wenzelm
parents:
34849
diff
changeset
|
128 |
|
34845 | 129 |
interpreters += (console -> interp) |
34841 | 130 |
} |
131 |
||
132 |
override def closeConsole(console: Console) |
|
133 |
{ |
|
134 |
interpreters -= console |
|
135 |
} |
|
136 |
||
34849 | 137 |
override def printInfoMessage(out: Output) |
138 |
{ |
|
139 |
out.print(null, |
|
140 |
"This shell evaluates Isabelle/Scala expressions.\n\n" + |
|
141 |
"The following special toplevel bindings are provided:\n" + |
|
34851
304a86164dd2
provide global "Isabelle" within interpreter loop -- using import instead of val avoids pontential conflicts with later import isabelle.jedit._;
wenzelm
parents:
34850
diff
changeset
|
142 |
" view -- current jEdit/Swing view (e.g. view.getBuffer, view.getTextArea)\n" + |
45580 | 143 |
" console -- jEdit Console plugin\n" + |
144 |
" Isabelle -- Isabelle plugin (e.g. Isabelle.session, Isabelle.document_model)\n") |
|
34849 | 145 |
} |
146 |
||
34841 | 147 |
override def printPrompt(console: Console, out: Output) |
37175 | 148 |
{ |
34844 | 149 |
out.writeAttrs(ConsolePane.colorAttributes(console.getInfoColor), "scala>") |
37175 | 150 |
out.writeAttrs(ConsolePane.colorAttributes(console.getPlainColor), " ") |
151 |
} |
|
34841 | 152 |
|
153 |
override def execute(console: Console, input: String, out: Output, err: Output, command: String) |
|
154 |
{ |
|
34846
ca76b3978540
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
wenzelm
parents:
34845
diff
changeset
|
155 |
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
|
156 |
with_console(console, out, err) { interp.interpret(command) } |
34844 | 157 |
if (err != null) err.commandDone() |
37175 | 158 |
out.commandDone() |
34841 | 159 |
} |
34849 | 160 |
|
161 |
override def stop(console: Console) |
|
162 |
{ |
|
163 |
closeConsole(console) |
|
164 |
console.clear |
|
165 |
openConsole(console) |
|
166 |
val out = console.getOutput |
|
167 |
out.commandDone |
|
168 |
printPrompt(console, out) |
|
169 |
} |
|
34841 | 170 |
} |