Browse Source

now able to pass arguments to source block from header-arguments variable assignment

Eric Schulte 16 years ago
parent
commit
9865923f84
3 changed files with 24 additions and 17 deletions
  1. 19 15
      litorgy/litorgy-ref.el
  2. 1 1
      litorgy/litorgy.el
  3. 4 1
      rorg.org

+ 19 - 15
litorgy/litorgy-ref.el

@@ -59,16 +59,6 @@ names, and the emacs-lisp representation of the related value."
   (mapcar #'litorgy-ref-parse
    (delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params))))
 
-(defun litorgy-ref-literal (ref)
-  "Determine if the right side of a header argument variable
-assignment is a literal value or is a reference to some external
-resource.  If REF is literal then return it's value, otherwise
-return nil."
-  (let ((out (string-to-number ref)))
-    (if (or (not (equal out 0)) (string= ref "0") (string= ref "0.0")) out ;; number
-      (if (string-match "\"\\(.+\\)\"" ref) (read ref) ;; string
-        nil)))) ;; reference
-
 (defun litorgy-ref-parse (assignment)
   "Parse a variable ASSIGNMENT in a header argument.  If the
 right hand side of the assignment has a literal value return that
@@ -77,17 +67,34 @@ and find it's value using `litorgy-ref-resolve-reference'.
 Return a list with two elements.  The first element of the list
 will be the name of the variable, and the second will be an
 emacs-lisp representation of the value of the variable."
-  (if (string-match "\\(.+\\)=\\(.+\\)" assignment)
+  (message (format "assignment=%S" assignment))
+  (if (string-match "\\(.+?\\)=\\(.+\\)" assignment)
       (let ((var (match-string 1 assignment))
             (ref (match-string 2 assignment)))
         (cons (intern var)
               (or (litorgy-ref-literal ref)
                   (litorgy-ref-resolve-reference ref))))))
 
+(defun litorgy-ref-literal (ref)
+  "Determine if the right side of a header argument variable
+assignment is a literal value or is a reference to some external
+resource.  If REF is literal then return it's value, otherwise
+return nil."
+  (let ((out (string-to-number ref)))
+    (if (or (not (equal out 0)) (string= ref "0") (string= ref "0.0")) out ;; number
+      (if (string-match "\"\\(.+\\)\"" ref) (read ref) ;; string
+        nil)))) ;; reference
+
 (defun litorgy-ref-resolve-reference (ref)
   "Resolve the reference and return it's value"
   (save-excursion
-    (let (direction type args)
+    (let (direction type args new-ref)
+      ;; assign any arguments to pass to source block
+      (when (string-match "\\(.+\\)\(\\(.+\\)\)" ref)
+        (save-match-data
+          (setq args (mapcar (lambda (ref) (cons :var ref))
+                           (split-string (match-string 2 ref) ",[ \f\t\n\r\v]*"))))
+        (setq ref (match-string 1 ref)))
       (when (string-match "\\(.+\\):\\(.+\\)" ref)
         (find-file (match-string 1 ref))
         (setf ref (match-string 2 ref)))
@@ -118,9 +125,6 @@ emacs-lisp representation of the value of the variable."
                    (mapcar #'litorgy-read row))
                  (org-table-to-lisp)))
         ('source-block
-         ;; assign any arguments to pass to source block
-         (when (string-match "\(\\(.+\\)\)" ref)
-           (setq args (mapcar (lambda (ref) (cons :var ref)) (split-string ref ",[ \f\t\n\r\v]*"))))
          (litorgy-execute-src-block t nil args))))))
 
 (defun litorgy-ref-at-ref-p ()

+ 1 - 1
litorgy/litorgy.el

@@ -113,7 +113,7 @@ the header arguments specified at the source code block." ; TODO implement!!
   (let* ((info (or info (litorgy-get-src-block-info)))
          (lang (first info))
          (body (second info))
-         (params (third info))
+         (params (org-combine-plists (third info) params))
          (cmd (intern (concat "litorgy-execute:" lang)))
          result)
     (unless (member lang litorgy-interpreters)

+ 4 - 1
rorg.org

@@ -634,10 +634,13 @@ inline source code blocks) [[file:test-export.org]]
 
 : 15
 
-#+begin_src emacs-lisp :var result=triple(n=8)
+#+begin_src emacs-lisp :var result=triple(n=default, m=98) :results replace
 result
 #+end_src
 
+: 15
+
+
 #+begin_src ruby :var num="eric"
 num+" schulte"
 #+end_src