doomemacs/modules/tools/eval/autoload/settings.el
2026-03-10 21:59:14 -04:00

79 lines
3 KiB
EmacsLisp

;;; tools/eval/autoload/settings.el -*- lexical-binding: t; -*-
;;
;;; REPLs
;;;###autoload
(defvar +eval-repl-handler-alist nil
"An alist mapping major modes to plists that describe REPLs. Used by
`+eval/open-repl-other-window' and filled with the `:repl' setting.")
;;;###autodef
(defun set-repl-handler! (modes command &rest plist)
"Defines a REPL for MODES.
MODES is either a single major mode symbol or a list of them. COMMAND is a
function that creates and returns the REPL buffer.
COMMAND can either be a function that takes no arguments, or an interactive
command that will be called interactively. COMMANDS must return either the repl
buffer or a function that takes no arguments and returns the repl buffer.
PLIST is a property list that map special attributes to this repl. These are
recognized:
:persist BOOL
If non-nil, this REPL won't be killed when its window is closed.
:send-region FUNC
A function that accepts a BEG and END, and sends the contents of the region
to the REPL. Defaults to `+eval/send-region-to-repl'.
:send-buffer FUNC
A function of no arguments that sends the contents of the buffer to the
REPL. Defaults to `+eval/region', which will run the :send-region specified
function or `+eval/send-region-to-repl'."
(declare (indent defun))
(dolist (mode (ensure-list modes))
(setf (alist-get mode +eval-repl-handler-alist)
(cons command plist))))
;;
;;; Evaluation
;;;###autoload
(defvar +eval-handler-alist nil
"Alist mapping major modes to interactive runner functions.")
;;;###autodef
(defun set-eval-handler! (modes command)
"Define a code evaluator for major mode MODES with `quickrun'.
MODES can be list of major mode symbols, or a single one.
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-handler-alist', which is used by `+eval/region'."
(declare (indent defun))
(dolist (mode (ensure-list modes))
(cond ((symbolp command)
(setf (alist-get mode +eval-handler-alist nil t)
command))
((stringp command)
(after! quickrun
(setf (alist-get mode (if (stringp mode)
quickrun-file-alist
quickrun--major-mode-alist)
nil t)
command)))
((listp command)
(after! quickrun
(quickrun-add-command
(or (alist-get mode quickrun--major-mode-alist)
(string-remove-suffix "-mode" (symbol-name mode)))
command :mode mode))))))