Subject: bug#44133: org-table insert/delete multiple columns [PATCH INCLUDED]

From: Boruch Baum <[email-masked]>
Date: Thu, 22 Oct 2020 03:27:09 -0400

Currently, functions org-table-insert-column and
org-table-delete-column only perform the operation a single time. The
attached patch adds a feature to allow insertion and deletion of
multiple columns by specifying a numeric prefix argument.

A nice follow-up would be for someone else to add operations on a range,
but I'm not offering that here.

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0
diff --git a/org-table.el b/org-table.el
index dcf7430..23bb480 100644
--- a/org-table.el
+++ b/org-table.el
@@ -1386,27 +1386,29 @@ However, when FORCE is non-nil, create new columns if necessary."
       (if (looking-at " ") (forward-char 1)))))

 ;;;###autoload
-(defun org-table-insert-column ()
-  "Insert a new column into the table."
-  (interactive)
+(defun org-table-insert-column (&optional arg)
+  "Insert a new column into the table.
+With optional prefix ARG, perform the operation that many times."
+  (interactive "p")
   (unless (org-at-table-p) (user-error "Not at a table"))
-  (org-table-find-dataline)
-  (let* ((col (max 1 (org-table-current-column)))
-	 (beg (org-table-begin))
-	 (end (copy-marker (org-table-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)))
-    (set-marker end nil)
-    (org-table-align)
-    (when (or (not org-table-fix-formulas-confirm)
-	      (funcall org-table-fix-formulas-confirm "Fix formulas? "))
-      (org-table-fix-formulas "$" nil (1- col) 1)
-      (org-table-fix-formulas "$LR" nil (1- col) 1))))
+  (dotimes (n (max arg 0))
+    (org-table-find-dataline)
+    (let* ((col (max 1 (org-table-current-column)))
+  	 (beg (org-table-begin))
+  	 (end (copy-marker (org-table-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)))
+      (set-marker end nil)
+      (org-table-align)
+      (when (or (not org-table-fix-formulas-confirm)
+  	      (funcall org-table-fix-formulas-confirm "Fix formulas? "))
+        (org-table-fix-formulas "$" nil (1- col) 1)
+        (org-table-fix-formulas "$LR" nil (1- col) 1)))))

 (defun org-table-find-dataline ()
   "Find a data line in the current table, which is needed for column commands."
@@ -1451,33 +1453,44 @@ non-nil, the one above is used."
 	       (if above min max))))))

 ;;;###autoload
-(defun org-table-delete-column ()
-  "Delete a column from the table."
-  (interactive)
+(defun org-table-delete-column (&optional arg)
+  "Delete a column from the table.
+With optional prefix ARG, perform the operation that many times."
+  (interactive "p")
   (unless (org-at-table-p) (user-error "Not at a table"))
-  (org-table-find-dataline)
-  (org-table-check-inside-data-field)
-  (let ((col (org-table-current-column))
-	(beg (org-table-begin))
-	(end (copy-marker (org-table-end))))
-    (org-table-save-field
-     (goto-char beg)
-     (while (< (point) end)
-       (if (org-at-table-hline-p)
-	   nil
-	 (org-table-goto-column col t)
-	 (and (looking-at "|[^|\n]+|")
-	      (replace-match "|")))
-       (forward-line)))
-    (set-marker end nil)
-    (org-table-goto-column (max 1 (1- col)))
-    (org-table-align)
-    (when (or (not org-table-fix-formulas-confirm)
-	      (funcall org-table-fix-formulas-confirm "Fix formulas? "))
-      (org-table-fix-formulas
-       "$" (list (cons (number-to-string col) "INVALID")) col -1 col)
-      (org-table-fix-formulas
-       "$LR" (list (cons (number-to-string col) "INVALID")) col -1 col))))
+    (org-table-find-dataline)
+    (org-table-check-inside-data-field)
+    (dotimes (n (min (max arg 0)
+                     (let ((p  (point))
+                           (c1 (org-table-current-column))
+                           c2)
+                       (end-of-line)
+                       (setq c2 (org-table-current-column))
+                       (goto-char p)
+                       (- c2 c1))))
+      (let ((col (org-table-current-column))
+    	(beg (org-table-begin))
+    	(end (copy-marker (org-table-end))))
+        (org-table-save-field
+         (goto-char beg)
+         (while (< (point) end)
+           (if (org-at-table-hline-p)
+    	   nil
+    	 (org-table-goto-column col t)
+    	 (and (looking-at "|[^|\n]+|")
+    	      (replace-match "|")))
+           (forward-line)))
+        (set-marker end nil)
+        (org-table-goto-column (max 1 (1- col)))
+        (org-table-align)
+        (when (or (not org-table-fix-formulas-confirm)
+    	      (funcall org-table-fix-formulas-confirm "Fix formulas? "))
+          (org-table-fix-formulas
+           "$" (list (cons (number-to-string col) "INVALID")) col -1 col)
+          (org-table-fix-formulas
+           "$LR" (list (cons (number-to-string col) "INVALID")) col -1 col)))
+      (org-table-next-field))
+    (org-table-previous-field))

 ;;;###autoload
 (defun org-table-move-column-right ()