summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Guerry <bzg@altern.org>2014-01-12 11:44:49 (GMT)
committer Bastien Guerry <bzg@altern.org>2014-01-12 11:44:49 (GMT)
commit27b6b164efa2bbe71865de10e3f198bbf85f5a87 (patch)
treeec6f2b0b4c5d6ed56885bd785dc512415b337906
parente9e182a7f5d3d91a74c2d5e0b6dd78048d364788 (diff)
parent9a5b8a74349a9439fd22bbea590127c51539a39f (diff)
downloadorg-mode-27b6b164efa2bbe71865de10e3f198bbf85f5a87.zip
org-mode-27b6b164efa2bbe71865de10e3f198bbf85f5a87.tar.gz
Merge branch 'master' of orgmode.org:org-mode
-rw-r--r--doc/org.texi6
-rw-r--r--lisp/org-table.el28
-rw-r--r--testing/lisp/test-org-table.el70
3 files changed, 104 insertions, 0 deletions
diff --git a/doc/org.texi b/doc/org.texi
index 871c0f7..9601d32 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -2624,6 +2624,12 @@ table in that entry. REF is an absolute field or range reference as
described above for example @code{@@3$3} or @code{$somename}, valid in the
referenced table.
+Indirection of NAME-OR-ID: When NAME-OR-ID has the format @code{@@ROW$COLUMN}
+it will be substituted with the name or ID found in this field of the current
+table. For example @code{remote($1, @@>$2)} => @code{remote(year_2013,
+@@>$1)}. The format @code{B3} is not supported because it can not be
+distinguished from a plain table name or ID.
+
@node Formula syntax for Calc
@subsection Formula syntax for Calc
@cindex formula syntax, Calc
diff --git a/lisp/org-table.el b/lisp/org-table.el
index 9124901..007fc14 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -2657,6 +2657,7 @@ not overwrite the stored one."
;; Check for old vertical references
(setq form (org-table-rewrite-old-row-references form))
;; Insert remote references
+ (setq form (org-table-remote-reference-indirection form))
(while (string-match "\\<remote([ \t]*\\([-_a-zA-Z0-9]+\\)[ \t]*,[ \t]*\\([^\n)]+\\))" form)
(setq form
(replace-match
@@ -5010,6 +5011,33 @@ list of the fields in the rectangle."
(org-table-get-range (match-string 0 form) tbeg 1))
form)))))))))
+(defun org-table-remote-reference-indirection (form)
+ "Return formula with table remote references substituted by indirection.
+For example \"remote($1, @>$2)\" => \"remote(year_2013, @>$1)\".
+This indirection works only with the format @ROW$COLUMN. The
+format \"B3\" is not supported because it can not be
+distinguished from a plain table name or ID."
+ (let ((start 0))
+ (while (string-match (concat
+ ;; Same as in `org-table-eval-formula'.
+ "\\<remote([ \t]*\\("
+ ;; Allow "$1", "@<", "$-1", "@<<$1" etc.
+ "[@$][^,)]+"
+ ;; Same as in `org-table-eval-formula'.
+ "\\)[ \t]*,[ \t]*\\([^\n)]+\\))")
+ form
+ start)
+ (setq start (match-end 0))
+ ;; Substitute the remote reference with the value found in the
+ ;; field.
+ (setq form
+ (replace-match
+ (save-match-data
+ (org-table-get-range (org-table-formula-handle-first/last-rc
+ (match-string 1 form))))
+ t t form 1))))
+ form)
+
(defmacro org-define-lookup-function (mode)
(let ((mode-str (symbol-name mode))
(first-p (equal mode 'first))
diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el
index edb51c4..dd13fbf 100644
--- a/testing/lisp/test-org-table.el
+++ b/testing/lisp/test-org-table.el
@@ -793,6 +793,76 @@ See also `test-org-table/copy-field'."
;; Do a calculation: Use Calc (or Lisp ) formula
"$2 = 2 * remote(table, @1$2)")))
+(ert-deftest test-org-table/remote-reference-indirect ()
+ "Access to remote reference with indirection of name or ID."
+ (let ((source-tables "
+#+NAME: 2012
+| amount |
+|--------|
+| 1 |
+| 2 |
+|--------|
+| 3 |
+#+TBLFM: @>$1 = vsum(@I..@II)
+
+#+NAME: 2013
+| amount |
+|--------|
+| 4 |
+| 8 |
+|--------|
+| 12 |
+#+TBLFM: @>$1 = vsum(@I..@II)
+"))
+
+ ;; Read several remote references from same column
+ (org-test-table-target-expect
+ (concat source-tables "
+#+NAME: summary
+| year | amount |
+|-------+---------|
+| 2012 | replace |
+| 2013 | replace |
+|-------+---------|
+| total | replace |
+")
+ (concat source-tables "
+#+NAME: summary
+| year | amount |
+|-------+--------|
+| 2012 | 3 |
+| 2013 | 12 |
+|-------+--------|
+| total | 15 |
+")
+ 1
+ ;; Calc formula
+ "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
+ ;; Lisp formula
+ (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
+ "@>$2 = '(+ @I..@II); N"))
+
+ ;; Read several remote references from same row
+ (org-test-table-target-expect
+ (concat source-tables "
+#+NAME: summary
+| year | 2012 | 2013 | total |
+|--------+---------+---------+---------|
+| amount | replace | replace | replace |
+")
+ (concat source-tables "
+#+NAME: summary
+| year | 2012 | 2013 | total |
+|--------+------+------+-------|
+| amount | 3 | 12 | 15 |
+")
+ 1
+ ;; Calc formula
+ "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
+ ;; Lisp formula
+ (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
+ "@2$> = '(+ $<<..$>>); N"))))
+
(ert-deftest test-org-table/org-at-TBLFM-p ()
(org-test-with-temp-text-in-file
"