Browse Source

now assigning variables from source-code blocks in multiple languages

Currently ruby, python, and emacs-lisp are working; still need to work
on shells and R.
Eric Schulte 16 years ago
parent
commit
0e29fb3601
5 changed files with 100 additions and 49 deletions
  1. 1 3
      litorgy/litorgy-lisp.el
  2. 2 2
      litorgy/litorgy-ref.el
  3. 48 35
      litorgy/litorgy-script.el
  4. 2 3
      litorgy/litorgy.el
  5. 47 6
      rorg.org

+ 1 - 3
litorgy/litorgy-lisp.el

@@ -43,9 +43,7 @@ function is called by `litorgy-execute-src-block'."
       (setq results
             (eval `(let ,(mapcar (lambda (var) `(,(car var) ',(cdr var))) vars)
                      ,(read body))))
-      (if (assoc :raw params)
-          results
-        (if (listp results) results (format "%S" results))))))
+      results)))
 
 (provide 'litorgy-lisp)
 ;;; litorgy-lisp.el ends here

+ 2 - 2
litorgy/litorgy-ref.el

@@ -53,7 +53,7 @@
 ;;; Code:
 (require 'litorgy)
 
-(defun litorgy-read-cell (cell)
+(defun litorgy-read (cell)
   "Convert the string value of CELL to a number if appropriate.
 Otherwise if cell looks like a list (meaning it starts with a
 '(') then read it as lisp, otherwise return it unmodified as a
@@ -118,7 +118,7 @@ representation of the value of the variable."
                   (case type
                     ('table
                      (mapcar (lambda (row)
-                                      (mapcar #'litorgy-read-cell row))
+                                      (mapcar #'litorgy-read row))
                                     (org-table-to-lisp)))
                     ('source-block
                      (litorgy-execute-src-block t)))))))))

+ 48 - 35
litorgy/litorgy-script.el

@@ -43,6 +43,21 @@ automatically generated wrapper for `litorgy-script-execute'.")
               (litorgy-script-execute ,cmd body params))))
         cmds))
 
+(defvar litorgy-script-ruby-wrapper-method
+  "
+def main
+%s
+end
+puts main
+")
+
+(defvar litorgy-script-python-wrapper-method
+  "
+def main():
+%s
+
+print main()")
+
 (defcustom litorgy-script-interpreters '("ruby" "python")
   "List of interpreters of scripting languages which can be
 executed through litorgy."
@@ -55,50 +70,48 @@ executed through litorgy."
   (let ((vars (litorgy-ref-variables params)))
     (save-window-excursion
       (with-temp-buffer
-        (when (string= "ruby" cmd) (insert "def main\n"))
-        ;; define any variables
-        (mapcar
-         (lambda (pair)
-           (insert (format "%s=%s\n"
-                           (car pair)
-                           (litorgy-script-table-to-ruby/python (cdr pair)))))
-         vars)
-        (case (intern cmd)
-          ('ruby
-           (insert body)
-           (insert "\nend\n\nputs main.inspect\n"))
-          ('python
-           (insert "def main():\n")
+        (insert
+         (format
+          (case (intern cmd)
+            ('ruby litorgy-script-ruby-wrapper-method)
+            ('python litorgy-script-python-wrapper-method))
+          (concat
+           (mapconcat ;; define any variables
+            (lambda (pair)
+              (format "\t%s=%s"
+                      (car pair)
+                      (litorgy-script-var-to-ruby/python (cdr pair))))
+            vars "\n")
+           "\n"
            (let ((body-lines (split-string body "[\n\r]+" t)))
-             (mapc
+             (mapconcat
               (lambda (line)
-                (insert (format "\t%s\n" line)))
-              (butlast body-lines))
-             (insert (format "\treturn %s\n" (car (last body-lines)))))
-           (insert "\nprint main()\n")))
+                (format "\t%s\n" line))
+              (butlast body-lines) "\n")
+             (format "\treturn %s\n" (car (last body-lines)))))))
         (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
         (litorgy-script-table-or-results (buffer-string))))))
 
-(defun litorgy-script-table-to-ruby/python (table)
-  "Convert an elisp table (nested lists) into a string of ruby
-source code specifying a table (nested arrays)."
-  (if (listp table)
-      (concat "[" (mapconcat #'litorgy-script-table-to-ruby/python table ", ") "]")
-    (format "%S" table)))
+(defun litorgy-script-var-to-ruby/python (var)
+  "Convert an elisp var into a string of ruby or python source
+code specifying a var of the same value."
+  (if (listp var)
+      (concat "[" (mapconcat #'litorgy-script-var-to-ruby/python var ", ") "]")
+    (format "%S" var)))
 
 (defun litorgy-script-table-or-results (results)
   "If the results look like a table, then convert them into an
 Emacs-lisp table, otherwise return the results as a string."
-  (when (string-match "^\\[.+\\]$" results)
-    (setq results
-          ;; somewhat hacky, but thanks to similarities between
-          ;; languages it seems to work
-          (read (replace-regexp-in-string
-                 "\\[" "(" (replace-regexp-in-string
-                            "\\]" ")" (replace-regexp-in-string
-                                       ", " " " (replace-regexp-in-string
-                                                 "'" "\"" results)))))))
-  results)
+  (litorgy-read
+   (if (string-match "^\\[.+\\]$" results)
+       ;; somewhat hacky, but thanks to similarities between languages
+       ;; it seems to work
+       (replace-regexp-in-string
+        "\\[" "(" (replace-regexp-in-string
+                   "\\]" ")" (replace-regexp-in-string
+                              ", " " " (replace-regexp-in-string
+                                        "'" "\"" results))))
+     results)))
 
 (provide 'litorgy-script)
 ;;; litorgy-script.el ends here

+ 2 - 3
litorgy/litorgy.el

@@ -106,8 +106,6 @@ source block)."
          result)
     (unless (member lang litorgy-interpreters)
       (error "Language is not in `litorgy-interpreters': %s" lang))
-    (when arg
-      (setq params (cons '(:raw . t) params)))
     (setq result (funcall cmd body params))
     (if arg
         result
@@ -136,7 +134,7 @@ form.  (language body header-arguments-alist)"
   (unless (save-excursion
             (beginning-of-line 1)
             (looking-at litorgy-src-block-regexp))
-    (error (format "not looking at src-block (%s)" (point))))
+    (error "not looking at src-block"))
   (let ((lang (litorgy-clean-text-properties (match-string 1)))
         (args (litorgy-clean-text-properties (or (match-string 3) "")))
         (body (litorgy-clean-text-properties (match-string 4))))
@@ -163,6 +161,7 @@ replace - insert results after the source block replacing any
           previously inserted results
 
 silent -- no results are inserted"
+  (unless (listp result) (setq result (format "%S" result)))
   (if (string-equal insert "replace") (litorgy-remove-result (listp result)))
   (if (= (length result) 0)
       (message "no result returned by source block")

+ 47 - 6
rorg.org

@@ -58,6 +58,9 @@ At least initially I'll try to implement this so that there is no need
 to specify whether the reference is to a table or a source-code block.
 That seems to be simpler both in terms of use and implementation.
 
+This is now working for emacs-lisp, ruby and python (and mixtures of
+the three) source blocks.  See the examples in the [[* (sandbox) referencing other source blocks][sandbox]].
+
 This is currently working only with emacs lisp as in the following
 example in the [[* emacs lisp source reference][emacs lisp source reference]].
 
@@ -265,7 +268,7 @@ x
 #+end_src
 
 
-** referencing other source blocks
+** (sandbox) referencing other source blocks
 Doing this in emacs-lisp first because it's trivial to convert
 emacs-lisp results to and from emacs-lisp.
 
@@ -284,8 +287,6 @@ used in the calculations of the second source block.
 (* first 3)
 #+end_src
 
-: 18
-
 This example is the same as the previous only the variable being
 passed through is a table rather than a number.
 
@@ -303,13 +304,53 @@ passed through is a table rather than a number.
 (transpose table)
 #+end_src
 
-#+begin_src emacs-lisp :var table=second_src_example
+#+begin_src emacs-lisp :var table=second_src_example :results replace
 (transpose table)
 #+end_src
 
-| 1 |         2 | 3 |
-| 4 | "schulte" | 6 |
+*** ruby python
+Now working for ruby
+
+#+srcname: start
+#+begin_src ruby
+89
+#+end_src
+
+#+begin_src ruby :var other=start :results replace
+2 * other
+#+end_src
+
+and for python
+
+#+SRCNAME: start_two
+#+begin_src python
+98
+#+end_src
+
+#+begin_src python :var another=start_two :results replace
+another + 3
+#+end_src
+
+*** mixed languages
+Since all variables are converted into Emacs Lisp it is no problem to
+reference variables specified in another language.
+
+#+SRCNAME: ruby-block
+#+begin_src ruby
+2
+#+end_src
+
+#+SRCNAME: lisp_block
+#+begin_src emacs-lisp :var ruby-variable=ruby-block
+(* ruby-variable 8)
+#+end_src
 
+#+begin_src python :var lisp_var=lisp_block
+lisp_var + 4
+#+end_src
+
+*** R
+not yet implemented
 
 
 * COMMENT Commentary