aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorStefan Monnier2024-06-04 10:58:29 -0400
committerStefan Monnier2024-06-09 00:19:03 -0400
commit3ecc6b4f3c2b070ed2c4463e2c5d8755ccc19f1c (patch)
tree36ec13f004e81faad33f8a42e30a96c4a40451d5 /lisp
parentce5d004b5b093842d9c46976c50453015fe1a7e7 (diff)
downloademacs-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.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/international/mule.el17
1 files changed, 14 insertions, 3 deletions
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.