all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Temporarily disable a write hook?
@ 2017-07-14 15:03 Skip Montanaro
  2017-07-14 15:11 ` Yuri Khan
  0 siblings, 1 reply; 5+ messages in thread
From: Skip Montanaro @ 2017-07-14 15:03 UTC (permalink / raw
  To: Help GNU Emacs

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



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

* Re: Temporarily disable a write hook?
  2017-07-14 15:03 Temporarily disable a write hook? Skip Montanaro
@ 2017-07-14 15:11 ` Yuri Khan
  2017-07-14 15:43   ` Skip Montanaro
  0 siblings, 1 reply; 5+ messages in thread
From: Yuri Khan @ 2017-07-14 15:11 UTC (permalink / raw
  To: Skip Montanaro; +Cc: Help GNU Emacs

On Fri, Jul 14, 2017 at 10:03 PM, Skip Montanaro
<skip.montanaro@gmail.com> 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.



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

* Re: Temporarily disable a write hook?
  2017-07-14 15:11 ` Yuri Khan
@ 2017-07-14 15:43   ` Skip Montanaro
  2017-07-14 16:13     ` Lele Gaifax
  0 siblings, 1 reply; 5+ messages in thread
From: Skip Montanaro @ 2017-07-14 15:43 UTC (permalink / raw
  To: Yuri Khan; +Cc: Help GNU Emacs

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 <yuri.v.khan@gmail.com> wrote:
> On Fri, Jul 14, 2017 at 10:03 PM, Skip Montanaro
> <skip.montanaro@gmail.com> 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.



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

* Re: Temporarily disable a write hook?
  2017-07-14 15:43   ` Skip Montanaro
@ 2017-07-14 16:13     ` Lele Gaifax
  2017-07-14 17:29       ` Skip Montanaro
  0 siblings, 1 reply; 5+ messages in thread
From: Lele Gaifax @ 2017-07-14 16:13 UTC (permalink / raw
  To: help-gnu-emacs

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.
lele@metapensiero.it  |                 -- Fortunato Depero, 1929.




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

* Re: Temporarily disable a write hook?
  2017-07-14 16:13     ` Lele Gaifax
@ 2017-07-14 17:29       ` Skip Montanaro
  0 siblings, 0 replies; 5+ messages in thread
From: Skip Montanaro @ 2017-07-14 17:29 UTC (permalink / raw
  To: Lele Gaifax; +Cc: Help GNU Emacs

> 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



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

end of thread, other threads:[~2017-07-14 17:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-14 15:03 Temporarily disable a write hook? Skip Montanaro
2017-07-14 15:11 ` Yuri Khan
2017-07-14 15:43   ` Skip Montanaro
2017-07-14 16:13     ` Lele Gaifax
2017-07-14 17:29       ` Skip Montanaro

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.