bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#59820: [PATCH] * nadvice/nadvice.el: support non-symbol (closure/lam


From: Daan Ro
Subject: bug#59820: [PATCH] * nadvice/nadvice.el: support non-symbol (closure/lambda) advices (old Emacs)
Date: Wed, 11 Oct 2023 13:04:51 +0700

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.

Sent from Mailspring

Attachment: 0001-nadvice-nadvice.el-support-non-symbol-advices.patch
Description: Binary data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]