branch: elpa/inf-clojure
commit d04c36c3c61b5f063356d43cc2cbf82243b88fb7
Author: Bozhidar Batsov <bozhi...@batsov.com>
Commit: Bozhidar Batsov <bozhi...@batsov.com>

    [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').
---
 CHANGELOG.md   |  2 ++
 inf-clojure.el | 63 ++++++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 50 insertions(+), 15 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index c72e1a6..15d9707 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,11 +12,13 @@
 * Handle properly ANSI color escape sequences in the REPL.
 * [#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`).
 * [#29](https://github.com/clojure-emacs/inf-clojure/issues/29): Add a command 
to restart the REPL.
+* [#31](https://github.com/clojure-emacs/inf-clojure/issues/31): Invoke 
different init command based on the project type (boot, lein or generic).
 
 ### Changes
 
 * Display the REPL in a different window by default (it used to be displayed 
in the current window).
 * [#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).
+* Removed the `inf-clojure` alias `run-clojure`.
 
 ### Bugs Fixed
 
diff --git a/inf-clojure.el b/inf-clojure.el
index 7a5baa8..b32ec1c 100644
--- a/inf-clojure.el
+++ b/inf-clojure.el
@@ -153,8 +153,28 @@ The following commands are available:
   (add-to-list 'completion-at-point-functions
                #'inf-clojure-completion-at-point))
 
-(defcustom inf-clojure-program "lein repl"
-  "The command used to start an inferior Clojure process in `inf-clojure-mode'.
+(defcustom inf-clojure-lein-cmd "lein repl"
+  "The command used to start a Clojure REPL for Leiningen projects.
+
+Alternative you can specify a TCP connection cons pair, instead
+of command, consisting of a host and port
+number (e.g. (\"localhost\" . 5555)).  That's useful if you're
+often connecting to a remote REPL process."
+  :type '(choice (string)
+                 (cons string integer)))
+
+(defcustom inf-clojure-boot-cmd "boot repl"
+  "The command used to start a Clojure REPL for Boot projects.
+
+Alternative you can specify a TCP connection cons pair, instead
+of command, consisting of a host and port
+number (e.g. (\"localhost\" . 5555)).  That's useful if you're
+often connecting to a remote REPL process."
+  :type '(choice (string)
+                 (cons string integer)))
+
+(defcustom inf-clojure-generic-cmd "lein repl"
+  "The command used to start a Clojure REPL outside Lein/Boot projects.
 
 Alternative you can specify a TCP connection cons pair, instead
 of command, consisting of a host and port
@@ -232,11 +252,12 @@ whichever process buffer you want to use.")
 
 (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.  Variable `inf-clojure-program' controls which Clojure 
interpreter
-is run.  Variables `inf-clojure-prompt', `inf-clojure-filter-regexp' and
-`inf-clojure-load-command' can customize this mode for different Clojure
-interpreters.
+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-command' can
+customize this mode for different Clojure REPLs.
 
 For information on running multiple processes in multiple buffers, see
 documentation for variable `inf-clojure-buffer'.
@@ -331,6 +352,20 @@ Fallback to `default-directory.' if not within a project."
                            inf-clojure-project-root-files)))
       default-directory))
 
+(defun inf-clojure-project-type ()
+  "Determine the type, either leiningen or boot of the current project."
+  (let ((default-directory (inf-clojure-project-root)))
+    (cond ((file-exists-p "project.clj") "lein")
+          ((file-exists-p "build.boot") "boot")
+          (t nil))))
+
+(defun inf-clojure-cmd (project-type)
+  "Determine the command `inf-clojure' needs to invoke for the PROJECT-TYPE."
+  (pcase project-type
+    ("lein" inf-clojure-lein-cmd)
+    ("boot" inf-clojure-boot-cmd)
+    (_ inf-clojure-generic-cmd)))
+
 (defun inf-clojure-clear-repl-buffer ()
   "Clear the REPL buffer."
   (interactive)
@@ -343,18 +378,19 @@ Fallback to `default-directory.' if not within a project."
 If there is a process already running in `*inf-clojure*', just switch
 to that buffer.
 With argument, allows you to edit the command line (default is value
-of `inf-clojure-program').  Runs the hooks from
+of `inf-clojure-*-cmd').  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 (if current-prefix-arg
-                         (read-string "Run Clojure: " inf-clojure-program)
-                       inf-clojure-program)))
+                         (read-string "Run Clojure: " (inf-clojure-cmd 
(inf-clojure-project-type)))
+                       (inf-clojure-cmd (inf-clojure-project-type)))))
   (if (not (comint-check-proc "*inf-clojure*"))
       ;; run the new process in the project's root when in a project folder
       (let ((default-directory (inf-clojure-project-root))
             (cmdlist (if (consp cmd)
                          (list cmd)
                        (split-string cmd))))
+        (message "Starting Clojure REPL via `%s'..." cmd)
         (set-buffer (apply #'make-comint
                            "inf-clojure" (car cmdlist) nil (cdr cmdlist)))
         (inf-clojure-mode)))
@@ -363,9 +399,6 @@ of `inf-clojure-program').  Runs the hooks from
       (pop-to-buffer-same-window "*inf-clojure*")
     (pop-to-buffer "*inf-clojure*")))
 
-;;;###autoload
-(defalias 'run-clojure 'inf-clojure)
-
 (defun inf-clojure-eval-region (start end &optional and-go)
   "Send the current region to the inferior Clojure process.
 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."
              (or pop-up-frames
                  (get-buffer-window inf-clojure-buffer t))))
         (pop-to-buffer inf-clojure-buffer))
-    (run-clojure inf-clojure-program))
+    (inf-clojure (inf-clojure-cmd (inf-clojure-project-type))))
   (when eob-p
     (push-mark)
     (goto-char (point-max))))
@@ -840,7 +873,7 @@ to suppress the usage of the target buffer discovery logic."
          (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)
-    (inf-clojure inf-clojure-program)
+    (inf-clojure (inf-clojure-cmd (inf-clojure-project-type)))
     (rename-buffer target-buffer-name)))
 
 (provide 'inf-clojure)

Reply via email to