tags:

views:

169

answers:

1

As is asked and answered here. I could use 'lein swank' to run clojure on Aquamacs.

I need to automate running 'lein swank', before running slime/clojure.

  • Q : Is there a way to this automatically? I mean how can I run the command 'lein swank' automatically when slime/clojure (M-x slime-connect) is called.
  • Q : If I have to come up with elisp code to run 'lein swank', how can I do that?

Added

Based on Jürgen Hötzel's answer, I modified the elisp as follows.

(defun lein-swank ()
(interactive)
(let ((default-directory (locate-dominating-file default-directory "/Users/smcho/bin/leiningen")))
  (when (not default-directory)
    (error "Not in a Leiningen project."))
  ;; you can customize slime-port using .dir-locals.el
  (let ((proc (start-process "lein-swank" nil "/Users/smcho/bin/leiningen/bin/lein" "swank" (number-to-string 4005))))
    (when proc
    (process-put proc :output nil)
    (set-process-sentinel proc (lambda (proc event)
                     (message "%s%s: `%S'" 
                          (process-get proc :output)
                          proc (replace-regexp-in-string "\n" "" event))))
    (set-process-filter proc
                (lambda (proc output)
                  ;; record last line of output until connected (possible error message)
                  (process-put proc :output (concat (process-get proc :output) output))
                  (when (string-match "Connection opened on" output)
                (slime-connect "localhost" 4005)
                ;; no need to further process output
                (set-process-filter proc nil))))
    (message "Starting swank server...")))))

But, I got this error.

No project.clj found in this directory. lein-swank: `"exited abnormally with code 1"'.

What I found was that I should change pwd to ~/bin/leiningen to run 'lein swank'. Just put lein binary inside the PATH string doesn't make it run.

A: 

I made a gist for this job:

http://gist.github.com/419364

Just use the interactive command "M-x lein-swank", which will spawn the command in the current directory and connect to it.

I made several improvements to lein-swank:

  1. lein-swank-command is now customizable: You can use Leiningen, if its bin directory is not part of your PATH environment.

  2. Added directory as interactive argument: If project.clj can not be found in a dominating location of your current directory, you can specify a location.

Jürgen Hötzel
@Jurgen : Thanks for the answer, I modified and tried, but I got some error. Can you check that?
prosseek
Just made lein-swank more flexible (custom variable for lein-swank-command) and less error prone (interactive argument if project.clj is non-existent).
Jürgen Hötzel
@Jurgen : Now it asks the 'Leiningen Project directory', I give it '~/bin/leiningen'. But there's an error saying : 'Searching for program:No such file or directory, lein'. What's wrong with this? And, I already know the leiningen directory. So, how can I change the code not to ask the directory?
prosseek
It asks for the directory where your project.clj is located. And you should set lein-swank-command to "~/bin/lein" (if this is your Leiningen script) by using "M-x customize-variable"
Jürgen Hötzel
@Jurgen : ~/bin/leiningen has the project.clj. But I got an error. And for lein-swank-command, is it OK to set (setq lein-swank-command "~/bin/lein").
prosseek
Yes, its OK to use setq. It's just not the recommended way to set custom variables.
Jürgen Hötzel