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);
/* 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;
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);
"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