unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#66825: last-coding-system-used in basic-save-buffer
@ 2023-10-29 18:40 Juri Linkov
  2023-10-29 19:27 ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Juri Linkov @ 2023-10-29 18:40 UTC (permalink / raw)
  To: 66825

[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 #<buffer .dir-locals.el>)
>   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.





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-10-31  7:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-29 18:40 bug#66825: last-coding-system-used in basic-save-buffer Juri Linkov
2023-10-29 19:27 ` Eli Zaretskii
2023-10-30  7:56   ` Juri Linkov
2023-10-30 12:15     ` Eli Zaretskii
2023-10-30 17:20       ` Juri Linkov
2023-10-30 17:45         ` Eli Zaretskii
2023-10-31  7:23           ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).