]> code.delx.au - gnu-emacs/blobdiff - lisp/org/ob-octave.el
Install org-mode version 7.3
[gnu-emacs] / lisp / org / ob-octave.el
index 2cdbaa0468cec3c207ad8487fb7123997373f119..d6affecd74d456e0f282cf4861e0feb8dcd26882 100644 (file)
@@ -5,7 +5,7 @@
 ;; Author: Dan Davison
 ;; Keywords: literate programming, reproducible research
 ;; Homepage: http://orgmode.org
-;; Version: 7.01
+;; Version: 7.3
 
 ;; This file is part of GNU Emacs.
 
 (defvar org-babel-octave-shell-command "octave -q"
   "Shell command to run octave as an external process.")
 
-(defun org-babel-expand-body:matlab (body params &optional processed-params)
-  "Expand BODY according to PARAMS, return the expanded body."
-  (org-babel-expand-body:octave body params processed-params))
-(defun org-babel-expand-body:octave (body params &optional processed-params)
-  "Expand BODY according to PARAMS, return the expanded body."
-  (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
-    (concat
-     (mapconcat
-      (lambda (pair)
-        (format "%s=%s"
-                (car pair)
-                (org-babel-octave-var-to-octave (cdr pair))))
-      vars "\n") "\n" body "\n")))
-
 (defvar org-babel-matlab-with-emacs-link nil
   "If non-nil use matlab-shell-run-region for session evaluation.
   This will use EmacsLink if (matlab-with-emacs-link) evaluates
@@ -76,7 +62,7 @@ delete('%s')
 (defvar org-babel-octave-wrapper-method
   "%s
 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
-else, save -ascii %s ans
+else, dlmwrite('%s', ans, '\\t')
 end")
 
 (defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
@@ -85,53 +71,60 @@ end")
 
 (defun org-babel-execute:matlab (body params)
   "Execute a block of matlab code with Babel."
-  (require 'matlab)
   (org-babel-execute:octave body params 'matlab))
+
 (defun org-babel-execute:octave (body params &optional matlabp)
   "Execute a block of octave code with Babel."
-  (let* ((processed-params (org-babel-process-params params))
-         (session
+  (let* ((session
          (funcall (intern (format "org-babel-%s-initiate-session"
                                   (if matlabp "matlab" "octave")))
-                  (nth 0 processed-params) params))
-         (vars (nth 1 processed-params))
-         (result-params (nth 2 processed-params))
-         (result-type (nth 3 processed-params))
+                  (cdr (assoc :session params)) params))
+         (vars (mapcar #'cdr (org-babel-get-header params :var)))
+         (result-params (cdr (assoc :result-params params)))
+         (result-type (cdr (assoc :result-type params)))
         (out-file (cdr (assoc :file params)))
-        (augmented-body
-         (org-babel-expand-body:octave body params processed-params))
+        (full-body
+         (org-babel-expand-body:generic
+          body params (org-babel-variable-assignments:octave params)))
         (result (org-babel-octave-evaluate
-                 session augmented-body result-type matlabp)))
+                 session full-body result-type matlabp)))
     (or out-file
         (org-babel-reassemble-table
          result
          (org-babel-pick-name
-         (nth 4 processed-params) (cdr (assoc :colnames params)))
+         (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
          (org-babel-pick-name
-         (nth 5 processed-params) (cdr (assoc :rownames params)))))))
+         (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
 
 (defun org-babel-prep-session:matlab (session params)
   "Prepare SESSION according to PARAMS."
-  (require 'matlab)
   (org-babel-prep-session:octave session params 'matlab))
+
+(defun org-babel-variable-assignments:octave (params)
+  "Return list of octave statements assigning the block's variables"
+  (mapcar
+   (lambda (pair)
+     (format "%s=%s"
+            (car pair)
+            (org-babel-octave-var-to-octave (cdr pair))))
+   (mapcar #'cdr (org-babel-get-header params :var))))
+
+(defalias 'org-babel-variable-assignments:matlab
+  'org-babel-variable-assignments:octave)
+
 (defun org-babel-octave-var-to-octave (var)
   "Convert an emacs-lisp value into an octave variable.
 Converts an emacs-lisp variable into a string of octave code
 specifying a variable of the same value."
   (if (listp var)
-      (concat "[" (mapconcat #'org-babel-octave-var-to-octave var ", ") "]")
-    (format "%S" var)))
+      (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
+                            (if (listp (car var)) "; " ",")) "]")
+    (format "%s" (or var "nil"))))
 
 (defun org-babel-prep-session:octave (session params &optional matlabp)
   "Prepare SESSION according to the header arguments specified in PARAMS."
   (let* ((session (org-babel-octave-initiate-session session params matlabp))
-         (vars (org-babel-ref-variables params))
-         (var-lines (mapcar
-                     (lambda (pair)
-                       (format "%s=%s"
-                               (car pair)
-                               (org-babel-octave-var-to-octave (cdr pair))))
-                     vars)))
+        (var-lines (org-babel-variable-assignments:octave params)))
     (org-babel-comint-in-buffer session
       (mapc (lambda (var)
               (end-of-line 1) (insert var) (comint-send-input nil t)
@@ -142,13 +135,13 @@ specifying a variable of the same value."
   "Create a matlab inferior process buffer.
 If there is not a current inferior-process-buffer in SESSION then
 create. Return the initialized session."
-  (require 'matlab)
   (org-babel-octave-initiate-session session params 'matlab))
+
 (defun org-babel-octave-initiate-session (&optional session params matlabp)
   "Create an octave inferior process buffer.
 If there is not a current inferior-process-buffer in SESSION then
 create. Return the initialized session."
-  (require 'octave-inf)
+  (if matlabp (require 'matlab) (require 'octave-inf))
   (unless (string= session "none")
     (let ((session (or session
                       (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
@@ -161,7 +154,7 @@ create. Return the initialized session."
          (current-buffer))))))
 
 (defun org-babel-octave-evaluate
-  (session body result-type lang &optional matlabp)
+  (session body result-type &optional matlabp)
   "Pass BODY to the octave process in SESSION.
 If RESULT-TYPE equals 'output then return the outputs of the
 statements in BODY, if RESULT-TYPE equals 'value then return the
@@ -177,17 +170,19 @@ value of the last statement in BODY, as elisp."
               org-babel-octave-shell-command)))
     (case result-type
       (output (org-babel-eval cmd body))
-      (value (let ((tmp-file (make-temp-file "org-babel-results-")))
+      (value (let ((tmp-file (org-babel-temp-file "octave-")))
               (org-babel-eval
                cmd
-               (format org-babel-octave-wrapper-method body tmp-file tmp-file))
-              (org-babel-eval-read-file tmp-file))))))
+               (format org-babel-octave-wrapper-method body
+                       (org-babel-process-file-name tmp-file 'noquote)
+                       (org-babel-process-file-name tmp-file 'noquote)))
+              (org-babel-octave-import-elisp-from-file tmp-file))))))
 
 (defun org-babel-octave-evaluate-session
   (session body result-type &optional matlabp)
   "Evaluate BODY in SESSION."
-  (let* ((tmp-file (make-temp-file "org-babel-results-"))
-        (wait-file (make-temp-file "org-babel-matlab-emacs-link-wait-signal-"))
+  (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
+        (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
         (full-body
          (case result-type
            (output
@@ -198,11 +193,15 @@ value of the last statement in BODY, as elisp."
             (if (and matlabp org-babel-matlab-with-emacs-link)
                 (concat
                  (format org-babel-matlab-emacs-link-wrapper-method
-                         body tmp-file tmp-file wait-file) "\n")
+                         body
+                         (org-babel-process-file-name tmp-file 'noquote)
+                         (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
               (mapconcat
                #'org-babel-chomp
                (list (format org-babel-octave-wrapper-method
-                             body tmp-file tmp-file)
+                             body
+                             (org-babel-process-file-name tmp-file 'noquote)
+                             (org-babel-process-file-name tmp-file 'noquote))
                      org-babel-octave-eoe-indicator) "\n")))))
         (raw (if (and matlabp org-babel-matlab-with-emacs-link)
                  (save-window-excursion
@@ -225,8 +224,7 @@ value of the last statement in BODY, as elisp."
                  (insert full-body) (comint-send-input nil t)))) results)
     (case result-type
       (value
-       (org-babel-octave-import-elisp-from-file
-       (org-babel-maybe-remote-file tmp-file)))
+       (org-babel-octave-import-elisp-from-file tmp-file))
       (output
        (progn
         (setq results
@@ -244,14 +242,14 @@ value of the last statement in BODY, as elisp."
   "Import data from FILE-NAME.
 This removes initial blank and comment lines and then calls
 `org-babel-import-elisp-from-file'."
-  (let ((temp-file (make-temp-file "org-babel-results-")) beg end)
+  (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
     (with-temp-file temp-file
       (insert-file-contents file-name)
       (re-search-forward "^[ \t]*[^# \t]" nil t)
       (if (< (setq beg (point-min))
             (setq end (point-at-bol)))
          (delete-region beg end)))
-    (org-babel-import-elisp-from-file temp-file)))
+    (org-babel-import-elisp-from-file temp-file '(16))))
 
 (defun org-babel-octave-read-string (string)
   "Strip \\\"s from around octave string"