Skip to content

Commit d04c36c

Browse files
committed
[Fix #31] Add basic project type support
Basically this means that we now have different REPL start commands for boot and lein projects. If we're not in a boot or lein project we fallback a generic command to start a REPL (`inf-clojure-generic-cmd').
1 parent 916ec97 commit d04c36c

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
* Handle properly ANSI color escape sequences in the REPL.
1313
* [#41](https://github.com/clojure-emacs/inf-clojure/issues/41): Add a command to quit the REPL (it's bound to `C-c C-q`).
1414
* [#29](https://github.com/clojure-emacs/inf-clojure/issues/29): Add a command to restart the REPL.
15+
* [#31](https://github.com/clojure-emacs/inf-clojure/issues/31): Invoke different init command based on the project type (boot, lein or generic).
1516

1617
### Changes
1718

1819
* Display the REPL in a different window by default (it used to be displayed in the current window).
1920
* [#26](https://github.com/clojure-emacs/inf-clojure/issues/26): Make switching to the REPL optional on `inf-clojure-load-file` (it's now controlled via a prefix argument).
21+
* Removed the `inf-clojure` alias `run-clojure`.
2022

2123
### Bugs Fixed
2224

inf-clojure.el

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,28 @@ The following commands are available:
153153
(add-to-list 'completion-at-point-functions
154154
#'inf-clojure-completion-at-point))
155155

156-
(defcustom inf-clojure-program "lein repl"
157-
"The command used to start an inferior Clojure process in `inf-clojure-mode'.
156+
(defcustom inf-clojure-lein-cmd "lein repl"
157+
"The command used to start a Clojure REPL for Leiningen projects.
158+
159+
Alternative you can specify a TCP connection cons pair, instead
160+
of command, consisting of a host and port
161+
number (e.g. (\"localhost\" . 5555)). That's useful if you're
162+
often connecting to a remote REPL process."
163+
:type '(choice (string)
164+
(cons string integer)))
165+
166+
(defcustom inf-clojure-boot-cmd "boot repl"
167+
"The command used to start a Clojure REPL for Boot projects.
168+
169+
Alternative you can specify a TCP connection cons pair, instead
170+
of command, consisting of a host and port
171+
number (e.g. (\"localhost\" . 5555)). That's useful if you're
172+
often connecting to a remote REPL process."
173+
:type '(choice (string)
174+
(cons string integer)))
175+
176+
(defcustom inf-clojure-generic-cmd "lein repl"
177+
"The command used to start a Clojure REPL outside Lein/Boot projects.
158178
159179
Alternative you can specify a TCP connection cons pair, instead
160180
of command, consisting of a host and port
@@ -232,11 +252,12 @@ whichever process buffer you want to use.")
232252

233253
(define-derived-mode inf-clojure-mode comint-mode "Inferior Clojure"
234254
"Major mode for interacting with an inferior Clojure process.
235-
Runs a Clojure interpreter as a subprocess of Emacs, with Clojure I/O through an
236-
Emacs buffer. Variable `inf-clojure-program' controls which Clojure interpreter
237-
is run. Variables `inf-clojure-prompt', `inf-clojure-filter-regexp' and
238-
`inf-clojure-load-command' can customize this mode for different Clojure
239-
interpreters.
255+
Runs a Clojure interpreter as a subprocess of Emacs, with Clojure
256+
I/O through an Emacs buffer. Variables of the type
257+
`inf-clojure-*-cmd' combined with the project type controls how
258+
a Clojure REPL is started. Variables `inf-clojure-prompt',
259+
`inf-clojure-filter-regexp' and `inf-clojure-load-command' can
260+
customize this mode for different Clojure REPLs.
240261
241262
For information on running multiple processes in multiple buffers, see
242263
documentation for variable `inf-clojure-buffer'.
@@ -331,6 +352,20 @@ Fallback to `default-directory.' if not within a project."
331352
inf-clojure-project-root-files)))
332353
default-directory))
333354

355+
(defun inf-clojure-project-type ()
356+
"Determine the type, either leiningen or boot of the current project."
357+
(let ((default-directory (inf-clojure-project-root)))
358+
(cond ((file-exists-p "project.clj") "lein")
359+
((file-exists-p "build.boot") "boot")
360+
(t nil))))
361+
362+
(defun inf-clojure-cmd (project-type)
363+
"Determine the command `inf-clojure' needs to invoke for the PROJECT-TYPE."
364+
(pcase project-type
365+
("lein" inf-clojure-lein-cmd)
366+
("boot" inf-clojure-boot-cmd)
367+
(_ inf-clojure-generic-cmd)))
368+
334369
(defun inf-clojure-clear-repl-buffer ()
335370
"Clear the REPL buffer."
336371
(interactive)
@@ -343,18 +378,19 @@ Fallback to `default-directory.' if not within a project."
343378
If there is a process already running in `*inf-clojure*', just switch
344379
to that buffer.
345380
With argument, allows you to edit the command line (default is value
346-
of `inf-clojure-program'). Runs the hooks from
381+
of `inf-clojure-*-cmd'). Runs the hooks from
347382
`inf-clojure-mode-hook' (after the `comint-mode-hook' is run).
348383
\(Type \\[describe-mode] in the process buffer for a list of commands.)"
349384
(interactive (list (if current-prefix-arg
350-
(read-string "Run Clojure: " inf-clojure-program)
351-
inf-clojure-program)))
385+
(read-string "Run Clojure: " (inf-clojure-cmd (inf-clojure-project-type)))
386+
(inf-clojure-cmd (inf-clojure-project-type)))))
352387
(if (not (comint-check-proc "*inf-clojure*"))
353388
;; run the new process in the project's root when in a project folder
354389
(let ((default-directory (inf-clojure-project-root))
355390
(cmdlist (if (consp cmd)
356391
(list cmd)
357392
(split-string cmd))))
393+
(message "Starting Clojure REPL via `%s'..." cmd)
358394
(set-buffer (apply #'make-comint
359395
"inf-clojure" (car cmdlist) nil (cdr cmdlist)))
360396
(inf-clojure-mode)))
@@ -363,9 +399,6 @@ of `inf-clojure-program'). Runs the hooks from
363399
(pop-to-buffer-same-window "*inf-clojure*")
364400
(pop-to-buffer "*inf-clojure*")))
365401

366-
;;;###autoload
367-
(defalias 'run-clojure 'inf-clojure)
368-
369402
(defun inf-clojure-eval-region (start end &optional and-go)
370403
"Send the current region to the inferior Clojure process.
371404
Prefix argument AND-GO means switch to the Clojure buffer afterwards."
@@ -426,7 +459,7 @@ With prefix argument EOB-P, positions cursor at end of buffer."
426459
(or pop-up-frames
427460
(get-buffer-window inf-clojure-buffer t))))
428461
(pop-to-buffer inf-clojure-buffer))
429-
(run-clojure inf-clojure-program))
462+
(inf-clojure (inf-clojure-cmd (inf-clojure-project-type))))
430463
(when eob-p
431464
(push-mark)
432465
(goto-char (point-max))))
@@ -840,7 +873,7 @@ to suppress the usage of the target buffer discovery logic."
840873
(target-buffer-name (buffer-name target-buffer)))
841874
;; TODO: Try to recycle the old buffer instead of killing and recreating it
842875
(inf-clojure-quit target-buffer)
843-
(inf-clojure inf-clojure-program)
876+
(inf-clojure (inf-clojure-cmd (inf-clojure-project-type)))
844877
(rename-buffer target-buffer-name)))
845878

846879
(provide 'inf-clojure)

0 commit comments

Comments
 (0)