From 3ecc6b4f3c2b070ed2c4463e2c5d8755ccc19f1c Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Tue, 4 Jun 2024 10:58:29 -0400 Subject: (find-auto-coding): Provide filename to `auto-coding-functions` Allow `auto-coding-functions` to know the file name. Motivated by the needs of Editorconfig support. * lisp/international/mule.el (auto-coding-file-name): New var. (find-auto-coding): Let-bind it for `auto-coding-functions`. Document the expectation that the arg be an absolute file name. * doc/lispref/nonascii.texi (Default Coding Systems): Mention `auto-coding-file-name`. * test/lisp/international/mule-util-resources/test.utf-16le: New file. * test/lisp/international/mule-tests.el (mule-tests--dir): New var. (mule-tests--auto-coding): New fun. (mule-tests--auto-coding-functions): New test. --- test/lisp/international/mule-tests.el | 19 +++++++++++++++++++ .../international/mule-util-resources/test.utf-16le | Bin 0 -> 26 bytes 2 files changed, 19 insertions(+) create mode 100644 test/lisp/international/mule-util-resources/test.utf-16le (limited to 'test') diff --git a/test/lisp/international/mule-tests.el b/test/lisp/international/mule-tests.el index 9a80ced55ae..9c869cc8e6f 100644 --- a/test/lisp/international/mule-tests.el +++ b/test/lisp/international/mule-tests.el @@ -25,6 +25,8 @@ (require 'ert-x) ;For `ert-simulate-keys'. +(defconst mule-tests--dir (file-name-directory (macroexp-file-name))) + (ert-deftest find-auto-coding--bug27391 () "Check that Bug#27391 is fixed." (with-temp-buffer @@ -94,6 +96,23 @@ ;; The chinese-hz encoding is not ASCII compatible. (should-not (coding-system-get 'chinese-hz :ascii-compatible-p))) +(defun mule-tests--auto-coding (_size) + (when (and (stringp auto-coding-file-name) + (string-match-p "\\.utf-16le\\'" auto-coding-file-name)) + 'utf-16le-with-signature)) + +(ert-deftest mule-tests--auto-coding-functions () + (unwind-protect + (progn + (add-hook 'auto-coding-functions #'mule-tests--auto-coding) + (with-temp-buffer + (insert-file-contents + (expand-file-name "mule-util-resources/test.utf-16le" + mule-tests--dir)) + (goto-char (point-min)) + (should (search-forward "été" nil t)))) + (remove-hook 'auto-coding-functions #'mule-tests--auto-coding))) + ;;; Testing `sgml-html-meta-auto-coding-function'. (defvar sgml-html-meta-pre "" diff --git a/test/lisp/international/mule-util-resources/test.utf-16le b/test/lisp/international/mule-util-resources/test.utf-16le new file mode 100644 index 00000000000..8536adb5341 Binary files /dev/null and b/test/lisp/international/mule-util-resources/test.utf-16le differ -- cgit v1.2.1