From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Daniel =?UTF-8?Q?Mart=C3=ADn?= via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#65864: [PATCH] Add option to save a buffer without running save hooks Date: Mon, 11 Sep 2023 12:24:40 +0200 Message-ID: References: Reply-To: Daniel =?UTF-8?Q?Mart=C3=ADn?= Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25955"; mail-complaints-to="usenet@ciao.gmane.io" To: 65864@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Sep 11 12:28:21 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qfe9V-0006Wn-D6 for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 11 Sep 2023 12:28:21 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qfe9A-0004bH-Tk; Mon, 11 Sep 2023 06:28:00 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qfe98-0004az-U5 for bug-gnu-emacs@gnu.org; Mon, 11 Sep 2023 06:27:58 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qfe98-0003wk-Ly for bug-gnu-emacs@gnu.org; Mon, 11 Sep 2023 06:27:58 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qfe9C-00063Q-JX for bug-gnu-emacs@gnu.org; Mon, 11 Sep 2023 06:28:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Daniel =?UTF-8?Q?Mart=C3=ADn?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 11 Sep 2023 10:28:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 65864 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.169442803323215 (code B ref -1); Mon, 11 Sep 2023 10:28:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 11 Sep 2023 10:27:13 +0000 Original-Received: from localhost ([127.0.0.1]:52187 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qfe8O-00062M-E2 for submit@debbugs.gnu.org; Mon, 11 Sep 2023 06:27:12 -0400 Original-Received: from lists.gnu.org ([2001:470:142::17]:36222) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qfe8K-00061n-C4 for submit@debbugs.gnu.org; Mon, 11 Sep 2023 06:27:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qfe84-0004Wi-E0 for bug-gnu-emacs@gnu.org; Mon, 11 Sep 2023 06:26:54 -0400 Original-Received: from sonic301-47.consmr.mail.ir2.yahoo.com ([77.238.176.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qfe80-0003lO-BM for bug-gnu-emacs@gnu.org; Mon, 11 Sep 2023 06:26:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.es; s=s2048; t=1694428003; bh=OUm1zonhvONycADP5RjZu8hxGRwIyeNgEQqJ0yTxOD8=; h=From:To:Subject:Date:References:From:Subject:Reply-To; b=bVJGKKnG55suPAhW6slLPG2g5JJv/AhnzbYreH/UVUuPiZSOCXV3bFBSRITsHVlNPucdtp7BxtK8J0qlranx8StEIgObvaEnnHb1uj3kfMf71l6zf++/CjUKA15XJ7E0l0nXFP96sOzUIn3qf/cHNn/Ovz5p5aEUNMuAtZDLhIdV12JBAE57YbaMNDZGAreMP4JCU7S7tKcgvPvMCgRxLtyODy9EIioqHKt4r8EYzQxaHHKUOcEGWI/VtlQ/6tEXbNGFktQUeqUNwoH9dpjuNHBXuoFswMoSypOWTk9si+j9bQzKWpuftu5kPi3Yuoyxs/AQNh+zi8ps0sOLStslcg== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1694428003; bh=h6K9hkrnhfyI+hxFlOOwGPXnTQ6q4PZwxmVg5wYTvhv=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=o+fVOGG8EW3ApCfFIkeT4BgurwwCFYtW8mNrIXmyfAA4UaAwb1BrCJKhHjxsF+Z5+n4rx1sniR9e99sdBXxzaxVB9+/+AGgb3ODcZCThOTiO07v4HDtsQe3MllUFpyw7+CzyURsoLlyO0unr4Jql3g3+0yR9+WyNQvle++ffxY4xOGGFbv5F0ooLiQLtL6W+2gyrzXYK+PAastWvaPjxRAxmL0ASjTrG79Jchtwwtb5jp8dAMAXWXqEflCSUQTktgDlg5IdLaeaodTzcNoUk+zzU9o3sD9fqFQXjimUarn7OcEA9PTojmJhNT9o4dY3JujeDSb4U41LayHQm3OHnow== X-YMail-OSG: EM6lO6gVM1m7G_JuTau9KY6twUUAYLS4ensMO5pkHuRuj8gPrXyHKwv1mzJeuFS BXHRCD6lpXQyldGh59Fmil4rEMPdtj83z3rKvwLueBAKsYV0KXOOxWXLqyYtZZOo4iXwhNiDFSCz oQe7DuXWHTdZUf6b.w2QHq.JV5Nlk06kowP5hUMnawza7EukTMs7ovbU1tdl3ggTxVUtdnMWmfTY wGmsop6lUN7t6STUHwQ.lSeh1dBfvFzDs66ia8tm3b_8TADe.hhsgr8BWX5r1g9hQA93IcH0XSb1 IfzS6JA3_EIa.S7yA.k.2gyjvD9ffZOmMgGaScKn7myNLObdalvxoC2pSIQoQPvohVRWnYj8PTuP RgP8MPkamag1Lo8pOXS2fPiEk2F4Xgid7kiQRTu93Q7zAIg0uIodYn3_pQnSkOuyCehsI5vVvyb7 tB5RKsbhJdFtADSfLI1NWlb7hYNcaeZTbyc6WnExZc43Js8waRD6YIQkQJM.otXGcigY7Lw1J_iY j7GtIkaEzzdP.u2KA8uZ6wkF11dJ9A5UcYMPXUm2dTKYy8XH38bibtG4nR8kZUaYTH75EldVziKV wpyHrNA.gCXEQlgzUXYLyCuEQhU_e3uwYm60UjqQFgmLebbru8rm9IiG.w10FOHtGyD9j2Izzwn5 5flgQqJFUaIoNDOPhTg27EhjUYs0WMWIMnJ5WveQ5wONvow1VwdmkVimKF3p35jO6sBwT4NNF704 hbvmko0fFBSHGLVsBNF1m2zp3UlKlbc3ARaD55wqHCLAjmjLJU4B90eNUBbLOUeyU1923oN_s3rL U.HMJ4tWchDhvyY4EUW1x4pcH48myrpbq8KGxlVLZ_ X-Sonic-MF: X-Sonic-ID: 2a0b0489-4af7-419e-ac4d-c1cb7cd3b61e Original-Received: from sonic.gate.mail.ne1.yahoo.com by sonic301.consmr.mail.ir2.yahoo.com with HTTP; Mon, 11 Sep 2023 10:26:43 +0000 Original-Received: by hermes--production-ir2-5cc57b9c45-xh7tc (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID c9fbca19c0b0460aa2de0e4d46e07903; Mon, 11 Sep 2023 10:24:41 +0000 (UTC) X-Mailer: WebService/1.1.21763 mail.backend.jedi.jws.acl:role.jedi.acl.token.atz.jws.hermes.yahoo Received-SPF: pass client-ip=77.238.176.124; envelope-from=mardani29@yahoo.es; helo=sonic301-47.consmr.mail.ir2.yahoo.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:270034 Archived-At: --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Add-option-to-save-a-buffer-without-running-save-hoo.patch >From 6e1f19403937c266acd3d858ecc6c9e0e6b48ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= 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 --=-=-=--