diff options
| author | Stefan Monnier | 2025-06-22 22:25:03 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2025-06-27 23:11:39 -0400 |
| commit | 6c0bbf0f921682a185ebd57efef1e9d4f8ced788 (patch) | |
| tree | a07e91ca4549ce6b5c273aa0cbd1e0b19a4b7540 | |
| parent | cb484ead91cb20f1cad31ac878a65ea1e9354871 (diff) | |
| download | emacs-6c0bbf0f921682a185ebd57efef1e9d4f8ced788.tar.gz emacs-6c0bbf0f921682a185ebd57efef1e9d4f8ced788.zip | |
(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.
| -rw-r--r-- | src/fileio.c | 31 | ||||
| -rw-r--r-- | test/src/fileio-tests.el | 26 |
2 files changed, 45 insertions, 12 deletions
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. */) | |||
| 4550 | beg_offset += same_at_start - BEGV_BYTE; | 4550 | beg_offset += same_at_start - BEGV_BYTE; |
| 4551 | end_offset -= ZV_BYTE - same_at_end; | 4551 | end_offset -= ZV_BYTE - same_at_end; |
| 4552 | 4552 | ||
| 4553 | /* This binding is to avoid ask-user-about-supersession-threat | 4553 | if (!NILP (visit) && BEG == BEGV && Z == ZV) |
| 4554 | being called in insert_from_buffer or del_range_bytes (via | 4554 | /* This binding is to avoid ask-user-about-supersession-threat |
| 4555 | prepare_to_modify_buffer). | 4555 | being called in insert_from_buffer or del_range_bytes (via |
| 4556 | AFAICT we could avoid ask-user-about-supersession-threat by setting | 4556 | prepare_to_modify_buffer). |
| 4557 | current_buffer->modtime earlier, but we could still end up calling | 4557 | Such a prompt makes no sense if we're VISITing the file, |
| 4558 | ask-user-about-supersession-threat if the file is modified while | 4558 | since the insertion makes the buffer *more* like the file |
| 4559 | we read it, so we bind buffer-file-name instead. */ | 4559 | rather than the reverse. |
| 4560 | specbind (Qbuffer_file_name, Qnil); | 4560 | AFAICT we could avoid ask-user-about-supersession-threat by |
| 4561 | setting current_buffer->modtime earlier, but we could still | ||
| 4562 | end up calling ask-user-about-supersession-threat if the file | ||
| 4563 | is modified while we read it, so we bind buffer-file-name | ||
| 4564 | instead. */ | ||
| 4565 | specbind (Qbuffer_file_name, Qnil); | ||
| 4561 | del_range_byte (same_at_start, same_at_end); | 4566 | del_range_byte (same_at_start, same_at_end); |
| 4562 | /* Insert from the file at the proper position. */ | 4567 | /* Insert from the file at the proper position. */ |
| 4563 | temp = BYTE_TO_CHAR (same_at_start); | 4568 | temp = BYTE_TO_CHAR (same_at_start); |
| @@ -4666,8 +4671,9 @@ by calling `format-decode', which see. */) | |||
| 4666 | /* Truncate the buffer to the size of the file. */ | 4671 | /* Truncate the buffer to the size of the file. */ |
| 4667 | if (same_at_start != same_at_end) | 4672 | if (same_at_start != same_at_end) |
| 4668 | { | 4673 | { |
| 4669 | /* See previous specbind for the reason behind this. */ | 4674 | if (!NILP (visit) && BEG == BEGV && Z == ZV) |
| 4670 | specbind (Qbuffer_file_name, Qnil); | 4675 | /* See previous specbind for the reason behind this. */ |
| 4676 | specbind (Qbuffer_file_name, Qnil); | ||
| 4671 | del_range_byte (same_at_start, same_at_end); | 4677 | del_range_byte (same_at_start, same_at_end); |
| 4672 | } | 4678 | } |
| 4673 | inserted = 0; | 4679 | inserted = 0; |
| @@ -4716,8 +4722,9 @@ by calling `format-decode', which see. */) | |||
| 4716 | we are taking from the decoded string. */ | 4722 | we are taking from the decoded string. */ |
| 4717 | inserted -= (ZV_BYTE - same_at_end) + (same_at_start - BEGV_BYTE); | 4723 | inserted -= (ZV_BYTE - same_at_end) + (same_at_start - BEGV_BYTE); |
| 4718 | 4724 | ||
| 4719 | /* See previous specbind for the reason behind this. */ | 4725 | if (!NILP (visit) && BEG == BEGV && Z == ZV) |
| 4720 | specbind (Qbuffer_file_name, Qnil); | 4726 | /* See previous specbind for the reason behind this. */ |
| 4727 | specbind (Qbuffer_file_name, Qnil); | ||
| 4721 | if (same_at_end != same_at_start) | 4728 | if (same_at_end != same_at_start) |
| 4722 | { | 4729 | { |
| 4723 | del_range_byte (same_at_start, same_at_end); | 4730 | 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." | |||
| 235 | "2025/02/01 23:15:59.123456700"))) | 235 | "2025/02/01 23:15:59.123456700"))) |
| 236 | (delete-file tfile)))) | 236 | (delete-file tfile)))) |
| 237 | 237 | ||
| 238 | (defconst ert--tests-dir | ||
| 239 | (file-name-directory (macroexp-file-name))) | ||
| 240 | |||
| 241 | (ert-deftest fileio-tests--insert-file-contents-supersession () | ||
| 242 | (ert-with-temp-file file | ||
| 243 | (write-region "foo" nil file) | ||
| 244 | (let* ((asked nil) | ||
| 245 | (buf (find-file-noselect file)) | ||
| 246 | (auast (lambda (&rest _) (setq asked t)))) | ||
| 247 | (unwind-protect | ||
| 248 | (with-current-buffer buf | ||
| 249 | ;; Pretend someone else edited the file. | ||
| 250 | (write-region "bar" nil file 'append) | ||
| 251 | ;; Use `advice-add' rather than `cl-letf' because the function | ||
| 252 | ;; may not be defined yet. | ||
| 253 | (advice-add 'ask-user-about-supersession-threat :override auast) | ||
| 254 | ;; Modify the local buffer via `insert-file-contents'. | ||
| 255 | (insert-file-contents | ||
| 256 | (expand-file-name "lread-resources/somelib.el" | ||
| 257 | ert--tests-dir) | ||
| 258 | nil nil nil 'replace)) | ||
| 259 | (advice-remove 'ask-user-about-supersession-threat auast) | ||
| 260 | (kill-buffer buf)) | ||
| 261 | ;; We should have prompted about the supersession threat. | ||
| 262 | (should asked)))) | ||
| 263 | |||
| 238 | 264 | ||
| 239 | ;;; fileio-tests.el ends here | 265 | ;;; fileio-tests.el ends here |