Merge branch 'master' of ssh://common-lisp.net/project/parenscript/public_html/git...
[parenscript.git] / extras / js-expander.el
blob7c5d50fd9b2b6bbe8cc7c5ceb2436621ce4a90f0
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;;; This is an extension to SLIME that is inspired by (and works
3 ;;;; like) the SLIME 'C-c M-m' macroexpansion feature.
5 ;;;; After loading, 'C-c j' (PS) or 'C-c d' (PS-DOC) at a ParenScript
6 ;;;; expression in a slime-mode buffer will bring up a buffer with the
7 ;;;; resulting Javascript code. Note that the extension does not work
8 ;;;; in slime-repl-mode, which is intentional.
10 ;;;; Copyright 2007, Vladimir Sedach. See the COPYING file in the
11 ;;;; Parenscript distribution for licensing information.
13 ;;; The code below is a generic facility for adding "macroexpand-like" buffer expansion to Slime
14 (defun slime-eval-custom-expand (expander exp-str package buffer-name buffer-mode printer)
15 (lexical-let ((package package)
16 (buffer-name buffer-name)
17 (buffer-mode buffer-mode)
18 (printer printer))
19 (slime-eval-async
20 (list 'swank:eval-and-grab-output (format "(%s %s)" expander exp-str))
21 (lambda (expansion)
22 (slime-with-popup-buffer (buffer-name)
23 (funcall buffer-mode)
24 (setq buffer-read-only nil)
25 (erase-buffer)
26 (insert (funcall printer (second expansion)))
27 (setq buffer-read-only t)
28 (font-lock-fontify-buffer)))
29 package)))
31 (defun* slime-add-custom-expander (key expander buffer-name &optional (buffer-mode 'slime-mode) (printer #'identity))
32 (define-key slime-parent-map (concat "\C-c" key)
33 (lexical-let ((expander expander)
34 (buffer-name buffer-name)
35 (buffer-mode buffer-mode)
36 (printer printer))
37 (lambda (&rest _)
38 (interactive "P")
39 (slime-eval-custom-expand expander
40 (first (slime-sexp-at-point-for-macroexpansion))
41 (slime-current-package)
42 buffer-name
43 buffer-mode
44 printer)))))
46 ;;; This actually defines the expander. If the code above belongs in slime.el, the code below would go into .emacs
47 (map nil (lambda (x)
48 (slime-add-custom-expander (car x)
49 (cdr x)
50 "*Parenscript generated Javascript*"
51 (if (featurep 'javascript-mode) 'javascript-mode 'c-mode)
52 #'read))
53 '(("j" . ps:ps) ("d" . ps:ps-doc)))