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

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

Temporarily disable a write hook?


From: Skip Montanaro
Subject: Temporarily disable a write hook?
Date: Fri, 14 Jul 2017 10:03:16 -0500

I'm a Python programmer, so I often edit Python source. I don't want
trailing whitespace. That holds true for almost everything else I edit
(C, SQL, Bash, etc...). In fact, even if I'm editing text files (ReST,
Markdown, LaTeX, whatever), I want this behavior. Accordingly, I have
this in ~/.emacs:

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

Well, I got burned by this. I was editing a CSV file in which one cell
in the last column of a row ended with a space. It was, unfortunately,
a regular expression, so the space was significant.

Given that I *do* want to zap trailing whitespace on write *almost all
the time*, how best to solve this issue? Is there a way to
conditionally invoke the before-save-hook, or do I simply write my own
wrapper which stares at the file extension to decide whether to call
delete-trailing-whitespace? I was hoping that save-buffer didn't pay
attention to the numeric prefix, but that was too much to ask. It is
intimately concerned with the value of the numeric prefix. (I suppose
I could write my own wrapper for save-buffer which suppresses the
before-save-hook if the numeric prefix is negative, or something, but
I'd be opening myself up for a later behavioral change to Emacs.) Also
out-of-the-ordinary in this particular example, though the file was in
CSV format, I didn't give it a ".csv" extension for some reason, so I
couldn't just say, "ah, it ends in '.csv', avoid the call to
(delete-trailing-whitespace)." Oh, the tangled webs we weave...

Skip



reply via email to

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