From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#66825: last-coding-system-used in basic-save-buffer Date: Sun, 29 Oct 2023 20:40:32 +0200 Organization: LINKOV.NET Message-ID: <86v8apv3m7.fsf_-_@mail.linkov.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37743"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) To: 66825@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Oct 29 19:44:00 2023 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 1qxAlU-0009fn-KT for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 29 Oct 2023 19:44:00 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qxAl2-00018B-QT; Sun, 29 Oct 2023 14:43:32 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qxAl0-00017b-H3 for bug-gnu-emacs@gnu.org; Sun, 29 Oct 2023 14:43:30 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qxAkz-0007tm-R6 for bug-gnu-emacs@gnu.org; Sun, 29 Oct 2023 14:43:30 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qxAlW-00081e-Ff for bug-gnu-emacs@gnu.org; Sun, 29 Oct 2023 14:44:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 29 Oct 2023 18:44:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 66825 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.169860502230808 (code B ref -1); Sun, 29 Oct 2023 18:44:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 29 Oct 2023 18:43:42 +0000 Original-Received: from localhost ([127.0.0.1]:43346 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qxAlC-00080p-Aa for submit@debbugs.gnu.org; Sun, 29 Oct 2023 14:43:42 -0400 Original-Received: from lists.gnu.org ([2001:470:142::17]:39176) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qxAl7-00080K-VP for submit@debbugs.gnu.org; Sun, 29 Oct 2023 14:43:38 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qxAkV-00014k-SF for bug-gnu-emacs@gnu.org; Sun, 29 Oct 2023 14:42:59 -0400 Original-Received: from relay2-d.mail.gandi.net ([217.70.183.194]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qxAkT-0007qy-KZ for bug-gnu-emacs@gnu.org; Sun, 29 Oct 2023 14:42:59 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 988E640005 for ; Sun, 29 Oct 2023 18:42:54 +0000 (UTC) X-GND-Sasl: juri@linkov.net Received-SPF: pass client-ip=217.70.183.194; envelope-from=juri@linkov.net; helo=relay2-d.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:273521 Archived-At: [Creating a separate bug report from bug#66317 since sometimes the bug occurs even when project-mode-line is nil] >> Ok, here is 100% reproducible minimal test case: >> 0. emacs-30 -Q >> 1. eval: >> (progn >> (require 'project) >> (setq project-mode-line t) >> (setq set-message-functions '(set-multi-message))) >> 2. in a temporary directory: M-! git init RET >> 3. C-x C-f .dir-locals.el RET >> 4. insert: ((fundamental-mode . ((mode . flyspell)))) >> 5. C-x C-s >> At this point even buffer-file-coding-system of .dir-locals.el >> is changed to 't' (raw-text-unix). The same happens when saving >> any file in that project. >> The problem doesn't occur when flyspell-mode is enabled from >> file-local variables, only from dir-locals. > > I can repro. But it's as weird a bug as they come. > > I guess it's a combination of using flyspell-mode and editing > .dir-locals.el? Or have you seen other buffers' b-f-c-s changed this way > too? > > If not, it might have something to do with flyspell-mode's use of > sit-for?.. But I'm only saying that because it's the only feature of this > mode that I'm regularly reminded of. > > I tried using a variable watcher: > > (add-variable-watcher > 'buffer-file-coding-system > (lambda (_sym value op where) > (message "%s %s %s" value op where) > (if (eq 'raw-text-unix value) (backtrace)) > )) > > but it just prints > > prefer-utf-8-unix set *temp*-925453 [2 times] > raw-text-unix set .dir-locals.el > backtrace() > (if (eq 'raw-text-unix value) (backtrace)) > (closure (t) (_sym value op where) (message "%s %s %s" value op where) > (if (eq 'raw-text-unix value) (backtrace)))(buffer-file-coding-system > raw-text-unix set #) > basic-save-buffer(t) > save-buffer(1) > funcall-interactively(save-buffer 1) > call-interactively(save-buffer nil nil) > command-execute(save-buffer) > > OTOH, the bug is very reliable to reproduce: add the aforementioned line to > dir-locals and save -> the coding system changes to raw-text. Delete the > line and save -> and it's prefer-utf-8 again. Indeed, the problem is in basic-save-buffer on the following line: (setq buffer-file-coding-system last-coding-system-used) It's hard to guess why this code relies on the value that can be changed by other functions during saving the buffer. For example, (progn (setq last-coding-system-used 'prefer-utf-8-unix) (project-name (project-current)) (message "%S" last-coding-system-used)) prints "raw-text-unix" because it enables 'flyspell-mode' that calls: (defun ispell-buffer-local-parsing () (ispell-send-string "!\n") where 'process-send-string' changes 'last-coding-system-used' to "raw-text-unix" in: send_process (Lisp_Object proc, const char *buf, ptrdiff_t len, Lisp_Object object) { ... Vlast_coding_system_used = CODING_ID_NAME (coding->id); A possible workaround would be to protect the value of last-coding-system-used in 'project-mode-line-format': (defun project-mode-line-format () "Compose the project mode-line." (when-let ((project (project-current))) (let (last-coding-system-used) (concat " " (propertize (project-name project) 'face project-mode-line-face 'mouse-face 'mode-line-highlight 'help-echo "mouse-1: Project menu" 'local-map project-mode-line-map))))) However, I noticed that occasionally this bug occurs even when this function is not used. So the proper fix needed in 'basic-save-buffer', but I don't know if it's intended that some function should change 'last-coding-system-used' during saving the buffer.