2

Chances are that every time utf-coding should be activated by invoking 'M-x revert-coding-system` , even though a long list of utf-prefer configuration:

(prefer-coding-system        'utf-8)
(set-terminal-coding-system  'utf-8)
(set-keyboard-coding-system  'utf-8)
(set-selection-coding-system 'utf-8)
(setq locale-coding-system   'utf-8)
(setq-default buffer-file-coding-system 'utf-8)

Try to automate the process by

;;Set buffer coding system
(add-hook 'org-mode-hook (lambda () (revert-buffer-with-coding-system  "utf-8")))

However, it does not work as expected.

What's the problem with my function to to handle the revert-buffer-with-coding-system'

2
  • 1. Calling revert-buffer-with-coding-system in the major mode hook looks wrong. The selection of the major mode (set-auto-mode in normal-mode in after-find-file) is part of revert-buffer. 2. Why do you want to call revert-buffer-with-coding-system in the first place? Does the auto-selection of the coding system go wrong such that characters are shown in wrong encoding?
    – Tobias
    Jul 17, 2019 at 8:50
  • Yes, it failed to display Chinese characters. @Tobias
    – Wizard
    Jul 17, 2019 at 18:24

1 Answer 1

1
+50

As I already tried to describe in my comment:
(add-hook 'org-mode-hook (lambda () (revert-buffer-with-coding-system "utf-8")))
cannot work because revert-buffer-with-coding-system indirectly runs org-mode-hook. So you get into an infinite loop.

Thereby, "indirectly runs" means:

  • the major mode hook org-mode-hook is run by the major mode function org-mode
  • the major mode function is run by set-auto-mode-0 with is part of set-auto-mode
  • set-auto-mode is run by normal-mode
  • normal-mode is run by revert-buffer (which is the essential part of revert-buffer-with-coding-system)

You can enforce the coding system for reading .org files with the following line in your init file:

(add-to-list 'file-coding-system-alist '("\\.org\\'" . utf-8))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.