Browse Source

Fix `org-forward-sentence' on a headline

* lisp/org.el (org-forward-sentence): Fix `org-forward-sentence' on a headline.

* testing/lisp/test-org.el (test-org/forward-sentence): Add tests.

Reported-by: Mat Vibrys <vibrysec@gmail.com>
<http://lists.gnu.org/archive/html/emacs-orgmode/2017-09/msg00282.html>
Nicolas Goaziou 7 years ago
parent
commit
95271315cc
2 changed files with 33 additions and 16 deletions
  1. 21 16
      lisp/org.el
  2. 12 0
      testing/lisp/test-org.el

+ 21 - 16
lisp/org.el

@@ -23593,23 +23593,28 @@ depending on context."
 This will call `forward-sentence' or `org-table-end-of-field',
 depending on context."
   (interactive)
-  (let* ((element (org-element-at-point))
-	 (contents-end (org-element-property :contents-end element))
-	 (table (org-element-lineage element '(table) t)))
-    (if (and table
-	     (>= (point) (org-element-property :contents-begin table))
-	     (< (point) contents-end))
-	(call-interactively #'org-table-end-of-field)
+  (if (and (org-at-heading-p)
+	   (save-restriction (skip-chars-forward " \t") (not (eolp))))
       (save-restriction
-	(when (and contents-end
-		   (> (point-max) contents-end)
-		   ;; Skip blank lines between elements.
-		   (< (org-element-property :end element)
-		      (save-excursion (goto-char contents-end)
-				      (skip-chars-forward " \r\t\n"))))
-	  (narrow-to-region (org-element-property :contents-begin element)
-			    contents-end))
-	(call-interactively #'forward-sentence)))))
+	(narrow-to-region (line-beginning-position) (line-end-position))
+	(call-interactively #'forward-sentence))
+    (let* ((element (org-element-at-point))
+	   (contents-end (org-element-property :contents-end element))
+	   (table (org-element-lineage element '(table) t)))
+      (if (and table
+	       (>= (point) (org-element-property :contents-begin table))
+	       (< (point) contents-end))
+	  (call-interactively #'org-table-end-of-field)
+	(save-restriction
+	  (when (and contents-end
+		     (> (point-max) contents-end)
+		     ;; Skip blank lines between elements.
+		     (< (org-element-property :end element)
+			(save-excursion (goto-char contents-end)
+					(skip-chars-forward " \r\t\n"))))
+	    (narrow-to-region (org-element-property :contents-begin element)
+			      contents-end))
+	  (call-interactively #'forward-sentence))))))
 
 (define-key org-mode-map "\M-a" 'org-backward-sentence)
 (define-key org-mode-map "\M-e" 'org-forward-sentence)

+ 12 - 0
testing/lisp/test-org.el

@@ -3412,6 +3412,18 @@ SCHEDULED: <2017-05-06 Sat>
   ;; on blank lines in-between.
   (should
    (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
+     (org-forward-sentence)
+     (eobp)))
+  ;; On a headline, stop at the end of the line, unless point is
+  ;; already there.
+  (should
+   (equal
+    "* Headline"
+    (org-test-with-temp-text "* <point>Headline\nSentence."
+      (org-forward-sentence)
+      (buffer-substring-no-properties (line-beginning-position) (point)))))
+  (should
+   (org-test-with-temp-text "* Headline<point>\nSentence."
      (org-forward-sentence)
      (eobp))))