org-up-heading-safe: Return true level, ignoring org-odd-levels-only

* lisp/org.el (org-up-heading-safe): Fix return value, making sure
that the returned level is not reduced.  This is what other code
expects and what used to be the case before refactoring, in previous
version of the function.
* testing/lisp/test-org.el (test-org/up-heading-safe): Add test.

Reported-by: E.L.K. <some.any.key@gmail.com>
Link: https://orgmode.org/list/CAF+cOoPuh8rzVRoV9-pGSP3SVRm-M8ZQrM=xGB+o4TqJat_O-A@mail.gmail.com
This commit is contained in:
Ihor Radchenko 2024-02-29 14:36:26 +03:00
parent 731d16f9e9
commit 6ef0154576
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 12 additions and 4 deletions

View File

@ -21248,14 +21248,15 @@ With argument, move up ARG levels."
(defun org-up-heading-safe ()
"Move to the heading line of which the present line is a subheading.
Return the heading level, as number or nil when there is no such heading.
Return the true heading level, as number or nil when there is no such
heading.
When point is not at heading, go to the parent of the current heading.
When point is at or inside an inlinetask, go to the containing
heading.
This version will not throw an error. It will return the level of the
headline found, or nil if no higher level is found.
This version will not throw an error. It will return the true level
of the headline found, or nil if no higher level is found.
When no higher level is found, the still move point to the containing
heading, if there is any in the accessible portion of the buffer.
@ -21271,7 +21272,7 @@ available portion of the buffer."
(<= (point-min) (org-element-begin parent)))
(progn
(goto-char (org-element-begin parent))
(org-element-property :level parent))
(org-element-property :true-level parent))
(when (and current-heading
(<= (point-min) (org-element-begin current-heading)))
(goto-char (org-element-begin current-heading))

View File

@ -2458,6 +2458,13 @@ Test
** H2<point>"
(should (= 1 (org-up-heading-safe)))
(should (looking-at-p "^\\* H1")))
;; Return true level. Ignore `org-odd-levels-only'.
(let ((org-odd-levels-only t))
(org-test-with-temp-text "
*** H1
***** H2<point>"
(should (= 3 (org-up-heading-safe)))
(should (looking-at-p "^\\*\\{3\\} H1"))))
;; Do not jump beyond the level 1 heading.
(org-test-with-temp-text "
Text.