unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 65864@debbugs.gnu.org
Subject: bug#65864: [PATCH] Add option to save a buffer without running save hooks
Date: Mon, 11 Sep 2023 12:24:40 +0200	[thread overview]
Message-ID: <m1y1hdm2hj.fsf@yahoo.es> (raw)
In-Reply-To: m1y1hdm2hj.fsf.ref@yahoo.es

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


       reply	other threads:[~2023-09-11 10:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
2023-09-11 12:45   ` bug#65864: [PATCH] Add option to save a buffer without running save hooks 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m1y1hdm2hj.fsf@yahoo.es \
    --to=bug-gnu-emacs@gnu.org \
    --cc=65864@debbugs.gnu.org \
    --cc=mardani29@yahoo.es \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).