feature/eval: rewrite module
This commit is contained in:
parent
05e1a15ba8
commit
74d50f6159
6 changed files with 140 additions and 136 deletions
|
@ -1,13 +1,31 @@
|
||||||
;;; feature/repl/autoload/build.el
|
;;; feature/eval/autoload/build.el
|
||||||
|
|
||||||
;; TODO
|
(defvar-local +eval-last-builder nil
|
||||||
|
"The last builder run in the current buffer.")
|
||||||
|
|
||||||
|
(defun +eval--read-builder ()
|
||||||
|
(let ((builders (cl-remove-if-not
|
||||||
|
(lambda (plist)
|
||||||
|
(when-let (pred (plist-get (cdr plist) :when))
|
||||||
|
(eval pred)))
|
||||||
|
(cdr (assq major-mode +eval-builders)))))
|
||||||
|
(completing-read
|
||||||
|
"Build: "
|
||||||
|
(mapcar 'car builders)
|
||||||
|
nil t)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +eval/build ())
|
(defun +eval/build (&optional builder)
|
||||||
|
(interactive (list (+eval--read-builder)))
|
||||||
|
(unless builder
|
||||||
|
(error "No builder for this buffer"))
|
||||||
|
(let ((desc (assq builder (assq major-mode +eval-builders))))
|
||||||
|
(unless desc
|
||||||
|
(error "Builder not found in registered builders"))
|
||||||
|
(message "Running %s" builder)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +eval/rebuild ())
|
(defun +eval/rebuild (&optional builder)
|
||||||
|
(interactive (list +eval-last-builder))
|
||||||
;;;###autoload
|
(+eval/build +eval-last-builder))
|
||||||
(defun +eval-get-builder ())
|
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
;;; feature/repl/autoload/eval.el
|
;;; feature/eval/autoload/eval.el
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +repl/eval-buffer ()
|
(defun +eval/buffer ()
|
||||||
"Evaluate the whole buffer."
|
"Evaluate the whole buffer."
|
||||||
(interactive)
|
(interactive)
|
||||||
(cond ((eq major-mode 'emacs-lisp-mode)
|
(cond ((eq major-mode 'emacs-lisp-mode)
|
||||||
(+repl/eval-region (point-min) (point-max)))
|
(+eval/region (point-min) (point-max)))
|
||||||
(t (quickrun))))
|
(t (quickrun))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +repl/eval-region (beg end)
|
(defun +eval/region (beg end)
|
||||||
"Evaluate a region and, if large enough, prints its output to a popup buffer (if an
|
"Evaluate a region and, if large enough, prints its output to a popup buffer (if an
|
||||||
elisp buffer). Otherwise forward the region to Quickrun."
|
elisp buffer). Otherwise forward the region to Quickrun."
|
||||||
(interactive "r")
|
(interactive "r")
|
||||||
|
@ -24,8 +24,8 @@ elisp buffer). Otherwise forward the region to Quickrun."
|
||||||
(read-only-mode -1)
|
(read-only-mode -1)
|
||||||
(setq-local scroll-margin 0)
|
(setq-local scroll-margin 0)
|
||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
|
(set-syntax-table emacs-lisp-mode-syntax-table)
|
||||||
(prin1 result buf)
|
(prin1 result buf)
|
||||||
(emacs-lisp-mode)
|
|
||||||
(pp-buffer)
|
(pp-buffer)
|
||||||
(read-only-mode 1)
|
(read-only-mode 1)
|
||||||
(setq lines (count-lines (point-min) (point-max)))
|
(setq lines (count-lines (point-min) (point-max)))
|
||||||
|
@ -38,7 +38,7 @@ elisp buffer). Otherwise forward the region to Quickrun."
|
||||||
(t (quickrun-region beg end)))))
|
(t (quickrun-region beg end)))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +repl/eval-region-and-replace (beg end)
|
(defun +eval/region-and-replace (beg end)
|
||||||
(interactive "r")
|
(interactive "r")
|
||||||
(cond ((eq major-mode 'emacs-lisp-mode)
|
(cond ((eq major-mode 'emacs-lisp-mode)
|
||||||
(kill-region beg end)
|
(kill-region beg end)
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
;;; feature/eval/autoload/evil.el
|
;;; feature/eval/autoload/evil.el
|
||||||
|
|
||||||
;;;###autoload (autoload '+repl:eval-region "feature/eval/autoload/evil" nil t)
|
;;;###autoload (autoload '+eval:region "feature/eval/autoload/evil" nil t)
|
||||||
(evil-define-operator +repl:eval-region (beg end)
|
(evil-define-operator +eval:region (beg end)
|
||||||
|
"Send region to the currently open repl, if available."
|
||||||
:move-point nil
|
:move-point nil
|
||||||
(interactive "<r>")
|
(interactive "<r>")
|
||||||
(+repl/eval-region beg end))
|
(+eval/region beg end))
|
||||||
|
|
||||||
;;;###autoload (autoload '+repl:eval-region-and-replace "feature/eval/autoload/evil" nil t)
|
;;;###autoload (autoload '+eval:replace-region "feature/eval/autoload/evil" nil t)
|
||||||
(evil-define-operator +repl:eval-region-and-replace (beg end)
|
(evil-define-operator +eval:replace-region (beg end)
|
||||||
:move-point nil
|
:move-point nil
|
||||||
(interactive "<r>")
|
(interactive "<r>")
|
||||||
(+repl/eval-region-and-replace beg end))
|
(+eval/region-and-replace beg end))
|
||||||
|
|
||||||
|
|
|
@ -1,45 +1,30 @@
|
||||||
;;; feature/repl/autoload/repl.el
|
;;; feature/eval/autoload/repl.el
|
||||||
|
|
||||||
|
(defvar +eval-last-repl-buffer nil
|
||||||
|
"The buffer of the last open repl.")
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +repl/open ()
|
(defun +eval/repl ()
|
||||||
|
"Open the REPL associated with the current major-mode. If selection is active,
|
||||||
|
send region to repl."
|
||||||
(interactive)
|
(interactive)
|
||||||
;; TODO
|
(when-let (command (cdr (assq major-mode +eval-repls)))
|
||||||
(error "Not implemented"))
|
(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))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +repl/send ()
|
(defun +eval/repl-send-region (beg end &optional inhibit-run-p)
|
||||||
(interactive)
|
"Send a selection to the REPL."
|
||||||
;; TODO
|
(interactive "r")
|
||||||
(error "Not implemented"))
|
(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)))))
|
||||||
|
|
||||||
;;;###autoload (autoload '+repl:open "feature/repl/autoload/repl" nil t)
|
|
||||||
;;;###autoload (autoload '+repl:send "feature/repl/autoload/repl" nil t)
|
|
||||||
|
|
||||||
(after! evil
|
|
||||||
(evil-define-command +repl:open (&optional bang command)
|
|
||||||
:repeat nil
|
|
||||||
(interactive "<!><a>")
|
|
||||||
(if (and doom-repl-buffer (buffer-live-p doom-repl-buffer))
|
|
||||||
(doom-popup-buffer doom-repl-buffer)
|
|
||||||
(rtog/toggle-repl (if (use-region-p) 4))
|
|
||||||
(setq doom-repl-buffer (current-buffer))
|
|
||||||
(when command
|
|
||||||
(with-current-buffer doom-repl-buffer
|
|
||||||
(insert command)
|
|
||||||
(unless bang (comint-send-input))))))
|
|
||||||
|
|
||||||
(evil-define-operator +repl:eval (&optional beg end bang)
|
|
||||||
:type inclusive :repeat nil
|
|
||||||
(interactive "<r><!>")
|
|
||||||
(let ((region-p (use-region-p))
|
|
||||||
(selection (s-trim (buffer-substring-no-properties beg end))))
|
|
||||||
(doom:repl bang)
|
|
||||||
(when (and region-p beg end)
|
|
||||||
(let* ((buf doom-repl-buffer)
|
|
||||||
(win (get-buffer-window buf)))
|
|
||||||
(unless (eq buf (doom-popup-p (get-buffer-window buf)))
|
|
||||||
(doom-popup-buffer buf))
|
|
||||||
(when (and doom-repl-buffer (buffer-live-p doom-repl-buffer))
|
|
||||||
(with-current-buffer doom-repl-buffer
|
|
||||||
(goto-char (point-max))
|
|
||||||
(insert selection))))))))
|
|
||||||
|
|
|
@ -1,60 +1,80 @@
|
||||||
;;; feature/repl/config.el
|
;;; feature/eval/config.el
|
||||||
|
|
||||||
;; + Running inline code using `quickrun'
|
;; This module creates a centralized way of invoking/sending selections to REPLs
|
||||||
;; + REPLs using `repl-toggle'
|
;; (with `repl-toggle'), building projects/files, and running code (with
|
||||||
|
;; `quickrun'), revealing its output in a popup window.
|
||||||
|
|
||||||
(defvar doom-repl-buffer nil
|
;;
|
||||||
"The current REPL buffer.")
|
;; Code building
|
||||||
|
;;
|
||||||
|
|
||||||
(defvar +eval-builders (make-hash-table :test 'equal)
|
(defvar +eval-builders nil
|
||||||
"A hash-table of plists, containing functions for building source code. Used
|
"A nested alist, mapping major modes to build function plists. Used by
|
||||||
by `+eval/build', and filled with the `:build' setting")
|
`+eval/build' and filled with the `:build' setting.")
|
||||||
|
|
||||||
;; remove ellipsis when printing sexp in message buffer
|
(def-setting! :build (name mode fn &rest plist)
|
||||||
|
"Define a build function (FN) for MODE (can be major or minor) called NAME.
|
||||||
|
|
||||||
|
PLIST accepts the following properties:
|
||||||
|
|
||||||
|
:when FORM A predicate to determine if the builder is appropriate for this
|
||||||
|
buffer."
|
||||||
|
`(progn
|
||||||
|
(unless (assq ',mode +eval-builders)
|
||||||
|
(push (list ',mode nil) +eval-builders))
|
||||||
|
(push (list ',name :fn #',fn ,@plist)
|
||||||
|
(cdr (assq ',mode +eval-builders)))))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; REPLs
|
||||||
|
;;
|
||||||
|
|
||||||
|
(defvar +eval-repls nil
|
||||||
|
"An alist mapping major modes to plists that describe REPLs. Used by
|
||||||
|
`+eval/repl' and filled with the `:repl' setting.")
|
||||||
|
|
||||||
|
(define-minor-mode +eval-repl-mode
|
||||||
|
"A minor mode for REPL buffers."
|
||||||
|
:init-value nil)
|
||||||
|
|
||||||
|
(def-setting! :repl (mode command)
|
||||||
|
"Define a REPL for a mode. MODE is a major mode and COMMAND is a function that
|
||||||
|
invokes the repl. Takes the same arguements as `rtog/add-repl'."
|
||||||
|
`(push ',(cons mode command) +eval-repls))
|
||||||
|
|
||||||
|
(set! :popup
|
||||||
|
'(:custom (lambda (b &rest _) (buffer-local-value '+eval-repl-mode b)))
|
||||||
|
:size 16 :noesc t)
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; Evaluation
|
||||||
|
;;
|
||||||
|
|
||||||
|
;; remove ellipsis when printing sexps in message buffer
|
||||||
(setq eval-expression-print-length nil
|
(setq eval-expression-print-length nil
|
||||||
eval-expression-print-level nil)
|
eval-expression-print-level nil)
|
||||||
|
|
||||||
(def-setting! :repl (mode command)
|
|
||||||
"Define a REPL for a mode. Takes the same arguements as `rtog/add-repl'."
|
|
||||||
`(after! repl-toggle
|
|
||||||
(rtog/add-repl ',mode ',command)))
|
|
||||||
|
|
||||||
(def-setting! :build (name mode pred-fn &optional build-fn)
|
|
||||||
"Define a build command function (BUILD-FN) for major-mode MODE, called NAME
|
|
||||||
-- a symbol -- PRED-FN is a predicate function that determines this builder's
|
|
||||||
suitability for the current buffer."
|
|
||||||
(unless build-fn
|
|
||||||
(setq build-fn pred-fn
|
|
||||||
pred-fn nil))
|
|
||||||
`(puthash ',(cons name mode)
|
|
||||||
(list :predicate ,pred-fn :fn ,build-fn)
|
|
||||||
+eval-builders))
|
|
||||||
|
|
||||||
(def-setting! :eval (mode command)
|
(def-setting! :eval (mode command)
|
||||||
"Define a code evaluator for `quickrun'.
|
"Define a code evaluator for major mode MODE with `quickrun'.
|
||||||
|
|
||||||
1. If MODE is a string and COMMAND is the string, MODE is a file regexp and
|
1. If MODE is a string and COMMAND is the string, MODE is a file regexp and
|
||||||
COMMAND is a string key for an entry in `quickrun-file-alist'.
|
COMMAND is a string key for an entry in `quickrun-file-alist'.
|
||||||
2. If MODE is not a string and COMMAND is a string, MODE is a major-mode symbol
|
2. If MODE is not a string and COMMAND is a string, MODE is a major-mode symbol
|
||||||
and COMMAND is a key; they will be registered in
|
and COMMAND is a key (for `quickrun--language-alist'), and will be registered
|
||||||
`quickrun--major-mode-alist'.
|
in `quickrun--major-mode-alist'.
|
||||||
3. If MODE is not a string and COMMAND is a list, use `quickrun-add-command'. e.g.
|
3. If MODE is not a string and COMMAND is an alist, see `quickrun-add-command':
|
||||||
(quickrun-add-command MODE COMMAND :mode MODE)"
|
(quickrun-add-command MODE COMMAND :mode MODE)"
|
||||||
(if (stringp command)
|
`(after! quickrun
|
||||||
`(after! quickrun
|
,(if (stringp command)
|
||||||
(push ',(cons mode command)
|
`(push ',(cons mode command)
|
||||||
,(if (stringp mode)
|
,(if (stringp mode)
|
||||||
'quickrun-file-alist
|
'quickrun-file-alist
|
||||||
'quickrun--major-mode-alist)))
|
'quickrun--major-mode-alist))
|
||||||
`(after! quickrun
|
`(quickrun-add-command
|
||||||
(quickrun-add-command
|
,(symbol-name mode)
|
||||||
,(symbol-name mode)
|
',command :mode ',mode))))
|
||||||
',command :mode ',mode))))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
|
||||||
;; Packages
|
|
||||||
;;
|
|
||||||
|
|
||||||
(def-package! quickrun
|
(def-package! quickrun
|
||||||
:commands (quickrun
|
:commands (quickrun
|
||||||
|
@ -64,47 +84,29 @@ suitability for the current buffer."
|
||||||
quickrun-compile-only
|
quickrun-compile-only
|
||||||
quickrun-replace-region)
|
quickrun-replace-region)
|
||||||
:init
|
:init
|
||||||
|
;; Use standard linum-mode for quickrun eval windows; so we can have different
|
||||||
|
;; rules for it.
|
||||||
(add-hook 'quickrun/mode-hook 'linum-mode)
|
(add-hook 'quickrun/mode-hook 'linum-mode)
|
||||||
|
|
||||||
:config
|
:config
|
||||||
(set! :popup "*quickrun*" :size 10)
|
(set! :popup "*quickrun*" :size 10)
|
||||||
|
|
||||||
;; don't auto-focus quickrun windows. Shackle handles that for us.
|
;; don't auto-focus quickrun windows. Shackle handles that for us.
|
||||||
(setq quickrun-focus-p nil)
|
(setq quickrun-focus-p nil)
|
||||||
|
|
||||||
(defun +repl*quickrun-close-popup (&optional _ _ _ _)
|
(defun +eval*quickrun-auto-close (&rest _)
|
||||||
"Allows us to silently re-run quickrun from within the quickrun buffer."
|
"Allows us to silently re-run quickrun from within the quickrun buffer."
|
||||||
(awhen (get-buffer-window quickrun/buffer-name)
|
(when-let (win (get-buffer-window quickrun--buffer-name))
|
||||||
(let (message-log-max)
|
(let ((inhibit-message t))
|
||||||
(quickrun/kill-running-process)
|
(quickrun--kill-running-process)
|
||||||
(message ""))
|
(message ""))
|
||||||
(doom/popup-close it)))
|
(delete-window win)))
|
||||||
|
(advice-add 'quickrun :before '+eval*quickrun-auto-close)
|
||||||
|
(advice-add 'quickrun-region :before '+eval*quickrun-auto-close)
|
||||||
|
|
||||||
(defun +repl|quickrun-scroll-to-bof ()
|
(defun +eval|quickrun-scroll-to-bof ()
|
||||||
"Ensures window is scrolled to BOF on invocation."
|
"Ensures window is scrolled to BOF on invocation."
|
||||||
(with-selected-window (get-buffer-window quickrun/buffer-name)
|
(with-selected-window (get-buffer-window quickrun--buffer-name)
|
||||||
(goto-char (point-min))))
|
(goto-char (point-min))))
|
||||||
|
(add-hook 'quickrun-after-run-hook '+eval|quickrun-scroll-to-bof))
|
||||||
;;; Popup hacks
|
|
||||||
(advice-add 'quickrun :before '+repl*quickrun-close-popup)
|
|
||||||
(advice-add 'quickrun-region :before '+repl*quickrun-close-popup)
|
|
||||||
;; Ensures window is scrolled to BOF
|
|
||||||
(add-hook 'quickrun-after-run-hook '+repl|quickrun-scroll-to-bof))
|
|
||||||
|
|
||||||
|
|
||||||
(def-package! repl-toggle
|
|
||||||
:commands rtog/toggle-repl
|
|
||||||
:preface (defvar rtog/mode-repl-alist nil)
|
|
||||||
:config
|
|
||||||
(set! :popup
|
|
||||||
'(:custom (lambda (b &rest _)
|
|
||||||
(when (and (featurep 'repl-toggle)
|
|
||||||
(string-prefix-p "*" (buffer-name (get-buffer b))))
|
|
||||||
(buffer-local-value 'repl-toggle-mode b))))
|
|
||||||
:size 16)
|
|
||||||
|
|
||||||
(map! :map repl-toggle-mode-map
|
|
||||||
:ei "C-n" 'comint-next-input
|
|
||||||
:ei "C-p" 'comint-previous-input
|
|
||||||
:ei "<down>" 'comint-next-input
|
|
||||||
:ei "<up>" 'comint-previous-input))
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
;; -*- no-byte-compile: t; -*-
|
;; -*- no-byte-compile: t; -*-
|
||||||
;;; feature/repl/packages.el
|
;;; feature/eval/packages.el
|
||||||
|
|
||||||
(package! quickrun)
|
(package! quickrun)
|
||||||
(package! repl-toggle)
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue