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: Tue, 10 Jan 2023 09:37:26 +0100 Message-ID: <87h6wyss3d.fsf@thornhill.no> References: <87o7ratva2.fsf@thornhill.no> <87bkn9tasb.fsf@thornhill.no> <83sfgloz5w.fsf@gnu.org> <875ydgu8dd.fsf@thornhill.no> <83fsckpznk.fsf@gnu.org> <87358ku6x2.fsf@thornhill.no> <837cxvq3x9.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="16567"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 60623@debbugs.gnu.org, juri@linkov.net, casouri@gmail.com, 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 Tue Jan 10 10:44:22 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 1pFBB7-00048c-Vg for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 10 Jan 2023 10:44:22 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pFA8z-0000nj-Nt; Tue, 10 Jan 2023 03:38:05 -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 1pFA8w-0000mk-RC for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2023 03:38: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 1pFA8w-0000UB-Is for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2023 03:38:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pFA8w-0007IP-C1 for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2023 03:38: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: Tue, 10 Jan 2023 08:38:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60623 X-GNU-PR-Package: emacs Original-Received: via spool by 60623-submit@debbugs.gnu.org id=B60623.167333985428010 (code B ref 60623); Tue, 10 Jan 2023 08:38:02 +0000 Original-Received: (at 60623) by debbugs.gnu.org; 10 Jan 2023 08:37:34 +0000 Original-Received: from localhost ([127.0.0.1]:38744 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pFA8T-0007Hh-IM for submit@debbugs.gnu.org; Tue, 10 Jan 2023 03:37:34 -0500 Original-Received: from out2.migadu.com ([188.165.223.204]:34461) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pFA8Q-0007HY-5K for 60623@debbugs.gnu.org; Tue, 10 Jan 2023 03:37:31 -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=1673339848; 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=EtQKFXSdKUbHgJowjd1QAy1RQDga0KXoFgJGJY7Co3c=; b=MH2HYSTIpbBWufr1JgDlPHUi1IN4xwPhYSiQLKkC5m8E9mHbylq1zqaB5/PGsnX7/Lq1ZD EEVWI+HE2xyKQyebAKXhHOqot/2wU+weu+Hls5naV34yAYKNOy6hM/BF5hxLnfX2QjQW22 I4j8FxoM0FIsX9nZfk29aFjG8uIIGV64hMk8HDH1c4XJFHmDOhHLoGrmb1yyeRtZX07+/m 08ZEUXcLY6RRQ0JgEA1y1D0n8y1mrczTGQMlq8mlVWt0qbJEsMJxabVBh94xb+u1WRnsZB 1LY3fwR8jZTuIqx+wVJwUg2xLuzrhq+exq0J7BMaUSdelb9ew/Iq63q9CSqIAg== In-Reply-To: <837cxvq3x9.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:253064 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Theodor Thornhill >> Cc: mardani29@yahoo.es, 60623@debbugs.gnu.org, casouri@gmail.com, >> monnier@iro.umontreal.ca, juri@linkov.net >> Date: Sun, 08 Jan 2023 21:07:21 +0100 >> >> >> Ok, so in other words, this patch is good to go? >> > >> > Yes, I think so. >> > >> >> Great! >> >> >> 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? >> > >> > SGTM. >> > >> >> Nice. Will you install this for me? > > I'm under the impression that this is still being discussed? > >> >> Will the changes to the manual lie in "26.2 Sentences"? in the Emacs >> >> manual? >> > >> > No, because these are not really sentences in some human-readable >> > language, these are program parts. As such they should be somewhere >> > under "27 Programs", possibly in "Defuns". >> > >> > However, "Sentences" might mention that programming modes have their >> > own interpretation of "sentence" and corresponding movement commands. >> >> Yeah, that makes sense. Should I make an attempt at such formulations, >> or will you do it at a later time? > > It is better that you try, if only to gain experience ;-) How about this for starter? Theo --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-forward-sentence-with-tree-sitter-support-bug-60.patch >From 7b954b4f16d1dc832733932ca58f6e906ef0705d 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. * doc/emacs/programs.texi (Defuns): Add new subsection. (Moving by Sentences): Add some documentation with xrefs to the elisp manual and related nodes. * doc/lispref/positions.texi (List Motion): Mention treesit-sentence-type-regexp and describe how to enable this functionality. --- doc/emacs/programs.texi | 37 ++++++++++++++++++++++++++++++++++++ doc/emacs/text.texi | 8 ++++++++ doc/lispref/positions.texi | 14 ++++++++++++++ etc/NEWS | 16 ++++++++++++++++ lisp/textmodes/paragraphs.el | 15 +++++++++++++-- lisp/treesit.el | 27 ++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 2 deletions(-) diff --git a/doc/emacs/programs.texi b/doc/emacs/programs.texi index 44cad5a148e..f7cdd99fa2b 100644 --- a/doc/emacs/programs.texi +++ b/doc/emacs/programs.texi @@ -163,6 +163,7 @@ Defuns * Left Margin Paren:: An open-paren or similar opening delimiter starts a defun if it is at the left margin. * Moving by Defuns:: Commands to move over or mark a major definition. +* Moving by Sentences:: Commands to move over certain definitions in code. * Imenu:: Making buffer indexes as menus. * Which Function:: Which Function mode shows which function you are in. @end menu @@ -254,6 +255,42 @@ Moving by Defuns language. Other major modes may replace any or all of these key bindings for that purpose. +@node Moving by Sentences +@subsection Moving by Sentences + + These commands move point or set up the region based on definitions, +also called @dfn{sentences}. Even though sentences is usually +considered when writing human languages, Emacs can use the same +commands to move over certain constructs in programming languages +(@pxref{Sentences}, @pxref{Moving by Defuns}). In a programming +language a sentence is usually a complete language construct smaller +than defuns, but larger than sexps (@pxref{List Motion,,, elisp, The +Emacs Lisp Reference Manual}). + +@table @kbd +@item M-a +Move to beginning of current or preceding sentence +(@code{backward-sentence}). +@item M-e +Move to end of current or following sentence (@code{forward-sentence}). +@end table + +@cindex move to beginning or end of sentence +@cindex sentence, move to beginning or end +@kindex M-a +@kindex M-e +@findex backward-sentence +@findex forward-sentence + The commands to move to the beginning and end of the current +sentence are @kbd{M-a} (@code{backward-sentence}) and @kbd{M-e} +(@code{forward-sentence}). If you repeat one of these commands, or +use a positive numeric argument, each repetition moves to the next +sentence in the direction of motion. + + @kbd{M-a} with a negative argument @minus{}@var{n} moves forward +@var{n} times to the next end of a sentence. Likewise, @kbd{M-e} with +a negative argument moves back to a start of a sentence. + @node Imenu @subsection Imenu @cindex index of buffer definitions diff --git a/doc/emacs/text.texi b/doc/emacs/text.texi index 8fbf731a4f7..373582a93a4 100644 --- a/doc/emacs/text.texi +++ b/doc/emacs/text.texi @@ -253,6 +253,14 @@ Sentences of a sentence. Set the variable @code{sentence-end-without-period} to @code{t} in such cases. + Even though the above mentioned sentence movement commands are based +on human languages, other Emacs modes can set these command to get +similar functionality. What exactly a sentence is in a non-human +language is dependent on the target language, but usually it is +complete statements, such as a variable definition and initialization, +or a conditional statement (@pxref{Moving by Sentences,,, emacs, The +extensible self-documenting text editor}). + @node Paragraphs @section Paragraphs @cindex paragraphs diff --git a/doc/lispref/positions.texi b/doc/lispref/positions.texi index f3824436246..639d0d8025e 100644 --- a/doc/lispref/positions.texi +++ b/doc/lispref/positions.texi @@ -858,6 +858,20 @@ List Motion recognize nested defuns. @end defvar +@defvar treesit-sentence-type-regexp +The value of this variable is a regexp matching the node type of sentence +nodes. (For ``node'' and ``node type'', @pxref{Parsing Program Source}.) + +@findex treesit-forward-sentence +@findex forward-sentence +@findex backward-sentence +If Emacs is compiled with tree-sitter, it can use the tree-sitter +parser information to move across syntax constructs. Since what +exactly is considered a sentence varies between languages, a major mode +should set @code{treesit-sentence-type-regexp} to determine that. Then +the mode can get navigation-by-sentence functionality for free, by using +@code{forward-sentence} and @code{backward-sentence}. + @node Skipping Characters @subsection Skipping Characters @cindex skipping characters diff --git a/etc/NEWS b/etc/NEWS index 3aa8f2abb77..af15a9b4545 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -66,6 +66,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..95f0fec739f 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1792,6 +1792,31 @@ treesit-text-type-regexp \"text_block\" in the case of a string. This is used by `prog-fill-reindent-defun' and friends.") +(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)))) + (defun treesit-default-defun-skipper () "Skips spaces after navigating a defun. This function tries to move to the beginning of a line, either by @@ -2256,6 +2281,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 --=-=-=--