;;; ob-octave.el --- Babel Functions for Octave and MATLAB -*- lexical-binding: t; -*- ;; Copyright (C) 2010-2024 Free Software Foundation, Inc. ;; Author: Dan Davison ;; Keywords: literate programming, reproducible research ;; URL: https://orgmode.org ;; This file is part of GNU Emacs. ;; GNU Emacs 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 of the License, or ;; (at your option) any later version. ;; GNU Emacs 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. If not, see . ;;; Commentary: ;;; Requirements: ;; octave ;; octave-mode.el and octave-inf.el come with GNU emacs ;;; Code: (require 'cl-seq) (require 'org-macs) (org-assert-version) (require 'ob) (require 'org-macs) (declare-function matlab-shell "ext:matlab-mode") (declare-function matlab-shell-run-region "ext:matlab-mode") ;; With `org-babel-default-header-args:matlab' set to ;; '((:session . "*MATLAB*"))) ;; each matlab code block evaluation will reuse the "*MATLAB*" buffer ;; created by `matlab-shell' for code evaluation. The benefit of this ;; is that evaluation is very fast for evaluations after the first ;; evaluation. The first evaluation may be slower because it can take ;; a long time to start MATLAB. Reusing the session buffer means ;; state is maintained between evaluations. To avoid reuse of state, ;; you can clear the MATLAB workspace. This setting aligns with ;; typical MATLAB use, where MATLAB is started and used for a long ;; period of period of time. Another benefit of this setting is that ;; you can see the history of the evaluations in the "*MATLAB*" ;; command window buffer. For example: ;; ;; #+begin_src matlab :results output ;; a = 123 ;; #+end_src ;; ;; #+RESULTS: ;; : a = ;; : ;; : 123 ;; ;; #+begin_src matlab :results output ;; b = a * 2 ;; #+end_src ;; ;; #+RESULTS: ;; : b = ;; : ;; : 246 ;; ;; #+begin_src matlab :results output ;; clear ;; c = b * 2 ;; #+end_src ;; ;; #+RESULTS: ;; : Unrecognized function or variable 'b'. ;; ;; If you want a new session each time you evaluate a MATLAB code block, ;; (setq 'org-babel-default-header-args:matlab '()) ;; However, this will make evaluations slower and is not typically how ;; people use MATLAB. (defvar org-babel-default-header-args:matlab '((:session . "*MATLAB*"))) (defvar org-babel-default-header-args:octave '()) (defvar org-babel-matlab-shell-command "matlab -nosplash" "Shell command to run matlab as an external process.") (defvar org-babel-octave-shell-command "octave -q" "Shell command to run octave as an external process.") (make-obsolete-variable 'org-babel-matlab-with-emacs-link "MATLAB removed EmacsLink in R2009a." "2009") (make-obsolete-variable 'org-babel-matlab-emacs-link-wrapper-method "MATLAB removed EmacsLink in R2009a." "2009") (defvar org-babel-matlab-print "print(\"-dpng\", %S);\nans=%S;" ;; MATLAB command-function duality requires that the file name be specified ;; without quotes. Using: print -dpng "file.png", would produce a file with ;; the quotes in the file name on disk. Therefore, use the functional form ;; to handle files with spaces, print("-dpng", "file.png"). ;; Example: ;; #+begin_src matlab :file "sine wave.png" :results file graphics ;; t = [0 : 0.1 : 2*pi]; ;; y = sin(t); ;; plot(t, y); ;; set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 4 3]) % Set the size to 4" x 3" ;; #+end_src ;; ;; #+RESULTS: ;; [[file:sine wave.png]] "MATLAB format specifier to print current figure to a file.") (defvar org-babel-octave-print "print -dpng %S\nans=%S" "Octave format specifier to print current figure to a file.") (defvar org-babel-matlab-wrapper-method (concat "\ cd('%s'); %s if ~exist('ans', 'var') ans = ''; end; \ writematrix(ans, '%s', 'Delimiter', 'tab'); ") "Format specifier used when evaluating MATLAB code blocks. Arguments are the `default-directory', the MATLAB code, and a result file.txt.") (defvar org-babel-octave-wrapper-method "%s if ischar(ans), fid = fopen('%s', 'w'); fdisp(fid, ans); fclose(fid); else, dlmwrite('%s', ans, '\\t') end") (defvar org-babel-octave-eoe-indicator "'org_babel_eoe'") (defvar org-babel-octave-eoe-output "ans = org_babel_eoe") (defun org-babel-execute:matlab (body params) "Execute Matlab BODY according to PARAMS." (org-babel-execute:octave body params 'matlab)) (defun org-babel-execute:octave (body params &optional matlabp) "Execute Octave or Matlab BODY according to PARAMS. When MATLABP is non-nil, execute Matlab. Otherwise, execute Octave." (let* ((session (funcall (intern (format "org-babel-%s-initiate-session" (if matlabp "matlab" "octave"))) (cdr (assq :session params)) params)) (result-type (cdr (assq :result-type params))) (full-body (org-babel-expand-body:generic body params (org-babel-variable-assignments:octave params))) (gfx-file (ignore-errors (org-babel-graphical-output-file params))) (result (org-babel-octave-evaluate session (if gfx-file (mapconcat 'identity (list "set (0, \"defaultfigurevisible\", \"off\");" full-body (format (if matlabp org-babel-matlab-print org-babel-octave-print) gfx-file gfx-file)) "\n") full-body) result-type matlabp))) (if gfx-file nil (org-babel-reassemble-table result (org-babel-pick-name (cdr (assq :colname-names params)) (cdr (assq :colnames params))) (org-babel-pick-name (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))) (defun org-babel-prep-session:matlab (session params) "Prepare SESSION according to PARAMS." (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. The variables are taken from PARAMS." (mapcar (lambda (pair) (format "%s=%s;" (car pair) (org-babel-octave-var-to-octave (cdr pair)))) (org-babel--get-vars params))) (defalias 'org-babel-variable-assignments:matlab 'org-babel-variable-assignments:octave) (defun org-babel-octave-var-to-octave (value) "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 value) (concat "[" (mapconcat #'org-babel-octave-var-to-octave value (if (listp (car value)) "; " ",")) "]") (cond ((stringp value) (format "'%s'" value)) (t (format "%s" value))))) (defun org-babel-prep-session:octave (session params &optional matlabp) "Prepare SESSION according to the header arguments specified in PARAMS. The session will be an Octave session, unless MATLABP is non-nil." (let* ((session (org-babel-octave-initiate-session session params matlabp)) (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) (org-babel-comint-wait-for-output session)) var-lines)) session)) (defun org-babel-matlab-initiate-session (&optional session params) "Create a matlab inferior process buffer. If there is not a current inferior-process-buffer in SESSION then create. Return the initialized session. PARAMS are src block parameters." (org-babel-octave-initiate-session session params 'matlab)) (defun org-babel-matlab-shell () "Start and/or wait for MATLAB shell." (require 'matlab-shell) ;; make `matlab-shell-busy-checker' available (cond ((fboundp 'matlab-shell-busy-checker) ;; Start the shell if needed. `matlab-shell' will reuse existing if already running. (matlab-shell) ;; If we just started the matlab-shell, wait for the prompt. If we do not ;; wait, then the startup messages will show up in the evaluation results. (matlab-shell-busy-checker 'wait-for-prompt)) (t (message (concat "You version of matlab-mode is old.\n" "Please update, see https://github.com/mathworks/Emacs-MATLAB-Mode\n" "Updating will eliminate unexpected output in your results\n")) (sit-for 3) (matlab-shell)))) (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. The session will be an Octave session, unless MATLABP is non-nil." (if matlabp (org-require-package 'matlab "matlab-mode") (or (require 'octave-inf nil 'noerror) (require 'octave))) (unless (string= session "none") (let ((session (or session (if matlabp "*Inferior Matlab*" "*Inferior Octave*")))) (if (org-babel-comint-buffer-livep session) (progn (when (and matlabp (fboundp 'matlab-shell-busy-checker)) ;; Can't evaluate if the matlab-shell is currently running code (matlab-shell-busy-checker 'error-if-busy)) session) (save-window-excursion (if matlabp (org-babel-matlab-shell) (run-octave)) (rename-buffer (if (bufferp session) (buffer-name session) (if (stringp session) session (buffer-name)))) (current-buffer)))))) (defun org-babel-octave-evaluate (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 value of the last statement in BODY, as elisp." (if session (org-babel-octave-evaluate-session session body result-type matlabp) (org-babel-octave-evaluate-external-process body result-type matlabp))) (defun org-babel-octave-wrapper-tmp-file (matlabp) "Return a local tmp file with name adjusted for MATLABP." (if matlabp ;; writematrix requires a file ending with '.txt' (org-babel-temp-file "matlab-" ".txt") (org-babel-temp-file "octave-"))) (defun org-babel-octave-get-code-to-eval (body tmp-file matlabp) "Format BODY of the code block for evaluation saving results to TMP-FILE. If MATLABP, format for MATLAB, else format for Octave." (if matlabp (format org-babel-matlab-wrapper-method default-directory body tmp-file) (format org-babel-octave-wrapper-method body tmp-file tmp-file))) (defun org-babel-octave-evaluate-external-process (body result-type matlabp) "Evaluate BODY in an external Octave or MATLAB process. Process the result as RESULT-TYPE. Use Octave, unless MATLABP is non-nil." (let ((cmd (if matlabp org-babel-matlab-shell-command org-babel-octave-shell-command))) (pcase result-type (`output (org-babel-eval cmd body)) (`value (let ((tmp-file (org-babel-process-file-name (org-babel-octave-wrapper-tmp-file matlabp) 'noquote))) (org-babel-eval cmd (org-babel-octave-get-code-to-eval body tmp-file matlabp)) (org-babel-octave-import-elisp-from-file tmp-file)))))) (defun org-babel-body-for-output (body matlabp) "If MATLABP, fixup BODY for MATLAB output result-type." (when matlabp ;; When we send multi-line input to `matlab-shell', we'll see the "body" ;; code lines echoed in the output which is not what one would expect. To ;; remove these unwanted lines, we append a comment "%-" to each ;; line in the body MATLAB code. After we collect the results from ;; evaluation, we leverage the "%-" to remove the unwanted lines. ;; Example of desired behavior: ;; #+begin_src matlab :results output ;; disp('The results are:') ;; a = [1, 2; 3, 4] ;; b = a * 2 ;; #+end_src ;; ;; #+RESULTS: ;; #+begin_example ;; The results are: ;; ;; a = ;; ;; 1 2 ;; 3 4 ;; ;; b = ;; ;; 2 4 ;; 6 8 ;; #+end_example (setq body (replace-regexp-in-string "\n" " %--\n" body)) (when (not (string-match "\n\\'" body)) (setq body (concat body " %--")))) body) (defun org-babel-fix-up-output (results matlabp) "Fix up RESULTS for output result-type when MATLABP." ;; When we send multi-line input to `matlab-shell', we'll see the "body" code ;; lines echoed in the output. Therefore, leverage the "%-" to ;; remove the unnecessary lines. (if matlabp (let ((fixed-results (replace-regexp-in-string "^[^\n]*%--\n" "" results))) ;; Remove unnecessary starting blank line caused by stripping %- (setq fixed-results (replace-regexp-in-string "\\`[[:space:]\r\n]+" "" fixed-results)) ;; matlab-shell wraps errors in and so remove those (replace-regexp-in-string "[\r\n]" "" fixed-results)) results)) (defun org-babel-octave-evaluate-session (session body result-type &optional matlabp) "Evaluate BODY in SESSION." (let* ((tmp-file (org-babel-octave-wrapper-tmp-file matlabp)) (full-body (pcase result-type (`output (mapconcat #'org-babel-chomp (list (org-babel-body-for-output body matlabp) org-babel-octave-eoe-indicator) "\n")) (`value (mapconcat #'org-babel-chomp (list (org-babel-octave-get-code-to-eval body tmp-file matlabp) org-babel-octave-eoe-indicator) "\n")))) (raw-results (org-babel-comint-with-output (session (if matlabp org-babel-octave-eoe-indicator org-babel-octave-eoe-output) t full-body) (insert full-body) (comint-send-input nil t))) results) (pcase result-type (`value (org-babel-octave-import-elisp-from-file tmp-file)) (`output (setq results (if matlabp (cdr (reverse (delete "" (mapcar #'org-strip-quotes (mapcar #'org-trim raw-results))))) (cdr (member org-babel-octave-eoe-output (reverse (mapcar #'org-strip-quotes (mapcar #'org-trim raw-results))))))) (org-babel-fix-up-output (mapconcat #'identity (reverse results) "\n") matlabp))))) (defun org-babel-octave-import-elisp-from-file (file-name) "Import data from FILE-NAME. This removes initial blank and comment lines and then calls `org-babel-import-elisp-from-file'." (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) (when (< (setq beg (point-min)) (setq end (line-beginning-position))) (delete-region beg end))) (org-babel-import-elisp-from-file temp-file '(16)))) (provide 'ob-octave) ;;; ob-octave.el ends here