Setting up SLIME & Inferior-Lisp for Clojure in Emacs - emacs

SLIME
I'm pretty new to both Clojure & emacs and I've been trying to set up SLIME for Clojure. The official documentation implicitly assumes you know what your doing with emacs. There isn't just a bunch of code you can stick into your configuration files. Since I am interested in Clojure for Data Analysis, I don't really want to deal with Leiningen if at all possible, but I want the dynamic environment that slime provides.
I have installed Clojure from git in /opt/clojure/ and clojure-contrib in /opt/clojure-contrib and I can get a repl. I installed swank-clojure, clojure-mode, and slime from github in `~/.bin following this tutorial. I changed a few things around when this wasn't working by adding some stuff from the comments section of the official documentation.
When I start slime with M-x slime I get a continuous Polling "/tmp/slime.14113".. (Abort with 'M-x slime-abort-connection'.).
Here is my init-clj.el:
;; clojure-mode
(add-to-list 'load-path "~/.bin/clojure-mode")
;; swank-clojure
(add-to-list 'load-path "~/.bin/swank-clojure")
(setq swank-clojure-jar-path "/opt/clojure/clojure.jar"
swank-clojure-extra-classpaths (list
"~/.bin/swank-clojure/src/swank"
"/opt/clojure/clojure-contrib/target/clojure-contrib-1.2.0-SNAPSHOT.jar"))
(require 'swank-clojure)
;; slime
(eval-after-load "slime"
'(progn (slime-setup '(slime-repl))))
(add-to-list 'load-path "~/.bin/slime")
(require 'slime)
(eval-after-load 'slime '(setq slime-protocol-version 'ignore))
(slime-setup '(slime-repl))
(require 'clojure-mode)
(require 'clojure-test-mode)
Here is the error I get when I call it when ants.clj is open:
(progn (load "/home/kca/.bin/slime/swank-loader.lisp" :verbose t) (funcall (read- from-string "swank-loader:init")) (funcall (read-from-string "swank:start-server") "/tmp/slime.14113" :coding-system "iso-latin-1-unix"))
Clojure 1.2.0-master-SNAPSHOT
user=> java.lang.Exception: Unable to resolve symbol: progn in this context (NO_SOURCE_FILE:1)
Inferior Lisp
I made a script in .bin/ called clj-repl that holds the java command to start a repl. I then M-x set-variable inferior-lisp-program /home/wdkrnls/.bin/clj-repl. Emacs complains its the wrong type.

The best way to use Clojure is to start by installing Leiningen.
Then install Swank Clojure as a Leiningen plugin.
Next, I'd recommend stripping your current custom Clojure setup from .emacs, and installing ELPA, and then setting up the following initialization code in your .emacs file:
;; Find this line, added by ELPA:
(require 'package)
;; and add the following expression:
(add-to-list 'package-archives
'("marmalade" .
"http://marmalade-repo.org/packages/") t)
;; ... and the rest of the ELPA init code
(package-initialize)
Then, run package-list-packages and install clojure-mode and slime (and paredit for good measure), and anything else you might want.
This should have you all set up and ready to use SLIME in (Leiningen) Clojure projects. And despite the seemingly complex procedure here, you can create a single "uberjar" from your projects to deploy on other servers with absolutely no dependency hassle.

Try the method detailed here. It takes a couple of minutes to set everything up from scratch on a clean unix or mac box:
http://www.learningclojure.com/2010/08/clojure-emacs-swank-slime-maven-maven.html

Related

How to stop a package from initializing with package-initialize on emacs (slime)

I've been trying to get slime+sbcl working on my emacs (26.3) for a while. I first installed slime via melpa and that didn't work. I finally got slime able to work on a clean emacs (emacs -q) using quicklisp and the following code:
(load "~/quicklisp/slime-helper.el")
(setq inferior-lisp-program "sbcl")
However, when I put it into my actual init file and run it, it doesn't work. I figured that if I put package-enable-at-startup to nil and commented out package-initialize, that slime works. My guess is that the installed slime through melpa is "overriding" the slime initialization using slime-helper. I can't uninstall slime via melpa because of package dependencies and am worried I might mess something up. But I also need all of my packages to initialize except slime. So I was wondering if there was anyway to initialize all my packages, but suppress the slime package.
I think the easiest method would be to disable the loading of "elpa slime" with package-load-list. See its documentation with C-hv package-load-list Return. In short you'd put something like this in your init file.
(require 'package)
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/")
'("gnu" . "https://elpa.gnu.org/packages/"))
(setq package-load-list '((slime nil))) ;; don't load slime
(package-initialize)
(load (expand-file-name "~/quicklisp/slime-helper.el"))
(setq inferior-lisp-program "sbcl")
package-initialize will skip loading slime (and manipulating your load-path), allowing the "quicklisp slime" to appear first on your load-path. This may or may not break dependencies loaded by the package system. If they do break, I'd see if quicklisp can manage them and deal with them that way, or I would manually manage them.

How to connect Hyperspec documentation to Emacs SLIME on MS Windows

With this minimal init file:
(setq package-load-list '((slime t)))
(setq inferior-lisp-program "clisp")
(package-initialize)
(setq package-enable-at-startup nil)
(require 'slime)
(slime-setup)
(slime)
(find-file "~/t/del.lisp")
Everything seems to work, such as slime-eval-defun and slime-complete-symbol, except for looking up documentation. M-x slime-describe-symbol RET print RET results in this error:
CLHS-ROOT: variable *CLHS-ROOT-DEFAULT* has no value
What do I need to add in my init file to make it work?
I also tried downloading the hyperspec tar file and extracting it to a directory, and this code:
(setq package-load-list '((slime t)))
(setq inferior-lisp-program "clisp"
common-lisp-hyperspec-root "c:/run/HyperSpec/"
common-lisp-hyperspec-symbol-table "c:/run/HyperSpec/Data/Map_Sym.txt")
(package-initialize)
(setq package-enable-at-startup nil)
(require 'slime)
(slime-setup)
(slime)
(find-file "~/t/del.lisp")
That doesn't work either. I do not know if the bug is in that init file, or in the SLIME version I am using, because this is my first time with SLIME.
Versions:
MS Windows 7
Emacs version 24.3.1 (probably latest stable)
SLIME version 20130626.1151 (latest from MELPA) (One from Marmalade says it can't compile nil, I don't know what that means and so I am using one from MELPA instead)
GNU CLISP 2.49 (latest stable)
UPDATE
C-c C-d f RET print RET works fine. This is bound to slime-describe-function, which is undocumented, and not listed in SLIME menu. There is also slime-documentation-lookup which is bound to C-c C-d C-d which can open documentation for variables (not just functions) in a browser, and that works too. Looks like only `slime-describe-symbol doesn't work.
I haven't done it on Windows, but if I were you, I'd try to do this with Quicklisp: (ql:quickload "clhs") and follow the printed directions.
I'd also get SLIME from Quicklisp via (ql:quickload "quicklisp-slime-helper"), but if your slime works ok, no real need.
Assuming that SLIME is installed from an emacs package archive (preferably MELPA) (and that GNU CLISP is installed), here is combination of relevant portions from How to install Common Lisp and SLIME on MS Windows:
Assuming starting from scratch after commenting out any SLIME customization code you already have, start by putting the following code to your init file which should be evaluated after package-initialize:
(setq inferior-lisp-program "clisp")
(setq slime-auto-connect 'ask)
(defun my-slime-setup ()
(require 'slime)
(slime-setup))
(defvar my--slime-setup-done nil)
(defun my-slime-setup-once ()
(unless my--slime-setup-done
(my-slime-setup)
(setq my--slime-setup-done t)))
(defadvice lisp-mode (before my-slime-setup-once activate)
(my-slime-setup-once))
What that does is defining my-slime-setup and make sure the function runs just once if you are using SLIME that day. my-slime-setup is also a container to which you can add your own SLIME customization code.
Now to connect the downloaded documentation to SLIME, extract the downloaded archive and you will get a folder with name Hyperspec, and then you move that folder to the Emacs bin directory, or its parent directory, or its grandparent directory, Put the following code in Emacs init file.
(defun my-hyperspec-setup ()
(let ((dir (locate-dominating-file invocation-directory "HyperSpec/")))
(if dir
(progn
(setq common-lisp-hyperspec-root (expand-file-name "HyperSpec/" dir)))
(warn "No HyperSpec directory found"))))
and add my-hyperspec-setup to my-slime-setup like this:
(defun my-slime-setup ()
(my-hyperspec-setup)
(require 'slime)
(slime-setup))
and restart Emacs.
And now when you do M-x slime-describe-symbol RET print RET in a lisp buffer, it should show the description of PRINT in another buffer.
I should confess that I am sourcing from my own article and also answering my own question after about 8 months. The answer is tested with latest SLIME from MELPA and on a vanilla GNU Emacs.

emacs setup for both clojure and common lisp with slime-fancy (slime-autodoc)

I set up emacs for both clojure and common lisp, but I want also (slime-setup '(slime-fancy)) for common lisp. If I add that line to init.el, clojure won't work: it gives me repl, but it hangs after I run any code.
My configuration
For clojure:
I set up clojure-mode, slime, slime-repl via ELPA
I run $ lein swank in project directory
Then M-x slime-connect to hack clojure
For common lisp I place this after ELPA code in init.el:
(add-to-list 'load-path "~/.elisp/slime")
(require 'slime)
(add-to-list 'slime-lisp-implementations '(sbcl ("/opt/local/bin/sbcl") :coding-system utf-8-unix))
;; (slime-setup '(slime-fancy))
So if I uncomment the last line, clojure will be broken. But slime-fancy a very important meta package for hacking common lisp.
Is there a way to set them both up to work without changing configuration and restarting when I need to switch languages?
Update
I found that slime-autodoc loaded with slime-fancy is the cause of hangs.
(slime-setup '(slime-fancy))
(setq slime-use-autodoc-mode nil)
This configuration lets run both common lisp and clojure SLIMEs. Even simultaneously. But without slime-autodoc.
I also found I'm using the CVS version of SLIME since I manually do (add-to-list 'load-path "~/.elisp/slime") after ELPA code. That does not solve the problem. Maybe there is a version from some magic date which works with clojure? Here a guy says CVS version works for him: http://www.youtube.com/watch?v=lf_xI3fZdIg&feature=player_detailpage#t=221s
Here is a solution. (using hooks)
That is ugly but quite convenient.
(add-hook 'slime-connected-hook
(lambda ()
(if (string= (slime-lisp-implementation-type) "Clojure")
(setq slime-use-autodoc-mode nil)
(setq slime-use-autodoc-mode t))
))
(add-hook 'slime-mode-hook
(lambda ()
(if (eq major-mode 'clojure-mode)
(slime-autodoc-mode 0)
(slime-autodoc-mode 1))))
Update
If the problem still exists on the slime-repl buffer, try the following code:
(add-hook 'slime-repl-mode-hook
(lambda ()
(if (string= (slime-lisp-implementation-type) "Clojure")
(progn (setq slime-use-autodoc-mode nil)
(slime-autodoc-mode 0))
(progn (setq slime-use-autodoc-mode t)
(slime-autodoc-mode 1)))))
I've been contemplating on the same problem recently. The issue is that the SLIME in ELPA is trimmed down and is next to useless for Common Lisp. One way you can circumvent the problem is to check out SLIME from CVS from the same date as the checkout was done for the ELPA package and add manually the missing stuff. Someone on #clojure told me he did that and the solution worked.
I personally find such a solution pretty ugly, but until someone manages to get the Clojure support into upstream SLIME there won't be a better one.
Alternatively you can add features to the slime-setup one by one and see what feature exactly is causing the problem with the Clojure evaluation - after all slime-fancy is simply a metafeature that just loads the most popular contrib features.
Btw you don't need the lines
(add-to-list 'load-path "~/.elisp/slime/contrib")
(setq slime-backend "~/.elisp/slime/swank-loader.lisp")
(require 'slime)
The contrib dir will be added automatically to the load path, the back-end is the default and if you're using 'slime-autoloads you should require slime before that, since this defeats the purpose of the autoload.
I use sbcl, clozure, and clojure: Getting Emacs, Slime, Common Lisp (SBCL, Clozure), and Clojure to Work Together

Running Clojure and other Lisp at the same time on Emacs

I use Aquamacs, and Aquamacs is pre-equipped with SLIME.
(setq inferior-lisp-program "/usr/local/bin/sbcl") #####!!!
(add-to-list 'load-path "/Library/Application Support/Aquamacs Emacs/SLIME/contrib")
(add-to-list 'load-path "/Library/Application Support/Aquamacs Emacs/SLIME")
(require 'slime)
(slime-setup)
As is asked in somewhere, I try to use Clojure by adding this code.
(add-to-list 'load-path "~/clojure/clojure-mode")
(setq inferior-lisp-program "/Users/smcho/bin/clj") ################
(require 'clojure-mode)
(setq auto-mode-alist
(cons '("\\.clj$ . clojure-mode")
auto-mode-alist))
(add-hook 'clojure-mode-hook
'(lambda ()
(define-key clojure-mode-map "\C-c\C-e" 'lisp-eval-last-sexp)))
)
I couldn't make it Clojure run with SLIME, but I'm satisfied with the current setting, the only problem is that because of the (setq inferior-lisp-program ...) code, I have to change the .emacs code depending on I use Clojure or SBCL.
Is there any way to solve this problem? Can I choose between multiple (inferior) Lisps?
Added
I could make Clojure run on Aquamacs. Please refer to Running Clojure with 'lein swank' on Aquamacs problem. Forget about the settings written above, if you want to run Aquamacs/Clojure. You need just one line, (slime-setup '(slime-repl)) and lein swank.
Sure, you can use C-u M-x slime instead of just M-x slime to have SLIME ask you for the name of the Lisp executable to be launched, with whatever is your default already filled in.
There's also a slime-lisp-implementations variable which I have configured like so:
(setq slime-lisp-implementations
`((clojure ,(swank-clojure-cmd) :init swank-clojure-init)
(sbcl ("sbcl") :coding-system utf-8-unix)))
I have to say that I just can't remember what this does for me anymore (if indeed it does anything)... Type C-h v slime-lisp-implementations to learn roughly what it's supposed to do. I seem to have to type sbcl if I want to start that, which is fine by me due to the high Clojure-to-SBCL ratio in my SLIME'ing.
Update:
I have just rediscovered M-- M-x slime (that first key is meta-minus for a negative argument), which prompts for one of the names of Lisp implementations in slime-lisp-implementations (with tab completion) and then starts the required Lisp. With the above example config, M-- M-x slime sbcl starts SBCL.
(I find this useful mostly because of how it allows one to configure more complex commands to start Lisp -- e.g. (sbcl-options ("sbcl" "--an-option" "--another-option") ...), (sbcl-clbuild ("/path/to/clbuild" "lisp") ...) -- and refer to them by name.)

Emacs (Cocoa Emacs) vs Aquamacs for running Clojure on Mac OS X

I used Aquamacs so far, and I need to install and run Clojure using SLIME. I googled to get some way to use Clojure on SLIME of Aquamacs, but without success.
Questions
Is it possible to install Clojure on Aquamacs? Or, can you guess why Clojure on Aquamacs doesn't work?
Is it normal that Emacs and Aquamacs can't share the same ELPA?
Is it possible to use ELPA to install Conjure on Emacs/Aquamacs?
I was told that one can use 'lein swank' to run as a server, do you know how to do that?
Sequences that I tried (and half succeeded)
I tried with Mac OS X Emacs, and by following the steps I could make it work. I mean, I could run Clojure with SLIME.
Emacs for Mac OS X
Step 1) Install ESK.
Git clone and copy all the files into the .emacs.d directory
Add the following code to .emacs and relaunch
(when
(load
(expand-file-name "~/.emacs.d/package.el"))
(package-initialize))
Step2) Install using ELPA
M-x package-list-packages to select packages
Install
clojure-mode, clojure-test-mode
slime, slime-repl
swank-clojure
M-x slime to install the clojure
Add the following code to .emacs and relaunch
;; clojure mode
(add-to-list 'load-path "/Users/smcho/.emacs.d/elpa/clojure-mode-1.7.1")
(require 'clojure-mode-autoloads)
(add-to-list 'load-path "/Users/smcho/.emacs.d/elpa/clojure-test-mode-1.4")
(require 'clojure-test-mode-autoloads)
;; slime
;(setq inferior-lisp-program "/Users/smcho/bin/clojure")
(add-to-list 'load-path "/Users/smcho/.emacs.d/elpa/slime-20100404")
(require 'slime-autoloads)
(add-to-list 'load-path "/Users/smcho/.emacs.d/elpa/slime-repl-20100404")
(require 'slime-repl-autoloads)
;; swank-clojure
(add-to-list 'load-path "/Users/smcho/.emacs.d/elpa/swank-clojure-1.1.0")
(require 'slime-repl-autoloads)
Aquamacs
Now I could use Clojure on Emacs, I tried the same(or very similar) method to run Clojure on Aquamacs once more.
Step 1) Install ESK for Aquamacs
Copy the files to ~/Library/Preference/Aquamacs Emacs
Modify "~/Library/Preferences/Aquamacs Emacs/Preferences.el" to add the following
(setq kitfiles-dir (concat (file-name-directory
(or (buffer-file-name) load-file-name)) "/aquamacs-emacs-starter-kit"))
; set up our various directories to load
(add-to-list 'load-path kitfiles-dir)
(require 'init)
Step2)
* Follow the same step as before to install all the (same) packages, but "M-x slime" gives me the following error message. "Symbol's function definition is void: define-slime-contrib"
ELPA
I tried to combine the packages from Emacs and Aquamacs, but they don't combine. I thought I could use the ELPA itself, not from the ESK to make it shared.
The result was not good, as ELPA couldn't download the swank-conjure package.
Success - Running Aquamacs/Clojure with 'lein swank'.
Please refer to this.
Aquamacs most definitely works with Clojure, since the author of Clojure uses it. However, I use Emacs, and after you perform the steps above in the Emacs section, I recommend checking out labrepl,
http://github.com/relevance/labrepl
If you don't have leiningen, the link to get and install it is in the instructions of the labrepl readme file. I found it extremely helpful when first learning how to set up an environment for Clojure programming. You can take apart the project.clj file in labrepl and piece together how it works pretty easily. Not to mention the lessons and training in the built in web application that comes with labrepl.
If you want to use lein swank instead:
Make sure you have leiningen installed. In your project.clj dev dependencies you want to have an entry like this:
[leiningen/lein-swank "1.1.0"]
http://clojars.org/leiningen/lein-swank
Then after you've done lein deps you should be able to run lein swank and then from within Emacs run M-x slime-connect and just press enter through the defaults.
If you're going to go this route, here is the link directly to leiningen so you can skip the labrepl repository: http://github.com/technomancy/leiningen
I found this the easiest setup for the latest version of everything. Here's a quick summary where I"m assuming you have leiningen installed.
Install the swank-clojure plugin
$ lein plugin install swank-clojure 1.3.2
Create your clojure project
$ lein new test-project
$ cd test-project
$ lein deps
Then open one of the clojure files from your project in emacs and run clojure-jack-in
M-x clojure-jack-in
You are now in a slime buffer with clojure and the dependencies for your project loaded.
Paul Barry gives an lecture how to use Aquamacs and Clojure.
It's simple as you can add this code to the .emacs file,
(add-to-list 'load-path "~/clojure/clojure-mode")
(setq inferior-lisp-program "/Users/smcho/bin/clj")
(require 'clojure-mode)
(setq auto-mode-alist
(cons '("\\.clj\\'" . clojure-mode)
auto-mode-alist))
(add-hook 'clojure-mode-hook
(lambda ()
(define-key clojure-mode-map "\C-c\C-e" 'lisp-eval-last-sexp)))
I could run 'M-x clojure-mode', and C-c C-z for REPL.
And as is asked and answered in Running Clojure and other Lisp at the same time on Emacs, I could use both Clojure/Lisp on Aquamacs.