Subject: Re: bug#76679: 31.0.50; forward-sentence not working in the narrowed buffer under c-ts-mode

From: Juri Linkov <[email-masked]>
Date: Tue, 11 Mar 2025 19:17:03 +0200

>> However, this report raises a different question: whether forward-sentence
>> should navigate to the end of the narrowed region when the semicolon is hidden?
>
> Nothing else would make sense.  Almost all Emacs commands behave like
> there's nothing beyond the end of the narrowed region.  The few
> exceptions are only in very specialized cases, which this one isn't,
> because it's just a movement command.

Ok, this patch provides the same behavior as for forward-sentence:

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 319dc5a41fc..f94c97c2e7c 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -3417,9 +3417,10 @@ treesit-forward-sentence
 by `text' and `sentence' in `treesit-thing-settings'."
   (if (treesit-node-match-p (treesit-node-at (point)) 'text t)
       (funcall #'forward-sentence-default-function arg)
-    (funcall
-     (if (> arg 0) #'treesit-end-of-thing #'treesit-beginning-of-thing)
-     'sentence (abs arg))))
+    (or (funcall
+         (if (> arg 0) #'treesit-end-of-thing #'treesit-beginning-of-thing)
+         'sentence (abs arg))
+        (goto-char (if (> arg 0) (point-max) (point-min))))))