Browse Source

org-table: Fix `org-table-current-column' on hlines

* lisp/org-table.el (org-table-current-column): Return a meaningful
  value also on hlines.
(org-table-toggle-column-width): Use `org-table-current-column'
instead of re-inventing wheel.

* testing/lisp/test-org-table.el (test-org-table/current-column): New
  test.
Nicolas Goaziou 3 years ago
parent
commit
9cf9f56ff6
2 changed files with 24 additions and 14 deletions
  1. 9 14
      lisp/org-table.el
  2. 15 0
      testing/lisp/test-org-table.el

+ 9 - 14
lisp/org-table.el

@@ -1327,13 +1327,16 @@ value."
 		 "")))))
 
 (defun org-table-current-column ()
-  "Find out which column we are in."
+  "Return current column number."
   (interactive)
   (save-excursion
-    (let ((column 0) (pos (point)))
+    (let ((pos (point)))
       (beginning-of-line)
-      (while (search-forward "|" pos t) (cl-incf column))
-      column)))
+      (if (not (search-forward "|" pos t)) 0
+	(let ((column 1)
+	      (separator (if (org-at-table-hline-p) "[+|]" "|")))
+	  (while (re-search-forward separator pos t) (cl-incf column))
+	  column)))))
 
 (defun org-table-current-dline ()
   "Find out what table data line we are in.
@@ -4040,8 +4043,7 @@ When called with `\\[universal-argument] \\[universal-argument]' \
 prefix, expand all columns."
   (interactive "P")
   (unless (org-at-table-p) (user-error "Not in a table"))
-  (let* ((pos (point))
-	 (begin (org-table-begin))
+  (let* ((begin (org-table-begin))
 	 (end (org-table-end))
 	 ;; Compute an upper bound for the number of columns.
 	 ;; Nonexistent columns are ignored anyway.
@@ -4058,14 +4060,7 @@ prefix, expand all columns."
 		 (org-table--read-column-selection
 		  (read-string "Column ranges (e.g. 2-4 6-): ")
 		  max-columns)
-	       ;; Find current column, even when on a hline.
-	       (let ((separator (if (org-at-table-hline-p) "+" "|"))
-		     (c 1))
-		 (save-excursion
-		   (beginning-of-line)
-		   (search-forward "|" pos t)
-		   (while (search-forward separator pos t) (cl-incf c)))
-		 (list c))))
+	       (list (org-table-current-column))))
 	    ((pred stringp) (org-table--read-column-selection arg max-columns))
 	    ((or `(4) `(16)) nil)
 	    (_ (user-error "Invalid argument: %S" arg)))))

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

@@ -2588,6 +2588,21 @@ See also `test-org-table/copy-field'."
 
 ;;; Miscellaneous
 
+(ert-deftest test-org-table/current-column ()
+  "Test `org-table-current-column' specifications."
+  (should
+   (= 1 (org-test-with-temp-text "| <point>a |"
+	  (org-table-current-column))))
+  (should
+   (= 1 (org-test-with-temp-text "|-<point>--|"
+	  (org-table-current-column))))
+  (should
+   (= 2 (org-test-with-temp-text "| 1 | <point>2 |"
+	  (org-table-current-column))))
+  (should
+   (= 2 (org-test-with-temp-text "|---+-<point>--|"
+	  (org-table-current-column)))))
+
 (ert-deftest test-org-table/get-field ()
   "Test `org-table-get-field' specifications."
   ;; Regular test.