diff options
| author | Stefan Monnier | 2024-06-04 10:58:29 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2024-06-09 00:19:03 -0400 |
| commit | 3ecc6b4f3c2b070ed2c4463e2c5d8755ccc19f1c (patch) | |
| tree | 36ec13f004e81faad33f8a42e30a96c4a40451d5 | |
| parent | ce5d004b5b093842d9c46976c50453015fe1a7e7 (diff) | |
| download | emacs-3ecc6b4f3c2b070ed2c4463e2c5d8755ccc19f1c.tar.gz emacs-3ecc6b4f3c2b070ed2c4463e2c5d8755ccc19f1c.zip | |
(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.
| -rw-r--r-- | doc/lispref/nonascii.texi | 3 | ||||
| -rw-r--r-- | etc/NEWS | 5 | ||||
| -rw-r--r-- | lisp/international/mule.el | 17 | ||||
| -rw-r--r-- | test/lisp/international/mule-tests.el | 19 | ||||
| -rw-r--r-- | test/lisp/international/mule-util-resources/test.utf-16le | bin | 0 -> 26 bytes |
5 files changed, 41 insertions, 3 deletions
diff --git a/doc/lispref/nonascii.texi b/doc/lispref/nonascii.texi index b33082e2b24..1482becb9f5 100644 --- a/doc/lispref/nonascii.texi +++ b/doc/lispref/nonascii.texi | |||
| @@ -1654,6 +1654,9 @@ argument, @var{size}, which tells it how many characters to look at, | |||
| 1654 | starting from point. If the function succeeds in determining a coding | 1654 | starting from point. If the function succeeds in determining a coding |
| 1655 | system for the file, it should return that coding system. Otherwise, | 1655 | system for the file, it should return that coding system. Otherwise, |
| 1656 | it should return @code{nil}. | 1656 | it should return @code{nil}. |
| 1657 | Each function can also find the name of the file to which | ||
| 1658 | the buffer's content belong in the variable | ||
| 1659 | @code{auto-coding-file-name}. | ||
| 1657 | 1660 | ||
| 1658 | The functions in this list could be called either when the file is | 1661 | The functions in this list could be called either when the file is |
| 1659 | visited and Emacs wants to decode its contents, and/or when the file's | 1662 | visited and Emacs wants to decode its contents, and/or when the file's |
| @@ -2187,6 +2187,11 @@ completion candidate. | |||
| 2187 | * Lisp Changes in Emacs 30.1 | 2187 | * Lisp Changes in Emacs 30.1 |
| 2188 | 2188 | ||
| 2189 | +++ | 2189 | +++ |
| 2190 | ** 'auto-coding-functions' can know the name of the file. | ||
| 2191 | The functions on this hook can now find the name of the file to | ||
| 2192 | which the text belongs by consulting the variable 'auto-coding-file-name'. | ||
| 2193 | |||
| 2194 | +++ | ||
| 2190 | ** New user option 'compilation-safety' to control safety of native code. | 2195 | ** New user option 'compilation-safety' to control safety of native code. |
| 2191 | It's now possible to control how safe is the code generated by native | 2196 | It's now possible to control how safe is the code generated by native |
| 2192 | compilation, by customizing this user option. It is also possible to | 2197 | compilation, by customizing this user option. It is also possible to |
diff --git a/lisp/international/mule.el b/lisp/international/mule.el index a17221e6d21..ed74fdae755 100644 --- a/lisp/international/mule.el +++ b/lisp/international/mule.el | |||
| @@ -1806,6 +1806,9 @@ or nil." | |||
| 1806 | (setq alist (cdr alist))))) | 1806 | (setq alist (cdr alist))))) |
| 1807 | coding-system))) | 1807 | coding-system))) |
| 1808 | 1808 | ||
| 1809 | (defvar auto-coding-file-name nil | ||
| 1810 | "Variable holding the name of the file for `auto-coding-functions'.") | ||
| 1811 | |||
| 1809 | ;; See the bottom of this file for built-in auto coding functions. | 1812 | ;; See the bottom of this file for built-in auto coding functions. |
| 1810 | (defcustom auto-coding-functions '(sgml-xml-auto-coding-function | 1813 | (defcustom auto-coding-functions '(sgml-xml-auto-coding-function |
| 1811 | sgml-html-meta-auto-coding-function) | 1814 | sgml-html-meta-auto-coding-function) |
| @@ -1820,6 +1823,9 @@ called both when the file is visited and Emacs wants to decode | |||
| 1820 | its contents, and when the file's buffer is about to be saved | 1823 | its contents, and when the file's buffer is about to be saved |
| 1821 | and Emacs wants to determine how to encode its contents. | 1824 | and Emacs wants to determine how to encode its contents. |
| 1822 | 1825 | ||
| 1826 | The name of the file is provided to the function via the variable | ||
| 1827 | `auto-coding-file-name'. | ||
| 1828 | |||
| 1823 | If one of these functions succeeds in determining a coding | 1829 | If one of these functions succeeds in determining a coding |
| 1824 | system, it should return that coding system. Otherwise, it | 1830 | system, it should return that coding system. Otherwise, it |
| 1825 | should return nil. | 1831 | should return nil. |
| @@ -1847,13 +1853,17 @@ files.") | |||
| 1847 | coding-system)) | 1853 | coding-system)) |
| 1848 | 1854 | ||
| 1849 | (put 'enable-character-translation 'permanent-local t) | 1855 | (put 'enable-character-translation 'permanent-local t) |
| 1850 | (put 'enable-character-translation 'safe-local-variable 'booleanp) | 1856 | (put 'enable-character-translation 'safe-local-variable #'booleanp) |
| 1851 | 1857 | ||
| 1852 | (defun find-auto-coding (filename size) | 1858 | (defun find-auto-coding (filename size) |
| 1859 | ;; FIXME: Shouldn't we use nil rather than "" to mean that there's no file? | ||
| 1860 | ;; FIXME: Clarify what the SOURCE is for in the return value? | ||
| 1853 | "Find a coding system for a file FILENAME of which SIZE bytes follow point. | 1861 | "Find a coding system for a file FILENAME of which SIZE bytes follow point. |
| 1854 | These bytes should include at least the first 1k of the file | 1862 | These bytes should include at least the first 1k of the file |
| 1855 | and the last 3k of the file, but the middle may be omitted. | 1863 | and the last 3k of the file, but the middle may be omitted. |
| 1856 | 1864 | ||
| 1865 | FILENAME should be an absolute file name | ||
| 1866 | or \"\" (which means that there is no associated file). | ||
| 1857 | The function checks FILENAME against the variable `auto-coding-alist'. | 1867 | The function checks FILENAME against the variable `auto-coding-alist'. |
| 1858 | If FILENAME doesn't match any entries in the variable, it checks the | 1868 | If FILENAME doesn't match any entries in the variable, it checks the |
| 1859 | contents of the current buffer following point against | 1869 | contents of the current buffer following point against |
| @@ -1998,7 +2008,8 @@ use \"coding: 'raw-text\" instead." :warning) | |||
| 1998 | (setq coding-system (ignore-errors | 2008 | (setq coding-system (ignore-errors |
| 1999 | (save-excursion | 2009 | (save-excursion |
| 2000 | (goto-char (point-min)) | 2010 | (goto-char (point-min)) |
| 2001 | (funcall (pop funcs) size))))) | 2011 | (let ((auto-coding-file-name filename)) |
| 2012 | (funcall (pop funcs) size)))))) | ||
| 2002 | (if coding-system | 2013 | (if coding-system |
| 2003 | (cons coding-system 'auto-coding-functions))))) | 2014 | (cons coding-system 'auto-coding-functions))))) |
| 2004 | 2015 | ||
| @@ -2013,7 +2024,7 @@ function by default." | |||
| 2013 | (if (and found (coding-system-p (car found))) | 2024 | (if (and found (coding-system-p (car found))) |
| 2014 | (car found)))) | 2025 | (car found)))) |
| 2015 | 2026 | ||
| 2016 | (setq set-auto-coding-function 'set-auto-coding) | 2027 | (setq set-auto-coding-function #'set-auto-coding) |
| 2017 | 2028 | ||
| 2018 | (defun after-insert-file-set-coding (inserted &optional visit) | 2029 | (defun after-insert-file-set-coding (inserted &optional visit) |
| 2019 | "Set `buffer-file-coding-system' of current buffer after text is inserted. | 2030 | "Set `buffer-file-coding-system' of current buffer after text is inserted. |
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 @@ | |||
| 25 | 25 | ||
| 26 | (require 'ert-x) ;For `ert-simulate-keys'. | 26 | (require 'ert-x) ;For `ert-simulate-keys'. |
| 27 | 27 | ||
| 28 | (defconst mule-tests--dir (file-name-directory (macroexp-file-name))) | ||
| 29 | |||
| 28 | (ert-deftest find-auto-coding--bug27391 () | 30 | (ert-deftest find-auto-coding--bug27391 () |
| 29 | "Check that Bug#27391 is fixed." | 31 | "Check that Bug#27391 is fixed." |
| 30 | (with-temp-buffer | 32 | (with-temp-buffer |
| @@ -94,6 +96,23 @@ | |||
| 94 | ;; The chinese-hz encoding is not ASCII compatible. | 96 | ;; The chinese-hz encoding is not ASCII compatible. |
| 95 | (should-not (coding-system-get 'chinese-hz :ascii-compatible-p))) | 97 | (should-not (coding-system-get 'chinese-hz :ascii-compatible-p))) |
| 96 | 98 | ||
| 99 | (defun mule-tests--auto-coding (_size) | ||
| 100 | (when (and (stringp auto-coding-file-name) | ||
| 101 | (string-match-p "\\.utf-16le\\'" auto-coding-file-name)) | ||
| 102 | 'utf-16le-with-signature)) | ||
| 103 | |||
| 104 | (ert-deftest mule-tests--auto-coding-functions () | ||
| 105 | (unwind-protect | ||
| 106 | (progn | ||
| 107 | (add-hook 'auto-coding-functions #'mule-tests--auto-coding) | ||
| 108 | (with-temp-buffer | ||
| 109 | (insert-file-contents | ||
| 110 | (expand-file-name "mule-util-resources/test.utf-16le" | ||
| 111 | mule-tests--dir)) | ||
| 112 | (goto-char (point-min)) | ||
| 113 | (should (search-forward "été" nil t)))) | ||
| 114 | (remove-hook 'auto-coding-functions #'mule-tests--auto-coding))) | ||
| 115 | |||
| 97 | ;;; Testing `sgml-html-meta-auto-coding-function'. | 116 | ;;; Testing `sgml-html-meta-auto-coding-function'. |
| 98 | 117 | ||
| 99 | (defvar sgml-html-meta-pre "<!doctype html><html><head>" | 118 | (defvar sgml-html-meta-pre "<!doctype html><html><head>" |
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 --- /dev/null +++ b/test/lisp/international/mule-util-resources/test.utf-16le | |||
| Binary files differ | |||