From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun Date: Wed, 07 Dec 2022 10:31:13 +0100 Message-ID: <87pmcvzhjy.fsf@thornhill.no> References: <5B538888-E454-4F75-B3B8-AB20E10B3E89@gmail.com> <83o7sniokt.fsf@gnu.org> <56840149-5EB9-4C22-841F-45F410BFC885@thornhill.no> <83edtjimyo.fsf@gnu.org> <875yet975l.fsf@thornhill.no> <87y1rp7dw2.fsf@thornhill.no> <87o7sjcpr4.fsf@thornhill.no> <83359vbavi.fsf@gnu.org> Reply-To: Theodor Thornhill 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="17599"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 59662@debbugs.gnu.org, casouri@gmail.com, mardani29@yahoo.es To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Dec 07 10:32:20 2022 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 1p2qmp-0004OI-Uk for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 07 Dec 2022 10:32:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p2qmj-0007vV-Ie; Wed, 07 Dec 2022 04:32:14 -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 1p2qmY-0007sV-Hl for bug-gnu-emacs@gnu.org; Wed, 07 Dec 2022 04:32:05 -0500 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 1p2qmY-0007JS-65 for bug-gnu-emacs@gnu.org; Wed, 07 Dec 2022 04:32:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p2qmX-0001U7-PS for bug-gnu-emacs@gnu.org; Wed, 07 Dec 2022 04:32:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 07 Dec 2022 09:32:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59662 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-Cc: bug-gnu-emacs@gnu.org, 59662@debbugs.gnu.org, casouri@gmail.com, mardani29@yahoo.es Original-Received: via spool by submit@debbugs.gnu.org id=B.16704054855691 (code B ref -1); Wed, 07 Dec 2022 09:32:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 7 Dec 2022 09:31:25 +0000 Original-Received: from localhost ([127.0.0.1]:48816 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p2qlw-0001Tj-VB for submit@debbugs.gnu.org; Wed, 07 Dec 2022 04:31:25 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:51750) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p2qlt-0001Tc-0Q for submit@debbugs.gnu.org; Wed, 07 Dec 2022 04:31:21 -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 1p2qls-0007XV-RA for bug-gnu-emacs@gnu.org; Wed, 07 Dec 2022 04:31:20 -0500 Original-Received: from out-233.mta0.migadu.com ([2001:41d0:1004:224b::e9]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p2qlq-000753-8q for bug-gnu-emacs@gnu.org; Wed, 07 Dec 2022 04:31:20 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1670405475; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=kp0W5L+/5GKMJUYpjk4zpiNaosZUwSHlr5CwSI5nOu8=; b=GqplYhc2G4OT49o5wv6XgU8wxK0c13if2rDvWrXjQpAVtjvVik9v9kr/vbp9MKmeh93x6B 9u5zY6A0IZkJ981Yh70R02AcWZYAtVtSkjVglO7g5lc3vxihce1glkO4Bv9CEc6LknBO5q f961MXwWY8z3rNgX5g9IMxtI9upNijR9XHbH/ND7uOBE1PspiAp+KT0Z4/Eq0mVxkNDQyb Xj7GqLt7kLp4ZfytHC3hkTAlgJeZx/qbIT9PY3R/y7iJCBUSdFJvwTQmRgMizkiF8ehuls SstNNt+6uYrc8nR/Izzoj6otTN7CRTGUJduTf4ZhR5+d7GQng7lA9CNqOmTZwQ== In-Reply-To: <83359vbavi.fsf@gnu.org> X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=2001:41d0:1004:224b::e9; envelope-from=theo@thornhill.no; helo=out-233.mta0.migadu.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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:250170 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Eli Zaretskii writes: >> From: Theodor Thornhill >> Cc: "Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army kni= fe >> of text editors" , Eli Zaretskii , >> casouri@gmail.com, 59662@debbugs.gnu.org >> Date: Sun, 04 Dec 2022 13:33:35 +0100 >>=20 >> Theodor Thornhill writes: >>=20 >> > On 2 December 2022 22:09:55 CET, "Daniel Mart=C3=ADn" wrote: >> >>Theodor Thornhill writes: >> >> >> >>> Right - thanks. However, this makes me wonder - should we really be >> >>> setting mark here? I see that c-indent-defun does not, and it feels >> >>> weird that indenting adds to the mark ring. What do you think? My >> >>> first patch used >> >>> >> >>> (indent-region >> >>> (treesit-node-start node) >> >>> (treesit-node-end node)) >> >>> >> >>> Which behaves similarly to c-indent-defun. >> >>> >> >>> See attached patch. >> >> >> >>I don't have a strong opinion, but if we can indent without setting the >> >>mark, I think it'd be a cleaner command. Specially if c-indent-defun >> >>doesn't set the mark either. >> > >> > Yeah, we don't need to set the mark. Thanks for the feedback :) >> > Theo >>=20 >> Eli, do you have any opinion on this matter? Should we implement this >> function without using marks? That will make the function a smidge >> bigger, but would not mess up the mark-ring. I think that's desirable, >> at least. > > IMO, it is better not to set the mark, indeed. Agreed, see attached patch. What do you think, Yuan and Eli? Should we expose the internal helper I used, or maybe this function could just as well live in treesit.c? Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-c-ts-mode-indent-defun.patch >From 7638a3b0c4c15800f70d0d7e1f444d7512e8994f Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Fri, 2 Dec 2022 16:05:35 +0100 Subject: [PATCH] Add c-ts-mode-indent-defun Add in this function to mimic 'c-indent-defun' * lisp/progmodes/c-ts-mode.el (c-ts-mode-indent-defun): New function. (c-ts-mode-map): New mode map that uses said function. (bug#59662) --- lisp/progmodes/c-ts-mode.el | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 0c66b4959e..998e988901 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -517,9 +517,30 @@ c-ts-mode--end-of-defun (if (looking-at "\\s<\\|\n") (forward-line 1))))) +(defun c-ts-mode-indent-defun () + "Indent the current top-level declaration syntactically. + +`treesit-defun-type-regexp' defines what constructs to indent." + (interactive "*") + (when-let ((node (treesit-search-forward + (treesit-node-at (point)) + treesit-defun-type-regexp t t))) + ;; Use internal function to make sure we get the top-level node. + (setq node (treesit--defun-maybe-top-level node)) + (indent-region + (treesit-node-start node) + (treesit-node-end node)))) + +(defvar-keymap c-ts-mode-map + :doc "Keymap for the C language with tree-sitter" + :parent prog-mode-map + "C-c C-q" #'c-ts-mode-indent-defun) + ;;;###autoload (define-derived-mode c-ts-base-mode prog-mode "C" - "Major mode for editing C, powered by tree-sitter." + "Major mode for editing C, powered by tree-sitter. + +\\{c-ts-mode-map}" :syntax-table c-ts-mode--syntax-table ;; Navigation. -- 2.34.1 --=-=-=--