╭───────────╴
│ Config
╰───────────╴

Emacs  : GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.16.0) of 2024-08-14

Package: Org mode from git.savannah.gnu.org/srv/git/emacs/org-mode.git

Commit: 499bd92e2, CommitDate: Tue Jul 22 19:59:02 2025 +0200

Current state: emacs -Q

╭───────────╴
│ Subtle bug in org-table-recalculate
╰───────────╴

Sometimes org-table-recalculate does not fully complete its task.

╭───────────╴
│ It happens when:
╰───────────╴

- asking org-table-recalculate NOT to align the table (last parameter passed as t)
- the formulas refer to a non-existing column (2nd column in the example)
- org-table-formula-create-columns set to t, so that a new column will be created for the formulas to deposit their results
- there is both a column formula and a single-cell formula

Then the cell for the single-cell formula is not created.

╭────────────╴
│ To reproduce:
╰────────────╴

Here is a table with one column, and formulas for a future 2nd column.

| n |
|---+
| 1 |
| 2 |
| 3 |
#+TBLFM: $2=$1*11::@1$2=eleven

Here is a test function:

#+begin_src elisp
(defun test ()
  (interactive)
  (let ((org-table-formula-create-columns t))
    (org-table-recalculate t t)))
#+end_src

Type M-x test anywhere in the table.

The cell formula @1$2=eleven is ignored.

The bug sometimes appears also by just typing C-u C-c * anywhere in the table.

╭────────────╴
│ Why?
╰────────────╴

It seems the bug stems from org-table.el line 3084.
The variable column-count gets the value 2, which is wrong. It should be 1.

Why is it wrong?
Because column-count is computed when the point is on a random row of the table. It would be right if the point was on the first row, where the single-cell formula is supposed to land.

Then, through a sophisticated closure, the org-table-goto-field function thinks it must NOT create a new cell.

Reading the mailing-list, it appears that org-table-formula-create-columns was put in place on October 11 2014.

╭────────────╴
│ How to fix it?
╰────────────╴

No idea.

╭────────────╴
│ An ert test-bench
╰────────────╴

The following snippet may be added to the test-org-table.el file,
right after test-org-table/field-formula-outside-table

#+begin_src elisp
(ert-deftest test-org-table/field-formula-outside-table-2 ()
  "Test `org-table-formula-create-columns' variable."
  ;; If the variable is non-nil, field formulas and columns formulas
  ;; can create tables, even when field & column formulas are mixed.
  (should
   (equal
    "
| n | eleven |
|---+--------|
| 1 |     11 |
| 2 |     22 |
| 3 |     33 |
,#+TBLFM: $2=$1*11::@1$2=eleven"
    (org-test-with-temp-text "
| n |
|---+
| 1 |
|<point> 2 |
| 3 |
,#+TBLFM: $2=$1*11::@1$2=eleven"
      (let ((org-table-formula-create-columns t))
        (org-table-recalculate t t)
        (org-table-align))
      (buffer-string)))))
#+end_src



Reply via email to