Browse Source

TBLFM remote ref: Add indirection of name or ID

* doc/org.texi (References): Add description for indirection of
NAME-OR-ID.

* lisp/org-table.el (org-table-eval-formula): Make use of
`org-table-remote-reference-indirection'.
(org-table-remote-reference-indirection): New function.

* testing/lisp/test-org-table.el
(test-org-table/remote-reference-indirect): Change to use remote
reference indirection.
Michael Brand 4 years ago
parent
commit
bc1d54b84b
3 changed files with 40 additions and 14 deletions
  1. 6 0
      doc/org.texi
  2. 30 0
      lisp/org-table.el
  3. 4 14
      testing/lisp/test-org-table.el

+ 6 - 0
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

+ 30 - 0
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,35 @@ 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 ((index-last -1) index-this)
+    (while (and (setq index-this
+		      (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))
+		;; Protect from last replace replaced itself.
+		(/= index-last index-this))
+      (setq index-last index-this)
+      ;; 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))

+ 4 - 14
testing/lisp/test-org-table.el

@@ -837,14 +837,9 @@ See also `test-org-table/copy-field'."
 ")
      1
      ;; Calc formula
-     (concat "#+TBLFM: "
-	     "@2$2 = remote(2012, @>$1) :: "
-	     "@3$2 = remote(2013, @>$1) :: "
-	     "@>$2 = vsum(@I..@II)")
+     "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
      ;; Lisp formula
-     (concat "#+TBLFM: "
-	     "@2$2 = '(identity remote(2012, @>$1)); N :: "
-	     "@3$2 = '(identity remote(2013, @>$1)); N :: "
+     (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
 	     "@>$2 = '(+ @I..@II); N"))
 
     ;; Read several remote references from same row
@@ -863,14 +858,9 @@ See also `test-org-table/copy-field'."
 ")
      1
      ;; Calc formula
-     (concat "#+TBLFM: "
-	     "@2$2 = remote(2012, @>$1) :: "
-	     "@2$3 = remote(2013, @>$1) :: "
-	     "@2$> = vsum($<<..$>>)")
+     "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
      ;; Lisp formula
-     (concat "#+TBLFM: "
-	     "@2$2 = '(identity remote(2012, @>$1)); N :: "
-	     "@2$3 = '(identity remote(2013, @>$1)); N :: "
+     (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
 	     "@2$> = '(+ $<<..$>>); N"))))
 
 (ert-deftest test-org-table/org-at-TBLFM-p ()