(Finsert_file_contents): Refine commit d07af40d88

* 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.
This commit is contained in:
Stefan Monnier
2025-06-22 22:25:03 -04:00
parent cb484ead91
commit 6c0bbf0f92
2 changed files with 45 additions and 12 deletions

View 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);

View 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