Bläddra i källkod

Merge branch 'maint'

Nicolas Goaziou 7 år sedan
förälder
incheckning
64cbcb1bff
2 ändrade filer med 107 tillägg och 18 borttagningar
  1. 21 18
      lisp/org-table.el
  2. 86 0
      testing/lisp/test-org-table.el

+ 21 - 18
lisp/org-table.el

@@ -1341,7 +1341,7 @@ value."
 Only data lines count for this."
   (save-excursion
     (let ((c 0)
-	  (pos (point)))
+	  (pos (line-beginning-position)))
       (goto-char (org-table-begin))
       (while (<= (point) pos)
 	(when (looking-at org-table-dataline-regexp) (cl-incf c))
@@ -1536,28 +1536,31 @@ non-nil, the one above is used."
 	 (dline1 (org-table-current-dline))
 	 (dline2 (+ dline1 (if up -1 1)))
 	 (tonew (if up 0 2))
-	 txt hline2p)
+	 hline2p)
+    (when (and up (= (point-min) (line-beginning-position)))
+      (user-error "Cannot move row further"))
     (beginning-of-line tonew)
-    (unless (org-at-table-p)
+    (when (or (and (not up) (eobp)) (not (org-at-table-p)))
       (goto-char pos)
       (user-error "Cannot move row further"))
     (setq hline2p (looking-at org-table-hline-regexp))
     (goto-char pos)
-    (beginning-of-line 1)
-    (setq pos (point))
-    (setq txt (buffer-substring (point) (1+ (point-at-eol))))
-    (delete-region (point) (1+ (point-at-eol)))
-    (beginning-of-line tonew)
-    (insert txt)
-    (beginning-of-line 0)
-    (org-move-to-column col)
-    (unless (or hline1p hline2p
-		(not (or (not org-table-fix-formulas-confirm)
-			 (funcall org-table-fix-formulas-confirm
-				  "Fix formulas? "))))
-      (org-table-fix-formulas
-       "@" (list (cons (number-to-string dline1) (number-to-string dline2))
-		 (cons (number-to-string dline2) (number-to-string dline1)))))))
+    (let ((row (delete-and-extract-region (line-beginning-position)
+					  (line-beginning-position 2))))
+      (beginning-of-line tonew)
+      (unless (bolp) (insert "\n"))	;at eob without a newline
+      (insert row)
+      (unless (bolp) (insert "\n"))	;missing final newline in ROW
+      (beginning-of-line 0)
+      (org-move-to-column col)
+      (unless (or hline1p hline2p
+		  (not (or (not org-table-fix-formulas-confirm)
+			   (funcall org-table-fix-formulas-confirm
+				    "Fix formulas? "))))
+	(org-table-fix-formulas
+	 "@" (list
+	      (cons (number-to-string dline1) (number-to-string dline2))
+	      (cons (number-to-string dline2) (number-to-string dline1))))))))
 
 ;;;###autoload
 (defun org-table-insert-row (&optional arg)

+ 86 - 0
testing/lisp/test-org-table.el

@@ -2100,6 +2100,92 @@ is t, then new columns should be added as needed"
       (let ((org-table-tab-jumps-over-hlines nil)) (org-table-next-field))
       (buffer-string)))))
 
+
+
+;;; Moving rows, moving columns
+
+(ert-deftest test-org-table/move-row-down ()
+  "Test `org-table-move-row-down' specifications."
+  ;; Error out when row cannot be moved, e.g., it is the last row in
+  ;; the table.
+  (should-error
+   (org-test-with-temp-text "| a |"
+     (org-table-move-row-down)))
+  (should-error
+   (org-test-with-temp-text "| a |\n"
+     (org-table-move-row-down)))
+  (should-error
+   (org-test-with-temp-text "| a |\n| <point>b |"
+     (org-table-move-row-down)))
+  ;; Move data lines.
+  (should
+   (equal "| b |\n| a |\n"
+	  (org-test-with-temp-text "| a |\n| b |\n"
+	    (org-table-move-row-down)
+	    (buffer-string))))
+  (should
+   (equal "|---|\n| a |\n"
+	  (org-test-with-temp-text "| a |\n|---|\n"
+	    (org-table-move-row-down)
+	    (buffer-string))))
+  ;; Move hlines.
+  (should
+   (equal "| b |\n|---|\n"
+	  (org-test-with-temp-text "|---|\n| b |\n"
+	    (org-table-move-row-down)
+	    (buffer-string))))
+  (should
+   (equal "|---|\n|---|\n"
+	  (org-test-with-temp-text "|---|\n|---|\n"
+	    (org-table-move-row-down)
+	    (buffer-string))))
+  ;; Move rows even without a final newline.
+  (should
+   (equal "| b |\n| a |\n"
+	  (org-test-with-temp-text "| a |\n| b |"
+	    (org-table-move-row-down)
+	    (buffer-string)))))
+
+(ert-deftest test-org-table/move-row-up ()
+  "Test `org-table-move-row-up' specifications."
+  ;; Error out when row cannot be moved, e.g., it is the first row in
+  ;; the table.
+  (should-error
+   (org-test-with-temp-text "| a |"
+     (org-table-move-row-up)))
+  (should-error
+   (org-test-with-temp-text "| a |\n"
+     (org-table-move-row-up)))
+  ;; Move data lines.
+  (should
+   (equal "| b |\n| a |\n"
+	  (org-test-with-temp-text "| a |\n| <point>b |\n"
+	    (org-table-move-row-up)
+	    (buffer-string))))
+  (should
+   (equal "| b |\n|---|\n"
+	  (org-test-with-temp-text "|---|\n| <point>b |\n"
+	    (org-table-move-row-up)
+	    (buffer-string))))
+  ;; Move hlines.
+  (should
+   (equal "|---|\n| a |\n"
+	  (org-test-with-temp-text "| a |\n|<point>---|\n"
+	    (org-table-move-row-up)
+	    (buffer-string))))
+  (should
+   (equal "|---|\n|---|\n"
+	  (org-test-with-temp-text "|---|\n|<point>---|\n"
+	    (org-table-move-row-up)
+	    (buffer-string))))
+  ;; Move rows even without a final newline.
+  (should
+   (equal "| b |\n| a |\n"
+	  (org-test-with-temp-text "| a |\n| <point>b |"
+	    (org-table-move-row-up)
+	    (buffer-string)))))
+
+
 
 ;;; Miscellaneous