branch: elpa/recomplete
commit 7a5c4c86cdbf8ba6b045d5ace466e5dcb2f10db0
Author: Campbell Barton <ideasma...@gmail.com>
Commit: Campbell Barton <ideasma...@gmail.com>

    Cleanup: sanity checks for with-advice macro
---
 recomplete.el | 53 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 20 deletions(-)

diff --git a/recomplete.el b/recomplete.el
index 4cef047e71..ea2c350ece 100644
--- a/recomplete.el
+++ b/recomplete.el
@@ -7,7 +7,7 @@
 
 ;; URL: https://codeberg.org/ideasman42/emacs-recomplete
 ;; Version: 0.2
-;; Package-Requires: ((emacs "26.1"))
+;; Package-Requires: ((emacs "29.1"))
 
 ;;; Commentary:
 
@@ -77,28 +77,41 @@
 Advice are triplets of (SYMBOL HOW FUNCTION),
 see `advice-add' documentation."
   (declare (indent 1))
-  (let ((body-let nil)
+  (let ((advice-list advice)
+        (body-let nil)
         (body-advice-add nil)
         (body-advice-remove nil)
         (item nil))
-    (while (setq item (pop advice))
-      (let ((fn-sym (gensym))
-            (fn-advise (pop item))
-            (fn-advice-ty (pop item))
-            (fn-body (pop item)))
-        ;; Build the calls for each type.
-        (push (list fn-sym fn-body) body-let)
-        (push (list 'advice-add fn-advise fn-advice-ty fn-sym) body-advice-add)
-        (push (list 'advice-remove fn-advise fn-sym) body-advice-remove)))
-    (setq body-let (nreverse body-let))
-    (setq body-advice-add (nreverse body-advice-add))
-    ;; Compose the call.
-    `(let ,body-let
-       (unwind-protect
-           (progn
-             ,@body-advice-add
-             ,@body)
-         ,@body-advice-remove))))
+    (unless (listp advice-list)
+      (error "Advice must be a list"))
+    (cond
+     ((null advice-list)
+      (macroexp-warn-and-return
+       "An empty advice argument was found"
+       `(progn
+          ,@body)))
+     (t
+      (while (setq item (pop advice-list))
+        (unless (and (listp item) (eq 3 (length item)))
+          (error "Each advice must be a list of 3 items"))
+        (let ((fn-sym (gensym))
+              (fn-advise (pop item))
+              (fn-advice-ty (pop item))
+              (fn-body (pop item)))
+          ;; Build the calls for each type.
+          (push (list fn-sym fn-body) body-let)
+          (push (list 'advice-add fn-advise fn-advice-ty fn-sym) 
body-advice-add)
+          (push (list 'advice-remove fn-advise fn-sym) body-advice-remove)))
+      (setq body-let (nreverse body-let))
+      (setq body-advice-add (nreverse body-advice-add))
+
+      ;; Compose the call.
+      `(let ,body-let
+         (unwind-protect
+             (progn
+               ,@body-advice-add
+               ,@body)
+           ,@body-advice-remove))))))
 
 ;; Back-ported from emacs-29.1 (remove once older versions have beeen dropped).
 (defmacro recomplete--with-undo-amalgamate (&rest body)

Reply via email to