Bladeren bron

org-table: Insert new column to the right instead of the left

* lisp/org-table.el (org-table-insert-column): Insert new column to
  the right instead of the left.

* testing/lisp/test-org-table.el (test-org-table/insert-column): New
  test.
Nicolas Goaziou 8 jaren geleden
bovenliggende
commit
b8d473a04f
3 gewijzigde bestanden met toevoegingen van 73 en 8 verwijderingen
  1. 6 0
      etc/ORG-NEWS
  2. 13 8
      lisp/org-table.el
  3. 54 0
      testing/lisp/test-org-table.el

+ 6 - 0
etc/ORG-NEWS

@@ -174,6 +174,12 @@ See docstring for details.
 
 ** Miscellaneous
 
+*** ~org-table-insert-column~ inserts a column to the right
+
+It used to insert it on the left.  With this change,
+~org-table-insert-column~ and ~org-table-delete-column~ are
+reciprocal.
+
 *** ~org-publish-resolve-external-link~ accepts a new optional argument.
 *** ~org-irc.el~ now supports exporting =irc:= links properly
 

+ 13 - 8
lisp/org-table.el

@@ -1367,17 +1367,22 @@ However, when FORCE is non-nil, create new columns if necessary."
 	(end (copy-marker (org-table-end)))
 	(shrunk-columns (org-table--list-shrunk-columns)))
     (org-table-expand beg end)
-    (org-table-save-field
-     (goto-char beg)
-     (while (< (point) end)
-       (unless (org-at-table-hline-p)
-	 (org-table-goto-column col t)
-	 (insert "|   "))
-       (forward-line)))
+    (save-excursion
+      (goto-char beg)
+      (while (< (point) end)
+	(unless (org-at-table-hline-p)
+	  (org-table-goto-column col t)
+	  (unless (search-forward "|" (line-end-position) t 2)
+	    ;; Add missing vertical bar at the end of the row.
+	    (end-of-line)
+	    (insert "|"))
+	  (insert "  |"))
+	(forward-line)))
+    (org-table-goto-column (1+ col))
     (org-table-align)
     ;; Shift appropriately stored shrunk column numbers, then hide the
     ;; columns again.
-    (org-table--shrink-columns (mapcar (lambda (c) (if (< c col) c (1+ c)))
+    (org-table--shrink-columns (mapcar (lambda (c) (if (<= c col) c (1+ c)))
 				       shrunk-columns)
 			       beg end)
     (set-marker end nil)

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

@@ -2232,6 +2232,60 @@ is t, then new columns should be added as needed"
 	 (ignore-errors (org-table-previous-field))
 	 (char-after)))))
 
+
+;;; Inserting rows, inserting columns
+
+(ert-deftest test-org-table/insert-column ()
+  "Test `org-table-insert-column' specifications."
+  ;; Error when outside a table.
+  (should-error
+   (org-test-with-temp-text "Paragraph"
+     (org-table-insert-column)))
+  ;; Insert new column after current one.
+  (should
+   (equal "| a |   |\n"
+	  (org-test-with-temp-text "| a |"
+	    (org-table-insert-column)
+	    (buffer-string))))
+  (should
+   (equal "| a |   | b |\n"
+	  (org-test-with-temp-text "| <point>a | b |"
+	    (org-table-insert-column)
+	    (buffer-string))))
+  ;; Move point into the newly created column.
+  (should
+   (equal "  |"
+	  (org-test-with-temp-text "| <point>a |"
+	    (org-table-insert-column)
+	    (buffer-substring-no-properties (point) (line-end-position)))))
+  (should
+   (equal "  | b |"
+	  (org-test-with-temp-text "| <point>a | b |"
+	    (org-table-insert-column)
+	    (buffer-substring-no-properties (point) (line-end-position)))))
+  ;; Handle missing vertical bar in the last column.
+  (should
+   (equal "| a |   |\n"
+	  (org-test-with-temp-text "| a"
+	    (org-table-insert-column)
+	    (buffer-string))))
+  (should
+   (equal "  |"
+	  (org-test-with-temp-text "| <point>a"
+	    (org-table-insert-column)
+	    (buffer-substring-no-properties (point) (line-end-position)))))
+  ;; Handle column insertion when point is before first column.
+  (should
+   (equal " | a |   |\n"
+	  (org-test-with-temp-text " | a |"
+	    (org-table-insert-column)
+	    (buffer-string))))
+  (should
+   (equal " | a |   | b |\n"
+	  (org-test-with-temp-text " | a | b |"
+	    (org-table-insert-column)
+	    (buffer-string)))))
+
 
 
 ;;; Moving rows, moving columns