all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Seeking Advice about refactoring and advice snippet
@ 2017-02-10 11:43 Filipe Silva
  2017-02-10 13:08 ` Yuri Khan
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Filipe Silva @ 2017-02-10 11:43 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

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)

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)

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.


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

end of thread, other threads:[~2017-02-11  0:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.