Alexander Adolf <[email protected]> writes:

Hello Alex, all!

>> [...]
>> +(defun org--bounds-of-sentence-at-point ()
>> +  "`bounds-of-thing-at-point' provider function for sentences."
>> +  (save-excursion
>> +    (let* ((orig (point))
>> +           (sentence-end-double-space t)
>> [...]
> Hard-wiring 'sentence-end-double-space' might not be the ideal solution
> for everyone?

Whooops, I accidentally left that in from testing. Here's the patch with
it properly moved to the testing section, I hope. Sorry about the hiccup!

>From ba5d8f8ac8a5ce32db9532f6e2638ad39328b148 Mon Sep 17 00:00:00 2001
From: Sacha Chua <[email protected]>
Date: Wed, 11 Mar 2026 14:32:20 -0400
Subject: [PATCH] org.el: Handle sentence boundaries for thing-at-point

* lisp/org.el (org-mode): Handle sentence boundaries.
(org--bounds-of-sentence-at-point): Handle sentence boundaries.
* testing/lisp/test-org.el (test-org/thing-at-point/sentence): New test.
---
 lisp/org.el              | 24 ++++++++++++++++++++--
 testing/lisp/test-org.el | 43 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index fc51d4ba3..fc9897cc9 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5273,8 +5273,10 @@ define-derived-mode org-mode
                       forward-thing-provider-alist)))
   (when (boundp 'bounds-of-thing-at-point-provider-alist)
     (setq-local bounds-of-thing-at-point-provider-alist
-                (cons '(url . org--bounds-of-link-at-point)
-                      bounds-of-thing-at-point-provider-alist)))
+                (append
+                 '((url . org--bounds-of-link-at-point)
+                   (sentence . org--bounds-of-sentence-at-point))
+                 bounds-of-thing-at-point-provider-alist)))
 
   ;; If empty file that did not turn on Org mode automatically, make
   ;; it to.
@@ -9031,6 +9033,24 @@ defun org--bounds-of-link-at-point
       (cons (org-element-begin context)
             (org-element-end context)))))
 
+(defun org--bounds-of-sentence-at-point ()
+  "`bounds-of-thing-at-point' provider function for sentences."
+  (save-excursion
+    (let* ((orig (point))
+           (end (org-forward-sentence 1))
+           (beg (org-backward-sentence 1)))
+      (if (and (< beg end)
+               (<= orig end)
+               (<= beg orig))
+          (cons beg end)
+        (goto-char orig)
+        (setq beg (org-backward-sentence 1))
+        (setq end (org-forward-sentence 1))
+        (when (and (< beg end)
+                   (<= orig end)
+                   (<= beg orig))
+          (cons beg end))))))
+
 ;;; File search
 
 (defun org-do-occur (regexp &optional cleanup)
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 51265d255..7b9e4cb86 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -3749,6 +3749,49 @@ defmacro org-test-without-dow
       (should (equal (bounds-of-thing-at-point 'url)
                      '(1 . 51))))))
 
+(ert-deftest test-org/thing-at-point/sentence ()
+  "Test that `thing-at-point' returns the sentence at point."
+  (org-test-with-temp-text
+   "This sentence is before the list.  There's one here.  This is a test:
+
+- Is this the first sentence?  There is another.
+- This is the third sentence.
+- Some
+- are
+- over several lines!
+
+The end."
+   (when (boundp 'bounds-of-thing-at-point-provider-alist)
+     (let ((sentence-end-double-space t))
+       (goto-char (point-min))
+       (should (string= (thing-at-point 'sentence)
+                        "This sentence is before the list."))
+       (re-search-forward "one here\\.")
+       (should (string= (thing-at-point 'sentence)
+                        "There's one here."))
+       (re-search-forward "test")
+       (should (string= (thing-at-point 'sentence)
+                        "This is a test:"))
+       (re-search-forward "first")
+       (should (string= (thing-at-point 'sentence)
+                        "Is this the first sentence?"))
+       (re-search-forward "another")
+       (should (string= (thing-at-point 'sentence)
+                        "There is another."))
+       (re-search-forward "Some")
+       (should (string= (thing-at-point 'sentence)
+                        "Some\n- are\n- over several lines!")))))
+  (org-test-with-temp-text
+   "Also works:
+
+- Sentences with single spaces. Like this one."
+   (when (boundp 'bounds-of-thing-at-point-provider-alist)
+     (let ((sentence-end-double-space nil))
+       (goto-char (point-min))
+       (re-search-forward "single")
+       (should (string= (thing-at-point 'sentence)
+                        "Sentences with single spaces."))))))
+
 
 ;;; Node Properties
 
-- 
2.43.0

Sacha

Reply via email to