emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Dima Kogan <dima@secretsauce.net>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: emacs-orgmode@gnu.org
Subject: Re: Adding new table rows/cols in a formula update
Date: Fri, 10 Oct 2014 11:17:54 -0700	[thread overview]
Message-ID: <87k347eqbz.fsf@secretsauce.net> (raw)
In-Reply-To: <87siiwdytt.fsf@nicolasgoaziou.fr>

[-- Attachment #1: Type: text/plain, Size: 231 bytes --]

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> Would you mind providing a test for it in org-test-table.el and an entry
> in ORG-NEWS?
>
> The commit message should be something like the following...

OK. New patch attached.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-table-Field-formulas-can-now-create-columns-as-n.patch --]
[-- Type: text/x-diff, Size: 5150 bytes --]

From 273e642c937f0e12a2f71cf6499415406d708629 Mon Sep 17 00:00:00 2001
From: Dima Kogan <dima@secretsauce.net>
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.

TINYCHANGE
---
 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


  reply	other threads:[~2014-10-10 18:24 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-29 17:45 Adding new table rows/cols in a formula update Dima Kogan
2014-09-30 19:27 ` Dima Kogan
2014-09-30 19:54   ` Subhan Michael Tindall
2014-10-01  5:44     ` Dima Kogan
2014-10-01 19:38       ` Nicolas Goaziou
2014-10-01 19:38         ` Dima Kogan
2014-10-01 20:17         ` Nick Dokos
2014-10-03 18:07           ` Dima Kogan
2014-10-10 10:05             ` Nicolas Goaziou
2014-10-10 18:17               ` Dima Kogan [this message]
2014-10-11 13:09                 ` Bastien
2014-10-11 15:14                   ` Nicolas Goaziou
2014-10-11 17:02                     ` Dima Kogan
2014-10-12  9:18                       ` Nicolas Goaziou
2014-10-12 10:08                         ` Bastien
2014-10-12 12:06                           ` Nicolas Goaziou
2014-10-12 12:20                             ` Bastien
2014-10-13 17:14                       ` Achim Gratz
2014-12-08 19:50                         ` Achim Gratz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k347eqbz.fsf@secretsauce.net \
    --to=dima@secretsauce.net \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).