Skip to content

Commit a61dd84

Browse files
dpsuttonbbatsov
authored andcommitted
Insert top level defun into repl
Nice interaction to see the form and then the output.
1 parent 49390d9 commit a61dd84

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

inf-clojure.el

+33-4
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword
135135
(define-key map "\C-c\C-r" #'inf-clojure-eval-region)
136136
(define-key map "\C-c\M-r" #'inf-clojure-reload)
137137
(define-key map "\C-c\C-n" #'inf-clojure-eval-form-and-next)
138+
(define-key map (kbd "C-c C-j") #'inf-clojure-insert-defun)
138139
(define-key map "\C-c\C-z" #'inf-clojure-switch-to-repl)
139140
(define-key map "\C-c\C-i" #'inf-clojure-show-ns-vars)
140141
(define-key map (kbd "C-c C-S-a") #'inf-clojure-apropos)
@@ -682,15 +683,25 @@ Prefix argument AND-GO means switch to the Clojure buffer afterwards."
682683
"Send the string CODE to the inferior Clojure process to be executed."
683684
(inf-clojure--send-string (inf-clojure-proc) code))
684685

686+
(defun inf-clojure--defun-at-point (&optional bounds)
687+
"Return text or range of defun at point.
688+
If BOUNDS is truthy return a dotted pair of beginning and end of
689+
current defun else return the string.."
690+
(save-excursion
691+
(end-of-defun)
692+
(let ((end (point))
693+
(case-fold-search t)
694+
(func (if bounds #'cons #'buffer-substring-no-properties)))
695+
(beginning-of-defun)
696+
(funcall func (point) end))))
697+
685698
(defun inf-clojure-eval-defun (&optional and-go)
686699
"Send the current defun to the inferior Clojure process.
687700
Prefix argument AND-GO means switch to the Clojure buffer afterwards."
688701
(interactive "P")
689702
(save-excursion
690-
(end-of-defun)
691-
(let ((end (point)) (case-fold-search t))
692-
(beginning-of-defun)
693-
(inf-clojure-eval-region (point) end and-go))))
703+
(let ((bounds (inf-clojure--defun-at-point t)))
704+
(inf-clojure-eval-region (car bounds) (cdr bounds) and-go))))
694705

695706
(defun inf-clojure-eval-buffer (&optional and-go)
696707
"Send the current buffer to the inferior Clojure process.
@@ -731,6 +742,24 @@ With prefix argument EOB-P, positions cursor at end of buffer."
731742
(push-mark)
732743
(goto-char (point-max))))
733744

745+
(defun inf-clojure-insert-and-eval (form)
746+
"Insert FORM into process and evaluate.
747+
Indent FORM. FORM is expected to have been trimmed."
748+
(let ((clojure-process (inf-clojure-proc)))
749+
(with-current-buffer (process-buffer clojure-process)
750+
(comint-goto-process-mark)
751+
(let ((beginning (point)))
752+
(insert (format "%s" form))
753+
(let ((end (point)))
754+
(goto-char beginning)
755+
(indent-sexp end)))
756+
(comint-send-input t))))
757+
758+
(defun inf-clojure-insert-defun ()
759+
"Send current defun to process."
760+
(interactive)
761+
(inf-clojure-insert-and-eval (string-trim (inf-clojure--defun-at-point))))
762+
734763

735764
;;; Now that inf-clojure-eval-/defun/region takes an optional prefix arg,
736765
;;; these commands are redundant. But they are kept around for the user

0 commit comments

Comments
 (0)