GNU bug report logs - #59820
[PATCH] * nadvice/nadvice.el: support non-symbol (closure/lambda) advices (old Emacs)

Previous Next

Package: emacs;

Reported by: daanturo <daanturo <at> gmail.com>

Date: Sun, 4 Dec 2022 17:16:02 UTC

Severity: wishlist

Tags: moreinfo, patch

Full log


View this message in rfc822 format

From: Daan Ro <daanturo <at> gmail.com>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: "59820 <at> debbugs.gnu.org" <59820 <at> debbugs.gnu.org>, Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: bug#59820: [PATCH] * nadvice/nadvice.el: support non-symbol (closure/lambda) advices (old Emacs)
Date: Wed, 11 Oct 2023 13:04:51 +0700
[Message part 1 (text/plain, inline)]
As per S. Monnier's suggestion, I prefixed "nadvice--" to the interning symbol's
name (squashed patch attached).

> Daan, is there a specific use-case that motivates you to want to pass an
> anonymous lambda to this compatibility library?

I think lambdas are useful for temporary advices that doesn't need to be
attached to their symbols forever.

For example, when pressing "C-<backspace>", or some other editing operations, I
don't want it to modify the kill ring and the desktop's clipboard.

```elisp
(defun my-delete-instead-of-kill-when-interactive-a (func &rest args)
(if (called-interactively-p 'any)
(let* ((func (lambda (beg end &rest _) (delete-region beg end))))
(advice-add #'kill-region :override func)
(unwind-protect
(apply func args)
(advice-remove #'kill-region func)))
(apply func args)))

(advice-add #'backward-kill-word :around #'my-delete-instead-of-kill-when-interactive-a)
(advice-add #'subword-backward-kill :around #'my-delete-instead-of-kill-when-interactive-a)

(advice-add #'kill-line :around #'my-delete-instead-of-kill-when-interactive-a)
;; and other (potentially in the future) variants of `backward-kill-word' such
;; as `puni-backward-kill-word', `sp-backward-kill-word',
;; `sp-backward-kill-symbol', etc. that are bound to some key bindings

```
There are many short-lived advices like the anonymous function above that making
them a dedicated function isn't worthy, IMO. Especially closures that capture
lexical variables.

[Message part 2 (text/html, inline)]
[0001-nadvice-nadvice.el-support-non-symbol-advices.patch (application/octet-stream, attachment)]

This bug report was last modified 1 year and 251 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.