From 2718888f8e4afcbecb608539b3f16414a7399d3d Mon Sep 17 00:00:00 2001 From: Bastien Guerry Date: Tue, 1 Jan 2013 23:49:12 +0100 Subject: [PATCH] Fix behavior of `org-forward/backward-h-s-l' before first headline * org.el (org-forward-heading-same-level): Before the first headline, go to the first headline. (org-backward-heading-same-level): Before the first headline, go to the beginning of the buffer, like `outline-previous-visible-heading' does. --- lisp/org.el | 58 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 1837218ac..d46b020a5 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -22382,39 +22382,41 @@ Stop at the first and last subheadings of a superior heading. 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. -- 2.11.4.GIT