Fix REPL functionality; add :repl ex command

This commit is contained in:
Henrik Lissner 2017-05-12 14:18:00 +02:00
parent 7fbaf2ced1
commit de3853cde5
5 changed files with 52 additions and 23 deletions

View file

@ -100,10 +100,10 @@
+ [ ] tools/upload: add ~+upload/open-remote-file~ command to open current file on the remote (with TRAMP) + [ ] tools/upload: add ~+upload/open-remote-file~ command to open current file on the remote (with TRAMP)
+ [ ] Generalize ~doom-visual-bell~ by basing its background off a face + [ ] Generalize ~doom-visual-bell~ by basing its background off a face
** 2.0.2 [63/66] ** 2.0.2 [64/66]
+ [ ] Update screenshots + [ ] Update screenshots
+ [ ] send-to-REPL workflow: does it still work? (see ~:repl~ & ~+eval/repl-send-region~)
+ [ ] Fix ~0/0~ displaying in modeline (caused by leftover anzu state) + [ ] Fix ~0/0~ displaying in modeline (caused by leftover anzu state)
+ [X] send-to-REPL workflow: does it still work? (see ~:repl~ & ~+eval/repl-send-region~)
+ [X] completion/ivy: restore ag searching (for compressed files) + [X] completion/ivy: restore ag searching (for compressed files)
Now ~:ag~, ~:rg~, ~:agcwd~ and ~:rgcwd~ Now ~:ag~, ~:rg~, ~:agcwd~ and ~:rgcwd~
+ [X] ~add-transient-hook!~: add support for appending + [X] ~add-transient-hook!~: add support for appending

View file

@ -12,3 +12,11 @@
:move-point nil :move-point nil
(interactive "<r>") (interactive "<r>")
(+eval/region-and-replace beg end)) (+eval/region-and-replace beg end))
;;;###autoload (autoload '+eval:repl "feature/eval/autoload/evil" nil t)
(evil-define-operator +eval:repl (beg end &optional bang)
:move-point nil
(interactive "<r><!>")
(if (evil-normal-state-p)
(+eval/repl)
(+eval/repl-send-region beg end bang)))

View file

@ -1,30 +1,51 @@
;;; feature/eval/autoload/repl.el ;;; feature/eval/autoload/repl.el
(defvar +eval-last-repl-buffer nil (defvar +eval-repl-buffer nil
"The buffer of the last open repl.") "The buffer of the last open repl.")
;;;###autoload (defun +eval--ensure-in-repl-buffer (&optional command)
(defun +eval/repl () (if (eq (current-buffer) +eval-repl-buffer)
"Open the REPL associated with the current major-mode. If selection is active, t
send region to repl." (if (and +eval-repl-buffer (buffer-live-p +eval-repl-buffer))
(interactive) (if-let (win (get-buffer-window +eval-repl-buffer))
(when-let (command (cdr (assq major-mode +eval-repls))) (select-window win)
(let ((repl-buffer (save-window-excursion (funcall command)))) (doom-popup-buffer +eval-repl-buffer))
(when command
(let ((repl-buffer (save-window-excursion (call-interactively command))))
(unless (bufferp repl-buffer) (unless (bufferp repl-buffer)
(error "REPL command didn't return a buffer")) (error "REPL command didn't return a buffer"))
(with-current-buffer repl-buffer (+eval-repl-mode +1)) (with-current-buffer repl-buffer (+eval-repl-mode +1))
(setq +eval-last-repl-buffer repl-buffer) (setq +eval-repl-buffer repl-buffer)
(doom-popup-buffer repl-buffer)))) (select-window (doom-popup-buffer repl-buffer)))))
(when (eq (current-buffer) +eval-repl-buffer)
(goto-char (if (derived-mode-p 'comint-mode)
(cdr comint-last-prompt)
(point-max)))
t)))
;;;###autoload ;;;###autoload
(defun +eval/repl-send-region (beg end &optional inhibit-run-p) (defun +eval/repl ()
"Send a selection to the REPL." "Opens (or reopens) the REPL associated with the current major-mode and place
(interactive "r") the cursor at the prompt."
(when +eval-last-repl-buffer (interactive)
(let ((selection (buffer-substring-no-properties beg end))) (when-let (command (cdr (assq major-mode +eval-repls)))
(select-window (get-buffer-window +eval-last-repl-buffer)) (when (+eval--ensure-in-repl-buffer command)
(goto-char (point-max))
(insert selection)
(when (and (featurep 'evil) evil-mode) (when (and (featurep 'evil) evil-mode)
(evil-change-state 'insert))))) (call-interactively 'evil-append-line))
t)))
;;;###autoload
(defun +eval/repl-send-region (beg end &optional auto-execute-p)
"REPL must be open! Sends a selected region to it. If AUTO-EXECUTE-P, then
execute it immediately after."
(interactive "r")
(let ((selection (buffer-substring-no-properties beg end)))
(unless (+eval--ensure-in-repl-buffer)
(error "No REPL open"))
(when (and (featurep 'evil) evil-mode)
(call-interactively 'evil-append-line))
(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")))))

View file

@ -161,7 +161,7 @@
(:desc "Code tools" (:desc "Code tools"
:prefix "c" :prefix "c"
:desc "Build" :n "b" #'+eval/build :desc "Build" :n "b" #'+eval/build
:desc "Open/Send to REPL" :nv "r" #'+eval/repl :desc "Open/Send to REPL" :nv "r" #'+eval:repl
:desc "Open debugger" :n "R" #'+debug/open) :desc "Open debugger" :n "R" #'+debug/open)
(:desc "Personal" (:desc "Personal"

View file

@ -21,7 +21,7 @@
;; TODO (ex! "dbu[se]" 'doom:db-select) ;; TODO (ex! "dbu[se]" 'doom:db-select)
;; TODO (ex! "go[ogle]" 'doom:google-search) ;; TODO (ex! "go[ogle]" 'doom:google-search)
(ex! "http" 'httpd-start) ; start http server (ex! "http" 'httpd-start) ; start http server
(ex! "repl" '+eval/repl) ; invoke or send to repl (ex! "repl" '+eval:repl) ; invoke or send to repl
;; TODO (ex! "rx" 'doom:regex) ; open re-builder ;; TODO (ex! "rx" 'doom:regex) ; open re-builder
(ex! "sh[ell]" '+eshell:run) (ex! "sh[ell]" '+eshell:run)
(ex! "t[mux]" '+tmux:run) ; send to tmux (ex! "t[mux]" '+tmux:run) ; send to tmux