diff --git a/lisp/org-table.el b/lisp/org-table.el index 39555267c..ac0253ff1 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -387,16 +387,19 @@ portability of tables." (const :tag "Error on attempt to cross" error))) (defcustom org-table-formula-create-columns nil - "Non-nil means that evaluation of a field formula can add new -columns if an out-of-bounds field is being set." + "Non-nil means evaluation of formula can add new columns. +When non-nil, evaluating an out-of-bounds field can insert as +many columns as needed. When set to `warn', issue a warning when +doing so. When set to `prompt', ask user before creating a new +column. Otherwise, throw an error." :group 'org-table-calculation :version "26.1" :package-version '(Org . "8.3") :type '(choice - (const :tag "Setting an out-of-bounds field generates an error (default)" nil) - (const :tag "Setting an out-of-bounds field silently adds columns as needed" t) - (const :tag "Setting an out-of-bounds field adds columns as needed, but issues a warning message" warn) - (const :tag "When setting an out-of-bounds field, the user is prompted" prompt))) + (const :tag "Out-of-bounds field generates an error (default)" nil) + (const :tag "Out-of-bounds field silently adds columns as needed" t) + (const :tag "Out-of-bounds field adds columns, but issues a warning" warn) + (const :tag "Prompt user when setting an out-of-bounds field" prompt))) (defgroup org-table-import-export nil "Options concerning table import and export in Org mode." @@ -3358,7 +3361,9 @@ existing formula for column %s" t)) (and (eq org-table-formula-create-columns 'prompt) (yes-or-no-p - "Out-of-bounds formula. Add columns? "))))))) + "Out-of-bounds formula. Add columns? ")) + (user-error + "Missing columns in the table. Aborting")))))) (org-table-eval-formula nil formula t t t t)))) ;; Clean up markers and internal text property. (remove-text-properties (point-min) (point-max) '(org-untouchable t)) diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index 4f994270a..750290d36 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -1811,59 +1811,50 @@ See also `test-org-table/copy-field'." (buffer-string))))) (ert-deftest test-org-table/field-formula-outside-table () - "If `org-table-formula-create-columns' is nil, then a formula -that references an out-of-bounds column should do nothing. If it -is t, then new columns should be added as needed" - - (let ((org-table-formula-create-columns nil)) - - (should-error - (org-test-table-target-expect - " + "Test `org-table-formula-create-columns' variable." + ;; Refuse to create column if variable is nil. + (should-error + (org-test-with-temp-text " | 2 | | 4 | | 8 | -" - " -| 2 | -| 4 | -| 8 | -" - 1 - "#+TBLFM: @1$2=5") - :type (list 'error 'user-error))) - - (let ((org-table-formula-create-columns t)) - - ;; make sure field formulas work - (org-test-table-target-expect - " -| 2 | -| 4 | -| 8 | -" - " +#+TBLFM: @1$2=5" + (let ((org-table-formula-create-columns nil)) + (org-table-calc-current-TBLFM)) + (buffer-string)) + :type (list 'error 'user-error)) + ;; If the variable is non-nil, field formulas and columns formulas + ;; can create tables. + (should + (equal + " | 2 | 5 | | 4 | | | 8 | | -" - 1 - "#+TBLFM: @1$2=5") - - ;; and make sure column formulas work too - (org-test-table-target-expect - " +#+TBLFM: @1$2=5" + (org-test-with-temp-text " | 2 | | 4 | | 8 | -" - " +#+TBLFM: @1$2=5" + (let ((org-table-formula-create-columns t)) + (org-table-calc-current-TBLFM)) + (buffer-string)))) + (should + (equal + " | 2 | | 15 | | 4 | | 15 | | 8 | | 15 | -" - 1 - "#+TBLFM: $3=15"))) +#+TBLFM: $3=15" + (org-test-with-temp-text " +| 2 | +| 4 | +| 8 | +#+TBLFM: $3=15" + (let ((org-table-formula-create-columns t)) + (org-table-calc-current-TBLFM)) + (buffer-string))))) (ert-deftest test-org-table/duration () "Test durations in table formulas."