From 9ac6ff53b105925400a773a5088c9d0ec5b095a4 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 24 Jul 2021 11:16:15 +0200 Subject: [PATCH] Make the test for auto-mode-alist from .dir-local.el stricter * lisp/files.el (set-auto-mode--dir-local-valid-p): New function. (set-auto-mode--apply-alist): Use it as a stricter test. --- lisp/files.el | 15 +++++++++++---- test/lisp/files-resources/.dir-locals.el | 5 ++++- test/lisp/files-resources/auto-test.zot1 | 1 + test/lisp/files-resources/auto-test.zot2 | 1 + test/lisp/files-resources/auto-test.zot3 | 1 + test/lisp/files-tests.el | 8 +++++++- 6 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 test/lisp/files-resources/auto-test.zot1 create mode 100644 test/lisp/files-resources/auto-test.zot2 create mode 100644 test/lisp/files-resources/auto-test.zot3 diff --git a/lisp/files.el b/lisp/files.el index d915c2a30b5..aab839eab10 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -3238,14 +3238,21 @@ extra checks should be done." (setq mode (car mode) name (substring name 0 (match-beginning 0))) (setq name nil))) - (when (and dir-local mode) - (unless (string-suffix-p "-mode" (symbol-name mode)) - (message "Ignoring invalid mode `%s'" (symbol-name mode)) - (setq mode nil))) + (when (and dir-local mode + (not (set-auto-mode--dir-local-valid-p mode))) + (message "Ignoring invalid mode `%s'" mode) + (setq mode nil)) (when mode (set-auto-mode-0 mode keep-mode-if-same) t)))) +(defun set-auto-mode--dir-local-valid-p (mode) + "Say whether MODE can be used in a .dir-local.el `auto-mode-alist'." + (and (symbolp mode) + (string-suffix-p "-mode" (symbol-name mode)) + (commandp mode) + (not (provided-mode-derived-p mode 'special-mode)))) + (defun set-auto-mode (&optional keep-mode-if-same) "Select major mode appropriate for current buffer. diff --git a/test/lisp/files-resources/.dir-locals.el b/test/lisp/files-resources/.dir-locals.el index 84997b8a0c0..84393aa54d5 100644 --- a/test/lisp/files-resources/.dir-locals.el +++ b/test/lisp/files-resources/.dir-locals.el @@ -1,2 +1,5 @@ ;; This is used by files-tests.el. -((auto-mode-alist . (("\\.quux\\'" . tcl-mode)))) +((auto-mode-alist . (("\\.quux\\'" . tcl-mode) + ("\\.zot1\\'" . foobar) + ("\\.zot2\\'" . (lambda ())) + ("\\.zot3\\'" . dired-mode)))) diff --git a/test/lisp/files-resources/auto-test.zot1 b/test/lisp/files-resources/auto-test.zot1 new file mode 100644 index 00000000000..80acfcc9f7a --- /dev/null +++ b/test/lisp/files-resources/auto-test.zot1 @@ -0,0 +1 @@ +zot1 diff --git a/test/lisp/files-resources/auto-test.zot2 b/test/lisp/files-resources/auto-test.zot2 new file mode 100644 index 00000000000..975fc765513 --- /dev/null +++ b/test/lisp/files-resources/auto-test.zot2 @@ -0,0 +1 @@ +zot2 diff --git a/test/lisp/files-resources/auto-test.zot3 b/test/lisp/files-resources/auto-test.zot3 new file mode 100644 index 00000000000..faa07151606 --- /dev/null +++ b/test/lisp/files-resources/auto-test.zot3 @@ -0,0 +1 @@ +zot3 diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index fce7e3fd719..a5c82360177 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -1537,7 +1537,13 @@ The door of all subtleties! (ert-deftest files-test-dir-locals-auto-mode-alist () "Test an `auto-mode-alist' entry in `.dir-locals.el'" (find-file (ert-resource-file "whatever.quux")) - (should (eq major-mode 'tcl-mode))) + (should (eq major-mode 'tcl-mode)) + (find-file (ert-resource-file "auto-test.zot1")) + (should (eq major-mode 'fundamental-mode)) + (find-file (ert-resource-file "auto-test.zot2")) + (should (eq major-mode 'fundamental-mode)) + (find-file (ert-resource-file "auto-test.zot3")) + (should (eq major-mode 'fundamental-mode))) (provide 'files-tests) ;;; files-tests.el ends here -- 2.39.2