all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Filipe Silva <filipe.silva@gmail.com>
To: Chunyang Xu <mail@xuchunyang.me>
Cc: Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
Subject: Re: Seeking Advice about refactoring and advice snippet
Date: Fri, 10 Feb 2017 14:24:16 -0200	[thread overview]
Message-ID: <CAEwkUWMaSmxbSP+jchaiGghAxhwOrHnm_bZ8N=mQsjoKXU_oAA@mail.gmail.com> (raw)
In-Reply-To: <m2efz63zbu.fsf@xuchunyang.me>

Chunyang, thanks for the alternative approach!

I'd really like to know how to perform this simple refactoring though, just
to improve my knowledge of elisp a little bit.

[]s

Filipe

On Fri, Feb 10, 2017 at 12:17 PM, Chunyang Xu <mail@xuchunyang.me> wrote:

> Hi,
>
> It should be easier to use 'kill-buffer-query-functions'
>
>   (defun dont-kill-scratch ()
>     (if ((equal (buffer-name) "*scratch*"))
>         (progn (message "DENIED! don't kill my precious *scratch*!!")
>                nil)
>       t))
>
>   (add-hook 'kill-buffer-query-functions #'dont-kill-scratch)
>
> Filipe Silva writes:
>
> > Dear good people of the emacs help list,
> >
> > I have a working snippet that advices both kill-buffer and
> kill-this-buffer
> > to not kill the *scratch* buffer:
> >
> >       (defun ninrod/scratch-bodyguard (buffer-assassin &rest arguments)
> >         (let ((buffer-to-kill (buffer-name (current-buffer))))
> >           (if (equal buffer-to-kill "*scratch*")
> >               (message "DENIED! don't kill my precious *scratch*!!")
> >             (apply buffer-assassin arguments))))
> >       (defun ninrod/scratch-protection (buffer-assassin &rest arguments)
> >         (let ((buffer-to-kill (car arguments)))
> >           (if (equal buffer-to-kill "*scratch*")
> >               (message "DENIED! don't kill my precious *scratch*!!")
> >             (apply buffer-assassin arguments))))
> >       (advice-add #'kill-this-buffer :around #'ninrod/scratch-bodyguard)
> >       (advice-add #'kill-buffer :around #'ninrod/scratch-protection)
>
> Considering 'kill-this-buffer' calls 'kill-buffer', advising
> 'kill-buffer' alone is enough.
>
> > The problem is that these lines:
> >
> >       (message "DENIED! don't kill my precious *scratch*!!")
> >      (apply buffer-assassin arguments))))
> >
> > Are repeated in both functions, so I thought that I could apply the DRY
> > principle and refactor the snippet to this:
> >
> >     (defun ninrod--protection (buffer-assassin buffer-to-kill &rest
> > arguments)
> >       (if (equal buffer-to-kill "*scratch*")
> >           (message "DENIED! don't kill my precious *scratch*!!")
> >         (apply buffer-assassin arguments)))
> >     (defun ninrod/scratch-bodyguard (buffer-assassin &rest arguments)
> >       (let ((buffer-to-kill (buffer-name (current-buffer))))
> >         (ninrod--protection 'buffer-assassin buffer-to-kill arguments)))
> >     (defun ninrod/scratch-protection (buffer-assassin &rest arguments)
> >       (let ((buffer-to-kill (car arguments)))
> >         (ninrod--protection 'buffer-assassin buffer-to-kill arguments)))
> >     (advice-add #'kill-this-buffer :around #'ninrod/scratch-bodyguard)
> >     (advice-add #'kill-buffer :around #'ninrod/scratch-protection)
>
> You should not quote 'buffer-assassin'. And when you pass '&rest
> arguments' to 'ninrod/scratch-protection', you should use
> 'apply'. Something like the following works from here.
>
>   (defun ninrod/scratch-bodyguard (buffer-assassin &rest arguments)
>     (let ((buffer-to-kill (buffer-name (current-buffer))))
>       (apply #'ninrod--protection buffer-assassin buffer-to-kill
> arguments)))
>
>   (advice-add #'kill-this-buffer :around #'ninrod/scratch-bodyguard)
>
> > This causes all hell to break loose. Now I can't even close emacs,
> because
> > apparently emacs tries to kill all buffers
> > and as I've just tampered with the kill buffer functions, well, it's bad.
> > Very bad.
> >
> > I know I mean well, but I'm must be doing something very stupid. For
> > starters, I don't know if I can really pass around
> > functions as parameters? So it could be that?
> >
> > How would you refactor that snippet to apply the dry principle?
> >
> > thanks in advance,
> >
> > Filipe.
>
>


  reply	other threads:[~2017-02-10 16:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-10 11:43 Seeking Advice about refactoring and advice snippet Filipe Silva
2017-02-10 13:08 ` Yuri Khan
2017-02-10 16:22   ` Filipe Silva
2017-02-10 13:44 ` Narendra Joshi
2017-02-10 14:17 ` Chunyang Xu
2017-02-10 16:24   ` Filipe Silva [this message]
2017-02-10 17:02 ` Filipe Silva
2017-02-10 19:55 ` Stefan Monnier
2017-02-10 23:12   ` Filipe Silva
2017-02-11  0:01     ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAEwkUWMaSmxbSP+jchaiGghAxhwOrHnm_bZ8N=mQsjoKXU_oAA@mail.gmail.com' \
    --to=filipe.silva@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=mail@xuchunyang.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.