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:
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:
(let ((b (make-instance 'ltk:button
:text "Press Me"
:command (lambda ()
(format t "Hello World!~&")))))
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.
Two minor nits
1. For didactical purposes I'd suggest that you don't user
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
|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?|