A GUI Library For Lisp On OS X

by Christopher Roach

Related link: http://www.peter-herth.de/ltk/




Well, its been quite a while since my last blog entry (I've been enjoying myself a bit too much with my family preceded by a bit of skiing as well), but I'm back now and ready to give you all the next installment in my blog on programming languages for the Macintosh. If you've been following my weblog, then you'll no doubt have noticed that my theme for the past few entries has been Lisp—the functional and dynamic programming language with a strong history in AI research—and this post will not break from that tradition.




In my quest to find ways to use Lisp more and more in my everyday programming, I found myself wondering what I could do with Lisp in the arena of GUI libraries. I looked around for a bit and 'lo' and behold I came across Perter Herth's project for porting the Tk GUI library over to Lisp called Ltk. Now, if you've ever done any programming with other dynamic programming languages such as Perl, Python, or Ruby, you've probably come across Tk or perhaps you've even used it. If not, its definitely time you've taken a look at all it can do for you. (On a side note, if you're interested in learning a bit more about the Tk GUI toolkit, check out my articles on Ruby and Tk programming.)




Just in case you've never heard of Tk, its a GUI library that was originally designed to be used with the Tcl scripting language and its moved on since then to nearly every other scripting/glue/dynamic language available. If you've ever found yourself in the position of writing lots of small useful scripts, then Tk should definitely be your GUI library of choice. Its easy to use, portable, and tends to fit best with dynamic languages.




So, the point in all of this is that if, like me, you find Lisp to be a worthwhile, powerful, and interesting language and you've been wanting to find ways to use it more in your daily programming tasks, then take a look at Ltk. It works on OS X and its extremely easy to install.




Speaking of installation, all you need to get up to speed is an install of the Tk GUI library (this comes with the latest versions of OS X, however, you may wish to download and install a copy of the Tk with batteries binary, since Tiger only comes with a subset of the language), a distribution of Lisp (I've found that OpenMCL and SBCL are the easiest to setup, since they already come with ASDF which will be used to automatically load the Ltk library), and a copy of the Ltk library. Once, you've gotten everything downloaded, install both the Lisp distribution and the Tk binary and then add the ltk folder to a place where you would like to keep all of your Lisp related libraries. Then you'll need to tell your Lisp distro where to find all your library files. You'll do this by setting up your Lisp distro's ASDF central registry. To do this, add the following to your chosen distro's initialization file (for OpenMCL this will be openmcl-init.lisp and for SBCL this will be .sbclrc) in your home directory:





(require :asdf)

(setf asdf:*central-registry*
'(*default-pathname-defaults*
#p"/Path/to/your/central/registry/"))




Make sure to replace "/Path/to/your/central/registry/" with your own path, and make sure to end the path with a "/" since this is a directory and not a file. Once, you've added your distro's init file, you'll need to go to the directory you just specified and add a link to the ltk.asd file in ltk folder. The following will add this link:





$ cd /Path/to/your/central/registry/
$ ln -s ./ltk/ltk.asd .




These two lines will add a symbolic link to the ltk.asd file to the central registry, assuming that you've also added the ltk folder to this directory as well. If not, then point the link to wherever you decided to keep the folder.




Once you've finished all of these tasks, you should be totally setup and ready to try your hand at creating your very first Lisp/Ltk program. Open your favorite editor and create a file named hello.lisp. In this file place the following code:





(require 'ltk)

(defun hello-1()
(ltk:with-ltk ()
(let ((b (make-instance 'ltk:button
:master nil
:text "Press Me"
:command (lambda ()
(format t "Hello World!~&")))))
(ltk:pack b))))

(hello-1)




After you've added the above code to your new file and saved it, you should be able to execute your program by entering one of the following lines at the command prompt:





$ openmcl -l hello.lisp
$ sbcl --load hello.lisp




That should do it. If everything went smoothly with your installs, you should see a small window pop up with a single button in it that asks you to "Press Me". Click on the button and you should see the phrase "Hello World!" pop up in the terminal. If this worked for you, then congratulations, you've created your very first Lisp/Ltk-based GUI application. If not, feel free to leave me a comment and I'll try my best to walk you through any problems you might have had.




So, that's it, that's all there is to creating GUI based Lisp applications on OS X. Its my hope that this will get a few of you already interested in using Lisp to look into it even deeper, and any of you who are the fence about learning Lisp, hopefully this will give you one more reason for choosing Lisp as your next language to learn. (On yet another side note, if your interested in learning a bit more about Lisp, and you also feel the need to contribute to a new community of Lisp lovers and newbies, then take a look at volunteering your services to Peter Seibel's CL Gardeners group.)




Until next time, I wish you all success and fun with all of your programming endeavors.


4 Comments

nhabedi
2006-01-09 18:32:23
Two minor nits
1. For didactical purposes I'd suggest that you don't user (require 'ltk) because not every Lisp will interpret this as a request to load LTK via ASDF. Use (asdf:oos 'asdf:load-op :ltk) instead. And wrap this form with an eval-when if it's in the same file as the rest of the code.


2. Executing a program from the shell prompt via something like sbcl --load or openmcl -load might be OK for small "Hello World" programs but it's definitely not the way Lisp is normally used. (You work from the REPL instead.) Not to mention that you forgot to compile the code... :)

christopher_roach
2006-01-11 07:04:18
Two minor nits
Hey, thanks a bunch for the tips, especially the first one--I had no idea that not every Lisp implementation would interpret the call to require the same, I was just following the Ltk manual on that one.


Also, if any other Lisp guru's are reading these posts, please feel free to point out tips and tricks (or any troubles with any of my examples) that you wish. It seems to me that Lisp is one of those languages that you can only teach yourself so much and the rest is either earned through personal experience or passed on from the experiences of other more seasoned Lisp users.


Thanks for the tips, and I hope you're enjoying the posts.


Christopher

Paul
2006-06-07 08:32:13
My apologies for a very simple question, but where are you saving the hello.lisp file to get it to load as you suggest from the command line?
Christopher Roach
2006-06-11 10:53:24
Paul,


Sorry it's taken me a bit to get back to your query. Nevertheless, in answer to your question, the directory in which the hello.lisp file resides is the same directory from which you are running lisp and trying to load the file. Otherwise, you'll have to supply the path to your source code file.


For example, let's assume that I am using OpenMCL as my lisp implementation. On my laptop, I keep a directory of programs in my Documents folder that is further segregated into specific languages and then types of programs. So, I have a copy of the hello.lisp file in the following location:


~/Documents/programs/lisp/ltk/hello.lisp


Thus, if I am in the ltk folder and I wish to run the program, I would type the following into the command line:


$ openmcl -l hello.lisp


However, if I am running the program from my home directory, I would need to supply the following command to the Terminal:


$ openmcl -l Documents/programs/lisp/ltk/hello.lisp


I hope that clears everything up for you. Good luck and let me know if you have any further questions.


Christopher