2017-06-08 11:47:56 +02:00
|
|
|
;;; feature/eval/autoload/repl.el -*- lexical-binding: t; -*-
|
2017-02-13 16:57:08 -05:00
|
|
|
|
2018-09-11 21:24:56 +01:00
|
|
|
(defvar +eval-repl-buffers (make-hash-table :test 'equal)
|
2017-03-04 18:28:51 -05:00
|
|
|
"The buffer of the last open repl.")
|
2017-02-13 16:57:08 -05:00
|
|
|
|
2018-05-14 01:22:33 +02:00
|
|
|
(define-minor-mode +eval-repl-mode
|
|
|
|
"A minor mode for REPL buffers.")
|
|
|
|
|
2019-02-18 01:56:38 -05:00
|
|
|
(defun +eval--ensure-in-repl-buffer (&optional command other-window-p)
|
2018-09-11 21:24:56 +01:00
|
|
|
(maphash (lambda (key buffer)
|
|
|
|
(unless (buffer-live-p buffer)
|
|
|
|
(remhash key +eval-repl-buffers)))
|
|
|
|
+eval-repl-buffers)
|
2018-09-28 13:54:20 -04:00
|
|
|
(let* ((project-root (doom-project-root))
|
2018-07-01 01:10:51 +02:00
|
|
|
(key (cons major-mode project-root))
|
2018-09-11 21:24:56 +01:00
|
|
|
(buffer (gethash key +eval-repl-buffers)))
|
|
|
|
(cl-check-type buffer (or buffer null))
|
|
|
|
(unless (eq buffer (current-buffer))
|
2019-02-18 01:56:38 -05:00
|
|
|
(funcall (if other-window-p #'pop-to-buffer #'switch-to-buffer)
|
2018-07-01 01:10:51 +02:00
|
|
|
(if (buffer-live-p buffer)
|
|
|
|
buffer
|
2018-07-16 14:06:08 +02:00
|
|
|
(setq buffer
|
|
|
|
(save-window-excursion
|
|
|
|
(if (commandp command)
|
|
|
|
(call-interactively command)
|
|
|
|
(funcall command))))
|
2018-07-01 01:10:51 +02:00
|
|
|
(unless (bufferp buffer)
|
|
|
|
(error "REPL command didn't return a buffer"))
|
|
|
|
(with-current-buffer buffer (+eval-repl-mode +1))
|
2018-09-11 21:24:56 +01:00
|
|
|
(puthash key buffer +eval-repl-buffers)
|
2018-07-01 01:10:51 +02:00
|
|
|
buffer)))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(goto-char (if (and (derived-mode-p 'comint-mode)
|
|
|
|
(cdr comint-last-prompt))
|
|
|
|
(cdr comint-last-prompt)
|
|
|
|
(point-max)))
|
|
|
|
buffer)))
|
2017-05-12 14:18:00 +02:00
|
|
|
|
2019-02-18 01:56:38 -05:00
|
|
|
(defun +eval-open-repl (prompt-p other-window-p)
|
|
|
|
(let ((command (cdr (assq major-mode +eval-repls))))
|
|
|
|
(when (or (not command) prompt-p)
|
|
|
|
(let* ((choices (or (cl-loop for sym being the symbols
|
|
|
|
for sym-name = (symbol-name sym)
|
|
|
|
if (string-match "^\\(?:\\+\\)?\\([^/]+\\)/open-\\(?:\\(.+\\)-\\)?repl$" sym-name)
|
|
|
|
collect
|
|
|
|
(format "%s (%s)"
|
|
|
|
(match-string-no-properties 1 sym-name)
|
|
|
|
(or (match-string-no-properties 2 sym-name) "default")))
|
|
|
|
(user-error "There are no known available REPLs")))
|
|
|
|
(choice (or (completing-read "Open a REPL for: " choices)
|
|
|
|
(user-error "Aborting")))
|
|
|
|
(choice-split (split-string choice " " t))
|
|
|
|
(module (car choice-split))
|
|
|
|
(repl (substring (cadr choice-split) 1 -1)))
|
|
|
|
(setq command
|
|
|
|
(intern-soft
|
|
|
|
(format "+%s/open-%srepl" module
|
|
|
|
(if (string= repl "default")
|
|
|
|
""
|
|
|
|
repl))))))
|
|
|
|
(unless (commandp command)
|
|
|
|
(error "Couldn't find a valid REPL for %s" major-mode))
|
|
|
|
(when (+eval--ensure-in-repl-buffer command other-window-p)
|
|
|
|
(when (bound-and-true-p evil-mode)
|
|
|
|
(call-interactively #'evil-append-line))
|
|
|
|
t)))
|
|
|
|
|
2017-02-13 16:57:08 -05:00
|
|
|
;;;###autoload
|
2019-02-18 01:56:38 -05:00
|
|
|
(defun +eval/open-repl-same-window (&optional arg)
|
2017-05-12 14:18:00 +02:00
|
|
|
"Opens (or reopens) the REPL associated with the current major-mode and place
|
2018-01-24 00:57:52 -05:00
|
|
|
the cursor at the prompt.
|
|
|
|
|
2019-02-18 01:56:38 -05:00
|
|
|
If ARG (universal argument), prompt for a specific REPL to open."
|
|
|
|
(interactive "P")
|
|
|
|
(+eval-open-repl arg))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eval/open-repl-other-window (&optional arg)
|
|
|
|
"Does `+eval/open-repl', but in a popup window.
|
|
|
|
|
|
|
|
If ARG (universal argument), prompt for a specific REPL to open."
|
2018-01-24 00:57:52 -05:00
|
|
|
(interactive "P")
|
2019-02-18 01:56:38 -05:00
|
|
|
(+eval-open-repl arg t))
|
2017-02-13 16:57:08 -05:00
|
|
|
|
2017-03-04 18:28:51 -05:00
|
|
|
;;;###autoload
|
2017-10-03 02:49:08 +02:00
|
|
|
(defun +eval/send-region-to-repl (beg end &optional auto-execute-p)
|
2017-05-12 14:18:00 +02:00
|
|
|
"REPL must be open! Sends a selected region to it. If AUTO-EXECUTE-P, then
|
|
|
|
execute it immediately after."
|
2017-03-04 18:28:51 -05:00
|
|
|
(interactive "r")
|
2017-05-12 14:18:00 +02:00
|
|
|
(let ((selection (buffer-substring-no-properties beg end)))
|
|
|
|
(unless (+eval--ensure-in-repl-buffer)
|
|
|
|
(error "No REPL open"))
|
2017-06-08 11:47:56 +02:00
|
|
|
(when (bound-and-true-p evil-mode)
|
|
|
|
(call-interactively #'evil-append-line))
|
2017-05-12 14:18:00 +02:00
|
|
|
(insert (string-trim selection))
|
|
|
|
(when auto-execute-p
|
|
|
|
;; I don't use `comint-send-input' because different REPLs may have their
|
|
|
|
;; own. So I just emulate the keypress.
|
|
|
|
(execute-kbd-macro (kbd "RET")))))
|