help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Temporarily suppress a hook?


From: Emanuel Berg
Subject: Re: Temporarily suppress a hook?
Date: Thu, 12 Jul 2018 19:43:15 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]