This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
acrepl.el
359 lines (305 loc) · 10.8 KB
/
acrepl.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
;;; acrepl.el --- A Clojure REPL -*- lexical-binding: t; -*-
;; Author: sogaiu
;; Version: 20190907
;; Package-Requires: ((clojure-mode "5.11.0") (smartparens "1.11.0") (emacs "26.2"))
;; Keywords: clojure, repl
;; This file is not part of GNU Emacs.
;;; Commentary:
;; A Clojure REPL - simple socket repl interaction and editor functionality
;;;; Installation
;;;;; Manual
;; Ensure this file and the following dependencies (and their
;; dependencies) are in your load-path:
;;
;; ab.el -- should be included in the same repository
;; clojure-mode
;; smartparens
;;
;; and put this in your relevant init file:
;;
;; (require 'acrepl)
;;
;; Optionally, add:
;;
;; (add-hook 'clojure-mode-hook
;; #'acrepl-interaction-mode)
;;
;; for editor features to be enabled when visiting a buffer with
;; Clojure code in it.
;;;;; Automatic
;; TODO :)
;;;;; Usage
;; 0. Start up a clojure project (JVM, shadow-cljs, Arcadia, CLR) with a
;; socket repl and note the host and port
;;
;; N.B. for shadow-cljs', the info may be autodetected
;; 1. Connect to the socket repl by:
;;
;; M-x acrepl
;;
;; and at the prompt, specify a host and port like:
;;
;; localhost:23579
;;
;; i.e. a host or ip address followed by a colon and port number
;;
;; A buffer for interaction with the socket repl should appear.
;; 2. For editor features, in a relevant buffer with a clojure source file:
;;
;; M-x acrepl-interaction-mode
;;
;; There should be a ACRepl menu containing some convenience commands:
;;
;; Send ascertained region
;; Send buffer
;; Send expression at point
;; Send region
;; tap> expression at point
;;
;; Load buffer file
;; Load file
;;
;; Switch to REPL
;;;;; Acknowledgments
;; Thanks to those involved in:
;;
;; cider
;; clojure-mode
;; inf-clojure
;; miracle
;; monroe
;; replique
;; sesman
;; smartparens
;;
;; and transitively involved folks too ;)
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
;;;; Requirements
(require 'ab)
(require 'clojure-mode)
(require 'comint)
(require 'smartparens)
(require 'subr-x)
;;;; The Rest
(defgroup acrepl nil
"A Clojure REPL"
:prefix "acrepl-"
:group 'applications)
;; clojure repls (NOT unrepl nor prepl):
;;
;; my.namespace=>
;;
;; shadow-cljs:
;;
;; [1:1] my.namespace=>
;;
;; note that each has a trailing space
(defcustom acrepl-prompt-regexp "^[^> \n]*=> *"
"Regexp to recognize prompts in acrepl-mode."
:type 'regexp
:group 'acrepl)
(defcustom acrepl-default-endpoint "localhost:23579"
"Default host and port to connect to.
Host and port should be delimited with ':'."
:type 'string
:group 'acrepl)
(defvar acrepl-repl-buffer-name "*acrepl-repl*"
"Name of repl buffer.")
(defun acrepl-switch-to-repl ()
"Switch to the repl buffer named by `acrepl-repl-buffer-name`."
(interactive)
(pop-to-buffer acrepl-repl-buffer-name))
(defun acrepl-send-code (code-str)
"Send CODE-STR.
CODE-STR should be a Clojure form."
(interactive "sCode: ")
(let ((here (point))
(original-buffer (current-buffer))
(repl-buffer (get-buffer acrepl-repl-buffer-name)))
(if (not repl-buffer)
(message (format "%s is missing..." acrepl-repl-buffer-name))
;; switch to acrepl buffer to prepare for appending
(set-buffer repl-buffer)
(goto-char (point-max))
(insert code-str)
(comint-send-input)
(set-buffer original-buffer)
(if (eq original-buffer repl-buffer)
(goto-char (point-max))
(goto-char here)))))
(defun acrepl-send-region (start end &optional pre post)
"Send a region bounded by START and END.
Optional arguments PRE and POST are strings to insert before
and after the region about to be sent, respectively."
(interactive "r")
(let ((here (point))
(original-buffer (current-buffer))
(repl-buffer (get-buffer acrepl-repl-buffer-name)))
(if (not repl-buffer)
(message (format "%s is missing..." acrepl-repl-buffer-name))
;; switch to acrepl buffer to prepare for appending
(set-buffer repl-buffer)
(goto-char (point-max))
(when pre
(insert pre))
;; switch back
(set-buffer original-buffer)
(append-to-buffer repl-buffer start end)
(set-buffer repl-buffer)
(when post
(insert post))
(comint-send-input)
(set-buffer original-buffer)
(goto-char here))))
(defun acrepl-tap-region (start end)
"Apply tap> to a region bounded by START and END."
(interactive "r")
(acrepl-send-region start end "(tap> " ")"))
(defun acrepl-send-ascertained-region ()
"Send a region ascertained around point."
(interactive)
(condition-case err
(cl-destructuring-bind (start end) (ab-sexp-bounds (list 'def
'defn
'defn-
'defmacro
'ns
'require))
(acrepl-send-region start end))
(wrong-number-of-arguments
(message "Failed to find containing def* form."))
(error
(message "Error: %s %s" (car err) (cdr err)))))
(defun acrepl-send-buffer ()
"Send buffer content."
(interactive)
(acrepl-send-region (point-min) (point-max)))
(defun acrepl-send-expression-at-point ()
"Send expression at point."
(interactive)
(let* ((thing (sp-get-thing t))
(start (sp-get thing :beg))
(end (sp-get thing :end)))
(when (and start end)
(acrepl-send-region start end))))
(defun acrepl-tap-expression-at-point ()
"Apply tap> to expression at point."
(interactive)
(let* ((thing (sp-get-thing t))
(start (sp-get thing :beg))
(end (sp-get thing :end)))
(when (and start end)
(acrepl-tap-region start end))))
(defun acrepl-load-file (filename)
"Send the `load-file` form with full path of FILENAME."
(interactive "fFile name: ")
(acrepl-send-code (format "(load-file \"%s\")"
(expand-file-name filename))))
(defun acrepl-load-buffer-file ()
"Send the `load-file` form with buffer's full path."
(interactive)
(acrepl-load-file (buffer-file-name)))
(defvar acrepl-interaction-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-a" 'acrepl-send-ascertained-region)
(define-key map "\C-c\C-b" 'acrepl-send-buffer)
(define-key map "\C-c\C-e" 'acrepl-send-expression-at-point)
(define-key map "\C-c\C-t" 'acrepl-tap-expression-at-point)
(define-key map "\C-c\C-i" 'acrepl-load-file)
(define-key map "\C-c\C-l" 'acrepl-load-buffer-file)
(define-key map "\C-c\C-r" 'acrepl-send-region)
(define-key map "\C-c\C-z" 'acrepl-switch-to-repl)
(easy-menu-define acrepl-interaction-mode-map map
"A Clojure REPL Interaction Mode Menu"
'("ACRepl"
["Send ascertained region" acrepl-send-ascertained-region t]
["Send buffer" acrepl-send-buffer t]
["Send expression at point" acrepl-send-expression-at-point t]
["Send region" acrepl-send-region t]
["tap> expression at point" acrepl-tap-expression-at-point t]
"--"
["Load buffer file" acrepl-load-buffer-file t]
["Load file" acrepl-load-file t]
"--"
["Switch to REPL" acrepl-switch-to-repl t]))
map)
"ACRepl interaction mode map.")
(defvar acrepl-mode-map
(let ((map (copy-keymap comint-mode-map)))
(define-key map "\C-c\C-i" 'acrepl-load-file)
(easy-menu-define acrepl-mode-map map
"A Clojure REPL Mode Menu"
'("ACRepl"
["Load file" acrepl-load-file t]))
map)
"ACRepl mode map.")
(define-derived-mode acrepl-mode comint-mode "A Clojure REPL"
"Major mode for acrepl.
\\{acrepl-mode-map}"
:syntax-table clojure-mode-syntax-table
(setq comint-prompt-regexp acrepl-prompt-regexp)
(setq comint-prompt-read-only t)
(setq mode-line-process '(":%s"))
;; XXX: can use setq-local instead?
(set (make-local-variable 'font-lock-defaults)
'(clojure-font-lock-keywords t)))
;;;###autoload
(define-minor-mode acrepl-interaction-mode
"Minor mode for acrepl interaction from a Clojure buffer.
The following keys are available in `acrepl-interaction-mode`:
\\{acrepl-interaction-mode}"
nil " acrepl" acrepl-interaction-mode-map)
;;; XXX: git-specific and works only for shadow-cljs
(defun acrepl-guess-endpoint ()
"Guess an endpoint."
(let ((closest-dot-git-parent
(locate-dominating-file default-directory ".git")))
(when closest-dot-git-parent
(let ((socket-repl-port-file (concat closest-dot-git-parent
".shadow-cljs/socket-repl.port")))
(when (file-exists-p socket-repl-port-file)
(let ((port (string-to-number
(with-temp-buffer
(insert-file-contents socket-repl-port-file)
(buffer-string)))))
(when (> port 0)
(format "localhost:%s" port))))))))
;;;###autoload
(defun acrepl (endpoint)
"Start acrepl.
Query user for ENDPOINT which specifies the Clojure socket REPL
endpoint. ENDPOINT is a string of the form: \"hostname:port\"."
(interactive
(let ((endpoint (or (acrepl-guess-endpoint)
acrepl-default-endpoint)))
(list
(read-string (format "REPL endpoint (default '%s'): " endpoint)
endpoint nil endpoint))))
(unless
;(ignore-errors ;; XXX: uncomment at some point...
(let* ((ep (split-string endpoint ":"))
(host (car ep))
(port (string-to-number (cadr ep))))
(message "Connecting to socket REPL on '%s:%d'..." host port)
(with-current-buffer (get-buffer-create acrepl-repl-buffer-name)
(prog1
(make-comint-in-buffer "acrepl" acrepl-repl-buffer-name
(cons host port))
(goto-char (point-max))
(acrepl-mode)
(pop-to-buffer (current-buffer))
(goto-char (point-max)))))
(message "Failed to connect to %s" endpoint)))
(provide 'acrepl)
;;; acrepl.el ends here