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.bugs Subject: bug#62204: 30.0.50; Feature Request: treesit-major-mode-hook Date: Sat, 18 Mar 2023 00:49:02 -0700 Message-ID: References: <86cz5adyi9.fsf@aleks.bg> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.400.51.1.1\)) 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="7016"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 62204@debbugs.gnu.org, Eli Zaretskii To: code@aleks.bg Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Mar 18 08:50:13 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 1pdRKO-0001f1-4P for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Mar 2023 08:50:12 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pdRKG-0006La-0a; Sat, 18 Mar 2023 03:50:04 -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 1pdRKE-0006LB-6q for bug-gnu-emacs@gnu.org; Sat, 18 Mar 2023 03:50:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pdRKD-00031m-T7 for bug-gnu-emacs@gnu.org; Sat, 18 Mar 2023 03:50:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pdRKD-0003r9-Lm for bug-gnu-emacs@gnu.org; Sat, 18 Mar 2023 03:50:01 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: <86cz5adyi9.fsf@aleks.bg> Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 18 Mar 2023 07:50:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 62204 X-GNU-PR-Package: emacs Original-Received: via spool by 62204-submit@debbugs.gnu.org id=B62204.167912576214772 (code B ref 62204); Sat, 18 Mar 2023 07:50:01 +0000 Original-Received: (at 62204) by debbugs.gnu.org; 18 Mar 2023 07:49:22 +0000 Original-Received: from localhost ([127.0.0.1]:46111 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pdRJa-0003qC-Bt for submit@debbugs.gnu.org; Sat, 18 Mar 2023 03:49:22 -0400 Original-Received: from mail-pf1-f175.google.com ([209.85.210.175]:37410) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pdRJY-0003pu-HS for 62204@debbugs.gnu.org; Sat, 18 Mar 2023 03:49:21 -0400 Original-Received: by mail-pf1-f175.google.com with SMTP id z11so4334297pfh.4 for <62204@debbugs.gnu.org>; Sat, 18 Mar 2023 00:49:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679125754; h=to:cc:date:message-id:subject:mime-version :content-transfer-encoding:from:from:to:cc:subject:date:message-id :reply-to; bh=iJxInpkBTWRifh65p/VcXSLv1Nj/mhjQImEcSndzWH4=; b=cAj9FDT+Il17XJYSUjsE2nDFs9glbs1XlxSbL97iNeh0U+y9MtazT0OpLeV5G9W8vf b2oPLjer0wqE2REmjE3bqP97/NzaB60jevK7lQvm2mp6v7ov5tc5Z7obvMsoHWB2Jg0R xUnPfl0Ww5bGdko5XlFmuebZuVzc4IkSprST98ptSghMDfGrfD5tce7ymxS18vvEv3e/ RnGpDuqMGjOCNEeTzA15XC6tL2IXkzaL62A5ia+c5sjoEJVkjvMYOFdaOzSYCWVCfNuR GLzS9sEuAEQU14DRwQNmYSuFT5RgVbfo4N4az6/B9Zmpty2THBYuOvL1jTYjAcyiLCd1 dOeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679125754; 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=iJxInpkBTWRifh65p/VcXSLv1Nj/mhjQImEcSndzWH4=; b=BZzYBNl4E35ZlwspS+chlvZQyCXhUOLT0FeS+HVgpoU7P6jWbMSQxLt6YqSul7mOqa oG/KsRfJQafsp2g3qNTQMxgkDWUye1Fr05SItSDkzJq0Mh9rU89j7IAsh+9dXeeulpP2 JgXiyxwd0B7Tv5Lv2ysxPMVGjXCEjvVZgJaWBhM8VPgDeuOimZIDwudhA3dsPsOXik6Y cqy5FoIdm2r04bO4BKTb24cg7NGC1Vy8NK6VY9qGwQMAoj4S15pFtO2RLNBnFCr1Ovf0 jLWnPVca00CQRMWFuo/1iRY+py9puSybETJRykBdgphPtWzRl8ceJqM49s3XxU/gHMcB SHYg== X-Gm-Message-State: AO0yUKUYLRrGSe6n7JNJUkA/5w+vwp+WUCOgzrZh6TEBDOoIWv6/hyfK 8UMm95QvZOerJ9z64EFhZoyU2h25M+ZjVw== X-Google-Smtp-Source: AK7set8gsxF8IZMxbaSJxmJ3fBE3ZQis6LXyxZGJ4uwmY1cWjUKrNK1A71DvGBjxDhTHAs09zUIZhQ== X-Received: by 2002:a62:4e03:0:b0:5a8:aca5:817d with SMTP id c3-20020a624e03000000b005a8aca5817dmr8689908pfb.5.1679125754572; Sat, 18 Mar 2023 00:49:14 -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 j6-20020a62e906000000b00625e885a6ffsm2757135pfh.18.2023.03.18.00.49.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 18 Mar 2023 00:49:14 -0700 (PDT) X-Mailer: Apple Mail (2.3731.400.51.1.1) 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:258137 Archived-At: Aleksandar Dimitrov writes: >>> Currently, I've found two ways to accomplish loading my = functionality for all ts-modes: >>>=20 >>> - enumerate them all and use their respective hooks >>> - advise something like `treesit-major-mode-setup` to execute my = code >> >> Isn't it enough to check that the buffer has a treesit parser? > > I'm not sure I understand you, so I'll try to provide some code. > > I'd like to be able to do something like this: > > (defun my-setup () > "Code that depends on the presence of TS") > (add-hook 'treesit-major-mode-hook 'my-setup) > > If I understand you correctly, I could probably do something like = this: > > (defmacro add-ts-mode-hook (f) > "Add mode hook that only executes in ts modes" > `(add-hook 'prog-mode-hook > (lambda () > (when (treesit-language-at (point)) > (,f))))) > > I'd say there's bound to be more people who would like to configure a > certain behaviour whenever treesit is available, regardless of major > mode. A macro like the above could be a possible solution, but it > doesn't feel terribly ergonomic. > >> A hooks sounds too blunt and ad-hoc for your purposes, AFAIU. > > The reason I want to execute my function in a hook is that it sets > buffer local variables, and configures buffer-local behaviour, perhaps > even keybindings. I was under the impression that hooks are the = correct > place to do this. IIUC, you are trying to do something like (er/enable-mode-expansions 'clojure-mode = 'er/add-clojure-mode-expansions) right? But a tree-sitter-based expander doesn=E2=80=99t really depend on any = particular major mode. In essence, they depend on the existence of a tree-sitter parser in the current buffer. So I suggest that you define a universal expander (similar to er/expand-word, etc) that checks the existence of a tree-sitter parser and uses the parser to expand the region, and simply do nothing if there isn=E2=80=99t a parser. But to work with tree-sitter, expand-region might need to disable some of its expanders when a tree-sitter parser is available, in case = there=E2=80=99s some conflict between the existing language-specific expander and the tree-sitter expander. Yuan