Subject: [PATCH] Handle local-variable major-mode remaps specifying non-existent mode
From: Brian Leung <[email-masked]>
To: [email-masked]
Date: Tue, 12 Dec 2023 13:52:53 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, 
cairo
version 1.18.0, Xaw3d scroll bars)
Repository revision: 9434ad25ce2747864e0bcf5665f65eb65a079178
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 
11.0.12101009
System Description: NixOS 24.05 (Uakari)

Configured using:
 'configure
 --prefix=/nix/store/q131p42vldq964fr9rpdz1qmsqrywa00-emacs-git-20231211.0
 --disable-build-details --with-modules --with-x-toolkit=lucid
 --with-xft --with-cairo --with-compress-install
 --with-toolkit-scroll-bars --with-native-compilation
 --without-imagemagick --without-small-ja-dic --with-tree-sitter
 --without-xinput2 --without-xwidgets'
From 46435cac589506ecb131f480171a2ec1f0f03c55 Mon Sep 17 00:00:00 2001
From: Brian Leung <[email-masked]>
Date: Tue, 12 Dec 2023 05:42:56 -0800
Subject: [PATCH] Handle local-variable major-mode remaps specifying
 non-existent mode

In the .clang-format file of current Emacs HEAD, the major mode is
specified as yaml-mode via a local variable.  However, a user who has
loaded yaml-ts-mode and executed

(add-to-list 'major-mode-remap-alist '(yaml-mode . yaml-ts-mode)

but does not have yaml-mode defined will find that opening the
.clang-format file does not use yaml-ts-mode.

This patch fixes that.

* lisp/files.el (set-auto-mode): Check for any remapping specified in
major-mode-remap-alist.
(hack-local-variables--find-variables): Same.
---
 lisp/files.el | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lisp/files.el b/lisp/files.el
index f87e7807301..8e92da6d49d 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -3445,7 +3445,7 @@ set-auto-mode
     (and (not done)
 	 (setq mode (hack-local-variables t (not try-locals)))
 	 (not (memq mode modes))	; already tried and failed
-	 (if (not (functionp mode))
+	 (if (not (functionp (alist-get mode major-mode-remap-alist mode)))
 	     (message "Ignoring unknown mode `%s'" mode)
 	   (setq done t)
 	   (set-auto-mode-0 mode keep-mode-if-same)))
@@ -4182,7 +4182,9 @@ hack-local-variables--find-variables
 	        (forward-line 1)))))))
     (if (eq handle-mode t)
         ;; Return the final mode: setting that's defined.
-        (car (seq-filter #'fboundp result))
+        (seq-find (lambda (mode)
+                    (fboundp (alist-get mode major-mode-remap-alist mode)))
+                  result)
       result)))
 
 (defun hack-local-variables-apply ()
-- 
2.42.0