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: Fri, 02 Dec 2022 21:24:13 +0100 Message-ID: <87y1rp7dw2.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> 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="40902"; mail-complaints-to="usenet@ciao.gmane.io" Cc: eliz@gnu.org, casouri@gmail.com To: mardani29@yahoo.es, 59662@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Dec 02 21:25:19 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 1p1Cb1-000ASG-Bm for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 02 Dec 2022 21:25:19 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p1Cal-0005AV-K8; Fri, 02 Dec 2022 15:25:03 -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 1p1Cak-0005AF-M6 for bug-gnu-emacs@gnu.org; Fri, 02 Dec 2022 15:25:02 -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 1p1Cak-0002a4-Dg for bug-gnu-emacs@gnu.org; Fri, 02 Dec 2022 15:25:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p1Cak-0000vH-4f for bug-gnu-emacs@gnu.org; Fri, 02 Dec 2022 15:25:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 02 Dec 2022 20:25:02 +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-To: Daniel =?UTF-8?Q?Mart=C3=ADn?= , "Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors" X-Debbugs-Original-Cc: Eli Zaretskii , 59662@debbugs.gnu.org, casouri@gmail.com Original-Received: via spool by submit@debbugs.gnu.org id=B.16700126653533 (code B ref -1); Fri, 02 Dec 2022 20:25:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 2 Dec 2022 20:24:25 +0000 Original-Received: from localhost ([127.0.0.1]:48677 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p1Ca8-0000uv-N1 for submit@debbugs.gnu.org; Fri, 02 Dec 2022 15:24:25 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:32866) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p1Ca6-0000up-EA for submit@debbugs.gnu.org; Fri, 02 Dec 2022 15:24:23 -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 1p1Ca6-00057T-2U for bug-gnu-emacs@gnu.org; Fri, 02 Dec 2022 15:24:22 -0500 Original-Received: from out2.migadu.com ([2001:41d0:2:aacc::]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p1Ca3-00023O-HZ; Fri, 02 Dec 2022 15:24:21 -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=1670012656; 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=kMOeHX45rq0caDfHljenFi9lKVCFk5POLyBFwmpoJwY=; b=jRj+wpzS59q3JY29qe15lUgp4UqyFORw/zkh7O7wLTTPOOiySgAi6AHrYlKC9jlbMDKq12 FHh/WzQmVTngTJzpjYcfmuTgnRIji0GogFPjBK/PXuAPcCYBbzZC387SNqZrySFJvwDOfE rKlfnG7Aa55UxGqe/PmTHgx9ttt/HsFrR+Qmyv4vBUTiPCnQWLF0nZ+KxyiXs9k6xSQj7b MLAgOtFRrAPJjX4Tf4ayVjiDe2NcPj0nNWzZ6WKXh4WN42Q7SQPlRantU/7VpTdpEis0Qe drLz+JT/Qexrt6mjvWemci24dbm6wnjJH7PTxMhgKaI1wv0wJTVGbCkbUjYlBg== In-Reply-To: X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=2001:41d0:2:aacc::; envelope-from=theo@thornhill.no; helo=out2.migadu.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-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:249782 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Daniel Mart=C3=ADn writes: > Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife > of text editors" writes: > >>> >>> Why in lisp.el? we are talking about a feature of C-like languages. The >>> current binding of "C-c C-q" is in cc-cmds.el, so what I had in mind is= to >>> have its counterpart in c-ts-mode.el. >> >> Something like this? > > Thanks, some comments below: > Thanks for looking! >> >> From 26a8780950017a911bac7290366da05e0e35f13f 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. >> --- >> lisp/progmodes/c-ts-mode.el | 18 +++++++++++++++++- >> 1 file changed, 17 insertions(+), 1 deletion(-) >> >> diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el >> index fcabb5beac..3171600901 100644 >> --- a/lisp/progmodes/c-ts-mode.el >> +++ b/lisp/progmodes/c-ts-mode.el >> @@ -518,9 +518,25 @@ c-ts-mode--end-of-defun >> (if (looking-at "\\s<\\|\n") >> (forward-line 1))))) >>=20=20 >> +(defun c-ts-mode--indent-defun () > > This should be public: c-ts-mode-indent-defun > Yeah. >> + "Indent the current top-level declaration syntactically. >> + >> +`treesit-defun-type-regexp' defines what constructs to indent." >> + (interactive) > > (interactive "*") will handle read-only buffers a bit better. > I was wondering about that, but I couldn't really find any situation that gave any difference. But I've added it now. >> + (save-excursion >> + (mark-defun) >> + (indent-region (region-beginning) (region-end)))) > > Nit: Once you call (mark-defun), you can call > > (indent-region (point) (mark)) > > which should be more efficient than calling (region-beginning) or > (region-end). 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. Thanks, Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-c-ts-mode-indent-defun.patch >From 9c22569f7a98c9fd671664e0834bd079e8f20bd3 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. --- lisp/progmodes/c-ts-mode.el | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index fcabb5beac..677ff07a2d 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -518,9 +518,25 @@ 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 "*") + (save-excursion + (mark-defun) + (indent-region (point) (mark)))) + +(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 --=-=-=--