unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#59820: [PATCH] * nadvice/nadvice.el: support non-symbol (closure/lambda) advices (old Emacs)
@ 2022-12-04 17:14 daanturo
  2022-12-13  1:04 ` Stefan Kangas
  2022-12-13 13:50 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 2 replies; 10+ messages in thread
From: daanturo @ 2022-12-04 17:14 UTC (permalink / raw)
  To: 59820

[-- Attachment #1: Type: text/plain, Size: 1820 bytes --]

This patch provides the ELPA version (for Emacs < 24.4) of nadvice.el's
advice-add the ability to handle anonymous advices.

I have tested with a simple example:


```emacs-lisp

(let* ((sym (make-symbol "nadvice λ")))
  (defalias sym (lambda (&rest args) '(1)))
  (advice-add sym :around (lambda (func &rest args)
                            (append (apply func args) '(2))))
  (vector
   ;; advised returned value
   (funcall sym)
   (progn
     (advice-remove sym (lambda (func &rest args)
                          (append (apply func args) '(2))))
     ;; unadvised returned value
     (funcall sym))))

;; => [(1 2) (1)]

```


In GNU Emacs 24.3.1 (x86_64-redhat-linux-gnu, GTK+ Version 3.22.30)
 of 2020-04-04 on x86-01.bsys.centos.org
Windowing system distributor `The X.Org Foundation', version
 11.0.12201005
 
Configured using:
 `configure '--build=x86_64-redhat-linux-gnu'
 '--host=x86_64-redhat-linux-gnu' '--program-prefix='
 '--disable-dependency-tracking' '--prefix=/usr' '--exec-prefix=/usr'
 '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc'
 '--datadir=/usr/share' '--includedir=/usr/include'
 '--libdir=/usr/lib64' '--libexecdir=/usr/libexec'
 '--localstatedir=/var' '--sharedstatedir=/var/lib'
 '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--with-dbus'
 '--with-gif' '--with-jpeg' '--with-png' '--with-rsvg' '--with-tiff'
 '--with-xft' '--with-xpm' '--with-x-toolkit=gtk3' '--with-gpm=no'
 'build_alias=x86_64-redhat-linux-gnu'
 'host_alias=x86_64-redhat-linux-gnu' 'CFLAGS=-DMAIL_USE_LOCKF -O2 -g
 -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions
 -fstack-protector-strong --param=ssp-buffer-size=4
 -grecord-gcc-switches -m64 -mtune=generic' 'LDFLAGS=-Wl,-z,relro ''


-- 
Daanturo.

[-- Attachment #2: 0001-nadvice-nadvice.el-support-non-symbol-advices.patch --]
[-- Type: text/x-patch, Size: 3446 bytes --]

From b07fd697e097ed0ca6040781830ad42be2a9ac86 Mon Sep 17 00:00:00 2001
From: Daanturo <daanturo@gmail.com>
Date: Sun, 4 Dec 2022 21:34:52 +0700
Subject: [PATCH] * nadvice/nadvice.el: support non-symbol advices

(advice-add): by aliasing the function to a new symbol
---
 nadvice.el | 53 +++++++++++++++++++++++++++++++----------------------
 1 file changed, 31 insertions(+), 22 deletions(-)

diff --git a/nadvice.el b/nadvice.el
index 58523f6..443a5d0 100644
--- a/nadvice.el
+++ b/nadvice.el
@@ -52,30 +52,38 @@
 (defun advice-member-p (advice symbol)
   (ad-find-advice symbol 'around advice))
 
+
+(defun advice--ensure-symbol (func)
+  (if (symbolp func)
+      func
+    (let* ((sym (intern (format "%S" func))))
+      (unless (fboundp sym)
+        (defalias sym func))
+      sym)))
+
 ;;;###autoload
 (defun advice-add (symbol where function &optional props)
   (when props
     (error "This version of nadvice.el does not support PROPS"))
-  (unless (symbolp function)
-    (error "This version of nadvice.el requires FUNCTION to be a symbol"))
-  (let ((body (cond
-               ((eq where :before)
-                `(progn (apply #',function (ad-get-args 0)) ad-do-it))
-               ((eq where :after)
-                `(progn ad-do-it (apply #',function (ad-get-args 0))))
-               ((eq where :override)
-                `(setq ad-return-value (apply #',function (ad-get-args 0))))
-               ((eq where :around)
-                `(setq ad-return-value
-                       (apply #',function
-                              (lambda (&rest nadvice--rest-arg)
-                                (ad-set-args 0 nadvice--rest-arg)
-                                ad-do-it)
-                              (ad-get-args 0))))
-               (t (error "This version of nadvice.el does not handle %S"
-                         where)))))
+  (let* ((advice-fn (advice--ensure-symbol function))
+         (body (cond
+                ((eq where :before)
+                 `(progn (apply #',advice-fn (ad-get-args 0)) ad-do-it))
+                ((eq where :after)
+                 `(progn ad-do-it (apply #',advice-fn (ad-get-args 0))))
+                ((eq where :override)
+                 `(setq ad-return-value (apply #',advice-fn (ad-get-args 0))))
+                ((eq where :around)
+                 `(setq ad-return-value
+                        (apply #',advice-fn
+                               (lambda (&rest nadvice--rest-arg)
+                                 (ad-set-args 0 nadvice--rest-arg)
+                                 ad-do-it)
+                               (ad-get-args 0))))
+                (t (error "This version of nadvice.el does not handle %S"
+                          where)))))
     (ad-add-advice symbol
-                   `(,function nil t (advice lambda () ,body))
+                   `(,advice-fn nil t (advice lambda () ,body))
                    'around
                    nil)
     (ad-activate symbol)))
@@ -84,9 +92,10 @@
 (defun advice-remove (symbol function)
   ;; Just return nil if there is no advice, rather than signaling an
   ;; error.
-  (when (advice-member-p function symbol)
-    (ad-remove-advice symbol 'around function)
-    (ad-activate symbol)))
+  (let* ((advice-fn (advice--ensure-symbol function)))
+    (when (advice-member-p advice-fn symbol)
+      (ad-remove-advice symbol 'around advice-fn)
+      (ad-activate symbol))))
 
 )
 
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-10-11  6:50 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-04 17:14 bug#59820: [PATCH] * nadvice/nadvice.el: support non-symbol (closure/lambda) advices (old Emacs) daanturo
2022-12-13  1:04 ` Stefan Kangas
2022-12-13 13:50 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-12-13 14:59   ` daanturo
2022-12-13 15:02     ` Daan Ro
2023-10-09  9:45       ` Stefan Kangas
2023-10-09 22:06         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-10 10:44           ` Stefan Kangas
2023-10-11  6:04             ` Daan Ro
2023-10-11  6:50               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).