-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
inf-clojure.el
1651 lines (1454 loc) · 70.1 KB
/
inf-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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; inf-clojure.el --- Run an external Clojure process in an Emacs buffer -*- lexical-binding: t; -*-
;; Copyright © 2014-2022 Bozhidar Batsov
;; Authors: Bozhidar Batsov <[email protected]>
;; Olin Shivers <[email protected]>
;; Maintainer: Bozhidar Batsov <[email protected]>
;; URL: http://github.com/clojure-emacs/inf-clojure
;; Keywords: processes, comint, clojure
;; Version: 3.2.1
;; Package-Requires: ((emacs "26.2") (clojure-mode "5.11"))
;; This file is not part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This package provides basic interaction with a Clojure subprocess (REPL).
;; It's based on ideas from the popular `inferior-lisp` package.
;;
;; `inf-clojure` has two components - a nice Clojure REPL with
;; auto-completion and a minor mode (`inf-clojure-minor-mode`), which
;; extends `clojure-mode` with commands to evaluate forms directly in the
;; REPL.
;;
;; `inf-clojure` provides a set of essential features for interactive
;; Clojure/ClojureScript/ClojureCLR development:
;;
;; * REPL
;; * Interactive code evaluation
;; * Code completion
;; * Definition lookup
;; * Documentation lookup
;; * ElDoc
;; * Apropos
;; * Macroexpansion
;; * Support connecting to socket REPLs
;; * Support for Lumo
;; * Support for Planck
;; * Support for Joker
;;
;; For a more powerful/full-featured solution see https://github.com/clojure-emacs/cider.
;;
;; If you're installing manually, you'll need to:
;;
;; * drop the file somewhere on your load path (perhaps ~/.emacs.d)
;; * Add the following lines to your .emacs file:
;;
;; (autoload 'inf-clojure "inf-clojure" "Run an inferior Clojure process" t)
;; (add-hook 'clojure-mode-hook #'inf-clojure-minor-mode)
;;; Code:
(require 'comint)
(require 'clojure-mode)
(require 'eldoc)
(require 'thingatpt)
(require 'ansi-color)
(require 'cl-lib)
(require 'subr-x)
(defvar inf-clojure-startup-forms '((lein . "lein repl")
(boot . "boot repl")
(clojure . "clojure")
(cljs . "clojure -M -m cljs.main -r")
(lein-clr . "lein clr repl")
(planck . "planck -d")
(babashka . "bb")
(lumo . "lumo -d")
(joker . "joker")))
(defvar inf-clojure-repl-features
'((cljs . ((doc . "(cljs.repl/doc %s)")
(source . "(cljs.repl/source %s)")
(arglists . "(try (->> '%s cljs.core/resolve cljs.core/meta :arglists) (catch :default _ nil))")
(apropos . "(cljs.repl/apropos \"%s\")")
(ns-vars . "(cljs.repl/dir %s)")
(set-ns . "(in-ns '%s)")
(macroexpand . "(cljs.core/macroexpand '%s)")
(macroexpand-1 . "(cljs.core/macroexpand-1 '%s)")))
(lumo . ((load . "(clojure.core/load-file \"%s\")")
(doc . "(lumo.repl/doc %s)")
(source . "(lumo.repl/source %s)")
(arglists .
"(let [old-value lumo.repl/*pprint-results*]
(set! lumo.repl/*pprint-results* false)
(js/setTimeout #(set! lumo.repl/*pprint-results* old-value) 0)
(lumo.repl/get-arglists \"%s\"))")
(apropos . "(lumo.repl/apropos \"%s\")")
(ns-vars . "(lumo.repl/dir %s)")
(set-ns . "(in-ns '%s)")
(macroexpand . "(macroexpand-1 '%s)")
(macroexpand-1 . "(macroexpand-1 '%s)")
(completion .
"(let [ret (atom nil)]
(lumo.repl/get-completions \"%s\" (fn [res] (reset! ret (map str res))))
@ret)")))
(planck . ((load . "(load-file \"%s\")")
(doc . "(planck.repl/doc %s)")
(source . "(planck.repl/source %s)")
(arglists . "(planck.repl/get-arglists \"%s\")")
(apropos . "(doseq [var (sort (planck.repl/apropos \"%s\"))] (println (str var)))")
(ns-vars . "(planck.repl/dir %s)")
(set-ns . "(in-ns '%s)")
(macroexpand . "(macroexpand '%s)")
(macroexpand-1 . "(macroexpand-1 '%s)")
(completion . "(seq (js->clj (#'planck.repl/get-completions \"%s\")))")))
(joker . ((load . "(load-file \"%s\")")
(doc . "(joker.repl/doc %s)")
(arglists .
"(try
(:arglists
(joker.core/meta
(joker.core/resolve
(joker.core/read-string \"%s\"))))
(catch Error _ nil))")
(set-ns . "(in-ns '%s)")
(macroexpand . "(macroexpand '%s)")
(macroexpand-1 . "(macroexpand-1 '%s)")))
(babashka . ((load . "(clojure.core/load-file \"%s\")")
(doc . "(clojure.repl/doc %s)")
(source . "(clojure.repl/source %s)")
(arglists .
"(try (-> '%s clojure.core/resolve clojure.core/meta :arglists)
(catch Throwable e nil))")
(apropos . "(doseq [var (sort (clojure.repl/apropos \"%s\"))] (println (str var)))")
(ns-vars . "(clojure.repl/dir %s)")
(set-ns . "(clojure.core/in-ns '%s)")
(macroexpand . "(clojure.core/macroexpand '%s)")
(macroexpand-1 . "(clojure.core/macroexpand-1 '%s)")))
(node-babashka . ((load . "(clojure.core/load-file \"%s\")")
(doc . "(clojure.repl/doc %s)")
(source . "(clojure.repl/source %s)")
(arglists .
"(try (-> '%s clojure.core/resolve clojure.core/meta :arglists)
(catch Throwable e nil))")
(apropos . "(doseq [var (sort (clojure.repl/apropos \"%s\"))] (println (str var)))")
(ns-vars . "(clojure.repl/dir %s)")
(set-ns . "(clojure.core/in-ns '%s)")
(macroexpand . "(clojure.core/macroexpand '%s)")
(macroexpand-1 . "(clojure.core/macroexpand-1 '%s)")))
(clojure . ((load . "(clojure.core/load-file \"%s\")")
(doc . "(clojure.repl/doc %s)")
(source . "(clojure.repl/source %s)")
(arglists .
"(try
(:arglists
(clojure.core/meta
(clojure.core/resolve
(clojure.core/read-string \"%s\"))))
(catch #?(:clj Throwable :cljr Exception) e nil))")
(apropos . "(doseq [var (sort (clojure.repl/apropos \"%s\"))] (println (str var)))")
(ns-vars . "(clojure.repl/dir %s)")
(set-ns . "(clojure.core/in-ns '%s)")
(macroexpand . "(clojure.core/macroexpand '%s)")
(macroexpand-1 . "(clojure.core/macroexpand-1 '%s)")))
(lein-clr . ((load . "(clojure.core/load-file \"%s\")")
(doc . "(clojure.repl/doc %s)")
(source . "(clojure.repl/source %s)")
(arglists .
"(try
(:arglists
(clojure.core/meta
(clojure.core/resolve
(clojure.core/read-string \"%s\"))))
(catch Exception e nil))")
(apropos . "(doseq [var (sort (clojure.repl/apropos \"%s\"))] (println (str var)))")
(ns-vars . "(clojure.repl/dir %s)")
(set-ns . "(clojure.core/in-ns '%s)")
(macroexpand . "(clojure.core/macroexpand '%s)")
(macroexpand-1 . "(clojure.core/macroexpand-1 '%s)")))))
(defvar-local inf-clojure-repl-type nil
"Symbol to define your REPL type.
Its root binding is nil and it can be further customized using
either `setq-local` or an entry in `.dir-locals.el`." )
(defvar inf-clojure-buffer nil
"The current `inf-clojure' process buffer.
MULTIPLE PROCESS SUPPORT
===========================================================================
To run multiple Clojure processes, you start the first up
with \\[inf-clojure]. It will be in a buffer named `*inf-clojure*'.
Rename this buffer with \\[rename-buffer]. You may now start up a new
process with another \\[inf-clojure]. It will be in a new buffer,
named `*inf-clojure*'. You can switch between the different process
buffers with \\[switch-to-buffer].
Commands that send text from source buffers to Clojure processes --
like `inf-clojure-eval-defun' or `inf-clojure-show-arglists' -- have to choose a
process to send to, when you have more than one Clojure process around. This
is determined by the global variable `inf-clojure-buffer'. Suppose you
have three inferior Clojures running:
Buffer Process
foo inf-clojure
bar inf-clojure<2>
*inf-clojure* inf-clojure<3>
If you do a \\[inf-clojure-eval-defun] command on some Clojure source code,
what process do you send it to?
- If you're in a process buffer (foo, bar, or *inf-clojure*),
you send it to that process.
- If you're in some other buffer (e.g., a source file), you
send it to the process attached to buffer `inf-clojure-buffer'.
This process selection is performed by function `inf-clojure-proc'.
Whenever \\[inf-clojure] fires up a new process, it resets
`inf-clojure-buffer' to be the new process's buffer. If you only run
one process, this does the right thing. If you run multiple
processes, you might need to change `inf-clojure-buffer' to
whichever process buffer you want to use.")
(defun inf-clojure--get-feature (repl-type feature no-error)
"Get FEATURE for REPL-TYPE from repl-features.
If no-error is truthy don't error if feature is not present."
(let ((feature-form (alist-get feature (alist-get repl-type inf-clojure-repl-features))))
(cond (feature-form feature-form)
(no-error nil)
(t (error "%s not configured for %s" feature repl-type)))))
(defun inf-clojure-get-feature (proc feature &optional no-error)
"Get FEATURE based on repl type for PROC."
(let* ((repl-type (or (with-current-buffer (process-buffer proc)
inf-clojure-repl-type)
(error "REPL type is not known"))))
(inf-clojure--get-feature repl-type feature no-error)))
(defun inf-clojure--update-feature (repl-type feature form)
"Return a copy of the datastructure containing the repl features.
Given a REPL-TYPE (`clojure', `lumo', ...) and a FEATURE (`doc',
`apropos', ...) and a FORM this will return a new datastructure
that can be set as `inf-clojure-repl-features'."
(let ((original (alist-get repl-type inf-clojure-repl-features)))
(if original
(cons (cons repl-type (cons (cons feature form) (assoc-delete-all feature original)))
(assoc-delete-all repl-type inf-clojure-repl-features))
(error "Attempted to update %s form of unknown REPL type %s"
(symbol-name feature)
(symbol-name repl-type)))))
(defun inf-clojure-update-feature (repl-type feature form)
"Mutate the repl features to the new FORM.
Given a REPL-TYPE (`clojure', `lumo', ...) and a FEATURE (`doc',
`apropos', ...) and a FORM this will set
`inf-clojure-repl-features' with these new values."
(setq inf-clojure-repl-features (inf-clojure--update-feature repl-type feature form)))
(defun inf-clojure-proc (&optional no-error)
"Return the current inferior Clojure process.
When NO-ERROR is non-nil, don't throw an error when no process
has been found. See also variable `inf-clojure-buffer'."
(or (get-buffer-process (if (derived-mode-p 'inf-clojure-mode)
(current-buffer)
inf-clojure-buffer))
(unless no-error
(error "No Clojure subprocess; see variable `inf-clojure-buffer'"))))
(defun inf-clojure-repl-p (&optional buf)
"Indicates if BUF is an inf-clojure REPL.
If BUF is nil then defaults to the current buffer.
Checks the mode and that there is a live process."
(let ((buf (or buf (current-buffer))))
(and (with-current-buffer buf (derived-mode-p 'inf-clojure-mode))
(get-buffer-process buf)
(process-live-p (get-buffer-process buf)))))
(defun inf-clojure-repls ()
"Return a list of all inf-clojure REPL buffers."
(let (repl-buffers)
(dolist (b (buffer-list))
(when (inf-clojure-repl-p b)
(push (buffer-name b) repl-buffers)))
repl-buffers))
(defun inf-clojure--prompt-repl-buffer (prompt)
"Prompt the user to select an inf-clojure repl buffer.
PROMPT is a string to prompt the user.
Returns nil when no buffer is selected."
(let ((repl-buffers (inf-clojure-repls)))
(if (> (length repl-buffers) 0)
(when-let ((repl-buffer (completing-read prompt repl-buffers nil t)))
(get-buffer repl-buffer))
(user-error "No buffers have an inf-clojure process"))))
(defun inf-clojure-set-repl (always-ask)
"Set an inf-clojure buffer as the active (default) REPL.
If in a REPL buffer already, use that unless a prefix is used (or
ALWAYS-ASK). Otherwise get a list of all active inf-clojure
REPLS and offer a choice. It's recommended to rename REPL
buffers after they are created with `rename-buffer'."
(interactive "P")
(when-let ((new-repl-buffer
(if (or always-ask
(not (inf-clojure-repl-p)))
(inf-clojure--prompt-repl-buffer "Select default REPL: ")
(current-buffer))))
(setq inf-clojure-buffer new-repl-buffer)
(message "Current inf-clojure REPL set to %s" new-repl-buffer)))
(defvar inf-clojure--repl-type-lock nil
"Global lock for protecting against proc filter race conditions.
See http://blog.jorgenschaefer.de/2014/05/race-conditions-in-emacs-process-filter.html")
(defun inf-clojure--prompt-repl-type ()
"Set the REPL type to one of the available implementations."
(interactive)
(let ((types (mapcar #'car inf-clojure-repl-features)))
(intern
(completing-read "Set REPL type: "
(sort (mapcar #'symbol-name types) #'string-lessp)))))
(defgroup inf-clojure nil
"Run an external Clojure process (REPL) in an Emacs buffer."
:prefix "inf-clojure-"
:group 'clojure
:link '(url-link :tag "GitHub" "https://github.com/clojure-emacs/inf-clojure")
:link '(emacs-commentary-link :tag "Commentary" "inf-clojure"))
(defconst inf-clojure-version
(or (if (fboundp 'package-get-version)
(package-get-version))
"3.2.1")
"The current version of `inf-clojure'.")
(defcustom inf-clojure-prompt-read-only t
"If non-nil, the prompt will be read-only.
Also see the description of `ielm-prompt-read-only'."
:type 'boolean)
(defcustom inf-clojure-filter-regexp
"\\`\\s *\\(:\\(\\w\\|\\s_\\)\\)?\\s *\\'"
"What not to save on inferior Clojure's input history.
Input matching this regexp is not saved on the input history in Inferior Clojure
mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword
\(as in :a, :c, etc.)"
:type 'regexp)
(defun inf-clojure--modeline-info ()
"Return modeline info for `inf-clojure-minor-mode'.
Either \"no process\" or \"buffer-name(repl-type)\""
(if (and (bufferp inf-clojure-buffer)
(buffer-live-p inf-clojure-buffer))
(with-current-buffer inf-clojure-buffer
(format "%s(%s)" (buffer-name (current-buffer)) inf-clojure-repl-type))
"no process"))
(defvar inf-clojure-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map comint-mode-map)
(define-key map (kbd "C-x C-e") #'inf-clojure-eval-last-sexp)
(define-key map (kbd "C-c C-l") #'inf-clojure-load-file)
(define-key map (kbd "C-c C-a") #'inf-clojure-show-arglists)
(define-key map (kbd "C-c C-v") #'inf-clojure-show-var-documentation)
(define-key map (kbd "C-c C-s") #'inf-clojure-show-var-source)
(define-key map (kbd "C-c C-S-a") #'inf-clojure-apropos)
(define-key map (kbd "C-c M-o") #'inf-clojure-clear-repl-buffer)
(define-key map (kbd "C-c C-q") #'inf-clojure-quit)
(define-key map (kbd "C-c C-z") #'inf-clojure-switch-to-recent-buffer)
(easy-menu-define inf-clojure-mode-menu map
"Inferior Clojure REPL Menu"
'("Inf-Clojure REPL"
["Eval last sexp" inf-clojure-eval-last-sexp t]
"--"
["Load file" inf-clojure-load-file t]
"--"
["Show arglists" inf-clojure-show-arglists t]
["Show documentation for var" inf-clojure-show-var-documentation t]
["Show source for var" inf-clojure-show-var-source t]
["Apropos" inf-clojure-apropos t]
"--"
["Clear REPL" inf-clojure-clear-repl-buffer]
["Restart" inf-clojure-restart]
["Quit" inf-clojure-quit]
"--"
["Version" inf-clojure-display-version]))
map))
(defvar inf-clojure-insert-commands-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "d") #'inf-clojure-insert-defun)
(define-key map (kbd "C-d") #'inf-clojure-insert-defun)
(define-key map (kbd "e") #'inf-clojure-insert-last-sexp)
(define-key map (kbd "C-e") #'inf-clojure-insert-last-sexp)
map))
(defvar inf-clojure-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-M-x") #'inf-clojure-eval-defun) ; Gnu convention
(define-key map (kbd "C-x C-e") #'inf-clojure-eval-last-sexp) ; Gnu convention
(define-key map (kbd "C-c C-e") #'inf-clojure-eval-last-sexp)
(define-key map (kbd "C-c C-c") #'inf-clojure-eval-defun) ; SLIME/CIDER style
(define-key map (kbd "C-c C-b") #'inf-clojure-eval-buffer)
(define-key map (kbd "C-c C-r") #'inf-clojure-eval-region)
(define-key map (kbd "C-c M-r") #'inf-clojure-reload)
(define-key map (kbd "C-c C-n") #'inf-clojure-eval-form-and-next)
(define-key map (kbd "C-c C-j") inf-clojure-insert-commands-map)
(define-key map (kbd "C-c C-z") #'inf-clojure-switch-to-repl)
(define-key map (kbd "C-c C-i") #'inf-clojure-show-ns-vars)
(define-key map (kbd "C-c C-S-a") #'inf-clojure-apropos)
(define-key map (kbd "C-c C-m") #'inf-clojure-macroexpand)
(define-key map (kbd "C-c C-l") #'inf-clojure-load-file)
(define-key map (kbd "C-c C-a") #'inf-clojure-show-arglists)
(define-key map (kbd "C-c C-v") #'inf-clojure-show-var-documentation)
(define-key map (kbd "C-c C-s") #'inf-clojure-show-var-source)
(define-key map (kbd "C-c M-n") #'inf-clojure-set-ns)
(define-key map (kbd "C-c C-q") #'inf-clojure-quit)
(define-key map (kbd "C-c M-c") #'inf-clojure-connect)
(easy-menu-define inf-clojure-minor-mode-menu map
"Inferior Clojure Minor Mode Menu"
'("Inf-Clojure"
["Eval top-level sexp at point" inf-clojure-eval-defun t]
["Eval last sexp" inf-clojure-eval-last-sexp t]
["Eval region" inf-clojure-eval-region t]
["Eval buffer" inf-clojure-eval-buffer t]
"--"
["Load file..." inf-clojure-load-file t]
["Reload file... " inf-clojure-reload t]
"--"
["Switch to REPL" inf-clojure-switch-to-repl t]
["Set REPL ns" inf-clojure-set-ns t]
"--"
["Show arglists" inf-clojure-show-arglists t]
["Show documentation for var" inf-clojure-show-var-documentation t]
["Show source for var" inf-clojure-show-var-source t]
["Show vars in ns" inf-clojure-show-ns-vars t]
["Apropos" inf-clojure-apropos t]
["Macroexpand" inf-clojure-macroexpand t]
"--"
["Restart REPL" inf-clojure-restart]
["Quit REPL" inf-clojure-quit]))
map))
;;;###autoload
(defcustom inf-clojure-mode-line
'(:eval (format " inf-clojure[%s]" (inf-clojure--modeline-info)))
"Mode line lighter for cider mode.
The value of this variable is a mode line template as in
`mode-line-format'. See Info Node `(elisp)Mode Line Format' for details
about mode line templates.
Customize this variable to change how inf-clojure-minor-mode
displays its status in the mode line. The default value displays
the current REPL. Set this variable to nil to disable the
mode line entirely."
:type 'sexp
:risky t)
(defcustom inf-clojure-enable-eldoc t
"Var that allows disabling `eldoc-mode' in `inf-clojure'.
Set to nil to disable eldoc. Eldoc can be quite useful by
displaying function signatures in the modeline, but can also
cause multiple prompts to appear in the REPL and mess with *1,
*2, etc."
:type 'boolean
:safe #'booleanp
:package-version '(inf-clojure . "3.2.0"))
;;;###autoload
(define-minor-mode inf-clojure-minor-mode
"Minor mode for interacting with the inferior Clojure process buffer.
The following commands are available:
\\{inf-clojure-minor-mode-map}"
:lighter inf-clojure-mode-line
:keymap inf-clojure-minor-mode-map
(setq-local comint-input-sender 'inf-clojure--send-string)
(when inf-clojure-enable-eldoc
(inf-clojure-eldoc-setup))
(make-local-variable 'completion-at-point-functions)
(add-to-list 'completion-at-point-functions
#'inf-clojure-completion-at-point))
(defun inf-clojure--endpoint-p (x)
"Return non-nil if and only if X is a valid endpoint.
A valid endpoint consists of a host and port
number (e.g. (\"localhost\" . 5555))."
(and
(listp x)
(stringp (car x))
(numberp (cdr x))))
(defcustom inf-clojure-custom-startup
nil
"Form to be used to start `inf-clojure'.
Can be a cons pair of (host . port) where host is a string and
port is an integer, or a string to startup an interpreter like
\"planck\"."
:type '(choice (cons string integer) (const nil)))
(defcustom inf-clojure-custom-repl-type
nil
"REPL type to use for `inf-clojure' process buffer.
Should be a symbol that is a key in `inf-clojure-repl-features'."
:package-version '(inf-clojure . "3.0.0")
:type '(choice (const :tag "clojure" clojure)
(const :tag "cljs" cljs)
(const :tag "lumo" lumo)
(const :tag "planck" planck)
(const :tag "joker" joker)
(const :tag "babashka" babashka)
(const :tag "determine at startup" nil)))
(defvar inf-clojure-custom-repl-name nil
"A string to be used as the repl buffer name.")
(defun inf-clojure--whole-comment-line-p (string)
"Return non-nil iff STRING is a whole line semicolon comment."
(string-match-p "^\s*;" string))
(defun inf-clojure--sanitize-command (command)
"Sanitize COMMAND for sending it to a process.
An example of things that this function does is to add a final
newline at the end of the form. Return an empty string if the
sanitized command is empty."
(let ((sanitized (string-trim-right command)))
(if (string-blank-p sanitized)
""
(concat sanitized "\n"))))
(defun inf-clojure--send-string (proc string)
"A custom `comint-input-sender` / `comint-send-string`.
It performs the required side effects on every send for PROC and
STRING (for example set the buffer local REPL type). It should
always be preferred over `comint-send-string`. It delegates to
`comint-simple-send` so it always appends a newline at the end of
the string for evaluation. Refer to `comint-simple-send` for
customizations."
(let ((sanitized (inf-clojure--sanitize-command string)))
(inf-clojure--log-string sanitized "----CMD->")
(comint-send-string proc sanitized)))
(defcustom inf-clojure-reload-form "(require '%s :reload)"
"Format-string for building a Clojure expression to reload a file.
Reload forces loading of all the identified libs even if they are
already loaded.
This format string should use `%s' to substitute a namespace and
should result in a Clojure form that will be sent to the inferior
Clojure to load that file."
:type 'string
:safe #'stringp
:package-version '(inf-clojure . "2.2.0"))
;; :reload forces loading of all the identified libs even if they are
;; already loaded
;; :reload-all implies :reload and also forces loading of all libs that the
;; identified libs directly or indirectly load via require or use
(defun inf-clojure-reload-form (_proc)
"Return the form to query the Inf-Clojure PROC for reloading a namespace.
If you are using REPL types, it will pickup the most appropriate
`inf-clojure-reload-form` variant."
inf-clojure-reload-form)
(defcustom inf-clojure-reload-all-form "(require '%s :reload-all)"
"Format-string for building a Clojure expression to :reload-all a file.
Reload-all implies :reload and also forces loading of all libs
that the identified libs directly or indirectly load via require
or use.
This format string should use `%s' to substitute a namespace and
should result in a Clojure form that will be sent to the inferior
Clojure to load that file."
:type 'string
:safe #'stringp
:package-version '(inf-clojure . "2.2.0"))
(defun inf-clojure-reload-all-form (_proc)
"Return the form to query the Inf-Clojure PROC for :reload-all of a namespace.
If you are using REPL types, it will pickup the most appropriate
`inf-clojure-reload-all-form` variant."
inf-clojure-reload-all-form)
(defcustom inf-clojure-prompt "^[^=> \n]+=> *"
"Regexp to recognize prompts in the Inferior Clojure mode."
:type 'regexp)
(defcustom inf-clojure-subprompt " *#_=> *"
"Regexp to recognize subprompts in the Inferior Clojure mode."
:type 'regexp)
(defcustom inf-clojure-comint-prompt-regexp "^\\( *#_\\|[^=> \n]+\\)=> *"
"Regexp to recognize both main prompt and subprompt for comint.
This should usually be a combination of `inf-clojure-prompt' and
`inf-clojure-subprompt'."
:type 'regexp)
(defcustom inf-clojure-repl-use-same-window nil
"Controls whether to display the REPL buffer in the current window or not."
:type '(choice (const :tag "same" t)
(const :tag "different" nil))
:safe #'booleanp
:package-version '(inf-clojure . "2.0.0"))
(defcustom inf-clojure-auto-mode t
"Automatically enable inf-clojure-minor-mode.
All buffers in `clojure-mode' will automatically be in
`inf-clojure-minor-mode' unless set to nil."
:type 'boolean
:safe #'booleanp
:package-version '(inf-clojure . "3.1.0"))
(defun inf-clojure--clojure-buffers ()
"Return a list of all existing `clojure-mode' buffers."
(cl-remove-if-not
(lambda (buffer) (with-current-buffer buffer (derived-mode-p 'clojure-mode)))
(buffer-list)))
(defun inf-clojure-enable-on-existing-clojure-buffers ()
"Enable inf-clojure's minor mode on existing Clojure buffers.
See command `inf-clojure-minor-mode'."
(interactive)
(add-hook 'clojure-mode-hook #'inf-clojure-minor-mode)
(dolist (buffer (inf-clojure--clojure-buffers))
(with-current-buffer buffer
(inf-clojure-minor-mode +1))))
(defun inf-clojure-disable-on-existing-clojure-buffers ()
"Disable command `inf-clojure-minor-mode' on existing Clojure buffers."
(interactive)
(dolist (buffer (inf-clojure--clojure-buffers))
(with-current-buffer buffer
(inf-clojure-minor-mode -1))))
(define-derived-mode inf-clojure-mode comint-mode "Inferior Clojure"
"Major mode for interacting with an inferior Clojure process.
Runs a Clojure interpreter as a subprocess of Emacs, with Clojure
I/O through an Emacs buffer. Variables of the type
`inf-clojure-*-cmd' combined with the project type controls how
a Clojure REPL is started. Variables `inf-clojure-prompt',
`inf-clojure-filter-regexp' and `inf-clojure-load-form' can
customize this mode for different Clojure REPLs.
For information on running multiple processes in multiple buffers, see
documentation for variable `inf-clojure-buffer'.
\\{inf-clojure-mode-map}
Customization: Entry to this mode runs the hooks on `comint-mode-hook' and
`inf-clojure-mode-hook' (in that order).
You can send text to the inferior Clojure process from other buffers containing
Clojure source.
`inf-clojure-switch-to-repl' switches the current buffer to the Clojure
process buffer.
`inf-clojure-eval-defun' sends the current defun to the Clojure process.
`inf-clojure-eval-region' sends the current region to the Clojure process.
Prefixing the inf-clojure-eval/defun/region commands with
a \\[universal-argument] causes a switch to the Clojure process buffer after
sending the text.
Commands:\\<inf-clojure-mode-map>
\\[comint-send-input] after the end of the process' output sends the text from
the end of process to point.
\\[comint-send-input] before the end of the process' output copies the sexp
ending at point to the end of the process' output, and sends it.
\\[comint-copy-old-input] copies the sexp ending at point to the end of the
process' output,allowing you to edit it before sending it.
If `comint-use-prompt-regexp' is nil (the default), \\[comint-insert-input] on
old input copies the entire old input to the end of the process' output,
allowing you to edit it before sending it. When not used on old input, or if
`comint-use-prompt-regexp' is non-nil, \\[comint-insert-input] behaves
according to its global binding.
\\[backward-delete-char-untabify] converts tabs to spaces as it moves back.
\\[clojure-indent-line] indents for Clojure; with argument, shifts rest
of expression rigidly with the current line.
\\[indent-sexp] does \\[clojure-indent-line] on each line starting within
following expression. Paragraphs are separated only by blank lines.
Semicolons start comments. If you accidentally suspend your process,
use \\[comint-continue-subjob] to continue it."
(setq comint-input-sender 'inf-clojure--send-string)
(setq comint-prompt-regexp inf-clojure-comint-prompt-regexp)
(setq mode-line-process '(":%s"))
(clojure-mode-variables)
(clojure-font-lock-setup)
(when inf-clojure-enable-eldoc
(inf-clojure-eldoc-setup))
(setq comint-get-old-input #'inf-clojure-get-old-input)
(setq comint-input-filter #'inf-clojure-input-filter)
(setq-local comint-prompt-read-only inf-clojure-prompt-read-only)
(add-hook 'comint-preoutput-filter-functions #'inf-clojure-preoutput-filter nil t)
(add-hook 'completion-at-point-functions #'inf-clojure-completion-at-point nil t)
(ansi-color-for-comint-mode-on)
(when inf-clojure-auto-mode
(inf-clojure-enable-on-existing-clojure-buffers)))
(defun inf-clojure-get-old-input ()
"Return a string containing the sexp ending at point."
(save-excursion
(let ((end (point)))
(backward-sexp)
(buffer-substring (max (point) (comint-line-beginning-position)) end))))
(defun inf-clojure-input-filter (str)
"Return t if STR does not match `inf-clojure-filter-regexp'."
(not (string-match inf-clojure-filter-regexp str)))
(defun inf-clojure-chomp (string)
"Remove final newline from STRING."
(if (string-match "[\n]\\'" string)
(replace-match "" t t string)
string))
(defun inf-clojure-remove-subprompts (string)
"Remove subprompts from STRING."
(replace-regexp-in-string inf-clojure-subprompt "" string))
(defun inf-clojure-preoutput-filter (str)
"Preprocess the output STR from interactive commands."
(inf-clojure--log-string str "<-RES----")
(cond
((string-prefix-p "inf-clojure-" (symbol-name (or this-command last-command)))
;; Remove subprompts and prepend a newline to the output string
(inf-clojure-chomp (concat "\n" (inf-clojure-remove-subprompts str))))
(t str)))
(defun inf-clojure-clear-repl-buffer ()
"Clear the REPL buffer."
(interactive)
(with-current-buffer (if (derived-mode-p 'inf-clojure-mode)
(current-buffer)
inf-clojure-buffer)
(let ((comint-buffer-maximum-size 0))
(comint-truncate-buffer))))
(defun inf-clojure--swap-to-buffer-window (to-buffer)
"Switch to `TO-BUFFER''s window."
(let ((pop-up-frames
;; Be willing to use another frame
;; that already has the window in it.
(or pop-up-frames
(get-buffer-window to-buffer t))))
(pop-to-buffer to-buffer '(display-buffer-reuse-window . ()))))
(defun inf-clojure-switch-to-repl (eob-p)
"Switch to the inferior Clojure process buffer.
With prefix argument EOB-P, positions cursor at end of buffer."
(interactive "P")
(if (get-buffer-process inf-clojure-buffer)
(inf-clojure--swap-to-buffer-window inf-clojure-buffer)
(call-interactively #'inf-clojure))
(when eob-p
(push-mark)
(goto-char (point-max))))
(defun inf-clojure-switch-to-recent-buffer ()
"Switch to the most recently used `inf-clojure-minor-mode' buffer."
(interactive)
(let ((recent-inf-clojure-minor-mode-buffer (seq-find (lambda (buf)
(with-current-buffer buf (bound-and-true-p inf-clojure-minor-mode)))
(buffer-list))))
(if recent-inf-clojure-minor-mode-buffer
(inf-clojure--swap-to-buffer-window recent-inf-clojure-minor-mode-buffer)
(message "inf-clojure: No recent buffer known."))))
(defun inf-clojure-quit (&optional buffer)
"Kill the REPL buffer and its underlying process.
You can pass the target BUFFER as an optional parameter
to suppress the usage of the target buffer discovery logic."
(interactive)
(let ((target-buffer (or buffer (inf-clojure-select-target-repl))))
(when (get-buffer-process target-buffer)
(delete-process target-buffer))
(kill-buffer target-buffer)))
(defun inf-clojure-restart (&optional buffer)
"Restart the REPL buffer and its underlying process.
You can pass the target BUFFER as an optional parameter
to suppress the usage of the target buffer discovery logic."
(interactive)
(let* ((target-buffer (or buffer (inf-clojure-select-target-repl)))
(target-buffer-name (buffer-name target-buffer)))
;; TODO: Try to recycle the old buffer instead of killing and recreating it
(inf-clojure-quit target-buffer)
(call-interactively #'inf-clojure)
(rename-buffer target-buffer-name)))
(defun inf-clojure--project-name (dir)
"Extract a project name from a project DIR.
The name is simply the final segment of the path."
(file-name-nondirectory (directory-file-name dir)))
;;;###autoload
(defun inf-clojure (cmd &optional suppress-message)
"Run an inferior Clojure process, input and output via buffer `*inf-clojure*'.
If there is a process already running in `*inf-clojure*', just
switch to that buffer.
CMD is a string which serves as the startup command or a cons of
host and port.
Prompts user for repl startup command and repl type if not
inferrable from startup command. Uses `inf-clojure-custom-repl-type'
and `inf-clojure-custom-startup' if those are set.
Use a prefix to prevent using these when they
are set.
Prints a message that it has connected to the host and port
unless SUPPRESS-MESSAGE is truthy.
Runs the hooks from `inf-clojure-mode-hook' (after the
`comint-mode-hook' is run). \(Type \\[describe-mode] in the
process buffer for a list of commands.)"
(interactive (list (or (unless current-prefix-arg
inf-clojure-custom-startup)
(completing-read "Select Clojure REPL startup command: "
(mapcar #'cdr inf-clojure-startup-forms)
nil
'confirm-after-completion))))
(let* ((project-dir (clojure-project-dir))
(process-buffer-name (or
inf-clojure-custom-repl-name
(if project-dir
(format "inf-clojure %s" (inf-clojure--project-name project-dir))
"inf-clojure")))
;; comint adds the asterisks to both sides
(repl-buffer-name (format "*%s*" process-buffer-name)))
;; Create a new comint buffer if needed
(unless (comint-check-proc repl-buffer-name)
;; run the new process in the project's root when in a project folder
(let ((default-directory (or project-dir default-directory))
(cmdlist (if (consp cmd)
(list cmd)
(split-string-and-unquote cmd)))
(repl-type (or (unless prefix-arg
inf-clojure-custom-repl-type)
(car (rassoc cmd inf-clojure-startup-forms))
(inf-clojure--prompt-repl-type))))
(unless suppress-message
(message "Starting Clojure REPL via `%s'..." cmd))
(with-current-buffer (apply #'make-comint
process-buffer-name (car cmdlist) nil (cdr cmdlist))
(inf-clojure-mode)
(set-syntax-table clojure-mode-syntax-table)
(setq-local inf-clojure-repl-type repl-type)
(hack-dir-local-variables-non-file-buffer))))
;; update the default comint buffer and switch to it
(setq inf-clojure-buffer (get-buffer repl-buffer-name))
(if inf-clojure-repl-use-same-window
(pop-to-buffer-same-window repl-buffer-name)
(pop-to-buffer repl-buffer-name))
repl-buffer-name))
;;;###autol
(defun inf-clojure-connect (host port &optional suppress-message)
"Connect to a running socket REPL server via `inf-clojure'.
HOST is the host the process is running on, PORT is where it's
listening. SUPPRESS-MESSAGE is optional and if truthy will
prevent showing the startup message."
(interactive "shost: \nnport: ")
(inf-clojure (cons host port) suppress-message))
(defvar-local inf-clojure-socket-callback nil
"Used to transfer state between the socket process buffer & REPL buffer.")
(defvar-local inf-clojure-socket-buffer nil
"Used to kill the associated socket buffer when it's REPL buffer is killed.")
(defun inf-clojure-socket-filter (process output)
"A filter that gets triggered each time the socket receives new OUTPUT.
This function prints out the output received but also
watches for a prompt using the `inf-clojure-prompt' regexp, once
this happens a callback is triggered if available. The callback
is intended to be used to trigger a `inf-clojure-connect' once we
can determine that a socket REPL is ready to receive a
connection.
PROCESS is the process object that is being filtered.
OUTPUT is the latest data received from the process"
(let ((server-buffer (process-buffer process)))
(when (buffer-live-p server-buffer)
(with-current-buffer server-buffer
(insert output)))
(let ((prompt-displayed (string-match inf-clojure-prompt output)))
(when prompt-displayed
(with-current-buffer server-buffer
(when inf-clojure-socket-callback
(funcall inf-clojure-socket-callback)))))))
(defun inf-clojure-socket-repl-sentinel (process _event)
"Ensures socket REPL are cleaned up when the REPL buffer is closed.
PROCESS is the process object that is connected to a socket REPL.
EVENT is the event that triggered this function to be called."
(when (not (process-live-p process))
(let ((repl-buffer (process-buffer process)))
(with-current-buffer repl-buffer
(when inf-clojure-socket-buffer
(kill-buffer inf-clojure-socket-buffer))))))
(defvar inf-clojure-socket-repl-startup-forms
'((lein . "JVM_OPTS='-Dclojure.server.repl={:port %d :accept clojure.core.server/repl}' lein repl")
(boot . "export BOOT_JVM_OPTIONS='-Dclojure.server.repl=\"{:port %d :accept clojure.core.server/repl}\"' boot repl")
(clojure . "clojure -J-Dclojure.server.repl=\"{:port %d :accept clojure.core.server/repl}\"")
(cljs . "clojure -J-Dclojure.server.repl=\"{:port %d :accept cljs.server.browser/repl}\"")
(lein-clr . "JVM_OPTS='-Dclojure.server.repl={:port %d :accept clojure.core.server/repl}' lein clr repl")
(planck . "planck -n %d")
(babashka . "bb socket-repl %d")))
(defcustom inf-clojure-socket-repl-port
nil
"Port to be used when creating a socket REPL via `inf-clojure-socket-repl'.
If left as nil a random port will be selected between 5500-6000."
:type '(choice integer (const nil))
:package-version '(inf-clojure . "3.3"))
;;;###autoload
(defun inf-clojure-socket-repl (cmd)
"Start a socket REPL server and connects to it via `inf-clojure-connect'.
CMD is the command line instruction used to start the socket
REPL. It should be a string with \"%d\" in it to take a random
port. Set `inf-clojure-custom-startup' or choose from the
defaults provided in `inf-clojure-socket-repl-startup-forms'."
(interactive (list (or (unless current-prefix-arg
inf-clojure-custom-startup)
(completing-read "Select Clojure socket REPL startup command: "
(mapcar #'cdr inf-clojure-socket-repl-startup-forms)
nil
'confirm-after-completion))))
(let* ((host "localhost")
(port (or inf-clojure-socket-repl-port (+ 5500 (random 500))))
(project-dir (clojure-project-dir))
(repl-type (or (unless prefix-arg
inf-clojure-custom-repl-type)
(car (rassoc cmd inf-clojure-socket-repl-startup-forms))
(inf-clojure--prompt-repl-type)))
(project-name (inf-clojure--project-name (or project-dir "standalone")))
(socket-process-name (format "*%s-%s-socket-server*" project-name repl-type))
(socket-buffer (get-buffer-create
(format "*%s-%s-socket*" project-name repl-type)))
(socket-cmd (format cmd port))
(sock (let ((default-directory (or project-dir default-directory)))
(start-file-process-shell-command
socket-process-name socket-buffer
socket-cmd))))
(with-current-buffer socket-buffer
(setq-local
inf-clojure-socket-callback
(lambda ()
(let* ((inf-clojure-custom-repl-type repl-type)
(created-repl-buffer (inf-clojure-connect host port :suppress-message)))
(with-current-buffer (get-buffer created-repl-buffer)
(setq-local inf-clojure-socket-buffer socket-buffer)
(set-process-sentinel
(get-buffer-process (current-buffer))
#'inf-clojure-socket-repl-sentinel))))))
(set-process-filter sock #'inf-clojure-socket-filter)
(message "Starting %s socket REPL server at %s:%d with %s" repl-type host port socket-cmd)))
(defun inf-clojure--forms-without-newlines (str)
"Remove newlines between toplevel forms.
STR is a string of contents to be evaluated. When sending
multiple forms to a REPL, each newline triggers a prompt.
So we replace all newlines between top level forms but not inside
of forms."
(condition-case nil
(with-temp-buffer
(progn
(clojurec-mode)
(insert str)
(whitespace-cleanup)
(goto-char (point-min))
(while (not (eobp))
(while (looking-at "\n")
(delete-char 1))
(unless (eobp)
(clojure-forward-logical-sexp))
(unless (eobp)
(forward-char)))
(buffer-substring-no-properties (point-min) (point-max))))
(scan-error str)))
(defun inf-clojure-eval-region (start end &optional and-go)
"Send the current region to the inferior Clojure process.
Sends substring between START and END. Prefix argument AND-GO
means switch to the Clojure buffer afterwards."
(interactive "r\nP")
(let* ((str (buffer-substring-no-properties start end))