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#60623: 30.0.50; Add forward-sentence with tree sitter support Date: Sun, 08 Jan 2023 20:35:58 +0100 Message-ID: <875ydgu8dd.fsf@thornhill.no> References: <87o7ratva2.fsf@thornhill.no> <87bkn9tasb.fsf@thornhill.no> <83sfgloz5w.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="32169"; mail-complaints-to="usenet@ciao.gmane.io" Cc: casouri@gmail.com, 60623@debbugs.gnu.org, juri@linkov.net, monnier@iro.umontreal.ca, mardani29@yahoo.es To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jan 08 20:37:23 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 1pEbTt-00088a-OU for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 08 Jan 2023 20:37:21 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pEbTb-00065f-Vf; Sun, 08 Jan 2023 14:37: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 1pEbTa-00064w-EA for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2023 14:37: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 1pEbTa-0002p6-4z for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2023 14:37:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pEbTa-0006Ww-0z for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2023 14:37: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: Sun, 08 Jan 2023 19:37:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60623 X-GNU-PR-Package: emacs X-Debbugs-Original-Cc: casouri@gmail.com, bug-gnu-emacs@gnu.org, 60623@debbugs.gnu.org, juri@linkov.net, monnier@iro.umontreal.ca, mardani29@yahoo.es Original-Received: via spool by 60623-submit@debbugs.gnu.org id=B60623.167320656525015 (code B ref 60623); Sun, 08 Jan 2023 19:37:01 +0000 Original-Received: (at 60623) by debbugs.gnu.org; 8 Jan 2023 19:36:05 +0000 Original-Received: from localhost ([127.0.0.1]:34748 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pEbSf-0006VO-4P for submit@debbugs.gnu.org; Sun, 08 Jan 2023 14:36:05 -0500 Original-Received: from out2.migadu.com ([188.165.223.204]:34110) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pEbSb-0006Uw-O8 for 60623@debbugs.gnu.org; Sun, 08 Jan 2023 14:36:03 -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=1673206560; 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=KxDfZ76RzBpJB3tt7f6cQBnq6R/homj+ghB/KqLofKs=; b=HNWzMTWLcqT+skhy9EEZQ6AYtmIxJL4cyOFTUDig0Cgty+XRndqGpHwmbU1pQeHjNRk7rA NYKFhoKqAiKByAHf2LIW3uMqjk7VoVWu2ykqk+P+9zJL8BCFtRq26KoIo3+5tijc/V/jmK Rt3KGFn1JqxtctAQtrBvHTBa/gQTDOWMez356l2azr6wvCqI/7GECS2llFz0fNCUftcy/0 8YSRsItH+O71FjFyZa04Qlg3QL2s9c41/j1xEzVQqFR+X8477G75Bw2R8cNWnBIl1k1YQF ewgwsLdjG9e0GT939B6LEy13Fm4pZaF06RUGYBxeNxKdzv9xygw6T7W6mPsM/g== In-Reply-To: <83sfgloz5w.fsf@gnu.org> X-Migadu-Flow: FLOW_OUT 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:252956 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Theodor Thornhill >> Cc: 60623@debbugs.gnu.org, casouri@gmail.com, eliz@gnu.org, >> monnier@iro.umontreal.ca,Juri Linkov >> Date: Sun, 08 Jan 2023 14:29:08 +0100 >> >> @Eli, what doc changes do you see as needed here? > > More or less. They need some polishing, like a few words about what > does "sentence" mean in the tree-sitter context. But we can make > these changes after this is in the repository. > > Thanks. Ok, so in other words, this patch is good to go? I omitted the additions to java-ts-mode and c-ts-mode. I can make a separate commit to add some values that makes sense for multiple modes after? Will the changes to the manual lie in "26.2 Sentences"? in the Emacs manual? Theo --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-forward-sentence-with-tree-sitter-support-bug-60.patch >From 558382b5c712431f0a19262c6e1fb67fdedd908b Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Sun, 8 Jan 2023 20:28:02 +0100 Subject: [PATCH] Add forward-sentence with tree sitter support (bug#60623) * etc/NEWS: Mention the new changes. * lisp/textmodes/paragraphs.el (forward-sentence-default-function): Move old implementation to its own function. (forward-sentence-function): New defvar defaulting to old behavior. (forward-sentence): Use the variable in this function unconditionally. * lisp/treesit.el (treesit-sentence-type-regexp): New defvar. (treesit-forward-sentence): New defun. (treesit-major-mode-setup): Conditionally set forward-sentence-function. --- etc/NEWS | 16 ++++++++++++++++ lisp/textmodes/paragraphs.el | 15 +++++++++++++-- lisp/treesit.el | 28 ++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 690e9c3faa9..a0b2de056d8 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -57,6 +57,22 @@ treesit.el now unconditionally sets 'transpose-sexps-function' for all Tree-sitter modes. This functionality utilizes the new 'transpose-sexps-function'. +** New defvar forward-sentence-function. +Emacs now can set this variable to customize the behavior of the +'forward-sentence' function. + +** New defun forward-sentence-default-function. +The previous implementation of 'forward-sentence' is moved into its +own function, to be bound by 'forward-sentence-function'. + +** New defvar-local 'treesit-sentence-type-regexp. +Similarly to 'treesit-defun-type-regexp', this variable is used to +navigate sentences in Tree-sitter enabled modes. + +** New function 'treesit-forward-sentence'. +treesit.el now conditionally sets 'forward-sentence-function' for all +Tree-sitter modes that sets 'treesit-sentence-type-regexp'. + * Changes in Specialized Modes and Packages in Emacs 30.1 --- diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el index 73abb155aaa..fd2d83eeebf 100644 --- a/lisp/textmodes/paragraphs.el +++ b/lisp/textmodes/paragraphs.el @@ -441,13 +441,12 @@ end-of-paragraph-text (if (< (point) (point-max)) (end-of-paragraph-text)))))) -(defun forward-sentence (&optional arg) +(defun forward-sentence-default-function (&optional arg) "Move forward to next end of sentence. With argument, repeat. When ARG is negative, move backward repeatedly to start of sentence. The variable `sentence-end' is a regular expression that matches ends of sentences. Also, every paragraph boundary terminates sentences as well." - (interactive "^p") (or arg (setq arg 1)) (let ((opoint (point)) (sentence-end (sentence-end))) @@ -480,6 +479,18 @@ forward-sentence (let ((npoint (constrain-to-field nil opoint t))) (not (= npoint opoint))))) +(defvar forward-sentence-function #'forward-sentence-default-function + "Function to be used to calculate sentence movements. +See `forward-sentence' for a description of its behavior.") + +(defun forward-sentence (&optional arg) + "Move forward to next end of sentence. With argument, repeat. +When ARG is negative, move backward repeatedly to start of sentence. +Delegates its work to `forward-sentence-function'." + (interactive "^p") + (or arg (setq arg 1)) + (funcall forward-sentence-function arg)) + (defun count-sentences (start end) "Count sentences in current buffer from START to END." (let ((sentences 0) diff --git a/lisp/treesit.el b/lisp/treesit.el index a7f453a8899..0681e758b37 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1783,6 +1783,32 @@ treesit-end-of-defun (when treesit-defun-skipper (funcall treesit-defun-skipper)))) +(defvar-local treesit-sentence-type-regexp "" + "A regexp that matches the node type of sentence nodes. + +A sentence node is a node that is bigger than a sexp, and +delimits larger statements in the source code. It is, however, +smaller in scope than defuns. This is used by +`treesit-forward-sentence' and friends.") + +(defun treesit-forward-sentence (&optional arg) + "Tree-sitter `forward-sentence-function' function. + +ARG is the same as in `forward-sentence-function'. + +If inside comment or other nodes described in +`treesit-sentence-type-regexp', use +`forward-sentence-default-function', else move across nodes as +described by `treesit-sentence-type-regexp'." + + (if (string-match-p + treesit-text-type-regexp + (treesit-node-type (treesit-node-at (point)))) + (funcall #'forward-sentence-default-function arg) + (funcall + (if (> arg 0) #'treesit-end-of-thing #'treesit-beginning-of-thing) + treesit-sentence-type-regexp (abs arg)))) + (defvar-local treesit-text-type-regexp "\\`comment\\'" "A regexp that matches the node type of textual nodes. @@ -2256,6 +2282,8 @@ treesit-major-mode-setup #'treesit-add-log-current-defun)) (setq-local transpose-sexps-function #'treesit-transpose-sexps) + (when treesit-sentence-type-regexp + (setq-local forward-sentence-function #'treesit-forward-sentence)) ;; Imenu. (when treesit-simple-imenu-settings -- 2.34.1 --=-=-=--