Browse Source

Issue user-error when trying to drag before first element

* lisp/org.el (org-drag-element-backward):
(org-drag-element-forward): Check that org-element-at-point returns a
non-nil value to avoid a type-error when called before first element.

* testing/lisp/test-org.el (test-org/drag-element-backward):
(test-org/drag-element-forward): Add test for when point is before
first element.  Adjust existing should-error calls to specify
user-error.
Kyle Meyer 1 year ago
parent
commit
6983f8e8bf
2 changed files with 23 additions and 6 deletions
  1. 4 2
      lisp/org.el
  2. 19 4
      testing/lisp/test-org.el

+ 4 - 2
lisp/org.el

@@ -24693,7 +24693,8 @@ Move to the previous element at the same level, when possible."
   "Move backward element at point."
   (interactive)
   (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
-    (let* ((elem (org-element-at-point))
+    (let* ((elem (or (org-element-at-point)
+		     (user-error "No element at point")))
 	   (prev-elem
 	    (save-excursion
 	      (goto-char (org-element-property :begin elem))
@@ -24719,7 +24720,8 @@ Move to the previous element at the same level, when possible."
   "Move forward element at point."
   (interactive)
   (let* ((pos (point))
-	 (elem (org-element-at-point)))
+	 (elem (or (org-element-at-point)
+		   (user-error "No element at point"))))
     (when (= (point-max) (org-element-property :end elem))
       (user-error "Cannot drag element forward"))
     (goto-char (org-element-property :end elem))

+ 19 - 4
testing/lisp/test-org.el

@@ -3244,18 +3244,27 @@ Outside."
   ;; Error when trying to move first element of buffer.
   (should-error
    (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
-     (org-drag-element-backward)))
+     (org-drag-element-backward))
+   :type 'user-error)
   ;; Error when trying to swap nested elements.
   (should-error
    (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
      (forward-line)
-     (org-drag-element-backward)))
+     (org-drag-element-backward))
+   :type 'user-error)
   ;; Error when trying to swap an headline element and a non-headline
   ;; element.
   (should-error
    (org-test-with-temp-text "Test.\n* Head 1"
      (forward-line)
-     (org-drag-element-backward)))
+     (org-drag-element-backward))
+   :type 'user-error)
+  ;; Error when called before first element.
+  (should-error
+   (org-test-with-temp-text "\n"
+     (forward-line)
+     (org-drag-element-backward))
+   :type 'user-error)
   ;; Preserve visibility of elements and their contents.
   (should
    (equal '((63 . 82) (26 . 48))
@@ -3287,7 +3296,13 @@ Text.
   ;;    headline.
   (org-test-with-temp-text "Test.\n* Head 1"
     (should-error (org-drag-element-forward)))
-  ;; 4. Otherwise, swap elements, preserving column and blank lines
+  ;; 4. Error when called before first element.
+  (should-error
+   (org-test-with-temp-text "\n"
+     (forward-line)
+     (org-drag-element-backward))
+   :type 'user-error)
+  ;; 5. Otherwise, swap elements, preserving column and blank lines
   ;;    between elements.
   (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
     (search-forward "graph")