From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Filipe Silva Newsgroups: gmane.emacs.help Subject: Re: Seeking Advice about refactoring and advice snippet Date: Fri, 10 Feb 2017 15:02:16 -0200 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: blaine.gmane.org 1486746319 23616 195.159.176.226 (10 Feb 2017 17:05:19 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 10 Feb 2017 17:05:19 +0000 (UTC) To: Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 10 18:05:16 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ccEdI-0005wn-Ao for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 18:05:16 +0100 Original-Received: from localhost ([::1]:44957 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ccEdN-0000Oo-V7 for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 12:05:21 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59538) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ccEaR-0007IM-Mw for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 12:02:21 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ccEaQ-00066d-He for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 12:02:19 -0500 Original-Received: from mail-ot0-x229.google.com ([2607:f8b0:4003:c0f::229]:33885) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ccEaQ-00066U-BC for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 12:02:18 -0500 Original-Received: by mail-ot0-x229.google.com with SMTP id f9so32693157otd.1 for ; Fri, 10 Feb 2017 09:02:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=RQejS/CsOjFNon5r1r0VleeiwzKD5BvDPcOKzkstcXE=; b=A2AtrWdVZskLCMLuCr5MsRjY8vQE7iBYQAWyD/pOvVeOetyu3Fo1dOlLp09pfCGBvP +0vk+XeK2MnoJ3Ffj3pIVcnBYrjSvl4ZnRcDFus8LMJxzJ/WKomj56PuM8neS+Fijd1z G1dA9v3DkQqVHtQq6Km5zkKGvZ+nblGr1pgqCYFzpIgr6uHj43mDiQuYinacUuz9ivQ1 lE317H9UP3yN1OQBENXTkpUIIoL+Y73rDfUbZdnMDGi0ADkPc4dXl94bqByzbpPQMqV5 oFka+aVV1MIbhvCJfgvTCnf0gg7Z11yvW54vrK21eYPGeTXrIo0M17sz25bafI5fY7cj Yqww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=RQejS/CsOjFNon5r1r0VleeiwzKD5BvDPcOKzkstcXE=; b=VhiGem7dI0dK1El9g/q+eNBx0RbXOIpoScxwGieHfOA+3PRw/6gjFjpQ8YVgK0DgSz Q6CkslZ47lhMy+CzqqIL2PeLQ0x9/bcppjBN4uZnDX7gmPFBI/c7bDwPwwzV5jZiHP2A eWfisrTq+SKyNjat1qx7x7z0rjpZSOEixR9FN+Nlvop3d1lB3RsdLiaxaxjlzkVlqzZp wO61dly1AfCsqZi2W76aKbeR5FCHEZNV9F2GOVA1Kibn/qjcqOmlJyh6e541DpDZeoFG o4TZ7343eZ0Z/f8aJznPbudgzE80nobRuNsBjm6wtIMWTg/G+GJ3AgWIQAO+IA/BThwF i1sA== X-Gm-Message-State: AMke39n+SBMGkDo4eU5Y0iAqOXCpEHWGXcJVS5jkaS72g2Ax/6Vjc3NfNwleiZQv2Wj73LxF+YnPsSW/EQU23Q== X-Received: by 10.157.4.53 with SMTP id 50mr5794919otc.234.1486746137090; Fri, 10 Feb 2017 09:02:17 -0800 (PST) Original-Received: by 10.157.56.242 with HTTP; Fri, 10 Feb 2017 09:02:16 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4003:c0f::229 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:112293 Archived-At: Well, someone at emacs.stackexchange pointed out the the &rest keyword used in my ninrod--protection function was to blame. That fixed the problem. thanks! On Fri, Feb 10, 2017 at 9:43 AM, Filipe Silva wrote: > 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. > >