From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.help Subject: Re: Temporarily suppress a hook? Date: Thu, 12 Jul 2018 19:43:15 +0200 Organization: Aioe.org NNTP Server Message-ID: <86muuw9wp8.fsf@zoho.com> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1531417398 16730 195.159.176.226 (12 Jul 2018 17:43:18 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 12 Jul 2018 17:43:18 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jul 12 19:43:14 2018 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 1fdfcX-0004Gk-Gt for geh-help-gnu-emacs@m.gmane.org; Thu, 12 Jul 2018 19:43:13 +0200 Original-Received: from localhost ([::1]:33333 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fdfee-0000BJ-Lp for geh-help-gnu-emacs@m.gmane.org; Thu, 12 Jul 2018 13:45:24 -0400 Original-Path: usenet.stanford.edu!goblin3!goblin2!goblin.stu.neva.ru!aioe.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 53 Original-NNTP-Posting-Host: onLrbz09yV+MU3RaxdbMkg.user.gioia.aioe.org Original-X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.3 Mail-Copies-To: never Cancel-Lock: sha1:PeK0Mp/wiO/ZP4rHWQwkV4zMPLc= Original-Xref: usenet.stanford.edu gnu.emacs.help:223291 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:117414 Archived-At: Skip Montanaro wrote: > I have before-save-hook defined in ~/.emacs > > (add-hook 'before-save-hook > 'delete-trailing-whitespace) > > This is useful almost all the time. I just > discovered a case where it's not so helpful > (a file full of regular expressions to feed > into "grep -E -f ..." > > I can think of various ways to tweak the > regular expressions to not require trailing > whitespace, but let's assume for a moment > that's not possible. Is it possible to > suppress the before-save-hook on > a per-file basis? You just need to find some property of the exception file by which it is identifiable to the program (that is executing the hook functionality), be it its mode (best, as easily checked), file name suffix, or some property of the text (which might be more difficult to get unambiguously right). Then just do like this: ;; (setq before-save-hook nil) (defun before-save-hook-f () ;; insert `if' branch here, and ;; don't do anything if such a file, else ;; do: (delete-trailing-whitespace) ) (add-hook 'before-save-hook #'before-save-hook-f) BTW for everyone viewing pleasure here is a "one shot hook" that is executed only once, i.e. it removes itself after the first execution: (defun add-one-shot-hook-args-ignored (hook fun) (let ((name (cl-gensym))) (setf (symbol-function name) (lambda (&rest _unused) (remove-hook hook name) (funcall fun) )) (add-hook hook name) )) http://user.it.uu.se/~embe8573/emacs-init/w3m/w3m-unisearch.el -- underground experts united http://user.it.uu.se/~embe8573