tools/eval: don't select window after send-to-repl #1941
Also: - Refactors REPL logic - Open-repl commands now copy selection to new REPL and select them (without executing them). - Send-to-repl now auto-executes by default. Prefix arg = don't auto execute
This commit is contained in:
parent
cf2cea75cf
commit
cfcaad4bfc
1 changed files with 28 additions and 20 deletions
|
@ -6,7 +6,7 @@
|
||||||
(define-minor-mode +eval-repl-mode
|
(define-minor-mode +eval-repl-mode
|
||||||
"A minor mode for REPL buffers.")
|
"A minor mode for REPL buffers.")
|
||||||
|
|
||||||
(defun +eval--ensure-in-repl-buffer (&optional command other-window-p)
|
(defun +eval--ensure-in-repl-buffer (&optional command displayfn)
|
||||||
(maphash (lambda (key buffer)
|
(maphash (lambda (key buffer)
|
||||||
(unless (buffer-live-p buffer)
|
(unless (buffer-live-p buffer)
|
||||||
(remhash key +eval-repl-buffers)))
|
(remhash key +eval-repl-buffers)))
|
||||||
|
@ -16,7 +16,7 @@
|
||||||
(buffer (gethash key +eval-repl-buffers)))
|
(buffer (gethash key +eval-repl-buffers)))
|
||||||
(cl-check-type buffer (or buffer null))
|
(cl-check-type buffer (or buffer null))
|
||||||
(unless (eq buffer (current-buffer))
|
(unless (eq buffer (current-buffer))
|
||||||
(funcall (if other-window-p #'pop-to-buffer #'switch-to-buffer)
|
(funcall (or displayfn #'get-buffer-create)
|
||||||
(if (buffer-live-p buffer)
|
(if (buffer-live-p buffer)
|
||||||
buffer
|
buffer
|
||||||
(setq buffer
|
(setq buffer
|
||||||
|
@ -41,8 +41,11 @@
|
||||||
(point-max))))
|
(point-max))))
|
||||||
buffer)))
|
buffer)))
|
||||||
|
|
||||||
(defun +eval-open-repl (prompt-p &optional other-window-p)
|
(defun +eval-open-repl (prompt-p &optional displayfn)
|
||||||
(let ((command (cdr (assq major-mode +eval-repls))))
|
(let ((command (cdr (assq major-mode +eval-repls)))
|
||||||
|
(region (if (use-region-p)
|
||||||
|
(buffer-substring-no-properties (region-beginning)
|
||||||
|
(region-end)))))
|
||||||
(when (or (not command) prompt-p)
|
(when (or (not command) prompt-p)
|
||||||
(let* ((choices (or (cl-loop for sym being the symbols
|
(let* ((choices (or (cl-loop for sym being the symbols
|
||||||
for sym-name = (symbol-name sym)
|
for sym-name = (symbol-name sym)
|
||||||
|
@ -65,9 +68,11 @@
|
||||||
repl))))))
|
repl))))))
|
||||||
(unless (commandp command)
|
(unless (commandp command)
|
||||||
(error "Couldn't find a valid REPL for %s" major-mode))
|
(error "Couldn't find a valid REPL for %s" major-mode))
|
||||||
(when (+eval--ensure-in-repl-buffer command other-window-p)
|
(with-current-buffer (+eval--ensure-in-repl-buffer command displayfn)
|
||||||
(when (bound-and-true-p evil-mode)
|
(when (bound-and-true-p evil-mode)
|
||||||
(call-interactively #'evil-append-line))
|
(call-interactively #'evil-append-line))
|
||||||
|
(when region
|
||||||
|
(insert region))
|
||||||
t)))
|
t)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
|
@ -77,7 +82,7 @@ the cursor at the prompt.
|
||||||
|
|
||||||
If ARG (universal argument), prompt for a specific REPL to open."
|
If ARG (universal argument), prompt for a specific REPL to open."
|
||||||
(interactive "P")
|
(interactive "P")
|
||||||
(+eval-open-repl arg))
|
(+eval-open-repl arg #'switch-to-buffer))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +eval/open-repl-other-window (&optional arg)
|
(defun +eval/open-repl-other-window (&optional arg)
|
||||||
|
@ -85,20 +90,23 @@ If ARG (universal argument), prompt for a specific REPL to open."
|
||||||
|
|
||||||
If ARG (universal argument), prompt for a specific REPL to open."
|
If ARG (universal argument), prompt for a specific REPL to open."
|
||||||
(interactive "P")
|
(interactive "P")
|
||||||
(+eval-open-repl arg t))
|
(+eval-open-repl arg #'pop-to-buffer))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +eval/send-region-to-repl (beg end &optional auto-execute-p)
|
(defun +eval/send-region-to-repl (beg end &optional inhibit-auto-execute-p)
|
||||||
"REPL must be open! Sends a selected region to it. If AUTO-EXECUTE-P, then
|
"Execute the selected region in the REPL.
|
||||||
execute it immediately after."
|
Opens a REPL if one isn't already open. If AUTO-EXECUTE-P, then execute it
|
||||||
(interactive "r")
|
immediately after."
|
||||||
(let ((selection (buffer-substring-no-properties beg end)))
|
(interactive "rP")
|
||||||
(unless (+eval--ensure-in-repl-buffer)
|
(let ((selection (buffer-substring-no-properties beg end))
|
||||||
|
(buffer (+eval--ensure-in-repl-buffer)))
|
||||||
|
(unless buffer
|
||||||
(error "No REPL open"))
|
(error "No REPL open"))
|
||||||
(when (bound-and-true-p evil-mode)
|
(with-selected-window (get-buffer-window buffer)
|
||||||
(call-interactively #'evil-append-line))
|
(when (bound-and-true-p evil-local-mode)
|
||||||
(insert (string-trim selection))
|
(call-interactively #'evil-append-line))
|
||||||
(when auto-execute-p
|
(insert (string-trim selection))
|
||||||
;; I don't use `comint-send-input' because different REPLs may have their
|
(unless inhibit-auto-execute-p
|
||||||
;; own. So I just emulate the keypress.
|
;; `comint-send-input' isn't enough because some REPLs may not use
|
||||||
(execute-kbd-macro (kbd "RET")))))
|
;; comint, so just emulate the keypress.
|
||||||
|
(execute-kbd-macro (kbd "RET"))))))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue