fix(el-patch): properly defer non-feature autoload patches

This commit is contained in:
Kiana Sheibani 2026-02-09 02:51:25 -05:00
parent d26726773e
commit 75b3cacac5
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -767,31 +767,35 @@ Emacs's runtime and configuration systems are designed to be as flexible as poss
The package allows you to define /patches/ which modify the definitions of functions in systematic ways. The suite of tools provided by the package is easily comprehensive enough for my needs, but we can make the API a bit nicer with some macros and configure options:
#+begin_src emacs-lisp
(defun ~/el-patch-feature (feature body)
(cond ((and feature (symbolp feature))
`(progn (el-patch-feature ,feature)
(after! ,feature ,body)))
((consp feature)
`(progn (add-hook! 'el-patch-pre-validate-hook
(autoload-do-load ,feature))
(defer-until! (not (autoloadp ,feature)) ,body)))
(t body)))
(defmacro defpatch! (feature type-name &rest args)
"Define a patch over something defined in FEATURE."
"Define a patch over something defined in FEATURE.
FEATURE can be either a feature symbol or an expression which
evaluates to an autoload value."
(declare (doc-string 4) (indent 3))
(let* ((el-patch (intern (format "el-patch-%s" (car type-name))))
(patch `(,el-patch ,(cadr type-name) ,@args)))
(if feature
`(progn
(el-patch-feature ,feature)
(after! ,feature
,patch))
patch)))
(~/el-patch-feature feature patch)))
(defmacro deftemplate! (feature type-name &rest args)
"Define a template over something defined in FEATURE."
"Define a template over something defined in FEATURE.
FEATURE can be either a feature symbol or an expression which
evaluates to an autoload value."
(declare (doc-string 3) (indent 2))
(let* ((template `(el-patch-define-and-eval-template ,type-name ,@args)))
(if feature
`(progn
(el-patch-feature ,feature)
(after! ,feature
,template))
template)))
(~/el-patch-feature feature template)))
(after! el-patch
(setq el-patch-warn-on-eval-template nil))
;; Allow templates in non-precompiled config
(setq el-patch-warn-on-eval-template nil)
#+end_src
** Automated Nix Builds
@ -2251,10 +2255,7 @@ When editing a snippet, the binding =C-c C-t= can be used to test it in a fresh
Doom's command to create a new snippet, ~+snippets/new~, defines a template inside of itself purely for when creating a snippet through this command. This doesn't make much sense to me when file templates already exist as a standard system in Doom, and snippets are stored inside files!
#+begin_src emacs-lisp
;; Ensure function is loaded
(autoload-do-load (symbol-function #'+snippets/new))
(defpatch! nil
(defpatch! (symbol-function #'+snippets/new)
(defun +snippets/new) (&optional all-modes)
"Create a new snippet in `+snippets-dir'.
@ -2294,20 +2295,18 @@ Doom's =file-templates= module extends =yasnippet= to provide a nice file templa
This system works well for the most part, but there's a serious issue with its UI: the function that registers file templates, ~set-file-templates!~, takes a plist to configure the template. If this list is empty, an existing template is removed instead. This is not only unintuitive, but it prevents you from having an empty property list, which is often necessary! We'll patch the function to resolve this issue with a =:remove= key, as well as to have templates appended to the alist instead of prepended to make the order of templates more clear.
#+begin_src emacs-lisp
;; Ensure function loads during validation
(autoload-do-load (symbol-function #'set-file-template!))
(defpatch! nil
(defpatch! (symbol-function #'set-file-template!)
(defun +file-templates--set) (pred plist)
(if (el-patch-swap (null (car-safe plist))
(plist-member plist :remove))
(setq +file-templates-alist
(el-patch-swap (delq (assoc pred +file-templates-alist) +file-templates-alist)
(el-patch-swap (delq (assoc pred +file-templates-alist)
+file-templates-alist)
(assoc-delete-all pred +file-templates-alist)))
(el-patch-swap
(push `(,pred ,@plist) +file-templates-alist)
(setq +file-templates-alist
(nconc +file-templates-alist `((,pred ,@plist)))))))
(nconc +file-templates-alist `((,pred ,@plist)))))))
#+end_src
Now that we have our new-and-improved template registry system, we can add new file templates as we please.