unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#65864: [PATCH] Add option to save a buffer without running save hooks
       [not found] <m1y1hdm2hj.fsf.ref@yahoo.es>
@ 2023-09-11 10:24 ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-11 12:45   ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-11 10:24 UTC (permalink / raw)
  To: 65864

[-- Attachment #1: Type: text/plain, Size: 420 bytes --]

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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-option-to-save-a-buffer-without-running-save-hoo.patch --]
[-- Type: text/patch, Size: 4830 bytes --]

From 6e1f19403937c266acd3d858ecc6c9e0e6b48ade Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <mardani29@yahoo.es>
Date: Mon, 11 Sep 2023 11:55:00 +0200
Subject: [PATCH] Add option to save a buffer without running save hooks

* lisp/files.el (save-buffer): Set `ignore-save-hooks' when
`save-buffer' is run with a negative argument.
(basic-save-buffer): Do not run `before-save-hook' or
`after-save-hook' if `ignore-save-hooks' is set.
* doc/emacs/files.texi (Save Commands): Update the user manual.
* etc/NEWS: Advertise it.
---
 doc/emacs/files.texi |  5 ++++-
 etc/NEWS             |  5 +++++
 lisp/files.el        | 21 +++++++++++++--------
 3 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi
index 7efb4516d15..c1acd1e80d4 100644
--- a/doc/emacs/files.texi
+++ b/doc/emacs/files.texi
@@ -457,7 +457,10 @@ Save Commands
 @end example
 
 With a prefix argument, @kbd{C-u C-x C-s}, Emacs also marks the buffer
-to be backed up when the next save is done.  @xref{Backup}.
+to be backed up when the next save is done.  @xref{Backup}.  With a
+negative argument, @kbd{C-u - C-x C-s}, Emacs doesn't run the hook
+@code{before-save-hook} before saving the buffer, and doesn't run the
+hook @code{after-save-hook} after saving the buffer.
 
 @kindex C-x s
 @findex save-some-buffers
diff --git a/etc/NEWS b/etc/NEWS
index 51e89fc96dd..772aa49bc95 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -221,6 +221,11 @@ whereas if the mouse pointer is in the left half of a glyph, point
 will be put in front the buffer position corresponding to that glyph.
 By default this is disabled.
 
++++
+** You can now avoid running save hooks when saving a buffer.
+If you save a buffer with a negative argument, C-u - C-x C-s, Emacs
+won't run the hooks 'before-save-hook' and 'after-save-hook'.
+
 ** Internationalization
 
 ---
diff --git a/lisp/files.el b/lisp/files.el
index b67482a2f74..6ee28f23daa 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -5618,8 +5618,10 @@ save-buffer
  to become a backup when the next save is done,
  and makes the previous version into a backup file.
 
-With a numeric prefix argument of 0, never make the previous version
-into a backup file.
+With a numeric prefix argument of 0, never make the previous
+version into a backup file.  With a numeric prefix argument of
+-1, do not run the hooks `before-save-hook' and
+`after-save-hook'.
 
 Note that the various variables that control backups, such
 as `version-control', `backup-enable-predicate', `vc-make-backup-files',
@@ -5649,7 +5651,8 @@ save-buffer
   (interactive "p")
   (let ((modp (buffer-modified-p))
 	(make-backup-files (or (and make-backup-files (not (eq arg 0)))
-			       (memq arg '(16 64)))))
+			       (memq arg '(16 64))))
+        (ignore-save-hooks (eq arg -1)))
     (and modp (memq arg '(16 64)) (setq buffer-backed-up nil))
     ;; We used to display the message below only for files > 50KB, but
     ;; then Rmail-mbox never displays it due to buffer swapping.  If
@@ -5660,7 +5663,7 @@ save-buffer
              (not noninteractive)
              (not save-silently))
 	(message "Saving file %s..." (buffer-file-name)))
-    (basic-save-buffer (called-interactively-p 'any))
+    (basic-save-buffer (called-interactively-p 'any) ignore-save-hooks)
     (and modp (memq arg '(4 64)) (setq buffer-backed-up nil))))
 
 (defun delete-auto-save-file-if-necessary (&optional force)
@@ -5720,7 +5723,7 @@ save-buffer-coding-system
 
 (put 'save-buffer-coding-system 'permanent-local t)
 
-(defun basic-save-buffer (&optional called-interactively)
+(defun basic-save-buffer (&optional called-interactively ignore-save-hooks)
   "Save the current buffer in its visited file, if it has been modified.
 
 The hooks `write-contents-functions', `local-write-file-hooks'
@@ -5769,8 +5772,9 @@ basic-save-buffer
 		     (goto-char (point-max))
 		     (insert ?\n))))
 	    ;; Don't let errors prevent saving the buffer.
-	    (with-demoted-errors "Before-save hook error: %S"
-	      (run-hooks 'before-save-hook))
+            (unless ignore-save-hooks
+	      (with-demoted-errors "Before-save hook error: %S"
+	        (run-hooks 'before-save-hook)))
             ;; Give `write-contents-functions' a chance to
             ;; short-circuit the whole process.
 	    (unless (run-hook-with-args-until-success 'write-contents-functions)
@@ -5834,7 +5838,8 @@ basic-save-buffer
             ;; If the auto-save file was recent before this command,
 	    ;; delete it now.
 	    (delete-auto-save-file-if-necessary recent-save))
-	  (run-hooks 'after-save-hook))
+	  (unless ignore-save-hooks
+            (run-hooks 'after-save-hook)))
       (or noninteractive
           (not called-interactively)
           (files--message "(No changes need to be saved)")))))
-- 
2.40.1


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

* bug#65864: [PATCH] Add option to save a buffer without running save hooks
  2023-09-11 10:24 ` bug#65864: [PATCH] Add option to save a buffer without running save hooks Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-09-11 12:45   ` Eli Zaretskii
  2023-09-11 22:28     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2023-09-11 12:45 UTC (permalink / raw)
  To: Daniel Martín; +Cc: 65864, Stefan Monnier

> 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@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).





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

* bug#65864: [PATCH] Add option to save a buffer without running save hooks
  2023-09-11 12:45   ` Eli Zaretskii
@ 2023-09-11 22:28     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-12  8:11       ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-11 22:28 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 65864, Daniel Martín

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






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

* bug#65864: [PATCH] Add option to save a buffer without running save hooks
  2023-09-11 22:28     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-09-12  8:11       ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-12 12:59         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-13 16:44         ` Juri Linkov
  0 siblings, 2 replies; 7+ messages in thread
From: Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-12  8:11 UTC (permalink / raw)
  To: 65864; +Cc: eliz, monnier

Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of
text editors" <bug-gnu-emacs@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".





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

* bug#65864: [PATCH] Add option to save a buffer without running save hooks
  2023-09-12  8:11       ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-09-12 12:59         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-13 16:44         ` Juri Linkov
  1 sibling, 0 replies; 7+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-12 12:59 UTC (permalink / raw)
  To: Daniel Martín; +Cc: 65864, Eli Zaretskii

> 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






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

* bug#65864: [PATCH] Add option to save a buffer without running save hooks
  2023-09-12  8:11       ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-12 12:59         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-09-13 16:44         ` Juri Linkov
  2023-09-13 17:17           ` Stefan Kangas
  1 sibling, 1 reply; 7+ messages in thread
From: Juri Linkov @ 2023-09-13 16:44 UTC (permalink / raw)
  To: 65864; +Cc: eliz, monnier, mardani29

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





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

* bug#65864: [PATCH] Add option to save a buffer without running save hooks
  2023-09-13 16:44         ` Juri Linkov
@ 2023-09-13 17:17           ` Stefan Kangas
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Kangas @ 2023-09-13 17:17 UTC (permalink / raw)
  To: Juri Linkov, 65864; +Cc: eliz, monnier, mardani29

Juri Linkov <juri@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.





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

end of thread, other threads:[~2023-09-13 17:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <m1y1hdm2hj.fsf.ref@yahoo.es>
2023-09-11 10:24 ` bug#65864: [PATCH] Add option to save a buffer without running save hooks Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-11 12:45   ` Eli Zaretskii
2023-09-11 22:28     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-12  8:11       ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-12 12:59         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-13 16:44         ` Juri Linkov
2023-09-13 17:17           ` Stefan Kangas

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).