diff -r d70bb583451d -r c21ffeaaba4a .classpath --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.classpath Sun May 16 22:13:08 2010 +0200 @@ -0,0 +1,9 @@ + + + + + + + + + diff -r d70bb583451d -r c21ffeaaba4a .project --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.project Sun May 16 22:13:08 2010 +0200 @@ -0,0 +1,28 @@ + + + org.sciencecommons.protege.lisp + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff -r d70bb583451d -r c21ffeaaba4a .settings/org.eclipse.jdt.core.prefs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.settings/org.eclipse.jdt.core.prefs Sun May 16 22:13:08 2010 +0200 @@ -0,0 +1,3 @@ +#Thu Apr 22 07:05:40 CEST 2010 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled diff -r d70bb583451d -r c21ffeaaba4a META-INF/MANIFEST.MF --- a/META-INF/MANIFEST.MF Tue Apr 20 18:17:39 2010 +0200 +++ b/META-INF/MANIFEST.MF Sun May 16 22:13:08 2010 +0200 @@ -1,50 +1,19 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 -Bundle-Name: Protege Lisp Based Plugin +Bundle-Name: Protege Lisp Based Plugin Bundle-SymbolicName: org.sciencecommons.protege.lisp;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-Vendor: Alan Ruttenberg Bundle-Activator: org.sciencecommons.protege.lisp.Activator -Export-Package: org.armedbear.lisp, - org.armedbear.lisp.java, - org.armedbear.lisp.java.awt, - org.armedbear.lisp.java.swing, - org.armedbear.lisp.util -Import-Package: org.osgi.framework, - org.apache.log4j, - javax.swing, - javax.swing.border, - javax.swing.colorchooser, +Export-Package: org.armedbear.lisp +Import-Package: javax.swing, javax.swing.event, - javax.swing.filechooser, - javax.swing.plaf, - javax.swing.plaf.basic, - javax.swing.plaf.metal, - javax.swing.plaf.multi, - javax.swing.plaf.synth, - javax.swing.table, javax.swing.text, - javax.swing.text.html, - javax.swing.text.html.parser, - javax.swing.text.rtf, - javax.swing.tree, - javax.swing.undo, - org.w3c.dom, - org.w3c.dom.bootstrap, - org.w3c.dom.events, - org.w3c.dom.ls, + org.armedbear.lisp, + org.osgi.framework, org.xml.sax, - org.xml.sax.ext, - org.xml.sax.helpers, - org.armedbear.lisp, - org.armedbear.lisp.java, - org.armedbear.lisp.java.awt, - org.armedbear.lisp.java.swing, - org.armedbear.lisp.util -Bundle-ClassPath: .,lib/j.jar,lib/abcl.jar,lib/bsh-2.0b4.jar -Require-Bundle: org.eclipse.equinox.common, - org.eclipse.equinox.registry, - org.protege.common;bundle-version="4.1.0", - org.semanticweb.owl.owlapi;bundle-version="3.0.0", - org.protege.editor.core.application;bundle-version="4.1.0", - org.protege.editor.owl;bundle-version="4.1.0" + org.xml.sax.helpers +Bundle-ClassPath: .,lib/j.jar,lib/bsh-2.0b4.jar +Require-Bundle: org.protege.editor.core.application;bundle-version="4.1.0", + org.protege.editor.owl;bundle-version="4.1.0", + org.armedbear.lisp;bundle-version="1.0.0" diff -r d70bb583451d -r c21ffeaaba4a build.properties --- a/build.properties Tue Apr 20 18:17:39 2010 +0200 +++ b/build.properties Sun May 16 22:13:08 2010 +0200 @@ -3,6 +3,5 @@ bin.includes = META-INF/,\ .,\ lib/j.jar,\ - lib/abcl.jar,\ lib/bsh-2.0b4.jar diff -r d70bb583451d -r c21ffeaaba4a build.xml --- a/build.xml Tue Apr 20 18:17:39 2010 +0200 +++ b/build.xml Sun May 16 22:13:08 2010 +0200 @@ -50,6 +50,8 @@ + @@ -97,6 +99,7 @@ + diff -r d70bb583451d -r c21ffeaaba4a src/org/sciencecommons/protege/lisp/Activator.java --- a/src/org/sciencecommons/protege/lisp/Activator.java Tue Apr 20 18:17:39 2010 +0200 +++ b/src/org/sciencecommons/protege/lisp/Activator.java Sun May 16 22:13:08 2010 +0200 @@ -10,11 +10,9 @@ * to do any one-time startup of the environment. */ public void start(BundleContext context) throws Exception { - // LispContext.getInstance(); } public void stop(BundleContext context) throws Exception { - // LispContext.getInstance().dispose(); } } diff -r d70bb583451d -r c21ffeaaba4a src/org/sciencecommons/protege/lisp/LispContext.java --- a/src/org/sciencecommons/protege/lisp/LispContext.java Tue Apr 20 18:17:39 2010 +0200 +++ b/src/org/sciencecommons/protege/lisp/LispContext.java Sun May 16 22:13:08 2010 +0200 @@ -1,80 +1,54 @@ package org.sciencecommons.protege.lisp; -import org.armedbear.lisp.Interpreter; -import org.armedbear.lisp.Lisp; -import org.armedbear.lisp.Symbol; -import org.armedbear.lisp.LispObject; -import bsh.util.JConsole; -import java.io.InputStreamReader; import snow.swing.ConsoleDocument; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.text.JTextComponent; +import org.armedbear.lisp.Interpreter; + /* * The singleton approach is used here because we are relying on * the bundle being a singleton bundle. */ public class LispContext { - private static LispContext instance; - public Interpreter interpreter; - // public JConsole console; - public JScrollPane console; - public Thread lispThread; + private static LispContext instance; + public Interpreter interpreter; + public JScrollPane console; + public Thread lispThread; - private LispContext() { - // console = new JConsole(); - console = new JScrollPane(); - Runnable r = new Runnable() - { -// public void run() -// { -// System.setIn(console.getInputStream()); -// System.setOut(console.getOut()); -// System.setErr(console.getErr()); -// String[] args = {"--noinit"}; -// // String[] args = {}; -// interpreter = Interpreter.createDefaultInstance(args); -// Symbol.LOAD_VERBOSE.setSymbolValue(LispObject.getInstance(true)); -// if (interpreter == null) -// { console.print("null interpreter!"); } -// interpreter.eval("(print \"Starting up protege lisp interpreter\")"); -// // Lisp.resetIO(console.getInputStream(),console.getOut()); -// // interpreter.eval("(load \"/Users/alanr/repos/slimenew/slime/swank-loader.lisp\")"); -// interpreter.eval("(progn (loop do (print (read-char))))"); -// interpreter.run(); -// }; + private LispContext() { + console = new JScrollPane(); + Runnable r = new Runnable() { + public void run() { + try { + String[] args = {"--noinit"}; + interpreter = Interpreter.createInstance(); + } catch (Throwable e) { + e.printStackTrace(); + System.exit(1); + } + final ConsoleDocument d = new ConsoleDocument(interpreter.eval("#'top-level::top-level-loop")); + final JTextComponent txt = new JTextArea(d); + d.setupTextComponent(txt); + console.setViewportView(txt); + // interpreter.run(); + } + }; + Thread t = new Thread(null, r, "interpreter", 4194304L); + t.start(); + } - public void run() - { - try { - String[] args = {"--noinit"}; - interpreter = Interpreter.createInstance(); - } catch (Throwable e) { - e.printStackTrace(); - System.exit(1); - } - final ConsoleDocument d = new ConsoleDocument(interpreter.eval("#'top-level::top-level-loop")); - final JTextComponent txt = new JTextArea(d); - d.setupTextComponent(txt); - console.setViewportView(txt); - // interpreter.run(); - } - - }; - new Thread(null, r, "interpreter", 4194304L).start(); + public static LispContext getInstance() { + if (instance == null) { + instance = new LispContext(); } - - public static LispContext getInstance() { - if (instance == null) { - instance = new LispContext(); - } - return instance; - } - - public void dispose() { - interpreter.dispose(); - instance = null; - } + return instance; + } + + public void dispose() { + interpreter.dispose(); + instance = null; + } } diff -r d70bb583451d -r c21ffeaaba4a src/org/sciencecommons/protege/lisp/LispShellView.java --- a/src/org/sciencecommons/protege/lisp/LispShellView.java Tue Apr 20 18:17:39 2010 +0200 +++ b/src/org/sciencecommons/protege/lisp/LispShellView.java Sun May 16 22:13:08 2010 +0200 @@ -1,43 +1,19 @@ package org.sciencecommons.protege.lisp; +import java.awt.BorderLayout; +import java.awt.Window; +import java.awt.event.ActionEvent; +import java.io.File; +import java.util.HashSet; + +import javax.swing.JScrollPane; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; + import org.armedbear.lisp.Interpreter; -import org.armedbear.j.Editor; -import bsh.util.JConsole; -import java.io.InputStream; -import java.io.OutputStream; -import javax.swing.JScrollPane; - import org.protege.editor.core.ui.util.UIUtil; import org.protege.editor.core.ui.view.DisposableAction; import org.protege.editor.owl.ui.view.AbstractOWLViewComponent; -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.io.File; -import java.io.IOException; -import java.util.HashSet;/* - -* Copyright (C) 2007, University of Manchester -* -* Modifications to the initial code base are copyright of their -* respective authors, or their employers as appropriate. Authorship -* of the modifications may be determined from the ChangeLog placed at -* the end of this file. -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Lesser General Public -* License as published by the Free Software Foundation; either -* version 2.1 of the License, or (at your option) any later version. - -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. - -* You should have received a copy of the GNU Lesser General Public -* License along with this library; if not, write to the Free Software -* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ /** * Author: drummond
@@ -51,93 +27,81 @@ * 2010-04-01 */ -public class LispShellView extends AbstractOWLViewComponent { +public class LispShellView + extends AbstractOWLViewComponent +{ + private Interpreter interpreter; + private JScrollPane console; + private File lastRunScript = null; - private Interpreter interpreter; - // private JConsole console; - private JScrollPane console; - private File lastRunScript = null; + private DisposableAction runScriptAction = new DisposableAction("Load file", null){ - private DisposableAction runScriptAction = new DisposableAction("Load file", null){ + public void actionPerformed(ActionEvent event) { + handleRunScript(); + } - public void actionPerformed(ActionEvent event) { - handleRunScript(); - } - - public void dispose() { - } + public void dispose() { + } }; - private DisposableAction reRunScriptAction = new DisposableAction("Reload file", null){ + private DisposableAction reRunScriptAction = new DisposableAction("Reload file", null){ - public void actionPerformed(ActionEvent event) { - handleReRunScript(); - } + public void actionPerformed(ActionEvent event) { + handleReRunScript(); + } - public void dispose() { - } + public void dispose() { + } }; - private void handleRunScript() { - Window f = (Window) SwingUtilities.getAncestorOfClass(Window.class, this); - File file = UIUtil.openFile(f, "Select a file to load", new HashSet()); - if (file != null){ - lastRunScript = file; - reRunScriptAction.setEnabled(true); - runScript(file); - } + private void handleRunScript() { + Window f = (Window) SwingUtilities.getAncestorOfClass(Window.class, this); + File file = UIUtil.openFile(f, "Select a file to load", new HashSet()); + if (file != null){ + lastRunScript = file; + reRunScriptAction.setEnabled(true); + runScript(file); } + } - private void handleReRunScript() { - if (lastRunScript != null){ - runScript(lastRunScript); - } + private void handleReRunScript() { + if (lastRunScript != null){ + runScript(lastRunScript); } + } - private void runScript(File script){ - if (script != null){ - // try { - LispContext.getInstance().interpreter.eval("(load \""+script.toString()+"\")"); - LispContext.getInstance().interpreter.eval("(force-output t)"); - } - // if (result != null){ - // console.println(result); - //} - // } - // catch (IOException e) { - // throw new RuntimeException(e); - // } - // catch (EvalError evalError) { - // console.print(evalError, Color.RED); - // } - // } + private void runScript(File script){ + if (script != null){ + LispContext.getInstance().interpreter.eval("(load \""+script.toString()+"\")"); + LispContext.getInstance().interpreter.eval("(force-output t)"); } + } - protected void initialiseOWLView() throws Exception { - installUIClasses(); - org.armedbear.j.Editor.main(new String[0]); - setLayout(new BorderLayout()); + protected void initialiseOWLView() throws Exception { + installUIClasses(); + setLayout(new BorderLayout()); - console = LispContext.getInstance().console; - add(console, BorderLayout.CENTER); - addAction(runScriptAction, "A", "A"); - addAction(reRunScriptAction, "A", "B"); + console = LispContext.getInstance().console; + add(console, BorderLayout.CENTER); + addAction(runScriptAction, "A", "A"); + addAction(reRunScriptAction, "A", "B"); - reRunScriptAction.setEnabled(false); - } + reRunScriptAction.setEnabled(false); + + } - private void installUIClasses() { - UIManager.getDefaults().put(org.armedbear.j.ButtonUI.class.getCanonicalName(), org.armedbear.j.ButtonUI.class); - UIManager.getDefaults().put(org.armedbear.j.LabelUI.class.getCanonicalName(), org.armedbear.j.LabelUI.class); - UIManager.getDefaults().put(org.armedbear.j.ScrollBarUI.class.getCanonicalName(), org.armedbear.j.ScrollBarUI.class); - UIManager.getDefaults().put(org.armedbear.j.ToolBarUI.class.getCanonicalName(), org.armedbear.j.ToolBarUI.class); - UIManager.getDefaults().put(org.armedbear.j.ToolTipUI.class.getCanonicalName(), org.armedbear.j.ToolTipUI.class); - } + private void installUIClasses() { + UIManager.getDefaults().put(org.armedbear.j.ButtonUI.class.getCanonicalName(), org.armedbear.j.ButtonUI.class); + UIManager.getDefaults().put(org.armedbear.j.LabelUI.class.getCanonicalName(), org.armedbear.j.LabelUI.class); + UIManager.getDefaults().put(org.armedbear.j.ScrollBarUI.class.getCanonicalName(), org.armedbear.j.ScrollBarUI.class); + UIManager.getDefaults().put(org.armedbear.j.ToolBarUI.class.getCanonicalName(), org.armedbear.j.ToolBarUI.class); + UIManager.getDefaults().put(org.armedbear.j.ToolTipUI.class.getCanonicalName(), org.armedbear.j.ToolTipUI.class); + } - protected void disposeOWLView() { - // do nothing - } + protected void disposeOWLView() { + // do nothing + } } diff -r d70bb583451d -r c21ffeaaba4a src/snow/swing/ConsoleDocument.java --- a/src/snow/swing/ConsoleDocument.java Tue Apr 20 18:17:39 2010 +0200 +++ b/src/snow/swing/ConsoleDocument.java Sun May 16 22:13:08 2010 +0200 @@ -59,253 +59,253 @@ public class ConsoleDocument extends DefaultStyledDocument { - private StringBuffer inputBuffer = new StringBuffer(); + private StringBuffer inputBuffer = new StringBuffer(); - private Reader reader = new Reader() { + private Reader reader = new Reader() { - @Override - public void close() throws RuntimeException {} + @Override + public void close() throws RuntimeException {} - @Override - public synchronized int read(char[] cbuf, int off, int len) throws RuntimeException { - try { - int length = Math.min(inputBuffer.length(), len); - while(length <= 0) { - wait(); - length = Math.min(inputBuffer.length(), len); - } - inputBuffer.getChars(0, length, cbuf, off); - inputBuffer.delete(0, length); - return length; - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - }; + @Override + public synchronized int read(char[] cbuf, int off, int len) throws RuntimeException { + try { + int length = Math.min(inputBuffer.length(), len); + while(length <= 0) { + wait(); + length = Math.min(inputBuffer.length(), len); + } + inputBuffer.getChars(0, length, cbuf, off); + inputBuffer.delete(0, length); + return length; + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + }; - private Writer writer = new Writer() { + private Writer writer = new Writer() { - @Override - public void close() throws RuntimeException {} + @Override + public void close() throws RuntimeException {} - @Override - public void flush() throws RuntimeException {} + @Override + public void flush() throws RuntimeException {} - @Override - public void write(final char[] cbuf, final int off, final int len) throws RuntimeException { - try { - final int insertOffs; - synchronized(reader) { - if(inputBuffer.toString().matches("^\\s*$")) { - int length = inputBuffer.length(); - inputBuffer.delete(0, length); - } - insertOffs = getLength() - inputBuffer.length(); - reader.notifyAll(); - } - Runnable r = new Runnable() { - public void run() { - synchronized(reader) { - try { - superInsertString(insertOffs, - new String(cbuf, off, len), - null); - } catch(Exception e) { - assert(false); //BadLocationException should not happen here - } - } - } - }; - SwingUtilities.invokeAndWait(r); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - }; + @Override + public void write(final char[] cbuf, final int off, final int len) throws RuntimeException { + try { + final int insertOffs; + synchronized(reader) { + if(inputBuffer.toString().matches("^\\s*$")) { + int length = inputBuffer.length(); + inputBuffer.delete(0, length); + } + insertOffs = getLength() - inputBuffer.length(); + reader.notifyAll(); + } + Runnable r = new Runnable() { + public void run() { + synchronized(reader) { + try { + superInsertString(insertOffs, + new String(cbuf, off, len), + null); + } catch(Exception e) { + assert(false); //BadLocationException should not happen here + } + } + } + }; + SwingUtilities.invokeAndWait(r); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + }; - private boolean disposed = false; - private final Thread replThread; + private boolean disposed = false; + private final Thread replThread; - public ConsoleDocument(LispObject replFunction) { - final LispObject replWrapper = makeReplWrapper(new Stream(Symbol.SYSTEM_STREAM, new BufferedReader(reader)), - new Stream(Symbol.SYSTEM_STREAM, new BufferedWriter(writer)), - replFunction); - replThread = new Thread("REPL-thread-" + System.identityHashCode(this)) { - public void run() { - while(true) { - replWrapper.execute(); - yield(); - } - } - }; - replThread.start(); + public ConsoleDocument(LispObject replFunction) { + final LispObject replWrapper = makeReplWrapper(new Stream(Symbol.SYSTEM_STREAM, new BufferedReader(reader)), + new Stream(Symbol.SYSTEM_STREAM, new BufferedWriter(writer)), + replFunction); + replThread = new Thread("REPL-thread-" + System.identityHashCode(this)) { + public void run() { + while(true) { + replWrapper.execute(); + yield(); + } + } + }; + replThread.start(); + } + + @Override + public void insertString(int offs, String str, AttributeSet a) + throws BadLocationException { + synchronized(reader) { + int bufferStart = getLength() - inputBuffer.length(); + if(offs < bufferStart) { + throw new BadLocationException("Can only insert after " + bufferStart, offs); + } + superInsertString(offs, str, a); + inputBuffer.insert(offs - bufferStart, str); + if(processInputP(inputBuffer, str)) { + reader.notifyAll(); + } } + } + + protected void superInsertString(int offs, String str, AttributeSet a) + throws BadLocationException { + super.insertString(offs, str, a); + } + + /** + * Guaranteed to run with exclusive access to the buffer. + * @param sb NB sb MUST NOT be destructively modified!! + * @return + */ + protected boolean processInputP(StringBuffer sb, String str) { + if(str.indexOf("\n") == -1) { + return false; + } + int parenCount = 0; + int len = sb.length(); + for(int i = 0; i < len; i++) { + char c = sb.charAt(i); + if(c == '(') { + parenCount++; + } else if(c == ')') { + parenCount--; + if(parenCount == 0) { + return true; + } + } + } + return parenCount <= 0; + } + + @Override + public void remove(int offs, int len) throws BadLocationException { + synchronized(reader) { + int bufferStart = getLength() - inputBuffer.length(); + if(offs < bufferStart) { + throw new BadLocationException("Can only remove after " + bufferStart, offs); + } + super.remove(offs, len); + inputBuffer.delete(offs - bufferStart, offs - bufferStart + len); + } + } + + public Reader getReader() { + return reader; + } + + public Writer getWriter() { + return writer; + } + + public void setupTextComponent(final JTextComponent txt) { + addDocumentListener(new DocumentListener() { - @Override - public void insertString(int offs, String str, AttributeSet a) - throws BadLocationException { - synchronized(reader) { - int bufferStart = getLength() - inputBuffer.length(); - if(offs < bufferStart) { - throw new BadLocationException("Can only insert after " + bufferStart, offs); - } - superInsertString(offs, str, a); - inputBuffer.insert(offs - bufferStart, str); - if(processInputP(inputBuffer, str)) { - reader.notifyAll(); - } - } + // @Override + public void changedUpdate(DocumentEvent e) { + } + + // @Override + public void insertUpdate(DocumentEvent e) { + int len = getLength(); + if(len - e.getLength() == e.getOffset()) { //The insert was at the end of the document + txt.setCaretPosition(getLength()); + } + } + + // @Override + public void removeUpdate(DocumentEvent e) { + } + }); + txt.setCaretPosition(getLength()); + } + + public void dispose() { + disposed = true; + for(DocumentListener listener : getDocumentListeners()) { + removeDocumentListener(listener); } + try { + reader.close(); + writer.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + replThread.interrupt(); //really? + } - protected void superInsertString(int offs, String str, AttributeSet a) - throws BadLocationException { - super.insertString(offs, str, a); + private final LispObject debuggerHook = new Function() { + + @Override + public LispObject execute(LispObject condition, LispObject debuggerHook) { + if(disposed) { + return PACKAGE_SYS.findSymbol("%DEBUGGER-HOOK-FUNCTION").execute(condition, debuggerHook); + } else { + return NIL; + } + } + + }; + + public LispObject makeReplWrapper(final Stream in, final Stream out, final LispObject fn) { + return new Function() { + @Override + public LispObject execute() { + SpecialBindingsMark lastSpecialBinding = LispThread.currentThread().markSpecialBindings(); + try { + TwoWayStream ioStream = new TwoWayStream(in, out); + LispThread.currentThread().bindSpecial(Symbol.DEBUGGER_HOOK, debuggerHook); + LispThread.currentThread().bindSpecial(Symbol.STANDARD_INPUT, in); + LispThread.currentThread().bindSpecial(Symbol.STANDARD_OUTPUT, out); + LispThread.currentThread().bindSpecial(Symbol.ERROR_OUTPUT, out); + LispThread.currentThread().bindSpecial(Symbol.TERMINAL_IO, ioStream); + LispThread.currentThread().bindSpecial(Symbol.DEBUG_IO, ioStream); + LispThread.currentThread().bindSpecial(Symbol.QUERY_IO, ioStream); + return fn.execute(); + } finally { + LispThread.currentThread().resetSpecialBindings(lastSpecialBinding); + } + } + + }; + } + + public void disposeOnClose(final Window parent) { + parent.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + dispose(); + parent.removeWindowListener(this); + } + }); + } + + public static void main(String[] args) { + LispObject repl = null; + try { + repl = Interpreter.createInstance().eval("#'top-level::top-level-loop"); + } catch (Throwable e) { + e.printStackTrace(); + System.exit(1); } - - /** - * Guaranteed to run with exclusive access to the buffer. - * @param sb NB sb MUST NOT be destructively modified!! - * @return - */ - protected boolean processInputP(StringBuffer sb, String str) { - if(str.indexOf("\n") == -1) { - return false; - } - int parenCount = 0; - int len = sb.length(); - for(int i = 0; i < len; i++) { - char c = sb.charAt(i); - if(c == '(') { - parenCount++; - } else if(c == ')') { - parenCount--; - if(parenCount == 0) { - return true; - } - } - } - return parenCount <= 0; - } - - @Override - public void remove(int offs, int len) throws BadLocationException { - synchronized(reader) { - int bufferStart = getLength() - inputBuffer.length(); - if(offs < bufferStart) { - throw new BadLocationException("Can only remove after " + bufferStart, offs); - } - super.remove(offs, len); - inputBuffer.delete(offs - bufferStart, offs - bufferStart + len); - } - } - - public Reader getReader() { - return reader; - } - - public Writer getWriter() { - return writer; - } - - public void setupTextComponent(final JTextComponent txt) { - addDocumentListener(new DocumentListener() { - - // @Override - public void changedUpdate(DocumentEvent e) { - } - - // @Override - public void insertUpdate(DocumentEvent e) { - int len = getLength(); - if(len - e.getLength() == e.getOffset()) { //The insert was at the end of the document - txt.setCaretPosition(getLength()); - } - } - - // @Override - public void removeUpdate(DocumentEvent e) { - } - }); - txt.setCaretPosition(getLength()); - } - - public void dispose() { - disposed = true; - for(DocumentListener listener : getDocumentListeners()) { - removeDocumentListener(listener); - } - try { - reader.close(); - writer.close(); - } catch (Exception e) { - throw new RuntimeException(e); - } - replThread.interrupt(); //really? - } - - private final LispObject debuggerHook = new Function() { - - @Override - public LispObject execute(LispObject condition, LispObject debuggerHook) { - if(disposed) { - return PACKAGE_SYS.findSymbol("%DEBUGGER-HOOK-FUNCTION").execute(condition, debuggerHook); - } else { - return NIL; - } - } - - }; - - public LispObject makeReplWrapper(final Stream in, final Stream out, final LispObject fn) { - return new Function() { - @Override - public LispObject execute() { - SpecialBindingsMark lastSpecialBinding = LispThread.currentThread().markSpecialBindings(); - try { - TwoWayStream ioStream = new TwoWayStream(in, out); - LispThread.currentThread().bindSpecial(Symbol.DEBUGGER_HOOK, debuggerHook); - LispThread.currentThread().bindSpecial(Symbol.STANDARD_INPUT, in); - LispThread.currentThread().bindSpecial(Symbol.STANDARD_OUTPUT, out); - LispThread.currentThread().bindSpecial(Symbol.ERROR_OUTPUT, out); - LispThread.currentThread().bindSpecial(Symbol.TERMINAL_IO, ioStream); - LispThread.currentThread().bindSpecial(Symbol.DEBUG_IO, ioStream); - LispThread.currentThread().bindSpecial(Symbol.QUERY_IO, ioStream); - return fn.execute(); - } finally { - LispThread.currentThread().resetSpecialBindings(lastSpecialBinding); - } - } - - }; - } - - public void disposeOnClose(final Window parent) { - parent.addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - dispose(); - parent.removeWindowListener(this); - } - }); - } - - public static void main(String[] args) { - LispObject repl = null; - try { - repl = Interpreter.createInstance().eval("#'top-level::top-level-loop"); - } catch (Throwable e) { - e.printStackTrace(); - System.exit(1); - } - final ConsoleDocument d = new ConsoleDocument(repl); - final JTextComponent txt = new JTextArea(d); - d.setupTextComponent(txt); - JFrame f = new JFrame(); - f.add(new JScrollPane(txt)); - d.disposeOnClose(f); - f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); - f.pack(); - f.setVisible(true); - } + final ConsoleDocument d = new ConsoleDocument(repl); + final JTextComponent txt = new JTextArea(d); + d.setupTextComponent(txt); + JFrame f = new JFrame(); + f.add(new JScrollPane(txt)); + d.disposeOnClose(f); + f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); + f.pack(); + f.setVisible(true); + } }