Fixing Slime’s “connection broken by the peer” with Lispbuilder-sdl

Something more than a mental note.
While programming with Common Lisp using the Lispbuilder-sdl library probably you want to evaluate your programs within Emacs and Slime (as usual.)
By the way, even with the simplest SDL program on slime-eval-region (or whatever else) Slime terminate in an expected way with this message:

[…]Connection broken by the peer[…]

Googling hard, I’ve found a solution that works like a charm!
First of all, open your swank.lisp file (you can find it inside your Slime installation), then add this line:

(setf swank:*communication-style* :fd-handler)

Done! It was simple enough, wasn’t it?
Now you can evaluate your lisp game within Emacs!
Enjoy!

Enable AutoComplete.el for lisp-mode

I don’t know exactly why, but AutoComplete.el for Emacs doesn’t work by default for standard lisp-mode, so there is my tweak:

1) Copied the scheme-mode from .emacs.d/ac-dict to the same dir but renaming it lisp-mode
2) I’ve put the following into my .emacs:

(add-to-list 'load-path "~/.emacs.d")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(add-to-list 'ac-modes 'lisp-mode)
(ac-config-default)

The magic is provided by the line

(add-to-list 'ac-modes 'lisp-mode)

Hope this help 🙂

Setting up your environment to work with Clojure

Hi everybody,
in the last tutorial I’ve talked about Leiningen. Now the time has come to go deeper, learning how to work with Clojure with ease.

The mindless solution: Eclipse + counterclockwise

Eclipse is a general purpose IDE focused on JAVA, but you can easily customize it working with Clojure: read the tutorial here and you should be able to download and install the counterclockwise plugin. What you obtain will be an environment with syntax-highlight, REPL and the possibility to create and manage Clojure project like the Java ones.

Pros:
  • Easy to install/manage/update
Cons:
  • Even if you want to eval a single statement you have to start Eclipse

Emacs + SLIME + Swank

For someone Emacs is an extremely unfriendly editor, full of macros and shortcuts, for others is THE editor. I think that Emacs is a fantastic editor if you know how to work with it. In this part of the tutorial I’ll show you how to set up a working environment under Emacs where you can even eval Clojure code directly. If you are wondering about what is SLIME, you can simple image SLIME like your REPL. Swank is “a server that allows SLIME (the Superior Lisp Interaction Mode for Emacs) to connect to Clojure projects. To use it you must launch a swank server, then connect to it from within Emacs”. In a nutshell through Swank we can start a REPL within Emacs, eval our code snippets and much more. Before getting our hands dirty, we need to install ELPA, the Emacs Lisp Package Archive. Through ELPA we can install all the stuff needed with ease.

Let’s go!

The first thing to do is open Emacs and select the *scratch* buffer from Buffer menu (Buffer -> *scratch*) and paste the following code:

(let ((buffer (url-retrieve-synchronously
	       "http://tromey.com/elpa/package-install.el")))
  (save-excursion
    (set-buffer buffer)
    (goto-char (point-min))
    (re-search-forward "^$" nil 'move)
    (eval-region (point) (point-max))
    (kill-buffer (current-buffer))))

Now bring the cursor just after the last parenthesis and press C-j (control + j) or manually select the command with Lisp-Interaction -> Evaluate and Print and wait for the operations to complete. Now close and reopen your Emacs and open a minibuffer with Alt+x or ESC+x. You will see the lower part of the Emacs window labeled with M-x.
Type


package-list-packages

inside the box and press ENTER. The window will split up in two frames and you’ll see a list of packages. Navigate through each row, bring the cursor over each of the following packages and (one by one) press “i”. You will see a “I” (a.k.a Installing) beside each package name:


clojure-mode
slime
slime-repl
swank-clojure

Now press x (a.k.a eXecute) to install the packages. If something fails, retry opening the minibuffer and tying package-list-packages again. The packages installed will be displayed red. You need at least the first three. Now you have syntax-highlight and your REPL, but you can’t use it yet.

Putting all together

The last thing to do is creating a Leiningen project (do you remember my last tutorial?) specifying like a development dependency swank clojure. So, remembering some Leiningen commands we create a simple general purpose Clojure project to play with:


lein new clj-on-fly

Now open the project.clj file and make it looks like this:


(defproject clj-on-fly "1.0.0-SNAPSHOT"
    :description "FIXME: write"
    :dependencies [[org.clojure/clojure "1.2.0"]
                   [org.clojure/clojure-contrib "1.2.0"]]
    :dev-dependencies [[swank-clojure "1.2.1"]])

Now within the project directory execute

lein deps

in order to download swank clojure into the project lib directory. Now you are ready to start a swank server, and you can do it within emacs pressing ESC+x and typing “shell” inside the minibuffer.
To try it out, you can open the “core.clj” file inside your project with Emacs, now create a new shell and type


lein swank

You should see and output like this:


user=> Connection opened on local port  4005#<ServerSocket ServerSocket[addr=localhost/127.0.0.1,port=0,localport=4005]>

Now return to your core.clj buffer, open a minibuffer and type:


slime-connect

press always “y” to the questions and finally you should see your interactive REPL, which you can query and play with, as well as evaluating all the code or even a snippet. Enjoy!
I hope you have liked this article.

Bye!

Alfredo Di Napoli