From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Chunyang Xu Newsgroups: gmane.emacs.help Subject: Re: Seeking Advice about refactoring and advice snippet Date: Fri, 10 Feb 2017 22:17:09 +0800 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1486762411 5488 195.159.176.226 (10 Feb 2017 21:33:31 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 10 Feb 2017 21:33:31 +0000 (UTC) User-Agent: mu4e 0.9.18; emacs 26.0.50.2 Cc: Help Gnu Emacs mailing list To: Filipe Silva Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 10 22:33:26 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 1ccIoo-00012g-55 for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 22:33:26 +0100 Original-Received: from localhost ([::1]:46025 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ccIot-00026V-Q2 for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 16:33:31 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37381) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ccC18-0001FM-AJ for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 09:17:43 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ccC14-000596-AV for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 09:17:42 -0500 Original-Received: from smtpbg179.qq.com ([119.147.194.222]:33063) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ccC13-000580-NR for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 09:17:38 -0500 X-QQ-mid: bizesmtp16t1486736226ti1ksfg0 Original-Received: from Chunyangs-MacBook-Air.local (unknown [114.236.198.223]) by esmtp4.qq.com (ESMTP) with id ; Fri, 10 Feb 2017 22:17:05 +0800 (CST) X-QQ-SSF: 0110000000200050F410B00A0000000 X-QQ-FEAT: /peYvah/M2UeHbfUKuuJqTLY60MY92w037Y9elWIKE7jluEZ0+2yya/5hNQPT uWewBcWRy4MmXDGONLTGKf7PoPQuZ3U8SnLYYOGbPs9ofmr0Zm6dGLjH3chGJIegIH+zp4x q9rRrFGvX+cMcrgiHzLOUT+5AgUN85wwvLaDGDzUN4vS7fCfAwrfWADsSoQjGJVREkobW8M Rm0y2Uvzk2TOtTqPUiTEcBw8HOYLHGUuV1+wP8LUrBDJ4zy2f5IM65It+zwbCsRU= X-QQ-GoodBg: 0 In-reply-to: X-QQ-SENDSIZE: 520 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 119.147.194.222 X-Mailman-Approved-At: Fri, 10 Feb 2017 16:32:59 -0500 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:112297 Archived-At: 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.