mirror of
https://git.savannah.gnu.org/git/emacs/org-mode.git
synced 2024-08-27 18:32:52 +00:00
* lisp/ob-C.el (org-babel-prep-session:C): (org-babel-load-session:C): * lisp/ob-J.el: (org-babel-expand-body:J): (org-babel-execute:J): * lisp/ob-R.el: (org-babel-expand-body:R): * lisp/ob-abc.el: (org-babel-execute:abc): (org-babel-prep-session:abc): * lisp/ob-asymptote.el: (org-babel-execute:asymptote): (org-babel-prep-session:asymptote): * lisp/ob-awk.el: (org-babel-expand-body:awk): * lisp/ob-calc.el: (org-babel-expand-body:calc): * lisp/ob-clojure.el: * lisp/ob-comint.el: (org-babel-comint-in-buffer): (org-babel-comint-with-output): (org-babel-comint-eval-invisibly-and-wait-for-file): * lisp/ob-coq.el: * lisp/ob-css.el: (org-babel-execute:css): (org-babel-prep-session:css): * lisp/ob-ditaa.el: (org-babel-execute:ditaa): (org-babel-prep-session:ditaa): * lisp/ob-dot.el: (org-babel-execute:dot): (org-babel-prep-session:dot): * lisp/ob-ebnf.el: * lisp/ob-emacs-lisp.el: * lisp/ob-eval.el: * lisp/ob-forth.el: * lisp/ob-fortran.el: (org-babel-execute:fortran): (org-babel-prep-session:fortran): (org-babel-load-session:fortran): * lisp/ob-gnuplot.el: (org-babel-expand-body:gnuplot): (org-babel-prep-session:gnuplot): (org-babel-gnuplot-initiate-session): * lisp/ob-groovy.el: (org-babel-prep-session:groovy): (org-babel-groovy-initiate-session): * lisp/ob-haskell.el: (org-babel-haskell-initiate-session): * lisp/ob-io.el: (org-babel-prep-session:io): (org-babel-io-initiate-session): * lisp/ob-java.el: (org-babel-execute:java): * lisp/ob-js.el: * lisp/ob-keys.el: * lisp/ob-latex.el: (org-babel-prep-session:latex): * lisp/ob-ledger.el: (org-babel-execute:ledger): (org-babel-prep-session:ledger): * lisp/ob-lilypond.el: (org-babel-lilypond-commands): (org-babel-lilypond-process-basic): (org-babel-prep-session:lilypond): (org-babel-lilypond-parse-line-num): * lisp/ob-lisp.el: * lisp/ob-makefile.el: (org-babel-execute:makefile): (org-babel-prep-session:makefile): * lisp/ob-matlab.el: * lisp/ob-maxima.el: (org-babel-prep-session:maxima): * lisp/ob-mscgen.el: (org-babel-prep-session:mscgen): * lisp/ob-ocaml.el: (org-babel-execute:ocaml): (org-babel-prep-session:ocaml): * lisp/ob-octave.el: (org-babel-execute:octave): (org-babel-octave-initiate-session): * lisp/ob-org.el: (org-babel-prep-session:org): * lisp/ob-perl.el: (org-babel-prep-session:perl): (org-babel-perl--var-to-perl): (org-babel-perl-initiate-session): * lisp/ob-picolisp.el: (org-babel-expand-body:picolisp): (org-babel-execute:picolisp): * lisp/ob-plantuml.el: (org-babel-execute:plantuml): (org-babel-prep-session:plantuml): * lisp/ob-processing.el: (org-babel-prep-session:processing): * lisp/ob-python.el: (org-babel-python-initiate-session): * lisp/ob-ref.el: (org-babel-ref-resolve): * lisp/ob-ruby.el: (org-babel-ruby-initiate-session): * lisp/ob-sass.el: (org-babel-execute:sass): (org-babel-prep-session:sass): * lisp/ob-scala.el: (org-babel-execute:scala): (org-babel-prep-session:scala): (org-babel-scala-initiate-session): * lisp/ob-scheme.el: * lisp/ob-screen.el: (org-babel-prep-session:screen): (org-babel-screen-session-write-temp-file): (org-babel-screen-test): * lisp/ob.el: * lisp/org-colview.el: (org-columns-todo): (org-columns-set-tags-or-toggle): (org-columns-new): (org-columns-uncompile-format): (org-agenda-colview-summarize): * lisp/org-footnote.el: (electric-indent-mode): * lisp/org-indent.el: (org-indent-refresh-maybe): * lisp/org-list.el: * lisp/org-macro.el: (org-macro--collect-macros): * lisp/org-src.el: * lisp/org-table.el: (sort-fold-case): (org-table-create): (org-table-field-info): (org-table-transpose-table-at-point): (org-table-remove-rectangle-highlight): (orgtbl-create-or-convert-from-region): (org-define-lookup-function): * lisp/ox-ascii.el: (org-ascii-format-drawer-function): (org-ascii--has-caption-p): (org-ascii-bold): (org-ascii-center-block): (org-ascii-clock): (org-ascii-code): (org-ascii-dynamic-block): (org-ascii-entity): (org-ascii-example-block): (org-ascii-export-snippet): (org-ascii-export-block): (org-ascii-fixed-width): (org-ascii-footnote-reference): (org-ascii-horizontal-rule): (org-ascii-inline-src-block): (org-ascii-format-inlinetask-default): (org-ascii-italic): (org-ascii-keyword): (org-ascii-latex-environment): (org-ascii-latex-fragment): (org-ascii-line-break): (org-ascii-node-property): (org-ascii-planning): (org-ascii-quote-block): (org-ascii-radio-target): (org-ascii-special-block): (org-ascii-src-block): (org-ascii-statistics-cookie): (org-ascii-subscript): (org-ascii-superscript): (org-ascii-strike-through): (org-ascii-timestamp): (org-ascii-underline): (org-ascii-verbatim): (org-ascii-verse-block): (org-ascii-filter-headline-blank-lines): (org-ascii-filter-paragraph-spacing): (org-ascii-filter-comment-spacing): Use lexical binding.
274 lines
9.7 KiB
EmacsLisp
274 lines
9.7 KiB
EmacsLisp
;;; ob-octave.el --- Babel Functions for Octave and Matlab -*- lexical-binding: t; -*-
|
|
|
|
;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
|
|
|
|
;; Author: Dan Davison
|
|
;; Keywords: literate programming, reproducible research
|
|
;; Homepage: http://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 <http://www.gnu.org/licenses/>.
|
|
|
|
;;; Commentary:
|
|
|
|
;;; Requirements:
|
|
|
|
;; octave
|
|
;; octave-mode.el and octave-inf.el come with GNU emacs
|
|
|
|
;;; Code:
|
|
(require 'ob)
|
|
(eval-when-compile (require 'cl))
|
|
|
|
(declare-function matlab-shell "ext:matlab-mode")
|
|
(declare-function matlab-shell-run-region "ext:matlab-mode")
|
|
|
|
(defvar org-babel-default-header-args: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.")
|
|
|
|
(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
|
|
to a non-nil value.")
|
|
|
|
(defvar org-babel-matlab-emacs-link-wrapper-method
|
|
"%s
|
|
if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
|
|
else, save -ascii %s ans
|
|
end
|
|
delete('%s')
|
|
")
|
|
(defvar org-babel-octave-wrapper-method
|
|
"%s
|
|
if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', 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 a block of matlab code with Babel."
|
|
(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* ((session
|
|
(funcall (intern (format "org-babel-%s-initiate-session"
|
|
(if matlabp "matlab" "octave")))
|
|
(cdr (assoc :session params)) params))
|
|
(result-type (cdr (assoc :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 "print -dpng %s" gfx-file))
|
|
"\n")
|
|
full-body)
|
|
result-type matlabp)))
|
|
(if gfx-file
|
|
nil
|
|
(org-babel-reassemble-table
|
|
result
|
|
(org-babel-pick-name
|
|
(cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
|
|
(org-babel-pick-name
|
|
(cdr (assoc :rowname-names params)) (cdr (assoc :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."
|
|
(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
|
|
(if (listp (car var)) "; " ",")) "]")
|
|
(cond
|
|
((stringp var)
|
|
(format "'%s'" var))
|
|
(t
|
|
(format "%s" var)))))
|
|
|
|
(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))
|
|
(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."
|
|
(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."
|
|
(if matlabp (require 'matlab) (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) session
|
|
(save-window-excursion
|
|
(if matlabp (unless org-babel-matlab-with-emacs-link (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-evaluate-external-process (body result-type matlabp)
|
|
"Evaluate BODY in an external octave process."
|
|
(let ((cmd (if matlabp
|
|
org-babel-matlab-shell-command
|
|
org-babel-octave-shell-command)))
|
|
(case result-type
|
|
(output (org-babel-eval cmd body))
|
|
(value (let ((tmp-file (org-babel-temp-file "octave-")))
|
|
(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))))))
|
|
|
|
(defun org-babel-octave-evaluate-session
|
|
(session body result-type &optional matlabp)
|
|
"Evaluate BODY in SESSION."
|
|
(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
|
|
(mapconcat
|
|
#'org-babel-chomp
|
|
(list body org-babel-octave-eoe-indicator) "\n"))
|
|
(value
|
|
(if (and matlabp org-babel-matlab-with-emacs-link)
|
|
(concat
|
|
(format org-babel-matlab-emacs-link-wrapper-method
|
|
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
|
|
(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
|
|
(with-temp-buffer
|
|
(insert full-body)
|
|
(write-region "" 'ignored wait-file nil nil nil 'excl)
|
|
(matlab-shell-run-region (point-min) (point-max))
|
|
(message "Waiting for Matlab Emacs Link")
|
|
(while (file-exists-p wait-file) (sit-for 0.01))
|
|
"")) ;; matlab-shell-run-region doesn't seem to
|
|
;; make *matlab* buffer contents easily
|
|
;; available, so :results output currently
|
|
;; won't work
|
|
(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)
|
|
(case result-type
|
|
(value
|
|
(org-babel-octave-import-elisp-from-file tmp-file))
|
|
(output
|
|
(progn
|
|
(setq results
|
|
(if matlabp
|
|
(cdr (reverse (delq "" (mapcar
|
|
#'org-babel-octave-read-string
|
|
(mapcar #'org-babel-trim raw)))))
|
|
(cdr (member org-babel-octave-eoe-output
|
|
(reverse (mapcar
|
|
#'org-babel-octave-read-string
|
|
(mapcar #'org-babel-trim raw)))))))
|
|
(mapconcat #'identity (reverse results) "\n"))))))
|
|
|
|
(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)
|
|
(if (< (setq beg (point-min))
|
|
(setq end (point-at-bol)))
|
|
(delete-region beg end)))
|
|
(org-babel-import-elisp-from-file temp-file '(16))))
|
|
|
|
(defun org-babel-octave-read-string (string)
|
|
"Strip \\\"s from around octave string."
|
|
(if (string-match "^\"\\([^\000]+\\)\"$" string)
|
|
(match-string 1 string)
|
|
string))
|
|
|
|
(provide 'ob-octave)
|
|
|
|
|
|
|
|
;;; ob-octave.el ends here
|