Browse Source

Merge branch 'master' into gnuplot

Eric Schulte 16 years ago
parent
commit
549bb7f57c

+ 22 - 13
library-of-babel.org

@@ -5,28 +5,37 @@
 
 [[http://downlode.org/Etext/library_of_babel.html][Full text of the Borges short story]]
 
-(setq lob (org-babel-lob-parse-buffer))
-(setq x (gethash 'plot lob))
-(hash-table-count lob)
-(maphash (lambda (key val) (insert key)) lob)
-
-
 * Plotting code
   Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored.
 
-#+resname: R-plot-default-data
-| 0 | 0 |
-
-#+srcname: R-plot
-#+begin_src R :results silent :var data=R-plot-default-data
+#+srcname: R-plot(data=R-plot-example-data)
+#+begin_src R :session *R*
 plot(data)
+"R plot complete"
 #+end_src
 
-#+tblname: example-R-plot-data
+#+tblname: R-plot-example-data
 | 1 |  2 |
 | 2 |  4 |
 | 3 |  9 |
 | 4 | 16 |
 | 5 | 25 |
 
-#+lob: R-plot(data=example-R-plot-data)
+#+lob: R-plot(data=R-plot-example-data)
+
+#+resname: R-plot(data=R-plot-example-data)
+: R plot complete
+
+* Etc
+#+srcname: python-identity(a=1)
+#+begin_src python
+a
+#+end_src
+
+#+srcname: python-add(a=1, b=2)
+#+begin_src python
+a + b
+#+end_src
+
+
+

+ 37 - 50
lisp/langs/org-babel-R.el

@@ -37,36 +37,16 @@
 
 (defun org-babel-execute:R (body params)
   "Execute a block of R code with org-babel.  This function is
-called by `org-babel-execute-src-block'."
+called by `org-babel-execute-src-block' via multiple-value-bind."
   (message "executing R source code block...")
   (save-window-excursion
-    (let* ((vars (org-babel-ref-variables params))
-           (full-body (concat
-                       (mapconcat ;; define any variables
-                        (lambda (pair)
-                          (org-babel-R-assign-elisp (car pair) (cdr pair)))
-                        vars "\n") "\n" body "\n"))
-           (result-params (split-string (or (cdr (assoc :results params)) "")))
-           (result-type (cond ((member "output" result-params) 'output)
-                              ((member "value" result-params) 'value)
-                              (t 'value)))
-           (session (org-babel-R-initiate-session (cdr (assoc :session params))))
-           results)
-      ;; ;;; debugging statements
-      ;; (message (format "result-type=%S" result-type))
-      ;; (message (format "body=%S" body))
-      ;; (message (format "session=%S" session))
-      ;; (message (format "result-params=%S" result-params))
-      ;; evaluate body and convert the results to ruby
-      (setq results (org-babel-R-evaluate session full-body result-type))
-      (setq results (if (member "scalar" result-params)
-                        results
-                      (let ((tmp-file (make-temp-file "org-babel-R")))
-                        (with-temp-file tmp-file (insert results))
-                        (org-babel-import-elisp-from-file tmp-file))))
-      (if (and (member "vector" result-params) (not (listp results)))
-          (list (list results))
-        results))))
+    (let ((full-body (concat
+		      (mapconcat ;; define any variables
+		       (lambda (pair)
+			 (org-babel-R-assign-elisp (car pair) (cdr pair)))
+		       vars "\n") "\n" body "\n"))
+	  (session (org-babel-R-initiate-session session)))
+      (org-babel-R-evaluate session full-body result-type))))
 
 (defun org-babel-prep-session:R (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
@@ -101,8 +81,10 @@ called by `org-babel-execute-src-block'."
     (setq session (or session "*R*"))
     (if (org-babel-comint-buffer-livep session)
         session
-      (save-window-excursion (R) (rename-buffer (if (bufferp session) (buffer-name session)
-                                                  (if (stringp session) session (buffer-name)))) (current-buffer)))))
+      (save-window-excursion
+	(R)
+	(rename-buffer (if (bufferp session) (buffer-name session)
+			 (if (stringp session) session (buffer-name)))) (current-buffer)))))
 
 (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
 (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
@@ -113,7 +95,7 @@ write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.na
   "Pass BODY to the R process in BUFFER.  If RESULT-TYPE equals
 'output then return a list of the outputs of the statements in
 BODY, if RESULT-TYPE equals 'value then return the value of the
-last statement in BODY."
+last statement in BODY, as elisp."
   (if (not session)
       ;; external process evaluation
       (let ((in-tmp-file (make-temp-file "R-in-functional-results"))
@@ -121,38 +103,43 @@ last statement in BODY."
         (case result-type
           (output
            (with-temp-file in-tmp-file (insert body))
-           ;; (message "R --slave --no-save < '%s' > '%s'" in-tmp-file out-tmp-file)
-           (shell-command-to-string (format "R --slave --no-save < '%s' > '%s'" in-tmp-file out-tmp-file)))
+           (shell-command-to-string (format "R --slave --no-save < '%s' > '%s'"
+					    in-tmp-file out-tmp-file))
+	   (with-temp-buffer (insert-file-contents out-tmp-file) (buffer-string)))
           (value
            (with-temp-file in-tmp-file
              (insert (format org-babel-R-wrapper-method body out-tmp-file)))
-           ;; (message "R --no-save < '%s'" in-tmp-file)
-           (shell-command (format "R --no-save < '%s'" in-tmp-file))))
-        (with-temp-buffer (insert-file-contents out-tmp-file) (buffer-string)))
+           (shell-command (format "R --no-save < '%s'" in-tmp-file))
+	   (org-babel-import-elisp-from-file out-tmp-file))))
     ;; comint session evaluation
     (org-babel-comint-in-buffer buffer
       (let* ((tmp-file (make-temp-file "org-babel-R"))
              (last-value-eval
               (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
                       tmp-file))
-             (full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
+             (full-body (mapconcat #'org-babel-chomp
+				   (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
              (raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
                     (insert full-body) (inferior-ess-send-input)))
-             (results (let ((broke nil))
-                        (delete nil (mapcar (lambda (el)
-                                              (if (or broke
-                                                      (and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
-                                                  nil
-                                                (if (= (length el) 0)
-                                                    nil
-                                                  (if (string-match comint-prompt-regexp el)
-                                                      (substring el (match-end 0))
-                                                    el))))
-                                            (mapcar #'org-babel-trim raw))))))
+             (results
+	      (let ((broke nil))
+		(delete
+		 nil
+		 (mapcar (lambda (el)
+			   (if (or broke
+				   (and (string-match (regexp-quote org-babel-R-eoe-output)
+						      el) (setq broke t)))
+			       nil
+			     (if (= (length el) 0)
+				 nil
+			       (if (string-match comint-prompt-regexp el)
+				   (substring el (match-end 0))
+				 el))))
+			 (mapcar #'org-babel-trim raw))))))
         (case result-type
           (output (org-babel-trim (mapconcat #'identity results "\n")))
-          (value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
-          (t (reverse results)))))))
+          (value (org-babel-import-elisp-from-file tmp-file)))))))
+
 
 (provide 'org-babel-R)
 ;;; org-babel-R.el ends here

+ 1 - 1
lisp/langs/org-babel-ditaa.el

@@ -45,7 +45,7 @@
 
 (add-to-list 'org-babel-tangle-langs '("ditaa" "ditaa"))
 
-(defvar org-babel-default-header-args:ditaa '((:results . "file"))
+(defvar org-babel-default-header-args:ditaa '((:results . "file") (:exports . "results"))
   "Default arguments to use when evaluating a ditaa source block.")
 
 (defun org-babel-execute:ditaa (body params)

+ 1 - 1
lisp/langs/org-babel-gnuplot.el

@@ -41,7 +41,7 @@
 
 (add-to-list 'org-babel-tangle-langs '("gnuplot" "gnuplot"))
 
-(defvar org-babel-default-header-args:gnuplot '((:results . "file"))
+(defvar org-babel-default-header-args:gnuplot '((:results . "file") (:exports . "results"))
   "Default arguments to use when evaluating a gnuplot source block.")
 
 (defvar org-babel-gnuplot-timestamp-fmt nil)

+ 4 - 7
lisp/langs/org-babel-lisp.el

@@ -37,15 +37,12 @@
 
 (defun org-babel-execute:emacs-lisp (body params)
   "Execute a block of emacs-lisp code with org-babel.  This
-function is called by `org-babel-execute-src-block'."
+function is called by `org-babel-execute-src-block' via multiple-value-bind."
   (message "executing emacs-lisp code block...")
   (save-window-excursion
-    (let ((vars (org-babel-ref-variables params))
-          (print-level nil) (print-length nil) results)
-      (setq results
-            (eval `(let ,(mapcar (lambda (var) `(,(car var) ',(cdr var))) vars)
-                     ,(read (concat "(progn " body ")")))))
-      results)))
+    (let ((print-level nil) (print-length nil))
+      (eval `(let ,(mapcar (lambda (var) `(,(car var) ',(cdr var))) vars)
+	       ,(read (concat "(progn " body ")")))))))
 
 (provide 'org-babel-lisp)
 ;;; org-babel-lisp.el ends here

+ 34 - 43
lisp/langs/org-babel-python.el

@@ -38,32 +38,17 @@
 
 (defun org-babel-execute:python (body params)
   "Execute a block of Python code with org-babel.  This function is
-called by `org-babel-execute-src-block'."
+called by `org-babel-execute-src-block' via multiple-value-bind."
   (message "executing Python source code block")
-  (let* ((vars (org-babel-ref-variables params))
-         (result-params (split-string (or (cdr (assoc :results params)) "")))
-         (result-type (cond ((member "output" result-params) 'output)
-                            ((member "value" result-params) 'value)
-                            (t 'value)))
-         (full-body (concat
-                     (mapconcat ;; define any variables
-                      (lambda (pair)
-                        (format "%s=%s"
-                                (car pair)
-                                (org-babel-python-var-to-python (cdr pair))))
-                      vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
-         (session (org-babel-python-initiate-session (cdr (assoc :session params))))
-         (results (org-babel-python-evaluate session full-body result-type)))
-    (if (member "scalar" result-params)
-        results
-      (setq results (case result-type ;; process results based on the result-type
-                      ('output (let ((tmp-file (make-temp-file "org-babel-python")))
-                                 (with-temp-file tmp-file (insert results))
-                                 (org-babel-import-elisp-from-file tmp-file)))
-                      ('value (org-babel-python-table-or-results results))))
-      (if (and (member "vector" results) (not (listp results)))
-          (list (list results))
-        results))))
+  (let ((full-body (concat
+		    (mapconcat ;; define any variables
+		     (lambda (pair)
+		       (format "%s=%s"
+			       (car pair)
+			       (org-babel-python-var-to-python (cdr pair))))
+		     vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
+	(session (org-babel-python-initiate-session session)))
+    (org-babel-python-evaluate session full-body result-type)))
 
 (defun org-babel-prep-session:python (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
@@ -89,7 +74,7 @@ specifying a var of the same value."
       (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]")
     (format "%S" var)))
 
-(defun org-babel-python-table-or-results (results)
+(defun org-babel-python-table-or-string (results)
   "If the results look like a table, then convert them into an
 Emacs-lisp table, otherwise return the results as a string."
   (org-babel-read
@@ -114,7 +99,8 @@ then create.  Return the initialized session."
     (let* ((session (if session (intern session) :default))
            (python-buffer (org-babel-python-session-buffer session)))
       (run-python)
-      (setq org-babel-python-buffers (cons (cons session python-buffer) (assq-delete-all session org-babel-python-buffers)))
+      (setq org-babel-python-buffers (cons (cons session python-buffer)
+					   (assq-delete-all session org-babel-python-buffers)))
       session)))
 
 (defun org-babel-python-initiate-session (&optional session)
@@ -136,7 +122,7 @@ open('%s', 'w').write( str(main()) )")
   "Pass BODY to the Python process in BUFFER.  If RESULT-TYPE equals
 'output then return a list of the outputs of the statements in
 BODY, if RESULT-TYPE equals 'value then return the value of the
-last statement in BODY."
+last statement in BODY, as elisp."
   (if (not session)
       ;; external process evaluation
       (save-window-excursion
@@ -150,21 +136,27 @@ last statement in BODY."
           (value
            (let ((tmp-file (make-temp-file "python-functional-results")))
              (with-temp-buffer
-               (insert (format org-babel-python-wrapper-method
-                               (let ((lines (split-string (org-remove-indentation (org-babel-trim body)) "[\r\n]")))
-                                 (concat
-                                  (mapconcat
-                                   (lambda (line) (format "\t%s" line))
-                                   (butlast lines) "\n")
-                                  (format "\n\treturn %s" (last lines))))
-                               tmp-file))
+               (insert
+		(format
+		 org-babel-python-wrapper-method
+		 (let ((lines (split-string
+			       (org-remove-indentation (org-babel-trim body)) "[\r\n]")))
+		   (concat
+		    (mapconcat
+		     (lambda (line) (format "\t%s" line))
+		     (butlast lines) "\n")
+		    (format "\n\treturn %s" (last lines))))
+		 tmp-file))
                ;; (message "buffer=%s" (buffer-string)) ;; debugging
                (shell-command-on-region (point-min) (point-max) "python"))
-             (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))))
+             (org-babel-python-table-or-string
+	      (with-temp-buffer (insert-file-contents tmp-file) (buffer-string)))))))
     ;; comint session evaluation
     (org-babel-comint-in-buffer buffer
-      (let* ((full-body (mapconcat #'org-babel-trim
-                                   (list body org-babel-python-last-value-eval org-babel-python-eoe-indicator) "\n"))
+      (let* ((full-body
+	      (mapconcat 
+	       #'org-babel-trim
+	       (list body org-babel-python-last-value-eval org-babel-python-eoe-indicator) "\n"))
              (raw (org-babel-comint-with-output buffer org-babel-python-eoe-indicator t
                     ;; for some reason python is fussy, and likes enters after every input
                     (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
@@ -173,10 +165,9 @@ last statement in BODY."
                               (cdr (member org-babel-python-eoe-indicator
                                            (reverse (mapcar #'org-babel-trim raw)))))))
         (setq results (mapcar #'org-babel-python-read-string results))
-        (org-babel-trim (case result-type
-                          (output (mapconcat #'identity (reverse (cdr results)) "\n"))
-                          (value (car results))
-                          (t (reverse results))))))))
+        (case result-type
+	  (output (org-babel-trim (mapconcat #'identity (reverse (cdr results)) "\n")))
+	  (value (org-babel-python-table-or-string (org-babel-trim (car results)))))))))
 
 (defun org-babel-python-read-string (string)
   "Strip 's from around ruby string"

+ 19 - 29
lisp/langs/org-babel-ruby.el

@@ -38,29 +38,17 @@
 
 (defun org-babel-execute:ruby (body params)
   "Execute a block of Ruby code with org-babel.  This function is
-called by `org-babel-execute-src-block'."
+called by `org-babel-execute-src-block' via multiple-value-bind."
   (message "executing Ruby source code block")
-  (let* ((vars (org-babel-ref-variables params))
-         (result-params (split-string (or (cdr (assoc :results params)) "")))
-         (result-type (cond ((member "output" result-params) 'output)
-                            ((member "value" result-params) 'value)
-                            (t 'value)))
-         (full-body (concat
-                     (mapconcat ;; define any variables
-                      (lambda (pair)
-                        (format "%s=%s"
-                                (car pair)
-                                (org-babel-ruby-var-to-ruby (cdr pair))))
-                      vars "\n") "\n" body "\n")) ;; then the source block body
-         (session (org-babel-ruby-initiate-session (cdr (assoc :session params))))
-         (results (org-babel-ruby-evaluate session full-body result-type)))
-    (if (member "scalar" result-params)
-        results
-      (case result-type ;; process results based on the result-type
-        ('output (let ((tmp-file (make-temp-file "org-babel-ruby")))
-                   (with-temp-file tmp-file (insert results))
-                   (org-babel-import-elisp-from-file tmp-file)))
-        ('value (org-babel-ruby-table-or-results results))))))
+  (let ((full-body (concat
+		    (mapconcat ;; define any variables
+		     (lambda (pair)
+		       (format "%s=%s"
+			       (car pair)
+			       (org-babel-ruby-var-to-ruby (cdr pair))))
+		     vars "\n") "\n" body "\n")) ;; then the source block body
+	(session (org-babel-ruby-initiate-session session)))
+    (org-babel-ruby-evaluate session full-body result-type)))
 
 (defun org-babel-prep-session:ruby (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
@@ -90,7 +78,7 @@ specifying a var of the same value."
       (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
     (format "%S" var)))
 
-(defun org-babel-ruby-table-or-results (results)
+(defun org-babel-ruby-table-or-string (results)
   "If the results look like a table, then convert them into an
 Emacs-lisp table, otherwise return the results as a string."
   (org-babel-read
@@ -130,7 +118,7 @@ File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.
   "Pass BODY to the Ruby process in BUFFER.  If RESULT-TYPE equals
 'output then return a list of the outputs of the statements in
 BODY, if RESULT-TYPE equals 'value then return the value of the
-last statement in BODY."
+last statement in BODY, as elisp."
   (if (not session)
       ;; external process evaluation
       (save-window-excursion
@@ -147,11 +135,13 @@ last statement in BODY."
                (insert (format org-babel-ruby-wrapper-method body tmp-file))
                ;; (message "buffer=%s" (buffer-string)) ;; debugging
                (shell-command-on-region (point-min) (point-max) "ruby"))
-             (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))))
+             (org-babel-ruby-table-or-string
+	      (with-temp-buffer (insert-file-contents tmp-file) (buffer-string)))))))
     ;; comint session evaluation
-    (message "session evaluation")
-    (let* ((full-body (mapconcat #'org-babel-chomp
-                                 (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
+    (let* ((full-body
+	    (mapconcat
+	     #'org-babel-chomp
+	     (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
            (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
                   (insert full-body) (comint-send-input nil t)))
            (results (cdr (member org-babel-ruby-eoe-indicator
@@ -159,7 +149,7 @@ last statement in BODY."
                                                   (mapcar #'org-babel-trim raw)))))))
       (case result-type
         (output (mapconcat #'identity (reverse (cdr results)) "\n"))
-        (value (car results))))))
+        (value (org-babel-ruby-table-or-string (car results)))))))
 
 (defun org-babel-ruby-read-string (string)
   "Strip \\\"s from around ruby string"

+ 18 - 23
lisp/langs/org-babel-sh.el

@@ -38,30 +38,17 @@
 
 (defun org-babel-execute:sh (body params)
   "Execute a block of Shell commands with org-babel.  This
-function is called by `org-babel-execute-src-block'."
+function is called by `org-babel-execute-src-block' via multiple-value-bind."
   (message "executing Shell source code block")
-  (let* ((vars (org-babel-ref-variables params))
-         (result-params (split-string (or (cdr (assoc :results params)) "")))
-         (result-type (cond ((member "output" result-params) 'output)
-                            ((member "value" result-params) 'value)
-                            (t 'value)))
-         (full-body (concat
+  (let* ((full-body (concat
                      (mapconcat ;; define any variables
                       (lambda (pair)
                         (format "%s=%s"
                                 (car pair)
                                 (org-babel-sh-var-to-sh (cdr pair))))
                       vars "\n") "\n" body "\n\n")) ;; then the source block body
-         (session (org-babel-sh-initiate-session (cdr (assoc :session params))))
-         (results (org-babel-sh-evaluate session full-body result-type)))
-    (if (member "scalar" result-params)
-        results
-      (setq results (let ((tmp-file (make-temp-file "org-babel-shell")))
-                      (with-temp-file tmp-file (insert results))
-                      (org-babel-import-elisp-from-file tmp-file)))
-      (if (and (member "vector" results) (not (listp results)))
-          (list (list results))
-        results))))
+         (session (org-babel-sh-initiate-session session)))
+    (org-babel-sh-evaluate session full-body result-type)))
 
 (defun org-babel-prep-session:sh (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
@@ -118,7 +105,8 @@ then create.  Return the initialized session."
       (when newp
         (setq sh-buffer (current-buffer))
         (org-babel-comint-wait-for-output sh-buffer))
-      (setq org-babel-sh-buffers (cons (cons session sh-buffer) (assq-delete-all session org-babel-sh-buffers)))
+      (setq org-babel-sh-buffers (cons (cons session sh-buffer)
+				       (assq-delete-all session org-babel-sh-buffers)))
       session)))
 
 (defun org-babel-sh-initiate-session (&optional session)
@@ -138,13 +126,20 @@ last statement in BODY."
   (if (not session)
       ;; external process evaluation
       (save-window-excursion
-        (with-temp-buffer ;; TODO: figure out how to return non-output values from shell scripts
+        (with-temp-buffer
           (insert body)
           ;; (message "buffer=%s" (buffer-string)) ;; debugging
           (shell-command-on-region (point-min) (point-max) "sh" 'replace)
-          (buffer-string)))
+	  (case result-type
+	    (output (buffer-string))
+	    (value ;; TODO: figure out how to return non-output values from shell scripts
+	     (let ((tmp-file (make-temp-file "org-babel-sh"))
+		   (results (buffer-string)))
+	       (with-temp-file tmp-file (insert results))
+	       (org-babel-import-elisp-from-file tmp-file))))))
     ;; comint session evaluation
-    (let* ((full-body (mapconcat #'org-babel-chomp
+    (let* ((tmp-file (make-temp-file "org-babel-sh"))
+	   (full-body (mapconcat #'org-babel-chomp
                                  (list body org-babel-sh-eoe-indicator) "\n"))
            (raw (org-babel-comint-with-output buffer org-babel-sh-eoe-output nil
                   (insert full-body) (comint-send-input nil t)))
@@ -154,8 +149,8 @@ last statement in BODY."
       ;; (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
       (or (case result-type
             (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
-            (value (car results))
-            (t (reverse results))) ""))))
+            (value (with-temp-file tmp-file (insert (car results)))
+		   (org-babel-import-elisp-from-file tmp-file)))) "")))
 
 (defun org-babel-sh-strip-weird-long-prompt (string)
   (while (string-match "^% +[\r\n$]+ *" string)

+ 6 - 3
lisp/org-babel-comint.el

@@ -71,8 +71,9 @@ during execution of body."
                ;; wait for end-of-evaluation indicator
                (while (progn
                         (goto-char comint-last-input-end)
-                        (not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
-                                                  (re-search-forward (regexp-quote ,eoe-indicator) nil t)))))
+                        (not (save-excursion
+			       (and (re-search-forward comint-prompt-regexp nil t)
+				    (re-search-forward (regexp-quote ,eoe-indicator) nil t)))))
                  (accept-process-output (get-buffer-process (current-buffer)))
                  ;; ;; thought this would allow async background running, but I was wrong...
                  ;; (run-with-timer .5 .5 'accept-process-output (get-buffer-process (current-buffer)))
@@ -80,7 +81,9 @@ during execution of body."
            ;; remove filter
            (remove-hook 'comint-output-filter-functions 'my-filt)))
        ;; remove echo'd FULL-BODY from input
-       (if (and ,remove-echo (string-match (replace-regexp-in-string "\n" "\r\n" (regexp-quote ,full-body)) string-buffer))
+       (if (and ,remove-echo
+		(string-match
+		 (replace-regexp-in-string "\n" "\r\n" (regexp-quote ,full-body)) string-buffer))
            (setq raw (substring string-buffer (match-end 0))))
        (split-string string-buffer comint-prompt-regexp))))
 

+ 5 - 1
lisp/org-babel-exp.el

@@ -84,8 +84,12 @@ options and are taken from `org-babel-defualt-inline-header-args'."
             (if (string-match "\n$" body) "" "\n"))))
 
 (defun org-babel-exp-results (body lang params &optional inline)
+  ;; I expect there's a good reason why not, but would it be possible
+  ;; to use org-babel-execute-src-block here? [ded]
   (let* ((cmd (intern (concat "org-babel-execute:" lang)))
-         (result (funcall cmd body params))
+         (result
+	  (multiple-value-bind (session vars result-params result-type)
+	      (org-babel-process-params params) (funcall cmd body params)))
          (result-as-org (org-babel-result-to-org-string result)))
     (if inline
         (format "=%s=" result)

+ 19 - 10
lisp/org-babel-lob.el

@@ -57,12 +57,13 @@ add files to this list use the `org-babel-lob-ingest' command."
                     (assq-delete-all source-name org-babel-library-of-babel)))))))
 
 (org-babel-lob-ingest ;; actually add the source-blocks defined in library-of-babel.org
- (expand-file-name "library-of-babel.org"
-                   (expand-file-name ".." (file-name-directory (or load-file-name buffer-file-name)))))
+ (expand-file-name
+  "library-of-babel.org"
+  (expand-file-name ".." (file-name-directory (or load-file-name buffer-file-name)))))
 
 ;; functions for executing lob one-liners
 
-(defvar org-babel-lob-one-liner-regexp "#\\+lob:[ \t]+\\([^\n]+\\)\n")
+(defvar org-babel-lob-one-liner-regexp "#\\+lob:[ \t]+\\([^\(\)\n]+\\)\(\\([^\n]+\\)\)[ \t]*\n")
 
 (defun org-babel-lob-execute-maybe ()
   "Detect if this is context for a org-babel Library Of Babel
@@ -75,19 +76,27 @@ the Library."
 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-lob-execute-maybe)
 
 (defun org-babel-lob-get-info ()
-  "Return the information of the current Library of Babel line as
-a list of the following form.
-
-  (source-block-name header-arguments-alist)"
+  "Return the function call supplied on the current Library of
+Babel line as a string.
+
+This function is analogous to org-babel-get-src-block-name. For
+both functions, after they are called, (match-string 1) matches
+the function name, and (match-string 2) matches the function
+arguments inside the parentheses. I think perhaps these functions
+should be renamed to bring out this similarity, perhaps involving
+the word 'call'."
   (let ((case-fold-search t))
     (save-excursion
       (move-beginning-of-line 1)
       (if (looking-at org-babel-lob-one-liner-regexp)
-          (org-babel-clean-text-properties (match-string 1))))))
+          (org-babel-clean-text-properties 
+	   (format "%s(%s)" (match-string 1) (match-string 2)))))))
 
 (defun org-babel-lob-execute (info)
-  (let ((params (org-babel-parse-header-arguments (concat ":var results=" info))))
-    (org-babel-execute-src-block t (list "emacs-lisp" "results" params))))
+  (let ((params (org-babel-merge-params
+		 org-babel-default-header-args
+		 (org-babel-parse-header-arguments (concat ":var results=" info)))))
+    (org-babel-execute-src-block nil (list "emacs-lisp" "results" params))))
 
 (provide 'org-babel-lob)
 ;;; org-babel-lob.el ends here

+ 6 - 5
lisp/org-babel-ref.el

@@ -68,7 +68,8 @@ and find it's value using `org-babel-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 "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*" assignment)
+  (if (string-match
+       "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*" assignment)
       (let ((var (match-string 1 assignment))
             (ref (match-string 2 assignment)))
         (cons (intern var)
@@ -87,7 +88,7 @@ return nil."
       out)))
 
 (defun org-babel-ref-resolve-reference (ref)
-  "Resolve the reference and return it's value"
+  "Resolve the reference and return its value"
   (save-excursion
     (let ((case-fold-search t)
           type args new-refere new-referent result lob-info)
@@ -108,14 +109,14 @@ return nil."
       (if (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*"
                                        (regexp-quote ref) "[ \t]*$"))
                 (regexp (concat "^#\\+SRCNAME:[ \t]*"
-                                (regexp-quote ref) "[ \t]*$")))
+                                (regexp-quote ref) "\\(\(.*\)\\)?" "[ \t]*$")))
             (or (re-search-forward result_regexp nil t)
                 (re-search-forward result_regexp nil t)
                 (re-search-forward regexp nil t)
                 (re-search-backward regexp nil t)
                 ;; check the Library of Babel
                 (setq lob-info (cdr (assoc (intern ref) org-babel-library-of-babel)))))
-          (goto-char (match-beginning 0))
+          (unless lob-info (goto-char (match-beginning 0)))
         ;; ;; TODO: allow searching for names in other buffers
         ;; (setq id-loc (org-id-find ref 'marker)
         ;;       buffer (marker-buffer id-loc)
@@ -140,7 +141,7 @@ return nil."
         ('lob (setq result (org-babel-execute-src-block t lob-info args)))))))
 
 (defun org-babel-ref-at-ref-p ()
-  "Return the type of reference located at point or nil of none
+  "Return the type of reference located at point or nil if none
 of the supported reference types are found.  Supported reference
 types are tables and source blocks."
   (cond ((org-at-table-p) 'table)

+ 11 - 10
lisp/org-babel-table.el

@@ -63,8 +63,8 @@
 (defmacro sbe (source-block &rest variables)
   "Return the results of calling SOURCE-BLOCK with all assigning
 every variable in VARIABLES.  Each element of VARIABLES should be
-a two element list, who's first element is the name of the
-variable and second element is a string of it's value.  The
+a two 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.
 
@@ -76,14 +76,15 @@ results
   (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 ", ")
-                                     ")")))))
+       (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 t (list "emacs-lisp" "results" params)))
      "")))
 

+ 155 - 52
lisp/org-babel.el

@@ -49,10 +49,10 @@ then run `org-babel-pop-to-session'."
 
 (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
 
-(defvar org-babel-default-header-args '((:session . "none"))
+(defvar org-babel-default-header-args '((:session . "none") (:results . "replace"))
   "Default arguments to use when evaluating a source block.")
 
-(defvar org-babel-default-inline-header-args '((:results . "silent") (:exports . "results"))
+(defvar org-babel-default-inline-header-args '((:results . "silent") (:exports . "code"))
   "Default arguments to use when evaluating an inline source block.")
 
 (defvar org-babel-src-block-regexp nil
@@ -100,7 +100,6 @@ sh         Pass command to the shell and display the result
 perl       The perl interpreter
 python     The python interpreter
 ruby       The ruby interpreter
-babel      A degenerate source block (no body) to implement library-of-babel calls
 
 The source block regexp `org-babel-src-block-regexp' is updated
 when a new interpreter is added to this list through the
@@ -114,8 +113,7 @@ lisp code use the `org-babel-add-interpreter' function."
               (const "sh")
 	      (const "perl")
 	      (const "python")
-	      (const "ruby")
-  	      (const "babel")))
+	      (const "ruby")))
 
 ;;; functions
 (defun org-babel-pop-to-session (&optional arg info)
@@ -156,48 +154,78 @@ the header arguments specified at the source code block."
   (let* ((info (or info (org-babel-get-src-block-info)))
          (lang (first info))
          (body (second info))
-         (params (org-combine-plists params (third info)))
+         (params (org-babel-merge-params
+		  (third info) (org-babel-get-src-block-function-args) params))
+	 (processed-params (org-babel-process-params params))
+	 (result-params (third processed-params))
+	 (result-type (fourth processed-params))
          (cmd (intern (concat "org-babel-execute:" lang)))
          result)
     ;; (message "params=%S" params) ;; debugging statement
     (unless (member lang org-babel-interpreters)
       (error "Language is not in `org-babel-interpreters': %s" lang))
-    (setq result (funcall cmd body params))
-    ;; possibly force result into a vector
-    (if (and (not (listp result)) (cdr (assoc :results params))
-             (member "vector" (split-string (cdr (assoc :results params)))))
-        (setq result (list result)))
-    (if arg
-        (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
-      (org-babel-insert-result result (cdr (assoc :results params))))
-    result))
-
-(defun org-babel-eval-buffer (&optional arg)
+    (when arg (setq result-params (cons "silent" result-params)))
+    (setq result (multiple-value-bind (session vars result-params result-type) processed-params
+		   (funcall cmd body params)))
+    (if (eq result-type 'value)
+	(setq result (org-babel-process-result result result-params)))
+    (org-babel-insert-result result result-params)
+    (case result-type (output nil) (value result))))
+
+(defun org-babel-process-result (result result-params)
+  "Process returned value for insertion in buffer.
+
+Currently, this function forces to table output if :results
+vector has been supplied.
+
+  You can see below the various fragments of results-processing
+code that were present in the language-specific files. Out of
+those fragments, I've moved the org-babel-python-table-or-results
+and org-babel-import-elisp-from-file functionality into the
+org-babel-*-evaluate functions. I think those should only be used
+in the :results value case, as in the 'output case we are not
+concerned with creating elisp versions of results. "
+
+  (if (and (member "vector" result-params) (not (listp result)))
+      (list (list result))
+         result))
+
+(defun org-babel-execute-buffer (&optional arg)
   "Replace EVAL snippets in the entire buffer."
   (interactive "P")
   (save-excursion
     (goto-char (point-min))
-    (while (re-search-forward org-babel-regexp nil t)
-      (org-babel-eval-src-block arg))))
+    (while (re-search-forward org-babel-src-block-regexp nil t)
+      (goto-char (match-beginning 0))
+      (org-babel-execute-src-block arg)
+      (goto-char (match-end 0)))))
 
-(defun org-babel-eval-subtree (&optional arg)
+(defun org-babel-execute-subtree (&optional arg)
   "Replace EVAL snippets in the entire subtree."
   (interactive "P")
   (save-excursion
     (org-narrow-to-subtree)
-    (org-babel-eval-buffer)
+    (org-babel-execute-buffer)
     (widen)))
 
 (defun org-babel-get-src-block-name ()
-  "Return the name of the current source block if one exists"
+  "Return the name of the current source block if one exists.
+
+This function is analogous to org-babel-lob-get-info. For both
+functions, after they are called, (match-string 1) matches the
+function name, and (match-string 2) matches the function
+arguments inside the parentheses. I think perhaps these functions
+should be renamed to bring out this similarity, perhaps involving
+the word 'call'."
   (let ((case-fold-search t)
 	(head (org-babel-where-is-src-block-head)))
-    (save-excursion
-      (when head
-	(goto-char head)
-	(if (save-excursion (forward-line -1)
-			    (looking-at "#\\+srcname:[ \f\t\n\r\v]*\\([^ \f\t\n\r\v]+\\)"))
-	    (org-babel-clean-text-properties (match-string 1)))))))
+    (if head
+	(save-excursion
+	  (goto-char head)
+	  (if (save-excursion
+		(forward-line -1)
+		(looking-at "#\\+srcname:[ \f\t\n\r\v]*\\([^ \f\t\n\r\v]+\\)\(\\(.*\\)\)"))
+	      (org-babel-clean-text-properties (match-string 1)))))))
 
 (defun org-babel-get-src-block-info ()
   "Return the information of the current source block as a list
@@ -212,6 +240,12 @@ of the following form.  (language body header-arguments-alist)"
           (org-babel-parse-inline-src-block-match)
         nil)))) ;; indicate that no source block was found
 
+(defun org-babel-get-src-block-function-args ()
+  (when (org-babel-get-src-block-name)
+    (mapcar (lambda (ref) (cons :var ref))
+	    (split-string (org-babel-clean-text-properties (match-string 2))
+			  ",[ \f\t\n\r\v]*"))))
+
 (defmacro org-babel-map-source-blocks (file &rest body)
   "Evaluate BODY forms on each source-block in FILE."
   (declare (indent 1))
@@ -225,31 +259,48 @@ of the following form.  (language body header-arguments-alist)"
 (defun org-babel-parse-src-block-match ()
   (let* ((lang (org-babel-clean-text-properties (match-string 1)))
          (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
-    (list (org-babel-clean-text-properties (match-string 1))
-        (org-babel-strip-protective-comas (org-babel-clean-text-properties (match-string 4)))
-        (org-combine-plists
-         org-babel-default-header-args
-         (if (boundp lang-headers) (eval lang-headers) nil)
-         (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) "")))))))
+    (list lang
+	  (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 4)))
+	  (org-babel-merge-params
+	   org-babel-default-header-args
+	   (if (boundp lang-headers) (eval lang-headers) nil)
+	   (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) "")))))))
 
 (defun org-babel-parse-inline-src-block-match ()
   (let* ((lang (org-babel-clean-text-properties (match-string 1)))
          (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
     (list lang
-          (org-babel-strip-protective-comas (org-babel-clean-text-properties (match-string 4)))
-          (org-combine-plists
+          (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 4)))
+          (org-babel-merge-params
            org-babel-default-inline-header-args
            (if (boundp lang-headers) (eval lang-headers) nil)
            (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) "")))))))
 
 (defun org-babel-parse-header-arguments (arg-string)
   "Parse a string of header arguments returning an alist."
-  (delq nil
-        (mapcar
-         (lambda (arg) (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" arg)
-                           (cons (intern (concat ":" (match-string 1 arg))) (org-babel-chomp (match-string 2 arg)))
-                         (cons (intern (concat ":" arg)) nil)))
-         (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t))))
+  (if (> (length arg-string) 0)
+      (delq nil
+	    (mapcar
+	     (lambda (arg)
+	       (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" arg)
+		   (cons (intern (concat ":" (match-string 1 arg)))
+			 (org-babel-chomp (match-string 2 arg)))
+		 (cons (intern (concat ":" arg)) nil)))
+	     (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
+
+(defun org-babel-process-params (params)
+  "Parse params and resolve references.
+
+Return a list (session vars result-params result-type). These are
+made available to the org-babel-execute:LANG functions via
+multiple-value-bind."
+  (let* ((session (cdr (assoc :session params)))
+	 (vars (org-babel-ref-variables params))
+	 (result-params (split-string (or (cdr (assoc :results params)) "")))
+	 (result-type (cond ((member "output" result-params) 'output)
+			    ((member "value" result-params) 'value)
+			    (t 'value))))
+    (list session vars result-params result-type)))
 
 (defun org-babel-where-is-src-block-head ()
   "Return the point at the beginning of the current source
@@ -310,11 +361,13 @@ source block.  Specifically at the beginning of the #+RESNAME:
 line.  If no result exists for this block then create a
 #+RESNAME: line following the source block."
   (save-excursion
-    (let ((name (org-babel-get-src-block-name)) 
-	  (head (org-babel-where-is-src-block-head)) end)
+    (let* ((on-lob-line (progn (beginning-of-line 1)
+			       (looking-at org-babel-lob-one-liner-regexp)))
+	   (name (if on-lob-line (org-babel-lob-get-info) (org-babel-get-src-block-name)))
+	   (head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
       (when head (goto-char head))
       (or (and name (message name) (org-babel-find-named-result name))
-          (and (re-search-forward "#\\+end_src" nil t)
+          (and (or on-lob-line (re-search-forward "#\\+end_src" nil t))
                (progn (move-end-of-line 1)
 		      (if (eobp) (insert "\n") (forward-char 1))
 		      (setq end (point))
@@ -333,22 +386,24 @@ current source block.  With optional argument INSERT controls
 insertion of results in the org-mode file.  INSERT can take the
 following values...
 
-t ------ the default options, simply insert the results after the
+t ------ the default option, simply insert the results after the
          source block
-         
+
 replace - insert results after the source block replacing any
           previously inserted results
 
 silent -- no results are inserted"
-  (if insert (setq insert (split-string insert)))
   (if (stringp result)
       (progn
         (setq result (org-babel-clean-text-properties result))
         (if (member "file" insert) (setq result (org-babel-result-to-file result))))
     (unless (listp result) (setq result (format "%S" result))))
-  (if (and insert (member "replace" insert)) (org-babel-remove-result))
+  (if (and insert (member "replace" insert) (not (member "silent" insert)))
+      (org-babel-remove-result))
   (if (= (length result) 0)
-      (message "no result returned by source block")
+      (if (member "value" result-params)
+	  (message "No result returned by source block")
+	(message "Source block produced no output"))
     (if (and insert (member "silent" insert))
         (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result)
       (when (and (stringp result) ;; ensure results end in a newline
@@ -416,12 +471,60 @@ non-nil."
         (dotimes (n size)
           (move-beginning-of-line 1) (insert ": ") (forward-line 1))))))
 
+(defun org-babel-merge-params (&rest plists)
+  "Combine all parameter association lists in PLISTS.  Later
+elements of PLISTS override the values of previous element.  This
+takes into account some special considerations for certain
+parameters when merging lists."
+  (let (params results vars var ref)
+    (mapc (lambda (plist)
+	    (mapc (lambda (pair)
+		    (case (car pair)
+		      (:var
+		       ;; we want only one specification per variable
+		       (when (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*=[ \t]*\\([^\f\n\r\v]+\\)$" (cdr pair))
+			 ;; TODO: When is this not true?
+			 (setq var (intern (match-string 1 (cdr pair)))
+			       ref (match-string 2 (cdr pair))
+			       vars (cons (cons var ref) (assq-delete-all var vars)))))
+		      (:results
+		       ;; maintain list of unique :results specifications
+		       (setq results (org-babel-merge-results results (split-string (cdr pair)))))
+		      (t
+		       ;; replace: this covers e.g. :session
+		       (setq params (cons pair (assq-delete-all	(car pair) params))))))
+		  plist))
+	  plists)
+    (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
+    (while vars (setq params (cons (cons :var (pop vars)) params)))
+    (cons (cons :results (mapconcat 'identity results " ")) params)))
+
+(defun org-babel-merge-results (&rest result-params)
+  "Combine all result parameter lists in RESULT-PARAMS taking
+into account the fact that some groups of result params are
+mutually exclusive."
+  (let ((exclusive-groups '(("file" "vector" "scalar")
+                            ("replace" "silent")))
+        output)
+    (mapc (lambda (new-params)
+            (mapc (lambda (new-param)
+                    (mapc (lambda (exclusive-group)
+                            (when (member new-param exclusive-group)
+                              (mapcar (lambda (excluded-param)
+                                        (setq output (delete excluded-param output)))
+                                      exclusive-group)))
+                          exclusive-groups)
+                    (setq output (org-uniquify (cons new-param output))))
+                  new-params))
+          result-params)
+    output))
+
 (defun org-babel-clean-text-properties (text)
   "Strip all properties from text return."
   (set-text-properties 0 (length text) nil text) text)
 
-(defun org-babel-strip-protective-comas (body)
-  "Strip protective comas from bodies of source blocks."
+(defun org-babel-strip-protective-commas (body)
+  "Strip protective commas from bodies of source blocks."
   (replace-regexp-in-string "^,#" "#" body))
 
 (defun org-babel-read (cell)

+ 119 - 0
org-babel-ded-worg.org

@@ -0,0 +1,119 @@
+#+OPTIONS:    H:3 num:nil toc:2 \n:nil @:t ::t |:t ^:t -:t f:t *:t TeX:t LaTeX:t skip:nil d:(HIDE) tags:not-in-toc
+#+STARTUP:    align fold nodlcheck hidestars oddeven lognotestate
+#+SEQ_TODO:   TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@)
+#+TAGS:       Write(w) Update(u) Fix(f) Check(c) 
+#+TITLE:      org-babel: execution of source code blocks in org-mode
+#+AUTHOR:     Dan Davison
+#+EMAIL:      davison at stats dot ox dot ac dot uk
+#+LANGUAGE:   en
+#+CATEGORY:   worg
+
+* Introduction
+  Org-babel provides the following modifications to [[http://orgmode.org/manual/Literal-examples.html][the existing
+  support]] for blocks of source code examples in the org-mode core.
+  1. source code execution
+  2. arguments to source code blocks
+  
+* Basic org-babel functionality
+*** Source code execution
+    For interpreted languages such as shell, python, R, etc, org-babel
+    allows source blocks to be executed: the code is passed to the
+    interpreter and you have control over what is done with the
+    results of excecution. E.g. place point anywhere in the following
+    block and use C-c C-c to run the code:
+
+#+begin_src python :results output
+import time
+x = 4
+print("hello\n")
+#print time.ctime()
+print [5, 10]
+#+end_src
+
+#+resname:
+: hello
+: 510
+
+#+begin_src R :results value
+x = 4
+date()
+c(5, 10)
+#+end_src
+
+#+resname:
+|  5 |
+| 10 |
+
+
+
+
+
+
+
+
+
+
+
+*** What happens to the results?
+    Org-babel provides two fundamentally different modes for capturing
+    the results of code evaluation, specified by the :results header
+    argument:
+**** :results value
+     This means that the 'result' of code evaluation is defined to be
+     the *value* of the last statement in the block. Thus with this
+     setting, one can view the code block as a function with a return
+     value. And not only can one view it that way, but you can
+     actually use the return value of one source block as input for
+     another (see later). This setting is the default.
+**** :results output
+     With this setting, org-babel captures all the text output of the
+     code block and places it in the org buffer. One can think of this
+     as a 'scripting' mode: the code block contains a series of
+     commands, and you get the output of all the commands. Unlike in
+     the 'functional' mode specified by =:results value=, the code
+     block has no return value. (This mode will be familiar to Sweave
+     users).
+**** Additional :results settings
+     
+
+     
+*** Arguments to source code blocks
+    In addition to evaluation of code blocks, org-babel allows them to
+    be parameterised (i.e. have arguments). Thus source code blocks
+    now have the status of *functions*.
+
+    
+*** Internals
+    For those interested in hacking org-babel, it's worth going
+    through what actually happened there:
+***** org-babel-execute-src
+      1. parses source block info (recognises language, looks for
+	 arguments (there aren't any))
+      2. calls
+***** org-babel-execute:LANG
+      1. resolves referenced variables (there aren't any)
+      2. assigns any referenced variables and evaluates body
+***** org-babel-LANG-evaluate
+      Returns a string corresponding to either output or value of block.
+
+#+resname:
+: Sun Jul  5 14:17:31 EDT 2009
+
+
+#+begin_src R :results output
+    date()
+#+end_src
+
+#+resname:
+: Sun Jul  5 14:00:20 2009
+
+
+#+begin_src python
+    import time
+    time.ctime()
+#+end_src
+    
+#+resname:
+: Sun Jul  5 14:13:07 2009
+
+    

+ 119 - 22
org-babel.org

@@ -199,7 +199,16 @@ would then be [[#sandbox][the sandbox]].
 #+end_src
 
 
-* Tasks [29/45]
+* Tasks [30/49]
+** TODO make tangle files read-only
+   With a file-local variable setting, yea that makes sense.  Maybe
+   the header should reference the related org-mode file.
+
+** TODO fold source blocks on #+srcname line
+   I.e., as well as on #+begin_src line.
+** TODO take default values for header args from properties
+   Use file-wide and subtree wide properties to set default values for
+   header args.
 ** TODO support for working with =*Org Edit Src Example*= buffers [2/4]
 *** TODO optionally evaluate header references when we switch to =*Org Edit Src*= buffer
 That seems to imply that the header references need to be evaluated
@@ -593,7 +602,6 @@ Another example is in the [[*operations%20in%20on%20tables][grades example]].
 *** Current design
     This is covered by the [[file:library-of-babel.org][Library of Babel]], which will contain
     ready-made source blocks designed to carry out useful common tasks.
-
 ** PROPOSED Are we happy with current behaviour regarding vector/scalar output?
 This simple example of multilingual chaining produces vector output if
 there are spaces in the message and scalar otherwise.
@@ -843,6 +851,7 @@ $0
 [[file:snippets/org-mode/sb][sb -- snippet]]
 
 waiting for guidance from those more familiar with yasnippets
+** DONE LoB: allow output in buffer
 ** DONE allow default header arguments by language
 org-babel-default-header-args:lang-name
 
@@ -882,7 +891,6 @@ This should check to see if there is any need to re-export
 | "test-tangle.el" |
 
 *** DONE only tangle the file if it's actually necessary
-compare the ages of the files
 ** DONE add a function to jump to a source-block by name
    I've had an initial stab at that in org-babel-find-named-block
    (library-of-babel branch).
@@ -1976,7 +1984,7 @@ This could probably be added to [[file:lisp/org-babel-script.el][org-babel-scrip
 +----+----+
 #+end_src
 
-#+resname: implementing-ditaa
+#+resname:
 [[file:blue.png][blue.png]]
 
 *** STARTED gnuplot
@@ -2036,7 +2044,6 @@ for example
 
 #+resname: this-doesn't-match-orgtbl
 
-
 ** TODO collapsing consecutive newlines in string output
 
 #+srcname: multi-line-string-output
@@ -2051,7 +2058,6 @@ even a third"
 
 #+resname: multi-line-string-output
 
-
 ** TODO cursor movement when evaluating source blocks
    E.g. the pie chart example. Despite the save-window-excursion in
    org-babel-execute:R. (I never learned how to do this properly: org-R
@@ -2079,6 +2085,43 @@ identical: output results in raw stdout contents, whereas value
 converts it to elisp, perhaps to a table if it looks tabular. This is
 the same for the other languages. [Dan]
 
+** TODO are the org-babel-trim s necessary?
+   at the end of e.g. org-babel-R-evaluate, org-babel-python-evaluate, but
+   not org-babel-ruby-evaluate
+** results branch bugs
+*** TODO elisp reference fails for literal number
+#+srcname: elisp-test(a=4)
+#+begin_src emacs-lisp 
+(message a)
+#+end_src
+*** TODO use new merge function [[file:lisp/org-babel-ref.el::t%20nil%20org%20combine%20plists%20args%20nil][here]] and [[file:lisp/org-babel.el::params%20org%20combine%20plists%20params%20third%20info][here]]?
+    And at other occurrences of org-combine-plists?
+*** TODO LoB: with output to buffer, not working in buffers other than library-of-babel.org
+    I haven't fixed this yet. org-babel-ref-resolve-reference moves
+    point around, inside a save-excursion. Somehow when it comes to
+    inserting the results (after possible further recursive calls to
+    org-babel-ref-resolve-reference), point hasn't gone back to the
+    lob line.
+*** TODO LoB: output to buffer adds creeping blank lines
+   Compare the results of
+#+lob: python-add(a=5, b=17)
+
+#+resname: python-add(a=5, b=17)
+: 22
+--------------------------------
+
+#+begin_src python
+23
+#+end_src
+
+#+resname:
+: 23
+---------------------
+
+   Hmm, it's a bit confusing. I think it's to do with the fact that
+   LoB removes the entire (#+resname and result) and starts from
+   scratch, whereas #+begin_src only removes the result. I haven't
+   worked out what the correct fix is yet.
 ** DEFERRED weird escaped characters in shell prompt break shell evaluation
    E.g. this doesn't work. Should the shell sessions set a sane prompt
    when they start up? Or is it a question of altering
@@ -2107,6 +2150,31 @@ the same for the other languages. [Dan]
    the user's regular emacs init.  I can't think of a way for us to
    set this automatically, and we are SOL without a regexp to match
    the prompt.
+
+** DONE LoB: calls fail if reference has single character name
+   commit 21d058869df1ff23f4f8cc26f63045ac9c0190e2
+**** This doesn't work
+#+lob: R-plot(data=X)
+
+#+tblname: X
+| 1 |     1 |
+| 2 |    .5 |
+| 3 | .3333 |
+| 4 |   .25 |
+| 5 |    .2 |
+| 6 | .1666 |
+
+**** But this is OK
+#+tblname: XX
+| 1 |     1 |
+| 2 |    .5 |
+| 3 | .3333 |
+| 4 |   .25 |
+| 5 |    .2 |
+| 6 | .1666 |
+
+#+lob: R-plot(data=XX)
+
 ** DONE make :results replace the default?
    I'm tending to think that appending results to pre-existing results
    creates mess, and that the cleaner `replace' option should be the
@@ -2114,6 +2182,7 @@ the same for the other languages. [Dan]
    that to be updated, rather than have a new one be added.
    
    I agree.
+
 ** DONE ruby evaluation not working under ubuntu emacs 23
    With emacs 23.0.91.1 on ubuntu, for C-h f run-ruby I have the
    following, which seems to conflict with [[file:lisp/langs/org-babel-ruby.el::let%20session%20buffer%20save%20window%20excursion%20run%20ruby%20nil%20session%20current%20buffer][this line]] in org-babel-ruby.el.
@@ -2255,19 +2324,13 @@ tables
 "schulte"
 #+end_src
 
+
 #+begin_src R :var num=little-fake
 num
 #+end_src
 
 #+resname:
 : schulte
-: 11
-: 11
-: 11
-: schulte
-: 9
-: 9
-: 11
 
 #+srcname: set-debug-on-error
 #+begin_src emacs-lisp :results silent
@@ -2279,14 +2342,19 @@ num
 '(1 2 3)
 #+end_src
 
+
+
+
 #+srcname: bug-R-number-evaluation
-#+begin_src R :var table=bug-numerical-table :results replace
+#+begin_src R :var table=bug-numerical-table
 mean(mean(table))
 #+end_src
 
 #+resname:
 : 2
 
+
+
 #+tblname: bug-vert-table
 | 1 |
 | 2 |
@@ -2386,6 +2454,7 @@ As an example eval the following.  Adding a line to test
 ar.first.first
 #+end_src
 
+
 ** DONE space trailing language name
 fix regexp so it works when there's a space trailing the language name
 
@@ -2436,6 +2505,10 @@ recognition of ruby arrays as such.
 | 1 | 2 | 3 | 4 |
 
 
+
+
+
+
 * Tests
 Evaluate all the cells in this table for a comprehensive test of the
 org-babel functionality.
@@ -2491,6 +2564,9 @@ of these tests may fail.
 | set R session           | set-R-session-var          |     |         set |         set | pass |
 | get from R session      | get-R-session-var          |     |           5 |           5 | pass |
 #+TBLFM: $5='(if (= (length $3) 1) (progn (message (format "running %S" '(sbe $2 (n $3)))) (sbe $2 (n $3))) (sbe $2))::$6='(if (string= $4 $5) "pass" (format "expected %S but was %S" $4 $5))
+#+TBLFM: $5=""::$6=""
+
+The second line (followed by replacing '[]' with '') can be used to blank out the table results, in the absence of a better method.
 
 ** basic tests
 
@@ -2499,11 +2575,13 @@ of these tests may fail.
 (+ 1 4)
 #+end_src
 
+
 #+srcname: basic-shell
 #+begin_src sh :results silent
 expr 1 + 5
 #+end_src
 
+
 #+srcname: date-simple
 #+begin_src sh :results silent
 date
@@ -2514,17 +2592,20 @@ date
 "org-babel"
 #+end_src
 
+
 #+srcname: basic-python
 #+begin_src python :results silent
 'hello world'
 #+end_src
 
+
 #+srcname: basic-R
 #+begin_src R :results silent
 b <- 9
 b + 4
 #+end_src
 
+
 ** read tables
 
 #+tblname: test-table
@@ -2536,21 +2617,25 @@ b + 4
 (length (car table))
 #+end_src
 
+
 #+srcname: table-ruby
 #+begin_src ruby :results silent :var table=test-table
 table.first.join("-")
 #+end_src
 
+
 #+srcname: table-python
 #+begin_src python :var table=test-table
 table[1][1]
 #+end_src
 
+
 #+srcname: table-R
 #+begin_src R :var table=test-table
 mean(mean(table))
 #+end_src
 
+
 ** references
 
 Lets pass a references through all of our languages...
@@ -2595,6 +2680,7 @@ and Check that it is still a list
 table.class.name
 #+end_src
 
+
 ** source blocks as functions
 
 #+srcname: defun-fibb
@@ -2602,11 +2688,18 @@ table.class.name
 (defun fibbd (n) (if (< n 2) 1 (+ (fibbd (- n 1)) (fibbd (- n 2)))))
 #+end_src
 
+
 #+srcname: fibonacci
 #+begin_src emacs-lisp :results silent :var n=7
 (fibbd n)
 #+end_src
 
+
+
+
+
+
+
 ** sbe tests (these don't seem to be working...)
 Testing the insertion of results into org-mode tables.
 
@@ -2694,18 +2787,21 @@ To run these examples evaluate [[file:lisp/org-babel-init.el][org-babel-init.el]
 date
 #+end_src
 
-: Thu May 14 18:52:25 EDT 2009
+#+resname:
+: Sun Jul  5 18:54:39 EDT 2009
 
 #+begin_src ruby
 Time.now
 #+end_src
 
-: Thu May 14 18:59:09 -0400 2009
+#+resname:
+: Sun Jul 05 18:54:35 -0400 2009
 
 #+begin_src python
 "Hello World"
 #+end_src
 
+#+resname:
 : Hello World
 
 
@@ -2713,14 +2809,11 @@ Time.now
 
 #+begin_src R :results replace
 a <- 9
-b <- 17
+b <- 16
 a + b
 #+end_src
 
 #+resname:
-: 26
-
-
 : 25
 
 #+begin_src R
@@ -2728,6 +2821,7 @@ hist(rgamma(20,3,3))
 #+end_src
 
 
+
 ** org-babel plays with tables
 Alright, this should demonstrate both the ability of org-babel to read
 tables into a lisp source code block, and to then convert the results
@@ -2764,6 +2858,7 @@ out...
 '(1 2 3 4 5)
 #+end_src
 
+#+resname:
 | 1 | 2 | 3 | 4 | 5 |
 
 *** Ruby and Python
@@ -2779,13 +2874,15 @@ table.first.join(" - ")
 table[0]
 #+end_src
 
-#+resname:
-| 1 | 2 | 3 |
 
 #+begin_src ruby :var table=sandbox :results replace
 table
 #+end_src
 
+#+resname:
+: [[1, 2, 3], [4, "schulte", 6]]
+
+
 | 1 |         2 | 3 |
 | 4 | "schulte" | 6 |
 

+ 2 - 2
test-tangle.org

@@ -1,5 +1,5 @@
-#+TITLE: Testing Org-Babel Tangling
 #+OPTIONS: toc:2 ^:nil
+#+TITLE: Testing Org-Babel Tangling
 
 * Some file
 
@@ -80,7 +80,7 @@ plus_two(holder)
 
 ** Emacs Lisp initialization stuff
 
-#+srcname: lets-set-some-variables
+#+srcname: lets-set-some-variables()
 #+begin_src emacs-lisp 
   (setq test-tangle-loading "org-babel tangles")
   (setq test-tangle-advert "use org-babel-tangle for all your emacs initialization files!!")