2015-10-25 20:56:00 -04:00
|
|
|
;;; ob-octave.el --- Babel Functions for Octave and Matlab -*- lexical-binding: t; -*-
|
2010-03-18 14:45:39 -04:00
|
|
|
|
2024-01-01 20:47:10 -05:00
|
|
|
;; Copyright (C) 2010-2024 Free Software Foundation, Inc.
|
2010-03-18 14:45:39 -04:00
|
|
|
|
|
|
|
;; Author: Dan Davison
|
|
|
|
;; Keywords: literate programming, reproducible research
|
2021-09-26 03:44:29 -04:00
|
|
|
;; URL: https://orgmode.org
|
2010-03-18 14:45:39 -04:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
2010-03-18 14:45:39 -04:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2010-03-18 14:45:39 -04:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 12:32:35 -04:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2010-03-18 14:45:39 -04:00
|
|
|
;; 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.
|
2010-06-25 12:32:35 -04:00
|
|
|
|
2010-03-18 14:45:39 -04:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 18:52:52 -04:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2010-03-18 14:45:39 -04:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Requirements:
|
|
|
|
|
|
|
|
;; octave
|
|
|
|
;; octave-mode.el and octave-inf.el come with GNU emacs
|
|
|
|
|
|
|
|
;;; Code:
|
2022-08-04 09:53:05 -04:00
|
|
|
|
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
(require 'ob)
|
2018-05-09 20:04:12 -04:00
|
|
|
(require 'org-macs)
|
2010-03-18 14:45:39 -04:00
|
|
|
|
2010-07-04 11:24:08 -04:00
|
|
|
(declare-function matlab-shell "ext:matlab-mode")
|
|
|
|
(declare-function matlab-shell-run-region "ext:matlab-mode")
|
2010-07-02 22:21:31 -04:00
|
|
|
|
|
|
|
(defvar org-babel-default-header-args:matlab '())
|
2010-06-25 13:12:54 -04:00
|
|
|
(defvar org-babel-default-header-args:octave '())
|
|
|
|
|
2010-07-02 22:21:31 -04:00
|
|
|
(defvar org-babel-matlab-shell-command "matlab -nosplash"
|
2010-07-13 19:20:08 -04:00
|
|
|
"Shell command to run matlab as an external process.")
|
2010-03-18 14:45:39 -04:00
|
|
|
(defvar org-babel-octave-shell-command "octave -q"
|
2010-07-13 19:20:08 -04:00
|
|
|
"Shell command to run octave as an external process.")
|
2010-03-18 14:45:39 -04:00
|
|
|
|
2010-07-02 22:21:31 -04:00
|
|
|
(defvar org-babel-matlab-with-emacs-link nil
|
2010-07-13 19:20:08 -04:00
|
|
|
"If non-nil use matlab-shell-run-region for session evaluation.
|
2021-09-16 06:32:43 -04:00
|
|
|
This will use EmacsLink if (matlab-with-emacs-link) evaluates
|
|
|
|
to a non-nil value.")
|
2010-07-02 22:21:31 -04:00
|
|
|
|
|
|
|
(defvar org-babel-matlab-emacs-link-wrapper-method
|
2012-08-11 13:10:44 -04:00
|
|
|
"%s
|
2010-07-02 22:21:31 -04:00
|
|
|
if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
|
|
|
|
else, save -ascii %s ans
|
|
|
|
end
|
|
|
|
delete('%s')
|
|
|
|
")
|
2010-07-04 16:48:41 -04:00
|
|
|
(defvar org-babel-octave-wrapper-method
|
|
|
|
"%s
|
2022-10-29 01:48:45 -04:00
|
|
|
if ischar(ans), fid = fopen('%s', 'w'); fdisp(fid, ans); fclose(fid);
|
2010-08-03 23:54:24 -04:00
|
|
|
else, dlmwrite('%s', ans, '\\t')
|
2010-07-04 16:48:41 -04:00
|
|
|
end")
|
|
|
|
|
2015-09-17 19:08:20 -04:00
|
|
|
(defvar org-babel-octave-eoe-indicator "'org_babel_eoe'")
|
2010-07-04 16:48:41 -04:00
|
|
|
|
|
|
|
(defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
|
2010-07-02 22:21:31 -04:00
|
|
|
|
|
|
|
(defun org-babel-execute:matlab (body params)
|
2023-09-26 07:13:44 -04:00
|
|
|
"Execute Matlab BODY according to PARAMS."
|
2010-07-02 22:21:31 -04:00
|
|
|
(org-babel-execute:octave body params 'matlab))
|
2010-08-01 14:34:28 -04:00
|
|
|
|
2010-03-18 14:45:39 -04:00
|
|
|
(defun org-babel-execute:octave (body params &optional matlabp)
|
2023-09-26 07:13:44 -04:00
|
|
|
"Execute Octave or Matlab BODY according to PARAMS.
|
|
|
|
When MATLABP is non-nil, execute Matlab. Otherwise, execute Octave."
|
2010-10-21 07:52:24 -04:00
|
|
|
(let* ((session
|
2010-07-02 22:21:31 -04:00
|
|
|
(funcall (intern (format "org-babel-%s-initiate-session"
|
|
|
|
(if matlabp "matlab" "octave")))
|
2016-09-22 13:45:15 -04:00
|
|
|
(cdr (assq :session params)) params))
|
|
|
|
(result-type (cdr (assq :result-type params)))
|
2010-10-21 07:18:43 -04:00
|
|
|
(full-body
|
|
|
|
(org-babel-expand-body:generic
|
|
|
|
body params (org-babel-variable-assignments:octave params)))
|
2014-05-15 23:26:30 -04:00
|
|
|
(gfx-file (ignore-errors (org-babel-graphical-output-file params)))
|
2010-07-04 16:48:41 -04:00
|
|
|
(result (org-babel-octave-evaluate
|
2011-12-16 14:12:39 -05:00
|
|
|
session
|
2014-05-15 23:26:30 -04:00
|
|
|
(if gfx-file
|
2011-12-16 14:12:39 -05:00
|
|
|
(mapconcat 'identity
|
|
|
|
(list
|
|
|
|
"set (0, \"defaultfigurevisible\", \"off\");"
|
|
|
|
full-body
|
2022-11-08 14:31:47 -05:00
|
|
|
(format "print -dpng %S\nans=%S" gfx-file gfx-file))
|
2011-12-16 14:12:39 -05:00
|
|
|
"\n")
|
|
|
|
full-body)
|
|
|
|
result-type matlabp)))
|
2014-05-15 23:26:30 -04:00
|
|
|
(if gfx-file
|
2011-12-16 14:12:39 -05:00
|
|
|
nil
|
|
|
|
(org-babel-reassemble-table
|
|
|
|
result
|
|
|
|
(org-babel-pick-name
|
2016-09-22 13:45:15 -04:00
|
|
|
(cdr (assq :colname-names params)) (cdr (assq :colnames params)))
|
2011-12-16 14:12:39 -05:00
|
|
|
(org-babel-pick-name
|
2016-09-22 13:45:15 -04:00
|
|
|
(cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))
|
2010-03-18 14:45:39 -04:00
|
|
|
|
2010-07-02 22:21:31 -04:00
|
|
|
(defun org-babel-prep-session:matlab (session params)
|
|
|
|
"Prepare SESSION according to PARAMS."
|
|
|
|
(org-babel-prep-session:octave session params 'matlab))
|
2010-08-01 14:34:28 -04:00
|
|
|
|
2010-10-21 07:18:43 -04:00
|
|
|
(defun org-babel-variable-assignments:octave (params)
|
2023-09-26 07:13:44 -04:00
|
|
|
"Return list of octave statements assigning the block's variables.
|
|
|
|
The variables are taken from PARAMS."
|
2010-10-14 16:56:41 -04:00
|
|
|
(mapcar
|
|
|
|
(lambda (pair)
|
2011-03-31 01:28:59 -04:00
|
|
|
(format "%s=%s;"
|
2010-10-14 16:56:41 -04:00
|
|
|
(car pair)
|
|
|
|
(org-babel-octave-var-to-octave (cdr pair))))
|
2015-10-29 15:26:11 -04:00
|
|
|
(org-babel--get-vars params)))
|
2010-10-14 16:56:41 -04:00
|
|
|
|
2010-10-21 07:18:43 -04:00
|
|
|
(defalias 'org-babel-variable-assignments:matlab
|
|
|
|
'org-babel-variable-assignments:octave)
|
|
|
|
|
2023-09-26 07:13:44 -04:00
|
|
|
(defun org-babel-octave-var-to-octave (value)
|
|
|
|
"Convert an emacs-lisp VALUE into an octave variable.
|
2010-03-18 14:45:39 -04:00
|
|
|
Converts an emacs-lisp variable into a string of octave code
|
|
|
|
specifying a variable of the same value."
|
2023-09-26 07:13:44 -04:00
|
|
|
(if (listp value)
|
|
|
|
(concat "[" (mapconcat #'org-babel-octave-var-to-octave value
|
|
|
|
(if (listp (car value)) "; " ",")) "]")
|
2011-11-08 16:19:04 -05:00
|
|
|
(cond
|
2023-09-26 07:13:44 -04:00
|
|
|
((stringp value)
|
|
|
|
(format "'%s'" value))
|
2011-11-08 16:19:04 -05:00
|
|
|
(t
|
2023-09-26 07:13:44 -04:00
|
|
|
(format "%s" value)))))
|
2010-03-18 14:45:39 -04:00
|
|
|
|
|
|
|
(defun org-babel-prep-session:octave (session params &optional matlabp)
|
2023-09-26 07:13:44 -04:00
|
|
|
"Prepare SESSION according to the header arguments specified in PARAMS.
|
|
|
|
The session will be an Octave session, unless MATLABP is non-nil."
|
2010-03-18 14:45:39 -04:00
|
|
|
(let* ((session (org-babel-octave-initiate-session session params matlabp))
|
2010-10-21 07:18:43 -04:00
|
|
|
(var-lines (org-babel-variable-assignments:octave params)))
|
2010-03-18 14:45:39 -04:00
|
|
|
(org-babel-comint-in-buffer session
|
|
|
|
(mapc (lambda (var)
|
Xemacs incompatibilities
Carsten Dominik <carsten.dominik@gmail.com> writes:
> On May 17, 2010, at 4:39 PM, Michael Sperber wrote:
>
>> In particular, fixing the require won't be enough: org-babel-python.el
>> uses `run-python' and interacts with the inferior Python, whereas
>> python-mode.el defines `py-shell'.
>>
>> Should I try to abstract over the differences?
>
> Yes, this would be much appreciated. (I think, Eric or Dan?)
OK, I've attached a patch that makes `org-babel-python' work on XEmacs.
Most of the issues are pure XEmacs issues. Notes:
- XEmacs doesn't have [[:digit:]] - I hope to rectify this in the
future, but it seems there's no downside in this particular case to
replacing by [0-9].
- XEmacs doesn't have `move-end-of-line', but does have `end-of-line'.
I don't understand the intent of having both of these, but the code
seems fine with `end-of-line'.
- It seems there are way too few `require's throughout org-babel. I
don't know if it's OK to add the ones I needed.
- `org-babel-python-evaluate' looked broken as-is: It doesn't use the
`body' argument properly, the result is (I think) processed in the
wrong order and not properly split into lines. I've fixed all these,
but a review is probably in order.
2010-05-24 15:22:50 -04:00
|
|
|
(end-of-line 1) (insert var) (comint-send-input nil t)
|
2020-02-18 16:57:37 -05:00
|
|
|
(org-babel-comint-wait-for-output session))
|
|
|
|
var-lines))
|
2010-03-18 14:45:39 -04:00
|
|
|
session))
|
|
|
|
|
2010-07-02 22:21:31 -04:00
|
|
|
(defun org-babel-matlab-initiate-session (&optional session params)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Create a matlab inferior process buffer.
|
|
|
|
If there is not a current inferior-process-buffer in SESSION then
|
2023-09-26 07:13:44 -04:00
|
|
|
create. Return the initialized session. PARAMS are src block parameters."
|
2010-07-02 22:21:31 -04:00
|
|
|
(org-babel-octave-initiate-session session params 'matlab))
|
2010-08-01 14:34:28 -04:00
|
|
|
|
2015-10-25 20:56:00 -04:00
|
|
|
(defun org-babel-octave-initiate-session (&optional session _params matlabp)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Create an octave inferior process buffer.
|
|
|
|
If there is not a current inferior-process-buffer in SESSION then
|
2023-09-26 07:13:44 -04:00
|
|
|
create. Return the initialized session. The session will be an
|
|
|
|
Octave session, unless MATLABP is non-nil."
|
2023-01-23 10:06:46 -05:00
|
|
|
(if matlabp
|
|
|
|
(org-require-package 'matlab "matlab-mode")
|
|
|
|
(or (require 'octave-inf nil 'noerror)
|
|
|
|
(require 'octave)))
|
2010-03-18 14:45:39 -04:00
|
|
|
(unless (string= session "none")
|
2010-07-04 16:48:41 -04:00
|
|
|
(let ((session (or session
|
|
|
|
(if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
|
2010-03-18 14:45:39 -04:00
|
|
|
(if (org-babel-comint-buffer-livep session) session
|
|
|
|
(save-window-excursion
|
2010-04-30 16:47:07 -04:00
|
|
|
(if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
|
|
|
|
(run-octave))
|
2010-03-18 14:45:39 -04:00
|
|
|
(rename-buffer (if (bufferp session) (buffer-name session)
|
2010-07-04 16:48:41 -04:00
|
|
|
(if (stringp session) session (buffer-name))))
|
|
|
|
(current-buffer))))))
|
2010-03-18 14:45:39 -04:00
|
|
|
|
2010-07-02 22:21:31 -04:00
|
|
|
(defun org-babel-octave-evaluate
|
2021-09-29 03:22:47 -04:00
|
|
|
(session body result-type &optional matlabp)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Pass BODY to the octave process in SESSION.
|
2015-09-21 00:24:12 -04:00
|
|
|
If RESULT-TYPE equals `output' then return the outputs of the
|
|
|
|
statements in BODY, if RESULT-TYPE equals `value' then return the
|
2010-07-13 19:20:08 -04:00
|
|
|
value of the last statement in BODY, as elisp."
|
2010-03-18 14:45:39 -04:00
|
|
|
(if session
|
2010-07-04 16:48:41 -04:00
|
|
|
(org-babel-octave-evaluate-session session body result-type matlabp)
|
2010-03-18 14:45:39 -04:00
|
|
|
(org-babel-octave-evaluate-external-process body result-type matlabp)))
|
|
|
|
|
|
|
|
(defun org-babel-octave-evaluate-external-process (body result-type matlabp)
|
2023-09-26 07:13:44 -04:00
|
|
|
"Evaluate BODY in an external Octave or Matalab process.
|
|
|
|
Process the result as RESULT-TYPE. Use Octave, unless MATLABP is non-nil."
|
2010-07-04 16:48:41 -04:00
|
|
|
(let ((cmd (if matlabp
|
|
|
|
org-babel-matlab-shell-command
|
|
|
|
org-babel-octave-shell-command)))
|
2016-07-25 10:34:48 -04:00
|
|
|
(pcase result-type
|
|
|
|
(`output (org-babel-eval cmd body))
|
|
|
|
(`value (let ((tmp-file (org-babel-temp-file "octave-")))
|
2021-09-29 03:22:47 -04:00
|
|
|
(org-babel-eval
|
|
|
|
cmd
|
|
|
|
(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))))))
|
2010-07-02 22:21:31 -04:00
|
|
|
|
|
|
|
(defun org-babel-octave-evaluate-session
|
2016-07-25 10:34:48 -04:00
|
|
|
(session body result-type &optional matlabp)
|
2010-06-11 20:12:15 -04:00
|
|
|
"Evaluate BODY in SESSION."
|
2010-09-20 19:38:31 -04:00
|
|
|
(let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
|
2010-08-25 16:43:07 -04:00
|
|
|
(wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
|
2010-03-18 14:45:39 -04:00
|
|
|
(full-body
|
2016-07-25 10:34:48 -04:00
|
|
|
(pcase result-type
|
|
|
|
(`output
|
2010-03-18 14:45:39 -04:00
|
|
|
(mapconcat
|
|
|
|
#'org-babel-chomp
|
|
|
|
(list body org-babel-octave-eoe-indicator) "\n"))
|
2016-07-25 10:34:48 -04:00
|
|
|
(`value
|
2010-05-03 21:43:23 -04:00
|
|
|
(if (and matlabp org-babel-matlab-with-emacs-link)
|
|
|
|
(concat
|
|
|
|
(format org-babel-matlab-emacs-link-wrapper-method
|
2010-09-22 16:40:14 -04:00
|
|
|
body
|
|
|
|
(org-babel-process-file-name tmp-file 'noquote)
|
|
|
|
(org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
|
2010-05-03 21:43:23 -04:00
|
|
|
(mapconcat
|
|
|
|
#'org-babel-chomp
|
2010-07-02 22:21:31 -04:00
|
|
|
(list (format org-babel-octave-wrapper-method
|
2010-09-22 16:40:14 -04:00
|
|
|
body
|
|
|
|
(org-babel-process-file-name tmp-file 'noquote)
|
|
|
|
(org-babel-process-file-name tmp-file 'noquote))
|
2010-05-03 21:43:23 -04:00
|
|
|
org-babel-octave-eoe-indicator) "\n")))))
|
2010-03-18 21:04:25 -04:00
|
|
|
(raw (if (and matlabp org-babel-matlab-with-emacs-link)
|
|
|
|
(save-window-excursion
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert full-body)
|
2010-05-03 21:43:23 -04:00
|
|
|
(write-region "" 'ignored wait-file nil nil nil 'excl)
|
2010-03-18 21:04:25 -04:00
|
|
|
(matlab-shell-run-region (point-min) (point-max))
|
2010-05-03 21:43:23 -04:00
|
|
|
(message "Waiting for Matlab Emacs Link")
|
|
|
|
(while (file-exists-p wait-file) (sit-for 0.01))
|
2010-03-18 21:04:25 -04:00
|
|
|
"")) ;; matlab-shell-run-region doesn't seem to
|
2012-08-11 13:10:44 -04:00
|
|
|
;; make *matlab* buffer contents easily
|
|
|
|
;; available, so :results output currently
|
|
|
|
;; won't work
|
2010-06-12 18:57:38 -04:00
|
|
|
(org-babel-comint-with-output
|
2010-06-14 20:00:49 -04:00
|
|
|
(session
|
|
|
|
(if matlabp
|
|
|
|
org-babel-octave-eoe-indicator
|
|
|
|
org-babel-octave-eoe-output)
|
|
|
|
t full-body)
|
2020-02-18 16:57:37 -05:00
|
|
|
(insert full-body) (comint-send-input nil t))))
|
|
|
|
results)
|
2016-07-25 10:34:48 -04:00
|
|
|
(pcase result-type
|
|
|
|
(`value
|
2010-09-22 16:40:14 -04:00
|
|
|
(org-babel-octave-import-elisp-from-file tmp-file))
|
2016-07-25 10:34:48 -04:00
|
|
|
(`output
|
|
|
|
(setq results
|
|
|
|
(if matlabp
|
2023-02-17 05:38:54 -05:00
|
|
|
(cdr (reverse (delete "" (mapcar #'org-strip-quotes
|
|
|
|
(mapcar #'org-trim raw)))))
|
2016-07-25 10:34:48 -04:00
|
|
|
(cdr (member org-babel-octave-eoe-output
|
2018-10-06 03:40:33 -04:00
|
|
|
(reverse (mapcar #'org-strip-quotes
|
|
|
|
(mapcar #'org-trim raw)))))))
|
2016-07-25 10:34:48 -04:00
|
|
|
(mapconcat #'identity (reverse results) "\n")))))
|
2010-03-18 14:45:39 -04:00
|
|
|
|
2010-04-07 15:51:04 -04:00
|
|
|
(defun org-babel-octave-import-elisp-from-file (file-name)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Import data from FILE-NAME.
|
|
|
|
This removes initial blank and comment lines and then calls
|
|
|
|
`org-babel-import-elisp-from-file'."
|
2010-09-20 19:38:31 -04:00
|
|
|
(let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
|
2010-04-07 15:51:04 -04:00
|
|
|
(with-temp-file temp-file
|
|
|
|
(insert-file-contents file-name)
|
|
|
|
(re-search-forward "^[ \t]*[^# \t]" nil t)
|
2018-05-09 20:14:24 -04:00
|
|
|
(when (< (setq beg (point-min))
|
2022-08-22 22:54:57 -04:00
|
|
|
(setq end (line-beginning-position)))
|
2018-05-09 20:14:24 -04:00
|
|
|
(delete-region beg end)))
|
2010-08-03 23:54:24 -04:00
|
|
|
(org-babel-import-elisp-from-file temp-file '(16))))
|
2010-04-07 15:51:04 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
(provide 'ob-octave)
|
2010-06-25 12:32:35 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-octave.el ends here
|