Ver código fonte

babel: when passing values from a table an extra '$' prefix forces literal

  thanks to Maurizio Vitale for the suggestion.  This allows literal
  values to be passed to code blocks through `sbe' without forcing the
  use of quotes inside the table.
Eric Schulte 15 anos atrás
pai
commit
f400632f94
1 arquivos alterados com 31 adições e 19 exclusões
  1. 31 19
      contrib/babel/lisp/org-babel-table.el

+ 31 - 19
contrib/babel/lisp/org-babel-table.el

@@ -67,27 +67,39 @@ element list, whose first element is the name of the variable and
 second element is a string of its value.  The following call to
 `sbe' would be equivalent to the following source code block.
 
- (sbe 'source-block (n 2) (m 3))
+ (sbe 'source-block (n $2) (m 3))
 
-#+begin_src emacs-lisp :var results=source-block(n=2, m=3) :results silent
+#+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
 results
-#+end_src"
-  (unless (stringp source-block) (setq source-block (symbol-name source-block)))
-  (org-babel-table-truncate-at-newline ;; org-table cells can't be multi-line
-   (if (and source-block (> (length source-block) 0))
-       (let ((params
-	      (eval `(org-babel-parse-header-arguments
-		      (concat ":var results="
-			      ,source-block
-			      "("
-			      (mapconcat (lambda (var-spec)
-					   (format "%S=%s" (first var-spec) (second var-spec)))
-					 ',variables ", ")
-			      ")")))))
-         (org-babel-execute-src-block
-          nil (list "emacs-lisp" "results"
-                    (org-babel-merge-params '((:results . "silent")) params))))
-     "")))
+#+end_src
+
+NOTE: by default string variable names are interpreted as
+references to source-code blocks, to force interpretation of a
+cell's value as a string, prefix the identifier with two \"$\"s
+rather than a single \"$\" (i.e. \"$$2\" instead of \"$2\" in the
+example above."
+  (let ((variables (mapcar
+                    (lambda (var)
+                      (if (and (= 3 (length var)) (eq (second var) '$))
+                          (list (car var) (format "\"%s\"" (last var)))
+                        var))
+                    variables)))
+    (unless (stringp source-block) (setq source-block (symbol-name source-block)))
+    (org-babel-table-truncate-at-newline ;; org-table cells can't be multi-line
+     (if (and source-block (> (length source-block) 0))
+         (let ((params
+                (eval `(org-babel-parse-header-arguments
+                        (concat ":var results="
+                                ,source-block
+                                "("
+                                (mapconcat (lambda (var-spec)
+                                             (format "%S=%s" (first var-spec) (second var-spec)))
+                                           ',variables ", ")
+                                ")")))))
+           (org-babel-execute-src-block
+            nil (list "emacs-lisp" "results"
+                      (org-babel-merge-params '((:results . "silent")) params))))
+       ""))))
 
 (provide 'org-babel-table)
 ;;; org-babel-table.el ends here