I'm using swank-clojure from https://github.com/technomancy/swank-clojure.
I'd like to run clojure-jack-in automatically when a clojure file is opened in emacs. All my projects use lein, so it should always be appropriate.
Presumably you should check that it hasn't been called already. Something like this in ~/.emacs.d/init.el seems to work, but is there a downside or a better way to achieve the same effect?
(defun clojure-jack-in-once ()
"clojure-jack-in if it hasn't been run already,
as indicated by presence of *swank* buffer"
(if (eq nil (get-buffer "*swank*"))
(clojure-jack-in)))
(add-hook 'clojure-mode-hook 'clojure-jack-in-once)
You might want to extend that to check the liveness of the process, using process-status
and get-buffer-process
, but there is no fundamental reason you can't do that.
A slightly nicer bit of elisp would be:
(unless (get-buffer "*swank*")
(clojure-jack-in))
Adding the check:
(let ((proc (get-buffer-process "*swank*")))
(unless (and proc (eq (process-status proc) 'run))
(clojure-jack-in)))
That should check if the process is still running and automatically restart if required.