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

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

Re: How to advice save-buffers-kill-terminal?


From: Stefan Monnier
Subject: Re: How to advice save-buffers-kill-terminal?
Date: Thu, 14 Apr 2016 09:30:45 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux)

> (advice-add 'save-buffers-kill-terminal :around
>             #'(lambda (oldfunc &rest r)
>                 (cl-flet ((yes-or-no-p (msg)
>                             (message "test:%S" msg)))
>                   (apply oldfunc r))))

> The yes-or-no-p in cl-flet doesn't affect the calling of yes-or-no-p inside
> save-buffers-kill-terminal.

Indeed.  `cl-flet` is documented as being lexical, which means that it
only affects calls to `yes-or-no-p` made *syntactically within* (as
opposed to: *during the execution of*) the `cl-flet`.

> After a quick look, I found the suspicious *- lexical-binding:t -*- in
> files.el.

That's actually unrelated: `lexical-binding` affects the scoping of
variables, whereas your case depends on the scoping of functions.

> Any possibility to modify that yes-or-no-p inside
> save-buffers-kill-terminal?

Of course.  You could use

    (defvar my-skip-yes-or-no-p nil)
    (advice-add 'yes-or-no-p :around
                (lambda (orig-fun &rest args)
                  (if my-skip-yes-or-no-p
                      (apply #'message "test:%S" args)
                    (apply orig-fun args))))
    (advice-add 'save-buffers-kill-terminal :around
                #'(lambda (oldfunc &rest r)
                    (let ((my-skip-yes-or-no-p t))
                      (apply oldfunc r))))

or

    (advice-add 'save-buffers-kill-terminal :around
                #'(lambda (oldfunc &rest r)
                    (cl-letf (((symbol-function 'yes-or-no-p)
                               (lambda (msg) (message "test:%S" msg))))
                      (apply oldfunc r))))

The second is more concise, but I the like the first better, because
C-h f yes-or-no-p RET will then tell you honestly about the fact that
it's sometimes overridden.


        Stefan




reply via email to

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