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)
+ [ ] Generalize ~doom-visual-bell~ by basing its background off a face
** 2.0.2 [63/66]
** 2.0.2 [64/66]
+ [ ] 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)
+ [X] send-to-REPL workflow: does it still work? (see ~:repl~ & ~+eval/repl-send-region~)
+ [X] completion/ivy: restore ag searching (for compressed files)
Now ~:ag~, ~:rg~, ~:agcwd~ and ~:rgcwd~
+ [X] ~add-transient-hook!~: add support for appending

View file

@ -12,3 +12,11 @@
:move-point nil
(interactive "<r>")
(+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
(defvar +eval-last-repl-buffer nil
(defvar +eval-repl-buffer nil
"The buffer of the last open repl.")
(defun +eval--ensure-in-repl-buffer (&optional command)
(if (eq (current-buffer) +eval-repl-buffer)
t
(if (and +eval-repl-buffer (buffer-live-p +eval-repl-buffer))
(if-let (win (get-buffer-window +eval-repl-buffer))
(select-window win)
(doom-popup-buffer +eval-repl-buffer))
(when command
(let ((repl-buffer (save-window-excursion (call-interactively command))))
(unless (bufferp repl-buffer)
(error "REPL command didn't return a buffer"))
(with-current-buffer repl-buffer (+eval-repl-mode +1))
(setq +eval-repl-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
(defun +eval/repl ()
"Open the REPL associated with the current major-mode. If selection is active,
send region to repl."
"Opens (or reopens) the REPL associated with the current major-mode and place
the cursor at the prompt."
(interactive)
(when-let (command (cdr (assq major-mode +eval-repls)))
(let ((repl-buffer (save-window-excursion (funcall command))))
(unless (bufferp repl-buffer)
(error "REPL command didn't return a buffer"))
(with-current-buffer repl-buffer (+eval-repl-mode +1))
(setq +eval-last-repl-buffer repl-buffer)
(doom-popup-buffer repl-buffer))))
(when (+eval--ensure-in-repl-buffer command)
(when (and (featurep 'evil) evil-mode)
(call-interactively 'evil-append-line))
t)))
;;;###autoload
(defun +eval/repl-send-region (beg end &optional inhibit-run-p)
"Send a selection to the REPL."
(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")
(when +eval-last-repl-buffer
(let ((selection (buffer-substring-no-properties beg end)))
(select-window (get-buffer-window +eval-last-repl-buffer))
(goto-char (point-max))
(insert selection)
(when (and (featurep 'evil) evil-mode)
(evil-change-state 'insert)))))
(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"
:prefix "c"
: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 "Personal"

View file

@ -21,7 +21,7 @@
;; TODO (ex! "dbu[se]" 'doom:db-select)
;; TODO (ex! "go[ogle]" 'doom:google-search)
(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
(ex! "sh[ell]" '+eshell:run)
(ex! "t[mux]" '+tmux:run) ; send to tmux