Skip to content

Commit f0ae1df

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 other vars to further customize hostname, port and classpath used in the Lumo flavor.
1 parent 98b530a commit f0ae1df

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

inf-clojure.el

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ The following commands are available:
133133
134134
\\{inf-clojure-minor-mode-map}"
135135
:lighter "" :keymap inf-clojure-minor-mode-map
136+
(inf-clojure--flavor-setup)
136137
(inf-clojure-eldoc-setup)
137138
(make-local-variable 'completion-at-point-functions)
138139
(add-to-list 'completion-at-point-functions
@@ -208,6 +209,21 @@ whichever process buffer you want to use.")
208209

209210
(put 'inf-clojure-mode 'mode-class 'special)
210211

212+
(defcustom inf-clojure-repl-flavor 'clojure
213+
"Symbol to define your REPL flavor.
214+
The default flavor is 'clojure, 'lumo is the other supported
215+
one."
216+
:type 'symbol
217+
:options '(clojure lumo)
218+
:group 'inf-clojure)
219+
220+
(defun inf-clojure--flavor-setup ()
221+
"Setup inf-clojure defcustoms depending on the choose flavor."
222+
(pcase inf-clojure-repl-flavor
223+
(lumo (progn (message "[inf-clojure] will switch to the Lumo flavor")
224+
(inf-clojure--flavor-lumo-setup)))
225+
(_ (message "[inf-clojure] will default to the Clojure flavor"))))
226+
211227
(define-derived-mode inf-clojure-mode comint-mode "Inferior Clojure"
212228
"Major mode for interacting with an inferior Clojure process.
213229
Runs a Clojure interpreter as a subprocess of Emacs, with Clojure I/O through an
@@ -559,6 +575,26 @@ The value is nil if it can't find one."
559575
"Return the name of the symbol at point, otherwise nil."
560576
(or (thing-at-point 'symbol) ""))
561577

578+
(defun inf-clojure--empty-string-p (string)
579+
"Return true if the string is empty or nil. Expects STRING."
580+
(or (null string)
581+
(zerop (length string))))
582+
583+
(defun inf-clojure--read-classpath (classpath-option)
584+
"Read the classpath from the input CLASSPATH-OPTION."
585+
(let ((classpath-file-path (concat (inf-clojure-project-root) classpath-option)))
586+
(cond
587+
((and (file-exists-p classpath-file-path) (file-readable-p classpath-file-path))
588+
(f-read classpath-file-path))
589+
;; TODO launch a command that returns the classpath string?
590+
((message (concat "Option \"" classpath-option "\" was not a (readable) file, the classpath will be empty."))
591+
""))))
592+
593+
(defun inf-clojure--maybe-set-program (program)
594+
"Set inf-clojure-program iff it is not a cons cell."
595+
(when (nlistp inf-clojure-program)
596+
(setq inf-clojure-program program)))
597+
562598
;;; Documentation functions: var doc and arglist.
563599
;;; ======================================================================
564600

@@ -770,6 +806,64 @@ Return the number of nested sexp the point was over or after."
770806
(setq-local eldoc-documentation-function #'inf-clojure-eldoc)
771807
(apply #'eldoc-add-command inf-clojure-extra-eldoc-commands))
772808

773-
(provide 'inf-clojure)
809+
;;;; Lumo
810+
;;;; ====
811+
812+
(defgroup lumo nil
813+
"Run an external Lumo process (REPL) in an Emacs buffer."
814+
:group 'inf-clojure)
774815

816+
(defcustom inf-clojure-lumo-command "lumo"
817+
"The command used to launch lumo."
818+
:type 'string
819+
:group 'lumo)
820+
821+
(defcustom inf-clojure-lumo-args "-d"
822+
"The command arguments used to launch lumo."
823+
:type 'string
824+
:group 'lumo)
825+
826+
;; AR - TODO Alternatively you can specify a command string that will be called,
827+
;; which should return a string.
828+
(defcustom inf-clojure-lumo-classpath-generator "cp"
829+
"The file used to create the classpath string.
830+
The classpath string has to be a \":\" separated list of dir and
831+
files."
832+
:type 'string
833+
:group 'lumo)
834+
835+
;; AR - not used but left here because it is a possible sanity check
836+
(defun inf-clojure--lumo-mode-p ()
837+
"Return true if the lumo is the target REPL."
838+
(comint-send-string (inf-clojure-proc) "(js/global.hasOwnProperty \"$$LUMO_GLOBALS\")"))
839+
840+
(defun inf-clojure--lumo-program ()
841+
"Return inf-clojure-program for lumo."
842+
(concat inf-clojure-lumo-command
843+
" "
844+
(when (not (inf-clojure--empty-string-p inf-clojure-lumo-classpath-generator))
845+
(concat inf-clojure-lumo-args " "))
846+
"-c " (inf-clojure--read-classpath inf-clojure-lumo-classpath-generator)))
847+
848+
(defun inf-clojure--flavor-lumo-setup ()
849+
"Setup defcustoms for the Lumo flavor."
850+
;; The defcustoms for the following are already ok:
851+
;; * inf-clojure-set-ns-command
852+
;; * inf-clojure-macroexpand-command
853+
;; * inf-clojure-macroexpand-1-command
854+
;;
855+
;; https://github.com/anmonteiro/lumo/issues/84
856+
;; (setq inf-clojure-var-source-command "(lumo.repl/source %s)")
857+
;; https://github.com/anmonteiro/lumo/issues/87
858+
;; (setq inf-clojure-ns-vars-command "(lumo.repl/dir %s)")
859+
;; https://github.com/anmonteiro/lumo/issues/86
860+
;; (setq inf-clojure-var-apropos-command "(lumo.repl/apropos %s)")
861+
862+
;; Uncomment after https://github.com/anmonteiro/lumo/pull/88
863+
;; (setq inf-clojure-arglist-command "(lumo.repl/get-arglists \"%s\")")
864+
(setq inf-clojure-var-doc-command "(lumo.repl/doc %s)")
865+
(setq inf-clojure-completion-command "(or (doall (map str (lumo.repl/get-completions \"%s\")) '())")
866+
(inf-clojure--maybe-set-program (inf-clojure--lumo-program)))
867+
868+
(provide 'inf-clojure)
775869
;;; inf-clojure.el ends here

0 commit comments

Comments
 (0)