]> git.eshelyaron.com Git - emacs.git/commitdiff
(Finsert_file_contents): Refine commit d07af40d8826
authorStefan Monnier <monnier@iro.umontreal.ca>
Mon, 23 Jun 2025 02:25:03 +0000 (22:25 -0400)
committerEshel Yaron <me@eshelyaron.com>
Wed, 23 Jul 2025 20:06:44 +0000 (22:06 +0200)
* src/fileio.c (Finsert_file_contents): Inhibit ask-supersession
only if we're VISITing in a non-narrowed buffer (bug#78866).

* test/src/fileio-tests.el (ert--tests-dir): New var.
(fileio-tests--insert-file-contents-supersession): New test.

(cherry picked from commit 6c0bbf0f921682a185ebd57efef1e9d4f8ced788)

src/fileio.c
test/src/fileio-tests.el

index 7e1ac3fc383d2af0f6c450fa323f90675d197f87..9f04a5928bbf4f427be4a8130392ca05ef1a0d19 100644 (file)
@@ -4550,14 +4550,19 @@ by calling `format-decode', which see.  */)
          beg_offset += same_at_start - BEGV_BYTE;
          end_offset -= ZV_BYTE - same_at_end;
 
-          /* This binding is to avoid ask-user-about-supersession-threat
-            being called in insert_from_buffer or del_range_bytes (via
-            prepare_to_modify_buffer).
-             AFAICT we could avoid ask-user-about-supersession-threat by setting
-             current_buffer->modtime earlier, but we could still end up calling
-             ask-user-about-supersession-threat if the file is modified while
-             we read it, so we bind buffer-file-name instead.  */
-          specbind (Qbuffer_file_name, Qnil);
+          if (!NILP (visit) && BEG == BEGV && Z == ZV)
+            /* This binding is to avoid ask-user-about-supersession-threat
+              being called in insert_from_buffer or del_range_bytes (via
+              prepare_to_modify_buffer).
+              Such a prompt makes no sense if we're VISITing the file,
+              since the insertion makes the buffer *more* like the file
+              rather than the reverse.
+               AFAICT we could avoid ask-user-about-supersession-threat by
+               setting current_buffer->modtime earlier, but we could still
+               end up calling ask-user-about-supersession-threat if the file
+               is modified while we read it, so we bind buffer-file-name
+               instead.  */
+            specbind (Qbuffer_file_name, Qnil);
          del_range_byte (same_at_start, same_at_end);
          /* Insert from the file at the proper position.  */
          temp = BYTE_TO_CHAR (same_at_start);
@@ -4666,8 +4671,9 @@ by calling `format-decode', which see.  */)
          /* Truncate the buffer to the size of the file.  */
          if (same_at_start != same_at_end)
            {
-              /* See previous specbind for the reason behind this.  */
-              specbind (Qbuffer_file_name, Qnil);
+              if (!NILP (visit) && BEG == BEGV && Z == ZV)
+               /* See previous specbind for the reason behind this.  */
+               specbind (Qbuffer_file_name, Qnil);
              del_range_byte (same_at_start, same_at_end);
            }
          inserted = 0;
@@ -4716,8 +4722,9 @@ by calling `format-decode', which see.  */)
         we are taking from the decoded string.  */
       inserted -= (ZV_BYTE - same_at_end) + (same_at_start - BEGV_BYTE);
 
-      /* See previous specbind for the reason behind this.  */
-      specbind (Qbuffer_file_name, Qnil);
+      if (!NILP (visit) && BEG == BEGV && Z == ZV)
+        /* See previous specbind for the reason behind this.  */
+        specbind (Qbuffer_file_name, Qnil);
       if (same_at_end != same_at_start)
        {
          del_range_byte (same_at_start, same_at_end);
index 13cc5de29e8b1a2afad9998273225dc254702389..b6302c35fee112e4acb532b117b1152f1e4be8e8 100644 (file)
@@ -235,5 +235,31 @@ Also check that an encoding error can appear in a symlink."
                   "2025/02/01 23:15:59.123456700")))
       (delete-file tfile))))
 
+(defconst ert--tests-dir
+  (file-name-directory (macroexp-file-name)))
+
+(ert-deftest fileio-tests--insert-file-contents-supersession ()
+  (ert-with-temp-file file
+    (write-region "foo" nil file)
+    (let* ((asked nil)
+           (buf (find-file-noselect file))
+           (auast (lambda (&rest _) (setq asked t))))
+      (unwind-protect
+          (with-current-buffer buf
+            ;; Pretend someone else edited the file.
+            (write-region "bar" nil file 'append)
+            ;; Use `advice-add' rather than `cl-letf' because the function
+            ;; may not be defined yet.
+            (advice-add 'ask-user-about-supersession-threat :override auast)
+            ;; Modify the local buffer via `insert-file-contents'.
+            (insert-file-contents
+             (expand-file-name "lread-resources/somelib.el"
+                               ert--tests-dir)
+             nil nil nil 'replace))
+        (advice-remove 'ask-user-about-supersession-threat auast)
+        (kill-buffer buf))
+      ;; We should have prompted about the supersession threat.
+      (should asked))))
+
 
 ;;; fileio-tests.el ends here