Skip to content

Commit fba370e

Browse files
committed
Support REPL flavors and Lumo
This patch adds the inf-clojure-repl-flavor defcustom in order to support different kind of configurations based on the selected flavor. The default flavor is 'clojure, and the other supported one is 'lumo. It also adds defcustoms to further customize hostname, port and classpath used in the Lumo flavor.
1 parent 98b530a commit fba370e

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

inf-clojure.el

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ to load that file."
167167
:type 'regexp
168168
:group 'inf-clojure)
169169

170+
(defcustom inf-clojure-repl-flavor 'clojure
171+
"Symbol to define your Repl flavor.
172+
The default flavor is 'clojure, 'lumo is the other supported
173+
one."
174+
:type 'symbol
175+
:options '(clojure lumo)
176+
:group 'inf-clojure)
177+
170178
(defvar inf-clojure-buffer nil
171179
"The current inf-clojure process buffer.
172180
@@ -208,6 +216,36 @@ whichever process buffer you want to use.")
208216

209217
(put 'inf-clojure-mode 'mode-class 'special)
210218

219+
(defun inf-clojure--flavor-lumo-setup ()
220+
"Setup defcustoms for the Lumo flavor."
221+
;; The defcustoms for the following are already ok:
222+
;; * inf-clojure-set-ns-command
223+
;; * inf-clojure-macroexpand-command
224+
;; * inf-clojure-macroexpand-1-command
225+
;;
226+
;; https://github.com/anmonteiro/lumo/issues/84
227+
;; (setq inf-clojure-var-source-command "(lumo.repl/source %s)")
228+
;; https://github.com/anmonteiro/lumo/issues/87
229+
;; (setq inf-clojure-ns-vars-command "(lumo.repl/dir %s)")
230+
;; https://github.com/anmonteiro/lumo/issues/86
231+
;; (setq inf-clojure-var-apropos-command "(lumo.repl/apropos %s)")
232+
233+
;; Uncomment after https://github.com/anmonteiro/lumo/pull/88
234+
;; (setq inf-clojure-arglist-command "(lumo.repl/get-arglists \"%s\")")
235+
(setq inf-clojure-var-doc-command "(lumo.repl/doc %s)")
236+
(setq inf-clojure-completion-command "(or (doall (map str (lumo.repl/get-completions \"%s\")) '())")
237+
238+
(let ((inf-program (inf-clojure--lumo-program)))
239+
;; (message (concat "inf-clojure-program set to: \"" (prin1-to-string inf-program) "\""))
240+
(setq inf-clojure-program inf-program)))
241+
242+
(defun inf-clojure--flavor-setup ()
243+
"Setup inf-clojure defcustoms depending on the choose flavor."
244+
(pcase inf-clojure-repl-flavor
245+
(lumo (progn (message "[inf-clojure] will switch to the Lumo flavor")
246+
(inf-clojure--flavor-lumo-setup)))
247+
(_ (message "[inf-clojure] will default to the Clojure flavor"))))
248+
211249
(define-derived-mode inf-clojure-mode comint-mode "Inferior Clojure"
212250
"Major mode for interacting with an inferior Clojure process.
213251
Runs a Clojure interpreter as a subprocess of Emacs, with Clojure I/O through an
@@ -322,6 +360,7 @@ With argument, allows you to edit the command line (default is value
322360
of `inf-clojure-program'). Runs the hooks from
323361
`inf-clojure-mode-hook' (after the `comint-mode-hook' is run).
324362
\(Type \\[describe-mode] in the process buffer for a list of commands.)"
363+
(inf-clojure--flavor-setup)
325364
(interactive (list (if current-prefix-arg
326365
(read-string "Run Clojure: " inf-clojure-program)
327366
inf-clojure-program)))
@@ -770,6 +809,77 @@ Return the number of nested sexp the point was over or after."
770809
(setq-local eldoc-documentation-function #'inf-clojure-eldoc)
771810
(apply #'eldoc-add-command inf-clojure-extra-eldoc-commands))
772811

773-
(provide 'inf-clojure)
812+
;;;; Lumo
813+
;;;; ====
774814

815+
(defgroup lumo nil
816+
"Run an external Lumo process (REPL) in an Emacs buffer."
817+
:group 'inf-clojure)
818+
819+
;; AR - TODO Alternatively you can specify a command string that will be called,
820+
;; which should return a string.
821+
(defcustom inf-clojure-lumo-classpath-generator "cp"
822+
"The file used to create the classpath string.
823+
The classpath string has to be a \":\" separated list of dir and
824+
files."
825+
:type '(choice (string))
826+
:group 'lumo)
827+
828+
(defcustom inf-clojure-lumo-command "lumo"
829+
"The command used to launch lumo."
830+
:type '(choice (string))
831+
:group 'lumo)
832+
833+
(defcustom inf-clojure-lumo-args "-d"
834+
"The command arguments used to launch lumo."
835+
:type '(choice (string))
836+
:group 'lumo)
837+
838+
(defcustom inf-clojure-lumo-hostname "localhost"
839+
"The hostname used to launch lumo.
840+
Note that only if port is specified this will be used."
841+
:type '(choice (string))
842+
:group 'lumo)
843+
844+
(defcustom inf-clojure-lumo-port ""
845+
"The port used to launch lumo.
846+
Note that if port is specified, inf-clojure will connect to it by
847+
HTTP socket. No process will be actually started by lumo-mode."
848+
:type '(choice (string)
849+
(integer))
850+
:group 'lumo)
851+
852+
;; AR - not used but left it here because as possible sanity check
853+
(defun inf-clojure--lumo-mode-p ()
854+
"Return true if the lumo is the target REPL."
855+
(comint-send-string (inf-clojure-proc) "(js/global.hasOwnProperty \"$$LUMO_GLOBALS\")"))
856+
857+
(defun inf-clojure--empty-string-p (string)
858+
"Return true if the string is empty or nil. Expects STRING."
859+
(or (null string)
860+
(zerop (length string))))
861+
862+
(defun inf-clojure--read-classpath (classpath-option)
863+
"Read the classpath from the input CLASSPATH-OPTION."
864+
(let ((classpath-file-path (concat (inf-clojure-project-root) classpath-option)))
865+
(cond
866+
((and (file-exists-p classpath-file-path) (file-readable-p classpath-file-path))
867+
(f-read classpath-file-path))
868+
;; TODO launch a command that returns the classpath string?
869+
((user-error (concat "Option \"" classpath-option "\" was not a (readable) file, the classpath will be empty."))
870+
""))))
871+
872+
(defun inf-clojure--lumo-program ()
873+
"Return inf-clojure-program for lumo."
874+
(if (inf-clojure--empty-string-p inf-clojure-lumo-port)
875+
(concat inf-clojure-lumo-command
876+
" "
877+
(when (not (inf-clojure--empty-string-p inf-clojure-lumo-classpath-generator))
878+
(concat inf-clojure-lumo-args " "))
879+
"-c " (inf-clojure--read-classpath inf-clojure-lumo-classpath-generator))
880+
(cons inf-clojure-lumo-hostname (if (stringp inf-clojure-lumo-port)
881+
(string-to-number inf-clojure-lumo-port)
882+
inf-clojure-lumo-port))))
883+
884+
(provide 'inf-clojure)
775885
;;; inf-clojure.el ends here

0 commit comments

Comments
 (0)