Skip to content

Commit 447f89f

Browse files
committed
Set REPL type from startup form, prompt, or defcustom at startup
1 parent fe22ce2 commit 447f89f

File tree

4 files changed

+55
-49
lines changed

4 files changed

+55
-49
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### New features
6+
7+
* Set REPL type from startup form or prompt at startup, introduce `inf-clojure-custom-repl-type` defcustom
8+
59
## 2.2.0 (2020-04-15)
610

711
### New features

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ for the symbol you want to show the docstring for.
136136

137137
## Configuration
138138

139+
## Most Common Configuration
140+
141+
Most likely you will want to set the startup command and the repl
142+
type. This is most easily set with the follow dir-locals
143+
144+
```emacs-lisp
145+
((nil
146+
(inf-clojure-custom-startup . "clojure -A:compliment")
147+
(inf-clojure-custom-repl-type . clojure)))
148+
```
149+
150+
There are two important commands here:
151+
1. `inf-clojure-custom-startup`: Which startup command to use so
152+
inf-clojure can run the inferior process and
153+
2. `inf-clojure-custom-repl-type`: Which repl type it is so
154+
inf-clojure knows how to format commands to the repl
155+
156+
### All Configuration
139157
**Note:** The configuration options were changed massively in `inf-clojure` 3.0.
140158

141159
In the time-honoured Emacs tradition `inf-clojure`'s behaviour is extremely

inf-clojure.el

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@
7171
(require 'subr-x)
7272
(require 'seq)
7373

74-
(defvar inf-clojure-recognize-alist '((lumo . "lumo.repl")
75-
(planck . "planck.repl")
76-
;; cljs goes after the selfhosts
77-
(cljs . "cljs.repl")
78-
(joker . "joker.repl")
79-
(babashka . "babashka.classpath")
80-
(clojure . "clojure.core.server")))
81-
8274
(defvar inf-clojure-startup-forms '((lein . "lein repl")
8375
(boot . "boot repl")
8476
(clojure . "clojure")
@@ -217,37 +209,13 @@ either `setq-local` or an entry in `.dir-locals.el`." )
217209
"Global lock for protecting against proc filter race conditions.
218210
See http://blog.jorgenschaefer.de/2014/05/race-conditions-in-emacs-process-filter.html")
219211

220-
(defun inf-clojure--detect-repl-type (proc)
221-
"Identifies the current REPL type for PROC."
222-
(when (not inf-clojure--repl-type-lock)
223-
(let ((inf-clojure--repl-type-lock t))
224-
(or (seq-some (lambda (r)
225-
(when (inf-clojure--some-response-p
226-
proc (format "(find-ns '%s)" (cdr r)))
227-
(car r)))
228-
inf-clojure-recognize-alist)
229-
'clojure))))
230-
231-
(defun inf-clojure-set-repl-type ()
212+
(defun inf-clojure--prompt-repl-type ()
232213
"Set the REPL type to one of the available implementations."
233214
(interactive)
234-
(let* ((proc (inf-clojure-proc))
235-
(types (mapcar #'car inf-clojure-repl-features))
236-
(type-to-set (intern
237-
(completing-read "Set REPL type:"
238-
(sort (mapcar #'symbol-name types) #'string-lessp)))))
239-
(with-current-buffer (process-buffer proc)
240-
(setq-local inf-clojure-repl-type type-to-set))))
241-
242-
(defun inf-clojure--set-repl-type (proc)
243-
"Set the REPL type if has not already been set.
244-
It requires a REPL PROC for inspecting the correct type."
245-
;; todo: don't like this happening so frequently
246-
(with-current-buffer (process-buffer proc)
247-
(if (not inf-clojure-repl-type)
248-
(let ((repl-type (inf-clojure--detect-repl-type proc)))
249-
(setq-local inf-clojure-repl-type repl-type))
250-
inf-clojure-repl-type)))
215+
(let ((types (mapcar #'car inf-clojure-repl-features)))
216+
(intern
217+
(completing-read "Set REPL type:"
218+
(sort (mapcar #'symbol-name types) #'string-lessp)))))
251219

252220
(defgroup inf-clojure nil
253221
"Run an external Clojure process (REPL) in an Emacs buffer."
@@ -390,6 +358,19 @@ Can be a cons pair of (host . port) where host is a string and
390358
port is an integer, or a string to startup an interpreter like
391359
\"planck\".")
392360

361+
(defcustom inf-clojure-custom-repl-type
362+
nil
363+
"REPL type to use for inf-clojure process buffer.
364+
Should be a symbol that is a key in `inf-clojure-repl-features'."
365+
:package-version '(inf-clojure . "3.0.0")
366+
:type '(choice (const :tag "clojure" clojure)
367+
(const :tag "cljs" cljs)
368+
(const :tag "lumo" lumo)
369+
(const :tag "planck" planck)
370+
(const :tag "joker" joker)
371+
(const :tag "babashka" babashka)
372+
(const :tag "determine at startup" nil)))
373+
393374
(defun inf-clojure--whole-comment-line-p (string)
394375
"Return non-nil iff STRING is a whole line semicolon comment."
395376
(string-match-p "^\s*;" string))
@@ -424,7 +405,6 @@ always be preferred over `comint-send-string`. It delegates to
424405
`comint-simple-send` so it always appends a newline at the end of
425406
the string for evaluation. Refer to `comint-simple-send` for
426407
customizations."
427-
(inf-clojure--set-repl-type proc)
428408
(let ((sanitized (inf-clojure--sanitize-command string)))
429409
(inf-clojure--log-string sanitized "----CMD->")
430410
(comint-send-string proc sanitized)))
@@ -449,7 +429,6 @@ Clojure to load that file."
449429
"Return the form to query the Inf-Clojure PROC for reloading a namespace.
450430
If you are using REPL types, it will pickup the most appropriate
451431
`inf-clojure-reload-form` variant."
452-
(inf-clojure--set-repl-type proc)
453432
inf-clojure-reload-form)
454433

455434
(defcustom inf-clojure-reload-all-form "(require '%s :reload-all)"
@@ -468,7 +447,6 @@ Clojure to load that file."
468447
"Return the form to query the Inf-Clojure PROC for :reload-all of a namespace.
469448
If you are using REPL types, it will pickup the most appropriate
470449
`inf-clojure-reload-all-form` variant."
471-
(inf-clojure--set-repl-type proc)
472450
inf-clojure-reload-all-form)
473451

474452
(defcustom inf-clojure-prompt "^[^=> \n]+=> *"
@@ -698,11 +676,15 @@ run).
698676
(let ((default-directory (inf-clojure-project-root))
699677
(cmdlist (if (consp cmd)
700678
(list cmd)
701-
(split-string cmd))))
679+
(split-string cmd)))
680+
(repl-type (or inf-clojure-custom-repl-type
681+
(car (rassoc cmd inf-clojure-startup-forms))
682+
(inf-clojure--prompt-repl-type))))
702683
(message "Starting Clojure REPL via `%s'..." cmd)
703684
(with-current-buffer (apply #'make-comint
704685
"inf-clojure" (car cmdlist) nil (cdr cmdlist))
705686
(inf-clojure-mode)
687+
(setq-local inf-clojure-repl-type repl-type)
706688
(hack-dir-local-variables-non-file-buffer))))
707689
(setq inf-clojure-buffer "*inf-clojure*")
708690
(if inf-clojure-repl-use-same-window

todo.org

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
* Core
22

3-
** TODO set repl type on connection not first command
3+
** DONE set repl type on connection not first command
44
For some reason ~inf-clojure--set-repl-type~ is called in:
55
1. inf-clojure--send-string
66
2. inf-clojure-reload-form
@@ -11,13 +11,6 @@ Seems better to do this on the two different connection methods and then be done
1111
** DONE do we need repl type in both source buffer and connection?
1212
these can get out of sync and lead to confusing errors when closing a repl and opening a new one. It seems like we keep the repl-type in the source buffer to prevent a single ~(with-current-buffer (process-buffer proc) inf-clojure-repl-type)~
1313

14-
** TODO nice startup
15-
There's some project detection but that's becoming less and less useful as time goes on. Shadow, lein, deps.edn can all easily be mixed in the same project. And then lumo, planck, or bb scripts could live side by side. Rather than trying to guess the project type, I think i'd like to mimic geiser's style of handling multiple scheme backends. Perhaps ~m-x inf-clojure-run-planck~ and similar could help out.
16-
17-
Some considerations:
18-
- is this path aware? IE, don't show an option to run planck, lumo, etc, if they aren't visible or installed?
19-
- should it have a rebuild function so that user registered implementations can show up in the ~m-x~ menu as well?
20-
2114
** DONE Better dispatch for the implementations
2215
Right now the functions are kinda clunky cond statements:
2316
#+BEGIN_SRC emacs-lisp
@@ -65,6 +58,9 @@ The source primitive is quite nice but we most likely need a way to navigate to
6558

6659
** TODO PREPL
6760
Be nice to implement this now that we have parseedn in elisp to understand edn.
61+
62+
** TODO inhibit custom repl-type and startup form
63+
its nice to have these in dir-locals to just start up. but if you normally have ~clojure -m cljs.main -r~ as the startup command but you want to crank up a clj repl there's no way without removing those dir locals.
6864
* Nice-to-haves
6965
** TODO Put repl type in modeline
7066
Rather than just ~*inf-clojure*~ we could put the repl type. Make it easy to follow and makes it easy to see when it gets it wrong.
@@ -81,3 +77,9 @@ Seems a bit heavy handed but its working for me so far.
8177

8278
** TODO is disabling color still required?
8379
in the readme it mentions that color should be turned off. in my usage I haven't run into this problem at all. perhaps no longer true?
80+
** TODO nice startup
81+
There's some project detection but that's becoming less and less useful as time goes on. Shadow, lein, deps.edn can all easily be mixed in the same project. And then lumo, planck, or bb scripts could live side by side. Rather than trying to guess the project type, I think i'd like to mimic geiser's style of handling multiple scheme backends. Perhaps ~m-x inf-clojure-run-planck~ and similar could help out.
82+
83+
Some considerations:
84+
- is this path aware? IE, don't show an option to run planck, lumo, etc, if they aren't visible or installed?
85+
- should it have a rebuild function so that user registered implementations can show up in the ~m-x~ menu as well?

0 commit comments

Comments
 (0)