Undo previous commmit.

parent ac9d7d9b
......@@ -464,8 +464,6 @@ will load ASDF-INSTALL.
<include name="abcl.in"/>
<include name="abcl.bat.in"/>
<include name="examples/**"/>
<!-- The remainder of these files are used by the Lisp hosted
build in 'build-abcl.lisp' but not used by Ant, so include
them in the source distribution. -->
......
ABCL Examples
=============
Contributions from:
Ville Voutilainen, Alex Muscar, Blake McBride, and Mark Evenson
google-app-engine
This example shows how to run a ABCL in a Java Servlet context in
general and in Google App Engine (GAE) in particular.
gui
Examples of how to interact with Swing/AWT GUI elements.
java-exception
Handling Java exceptions with the Lisp condition system.
java-interface
Implementing a Java interface with Lisp.
java-to-lisp-1
Simple examples of calling Lisp from Java.
java-to-lisp-2
More involved example of calling Lisp from Java including
parameters and return values.
jsr-223
Using the implementation of the JSR-223 interface to use Common
Lisp as a pluggable scripting language on the JVM.
lisp-to-java
Calling Java code from Lisp.
misc
Code snippets currently without documentation.
\ No newline at end of file
;;; complete.lisp
;;;
;;; Copyright (C) 2004 Peter Graves
;;; $Id: complete.lisp,v 1.2 2004-09-05 00:12:25 piso Exp $
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation; either version 2
;;; of the License, or (at your option) any later version.
;;;
;;; This program 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 General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
(in-package "J")
(export 'complete)
(defvar *prefix* nil)
(defvar *completions* ())
(defvar *completion-index* 0)
(defun compound-prefix-match (prefix target)
(let ((tlen (length target))
(tpos 0))
(dotimes (i (length prefix))
(when (>= tpos tlen)
(return-from compound-prefix-match nil))
(let ((ch (schar prefix i)))
(if (char= ch #\-)
(unless (setf tpos (position #\- target :start tpos))
(return-from compound-prefix-match nil))
(unless (char-equal ch (schar target tpos))
(return-from compound-prefix-match nil)))
(incf tpos)))
t))
(defun completion-set (prefix)
(let ((result ()))
(do-external-symbols (symbol "CL")
(let ((name (symbol-name symbol)))
(when (compound-prefix-match prefix name)
(push symbol result))))
result))
(defun completion-prefix ()
(let* ((string (line-chars (current-line)))
(end (mark-charpos (current-point))))
(do ((start (1- end) (1- start)))
((< start 0) (subseq string 0 end))
(let ((ch (schar string start)))
(when (or (eql ch #\space) (eql ch #\())
(incf start)
(return-from completion-prefix (subseq string start end)))))))
(defun complete ()
(cond ((eq *last-command* 'complete)
(unless (> (length *completions*) 1)
(return-from complete))
(undo)
(incf *completion-index*)
(when (> *completion-index* (1- (length *completions*)))
(setf *completion-index* 0)))
(t
(setf *prefix* (completion-prefix)
*completions* nil
*completion-index* 0)
(when *prefix*
(setf *completions* (completion-set *prefix*)))))
(when *completions*
(let ((completion (string-downcase (nth *completion-index* *completions*)))
(point (current-point)))
(with-single-undo
(goto-char (make-mark (mark-line point)
(- (mark-charpos point) (length *prefix*))))
(set-mark point)
(delete-region)
(insert completion)))
(setf *current-command* 'complete))
(values))
(map-key-for-mode "Ctrl Space" "(complete)" "Lisp")
(map-key-for-mode "Ctrl Space" "(complete)" "Lisp Shell")
Google App Engine
=================
Alex Muscar
Running ABCL in a Google App Engine container.
This example shows how to run your servlet off ABCL in general
and in Google App Engine (GAE) in particular.
When uploading your code to the server, be sure to put abcl.jar
in war/WEB-INF/lib.
<project default="compile">
<property name="sdk.dir"
location="../../../appengine-java-sdk" />
<import file="${sdk.dir}/config/user/ant-macros.xml" />
<path id="project.classpath">
<pathelement path="war/WEB-INF/classes" />
<fileset dir="war/WEB-INF/lib">
<include name="**/*.jar" />
</fileset>
<fileset dir="${sdk.dir}/lib">
<include name="shared/**/*.jar" />
</fileset>
</path>
<target name="copyjars"
description="Copies the App Engine and ABCL JARs to the WAR.">
<copy
todir="war/WEB-INF/lib"
flatten="true">
<fileset dir="${sdk.dir}/lib/user">
<include name="**/*.jar" />
</fileset>
<fileset dir="../..">
<include name="dist/*.jar" />
</fileset>
</copy>
<copy
todir="war/fasls">
<fileset dir="src">
<include name="*.abcl" />
</fileset>
</copy>
</target>
<target name="compile" depends="copyjars,abcl.jar"
description="Compiles Java source and copies other source files to the WAR.">
<mkdir dir="war/WEB-INF/classes" />
<copy todir="war/WEB-INF/classes">
<fileset dir="src">
<exclude name="**/*.java" />
</fileset>
</copy>
<javac
srcdir="src"
destdir="war/WEB-INF/classes"
classpathref="project.classpath"
includeantruntime="false"
debug="on" />
</target>
<target name="abcl.jar">
<ant dir="../.." target="abcl.jar"/>
</target>
<target name="clean" description="Cleans all the jars and fasls.">
<delete>
<fileset dir="." includes="**/*.jar" />
<fileset dir="." includes="**/*.class" />
<fileset dir="." includes="**/*.abcl" />
</delete>
</target>
<target name="runserver" depends="compile"
description="Starts the development server.">
<dev_appserver war="war" />
</target>
<target name="runserver-debug" depends="compile"
description="Starts the development server.">
<dev_appserver war="war" port="8888"/>
</target>
</project>
\ No newline at end of file
package abcl_ae;
import java.io.FileInputStream;
import java.io.IOException;
import org.armedbear.lisp.Lisp;
import org.armedbear.lisp.Load;
import org.armedbear.lisp.Interpreter;
import org.armedbear.lisp.Symbol;
import org.armedbear.lisp.Pathname;
public final class AbclInit {
static private Object lock = new Object();
static private boolean initialized = false;
// package access level
static void init() {
if (initialized)
return;
synchronized (lock) {
if (initialized)
return;
Interpreter.initializeLisp();
Load.load("fasls/first-servlet.abcl");
initialized = true;
}
}
}
\ No newline at end of file
package abcl_ae;
import java.io.IOException;
import javax.servlet.http.*;
import javax.servlet.*;
import org.armedbear.lisp.Interpreter;
import org.armedbear.lisp.LispThread;
import org.armedbear.lisp.Lisp;
import org.armedbear.lisp.Symbol;
import org.armedbear.lisp.SpecialBinding;
import org.armedbear.lisp.SpecialBindingsMark;
import org.armedbear.lisp.Load;
import org.armedbear.lisp.Stream;
public class HelloWorldServlet extends HttpServlet {
static private Symbol doGet = null;
public void init() throws ServletException {
AbclInit.init();
doGet = Lisp.internInPackage("DO-GET", "FIRST-SERVLET");
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
LispThread currentThread = LispThread.currentThread();
SpecialBindingsMark mark = currentThread.markSpecialBindings();
currentThread.bindSpecial(
Symbol.STANDARD_OUTPUT,
new Stream(Symbol.SYSTEM_STREAM, resp.getOutputStream(),
Symbol.CHARACTER, false));
try {
currentThread.execute(doGet);
} finally {
currentThread.resetSpecialBindings(mark);
}
}
}
\ No newline at end of file
(defpackage #:first-servlet
(:use :cl)
(:export #:do-get))
(in-package #:first-servlet)
(defun do-get ()
(format t "Hello, World!~%"))
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>abcl-test</application>
<version>1</version>
</appengine-web-app>
(defpackage #:first-servlet
(:use :cl)
(:export #:do-get))
(in-package #:first-servlet)
(defun do-get ()
(format t "Hello, World!~%"))
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>abcl_ae.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
</web-app>
\ No newline at end of file
Graphical User Interface
========================
abcl.DialogPromptStream
swing.SwingDialogPromptStream
awt.AwtDialogPromptStream
Provides an example of a GUI abstraction that serves as a drop-in
replacement for *DEBUG-IO*. The concrete classes
SwingDialogPromptStream and AwtDialogPromptStream provide
implemntations in Swing and AWT respectively.
awt.ActionListener
awt.ComponentAdapter
awt.KeyAdaptor
awt.MouseAdaptor
awt.MotionMouseAdaptor
awt.WindowAdaptor
How to map standard AWT listeners and adaptors to Lisp by using the
org.armedbear.lisp.JHandler callback mechanism.
package org.armedbear.lisp.java;
package abcl;
import java.io.IOException;
import java.io.Reader;
......@@ -6,12 +6,17 @@ import java.io.StringReader;
import java.io.StringWriter;
import org.armedbear.lisp.Stream;
/**
* A bidirectional stream that captures input from a modal dialog. The dialog reports a label (prompt line)
* which shows to the user everything that has been printed to the stream up to the moment when the dialog
* became visible. It is usable as a drop-in replacement for e.g. *debug-io*.<br />
* This is an abstract class that does not depend on any GUI library. Subclasses are expected to provide
* the actual code to show the dialog and read input from the user.
* A bidirectional stream that captures input from a modal dialog. The
* dialog reports a label (prompt line) which shows to the user
* everything that has been printed to the stream up to the moment
* when the dialog became visible. It is usable as a drop-in
* replacement for e.g. *debug-io*.<br /> This is an abstract class
* that does not depend on any GUI library. Subclasses are expected to
* provide the actual code to show the dialog and read input from the
* user.
*
* @author Alessio Stalla
*
*/
......
/*
* ActionListener.java
*
* Copyright (C) 2003 Peter Graves
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package awt;
import org.armedbear.lisp.JHandler;
import java.awt.event.ActionEvent;
import java.awt.Button;
import java.awt.List;
import java.awt.MenuItem;
import java.awt.TextField;
import javax.swing.AbstractButton;
import javax.swing.JTextField;
public class ActionListener implements java.awt.event.ActionListener
{
public void actionPerformed(ActionEvent actionevent) {
String as[] = { actionevent.paramString(), actionevent.getActionCommand() };
int ai[] = { actionevent.getModifiers() };
long al[] = { actionevent.getWhen() }; // not yet used
JHandler.callLisp("ACTIONPERFORMED", handle, as, ai);
}
//AWT
public static synchronized void addTo(Button button) {
ActionListener actionlistener = new ActionListener();
actionlistener.handle = button;
button.addActionListener(actionlistener);
}
public static synchronized void addTo(List list) {
ActionListener actionlistener = new ActionListener();
actionlistener.handle = list;
list.addActionListener(actionlistener);
}
public static synchronized void addTo(MenuItem menuitem) {
ActionListener actionlistener = new ActionListener();
actionlistener.handle = menuitem;
menuitem.addActionListener(actionlistener);
}
public static synchronized void addTo(TextField textfield) {
ActionListener actionlistener = new ActionListener();
actionlistener.handle = textfield;
textfield.addActionListener(actionlistener);
}
//Swing
//takes care of JButton, JMenuItem, JToggleButton etc.
public static synchronized void addTo(AbstractButton ab) {
ActionListener actionlistener = new ActionListener();
actionlistener.handle = ab;
ab.addActionListener(actionlistener);
}
public static synchronized void addTo(JTextField textfield) {
ActionListener actionlistener = new ActionListener();
actionlistener.handle = textfield;
textfield.addActionListener(actionlistener);
}
private Object handle;
}
// $Id$
package awt;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import abcl.DialogPromptStream;
import javax.swing.JButton;
public class AwtDialogPromptStream extends DialogPromptStream {
Dialog dialog = new Dialog((Frame)null, true);
private Label prompt = new Label();
private TextField input = new TextField(32);
public AwtDialogPromptStream() {
this("Prompt");
}
public AwtDialogPromptStream(String title) {
super();
dialog.setTitle(title);
Panel tmpPanel = new Panel();
tmpPanel.add(prompt);
tmpPanel.add(input);
dialog.add(tmpPanel);
JButton okBtn = new JButton("Ok");
okBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
synchronized(dialog) {
dialog.dispose();
}
}
});
tmpPanel = new Panel(new FlowLayout());
tmpPanel.add(okBtn);
dialog.add(tmpPanel, BorderLayout.SOUTH);
}
@Override
protected void closeDialog() {
dialog.dispose();
}
@Override
protected String readInputFromModalDialog(String promptText) {
prompt.setText(promptText);
dialog.pack();
dialog.setVisible(true);
return input.getText();
}
}
/*
* ComponentAdapter.java
*
* Copyright (C) 2003 Peter Graves
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package awt;
import org.armedbear.lisp.JHandler;
import java.awt.Component;
import java.awt.event.ComponentEvent;
public class ComponentAdapter extends java.awt.event.ComponentAdapter {
public static synchronized void addTo(Component component) {
component.addComponentListener(new ComponentAdapter());
}
private void call(String s, ComponentEvent componentevent) {
JHandler.callLisp(s, componentevent.getComponent(), componentevent.paramString());
}
public void componentHidden(ComponentEvent componentevent) {
call("COMPONENTHIDDEN", componentevent);
}
public void componentMoved(ComponentEvent componentevent) {
call("COMPONENTMOVED", componentevent);
}
public void componentResized(ComponentEvent componentevent) {
call("COMPONENTRESIZED", componentevent);
}
public void componentShown(ComponentEvent componentevent) {
call("COMPONENTSHOWN", componentevent);
}
}
/*
* ItemListener.java
*
* Copyright (C) 2003-2005 Peter Graves
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package awt;
import java.awt.Checkbox;
import java.awt.CheckboxMenuItem;
import java.awt.Choice;
import java.awt.ItemSelectable;
import java.awt.List;
import java.awt.event.ItemEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.DefaultButtonModel;
import javax.swing.JComboBox;
import org.armedbear.lisp.JHandler;
public class ItemListener implements java.awt.event.ItemListener
{
public void itemStateChanged(ItemEvent itemevent)
{
String as[] = { itemevent.paramString(), itemevent.getItem().toString() };
int ai[] = { itemevent.getStateChange() != ItemEvent.SELECTED ? 0 : 1 };
JHandler.callLisp("ITEMSTATECHANGED", handle, as, ai);
}
public static synchronized void addTo(Checkbox checkbox)
{
ItemListener itemlistener = new ItemListener();
itemlistener.handle = checkbox;
checkbox.addItemListener(itemlistener);
}
public static synchronized void addTo(CheckboxMenuItem checkboxmenuitem)
{
ItemListener itemlistener = new ItemListener();
itemlistener.handle = checkboxmenuitem;
checkboxmenuitem.addItemListener(itemlistener);
}
public static synchronized void addTo(Choice choice)
{
ItemListener itemlistener = new ItemListener();
itemlistener.handle = choice;
choice.addItemListener(itemlistener);
}
public static synchronized void addTo(ItemSelectable itemselectable)
{
ItemListener itemlistener = new ItemListener();
itemlistener.handle = itemselectable;
itemselectable.addItemListener(itemlistener);
}
public static synchronized void addTo(List list)
{
ItemListener itemlistener = new ItemListener();
itemlistener.handle = list;
list.addItemListener(itemlistener);
}
//Swing
public static synchronized void addTo(AbstractButton abstractbutton)
{
ItemListener itemlistener = new ItemListener();
itemlistener.handle = abstractbutton;
abstractbutton.addItemListener(itemlistener);
}