all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Temporarily suppress a hook?
@ 2018-07-09  0:02 Skip Montanaro
  2018-07-09  0:43 ` Drew Adams
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Skip Montanaro @ 2018-07-09  0:02 UTC (permalink / raw)
  To: Help GNU Emacs

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?

Thx,

Skip



^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Temporarily suppress a hook?
  2018-07-09  0:02 Temporarily suppress a hook? Skip Montanaro
@ 2018-07-09  0:43 ` Drew Adams
  2018-07-09  1:39   ` Skip Montanaro
  2018-07-09  4:43 ` Bob Proulx
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 12+ messages in thread
From: Drew Adams @ 2018-07-09  0:43 UTC (permalink / raw)
  To: Skip Montanaro, Help GNU Emacs

> 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?

Both `add-hook' and `remove-hook' take an optional
LOCAL argument, to affect only the current buffer
(the buffer-local value of the hook).



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
  2018-07-09  0:43 ` Drew Adams
@ 2018-07-09  1:39   ` Skip Montanaro
  2018-07-09 12:07     ` João Távora
  0 siblings, 1 reply; 12+ messages in thread
From: Skip Montanaro @ 2018-07-09  1:39 UTC (permalink / raw)
  To: Drew Adams; +Cc: Help GNU Emacs

> I have before-save-hook defined in ~/.emacs
> > (add-hook 'before-save-hook 'delete-trailing-whitespace)


Both `add-hook' and `remove-hook' take an optional
> LOCAL argument, to affect only the current buffer
> (the buffer-local value of the hook).
>

It's not clear how that helps me. In essentially every other file I edit, I
want to delete trailing whitespace, so I enable it globally. It seems
buffer-local enabling of that hook would be appropriate if I only
occasionally wanted to enable it.

Skip

>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
  2018-07-09  0:02 Temporarily suppress a hook? Skip Montanaro
  2018-07-09  0:43 ` Drew Adams
@ 2018-07-09  4:43 ` Bob Proulx
  2018-07-09  5:21 ` Óscar Fuentes
  2018-07-09 14:41 ` Stefan Monnier
  3 siblings, 0 replies; 12+ messages in thread
From: Bob Proulx @ 2018-07-09  4:43 UTC (permalink / raw)
  To: help-gnu-emacs

Skip Montanaro wrote:
> Is it possible to suppress the before-save-hook on a
> per-file basis?

I know this isn't quite what you are wanting but sometimes a crazy
squirrel cage argument which is outside the normal box can still be
useful...

Could you suppress you entire .emacs so as to avoid all of your
customization?

  emacs -q that-special-flie

Or you would pick another user on your system that has a compatible
file to you but without the hooks.  Perhaps root?  Perhaps another
user?  Use their .emacs file for that one file.

  emacs -u root that-special-flie

I know that will be less desirable for many reasons.  You won't get
your normal configuration for other things.  But if you can take a
default configuration for just that moment then it would suppress your
hooks as they would not be added then.

Bob



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
  2018-07-09  0:02 Temporarily suppress a hook? Skip Montanaro
  2018-07-09  0:43 ` Drew Adams
  2018-07-09  4:43 ` Bob Proulx
@ 2018-07-09  5:21 ` Óscar Fuentes
  2018-07-09  6:00   ` tomas
  2018-07-09 14:41 ` Stefan Monnier
  3 siblings, 1 reply; 12+ messages in thread
From: Óscar Fuentes @ 2018-07-09  5:21 UTC (permalink / raw)
  To: help-gnu-emacs

Skip Montanaro <skip.montanaro@gmail.com> writes:

> 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?

AFAIK, no. But you can use your own function instead of
delete-trailing-whitespace:

(defun my-delete-trailing-whitespace ()
  (interactive)
  (unless my-special-buffer
    (delete-trailing-whitespace)))

(add-hook 'before-save-hook 'my-delete-trailing-whitespace)

Or advice delete-trailing-whitespace.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
  2018-07-09  5:21 ` Óscar Fuentes
@ 2018-07-09  6:00   ` tomas
  2018-07-09  7:53     ` Óscar Fuentes
  2018-07-09 11:01     ` Skip Montanaro
  0 siblings, 2 replies; 12+ messages in thread
From: tomas @ 2018-07-09  6:00 UTC (permalink / raw)
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, Jul 09, 2018 at 07:21:12AM +0200, Óscar Fuentes wrote:
> Skip Montanaro <skip.montanaro@gmail.com> writes:

[...]

> AFAIK, no. But you can use your own function instead of
> delete-trailing-whitespace:
> 
> (defun my-delete-trailing-whitespace ()
>   (interactive)
>   (unless my-special-buffer
>     (delete-trailing-whitespace)))

> (add-hook 'before-save-hook 'my-delete-trailing-whitespace)
> 
> Or advice delete-trailing-whitespace.

Or perhaps make the hook read a local variable you set in
the file (see "file local variables") and act accordingly.

This way you could declare whithin those special files that you
don't want trailing space removed.

Cheers
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAltC+esACgkQBcgs9XrR2kYfqgCfSINQuXmDc0x0G8R49ru0b49N
dBUAnR1deWZTVdumkW8QpAGtObCxyYZc
=xV7B
-----END PGP SIGNATURE-----



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
  2018-07-09  6:00   ` tomas
@ 2018-07-09  7:53     ` Óscar Fuentes
  2018-07-09 11:01     ` Skip Montanaro
  1 sibling, 0 replies; 12+ messages in thread
From: Óscar Fuentes @ 2018-07-09  7:53 UTC (permalink / raw)
  To: help-gnu-emacs

<tomas@tuxteam.de> writes:

> On Mon, Jul 09, 2018 at 07:21:12AM +0200, Óscar Fuentes wrote:
>> Skip Montanaro <skip.montanaro@gmail.com> writes:
>
> [...]
>
>> AFAIK, no. But you can use your own function instead of
>> delete-trailing-whitespace:
>> 
>> (defun my-delete-trailing-whitespace ()
>>   (interactive)
>>   (unless my-special-buffer
>>     (delete-trailing-whitespace)))
>
>> (add-hook 'before-save-hook 'my-delete-trailing-whitespace)
>> 
>> Or advice delete-trailing-whitespace.
>
> Or perhaps make the hook read a local variable you set in
> the file (see "file local variables") and act accordingly.
>
> This way you could declare whithin those special files that you
> don't want trailing space removed.

I thought that it was unnecessary to explain that my-special-buffer is
whatever predicate the OP feels convenient.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
  2018-07-09  6:00   ` tomas
  2018-07-09  7:53     ` Óscar Fuentes
@ 2018-07-09 11:01     ` Skip Montanaro
  2018-07-09 17:33       ` Marcin Borkowski
  1 sibling, 1 reply; 12+ messages in thread
From: Skip Montanaro @ 2018-07-09 11:01 UTC (permalink / raw)
  To: tomas; +Cc: Óscar Fuentes, Help GNU Emacs

On Mon, Jul 9, 2018, 1:00 AM <tomas@tuxteam.de> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Mon, Jul 09, 2018 at 07:21:12AM +0200, Óscar Fuentes wrote:
> > Skip Montanaro <skip.montanaro@gmail.com> writes:
>
> [...]
>
> > AFAIK, no. But you can use your own function instead of
> > delete-trailing-whitespace:
> >
> > (defun my-delete-trailing-whitespace ()
> >   (interactive)
> >   (unless my-special-buffer
> >     (delete-trailing-whitespace)))
>
> > (add-hook 'before-save-hook 'my-delete-trailing-whitespace)
> >
> > Or advice delete-trailing-whitespace.
>
> Or perhaps make the hook read a local variable you set in
> the file (see "file local variables") and act accordingly.
>
> This way you could declare whithin those special files that you
> don't want trailing space removed.
>

Thanks all. Óscar's idea seems to come closest to a solution. I might use
that to suppress the hook for files with, say, a ".txt" extension. Those
would benefit least from scrubbing all trailing whitespace. Or... I could
use a prefix argument and check to see if it's been set to a magic value.
C-u -7 C-x C-s. Assuming I can read any prefix arg set by the user.

Unfortunately, file local variables aren't too helpful here. "grep -f file"
reads a series of patterns from the named file, so interprets every line as
a pattern. While it might work to track on a file local variable section,
you run the risk of a line being a false positive match for a line in the
file(s) you're running grep against.

Skip

>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
  2018-07-09  1:39   ` Skip Montanaro
@ 2018-07-09 12:07     ` João Távora
  0 siblings, 0 replies; 12+ messages in thread
From: João Távora @ 2018-07-09 12:07 UTC (permalink / raw)
  To: Skip Montanaro; +Cc: help-gnu-emacs@gnu.org List

On Mon, Jul 9, 2018, 02:40 Skip Montanaro <skip.montanaro@gmail.com> wrote:

> > I have before-save-hook defined in ~/.emacs
> > > (add-hook 'before-save-hook 'delete-trailing-whitespace)
>
>
> Both `add-hook' and `remove-hook' take an optional
> > LOCAL argument, to affect only the current buffer
> > (the buffer-local value of the hook).
> >
>
> It's not clear how that helps me. In essentially every other file I edit, I
> want to delete trailing whitespace, so I enable it globally. It seems
> buffer-local enabling of that hook would be appropriate if I only
> occasionally wanted to enable it.
>

Indeed, using remove-hook with a local argument in those special buffers
won't help you if you've already affected the global value of
before-save-hook.

But you can add the hook locally, as Drew suggests, in those buffers where
you need it, and refrain from doing so in the buffers where you don't.
Prog-mode is a very pervasive mode: many modes derive from it.

Here's what I mean

(add-hook 'prog-mode-hook
  (lambda ()
    (add-hook 'before-save-hook 'delete-trailing-whitespace nil t)))

Now, in those special files/buffers (some special major mode?), a
remove-hook should do what you want.

João


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
  2018-07-09  0:02 Temporarily suppress a hook? Skip Montanaro
                   ` (2 preceding siblings ...)
  2018-07-09  5:21 ` Óscar Fuentes
@ 2018-07-09 14:41 ` Stefan Monnier
  3 siblings, 0 replies; 12+ messages in thread
From: Stefan Monnier @ 2018-07-09 14:41 UTC (permalink / raw)
  To: help-gnu-emacs

> I have before-save-hook defined in ~/.emacs
> (add-hook 'before-save-hook 'delete-trailing-whitespace)
[...]
> possible.  Is it possible to suppress the before-save-hook on a
> per-file basis?

I suggest you file a feature request (via M-x report-emacs-bug)

I think it should be possible to buffer-locally "remove" a function from
a hook, even if that function was added globally (I toyed with an
implementation of that idea which added `(not . FUNCTION)` to the
(buffer-local part of the) hook, and then `run-hook` would honor such
a value by disregarding any subsequent occurrence of FUNCTION).

With my hack, you'd be able to do:

    (add-hook 'my-special-mode-hook
              (lambda ()
                (remove-hook 'before-save-hook
                             #'delete-trailing-whitespace 'local)))

so that buffers using `my-special-mode` won't delete their trailing spaces.


        Stefan




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
  2018-07-09 11:01     ` Skip Montanaro
@ 2018-07-09 17:33       ` Marcin Borkowski
  0 siblings, 0 replies; 12+ messages in thread
From: Marcin Borkowski @ 2018-07-09 17:33 UTC (permalink / raw)
  To: Skip Montanaro; +Cc: Óscar Fuentes, Help GNU Emacs


On 2018-07-09, at 13:01, Skip Montanaro <skip.montanaro@gmail.com> wrote:

> C-u -7 C-x C-s. Assuming I can read any prefix arg set by the user.

Yes, you can.  See http://mbork.pl/2017-07-09_current-prefix-arg and an
example use in http://mbork.pl/2018-07-02_Smart_yanking .

Hth,

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Temporarily suppress a hook?
       [not found] <mailman.3286.1531094567.1292.help-gnu-emacs@gnu.org>
@ 2018-07-12 17:43 ` Emanuel Berg
  0 siblings, 0 replies; 12+ messages in thread
From: Emanuel Berg @ 2018-07-12 17:43 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2018-07-12 17:43 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-09  0:02 Temporarily suppress a hook? Skip Montanaro
2018-07-09  0:43 ` Drew Adams
2018-07-09  1:39   ` Skip Montanaro
2018-07-09 12:07     ` João Távora
2018-07-09  4:43 ` Bob Proulx
2018-07-09  5:21 ` Óscar Fuentes
2018-07-09  6:00   ` tomas
2018-07-09  7:53     ` Óscar Fuentes
2018-07-09 11:01     ` Skip Montanaro
2018-07-09 17:33       ` Marcin Borkowski
2018-07-09 14:41 ` Stefan Monnier
     [not found] <mailman.3286.1531094567.1292.help-gnu-emacs@gnu.org>
2018-07-12 17:43 ` Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.