-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathswank-clojure.el
368 lines (325 loc) · 14.5 KB
/
swank-clojure.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
;;; swank-clojure.el --- slime adapter for clojure
;;
;; Copyright (C) 2008-2010 Jeffrey Chu and Phil Hagelberg
;;
;; Authors: Jeffrey Chu <jochu0@gmail.com>
;; Phil Hagelberg <technomancy@gmail.com>
;;
;; URL: http://github.com/technomancy/swank-clojure
;; Version: 1.1.0
;; Keywords: languages, lisp
;; Package-Requires: ((slime-repl "20091016") (clojure-mode "1.6"))
;;
;; This file is licensed under the terms of the GNU General Public
;; License as distributed with Emacs (press C-h C-c to view it).
;;
;;; Commentary:
;;
;; NOTE: swank-clojure.el is currently unmaintained. Please see the
;; swank-clojure readme for examples of how to start a swank server
;; from your build tool and connect to it via SLIME.
;;
;; The purpose of this file is to set up `slime-lisp-implementations'
;; to allow SLIME to communicate with the Swank server implemented in
;; Clojure. There are four ways to launch a session:
;;
;; 1. Standalone: If you just hit M-x slime, swank-clojure will
;; download the jars for Clojure, contrib, and swank-clojure,
;; launch an instance, and connect to it. If you just want to try
;; out Clojure, this is all you need. Just get Swank Clojure
;; through package.el (http://tromey.com/elpa) and stop reading here.
;;
;; 2. Custom classpath: If you want to hack on Clojure or Contrib, set
;; swank-clojure-classpath to a list of paths to the jars you want to
;; use and then hit M-x slime.
;;
;; 3. Project: Put your project's dependencies (either manually or using
;; Leiningen or Maven) in the directory named by
;; `swank-clojure-project-dep-path' (lib/ by default), then launch M-x
;; swank-clojure-project. Note that the directory must contain
;; swank-clojure.jar, it will not automatically be added to the
;; classpath as it was in past versions that had to run from a checkout.
;;
;; 4. Standalone Server: Users of leiningen or clojure-maven-plugin
;; can launch a server from a shell
;; (http://wiki.github.com/technomancy/leiningen/emacs-integration)
;; and connect to it from within Emacs using M-x slime-connect.
;;
;;; Code:
;;
(require 'slime)
(require 'clojure-mode)
(defgroup swank-clojure nil
"SLIME/swank support for clojure"
:prefix "swank-clojure-"
:group 'applications)
(defcustom swank-clojure-java-path "java"
"The location of the java executable"
:type 'string
:group 'swank-clojure)
(defcustom swank-clojure-jar-home "~/.swank-clojure/"
"The directory where the jars necessary to run swank-clojure are kept."
:type 'string
:group 'swank-clojure)
(defun swank-clojure-default-classpath ()
(append
(when (and (file-directory-p "~/.clojure")
(directory-files "~/.clojure" nil "swank-clojure.*jar$"))
(directory-files "~/.clojure" t ".jar$"))
(when (file-directory-p swank-clojure-jar-home)
(directory-files swank-clojure-jar-home t ".jar$"))))
(defcustom swank-clojure-classpath
(swank-clojure-default-classpath)
"The classpath from which clojure will load from (passed into
java as the -cp argument). On default, it includes all jar files
within ~/.clojure/ and ~/.swank-clojure"
:type 'list
:group 'swank-clojure)
;; For backwards-compatibility:
(defvaralias 'swank-clojure-extra-classpaths 'swank-clojure-classpath)
(defcustom swank-clojure-library-paths nil
"The library paths used when loading shared libraries,
used to set the java.library.path property"
:type 'list
:group 'swank-clojure)
(defcustom swank-clojure-extra-vm-args nil
"Extra arguments to be passed to the Java VM when starting clojure.
For example -Xmx512m or -Dsun.java2d.noddraw=true"
:type 'list
:group 'swank-clojure)
(defcustom swank-clojure-binary nil
"Used as a binary executable (instead of swank-clojure-java-path) if non-nil."
:type 'string
:group 'swank-clojure)
(defcustom swank-clojure-init-files nil
"If provided, will be used to initialize the REPL environment."
:type 'list
:group 'swank-clojure)
(defcustom swank-clojure-compile-p nil
"Whether to instruct swank-clojure to compile files. Set to nil
if it's causing you problems."
:type 'boolean
:group 'swank-clojure)
(defcustom swank-clojure-project-dep-path "lib"
"The directory (relative to the project root) to look for dependencies in
when using `swank-clojure-project'."
:type 'string
:group 'swank-clojure)
(defcustom swank-clojure-deps
(list (concat "http://repo.technomancy.us/"
"swank-clojure-1.1.0.jar")
(concat "http://build.clojure.org/snapshots/org/"
"clojure/clojure/1.1.0-master-SNAPSHOT/"
"clojure-1.1.0-master-20091202.150145-1.jar")
(concat "http://build.clojure.org/snapshots/org/"
"clojure/clojure-contrib/1.1.0-master-SNAPSHOT/"
"clojure-contrib-1.1.0-master-20091212.205045-1.jar"))
"A list of urls of jars required to run swank-clojure. If they
don't exist in `swank-clojure-jar-home' and
`swank-clojure-classpath' is not set, the user will be prompted
to download them when invoking `slime'.
Due to a bug in url-retrieve-synchronously, they must be
downloaded in order of size (ascending), so if you customize
this, keep that in mind."
:type 'list
:group 'swank-clojure)
(defface swank-clojure-dim-trace-face
'((((class color) (background dark))
(:foreground "grey50"))
(((class color) (background light))
(:foreground "grey55")))
"Face used to dim parentheses."
:group 'slime-ui)
;;;###autoload
(defun swank-clojure-init (file encoding)
(concat
(when swank-clojure-compile-p
"(require 'swank.loader)\n\n(swank.loader/init)\n\n")
"(require 'swank.swank)\n\n"
(when (boundp 'slime-protocol-version)
(format "(swank.swank/ignore-protocol-version %S)\n\n"
slime-protocol-version))
;; Hacked in call to get the localhost address to work around a bug
;; where the REPL doesn't pop up until the user presses Enter.
"(do (.. java.net.InetAddress getLocalHost getHostAddress) nil)"
(format "(swank.swank/start-server %S :encoding %S)\n\n"
(expand-file-name file)
(format "%s" (slime-coding-system-cl-name encoding)))))
(defun swank-clojure-find-package ()
(let ((regexp "^(\\(clojure.core/\\)?\\(in-\\)?ns\\+?[ \t\n\r]+\\(#\\^{[^}]+}[ \t\n\r]+\\)?[:']?\\([^()\" \t\n]+\\>\\)"))
(save-excursion
(when (or (re-search-backward regexp nil t)
(re-search-forward regexp nil t))
(match-string-no-properties 4)))))
;;;###autoload
(defun swank-clojure-slime-mode-hook ()
(slime-mode 1)
(set (make-local-variable 'slime-find-buffer-package-function)
'swank-clojure-find-package))
(defun swank-clojure-update-indentation (sym indent)
(put sym 'clojure-indent-function indent))
(defun swank-clojure-concat-paths (paths)
"Concatenate given list of `paths' using `path-separator'. (`expand-file-name'
will be used over paths too.)"
(mapconcat 'identity (mapcar 'expand-file-name paths) path-separator))
(defun swank-clojure-parse-jar-name (url)
(car (last (split-string url "/"))))
(defun swank-clojure-download-jar (url)
(let ((jar-name (swank-clojure-parse-jar-name url)))
(message "Downloading %s..." jar-name)
(let ((download-buffer (url-retrieve-synchronously url)))
(save-excursion
(condition-case e
(progn
(set-buffer download-buffer)
(re-search-forward "HTTP/[0-9]\.[0-9] 200 OK")
(re-search-forward "^$" nil 'move)
(delete-region (point-min) (+ 1 (point)))
(write-file (concat swank-clojure-jar-home "/" jar-name))
(kill-buffer nil))
(error
;; no recursive directory deletion on emacs 22 =(
(dolist (j (directory-files swank-clojure-jar-home t "[^.]+$"))
(delete-file j))
(delete-directory swank-clojure-jar-home)
(error "Failed to download Clojure jars.")))))))
(defun swank-clojure-dep-exists-p (jar-url)
"True if the jar file in `jar-url' exists in `swank-clojure-jar-home'."
(file-exists-p (expand-file-name (swank-clojure-parse-jar-name jar-url)
swank-clojure-jar-home)))
(defun swank-clojure-check-install ()
"Prompt to install Clojure if it's not already present."
(when (and (not swank-clojure-classpath)
(or (not (file-exists-p swank-clojure-jar-home))
(> (count-if-not 'swank-clojure-dep-exists-p swank-clojure-deps)
0))
(y-or-n-p "It looks like Clojure is not installed. Install now? "))
(make-directory swank-clojure-jar-home t)
(dolist (j swank-clojure-deps)
(swank-clojure-download-jar j))
(setq swank-clojure-classpath (swank-clojure-default-classpath))))
;;;###autoload
(defun swank-clojure-cmd ()
"Create the command to start clojure according to current settings."
(swank-clojure-check-install)
(if swank-clojure-binary
(if (listp swank-clojure-binary)
swank-clojure-binary
(list swank-clojure-binary))
(delete-if
'null
(append
(list swank-clojure-java-path)
swank-clojure-extra-vm-args
(list
(when swank-clojure-library-paths
(concat "-Djava.library.path="
(swank-clojure-concat-paths swank-clojure-library-paths)))
"-classpath"
(swank-clojure-concat-paths swank-clojure-classpath)
"clojure.main")
(let ((init-opts '()))
;; TODO: cleanup
(dolist (init-file swank-clojure-init-files init-opts)
(setq init-opts (append init-opts (list "-i" init-file))))
init-opts)
(list "--repl")))))
(defun swank-clojure-reset-implementation ()
"Redefines the clojure entry in `slime-lisp-implementations'."
(aput 'slime-lisp-implementations 'clojure
(list (swank-clojure-cmd) :init 'swank-clojure-init)))
;;;###autoload
(defadvice slime-read-interactive-args (before add-clojure)
;; Unfortunately we need to construct our Clojure-launching command
;; at slime-launch time to reflect changes in the classpath. Slime
;; has no mechanism to support this, so we must resort to advice.
(require 'assoc)
(swank-clojure-reset-implementation))
;; Change the repl to be more clojure friendly
(defun swank-clojure-slime-repl-modify-syntax ()
(when (string-match "\\*slime-repl clojure\\*" (buffer-name))
;; modify syntax
(set-syntax-table clojure-mode-syntax-table)
;; set indentation function (already local)
(setq lisp-indent-function 'clojure-indent-function)
;; set paredit keys
(when (and (featurep 'paredit) paredit-mode (>= paredit-version 21))
(define-key slime-repl-mode-map "{" 'paredit-open-curly)
(define-key slime-repl-mode-map "}" 'paredit-close-curly))))
;; Debugger
(defun swank-clojure-dim-font-lock ()
"Dim irrelevant lines in Clojure debugger buffers."
(if (string-match "clojure" (buffer-name))
(font-lock-add-keywords
nil `((,(concat " [0-9]+: " (regexp-opt '("clojure.core"
"clojure.lang"
"swank." "java."))
;; TODO: regexes ending in .* are ignored by
;; font-lock; what gives?
"[a-zA-Z0-9\\._$]*")
. font-lock-comment-face)) t)))
(add-hook 'sldb-mode-hook 'swank-clojure-dim-font-lock)
(defvar swank-clojure-project-hook nil
"A hook to run when a new SLIME session starts via `swank-clojure-project'.
The `path' variable is bound to the project root when these functions run.")
(defun swank-clojure-javadoc (classname)
"Show the javadoc for classname using clojure.contrib.repl-utils/javadoc"
(interactive (list (read-from-minibuffer "Javadoc for: " (slime-sexp-at-point))))
(slime-eval
`(swank:eval-and-grab-output
,(concat "(try
(require 'clojure.contrib.repl-utils)
(@(ns-resolve 'clojure.contrib.repl-utils 'javadoc) " classname ")
(catch Throwable t (.getMessage t)))"))))
(defun directoryp (path)
"Return t is path is a directory or a symlink pointing to a directory."
(let ((first-attr (car (file-attributes path))))
(if (stringp first-attr)
(directoryp first-attr)
first-attr)))
;;;###autoload
(defun swank-clojure-project (path)
"Setup classpath for a clojure project and starts a new SLIME session.
Kills existing SLIME session, if any."
(interactive (list
(read-directory-name
"Project root: "
(if (functionp 'locate-dominating-file) ; Emacs 23 only
(locate-dominating-file default-directory "src")
default-directory))))
;; TODO: allow multiple SLIME sessions per Emacs instance
(when (get-buffer "*inferior-lisp*") (kill-buffer "*inferior-lisp*"))
(let ((slime-lisp-implementations (copy-list slime-lisp-implementations))
(swank-clojure-extra-vm-args (copy-list swank-clojure-extra-vm-args))
(swank-clojure-binary nil)
(swank-clojure-classpath (let ((l (expand-file-name
swank-clojure-project-dep-path path)))
(if (file-directory-p l)
(append
(directory-files l t ".jar$")
(remove-if-not
'directoryp
(directory-files l t "^[^\\.]")))))))
(add-to-list 'swank-clojure-classpath (expand-file-name "classes/" path))
(add-to-list 'swank-clojure-classpath (expand-file-name "src/" path))
(add-to-list 'swank-clojure-classpath (expand-file-name "test/" path))
(add-to-list 'swank-clojure-classpath (expand-file-name "resources/" path))
;; For Maven style project layouts
(when (file-exists-p (expand-file-name "pom.xml" path))
(dolist (d '("src/main/clojure/" "src/test/clojure/"
"target/test-classes/" "target/classes/" "target/dependency/"))
(add-to-list 'swank-clojure-classpath (expand-file-name d path) t))
(dolist (d (let ((l (expand-file-name "target/dependency/" path)))
(if (file-directory-p l)
(directory-files l t ".jar$"))))
(add-to-list 'swank-clojure-classpath (expand-file-name d path) t))
(add-to-list 'swank-clojure-extra-vm-args
(format "-Dclojure.compile.path=%s"
(expand-file-name "target/classes/" path))))
(swank-clojure-reset-implementation)
(run-hooks 'swank-clojure-project-hook)
(save-window-excursion
(let ((default-directory path))
(slime 'clojure)))))
(provide 'swank-clojure)
;;; swank-clojure.el ends here