GNU bug report logs - #65864
[PATCH] Add option to save a buffer without running save hooks

Previous Next

Package: emacs;

Reported by: Daniel Martín <mardani29 <at> yahoo.es>

Date: Mon, 11 Sep 2023 10:28:02 UTC

Severity: wishlist

Tags: patch

To reply to this bug, email your comments to 65864 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Mon, 11 Sep 2023 10:28:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Daniel Martín <mardani29 <at> yahoo.es>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Mon, 11 Sep 2023 10:28:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Daniel Martín <mardani29 <at> yahoo.es>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] Add option to save a buffer without running save hooks
Date: Mon, 11 Sep 2023 12:24:40 +0200
[Message part 1 (text/plain, inline)]
Tags: patch


Users can customize `before-save-hook' to add things like
`delete-trailing-whitespace' or `copyright-update'.  However, there are
cases where you want to save a buffer without running any save hooks
without changing your configuration and then changing it back.

I've attached a patch to make `save-buffer', when invoked with a
negative argument (C-u - C-x C-s), temporarily avoid running any save
hooks.

[0001-Add-option-to-save-a-buffer-without-running-save-hoo.patch (text/patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Mon, 11 Sep 2023 12:47:02 GMT) Full text and rfc822 format available.

Message #8 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Daniel Martín <mardani29 <at> yahoo.es>
Cc: 65864 <at> debbugs.gnu.org, Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Mon, 11 Sep 2023 15:45:39 +0300
> Date: Mon, 11 Sep 2023 12:24:40 +0200
> From:  Daniel Martín via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <bug-gnu-emacs <at> gnu.org>
> 
> Users can customize `before-save-hook' to add things like
> `delete-trailing-whitespace' or `copyright-update'.  However, there are
> cases where you want to save a buffer without running any save hooks
> without changing your configuration and then changing it back.
> 
> I've attached a patch to make `save-buffer', when invoked with a
> negative argument (C-u - C-x C-s), temporarily avoid running any save
> hooks.

Should we perhaps consider a more general approach: run a command
while disabling the hooks it calls?  It sounds strange to me to single
out just this one command.

I'm interested in hearing Stefan's opinions on this (as well as those
of anyone else who wants to speak up).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Mon, 11 Sep 2023 22:31:02 GMT) Full text and rfc822 format available.

Message #11 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 65864 <at> debbugs.gnu.org,
 Daniel Martín <mardani29 <at> yahoo.es>
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Mon, 11 Sep 2023 18:28:59 -0400
>> Users can customize `before-save-hook' to add things like
>> `delete-trailing-whitespace' or `copyright-update'.  However, there are
>> cases where you want to save a buffer without running any save hooks
>> without changing your configuration and then changing it back.

Do you have concrete examples, to helps us assess what's really at stake
here?  Maybe there are other ways to look at the problem :-)

"without running any save hooks" at all really?  Like without running
`write-contents-functions`, nor `write-file-functions`, nor
`write-region-annotat-functions`, nor `make-backup-file-name-function`, ...?
How 'bout the file-name-handlers, then?

Maybe `write-region` would be a better starting point than `buffer-save`?

>> I've attached a patch to make `save-buffer', when invoked with a
>> negative argument (C-u - C-x C-s), temporarily avoid running any save
>> hooks.
>
> Should we perhaps consider a more general approach: run a command
> while disabling the hooks it calls?  It sounds strange to me to single
> out just this one command.

Hmm... sounds iffy.  I generally prefer ordering a plain burger than
ordering a "cheeseburger but hold the cheese" :-)

IOW, provide *another* function which performs the core operation.
[ And we usually do that.  ]


        Stefan





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Tue, 12 Sep 2023 08:14:02 GMT) Full text and rfc822 format available.

Message #14 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Daniel Martín <mardani29 <at> yahoo.es>
To: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of
 text editors" <bug-gnu-emacs <at> gnu.org>
Cc: 65864 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>,
 Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Tue, 12 Sep 2023 10:11:08 +0200
Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of
text editors" <bug-gnu-emacs <at> gnu.org> writes:

>>> Users can customize `before-save-hook' to add things like
>>> `delete-trailing-whitespace' or `copyright-update'.  However, there are
>>> cases where you want to save a buffer without running any save hooks
>>> without changing your configuration and then changing it back.
>
> Do you have concrete examples, to helps us assess what's really at stake
> here?  Maybe there are other ways to look at the problem :-)
>

For example, I have `delete-trailing-whitespace' in my
`before-save-hook', but for a particular buffer I wanted to save it
without removing the trailing whitespace, because trailing whitespace
was syntactically meaningful in that case (the buffer contained some
Markdown-like source code).

I searched the Internet for some solutions and what I ended up doing is
C-x C-q (to make the buffer read-only), C-x C-s, and C-x C-q to make the
buffer writable again (from
https://stackoverflow.com/questions/14913398/in-emacs-how-do-i-save-without-running-save-hooks).
It felt a bit like a "hack".




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Tue, 12 Sep 2023 08:14:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Tue, 12 Sep 2023 13:00:02 GMT) Full text and rfc822 format available.

Message #20 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Daniel Martín <mardani29 <at> yahoo.es>
Cc: 65864 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Tue, 12 Sep 2023 08:59:39 -0400
> For example, I have `delete-trailing-whitespace' in my
> `before-save-hook', but for a particular buffer I wanted to save it
> without removing the trailing whitespace, because trailing whitespace
> was syntactically meaningful in that case (the buffer contained some
> Markdown-like source code).

So you needed it for use by a human rather than for use by ELisp code?

> I searched the Internet for some solutions and what I ended up doing is
> C-x C-q (to make the buffer read-only), C-x C-s, and C-x C-q to make the
> buffer writable again (from
> https://stackoverflow.com/questions/14913398/in-emacs-how-do-i-save-without-running-save-hooks).
> It felt a bit like a "hack".

It's a hack, indeed, and it could fail if the hook function was careful to
let-bind `inhibit-read-only`.

Personally I think I would have used `C-x h M-x write-region RET` (or
fixed my config since clearly having this hook function active for this
file is wrong).


        Stefan





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Wed, 13 Sep 2023 16:50:01 GMT) Full text and rfc822 format available.

Message #23 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Daniel Martín via "Bug reports for GNU Emacs, the
 Swiss army knife of text editors" <bug-gnu-emacs <at> gnu.org>
Cc: 65864 <at> debbugs.gnu.org, eliz <at> gnu.org, monnier <at> iro.umontreal.ca,
 Daniel Martín <mardani29 <at> yahoo.es>
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Wed, 13 Sep 2023 19:44:20 +0300
> For example, I have `delete-trailing-whitespace' in my
> `before-save-hook', but for a particular buffer I wanted to save it
> without removing the trailing whitespace, because trailing whitespace
> was syntactically meaningful in that case (the buffer contained some
> Markdown-like source code).
>
> I searched the Internet for some solutions and what I ended up doing is
> C-x C-q (to make the buffer read-only), C-x C-s, and C-x C-q to make the
> buffer writable again (from
> https://stackoverflow.com/questions/14913398/in-emacs-how-do-i-save-without-running-save-hooks).
> It felt a bit like a "hack".

I confirm that 'C-u - C-x C-s' will save me from trouble that I have with

  (add-hook 'before-save-hook 'time-stamp nil t)

because often I don't need to update the timestamp for a small fix.
It takes too much time to revisit the file with M-x find-file-literally,
then manually restore the previous timestamp (to not commit unnecessary change),
and save the file again.  Your patch will help substantially, thanks for this.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Wed, 13 Sep 2023 16:50:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Wed, 13 Sep 2023 17:18:01 GMT) Full text and rfc822 format available.

Message #29 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>, 65864 <at> debbugs.gnu.org
Cc: eliz <at> gnu.org, monnier <at> iro.umontreal.ca, mardani29 <at> yahoo.es
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Wed, 13 Sep 2023 10:17:02 -0700
Juri Linkov <juri <at> linkov.net> writes:

> I confirm that 'C-u - C-x C-s' will save me from trouble that I have with
>
>   (add-hook 'before-save-hook 'time-stamp nil t)
>
> because often I don't need to update the timestamp for a small fix.
> It takes too much time to revisit the file with M-x find-file-literally,
> then manually restore the previous timestamp (to not commit unnecessary change),
> and save the file again.  Your patch will help substantially, thanks for this.

Same here, FWIW.




Severity set to 'wishlist' from 'normal' Request was from Stefan Kangas <stefankangas <at> gmail.com> to control <at> debbugs.gnu.org. (Fri, 15 Sep 2023 10:54:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Wed, 12 Feb 2025 04:26:02 GMT) Full text and rfc822 format available.

Message #34 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 65864 <at> debbugs.gnu.org, eliz <at> gnu.org, monnier <at> iro.umontreal.ca,
 mardani29 <at> yahoo.es
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Tue, 11 Feb 2025 20:25:30 -0800
Stefan Kangas <stefankangas <at> gmail.com> writes:

> Juri Linkov <juri <at> linkov.net> writes:
>
>> I confirm that 'C-u - C-x C-s' will save me from trouble that I have with
>>
>>   (add-hook 'before-save-hook 'time-stamp nil t)
>>
>> because often I don't need to update the timestamp for a small fix.
>> It takes too much time to revisit the file with M-x find-file-literally,
>> then manually restore the previous timestamp (to not commit unnecessary change),
>> and save the file again.  Your patch will help substantially, thanks for this.
>
> Same here, FWIW.

Does anyone object to installing this patch?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Wed, 12 Feb 2025 13:46:02 GMT) Full text and rfc822 format available.

Message #37 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: 65864 <at> debbugs.gnu.org, mardani29 <at> yahoo.es, monnier <at> iro.umontreal.ca,
 juri <at> linkov.net
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Wed, 12 Feb 2025 15:45:11 +0200
> From: Stefan Kangas <stefankangas <at> gmail.com>
> Date: Tue, 11 Feb 2025 20:25:30 -0800
> Cc: 65864 <at> debbugs.gnu.org, eliz <at> gnu.org, monnier <at> iro.umontreal.ca, 
> 	mardani29 <at> yahoo.es
> 
> Stefan Kangas <stefankangas <at> gmail.com> writes:
> 
> > Juri Linkov <juri <at> linkov.net> writes:
> >
> >> I confirm that 'C-u - C-x C-s' will save me from trouble that I have with
> >>
> >>   (add-hook 'before-save-hook 'time-stamp nil t)
> >>
> >> because often I don't need to update the timestamp for a small fix.
> >> It takes too much time to revisit the file with M-x find-file-literally,
> >> then manually restore the previous timestamp (to not commit unnecessary change),
> >> and save the file again.  Your patch will help substantially, thanks for this.
> >
> > Same here, FWIW.
> 
> Does anyone object to installing this patch?

I don't necessarily object, but how can we usefully document this
behavior? who are those users that know by heart the hooks run by
save-buffer?  And without knowing that, how can a user decide whether
she does or doesn't want to use this feature: perhaps omitting some
hook disables behavior that is very important to the user?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Wed, 12 Feb 2025 14:02:02 GMT) Full text and rfc822 format available.

Message #40 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 65864 <at> debbugs.gnu.org, mardani29 <at> yahoo.es,
 Stefan Kangas <stefankangas <at> gmail.com>, juri <at> linkov.net
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Wed, 12 Feb 2025 09:00:57 -0500
>> >> I confirm that 'C-u - C-x C-s' will save me from trouble that I have with
>> >>
>> >>   (add-hook 'before-save-hook 'time-stamp nil t)
>> >>
>> >> because often I don't need to update the timestamp for a small fix.
>> >> It takes too much time to revisit the file with M-x find-file-literally,
>> >> then manually restore the previous timestamp (to not commit unnecessary change),
>> >> and save the file again.  Your patch will help substantially, thanks for this.
>> >
>> > Same here, FWIW.
>> 
>> Does anyone object to installing this patch?
>
> I don't necessarily object, but how can we usefully document this
> behavior? who are those users that know by heart the hooks run by
> save-buffer?  And without knowing that, how can a user decide whether
> she does or doesn't want to use this feature: perhaps omitting some
> hook disables behavior that is very important to the user?

It does seem like a blunt way to affect `time-stamp`.

Maybe the time-stamp package should instead provide a prefix command
`time-stamp-inhibit` which makes the next call to `time-stamp`
do nothing.

Or a more hackish way would be for `time-stamp` to consult
`current-prefix-arg`, so as to disable the update of the time stamp when
you do `C-u - C-x C-s`.


        Stefan





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Wed, 12 Feb 2025 14:07:02 GMT) Full text and rfc822 format available.

Message #43 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Ship Mints <shipmints <at> gmail.com>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 65864 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>, juri <at> linkov.net,
 Stefan Kangas <stefankangas <at> gmail.com>, mardani29 <at> yahoo.es
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Wed, 12 Feb 2025 09:06:02 -0500
[Message part 1 (text/plain, inline)]
That might interfere with prefix args people have set up for their own
saves. How about a new time-stamp-inhibit defvar which people can bind to t
in their own save-buffer wrapper they bind to C-x C-s?

On Wed, Feb 12, 2025 at 9:02 AM Stefan Monnier via Bug reports for GNU
Emacs, the Swiss army knife of text editors <bug-gnu-emacs <at> gnu.org> wrote:

> >> >> I confirm that 'C-u - C-x C-s' will save me from trouble that I have
> with
> >> >>
> >> >>   (add-hook 'before-save-hook 'time-stamp nil t)
> >> >>
> >> >> because often I don't need to update the timestamp for a small fix.
> >> >> It takes too much time to revisit the file with M-x
> find-file-literally,
> >> >> then manually restore the previous timestamp (to not commit
> unnecessary change),
> >> >> and save the file again.  Your patch will help substantially, thanks
> for this.
> >> >
> >> > Same here, FWIW.
> >>
> >> Does anyone object to installing this patch?
> >
> > I don't necessarily object, but how can we usefully document this
> > behavior? who are those users that know by heart the hooks run by
> > save-buffer?  And without knowing that, how can a user decide whether
> > she does or doesn't want to use this feature: perhaps omitting some
> > hook disables behavior that is very important to the user?
>
> It does seem like a blunt way to affect `time-stamp`.
>
> Maybe the time-stamp package should instead provide a prefix command
> `time-stamp-inhibit` which makes the next call to `time-stamp`
> do nothing.
>
> Or a more hackish way would be for `time-stamp` to consult
> `current-prefix-arg`, so as to disable the update of the time stamp when
> you do `C-u - C-x C-s`.
>
>
>         Stefan
>
>
>
>
>
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Wed, 12 Feb 2025 14:28:03 GMT) Full text and rfc822 format available.

Message #46 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>, Eli Zaretskii <eliz <at> gnu.org>
Cc: 65864 <at> debbugs.gnu.org, mardani29 <at> yahoo.es, juri <at> linkov.net
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Wed, 12 Feb 2025 06:27:41 -0800
Stefan Monnier <monnier <at> iro.umontreal.ca> writes:

>> I don't necessarily object, but how can we usefully document this
>> behavior? who are those users that know by heart the hooks run by
>> save-buffer?  And without knowing that, how can a user decide whether
>> she does or doesn't want to use this feature: perhaps omitting some
>> hook disables behavior that is very important to the user?
>
> It does seem like a blunt way to affect `time-stamp`.

Yes, it does seem like a sharp tool.  OTOH, we have some of them in
Emacs already.  Maybe we could make disabled-command cover it, somehow?

> Maybe the time-stamp package should instead provide a prefix command
> `time-stamp-inhibit` which makes the next call to `time-stamp`
> do nothing.

My use case for disabling `before-save-hook` with a prefix argument is
that in `emacs-lisp-mode`, I have this `before-save-hook`:

    (aggressive-indent--process-changed-list-and-indent
     ws-butler-before-save time-stamp)

Sometimes, I would really like to disable all three when saving.
Now, I can of course disable the modes, run special commands for
time-stamp, and so on.  It just seems less convenient.

I have also seen variations on hooks like these recommended online:

    (delete-trailing-whitespace time-stamp
     my-recompile-el check-parens)

Where `my-recompile-el` is some function that would automatically
recompile the file.

In cases such as these, the prefix argument would mean "just save this
file now without doing any other special processing".

That said, if this functionality is not considered generally useful, I'm
of course happy to add it to my local hacks and move on.

> Or a more hackish way would be for `time-stamp` to consult
> `current-prefix-arg`, so as to disable the update of the time stamp when
> you do `C-u - C-x C-s`.

That could work, but yes hackish, and wouldn't it mean that we'd have to
potentially reproduce the hack in `ws-butler-mode`, `check-parens`,
`delete-trailing-whitespace`, and so on?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65864; Package emacs. (Wed, 12 Feb 2025 15:53:02 GMT) Full text and rfc822 format available.

Message #49 received at 65864 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: 65864 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>, mardani29 <at> yahoo.es,
 juri <at> linkov.net
Subject: Re: bug#65864: [PATCH] Add option to save a buffer without running
 save hooks
Date: Wed, 12 Feb 2025 10:51:56 -0500
> My use case for disabling `before-save-hook` with a prefix argument is
> that in `emacs-lisp-mode`, I have this `before-save-hook`:
>
>     (aggressive-indent--process-changed-list-and-indent
>      ws-butler-before-save time-stamp)
>
> Sometimes, I would really like to disable all three when saving.
> Now, I can of course disable the modes, run special commands for
> time-stamp, and so on.  It just seems less convenient.

Maybe a `grep before-save-hook Emacs (Non)GNU ELPA MELPA` would be
a good way to see how whether `before-save-hook` is used only for such
"optional" functionality, which would be a good argument in favor of the
patch (tho I'd then update the doc of `before-save-hook` to mention that
it's not guaranteed it's run, so it's meant for "nice to have but not
indispensable" processing).

Also, Daniel's patch does the same with `after-save-hook` so we'd need
to do the same analysis with that hook.

> That could work, but yes hackish, and wouldn't it mean that we'd have to
> potentially reproduce the hack in `ws-butler-mode`, `check-parens`,
> `delete-trailing-whitespace`, and so on?

Maybe.  Then again, if people have `time-stamp` and `check-parens` on
their hook and they use `C-u - C-x C-s` to "save without bumping
time-stamp", they might be disappointed if the parens happen to be
unbalanced.


        Stefan





This bug report was last modified 122 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.