diff options
| author | Stefan Monnier | 2024-06-04 10:58:29 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2024-06-06 18:50:58 -0400 |
| commit | d12c9bc2a4c8383abdf7fa32b67f1ca0379227e3 (patch) | |
| tree | 08f6fae69fa78d18623b6a01ec15786adf252f1e | |
| parent | 6906ed28f1b4ac0cf0a2a725e7402290f33c26e2 (diff) | |
| download | emacs-d12c9bc2a4c8383abdf7fa32b67f1ca0379227e3.tar.gz emacs-d12c9bc2a4c8383abdf7fa32b67f1ca0379227e3.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`.
| -rw-r--r-- | doc/lispref/nonascii.texi | 3 | ||||
| -rw-r--r-- | etc/NEWS | 5 | ||||
| -rw-r--r-- | lisp/international/mule.el | 17 |
3 files changed, 22 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 |
| @@ -2164,6 +2164,11 @@ completion candidate. | |||
| 2164 | * Lisp Changes in Emacs 30.1 | 2164 | * Lisp Changes in Emacs 30.1 |
| 2165 | 2165 | ||
| 2166 | +++ | 2166 | +++ |
| 2167 | ** 'auto-coding-functions' can know the name of the file. | ||
| 2168 | The functions on this hook take can now find the name of the file to | ||
| 2169 | which the text belongs by consulting the variable 'auto-coding-file-name'. | ||
| 2170 | |||
| 2171 | +++ | ||
| 2167 | ** New user option 'compilation-safety' to control safety of native code. | 2172 | ** New user option 'compilation-safety' to control safety of native code. |
| 2168 | It's now possible to control how safe is the code generated by native | 2173 | It's now possible to control how safe is the code generated by native |
| 2169 | compilation, by customizing this user option. It is also possible to | 2174 | 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. |