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#68246: 30.0.50; Add non-TS mode as extra parent of TS modes Date: Wed, 17 Jan 2024 12:08:02 -0500 Message-ID: References: <831qavvcbo.fsf@gnu.org> <83a5phskd5.fsf@gnu.org> <83h6joqz0t.fsf@gnu.org> <834jfoq86m.fsf@gnu.org> <831qarrbjx.fsf@gnu.org> <87a5p84nlh.fsf@gmail.com> <83edekfldq.fsf@gnu.org> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29118"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 68246@debbugs.gnu.org, Eli Zaretskii , casouri@gmail.com, =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= , Stefan Kangas To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jan 17 18:10:24 2024 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 1rQ9Ql-0007Hy-GQ for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 17 Jan 2024 18:10:23 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rQ9QX-0003fI-Pk; Wed, 17 Jan 2024 12:10:09 -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 1rQ9QP-0003dN-Lo for bug-gnu-emacs@gnu.org; Wed, 17 Jan 2024 12:10:02 -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 1rQ9QP-0007d7-DD for bug-gnu-emacs@gnu.org; Wed, 17 Jan 2024 12:10:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rQ9QQ-0001Jl-Fq for bug-gnu-emacs@gnu.org; Wed, 17 Jan 2024 12:10:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 17 Jan 2024 17:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68246 X-GNU-PR-Package: emacs Original-Received: via spool by 68246-submit@debbugs.gnu.org id=B68246.17055113534988 (code B ref 68246); Wed, 17 Jan 2024 17:10:02 +0000 Original-Received: (at 68246) by debbugs.gnu.org; 17 Jan 2024 17:09:13 +0000 Original-Received: from localhost ([127.0.0.1]:53198 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rQ9Pd-0001IN-91 for submit@debbugs.gnu.org; Wed, 17 Jan 2024 12:09:13 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:23501) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rQ9Pb-0001I9-N2 for 68246@debbugs.gnu.org; Wed, 17 Jan 2024 12:09:12 -0500 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 9123C100068; Wed, 17 Jan 2024 12:09:04 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1705511343; bh=7xgEJiqn57WDdYqS6U/PVtuahchK70mP+d5+bwur2fY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=DoltqL9SCro1eepFzl4oelVGtkV92apGZEhBiIGcl1k/JO/LVvFVsVXfu0Ri9NZyf 9cT7naK0M/MG4ePwXKAJL6hqKU4xSAQaoHq3hy/3jMr3lznSgBEoH/fqueOFhX+We1 rZ9oTJHiwnLsKx8AvBni03NaiMcjSpMlz4co963Lomv0hXHcjxXZENxyAgpjT+74cP 2pJyQakIXn7t3UUTOSpyRaT+71wz442sQhprsP6dUKFKicsOSR3KbcBQfhzQfzq9IO pA7+ZSiF2zbVr9Rv030kK8TaxbDTeKr2gaZGYPCFRmNB5oD8sblGUBuoQJ80v+W5i+ McNF5NwfhPsJw== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 6BB1110001D; Wed, 17 Jan 2024 12:09:03 -0500 (EST) Original-Received: from lechazo (lechon.iro.umontreal.ca [132.204.27.242]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 589561209EF; Wed, 17 Jan 2024 12:09:03 -0500 (EST) In-Reply-To: (Dmitry Gutov's message of "Mon, 15 Jan 2024 04:10:16 +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:278396 Archived-At: > @@ -3150,6 +3150,9 @@ auto-mode-alist > Visiting a file whose name matches REGEXP specifies FUNCTION as the > mode function to use. FUNCTION will be called, unless it is nil. > > +FUNCTION can also be a keyword denoting a language, to be looked > +up in `major-mode-remap-alist'. Side note: the intention is OK but `major-mode-remap-alist' is a defcustom and should remain nil by default. It's there for the users to express which major modes they prefer. So if we want a mapping between some new language/type concept and major modes, it should be stored elsewhere (could be a plain alist that's handled as a kind of "implicit tail of `major-mode-remap-alist`"). > @@ -3206,10 +3209,10 @@ interpreter-mode-alist > ("emacs" . emacs-lisp-mode))) > "Alist mapping interpreter names to major modes. > This is used for files whose first lines match `auto-mode-interpreter-regexp'. > -Each element looks like (REGEXP . MODE). > +Each element looks like (REGEXP . MODE-OR-LANGUAGE). > If REGEXP matches the entire name (minus any directory part) of > the interpreter specified in the first line of a script, enable > -major mode MODE. > +MODE-OR-LANGUAGE. There's a similar need for "content type" rather than "language". If we want to mention "language" we should also take the opportunity to mention other related categorizations like "content type". > - (funcall (alist-get mode major-mode-remap-alist mode)) > + ;; XXX: When there's no mapping for `:', we could also > + ;; look for a function called `-mode'. > + (funcall (alist-get mode major-mode-remap-alist (if (keywordp mode) > + #'fundamental-mode > + mode))) > + (when (keywordp mode) ;Perhaps do that unconditionally. > + (run-hooks (intern (format "%s-language-hook" (buffer-language))))) That seems wrong: - Why should this hook run when `auto-mode-alist` says `:js` but not when doing `M-x javascript-mode` (or other ways to enable this mode)? - Why run this hook *after* the mode's `:after-hook` and after things like `after-change-major-mode-hook`? I think it should remain the major mode's responsibility to decide which hooks it runs. > +(defun buffer-language () > + "Return the language of the current buffer. > +A language is a lowercase keyword with the name of the language." > + ;; Alternatively, we could go through all the matchers in > + ;; auto-mode-alist, interpreter-mode-alist, > + ;; magic-fallback-mode-alist here, possibly using a cache keyed on > + ;; buffer-file-name. But that's probably an overkill: if the user > + ;; changes the settings, they can call `M-x revert-buffer' at the end. > + (if (keywordp (car set-auto-mode--last)) > + (car set-auto-mode--last) > + ;; Backward compatibility. > + (intern (format ":%s" (replace-regexp-in-string "\\(?:-ts\\)?-mode\\'" "" > + (symbol-name major-mode)))))) I'm not comfortable enshrining the "-ts-mode" convention here. Also I think if we want a `buffer-language` function, it should not rely on how the mode was installed (e.g. `set-auto-mode--last`) but only on the major mode itself, i.e. something like (defun buffer-language () (or buffer-language (some heuristic based on major-mode and/or derived-modes))) [ Of course, I already mentioned that I also suspect that there can/will be sometimes several languages (or none). ] > +(defun set-buffer-language (language) > + "Set the language of the current buffer. > +And switch the major mode appropriately." > + (interactive > + (list (let* ((ct (mapcan > + (lambda (pair) (and (keywordp (car pair)) > + (list (symbol-name (car pair))))) > + major-mode-remap-alist)) > + (lang (completing-read "Language: " ct))) > + (and lang (intern lang))))) > + (set-auto-mode-0 language)) I see several issues with this function (name and implementation), but I wonder when we'd ever need such a thing. > ;;;###autoload > (dolist (name (list "node" "nodejs" "gjs" "rhino")) > - (add-to-list 'interpreter-mode-alist (cons (purecopy name) 'js-mode))) > + (add-to-list 'interpreter-mode-alist (cons (purecopy name) :js))) BTW, my suggested patch basically proposes to use `-mode` instead of `:LANG>` which saves us from those changes since that matches our historical conventions. Another issue I see if we don't use something like `derived-mode-add-parents` is that all the various places where we use mode-indexing, such as `.dir-locals.el`, `ffap`, YASnippet, etc... will need to be extended with a way to use "languages" as well, and then we also need to define a sane precedence between settings that apply to a given mode and settings that apply to a given language (setting for `js-ts-mode` should presumably take precedence over settings for `:js` which should take precedence over settings for `prog-mode`). Stefan