Managing pure Common Lisp files on Medley
Managing Lisp code in the residential environment of Medley differs from similar tasks in traditional file based Common Lisp systems.
In a previous post I explained how the residential environment of Medley works, discussed some of its facilities and tools, and introduced a workflow for managing Common Lisp code under the residential environment. This leverages the way Medley is designed to work and minimizes friction.
In this post I explain how to use Medley as a file based environment, albeit with some friction and reduced functionality.
I show how to edit, organize, and load pure Common Lisp files, i.e. source files not under the control of the File Manager. Pure files are ordinary text files that contain only Common Lisp code, with no metadata or font control commands like the code databases the File Manager maintains.
Motivation
Letting the residential environment track and control code is the most convenient and productive way of using Medley for Lisp development.
But you can still manually manage pure Common Lisp files. For example, you may want to use from Medley some code you mainly intend to load into the environment and don't change often. This is the case of external libraries and programs developed elsewhere, which you call from new code or run in Medley.
Using Common Lisp code not developed for the pre ANSI implementation of Medley may need adaptation. So let's see how to edit source files.
Editing source files
To create or modify pure Common Lisp files use the TEdit word processor of Medley, not the SEdit structure editor. SEdit doesn't directly manipulate the arbitrary, unstructured text of pure files.
TEdit can read and write ordinary text files, but not by default. To save a file as ASCII execute the command Put
> Plain-Text
. Execute Get
> Unformatted Get
to load such a file.
The main downside of using TEdit for Common Lisp is the program is not Lisp aware. It doesn't support automatic indentation, prettyprinting, perenthesis matching, and code evaluation. Another major problem is cursor keys don't work in Medley, which severely limits text editing with TEdit. The Medley team is well aware of the issue.
An alternative is to use your favorite Lisp editor to edit the Common Lisp sources outside of Medley and then load the files from Medley.
Format of Pure Common Lisp files
Although Medley can load pure Common Lisp sources, these files do have some minimal formatting requirements. A pure Common Lisp file must begin with a semicolon ;
character, otherwise the system assumes it's an Interlisp file.
Other than that a pure file can contain any Common Lisp feature Medley supports, in any order the language allows.
Loading source files
Once a pure Lisp file is available, load it in Medley with the usual file loading forms such as CL:LOAD
. The way symbols are interned depends on whether the file defines or makes current any Common Lisp packages.
Let's go over the main cases: the code has no packages; a single file contains both package and function definitions; the code is split between a file for the package and another for the function definitions.
No package definition
The simplest type of pure file contains no package forms, just function definitions and other Common Lisp expressions.
In this case Medley interns symbols in the USER
package without exporting them. The Medley implementation of Common Lisp is closer to CLtLx than ANSI, so it provides the standardized packages LISP
(nicknames: COMMON-LISP
and CL
) and USER
instead of COMMON-LISP
and COMMON-LISP-USER
.
For example, suppose this file SQUARE.LISP
defines a function calc-square
to compute the square of its argument:
;;; SQUARE.LISP
(defun calc-square (x)
"Return the square of X."
(* x x))
After evaluating (load "SQUARE.LISP")
, at the >
prompt of a Common Lisp Exec you call the function using the proper package qualifier:
> (user::square 3)
9
If the USER
package isn't adequate for your code, on Medley CL:LOAD
also accepts the keyword parameter :package
to supply a package to which *package*
is bound when reading the file. For example, at the >
prompt of a Xerox Common Lisp (XCL) Exec you can pass the package XCL-USER
with a form like:
> (load "SQUARE.LISP" :package (find-package "XCL-USER"))
You must pass an actual package object to :package
, not a package designator like :xcl-user
.
Since XCL-USER
is the default package of XCL Execs no qualifier is required for calling the function:
> (square 3)
9
Package and definitions in one file
Not using packages may be adequate for very small files. However, typical Common Lisp programs do define their own packages.
This example file PKGDEMO.LISP
defines the package PKGDEMO
with nickname PD
that exports the function FUN
, which just prints a message:
;;; PKGDEMO.LISP
(in-package "XCL-USER")
(defpackage "PKGDEMO"
(:use "LISP" "XCL")
(:nicknames "PD")
(:export "FUN"))
(in-package "PKGDEMO")
(defun fun ()
(format t "Hello from PKGDEMO:FUN."))
The file holds both the package definition and the rest of the code. It's important that the first form in the file be in-package
to make current a known package like XCL-USER
. XCL-USER
is the Medley equivalent of COMMON-LISP-USER
and uses the LISP
package with the standard Common Lisp symbols.
After loading PKGDEMO.LISP
from an XCL Exec with (load "PKGDEMO.LISP")
you can call the exported function like this:
> (pkgdemo:fun)
Hello from PKGDEMO:FUN.
NIL
or via the package nickname:
> (pd:fun)
Hello from PKGDEMO:FUN.
NIL
Separate package and definition files
Unlike PKGDEMO.LISP
in the previous example, most Common Lisp programs split the code between at least two files, one holding the package definition and the other the function definitions. Let's rewrite PKGDEMO.LISP
by storing the package in the file PKGSPLIT-PKG.LISP
:
;;; PKGSPLIT-PKG.LISP
(in-package "XCL-USER")
(defpackage "PKGDEMO"
(:use "LISP" "XCL")
(:nicknames "PD")
(:export "FUN"))
The function definition goes into the file PKGSPLIT-FUN.LISP
that begins with an appropriate in-package
followed by the rest of the code:
;;; PKGSPLIT-FUN.LISP
(in-package "PKGDEMO")
(defun fun ()
(format t "Hello from PKGDEMO:FUN."))
Finally, make sure to load the package first with (load "PKGSPLIT-PKG.LISP")
, then the function with (load "PKGSPLIT-FUN.LISP")
. Loading the files creates the same objects as the single file example PKGDEMO.LISP
and you can call the function the same way:
> (pkgdemo:fun)
Hello from PKGDEMO:FUN.
NIL
> (pd:fun)
Hello from PKGDEMO:FUN.
NIL
Discuss... Email | Reply @amoroso@fosstodon.org