From b8d473a04fcf442fd4036c542521c9cb6f6bfa8d Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sun, 19 Nov 2017 23:22:16 +0100 Subject: [PATCH] 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. --- etc/ORG-NEWS | 6 ++++ lisp/org-table.el | 21 ++++++++----- testing/lisp/test-org-table.el | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 24cc06ae4..6448e676c 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 af3f71769..0388f7084 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 ee83ab04b..7c5d2cc9e 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 "| a | b |" + (org-table-insert-column) + (buffer-string)))) + ;; Move point into the newly created column. + (should + (equal " |" + (org-test-with-temp-text "| a |" + (org-table-insert-column) + (buffer-substring-no-properties (point) (line-end-position))))) + (should + (equal " | b |" + (org-test-with-temp-text "| 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 "| 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