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 14:24: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 1486743901 16286 195.159.176.226 (10 Feb 2017 16:25:01 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 10 Feb 2017 16:25:01 +0000 (UTC) Cc: Help Gnu Emacs mailing list To: Chunyang Xu Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 10 17:24:57 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 1ccE0C-0003Xt-61 for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 17:24:52 +0100 Original-Received: from localhost ([::1]:44687 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ccE0E-0005gF-JK for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 11:24:54 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48488) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ccDzf-0005e7-1q for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 11:24:20 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ccDzd-0002ch-Db for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 11:24:19 -0500 Original-Received: from mail-ot0-x22f.google.com ([2607:f8b0:4003:c0f::22f]:34207) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ccDzd-0002cd-7j for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 11:24:17 -0500 Original-Received: by mail-ot0-x22f.google.com with SMTP id f9so31799392otd.1 for ; Fri, 10 Feb 2017 08:24:17 -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 :cc; bh=IAWMWp/KYw5zcP7aPb92IlAqQOxOVWS4WBEZFt1+YAI=; b=jFVf+1VD+ZW3jKkgPjPgzjt+jr55/5YLsU6nQsr4IEa21xNwbLTlDYRRykQVLkFr7R mIXOH6TRWPBBdsL/f5HAJVPVTQ7MxoN9FmnEtvJiH7DJNzJd7xd0dMyUaWE4DC9C5Qqk OSoOTx1oe27NjEEnnUQWagoWkB7Rf8GqmdRRvQnhWOg+xnEfRAu1RUPKOQk1Y1x5SN0r EZKwGG9VmksR7x8goPSL+nozf80/J14ZlwgAFzNO2yroFzoITrFBvdlwxfX+Y3Y+rquH HLAL/iceemI+Ye3MthAvZzaScU/9vFuaRt//z4bM8f1KWXcpLo9xmUmliP0ZJfHcmnO2 7peQ== 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:cc; bh=IAWMWp/KYw5zcP7aPb92IlAqQOxOVWS4WBEZFt1+YAI=; b=l9PajI1r15ssYuyAJtajpeNoPTaiCKuyfCEGs1G8KiD+2dpJKB1gPRh3prFdDlj2Ue xySoNz55rAndDYqBCc0D6oItVLxEzl3JumITjYnkcVW1CoF/Qhz0rdQUrPRP7Jr8OrIa bVZAJ1RqUDpvdZXfQswJI/D3ntgwVILhye6i75rGXuqRE3AYgUMCjJFXKoT6tFVtdPMR I992NHgxdoCvBbAaZeU1vfaL6sJC/fhIeLe4QRigWgMAy9xB/6WPy8UWYnxUc+oBo2Ky HlEOspcB6Yqp6FQHKAPGCsVrKODYB8qvWkZrvcDi+stxzVvMEKEsCSr28HoeMhQIxrhc ptXg== X-Gm-Message-State: AMke39kgEpb3Mtv/j4lTkFtJTw5gcZfbo1DPElgqRTx9gUj+IpRTod50lzmgYP8EG48CWRv6WbOJ8ga5cfCaJw== X-Received: by 10.157.56.27 with SMTP id i27mr5760243otc.160.1486743856525; Fri, 10 Feb 2017 08:24:16 -0800 (PST) Original-Received: by 10.157.56.242 with HTTP; Fri, 10 Feb 2017 08:24: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::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:112292 Archived-At: 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 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. > >