From: Stefan Monnier Date: Mon, 23 Jun 2025 02:25:03 +0000 (-0400) Subject: (Finsert_file_contents): Refine commit d07af40d8826 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=cd2b247d117bd3d8884dd128988c32afb3709d82;p=emacs.git (Finsert_file_contents): Refine commit d07af40d8826 * 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) --- diff --git a/src/fileio.c b/src/fileio.c index 7e1ac3fc383..9f04a5928bb 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -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); diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index 13cc5de29e8..b6302c35fee 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -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