Normally this only looks at visible headings, but when INVISIBLE-OK is
non-nil it will also look at invisible ones."
(interactive "p")
- (org-back-to-heading invisible-ok)
- (org-at-heading-p)
- (let* ((level (- (match-end 0) (match-beginning 0) 1))
- (re (format "^\\*\\{1,%d\\} " level))
- l)
- (forward-char 1)
- (while (> arg 0)
- (while (and (re-search-forward re nil 'move)
- (setq l (- (match-end 0) (match-beginning 0) 1))
- (= l level)
- (not invisible-ok)
- (progn (backward-char 1) (outline-invisible-p)))
- (if (< l level) (setq arg 1)))
- (setq arg (1- arg)))
- (beginning-of-line 1)))
+ (if (not (ignore-errors (org-back-to-heading invisible-ok)))
+ (outline-next-heading)
+ (org-at-heading-p)
+ (let* ((level (- (match-end 0) (match-beginning 0) 1))
+ (re (format "^\\*\\{1,%d\\} " level))
+ l)
+ (forward-char 1)
+ (while (> arg 0)
+ (while (and (re-search-forward re nil 'move)
+ (setq l (- (match-end 0) (match-beginning 0) 1))
+ (= l level)
+ (not invisible-ok)
+ (progn (backward-char 1) (outline-invisible-p)))
+ (if (< l level) (setq arg 1)))
+ (setq arg (1- arg)))
+ (beginning-of-line 1))))
(defun org-backward-heading-same-level (arg &optional invisible-ok)
"Move backward to the arg'th subheading at same level as this one.
Stop at the first and last subheadings of a superior heading."
(interactive "p")
- (org-back-to-heading)
- (org-at-heading-p)
- (let* ((level (- (match-end 0) (match-beginning 0) 1))
- (re (format "^\\*\\{1,%d\\} " level))
- l)
- (while (> arg 0)
- (while (and (re-search-backward re nil 'move)
- (setq l (- (match-end 0) (match-beginning 0) 1))
- (= l level)
- (not invisible-ok)
- (outline-invisible-p))
- (if (< l level) (setq arg 1)))
- (setq arg (1- arg)))))
+ (if (not (ignore-errors (org-back-to-heading)))
+ (goto-char (point-min))
+ (org-at-heading-p)
+ (let* ((level (- (match-end 0) (match-beginning 0) 1))
+ (re (format "^\\*\\{1,%d\\} " level))
+ l)
+ (while (> arg 0)
+ (while (and (re-search-backward re nil 'move)
+ (setq l (- (match-end 0) (match-beginning 0) 1))
+ (= l level)
+ (not invisible-ok)
+ (outline-invisible-p))
+ (if (< l level) (setq arg 1)))
+ (setq arg (1- arg))))))
(defun org-forward-element ()
"Move forward by one element.