all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Lars Ingebrigtsen <larsi@gnus.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: monnier@iro.umontreal.ca,  emacs-devel@gnu.org
Subject: Re: master e8488bcc9c: Avoid having font locking triggering unnecessary auto-saving
Date: Mon, 09 May 2022 19:40:00 +0200	[thread overview]
Message-ID: <87mtfq38r3.fsf@gnus.org> (raw)
In-Reply-To: <87v8ue3b9g.fsf@gnus.org> (Lars Ingebrigtsen's message of "Mon, 09 May 2022 18:45:47 +0200")

The following builds (from bootstrap), all the tests pass, and it seems
to work.  But I'd like some more eyeballs on it anyway.  😓

diff --git a/doc/lispref/buffers.texi b/doc/lispref/buffers.texi
index d8cf3d7919..3e3b0bd9f0 100644
--- a/doc/lispref/buffers.texi
+++ b/doc/lispref/buffers.texi
@@ -541,10 +541,12 @@ Buffer Modification
 @ref{Text}.
 
 @defun buffer-modified-p &optional buffer
-This function returns @code{t} if the buffer @var{buffer} has been modified
-since it was last read in from a file or saved, or @code{nil}
-otherwise.  If @var{buffer} is not supplied, the current buffer
-is tested.
+This function returns non-@code{nil} if the buffer @var{buffer} has
+been modified since it was last read in from a file or saved, or
+@code{nil} otherwise.  If @var{buffer} has been autosaved after
+@var{buffer} was last modified, the symbol @code{autosaved} is
+returned.  If @var{buffer} is not supplied, the current buffer is
+tested.
 @end defun
 
 @defun set-buffer-modified-p flag
diff --git a/lisp/subr.el b/lisp/subr.el
index 01549cc6f7..54c9f35264 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -4594,21 +4594,17 @@ with-silent-modifications
 someone else, running buffer modification hooks, and other things
 of that nature."
   (declare (debug t) (indent 0))
-  (let ((modified (make-symbol "modified"))
-        (tick (make-symbol "tick")))
+  (let ((modified (make-symbol "modified")))
     `(let* ((,modified (buffer-modified-p))
-            (,tick (buffer-modified-tick))
             (buffer-undo-list t)
             (inhibit-read-only t)
             (inhibit-modification-hooks t))
        (unwind-protect
            (progn
              ,@body)
-         ;; We restore the buffer tick count, too, because otherwise
-         ;; we'll trigger a new auto-save.
-         (internal--set-buffer-modified-tick ,tick)
-         (unless ,modified
-           (restore-buffer-modified-p nil))))))
+         (when (or (not ,modified)
+                   (eq ,modified 'autosaved))
+           (restore-buffer-modified-p ,modified))))))
 
 (defmacro with-output-to-string (&rest body)
   "Execute BODY, return the text it sent to `standard-output', as a string."
diff --git a/src/buffer.c b/src/buffer.c
index 6334e197f0..d830943994 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -1376,12 +1376,23 @@ DEFUN ("buffer-local-variables", Fbuffer_local_variables,
 \f
 DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
        0, 1, 0,
-       doc: /* Return t if BUFFER was modified since its file was last read or saved.
-No argument or nil as argument means use current buffer as BUFFER.  */)
+       doc: /* Return non-nil if BUFFER was modified since its file was last read or saved.
+No argument or nil as argument means use current buffer as BUFFER.
+
+If BUFFER has been autosaved after BUFFER was last modified, the
+symbol `autosaved' is returned.  */)
   (Lisp_Object buffer)
 {
   struct buffer *buf = decode_buffer (buffer);
-  return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
+  if (BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf))
+    {
+      if (BUF_AUTOSAVE_MODIFF (buf) == BUF_MODIFF (buf))
+	return Qautosaved;
+      else
+	return Qt;
+    }
+  else
+    return Qnil;
 }
 
 DEFUN ("force-mode-line-update", Fforce_mode_line_update,
@@ -1475,16 +1486,19 @@ DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
      recent-auto-save-p from t to nil.
      Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified
      we risk changing recent-auto-save-p from nil to t.  */
-  SAVE_MODIFF = (NILP (flag)
-		 /* FIXME: This unavoidably sets recent-auto-save-p to nil.  */
-		 ? MODIFF
-		 /* Let's try to preserve recent-auto-save-p.  */
-		 : SAVE_MODIFF < MODIFF ? SAVE_MODIFF
-		 /* If SAVE_MODIFF == auto_save_modified == MODIFF,
-		    we can either decrease SAVE_MODIFF and auto_save_modified
-		    or increase MODIFF.  */
-		 : modiff_incr (&MODIFF));
-
+  if (NILP (flag))
+    /* This unavoidably sets recent-auto-save-p to nil.  */
+    SAVE_MODIFF = MODIFF;
+  else
+    {
+      if (EQ (flag, Qautosaved))
+	BUF_AUTOSAVE_MODIFF (b) = MODIFF;
+      /* If SAVE_MODIFF == auto_save_modified == MODIFF, we can either
+	 decrease SAVE_MODIFF and auto_save_modified or increase
+	 MODIFF.  */
+      else if (SAVE_MODIFF >= MODIFF)
+	SAVE_MODIFF = modiff_incr (&MODIFF);
+    }
   return flag;
 }
 
@@ -6465,5 +6479,7 @@ Functions (implicitly) running this hook are `get-buffer-create',
   defsubr (&Soverlay_put);
   defsubr (&Srestore_buffer_modified_p);
 
+  DEFSYM (Qautosaved, "autosaved");
+
   Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt);
 }


-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




  reply	other threads:[~2022-05-09 17:40 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <165191796540.22789.3432288633082546349@vcs2.savannah.gnu.org>
     [not found] ` <20220507100605.B7CA7C051FF@vcs2.savannah.gnu.org>
2022-05-07 16:06   ` master e8488bcc9c: Avoid having font locking triggering unnecessary auto-saving Stefan Monnier
2022-05-07 16:10     ` Eli Zaretskii
2022-05-07 16:27       ` Stefan Monnier
2022-05-07 16:40         ` Eli Zaretskii
2022-05-07 17:21           ` Stefan Monnier
2022-05-07 17:26             ` Eli Zaretskii
2022-05-07 16:49     ` Lars Ingebrigtsen
2022-05-09 12:37   ` Lars Ingebrigtsen
2022-05-09 13:22     ` Stefan Monnier
2022-05-09 13:35       ` Lars Ingebrigtsen
2022-05-09 13:47         ` Lars Ingebrigtsen
2022-05-09 15:43           ` Stefan Monnier
2022-05-09 16:05             ` Lars Ingebrigtsen
2022-05-09 16:30               ` Eli Zaretskii
2022-05-09 16:45                 ` Lars Ingebrigtsen
2022-05-09 17:40                   ` Lars Ingebrigtsen [this message]
2022-05-09 18:29                     ` Stefan Monnier
2022-05-10  1:51                       ` Lars Ingebrigtsen
2022-05-10  2:11                         ` Stefan Monnier
2022-05-10 11:45                           ` Lars Ingebrigtsen
2022-05-10 11:55                             ` Stefan Monnier
2022-05-11 11:22                               ` Lars Ingebrigtsen
2022-05-11 13:19                                 ` Stefan Monnier
2022-05-12  0:18                                   ` Lars Ingebrigtsen
2022-05-09 17:00               ` Lars Ingebrigtsen

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

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

  git send-email \
    --in-reply-to=87mtfq38r3.fsf@gnus.org \
    --to=larsi@gnus.org \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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 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.