summaryrefslogtreecommitdiff
path: root/cider-mode.el
diff options
context:
space:
mode:
authorBozhidar Batsov <bozhidar@batsov.com>2018-06-22 20:35:19 +0300
committerBozhidar Batsov <bozhidar@batsov.com>2018-06-22 20:35:19 +0300
commit6d4a5a81900387eb5ca67ae9b4e9787ae1ee7fe6 (patch)
tree85c6d29a9b55db26ba1d8ed5eae863e18297d177 /cider-mode.el
parentbb47916ba4741fe6e2a8a548a61eb11c97344b8a (diff)
[#2203] Move the insert and eval functionality out of
cider-interaction.el It's pretty self-contained and it's used only in cider-mode.el.
Diffstat (limited to 'cider-mode.el')
-rw-r--r--cider-mode.el74
1 files changed, 74 insertions, 0 deletions
diff --git a/cider-mode.el b/cider-mode.el
index 94f46627..f0a88de3 100644
--- a/cider-mode.el
+++ b/cider-mode.el
@@ -170,6 +170,80 @@ the related commands `cider-repl-clear-buffer' and
(cider-repl-clear-output))
(switch-to-buffer origin-buffer)))
+;;; Insert (and eval) in REPL functionality
+(defvar cider-insert-commands-map
+ (let ((map (define-prefix-command 'cider-insert-commands-map)))
+ ;; single key bindings defined last for display in menu
+ (define-key map (kbd "e") #'cider-insert-last-sexp-in-repl)
+ (define-key map (kbd "d") #'cider-insert-defun-in-repl)
+ (define-key map (kbd "r") #'cider-insert-region-in-repl)
+ (define-key map (kbd "n") #'cider-insert-ns-form-in-repl)
+
+ ;; duplicates with C- for convenience
+ (define-key map (kbd "C-e") #'cider-insert-last-sexp-in-repl)
+ (define-key map (kbd "C-d") #'cider-insert-defun-in-repl)
+ (define-key map (kbd "C-r") #'cider-insert-region-in-repl)
+ (define-key map (kbd "C-n") #'cider-insert-ns-form-in-repl)))
+
+(defcustom cider-switch-to-repl-after-insert-p t
+ "Whether to switch to the repl after inserting a form into the repl."
+ :type 'boolean
+ :group 'cider
+ :package-version '(cider . "0.18.0"))
+
+(defcustom cider-invert-insert-eval-p nil
+ "Whether to invert the behavior of evaling.
+Default behavior when inserting is to NOT eval the form and only eval with
+a prefix. This allows to invert this so that default behavior is to insert
+and eval and the prefix is required to prevent evaluation."
+ :type 'boolean
+ :group 'cider
+ :package-version '(cider . "0.18.0"))
+
+(defun cider-insert-in-repl (form eval)
+ "Insert FORM in the REPL buffer and switch to it.
+If EVAL is non-nil the form will also be evaluated."
+ (while (string-match "\\`[ \t\n\r]+\\|[ \t\n\r]+\\'" form)
+ (setq form (replace-match "" t t form)))
+ (with-current-buffer (cider-current-repl)
+ (goto-char (point-max))
+ (let ((beg (point)))
+ (insert form)
+ (indent-region beg (point)))
+ (when (if cider-invert-insert-eval-p
+ (not eval)
+ eval)
+ (cider-repl-return)))
+ (when cider-switch-to-repl-after-insert-p
+ (cider-switch-to-repl-buffer)))
+
+(defun cider-insert-last-sexp-in-repl (&optional arg)
+ "Insert the expression preceding point in the REPL buffer.
+If invoked with a prefix ARG eval the expression after inserting it."
+ (interactive "P")
+ (cider-insert-in-repl (cider-last-sexp) arg))
+
+(defun cider-insert-defun-in-repl (&optional arg)
+ "Insert the top level form at point in the REPL buffer.
+If invoked with a prefix ARG eval the expression after inserting it."
+ (interactive "P")
+ (cider-insert-in-repl (cider-defun-at-point) arg))
+
+(defun cider-insert-region-in-repl (start end &optional arg)
+ "Insert the curent region in the REPL buffer.
+START and END represent the region's boundaries.
+If invoked with a prefix ARG eval the expression after inserting it."
+ (interactive "rP")
+ (cider-insert-in-repl
+ (buffer-substring-no-properties start end) arg))
+
+(defun cider-insert-ns-form-in-repl (&optional arg)
+ "Insert the current buffer's ns form in the REPL buffer.
+If invoked with a prefix ARG eval the expression after inserting it."
+ (interactive "P")
+ (cider-insert-in-repl (cider-ns-form) arg))
+
+
;;; The menu-bar
(defconst cider-mode-menu