From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#67795: [PATCH] Handle local-variable major-mode remaps specifying non-existent mode Date: Tue, 12 Dec 2023 11:02:43 -0500 Message-ID: References: <87r0jrplx1.fsf@posteo.net> <83plzbtt2w.fsf@gnu.org> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19935"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Brian Leung , 67795@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Dec 12 17:04:04 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rD5Ep-0004zd-Qe for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 12 Dec 2023 17:04:04 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rD5Ea-0002TI-TX; Tue, 12 Dec 2023 11:03:48 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rD5EY-0002NL-T2 for bug-gnu-emacs@gnu.org; Tue, 12 Dec 2023 11:03:47 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rD5EY-0002iQ-Ib for bug-gnu-emacs@gnu.org; Tue, 12 Dec 2023 11:03:46 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rD5En-000133-V7 for bug-gnu-emacs@gnu.org; Tue, 12 Dec 2023 11:04:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 12 Dec 2023 16:04:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67795 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 67795-submit@debbugs.gnu.org id=B67795.17023969893947 (code B ref 67795); Tue, 12 Dec 2023 16:04:01 +0000 Original-Received: (at 67795) by debbugs.gnu.org; 12 Dec 2023 16:03:09 +0000 Original-Received: from localhost ([127.0.0.1]:57295 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rD5Dw-00011a-O6 for submit@debbugs.gnu.org; Tue, 12 Dec 2023 11:03:09 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:32376) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rD5Dv-000112-Aq for 67795@debbugs.gnu.org; Tue, 12 Dec 2023 11:03:07 -0500 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 7AB8544473B; Tue, 12 Dec 2023 11:02:46 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1702396964; bh=SQzfsTHyCsb/+4+DdqP8IQGDEy0c/EH05tePNGPXZBA=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=SuhctnhXc3velL8DdVaxWlRR8b38d2e92iHuJitFa3xqO/YthOBMCdAwvuaR0yYvI ie40taZ5C0sbfONBoy1ZHghrcnT6f5g+bi+qaEyqU8J0NrtcWBRYSvS8wpRXnJ4eKj Io/k/XCvTyQFbM7hZm97DV4RCBDnCB5JMovX1F2HYwbWxXeYc5VN4U9Raj8yl/tT8Z kdx1hEhotnbzXO5u/d+e2MzGdpofL7kJ1P5dsK+J4A67uhkNwOmN+1WQt6DEVyP4eT kF2RiAIeJ75cGw0bXV0cH+zKRMt1FRpj7Gob9eylT3xhNkRRwSEeTTnP/gEKLcIrxc sHQs5Qa7rK9Hw== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id C640D444738; Tue, 12 Dec 2023 11:02:44 -0500 (EST) Original-Received: from pastel (unknown [45.72.203.88]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 9E826120370; Tue, 12 Dec 2023 11:02:44 -0500 (EST) In-Reply-To: <83plzbtt2w.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 12 Dec 2023 16:04:55 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:276058 Archived-At: --=-=-= Content-Type: text/plain > Thanks, but if we want to integrate major-mode-remap-alist better, I'd > rather we did that in some lower-level place, so that we wouldn't need > to sprinkle these alist-get calls all over Emacs. > > Also, if we do this, there will be no way for specifying a particular > mode in file-local variables. Do we really want that? > > Stefan, WDYT? I agree that we should try to keep it in "one place", but I don't think it can be done right now without some code reorganization :-( I can't wrap my head around what `hack-local-variables--find-variables` is supposed to do, but for the other part of the change, maybe the patch below is a good start? Stefan --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=mode-remap.patch diff --git a/lisp/files.el b/lisp/files.el index f87e7807301..1935e4dbb4b 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -3428,12 +3451,9 @@ set-auto-mode (if modes (catch 'nop (dolist (mode (nreverse modes)) - (if (not (functionp mode)) - (message "Ignoring unknown mode `%s'" mode) - (setq done t) - (or (set-auto-mode-0 mode keep-mode-if-same) - ;; continuing would call minor modes again, toggling them off - (throw 'nop nil)))))) + (or (setq done (set-auto-mode-0 mode keep-mode-if-same)) + ;; continuing would call minor modes again, toggling them off + (throw 'nop nil))))) ;; Check for auto-mode-alist entry in dir-locals. (unless done (with-demoted-errors "Directory-local variables error: %s" @@ -3445,10 +3465,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)) - (message "Ignoring unknown mode `%s'" mode) - (setq done t) - (set-auto-mode-0 mode keep-mode-if-same))) + (setq done (set-auto-mode-0 mode keep-mode-if-same))) ;; If we didn't, look for an interpreter specified in the first line. ;; As a special case, allow for things like "#!/bin/env perl", which ;; finds the interpreter anywhere in $PATH. @@ -3490,7 +3507,7 @@ set-auto-mode (error "Problem in magic-mode-alist with element %s" re)))))))) - (set-auto-mode-0 done keep-mode-if-same))) + (setq done (set-auto-mode-0 done keep-mode-if-same)))) ;; Next compare the filename against the entries in auto-mode-alist. (unless done (setq done (set-auto-mode--apply-alist auto-mode-alist @@ -3515,7 +3532,7 @@ set-auto-mode (error "Problem with magic-fallback-mode-alist element: %s" re)))))))) - (set-auto-mode-0 done keep-mode-if-same))) + (setq done (set-auto-mode-0 done keep-mode-if-same)))) (unless done (set-buffer-major-mode (current-buffer))))) @@ -3539,17 +3556,22 @@ set-auto-mode-0 If optional arg KEEP-MODE-IF-SAME is non-nil, MODE is chased of any aliases and compared to current major mode. If they are the same, do nothing and return nil." - (unless (and keep-mode-if-same - (or (eq (indirect-function mode) + (let ((modefun (alist-get mode major-mode-remap-alist mode))) + (unless (and keep-mode-if-same + (or (eq (indirect-function mode) (indirect-function major-mode)) (and set-auto-mode--last (eq mode (car set-auto-mode--last)) (eq major-mode (cdr set-auto-mode--last))))) - (when mode - (funcall (alist-get mode major-mode-remap-alist mode)) - (unless (eq mode major-mode) - (setq set-auto-mode--last (cons mode major-mode))) - mode))) + (when mode + (if (not (functionp modefun)) + (progn + (message "Ignoring unknown mode `%s'" mode) + nil) + (funcall modefun) + (unless (eq mode major-mode) + (setq set-auto-mode--last (cons mode major-mode))) + mode))))) (defvar file-auto-mode-skip "^\\(#!\\|'\\\\\"\\)" "Regexp of lines to skip when looking for file-local settings. --=-=-=--