Browse Source

Merge branch 'master' of git+ssh://repo.or.cz/srv/git/org-mode

Carsten Dominik 15 years ago
parent
commit
001fc9748a

+ 122 - 0
contrib/babel/lisp/langs/org-babel-perl.el

@@ -0,0 +1,122 @@
+;;; org-babel-perl.el --- org-babel functions for perl evaluation
+
+;; Copyright (C) 2009 Dan Davison, Eric Schulte
+
+;; Author: Dan Davison, Eric Schulte
+;; Keywords: literate programming, reproducible research
+;; Homepage: http://orgmode.org
+;; Version: 0.01
+
+;;; License:
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3, or (at your option)
+;; any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;;; Commentary:
+
+;; Org-Babel support for evaluating perl source code.
+
+;;; Code:
+(require 'org-babel)
+
+(org-babel-add-interpreter "perl")
+
+(add-to-list 'org-babel-tangle-langs '("perl" "pl" "#!/usr/bin/env perl"))
+
+(defun org-babel-execute:perl (body params)
+  "Execute a block of Perl code with org-babel.  This function is
+called by `org-babel-execute-src-block' via multiple-value-bind."
+  (message "executing Perl source code block")
+  (let ((full-body (concat
+		    (mapconcat ;; define any variables
+		     (lambda (pair)
+		       (format "$%s=%s;"
+			       (car pair)
+			       (org-babel-perl-var-to-perl (cdr pair))))
+		     vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
+	(session (org-babel-perl-initiate-session session)))
+    (org-babel-perl-evaluate session full-body result-type)))
+
+(defun org-babel-prep-session:perl (session params)
+  "Prepare SESSION according to the header arguments specified in PARAMS."
+  (error "Sessions are not supported for Perl."))
+
+;; helper functions
+
+(defun org-babel-perl-var-to-perl (var)
+  "Convert an elisp var into a string of perl source code
+specifying a var of the same value."
+  (if (listp var)
+      (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
+    (format "%S" var)))
+
+(defvar org-babel-perl-buffers '(:default . nil))
+
+(defun org-babel-perl-initiate-session (&optional session)
+  "Simply return nil, as sessions are not supported by perl"
+nil)
+
+(defvar org-babel-perl-wrapper-method
+  "
+sub main {
+%s
+}
+@r = main;
+open(o, \">%s\");
+print o join(\"\\n\", @r), \"\\n\"")
+
+(defvar org-babel-perl-pp-wrapper-method
+  nil)
+
+(defun org-babel-perl-evaluate (session body &optional result-type)
+  "Pass BODY to the Perl process in SESSION.  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, as elisp."
+  (if (not session)
+      ;; external process evaluation
+      (save-window-excursion
+        (case result-type
+          (output
+           (with-temp-buffer
+             (insert body)
+             ;; (message "buffer=%s" (buffer-string)) ;; debugging
+             (shell-command-on-region (point-min) (point-max) "perl" 'replace)
+             (buffer-string)))
+          (value
+           (let ((tmp-file (make-temp-file "perl-functional-results")))
+             (with-temp-buffer
+               (insert
+		(format
+		 (if (member "pp" result-params)
+                     (error "Pretty-printing not implemented for perl")
+                   org-babel-perl-wrapper-method)
+		 (let ((lines (split-string
+			       (org-remove-indentation (org-babel-trim body)) "[\r\n]")))
+		   (concat
+		    (mapconcat #'identity (butlast lines) "\n")
+		    (format "\nreturn %s" (car (last lines)))))
+		 tmp-file))
+               ;; (message "buffer=%s" (buffer-string)) ;; debugging
+               (shell-command-on-region (point-min) (point-max) "perl"))
+             (let ((raw (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
+               (if (or (member "code" result-params) (member "pp" result-params))
+                   raw
+                 (org-babel-import-elisp-from-file tmp-file)))))))
+    ;; comint session evaluation
+    (error "Sessions are not supported for Perl.")))
+
+(provide 'org-babel-perl)
+;;; org-babel-perl.el ends here

+ 5 - 4
contrib/babel/lisp/langs/org-babel-python.el

@@ -40,15 +40,16 @@
   "Execute a block of Python code with org-babel.  This function is
   "Execute a block of Python code with org-babel.  This function is
 called by `org-babel-execute-src-block' via multiple-value-bind."
 called by `org-babel-execute-src-block' via multiple-value-bind."
   (message "executing Python source code block")
   (message "executing Python source code block")
-  (let ((full-body (concat
+  (let* ((full-body (concat
 		    (mapconcat ;; define any variables
 		    (mapconcat ;; define any variables
 		     (lambda (pair)
 		     (lambda (pair)
 		       (format "%s=%s"
 		       (format "%s=%s"
 			       (car pair)
 			       (car pair)
 			       (org-babel-python-var-to-python (cdr pair))))
 			       (org-babel-python-var-to-python (cdr pair))))
 		     vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
 		     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)))
+	(session (org-babel-python-initiate-session session))
+	(result (org-babel-python-evaluate session full-body result-type)))
+    (or (cdr (assoc :file params)) result)))
 
 
 (defun org-babel-prep-session:python (session params)
 (defun org-babel-prep-session:python (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
   "Prepare SESSION according to the header arguments specified in PARAMS."
@@ -160,7 +161,7 @@ last statement in BODY, as elisp."
 		    (mapconcat
 		    (mapconcat
 		     (lambda (line) (format "\t%s" line))
 		     (lambda (line) (format "\t%s" line))
 		     (butlast lines) "\n")
 		     (butlast lines) "\n")
-		    (format "\n\treturn %s" (last lines))))
+		    (format "\n\treturn %s" (car (last lines)))))
 		 tmp-file))
 		 tmp-file))
                ;; (message "buffer=%s" (buffer-string)) ;; debugging
                ;; (message "buffer=%s" (buffer-string)) ;; debugging
                (shell-command-on-region (point-min) (point-max) "python"))
                (shell-command-on-region (point-min) (point-max) "python"))

+ 4 - 3
contrib/babel/lisp/langs/org-babel-ruby.el

@@ -50,15 +50,16 @@
   "Execute a block of Ruby code with org-babel.  This function is
   "Execute a block of Ruby code with org-babel.  This function is
 called by `org-babel-execute-src-block' via multiple-value-bind."
 called by `org-babel-execute-src-block' via multiple-value-bind."
   (message "executing Ruby source code block")
   (message "executing Ruby source code block")
-  (let ((full-body (concat
+  (let* ((full-body (concat
 		    (mapconcat ;; define any variables
 		    (mapconcat ;; define any variables
 		     (lambda (pair)
 		     (lambda (pair)
 		       (format "%s=%s"
 		       (format "%s=%s"
 			       (car pair)
 			       (car pair)
 			       (org-babel-ruby-var-to-ruby (cdr pair))))
 			       (org-babel-ruby-var-to-ruby (cdr pair))))
 		     vars "\n") "\n" body "\n")) ;; then the source block body
 		     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)))
+	(session (org-babel-ruby-initiate-session session))
+	(result (org-babel-ruby-evaluate session full-body result-type)))
+    (or (cdr (assoc :file params)) result)))
 
 
 (defun org-babel-prep-session:ruby (session params)
 (defun org-babel-prep-session:ruby (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
   "Prepare SESSION according to the header arguments specified in PARAMS."

+ 1 - 0
contrib/babel/lisp/org-babel-tangle.el

@@ -72,6 +72,7 @@ TARGET-FILE can be used to specify a default export file for all
 source blocks.  Optional argument LANG can be used to limit the
 source blocks.  Optional argument LANG can be used to limit the
 exported source code blocks by language."
 exported source code blocks by language."
   (interactive)
   (interactive)
+  (save-buffer)
   (save-excursion
   (save-excursion
     (let ((block-counter 0)
     (let ((block-counter 0)
           path-collector)
           path-collector)

+ 3 - 2
contrib/babel/lisp/org-babel.el

@@ -176,8 +176,9 @@ the header arguments specified at the source code block."
   (let* ((info (or info (org-babel-get-src-block-info)))
   (let* ((info (or info (org-babel-get-src-block-info)))
          (lang (first info))
          (lang (first info))
 	 (params (setf (third info) (org-babel-merge-params (third info) params)))
 	 (params (setf (third info) (org-babel-merge-params (third info) params)))
-         (body (if (assoc :noweb params)
-                   (org-babel-expand-noweb-references info) (second info)))
+         (body (setf (second info)
+		     (if (assoc :noweb params)
+			 (org-babel-expand-noweb-references info) (second info))))
          (processed-params (org-babel-process-params params))
          (processed-params (org-babel-process-params params))
          (result-params (third processed-params))
          (result-params (third processed-params))
          (result-type (fourth processed-params))
          (result-type (fourth processed-params))