From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.devel Subject: Extending define-derived-mode Date: Mon, 29 May 2023 22:16:49 -0700 Message-ID: <20F07C52-6B39-4B24-8433-82E2226EADA6@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.500.231\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17280"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Mickey Petersen , Theodor Thornhill , Dmitry Gutov To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue May 30 07:17:50 2023 Return-path: Envelope-to: ged-emacs-devel@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 1q3rjx-0004Gy-Sn for ged-emacs-devel@m.gmane-mx.org; Tue, 30 May 2023 07:17:49 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q3rjI-00030i-QG; Tue, 30 May 2023 01:17:08 -0400 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 1q3rjF-00030W-OP for emacs-devel@gnu.org; Tue, 30 May 2023 01:17:05 -0400 Original-Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q3rjE-0000UX-0z for emacs-devel@gnu.org; Tue, 30 May 2023 01:17:05 -0400 Original-Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-64d2da69fdfso4685443b3a.0 for ; Mon, 29 May 2023 22:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685423822; x=1688015822; h=to:cc:date:message-id:subject:mime-version :content-transfer-encoding:from:from:to:cc:subject:date:message-id :reply-to; bh=g9UE2vhNfUgElIlvypCvtAQeD4He8hCYyoYeoeAnZX0=; b=M5cqRxlWeaQAT0+HICyQFmtWFakOJuPeyQuVlKCAtWQ+5IFFBakGE00VPz4RG2Qo9Q aO4Kvsr0mX+Id4QqpHKQOoEFJZfA7NbNdqsjV2Ae5s3T5wsVviqxl0vbwlJYv6AZCqiv qWAULLUcOyJGlrWv88aFgBu2Mn3f8RsxvzVh2WXXzr4412A962ayCcQECzB1UwfmuGX1 hM+IVNgNfKN9SUIGGhRFWbsIG+F8B8sSJ/YTPNlJuGoSKFCts9ftssduvW41LAxwSQl9 HguTurLioEMHxLOrla5hDeFB2eLHzmlj/vwGIetR9cbjnauEDAb4k9miHsM64IEDfI27 paKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685423822; x=1688015822; h=to:cc:date:message-id:subject:mime-version :content-transfer-encoding:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=g9UE2vhNfUgElIlvypCvtAQeD4He8hCYyoYeoeAnZX0=; b=CTG6/Xeza0HJPmO85uCGiXnY453n7wThayce/r2KE2UrjgZuCsSBJP70DkWxmILHhe efP4akuxxjKO724Y7T1VtlV8mybf4aGxAxsXEfCT/VDGmqRJ7k0Ns/tpB07O2LlHvIly C5VW1pY8zVsb3TKk4JvJi0iFYt2LFA3guo095Iq9Lp6uBw8DpIXup/ulN6l8iys/dC06 7WHR6kecNK7BW9GblQCxeNybctp1X7LoI41Tt8zAt0ukfGrvXNSZt9qgn5mDoLANEEsc 1sbsNGfEjQWZ5QPhD5p4TPuXOBhXpUxo62xmXPUtgNLaA4EilsxmP2aix2wy22+Tk4jc ONNg== X-Gm-Message-State: AC+VfDxvswRsnxJQ3Bz0ueVgAQxYWd18IMnxXEDVhEk0b0cklzLIGuie 6k2zC8+krIhZKGjJ6OOAwE7mnf047rc= X-Google-Smtp-Source: ACHHUZ4zVr105ftrOuVi9R/wyvwoKaOjASZDnnAih9KfETH7XupcXhh77Un8422x0zQ0rPXxM09LhA== X-Received: by 2002:a05:6a00:2351:b0:643:9e7c:3829 with SMTP id j17-20020a056a00235100b006439e7c3829mr1312034pfj.12.1685423822162; Mon, 29 May 2023 22:17:02 -0700 (PDT) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id m26-20020aa78a1a000000b0064d31ddb5e4sm793977pfa.43.2023.05.29.22.17.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 May 2023 22:17:01 -0700 (PDT) X-Mailer: Apple Mail (2.3731.500.231) Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=casouri@gmail.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:306401 Archived-At: When we were adding tree-sitter modes a couple of month ago, it was clear that the current major mode model needs some upgrade, I=E2=80=99d = like to discuss the things we need and how can we address them. Features we want: 1. Fallback modes: user enables xxx-ts-mode, but there=E2=80=99s no tree-sitter grammar for xxx, so Emacs falls back to xxx-mode instead. This feature is also desirable for some non-tree-sitter modes, like TeX modes. Ideally the dispatch should happen before major mode does anything. 2. Inheritance: 2.1 For xxx-mode and xxx-ts-mode, there should be shared hook. More generally, we want to be able to have a shared hook for similar modes (same language, similar language, etc). 2.2 For xxx-mode and xxx-ts-mode, they should be able to share some setup, and diverge on some other. 2.3 Right now we have a little inconsistency regarding hook inheritance. js-json-mode inherits from js-mode, json-ts-mode inherits from js-ts-mode. So there isn=E2=80=99t a shared hook that = runs in both js-json-mode and json-ts-mode. More generally, if there is a language X and a derived language Y, and we have x-mode, x-ts-mode, y-mode, y-ts-mode, how should inheritance of code and hooks works among them? y-mode probably wants to inherit from x-mode, and y-ts-mode probably wants to inherit hook (but not necessarily code [1]) from x-ts-mode. [1] If Y is different enough from X such that it has its own tree-sitter grammar, y-ts-mode cannot inherit x-ts-mode=E2=80=99s code = because they target different grammar. 3. Unrelated to tree-sitter, here=E2=80=99s something I personally want: it would be nice if every major mode can have a hook that=E2=80=99s not run by its derived modes. Use case: sage-mode inherits python-mode. I have eglot-ensure in python-mode-hook but don=E2=80=99t want it when using sage-mode. = Right now I have to wrap eglot-ensure with a lambda function so that it only runs when major-mode =3D python-mode. Implementation wise, here are my thoughts regarding each feature: 1. Shouldn=E2=80=99t be hard to add some facility to define-derived-mode = that runs before everything and dispatches to different major modes. Maybe define-derive-mode can have a :dispatch keyword which takes a arbitrary function; and if the function returns nil, rest of the major mode setup are skipped. 2. For those who don=E2=80=99t know, right now we define a = xxx-base-mode, which is then inherited by xxx-mode and xxx-ts-mode. My understanding is that xxx-base-mode is considered a temporary solution and will be replaced. Personally I don=E2=80=99t mind xxx-base-mode, why can=E2=80=99t we keep it? =46rom what I can see, major mode inheritance is used for three purposes: a) inheriting from a =E2=80=9Cvirtual=E2=80=9D mode (eg, = prog-mode) for hooks and keymaps; b) y-mode deriving from x-mode, where Y is a derived language from X; c) y-2-mode deriving from y-mode as an extension, where y-2-mode can be a user=E2=80=99s personal = customization, or distributed as packages (js2-mode). I think the current model serves a) and c) just fine. But for b), and for when there are multiple major modes for the same language, the single-inheritance model can be a bit lacking. I don=E2=80=99t = have a clear vision right now, perhaps we want multiple inheritance for hooks, perhaps we can simply allow define-derived-mode to specify additional hooks to run, and create some language hooks that are shared by every mode serving that language. I=E2=80=99d like to hear = your thoughts. 3. Like 1., shouldn=E2=80=99t be hard to modify define-derived-mode to = add a hook that=E2=80=99s not run by its derived modes. Yuan