Browse Source

Fix org-backward-element with point on first element in section

* lisp/org.el (org-backward-element): When called at the beginning of
  first element in section, the function shouldn't return an error but
  move point to headline or point-min instead.
* testing/lisp/test-org.el: Add test.
Nicolas Goaziou 12 years ago
parent
commit
664132667b
2 changed files with 47 additions and 31 deletions
  1. 21 18
      lisp/org.el
  2. 26 13
      testing/lisp/test-org.el

+ 21 - 18
lisp/org.el

@@ -21918,24 +21918,27 @@ Move to the next element at the same level, when possible."
   "Move backward by one element.
   "Move backward by one element.
 Move to the previous element at the same level, when possible."
 Move to the previous element at the same level, when possible."
   (interactive)
   (interactive)
-  (if (org-with-limited-levels (org-at-heading-p))
-      ;; At an headline, move to the previous one, if any, or stay
-      ;; here.
-      (let ((origin (point)))
-	(org-backward-heading-same-level 1)
-	(unless (org-with-limited-levels (org-at-heading-p))
-	  (goto-char origin)
-	  (error "Cannot move further up")))
-    (let* ((trail (org-element-at-point 'keep-trail))
-	   (elem (car trail))
-	   (prev-elem (nth 1 trail))
-	   (beg (org-element-property :begin elem)))
-      (cond
-       ;; Move to beginning of current element if point isn't there
-       ;; already.
-       ((/= (point) beg) (goto-char beg))
-       ((not prev-elem) (error "Cannot move further up"))
-       (t (goto-char (org-element-property :begin prev-elem)))))))
+  (cond ((bobp) (error "Cannot move further up"))
+	((org-with-limited-levels (org-at-heading-p))
+	 ;; At an headline, move to the previous one, if any, or stay
+	 ;; here.
+	 (let ((origin (point)))
+	   (org-backward-heading-same-level 1)
+	   (unless (org-with-limited-levels (org-at-heading-p))
+	     (goto-char origin)
+	     (error "Cannot move further up"))))
+	(t
+	 (let* ((trail (org-element-at-point 'keep-trail))
+		(elem (car trail))
+		(prev-elem (nth 1 trail))
+		(beg (org-element-property :begin elem)))
+	   (cond
+	    ;; Move to beginning of current element if point isn't
+	    ;; there already.
+	    ((/= (point) beg) (goto-char beg))
+	    (prev-elem (goto-char (org-element-property :begin prev-elem)))
+	    ((org-before-first-heading-p) (goto-char (point-min)))
+	    (t (org-back-to-heading)))))))
 
 
 ;;;###autoload
 ;;;###autoload
 (defun org-up-element ()
 (defun org-up-element ()

+ 26 - 13
testing/lisp/test-org.el

@@ -465,42 +465,55 @@ Outside."
 
 
 (ert-deftest test-org/backward-element ()
 (ert-deftest test-org/backward-element ()
   "Test `org-backward-element' specifications."
   "Test `org-backward-element' specifications."
-  ;; 1. At BOB (modulo some white spaces): should error.
+  ;; 1. Should error at BOB.
   (org-test-with-temp-text "    \nParagraph."
   (org-test-with-temp-text "    \nParagraph."
-    (org-skip-whitespace)
     (should-error (org-backward-element)))
     (should-error (org-backward-element)))
-  ;; 2. Not at the beginning of an element: move at its beginning.
+  ;; 2. Should move at BOB when called on the first element in buffer.
+  (should
+   (org-test-with-temp-text "\n#+TITLE: test"
+     (progn (forward-line)
+	    (org-backward-element)
+	    (bobp))))
+  ;; 3. Not at the beginning of an element: move at its beginning.
   (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
   (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
     (goto-line 3)
     (goto-line 3)
     (end-of-line)
     (end-of-line)
     (org-backward-element)
     (org-backward-element)
     (should (looking-at "Paragraph2.")))
     (should (looking-at "Paragraph2.")))
-  ;; 3. Headline tests.
+  ;; 4. Headline tests.
   (org-test-with-temp-text "
   (org-test-with-temp-text "
 * Head 1
 * Head 1
 ** Head 1.1
 ** Head 1.1
 *** Head 1.1.1
 *** Head 1.1.1
 ** Head 1.2"
 ** Head 1.2"
-    ;; 3.1. At an headline beginning: move to previous headline at the
+    ;; 4.1. At an headline beginning: move to previous headline at the
     ;;      same level.
     ;;      same level.
     (goto-line 5)
     (goto-line 5)
     (org-backward-element)
     (org-backward-element)
     (should (looking-at "** Head 1.1"))
     (should (looking-at "** Head 1.1"))
-    ;; 3.2. At an headline beginning: move to parent headline if no
+    ;; 4.2. At an headline beginning: move to parent headline if no
     ;;      headline at the same level.
     ;;      headline at the same level.
     (goto-line 3)
     (goto-line 3)
     (org-backward-element)
     (org-backward-element)
     (should (looking-at "* Head 1"))
     (should (looking-at "* Head 1"))
-    ;; 3.3. At the first top-level headline: should error.
+    ;; 4.3. At the first top-level headline: should error.
     (goto-line 2)
     (goto-line 2)
     (should-error (org-backward-element)))
     (should-error (org-backward-element)))
-  ;; 4. At beginning of first element inside a greater element:
+  ;; 5. At beginning of first element inside a greater element:
   ;;    expected to move to greater element's beginning.
   ;;    expected to move to greater element's beginning.
   (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
   (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
     (goto-line 3)
     (goto-line 3)
     (org-backward-element)
     (org-backward-element)
     (should (looking-at "#\\+BEGIN_CENTER")))
     (should (looking-at "#\\+BEGIN_CENTER")))
-  ;; 5. List tests.
+  ;; 6. At the beginning of the first element in a section: should
+  ;;    move back to headline, if any.
+  (should
+   (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
+     (progn (goto-char (point-max))
+	    (beginning-of-line)
+	    (org-backward-element)
+	    (org-at-heading-p))))
+  ;; 7. List tests.
   (org-test-with-temp-text "
   (org-test-with-temp-text "
 - item1
 - item1
 
 
@@ -516,19 +529,19 @@ Outside."
 
 
 
 
 Outside."
 Outside."
-    ;; 5.1. At beginning of sub-list: expected to move to the
+    ;; 7.1. At beginning of sub-list: expected to move to the
     ;;      paragraph before it.
     ;;      paragraph before it.
     (goto-line 4)
     (goto-line 4)
     (org-backward-element)
     (org-backward-element)
     (should (looking-at "item1"))
     (should (looking-at "item1"))
-    ;; 5.2. At an item in a list: expected to move at previous item.
+    ;; 7.2. At an item in a list: expected to move at previous item.
     (goto-line 8)
     (goto-line 8)
     (org-backward-element)
     (org-backward-element)
     (should (looking-at "  - sub2"))
     (should (looking-at "  - sub2"))
     (goto-line 12)
     (goto-line 12)
     (org-backward-element)
     (org-backward-element)
     (should (looking-at "- item1"))
     (should (looking-at "- item1"))
-    ;; 5.3. At end of list/sub-list: expected to move to list/sub-list
+    ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
     ;;      beginning.
     ;;      beginning.
     (goto-line 10)
     (goto-line 10)
     (org-backward-element)
     (org-backward-element)
@@ -536,7 +549,7 @@ Outside."
     (goto-line 15)
     (goto-line 15)
     (org-backward-element)
     (org-backward-element)
     (should (looking-at "- item1"))
     (should (looking-at "- item1"))
-    ;; 5.4. At blank-lines before list end: expected to move to top
+    ;; 7.4. At blank-lines before list end: expected to move to top
     ;; item.
     ;; item.
     (goto-line 14)
     (goto-line 14)
     (org-backward-element)
     (org-backward-element)