]> git.eshelyaron.com Git - emacs.git/commitdiff
scope.el: Handle cl-macrolet
authorEshel Yaron <me@eshelyaron.com>
Thu, 23 Jan 2025 08:52:30 +0000 (09:52 +0100)
committerEshel Yaron <me@eshelyaron.com>
Thu, 23 Jan 2025 08:52:30 +0000 (09:52 +0100)
lisp/emacs-lisp/scope.el

index 2a8193664a0badc32cd18adec643fd00fb91a561..ee7747fcbcf4ebc9a872db2ef67ddd76fbaf3cf0 100644 (file)
@@ -993,6 +993,21 @@ Optional argument LOCAL is a local context to extend."
        (more (scope-cl-lambda-1 l (car more) (cdr more) body))
        (t (scope-lambda l nil body))))))
 
+(defvar scope-macrolet-alist nil)
+
+(defun scope-cl-macrolet (local bindings body)
+  (if-let ((b (car bindings)))
+      (let ((name (car b))
+            (arglist (cadr b))
+            (mbody (cddr b)))
+        (scope-cl-lambda local arglist mbody)
+        (when-let ((bare (scope-sym-bare name)))
+          (when-let ((beg (scope-sym-pos name)))
+            (funcall scope-callback 'macro beg (length (symbol-name bare)) beg))
+          (let ((scope-macrolet-alist (scope-local-new bare (scope-sym-pos name) scope-macrolet-alist)))
+            (scope-cl-macrolet local (cdr bindings) body))))
+    (scope-n local body)))
+
 (defvar scope-safe-macros t
   "Specify which macros are safe to expand.
 
@@ -1001,7 +1016,7 @@ a (possibly empty) list of safe macros.")
 
 (defvar scope-unsafe-macros
   '( static-if cl-eval-when eval-when-compile eval-and-compile let-when-compile
-     rx))
+     rx cl-macrolet))
 
 (defun scope-safe-macro-p (macro)
   (and (not (memq macro scope-unsafe-macros))
@@ -1024,6 +1039,13 @@ a (possibly empty) list of safe macros.")
                    (symbol-with-pos-pos f) (length (symbol-name bare))
                    (alist-get bare scope-flet-alist))
           (scope-n local forms))
+         ((assq bare scope-macrolet-alist)
+          (funcall scope-callback 'macro
+                   (symbol-with-pos-pos f) (length (symbol-name bare))
+                   (alist-get bare scope-macrolet-alist))
+          ;; Local macros can be unsafe, so we do not expand them.
+          ;; Hence we cannot interpret their arguments.
+          )
          ((get bare 'scope-function)
           (funcall (get bare 'scope-function) local forms))
          ((functionp bare)
@@ -1318,6 +1340,8 @@ a (possibly empty) list of safe macros.")
             (scope-let* local (car forms) (cdr forms)))
            ((memq bare '(cl-eval-when)) ; Likewise!
             (scope-n local (cdr forms)))
+           ((memq bare '(cl-macrolet))  ; Also `cl-macrolet'.
+            (scope-cl-macrolet local (car forms) (cdr forms)))
            ((memq bare '(gv-define-expander))
             (scope-gv-define-expander local (car forms) (cadr forms)))
            ((memq bare '(gv-define-simple-setter))