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: Seeking Advice about refactoring and advice snippet Date: Fri, 10 Feb 2017 09:43:53 -0200 Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: blaine.gmane.org 1486727076 2547 195.159.176.226 (10 Feb 2017 11:44:36 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 10 Feb 2017 11:44:36 +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 12:44:31 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 1cc9cs-0000Ed-HC for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 12:44:30 +0100 Original-Received: from localhost ([::1]:43190 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc9cy-0003Ur-1F for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 06:44:36 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56412) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc9cL-0003To-Oz for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 06:43:58 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cc9cK-0004r9-Ew for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 06:43:57 -0500 Original-Received: from mail-oi0-x22f.google.com ([2607:f8b0:4003:c06::22f]:34932) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cc9cK-0004qa-6s for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 06:43:56 -0500 Original-Received: by mail-oi0-x22f.google.com with SMTP id j15so19188457oih.2 for ; Fri, 10 Feb 2017 03:43:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=heyhWVVOJ+YAr5pqEpcMp1Cs6hYocERLLzB1L5uXVDk=; b=CUK9VXZpxpYINwuHxihU64ffc54hGrHmg3VrxMITq0JE2NHYPh7AkRkqiF6dw7JGLA LO2430bFhq+Ux8VxClKWRi+V3nfpTtfmATVcUKD0luJw1lKuEGrjuXsxoPoU6cFbc4HT 373N5I/B3f6HY+CVnT30mi9sB8TNhv2rQPUPl/073LUv96lBp8mWnvPxUDT8iTEq6WKv sl83xW6bXhSlZSKGd2MDtzzhF4uGCIljN0eddJqZ4EHrDSlMcMS8PC2b58/6wRpAcWnA t2PYFY/+LTMFXPTrxeNAlYkvL/Ccuwkz85w+sHNcBtujlcxf21W2jSSnezH+epXSe/Ak udfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=heyhWVVOJ+YAr5pqEpcMp1Cs6hYocERLLzB1L5uXVDk=; b=GmuNZqwMGDeXZiqMYHYpzJSJ/2uTIpD8tdihpe9fzVmL3KmkcUt4upbr7jJR2XOI7b L5TVsUj8quTKqbIXFikuUPchC7jHlSFvv8VYndgVjKUncZ+huraFXIGeeijB+JFddDG6 XV3SgWwGb/P26I0E239772/Me0N7qTy+/cDIL8DRpKfS8PKmRlX6+qZMFKOMd19ekUA0 IQR/Ej6SUX//KUnA+eZ5785KRnSnxLiGgCKrHNbZWEfPJ/4r6e6n5sd8MAOBePPmb4S/ VG3GlPrOZw6QNtA6zVoF2b/DpU8GEi4yBKAUmTbhK7Xy4ZQckyG56Hl16WHQOSeWTE+L tYBQ== X-Gm-Message-State: AMke39n0ouu9R99rVRYkzHy7kVT8ZS3Ww2EpzuIvyBj6zIKSHh/AVlT0xypV2UblzUGy1/XJifUJX6AQltqk7w== X-Received: by 10.202.214.130 with SMTP id n124mr4083399oig.70.1486727033623; Fri, 10 Feb 2017 03:43:53 -0800 (PST) Original-Received: by 10.157.56.242 with HTTP; Fri, 10 Feb 2017 03:43:53 -0800 (PST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4003:c06::22f 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:112288 Archived-At: 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.