summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2017-11-19 23:22:16 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2017-11-19 23:22:16 +0100
commitb8d473a04fcf442fd4036c542521c9cb6f6bfa8d (patch)
treedbf3a0b23c388894367ff8e48f8170d259fc2690
parentde289d1fe84238470bcf178bac907a4becb0cf0f (diff)
downloadorg-mode-b8d473a04fcf442fd4036c542521c9cb6f6bfa8d.tar.gz
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.
-rw-r--r--etc/ORG-NEWS6
-rw-r--r--lisp/org-table.el21
-rw-r--r--testing/lisp/test-org-table.el54
3 files changed, 73 insertions, 8 deletions
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 24cc06a..6448e67 100644
--- a/etc/ORG-NEWS
+++ b/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
diff --git a/lisp/org-table.el b/lisp/org-table.el
index af3f717..0388f70 100644
--- a/lisp/org-table.el
+++ b/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)
diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el
index ee83ab0..7c5d2cc 100644
--- a/testing/lisp/test-org-table.el
+++ b/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