diff --git a/lisp/org.el b/lisp/org.el index 0808fc210..c1ef88812 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -20482,15 +20482,16 @@ With ARG, repeats or can move backward if negative." (beginning-of-line)) (_ nil))) (cl-incf arg)) - (while (and (> arg 0) (re-search-forward regexp nil :move)) + (while (and (> arg 0) (re-search-forward regexp nil t)) (pcase (get-char-property-and-overlay (point) 'invisible) (`(outline . ,o) (goto-char (overlay-end o)) - (end-of-line 2)) + (skip-chars-forward " \t\n") + (end-of-line)) (_ (end-of-line))) (cl-decf arg)) - (when (/= arg initial-arg) (beginning-of-line)))) + (if (> arg 0) (goto-char (point-max)) (beginning-of-line)))) (defun org-previous-visible-heading (arg) "Move to the previous visible heading. diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index d22446a09..e0e4c5d08 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -3114,6 +3114,54 @@ SCHEDULED: <2017-05-06 Sat> ;;; Navigation +(ert-deftest test-org/next-visible-heading () + "Test `org-next-visible-heading' specifications." + ;; Move to the beginning of the next headline, taking into + ;; consideration ARG. + (should + (org-test-with-temp-text "* H1\n* H2" + (org-next-visible-heading 1) + (looking-at "\\* H2"))) + (should + (org-test-with-temp-text "* H1\n* H2\n* H3" + (org-next-visible-heading 2) + (looking-at "\\* H3"))) + ;; Ignore invisible headlines. + (should + (org-test-with-temp-text "* H1\n** H2\n* H3" + (org-cycle) + (org-next-visible-heading 1) + (looking-at "\\* H3"))) + (should + (org-test-with-temp-text "* H1\n* H2\n* H3" + (org-next-visible-heading 1) + (looking-at "\\* H2"))) + ;; Move point between headlines, not on blank lines between. + (should + (org-test-with-temp-text "* H1\n** H2\n\n\n\n* H3" + (let ((org-cycle-separator-lines 1)) + (org-cycle) + (org-next-visible-heading 1)) + (looking-at "\\* H3"))) + ;; Move at end of buffer when there is no more headline. + (should + (org-test-with-temp-text "* H1" + (org-next-visible-heading 1) + (eobp))) + (should + (org-test-with-temp-text "* H1\n* H2" + (org-next-visible-heading 2) + (eobp))) + ;; With a negative argument, move backwards. + (should + (org-test-with-temp-text "* H1\n* H2\n* H3" + (org-next-visible-heading -1) + (looking-at "\\* H2"))) + (should + (org-test-with-temp-text "* H1\n* H2\n* H3" + (org-next-visible-heading -2) + (looking-at "\\* H1")))) + (ert-deftest test-org/forward-heading-same-level () "Test `org-forward-heading-same-level' specifications." ;; Test navigation at top level, forward and backward.