SLIMEでClojureを使う

Clojureのバージョンアップのせいか、どうも前に他のサイトで見た方法がうまくいかなくなってしまった。
試行錯誤してみた結果とりあえずちゃんと動くようになったので、色々とアヤシイところはあるもののまとめてみる。

SLIMEのインストール

Emacs Lispは~/elisp以下にインストールするものとする。まずはGitを利用して最新版をダウンロードする。87MBもあるのでけっこう時間かかる。SBCLは既に入っているものとする。

$ cd ~/elisp
$ git clone git://git.boinkor.net/slime.git

~/.emacsに以下のように書く

;; 処理系の指定
(setq inferior-lisp-program "sbcl")
;; SLIMEへのロードパスを通す
(add-to-list 'load-path "~/elisp/slime/")
(add-to-list 'load-path "~/elisp/slime/contrib/")
(require 'slime)
(slime-setup '(slime-repl)) ; slime-scratch slime-fancy slime-asdf
(add-hook 'slime-mode-hook
	  (lambda ()
	    ;; outline-minor-modeを有効化
	    (outline-minor-mode t)
	    ;; 各種キーバインド
	    (define-key slime-mode-map "\C-c\C-i" 'comment-region)
	    (define-key slime-mode-map "\C-c\C-o" 'uncomment-region)
	    (define-key slime-mode-map "\C-c\C-c" 'slime-hyperspec-lookup)
	    (define-key slime-mode-map "\M-n" 'scroll-up-one-line)
	    (define-key slime-mode-map "\M-p" 'scroll-down-one-line)
	    ))
(setq slime-net-coding-system 'utf-8-unix) ; 文字コードの指定
(setq slime-startup-animation nil)
(setq slime-truncate-lines nil)

再インストールの際などはコンパイラのバージョンが違っていたりするので、以前のコンパイル済みファイルは消しておいた方がいいかも。~/.slimeにあるので rm -r ~/.slime などとしておく。
Emacsを起動して M-x slime で関連ファイルのコンパイルが始まり、SLIMEのプロンプトが出てきたら成功。

Clojureのインストール

公式のダウンロードページから安定版のclojure-1.2.0とclojure-contrib-1.2.0をダウンロードしてくる。
clojure-contribの方はjarファイルが入っていないのでビルドする必要がある。ビルドのためのソフトがantからMavenっていうのに変わったらしい。/usr/ports/devel/maven2からMaven2を入れて、READMEを読みつつclojure-contribをビルド。
できたjarファイルは ~/.clojure というディレクトリを作ってそこに置いておく。

clojure-modeのインストール

$ cd ~/elisp
$ git clone git://github.com/jochu/clojure-mode.git 

~/.emacsに以下のように書く

(add-to-list 'load-path "~/elisp/clojure-mode")
(require 'clojure-mode)
(autoload 'clojure-mode "clojure-mode" "A major mode for Clojure" t)

Leiningenのインストール

id:t2ruさんの記事参照。基本的にはシェルスクリプトを落としてきて実行権限つけてパスの通ったところに置くだけ。

swank-clojureのインストール

$ cd ~/elisp
$ git clone git://github.com/jochu/clojure-mode.git

ソースをダウンロードしたはいいが、swank-clojureのビルドがよく分からないのでLeiningenでインストールしてjarファイルを~/.clojureにコピーする。

$ lein install swank-clojure 1.3.0-SNAPSHOT
$ cp ~/.m2/repository/swank-clojure/swank-clojure/1.3.0-SNAPSHOT/swank-clojure-1.3.0-SNAPSHOT.jar ~/.clojure/swank-clojure.jar

~/.emacsに以下のように書く

(add-to-list 'load-path "~/elisp/swank-clojure")
(require 'swank-clojure)
(require 'assoc)
;; /.clojure 内の各jarファイルにクラスパスを通す
(setq swank-clojure-jar-home "~/.clojure")
;; slime-lisp-implementationsにclojureの呼び出しコマンドを追加する
(swank-clojure-reset-implementation)

最後に各処理系別にコマンドを用意。

(add-to-list 'slime-lisp-implementations '(sbcl ("sbcl")))
(defun slime-clojure () (interactive) (slime 'clojure))
(defun slime-sbcl () (interactive) (slime 'sbcl))

M-x slime-clojureで起動を確認して終了。これでSBCLClojureの使い分けができるようになった。

追記

上のように設定してClojure+SLIMEの起動はうまくいったわけだが、デバッガから抜けるときに"error in process filter: Wrong number of arguments: nil,0"というエラーメッセージがミニバッファに毎回出るようになってしまった。しかもその度に数秒止まるのでなんとかしたい。https://github.com/technomancy/swank-clojure/issues/issue/31 によると、SLIMEがELPAからインストールしたものでないとこれが出るらしい。
git cloneで取ってきた最新版でやるときは、多少強引だが、SLIMEの展開先にあるslime.elの中のslime-dispatch-eventの定義を以下のように変更してしまう。

(defun slime-dispatch-event (event &optional process)
  (let ((slime-dispatching-connection (or process (slime-connection))))
    (or (run-hook-with-args-until-success 'slime-event-hooks event)
        (destructure-case event
          ((:emacs-rex form package thread continuation)
           (when (and (slime-use-sigint-for-interrupt) (slime-busy-p))
             (slime-display-oneliner "; pipelined request... %S" form))
           (let ((id (incf (slime-continuation-counter))))
             (slime-send `(:emacs-rex ,form ,package ,thread ,id))
             (push (cons id continuation) (slime-rex-continuations))
             (slime-recompute-modelines)))
          ((:return value id)
           (let ((rec (assq id (slime-rex-continuations))))
             (cond (rec (setf (slime-rex-continuations)
                              (remove rec (slime-rex-continuations)))
                        (slime-recompute-modelines)
-			(funcall (cdr rec) value))
+			(ignore-errors (funcall (cdr rec) value))) ; ここでエラーが起きているのでignore-errorsで黙らせる
                   (t
                    (error "Unexpected reply: %S %S" id value)))))
          ((:debug-activate thread level &optional select)
           (assert thread)
           (sldb-activate thread level select))

いいのかなぁ。これ・・・ しかしとりあえずはちゃんと動くようになったのよしとする。

追記2 クラスパスの指定

変数swank-clojure-classpathにクラスパスがリストとして入ってるので、追加したいパスをpushする。
例えば、プログラミングClojureのサンプルコードを利用したいなら、

$ cd ~/.clojure
$ git clone git://github.com/stuarthalloway/programming-clojure.git

ソースコードを取得して、.emacsに以下を追加する。

(push "~/.clojure/programming-clojure" swank-clojure-classpath)