aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2024-06-04 10:58:29 -0400
committerStefan Monnier2024-06-06 18:50:58 -0400
commitd12c9bc2a4c8383abdf7fa32b67f1ca0379227e3 (patch)
tree08f6fae69fa78d18623b6a01ec15786adf252f1e
parent6906ed28f1b4ac0cf0a2a725e7402290f33c26e2 (diff)
downloademacs-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.texi3
-rw-r--r--etc/NEWS5
-rw-r--r--lisp/international/mule.el17
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,
1654starting from point. If the function succeeds in determining a coding 1654starting from point. If the function succeeds in determining a coding
1655system for the file, it should return that coding system. Otherwise, 1655system for the file, it should return that coding system. Otherwise,
1656it should return @code{nil}. 1656it should return @code{nil}.
1657Each function can also find the name of the file to which
1658the buffer's content belong in the variable
1659@code{auto-coding-file-name}.
1657 1660
1658The functions in this list could be called either when the file is 1661The functions in this list could be called either when the file is
1659visited and Emacs wants to decode its contents, and/or when the file's 1662visited and Emacs wants to decode its contents, and/or when the file's
diff --git a/etc/NEWS b/etc/NEWS
index 808cd0562db..52486b7bbe9 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -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.
2168The functions on this hook take can now find the name of the file to
2169which 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.
2168It's now possible to control how safe is the code generated by native 2173It's now possible to control how safe is the code generated by native
2169compilation, by customizing this user option. It is also possible to 2174compilation, 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
1820its contents, and when the file's buffer is about to be saved 1823its contents, and when the file's buffer is about to be saved
1821and Emacs wants to determine how to encode its contents. 1824and Emacs wants to determine how to encode its contents.
1822 1825
1826The name of the file is provided to the function via the variable
1827`auto-coding-file-name'.
1828
1823If one of these functions succeeds in determining a coding 1829If one of these functions succeeds in determining a coding
1824system, it should return that coding system. Otherwise, it 1830system, it should return that coding system. Otherwise, it
1825should return nil. 1831should 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.
1854These bytes should include at least the first 1k of the file 1862These bytes should include at least the first 1k of the file
1855and the last 3k of the file, but the middle may be omitted. 1863and the last 3k of the file, but the middle may be omitted.
1856 1864
1865FILENAME should be an absolute file name
1866or \"\" (which means that there is no associated file).
1857The function checks FILENAME against the variable `auto-coding-alist'. 1867The function checks FILENAME against the variable `auto-coding-alist'.
1858If FILENAME doesn't match any entries in the variable, it checks the 1868If FILENAME doesn't match any entries in the variable, it checks the
1859contents of the current buffer following point against 1869contents 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.