feature/eval: rewrite module

This commit is contained in:
Henrik Lissner 2017-03-04 18:28:51 -05:00
parent 05e1a15ba8
commit 74d50f6159
6 changed files with 140 additions and 136 deletions

View file

@ -1,60 +1,80 @@
;;; feature/repl/config.el
;;; feature/eval/config.el
;; + Running inline code using `quickrun'
;; + REPLs using `repl-toggle'
;; This module creates a centralized way of invoking/sending selections to REPLs
;; (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)
"A hash-table of plists, containing functions for building source code. Used
by `+eval/build', and filled with the `:build' setting")
(defvar +eval-builders nil
"A nested alist, mapping major modes to build function plists. Used by
`+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
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)
"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
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
and COMMAND is a key; they will be registered in
`quickrun--major-mode-alist'.
3. If MODE is not a string and COMMAND is a list, use `quickrun-add-command'. e.g.
and COMMAND is a key (for `quickrun--language-alist'), and will be registered
in `quickrun--major-mode-alist'.
3. If MODE is not a string and COMMAND is an alist, see `quickrun-add-command':
(quickrun-add-command MODE COMMAND :mode MODE)"
(if (stringp command)
`(after! quickrun
(push ',(cons mode command)
,(if (stringp mode)
'quickrun-file-alist
'quickrun--major-mode-alist)))
`(after! quickrun
(quickrun-add-command
,(symbol-name mode)
',command :mode ',mode))))
;;
;; Packages
;;
`(after! quickrun
,(if (stringp command)
`(push ',(cons mode command)
,(if (stringp mode)
'quickrun-file-alist
'quickrun--major-mode-alist))
`(quickrun-add-command
,(symbol-name mode)
',command :mode ',mode))))
(def-package! quickrun
:commands (quickrun
@ -64,47 +84,29 @@ suitability for the current buffer."
quickrun-compile-only
quickrun-replace-region)
:init
;; Use standard linum-mode for quickrun eval windows; so we can have different
;; rules for it.
(add-hook 'quickrun/mode-hook 'linum-mode)
:config
(set! :popup "*quickrun*" :size 10)
;; don't auto-focus quickrun windows. Shackle handles that for us.
(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."
(awhen (get-buffer-window quickrun/buffer-name)
(let (message-log-max)
(quickrun/kill-running-process)
(when-let (win (get-buffer-window quickrun--buffer-name))
(let ((inhibit-message t))
(quickrun--kill-running-process)
(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."
(with-selected-window (get-buffer-window quickrun/buffer-name)
(with-selected-window (get-buffer-window quickrun--buffer-name)
(goto-char (point-min))))
;;; 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))
(add-hook 'quickrun-after-run-hook '+eval|quickrun-scroll-to-bof))