Temporarily disable a write hook?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Temporarily disable a write hook?

Skip Montanaro
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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Temporarily disable a write hook?

Yuri Khan-2
On Fri, Jul 14, 2017 at 10:03 PM, Skip Montanaro
<[hidden email]> wrote:

> (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.

Are you in full control of how this CSV file is edited?

If you are, you could double-quote the cell that has the significant
trailing space. (Of course this assumes a specification-compliant
parser.)

On the other hand, if that CSV file is intermediate data subject to
import/export, that won’t help.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Temporarily disable a write hook?

Skip Montanaro
Yeah, I am in complete control. As it turns out, I'm one of the
authors of Python's csv module, so I'm very familiar with quoting
fields. :-/ When I generated this particular file I explicitly
disabled quoting because it would have complicated some of my
searches, and I knew none of the patterns contained commas.

I solved this particular case by moving the column containing the
regular expressions left, leaving a column which should never contain
spaces as the rightmost column.

I'd still be curious to see if there is an elegant solution to this in Emacs.

Skip

On Fri, Jul 14, 2017 at 10:11 AM, Yuri Khan <[hidden email]> wrote:

> On Fri, Jul 14, 2017 at 10:03 PM, Skip Montanaro
> <[hidden email]> wrote:
>
>> (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.
>
> Are you in full control of how this CSV file is edited?
>
> If you are, you could double-quote the cell that has the significant
> trailing space. (Of course this assumes a specification-compliant
> parser.)
>
> On the other hand, if that CSV file is intermediate data subject to
> import/export, that won’t help.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Temporarily disable a write hook?

Lele Gaifax
I use https://github.com/purcell/whitespace-cleanup-mode, that mostly does the
right thing for me: it saves the trouble when I have to edit a source authored
by some less-picky-than-me on whitespace issues. When I really want to get rid
of trailing spaces, I do that explicitly, save and reload the buffer: from
then on, the mode above will keep it clean.

ciao, lele.
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
[hidden email]  |                 -- Fortunato Depero, 1929.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Temporarily disable a write hook?

Skip Montanaro
> I use https://github.com/purcell/whitespace-cleanup-mode, that mostly does the
> right thing for me.

Thanks, Lele. That looks like a good choice.

Skip

Loading...