From d12c9bc2a4c8383abdf7fa32b67f1ca0379227e3 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Tue, 4 Jun 2024 10:58:29 -0400 Subject: [PATCH 1/2] (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`. --- doc/lispref/nonascii.texi | 3 +++ etc/NEWS | 5 +++++ 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 @@ Default Coding Systems starting from point. If the function succeeds in determining a coding system for the file, it should return that coding system. Otherwise, it should return @code{nil}. +Each function can also find the name of the file to which +the buffer's content belong in the variable +@code{auto-coding-file-name}. The functions in this list could be called either when the file is visited 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 @@ -2163,6 +2163,11 @@ completion candidate. * Lisp Changes in Emacs 30.1 ++++ +** 'auto-coding-functions' can know the name of the file. +The functions on this hook take can now find the name of the file to +which the text belongs by consulting the variable 'auto-coding-file-name'. + +++ ** New user option 'compilation-safety' to control safety of native code. It's now possible to control how safe is the code generated by native 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 @@ auto-coding-regexp-alist-lookup (setq alist (cdr alist))))) coding-system))) +(defvar auto-coding-file-name nil + "Variable holding the name of the file for `auto-coding-functions'.") + ;; See the bottom of this file for built-in auto coding functions. (defcustom auto-coding-functions '(sgml-xml-auto-coding-function sgml-html-meta-auto-coding-function) @@ -1820,6 +1823,9 @@ auto-coding-functions its contents, and when the file's buffer is about to be saved and Emacs wants to determine how to encode its contents. +The name of the file is provided to the function via the variable +`auto-coding-file-name'. + If one of these functions succeeds in determining a coding system, it should return that coding system. Otherwise, it should return nil. @@ -1847,13 +1853,17 @@ auto-coding-alist-lookup coding-system)) (put 'enable-character-translation 'permanent-local t) -(put 'enable-character-translation 'safe-local-variable 'booleanp) +(put 'enable-character-translation 'safe-local-variable #'booleanp) (defun find-auto-coding (filename size) + ;; FIXME: Shouldn't we use nil rather than "" to mean that there's no file? + ;; FIXME: Clarify what the SOURCE is for in the return value? "Find a coding system for a file FILENAME of which SIZE bytes follow point. These bytes should include at least the first 1k of the file and the last 3k of the file, but the middle may be omitted. +FILENAME should be an absolute file name +or \"\" (which means that there is no associated file). The function checks FILENAME against the variable `auto-coding-alist'. If FILENAME doesn't match any entries in the variable, it checks the contents of the current buffer following point against @@ -1998,7 +2008,8 @@ find-auto-coding (setq coding-system (ignore-errors (save-excursion (goto-char (point-min)) - (funcall (pop funcs) size))))) + (let ((auto-coding-file-name filename)) + (funcall (pop funcs) size)))))) (if coding-system (cons coding-system 'auto-coding-functions))))) @@ -2013,7 +2024,7 @@ set-auto-coding (if (and found (coding-system-p (car found))) (car found)))) -(setq set-auto-coding-function 'set-auto-coding) +(setq set-auto-coding-function #'set-auto-coding) (defun after-insert-file-set-coding (inserted &optional visit) "Set `buffer-file-coding-system' of current buffer after text is inserted. -- 2.39.2