unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to advice save-buffers-kill-terminal?
@ 2016-04-14 13:16 Shiyao Ma
  2016-04-14 13:30 ` Stefan Monnier
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Shiyao Ma @ 2016-04-14 13:16 UTC (permalink / raw)
  To: help-gnu-emacs

For the following code:

(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.

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


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


(I know I can define a save-buffers-kill-terminal on my own, but that will
sooner or later not in sync with the official one).


Regards.


-- 

吾輩は猫である。ホームーページはhttps://introo.me <http://introo.me>。


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

* Re: How to advice save-buffers-kill-terminal?
  2016-04-14 13:16 How to advice save-buffers-kill-terminal? Shiyao Ma
@ 2016-04-14 13:30 ` Stefan Monnier
  2016-04-15  5:41   ` Shiyao Ma
  2016-04-14 13:52 ` Michael Heerdegen
  2016-04-20  0:52 ` Emanuel Berg
  2 siblings, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2016-04-14 13:30 UTC (permalink / raw)
  To: help-gnu-emacs

> (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




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

* Re: How to advice save-buffers-kill-terminal?
  2016-04-14 13:16 How to advice save-buffers-kill-terminal? Shiyao Ma
  2016-04-14 13:30 ` Stefan Monnier
@ 2016-04-14 13:52 ` Michael Heerdegen
  2016-04-20  0:52 ` Emanuel Berg
  2 siblings, 0 replies; 5+ messages in thread
From: Michael Heerdegen @ 2016-04-14 13:52 UTC (permalink / raw)
  To: help-gnu-emacs

Shiyao Ma <i@introo.me> writes:

> For the following code:
>
> (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.
>
> After a quick look, I found the suspicious *- lexical-binding:t -*- in
> files.el.
>
>
> Any possibility to modify that yes-or-no-p inside
> save-buffers-kill-terminal?

[Skipping the usual warnings that this is generally a bad idea]

Use `cl-letf' to bind a `symbol-function' place.

BTW, you are not just searching for `confirm-kill-emacs'?


Michael.




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

* Re: How to advice save-buffers-kill-terminal?
  2016-04-14 13:30 ` Stefan Monnier
@ 2016-04-15  5:41   ` Shiyao Ma
  0 siblings, 0 replies; 5+ messages in thread
From: Shiyao Ma @ 2016-04-15  5:41 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

Thanks.

Youre answer really helps.


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

* Re: How to advice save-buffers-kill-terminal?
  2016-04-14 13:16 How to advice save-buffers-kill-terminal? Shiyao Ma
  2016-04-14 13:30 ` Stefan Monnier
  2016-04-14 13:52 ` Michael Heerdegen
@ 2016-04-20  0:52 ` Emanuel Berg
  2 siblings, 0 replies; 5+ messages in thread
From: Emanuel Berg @ 2016-04-20  0:52 UTC (permalink / raw)
  To: help-gnu-emacs

Shiyao Ma <i@introo.me> writes:

> For the following code:
>
> (advice-add 'save-buffers-kill-terminal :around
> #'(lambda (oldfunc &rest r) (cl-flet ((yes-or-no-p
> (msg)

*Sigh*!

Did I ever hear of I guy who succeeded doing anything
with advices? Yes: several times!

:)

Here is what I would do. In the now commented-out
"kill-everything" function is where you shut
everything down gracefully, e.g., Gnus and any other
software you might run.

    (defun no-confirm-emacs-quit ()
      (interactive)
      ; (kill-everything)
      (save-buffers-kill-terminal t) ) ; silently save ... kill

    (define-key (current-global-map)
      [remap save-buffers-kill-terminal] #'no-confirm-emacs-quit)

Full example source:

    http://user.it.uu.se/~embe8573/conf/emacs-init/quit.el

Sensible blog post on this theme:

    http://user.it.uu.se/~embe8573/blogomatic/emacs/automatization-hooks-advices-or-new-functions.html

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 25 Blogomatic articles -                   




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

end of thread, other threads:[~2016-04-20  0:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-04-14 13:16 How to advice save-buffers-kill-terminal? Shiyao Ma
2016-04-14 13:30 ` Stefan Monnier
2016-04-15  5:41   ` Shiyao Ma
2016-04-14 13:52 ` Michael Heerdegen
2016-04-20  0:52 ` Emanuel Berg

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).