From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dima Kogan Subject: Re: Adding new table rows/cols in a formula update Date: Sat, 11 Oct 2014 10:02:31 -0700 Message-ID: <8761fqedyf.fsf@secretsauce.net> References: <87k34mgvup.fsf@secretsauce.net> <87fvf8hpqw.fsf@secretsauce.net> <87egusgx72.fsf@secretsauce.net> <8738b78tt0.fsf@nicolasgoaziou.fr> <87iok3h7d4.fsf@alphaville.bos.redhat.com> <8761g16n8c.fsf@secretsauce.net> <87siiwdytt.fsf@nicolasgoaziou.fr> <87k347eqbz.fsf@secretsauce.net> <871tqelplk.fsf@bzg.ath.cx> <87tx3aprjk.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55130) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xd04m-00065U-5G for emacs-orgmode@gnu.org; Sat, 11 Oct 2014 13:03:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xd04h-0002vF-2W for emacs-orgmode@gnu.org; Sat, 11 Oct 2014 13:03:28 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:56524) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xd04g-0002v5-UF for emacs-orgmode@gnu.org; Sat, 11 Oct 2014 13:03:23 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by gateway2.nyi.internal (Postfix) with ESMTP id 58E0120968 for ; Sat, 11 Oct 2014 13:03:22 -0400 (EDT) In-reply-to: <87tx3aprjk.fsf@nicolasgoaziou.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Nicolas Goaziou Cc: Bastien , emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Nicolas Goaziou writes: >> Thanks for your work on this, the patch looks good but it cannot be >> accepted as a "tiny change". Would you be willing to sign the FSF >> copyright assignment papers? > > Actually, it's not a tiny change. He signed copyright assignment > already. See http://permalink.gmane.org/gmane.emacs.orgmode/91352 Here's the same patch without the TINYCHANGE marker, if that's helpful. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-org-table-Field-formulas-can-now-create-columns-as-n.patch >From 6a361837f1f7b71a02ab5b918509def84c9fa43e Mon Sep 17 00:00:00 2001 From: Dima Kogan Date: Tue, 30 Sep 2014 22:36:21 -0700 Subject: [PATCH] org-table: Field formulas can now create columns as needed * org-table.el (org-table-formula-create-columns): New variable. (org-table-recalculate): Use the new org-table-formula-make-new-cols customization to control whether org creates new columns when a formula explicitly targets them. --- etc/ORG-NEWS | 6 +++++ lisp/org-table.el | 35 +++++++++++++++++++++++--- testing/lisp/test-org-table.el | 56 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 1af54ad..0a5af68 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -90,6 +90,12 @@ docstrings (including ~orgtbl-to-generic~) for details. *** Non-floating minted listings in Latex export It is not possible to specify =#+attr_latex: :float nil= in conjunction with source blocks exported by the minted package. +*** Field formulas can now create columns as needed +Previously, evaluating formulas that referenced out-of-bounds columns +would throw an error. A new variable +~org-table-formula-create-columns~ was added to adjust this +behavior. It is now possible to silently add new columns, to do so +with a warning or to explicitly ask the user each time. ** Miscellaneous *** File names in links accept are now compatible with URI syntax Absolute file names can now start with =///= in addition to =/=. E.g., diff --git a/lisp/org-table.el b/lisp/org-table.el index 7607ead..14c68d6 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -354,6 +354,18 @@ portability of tables." (const :tag "Stick to hline" nil) (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." + :group 'org-table-calculation + :version "24.5" + :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))) + (defgroup org-table-import-export nil "Options concerning table import and export in Org-mode." :tag "Org Table Import Export" @@ -3125,9 +3137,26 @@ known that the table will be realigned a little later anyway." (while (setq eq (pop eqlname1)) (message "Re-applying formula to field: %s" (car eq)) (org-goto-line (nth 1 eq)) - (org-table-goto-column (nth 2 eq)) - (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst - 'nostore 'noanalysis)) + (let ((column-target (nth 2 eq))) + (when (> column-target 1000) + (user-error "Formula column target too large")) + (let* ((column-count (progn (end-of-line) + (1- (org-table-current-column)))) + (create-new-column + (and (> column-target column-count) + (or (eq org-table-formula-create-columns t) + (and + (eq org-table-formula-create-columns 'warn) + (progn + (org-display-warning "Out-of-bounds formula added columns") + t)) + (and + (eq org-table-formula-create-columns 'prompt) + (yes-or-no-p "Out-of-bounds formula. Add columns?")))))) + (org-table-goto-column column-target nil create-new-column)) + + (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst + 'nostore 'noanalysis))) (org-goto-line thisline) (org-table-goto-column thiscol) diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index e083683..d99db27 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -1550,6 +1550,62 @@ See also `test-org-table/copy-field'." (progn (search-forward "# END RECEIVE ORGTBL table") (match-beginning 0))))))) +(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)) + + ;; need condition-case to trap the out-of-bounds user-error + (condition-case + nil + (org-test-table-target-expect + " +| 2 | +| 4 | +| 8 | +" + " +| 2 | +| 4 | +| 8 | +" + 1 + "#+TBLFM: @1$2=5") + ('user-error t))) + + (let ((org-table-formula-create-columns t)) + + ;; make sure field formulas work + (org-test-table-target-expect + " +| 2 | +| 4 | +| 8 | +" + " +| 2 | 5 | +| 4 | | +| 8 | | +" + 1 + "#+TBLFM: @1$2=5") + + ;; and make sure column formulas work too + (org-test-table-target-expect + " +| 2 | +| 4 | +| 8 | +" + " +| 2 | | 15 | +| 4 | | 15 | +| 8 | | 15 | +" + 1 + "#+TBLFM: $3=15"))) (provide 'test-org-table) -- 2.0.0 --=-=-=--