From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#62204: 30.0.50; Feature Request: treesit-major-mode-hook Date: Thu, 16 Mar 2023 08:38:34 +0200 Message-ID: <83fsa5qjyt.fsf@gnu.org> References: <86cz5adyi9.fsf@aleks.bg> <83wn3iqehe.fsf@gnu.org> <86pm99y1av.fsf@aleks.bg> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27911"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 62204@debbugs.gnu.org To: Aleksandar Dimitrov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Mar 16 07:39:20 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 1pchGi-00073q-Lw for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 16 Mar 2023 07:39:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pchGT-0003w3-C7; Thu, 16 Mar 2023 02:39:05 -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 1pchGQ-0003v1-MB for bug-gnu-emacs@gnu.org; Thu, 16 Mar 2023 02:39:03 -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 1pchGQ-0008Cy-DX for bug-gnu-emacs@gnu.org; Thu, 16 Mar 2023 02:39:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pchGP-0000iG-PA for bug-gnu-emacs@gnu.org; Thu, 16 Mar 2023 02:39:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 16 Mar 2023 06:39: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.16789487282719 (code B ref 62204); Thu, 16 Mar 2023 06:39:01 +0000 Original-Received: (at 62204) by debbugs.gnu.org; 16 Mar 2023 06:38:48 +0000 Original-Received: from localhost ([127.0.0.1]:41064 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pchGB-0000hn-SL for submit@debbugs.gnu.org; Thu, 16 Mar 2023 02:38:48 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:34056) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pchGA-0000hZ-UW for 62204@debbugs.gnu.org; Thu, 16 Mar 2023 02:38:47 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pchG4-00089q-Vk; Thu, 16 Mar 2023 02:38:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=RjSNMtthEHiopC8f19h0/3jl0EBU3SUjJnPG8e4DW/k=; b=kw42SogjkDAD KvYMvIZJSPu4B/5brANdX9B4knYeykN2/H9FE8bXHGQTiLe5YtB0yzQccww1/6u/fj9pBLgGwTsX5 Hf4d0V7pTrGii0chNfstgWPEF6/SDekWGV6jXfOaQH2jx77WCA/kCT+Lx5jDHC/lCwT1qTkAx5OJT qvccWI0BfXySgJ5qpoVZ/Rmw8ZAcBvXnuWjwoEvS6qGu4DMBzr6d/cElmf1QIjUymVcv5lXbrniOL ZtRfpKatFOTDjU/QbJVk9+nkuNWmVFa9JjTfCp2DiAgW1XFK24dm6BwagZ6v1pfllTn4qySQqtNoN mXF4u40aiKBTtU8UjxegJw==; Original-Received: from [87.69.77.57] (helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pchG1-0001Dz-39; Thu, 16 Mar 2023 02:38:38 -0400 In-Reply-To: <86pm99y1av.fsf@aleks.bg> (message from Aleksandar Dimitrov on Thu, 16 Mar 2023 01:35:17 +0100) 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:258005 Archived-At: > From: Aleksandar Dimitrov > Cc: 62204@debbugs.gnu.org > Date: Thu, 16 Mar 2023 01:35:17 +0100 > > 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))))) Something like that. Basically, any function that wants to do something that depends on tree-sitter being available for the major mode should make such a test to determine whether tree-sitter support is available. > 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. The above code doesn't have to run from a mode hook, it could be done directly where the "certain behavior" is implemented, as the condition for invoking that certain behavior. And if running the test each time is too expensive for some reason, the test could be optimized by performing it just once for each buffer where it runs, and saving the result in some buffer-local variable. If the above is for some reason unworkable or otherwise problematic, please tell why. > > 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. They are, but they are not the only such place. Many features in Emacs use buffer-local variables and keybindings without a special hook. Please also keep in mind that proliferation of general-purpose hooks is not without disadvantages. For starters, a hook disconnects the cause from the effect, and makes it harder to track the control flow and thus harder to understand how a given Lisp program works.