-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-slime-js.el
92 lines (76 loc) · 3.03 KB
/
setup-slime-js.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
(require 'slime)
(require 'slime-js)
(require 'js2-refactor)
(set-default 'slime-js-target-url "http://localhost:3000")
(set-default 'slime-js-connect-url "http://localhost:8009")
(set-default 'slime-js-starting-url "/")
(set-default 'slime-js-swank-command "swank-js")
(set-default 'slime-js-swank-args '())
(set-default 'slime-js-browser-command "open -a \"Google Chrome\"")
(set-default 'slime-js-browser-jacked-in-p nil)
(add-hook 'js2-mode-hook (lambda () (slime-js-minor-mode 1)))
(defun slime-js-run-swank ()
"Runs the swank side of the equation."
(interactive)
(apply #'make-comint "swank-js" slime-js-swank-command nil slime-js-swank-args))
(defun slime-js-jack-in-node ()
"Start a swank-js server and connect to it, opening a repl."
(interactive)
(slime-js-run-swank)
(sleep-for 1)
(setq slime-protocol-version 'ignore)
(slime-connect "localhost" 4005))
(defun slime-js-jack-in-browser ()
"Start a swank-js server, connect to it, open a repl, open a browser, connect to that."
(interactive)
(slime-js-jack-in-node)
(sleep-for 2)
(slime-js-set-target-url slime-js-target-url)
(shell-command (concat slime-js-browser-command " " slime-js-connect-url slime-js-starting-url))
(sleep-for 3)
(setq slime-remote-history nil)
(slime-js-sticky-select-remote (caadr (slime-eval '(js:list-remotes))))
(setq slime-js-browser-jacked-in-p t)
(global-set-key [f5] 'slime-js-reload))
(defadvice save-buffer (after save-css-buffer activate)
(when (and slime-js-browser-jacked-in-p (eq major-mode 'css-mode))
(slime-js-refresh-css)))
(defun js2-eval-friendly-node-p (n)
(or (and (js2-stmt-node-p n) (not (js2-block-node-p n)))
(and (js2-function-node-p n) (js2-function-node-name n))))
(defun slime-js--echo-result (result &rest _)
(message result))
(defun slime-js--replace-with-result (replacement beg end)
(save-excursion
(goto-char beg)
(delete-char (- end beg))
(insert replacement)))
(defun slime-js-eval-region (beg end &optional func)
(lexical-let ((func (or func 'slime-js--echo-result))
(beg beg)
(end end))
(slime-flash-region beg end)
(slime-js-eval
(buffer-substring-no-properties beg end)
#'(lambda (s) (funcall func (cadr s) beg end)))))
(defun slime-js-eval-statement (&optional func)
(let ((node (js2r--closest 'js2-eval-friendly-node-p)))
(slime-js-eval-region (js2-node-abs-pos node)
(js2-node-abs-end node)
func)))
(defun slime-js-eval-current ()
(interactive)
(if (use-region-p)
(slime-js-eval-region (point) (mark))
(slime-js-eval-statement)))
(defun slime-js-eval-and-replace-current ()
(interactive)
(if (use-region-p)
(slime-js-eval-region (point) (mark) 'slime-js--replace-with-result)
(slime-js-eval-statement 'slime-js--replace-with-result)))
(define-key slime-js-minor-mode-map (kbd "C-x C-e") 'slime-js-eval-current)
(define-key slime-js-minor-mode-map (kbd "C-c C-e") 'slime-js-eval-and-replace-current)
;; Remove slime-minor-mode from mode line if diminish.el is installed
(when (boundp 'diminish)
(diminish 'slime-js-minor-mode))
(provide 'setup-slime-js)