From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Lars Ingebrigtsen Newsgroups: gmane.emacs.bugs Subject: bug#13522: 24.2; save-buffer removes edited file under some conditions Date: Sat, 30 Apr 2022 18:47:04 +0200 Message-ID: <87r15ey0vr.fsf@gnus.org> References: <87622qaszq.fsf@xvii.vinc17.org> <8735jkye2q.fsf@gnus.org> <83mthsk63c.fsf@gnu.org> <87sfrkwsy5.fsf@gnus.org> <83fsnkk4so.fsf@gnu.org> <87ee33wifm.fsf@gnus.org> <83tubzi9ap.fsf@gnu.org> <87wngvuwb9.fsf@gnus.org> <20220315155522.GQ31771@zira.vinc17.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22260"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: Glenn Morris , 13522@debbugs.gnu.org To: Vincent Lefevre Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Apr 30 18:48:11 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nkqGP-0005Ux-TF for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 30 Apr 2022 18:48:10 +0200 Original-Received: from localhost ([::1]:54470 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nkqGO-0001Hu-LJ for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 30 Apr 2022 12:48:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52816) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nkqGI-0001HN-En for bug-gnu-emacs@gnu.org; Sat, 30 Apr 2022 12:48:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36838) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nkqGI-0007Xt-5N for bug-gnu-emacs@gnu.org; Sat, 30 Apr 2022 12:48:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nkqGI-0007YW-1o for bug-gnu-emacs@gnu.org; Sat, 30 Apr 2022 12:48:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Lars Ingebrigtsen Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 30 Apr 2022 16:48:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 13522 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: confirmed Original-Received: via spool by 13522-submit@debbugs.gnu.org id=B13522.165133723828861 (code B ref 13522); Sat, 30 Apr 2022 16:48:02 +0000 Original-Received: (at 13522) by debbugs.gnu.org; 30 Apr 2022 16:47:18 +0000 Original-Received: from localhost ([127.0.0.1]:58968 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nkqFZ-0007VQ-Tb for submit@debbugs.gnu.org; Sat, 30 Apr 2022 12:47:18 -0400 Original-Received: from quimby.gnus.org ([95.216.78.240]:50048) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nkqFY-0007V9-Fa for 13522@debbugs.gnu.org; Sat, 30 Apr 2022 12:47:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnus.org; s=20200322; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date: References:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=+B+TXkcqNZY9BQQaAQlxx/4HlF8M+aDmBIVjfn//bS8=; b=OsyVyGYde7idrh2waj8mCEq7R2 c7yJgpKISLqpSa0xJJVCDDg+BmzallfE3I7owADUaH78KWHPRXMBSV6QiDcz7QzptvTaodRJ1rv38 jeD5BCvwaslX+wXCGjzT9n2DCtDeAqvLVOi1GBiR5xs33VEBSEcOun/S6wgGN+qEapOM=; Original-Received: from [84.212.220.105] (helo=xo) by quimby.gnus.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nkqFM-0001ky-Q5; Sat, 30 Apr 2022 18:47:07 +0200 X-Now-Playing: Talking Heads's _The Name Of This Band Is Talking Heads_: "track" In-Reply-To: <20220315155522.GQ31771@zira.vinc17.org> (Vincent Lefevre's message of "Tue, 15 Mar 2022 16:55:22 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:231057 Archived-At: Vincent Lefevre writes: >> Yes, but the file is apparently missing after `C-z'. > > Actually, as I've said in another mail, you do not even need to do C-z. > The file is missing after C-x C-s: if I try to look at the file from > a different terminal (a real terminal or with GNU Screen), this file > is missing. This may also be problematic for those who have automatic > backups to a remote storage area (in particular if files matching *~ > are ignored for these backups). Yup; it's not ideal. Glenn applied one solution to this (binding coding-system-for-write), but that had other problems, apparently. I've now respun his original patch, and as far as I can tell, it works pretty well. However, it breaks files-tests-bug-18141. But before I try to debug this, does anybody see anything fundamentally misguided about this approach? diff --git a/lisp/files.el b/lisp/files.el index 2b0dc54db8..7e3bea614e 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5649,7 +5649,7 @@ basic-save-buffer-1 ;; This returns a value (MODES EXTENDED-ATTRIBUTES BACKUPNAME), like ;; backup-buffer. (defun basic-save-buffer-2 () - (let (tempsetmodes setmodes) + (let (tempsetmodes setmodes writecoding) (if (not (file-writable-p buffer-file-name)) (let ((dir (file-name-directory buffer-file-name))) (if (not (file-directory-p dir)) @@ -5665,6 +5665,9 @@ basic-save-buffer-2 buffer-file-name))) (setq tempsetmodes t) (error "Attempt to save to a file that you aren't allowed to write")))))) + (setq writecoding + (choose-write-coding-system nil nil buffer-file-name nil t + buffer-file-truename)) (or buffer-backed-up (setq setmodes (backup-buffer))) (let* ((dir (file-name-directory buffer-file-name)) @@ -5697,8 +5700,9 @@ basic-save-buffer-2 ;; Pass in nil&nil rather than point-min&max ;; cause we're saving the whole buffer. ;; write-region-annotate-functions may use it. - (write-region nil nil tempname nil realname - buffer-file-truename) + (let ((write-region-coding-system writecoding)) + (write-region nil nil tempname nil realname + buffer-file-truename)) (when save-silently (message nil))) ;; If we failed, restore the buffer's modtime. (error (set-visited-file-modtime old-modtime) @@ -5743,8 +5747,9 @@ basic-save-buffer-2 ;; Pass in nil&nil rather than point-min&max to indicate ;; we're saving the buffer rather than just a region. ;; write-region-annotate-functions may make use of it. - (write-region nil nil - buffer-file-name nil t buffer-file-truename) + (let ((write-region-coding-system writecoding)) + (write-region nil nil + buffer-file-name nil t buffer-file-truename)) (when save-silently (message nil)) (setq success t)) ;; If we get an error writing the new file, and we made diff --git a/src/fileio.c b/src/fileio.c index c418036fc6..f04383252a 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -5012,14 +5012,22 @@ build_annotations_unwind (Lisp_Object arg) /* Decide the coding-system to encode the data with. */ -static Lisp_Object -choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object filename, - Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, - struct coding_system *coding) +DEFUN ("choose-write-coding-system", Fchoose_write_coding_system, + Schoose_write_coding_system, 3, 6, 0, + doc: /* Choose coding system for write. +Arguments as for `write-region'. */ ) + (Lisp_Object start, Lisp_Object end, Lisp_Object filename, + Lisp_Object append, Lisp_Object visit, Lisp_Object lockname) { Lisp_Object val; Lisp_Object eol_parent = Qnil; + if (NILP (start)) + { + XSETFASTINT (start, BEGV); + XSETFASTINT (end, ZV); + } + if (auto_saving && NILP (Fstring_equal (BVAR (current_buffer, filename), BVAR (current_buffer, auto_save_file_name)))) @@ -5119,10 +5127,6 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file } val = coding_inherit_eol_type (val, eol_parent); - setup_coding_system (val, coding); - - if (!STRINGP (start) && EQ (Qt, BVAR (current_buffer, selective_display))) - coding->mode |= CODING_MODE_SELECTIVE_DISPLAY; return val; } @@ -5287,9 +5291,15 @@ write_region (Lisp_Object start, Lisp_Object end, Lisp_Object filename, We used to make this choice before calling build_annotations, but that leads to problems when a write-annotate-function takes care of unsavable chars (as was the case with X-Symbol). */ - Vlast_coding_system_used - = choose_write_coding_system (start, end, filename, - append, visit, lockname, &coding); + Vlast_coding_system_used = NILP (Vwrite_region_coding_system) ? + Fchoose_write_coding_system (start, end, filename, + append, visit, lockname) : + Vwrite_region_coding_system; + + setup_coding_system (Vlast_coding_system_used, &coding); + + if (!STRINGP (start) && !NILP (BVAR (current_buffer, selective_display))) + coding.mode |= CODING_MODE_SELECTIVE_DISPLAY; if (open_and_close_file && !auto_saving) { @@ -6372,6 +6382,7 @@ syms_of_fileio (void) DEFSYM (Qset_file_acl, "set-file-acl"); DEFSYM (Qfile_newer_than_file_p, "file-newer-than-file-p"); DEFSYM (Qinsert_file_contents, "insert-file-contents"); + DEFSYM (Qchoose_write_coding_system, "choose-write-coding-system"); DEFSYM (Qwrite_region, "write-region"); DEFSYM (Qverify_visited_file_modtime, "verify-visited-file-modtime"); DEFSYM (Qset_visited_file_modtime, "set-visited-file-modtime"); @@ -6614,6 +6625,11 @@ do (file-exists-p FILENAME) and FILENAME is handled by HANDLER, then DEFSYM (Qstdout, "stdout"); DEFSYM (Qstderr, "stderr"); + DEFVAR_LISP ("write-region-coding-system", Vwrite_region_coding_system, + doc: /* If non-nil, coding system for `write-region'. + You should only ever `let'-bind this around a `write-region' call. */); + Vwrite_region_coding_system = Qnil; + defsubr (&Sfind_file_name_handler); defsubr (&Sfile_name_directory); defsubr (&Sfile_name_nondirectory); @@ -6655,6 +6671,7 @@ do (file-exists-p FILENAME) and FILENAME is handled by HANDLER, then defsubr (&Sdefault_file_modes); defsubr (&Sfile_newer_than_file_p); defsubr (&Sinsert_file_contents); + defsubr (&Schoose_write_coding_system); defsubr (&Swrite_region); defsubr (&Scar_less_than_car); defsubr (&Sverify_visited_file_modtime); -- (domestic pets only, the antidote for overdose, milk.) bloggy blog: http://lars.ingebrigtsen.no