Browse Source

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.
Bastien Guerry 12 years ago
parent
commit
2718888f8e
1 changed files with 30 additions and 28 deletions
  1. 30 28
      lisp/org.el

+ 30 - 28
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
 Normally this only looks at visible headings, but when INVISIBLE-OK is
 non-nil it will also look at invisible ones."
 non-nil it will also look at invisible ones."
   (interactive "p")
   (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)
 (defun org-backward-heading-same-level (arg &optional invisible-ok)
   "Move backward to the arg'th subheading at same level as this one.
   "Move backward to the arg'th subheading at same level as this one.
 Stop at the first and last subheadings of a superior heading."
 Stop at the first and last subheadings of a superior heading."
   (interactive "p")
   (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 ()
 (defun org-forward-element ()
   "Move forward by one element.
   "Move forward by one element.