doomemacs/modules/feature/eval/config.el
Henrik Lissner c7254e7bdc
Major optimization refactor, across the board
+ enable lexical-scope everywhere (lexical-binding = t): ~5-10% faster
  startup; ~5-20% general boost
+ reduce consing, function calls & garbage collection by preferring
  cl-loop & dolist over lambda closures (for mapc[ar], add-hook, and
  various cl-lib filter/map/reduce functions) -- where possible
+ prefer functions with dedicated opcodes, like assq (see byte-defop's
  in bytecomp.el for more)
+ prefer pcase & cond (faster) over cl-case
+ general refactor for code readability
+ ensure naming & style conventions are adhered to
+ appease byte-compiler by marking unused variables with underscore
+ defer minor mode activation to after-init, emacs-startup or
  window-setup hooks; a customization opportunity for users + ensures
  custom functionality won't interfere with startup.
2017-06-09 00:47:45 +02:00

115 lines
3.9 KiB
EmacsLisp

;;; feature/eval/config.el -*- lexical-binding: t; -*-
;;
;; Code building
;;
(defvar +eval-builders nil
"A nested alist, mapping major modes to build function plists. Used by
`+eval/build' and filled with the `:build' setting.")
(def-setting! :build (name modes fn &rest plist)
"Define a build function (FN) for MODES (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."
`(dolist (mode ',(if (listp modes) modes (list modes)) +eval-builders)
(unless (assq mode +eval-builders)
(push (list mode) +eval-builders))
(push (cons ',name (append (list :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)
(defvar +eval-runners-alist nil
"Alist mapping major modes to interactive runner functions.")
(def-setting! :eval (mode command)
"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 (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).
4. If MODE is not a string and COMMANd is a symbol, add it to
`+eval-runners-alist', which is used by `+eval/region'."
(cond ((symbolp command)
`(push ',(cons mode command) +eval-runners-alist))
((stringp command)
`(after! quickrun
(push ',(cons mode command)
,(if (stringp mode)
'quickrun-file-alist
'quickrun--major-mode-alist))))
((listp command)
`(after! quickrun
(quickrun-add-command
,(symbol-name mode)
',command :mode ',mode)))))
(def-package! quickrun
:commands (quickrun
quickrun-region
quickrun-with-arg
quickrun-shell
quickrun-compile-only
quickrun-replace-region)
:init
(add-hook! 'quickrun--mode-hook (linum-mode +1))
:config
(set! :popup
'("*quickrun*" :size 10 :noesc t :autokill t :autoclose t)
'("*eval*" :size 12 :noselect t :autokill t :autoclose t)
'("*Pp Eval Output*" :size 12 :noselect t :autokill t :autoclose t))
(defun +eval*quickrun-auto-close (&rest _)
"Allows us to silently re-run quickrun from within the quickrun buffer."
(when-let (win (get-buffer-window quickrun--buffer-name))
(let ((inhibit-message t))
(quickrun--kill-running-process)
(message ""))
(delete-window win)))
(advice-add #'quickrun :before #'+eval*quickrun-auto-close)
(advice-add #'quickrun-region :before #'+eval*quickrun-auto-close)
(defun +eval|quickrun-scroll-to-bof ()
"Ensures window is scrolled to BOF on invocation."
(with-selected-window (get-buffer-window quickrun--buffer-name)
(goto-char (point-min))))
(add-hook 'quickrun-after-run-hook #'+eval|quickrun-scroll-to-bof))