From 35592141cc8951c91a4bd6cc02f73d1ddd35aca4 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 19 Apr 2022 16:22:37 +0200 Subject: [PATCH] Allow several mode: elements in the local variable section * etc/NEWS (mode): Fall back on outline-mode in older Emacsen. * lisp/files.el (hack-local-variables--find-variables): Use the final mode: line (which is the same as having several mode: bits in the header line. --- etc/NEWS | 1 + lisp/files.el | 11 +++++++---- test/lisp/files-resources/file-mode-multiple | 5 +++++ test/lisp/files-tests.el | 4 ++++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 test/lisp/files-resources/file-mode-multiple diff --git a/etc/NEWS b/etc/NEWS index 3e7788277d3..496ff32d202 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2071,6 +2071,7 @@ along with GNU Emacs. If not, see . Local variables: coding: utf-8 +mode: outline mode: emacs-news paragraph-separate: "[ ]*$" end: diff --git a/lisp/files.el b/lisp/files.el index 135a6177e9c..a9d4b5aea81 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -3978,8 +3978,7 @@ major-mode." (forward-line 1)) (goto-char (point-min)) - (while (not (or (eobp) - (and (eq handle-mode t) result))) + (while (not (eobp)) ;; Find the variable name; (unless (looking-at hack-local-variable-regexp) (user-error "Malformed local variable line: %S" @@ -4005,7 +4004,8 @@ major-mode." (not (string-match "-minor\\'" (setq val2 (downcase (symbol-name val))))) - (setq result (intern (concat val2 "-mode")))) + ;; Allow several mode: elements. + (push (intern (concat val2 "-mode")) result)) (cond ((eq var 'coding)) ((eq var 'lexical-binding) (unless hack-local-variables--warned-lexical @@ -4029,7 +4029,10 @@ major-mode." val) result)))))) (forward-line 1))))))) - result)) + (if (eq handle-mode t) + ;; Return the final mode: setting that's defined. + (car (seq-filter #'fboundp result)) + result))) (defun hack-local-variables-apply () "Apply the elements of `file-local-variables-alist'. diff --git a/test/lisp/files-resources/file-mode-multiple b/test/lisp/files-resources/file-mode-multiple new file mode 100644 index 00000000000..ac051ccbcbd --- /dev/null +++ b/test/lisp/files-resources/file-mode-multiple @@ -0,0 +1,5 @@ +Local variables: +mode: text +mode: test-mode-undef +mode: outline +end: diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index f76d047f302..c886bd79851 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -1821,5 +1821,9 @@ Prompt users for any modified buffer with `buffer-offer-save' non-nil." ;; Check that the mode cookie doesn't override the explicit setting. (should (eq major-mode 'emacs-lisp-mode))) +(ert-deftest files-test-set-mode-multiple () + (find-file (ert-resource-file "file-mode-multiple")) + (should (eq major-mode 'outline-mode))) + (provide 'files-tests) ;;; files-tests.el ends here -- 2.39.2