Merge branch 'maint'
[org-mode/org-tableheadings.git] / contrib / lisp / ob-julia.el
blob41c8b5a903c541ffbf6221996ff32467612a9791
1 ;;; ob-julia.el --- org-babel functions for julia code evaluation
3 ;; Copyright (C) 2013, 2014 G. Jay Kerns
4 ;; Author: G. Jay Kerns, based on ob-R.el by Eric Schulte and Dan Davison
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 3, or (at your option)
11 ;; any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
23 ;;; Commentary:
25 ;; The file provides Org-Babel support for evaluating julia code.
27 ;; See https://github.com/gjkerns/ob-julia/blob/master/ob-julia-doc.org
28 ;; for detailed instructions on how to get started. The git repository
29 ;; contains more documentation: git://github.com/gjkerns/ob-julia.git
31 ;;; Code:
32 (require 'ob)
33 (require 'cl-lib)
35 (declare-function orgtbl-to-csv "org-table" (table params))
36 (declare-function julia "ext:ess-julia" (&optional start-args))
37 (declare-function inferior-ess-send-input "ext:ess-inf" ())
38 (declare-function ess-make-buffer-current "ext:ess-inf" ())
39 (declare-function ess-eval-buffer "ext:ess-inf" (vis))
40 (declare-function org-number-sequence "org-compat" (from &optional to inc))
42 (defconst org-babel-header-args:julia
43 '((width . :any)
44 (horizontal . :any)
45 (results . ((file list vector table scalar verbatim)
46 (raw org html latex code pp wrap)
47 (replace silent append prepend)
48 (output value graphics))))
49 "julia-specific header arguments.")
51 (add-to-list 'org-babel-tangle-lang-exts '("julia" . "jl"))
53 (defvar org-babel-default-header-args:julia '())
55 (defcustom org-babel-julia-command inferior-julia-program-name
56 "Name of command to use for executing julia code."
57 :group 'org-babel
58 :version "24.4"
59 :package-version '(Org . "8.0")
60 :type 'string)
62 (defvar ess-local-process-name) ; dynamically scoped
63 (defun org-babel-edit-prep:julia (info)
64 (let ((session (cdr (assq :session (nth 2 info)))))
65 (when (and session (string-match "^\\*\\(.+?\\)\\*$" session))
66 (save-match-data (org-babel-julia-initiate-session session nil)))))
68 (defun org-babel-expand-body:julia (body params &optional graphics-file)
69 "Expand BODY according to PARAMS, return the expanded body."
70 (let ((graphics-file
71 (or graphics-file (org-babel-julia-graphical-output-file params))))
72 (mapconcat
73 #'identity
74 ((lambda (inside)
75 (if graphics-file
76 inside
77 inside))
78 (append (org-babel-variable-assignments:julia params)
79 (list body))) "\n")))
81 (defun org-babel-execute:julia (body params)
82 "Execute a block of julia code.
83 This function is called by `org-babel-execute-src-block'."
84 (save-excursion
85 (let* ((result-params (cdr (assq :result-params params)))
86 (result-type (cdr (assq :result-type params)))
87 (session (org-babel-julia-initiate-session
88 (cdr (assq :session params)) params))
89 (colnames-p (cdr (assq :colnames params)))
90 (rownames-p (cdr (assq :rownames params)))
91 (graphics-file (org-babel-julia-graphical-output-file params))
92 (full-body (org-babel-expand-body:julia body params graphics-file))
93 (result
94 (org-babel-julia-evaluate
95 session full-body result-type result-params
96 (or (equal "yes" colnames-p)
97 (org-babel-pick-name
98 (cdr (assq :colname-names params)) colnames-p))
99 (or (equal "yes" rownames-p)
100 (org-babel-pick-name
101 (cdr (assq :rowname-names params)) rownames-p)))))
102 (if graphics-file nil result))))
104 (defun org-babel-prep-session:julia (session params)
105 "Prepare SESSION according to the header arguments specified in PARAMS."
106 (let* ((session (org-babel-julia-initiate-session session params))
107 (var-lines (org-babel-variable-assignments:julia params)))
108 (org-babel-comint-in-buffer session
109 (mapc (lambda (var)
110 (end-of-line 1) (insert var) (comint-send-input nil t)
111 (org-babel-comint-wait-for-output session)) var-lines))
112 session))
114 (defun org-babel-load-session:julia (session body params)
115 "Load BODY into SESSION."
116 (save-window-excursion
117 (let ((buffer (org-babel-prep-session:julia session params)))
118 (with-current-buffer buffer
119 (goto-char (process-mark (get-buffer-process (current-buffer))))
120 (insert (org-babel-chomp body)))
121 buffer)))
123 ;; helper functions
125 (defun org-babel-variable-assignments:julia (params)
126 "Return list of julia statements assigning the block's variables."
127 (let ((vars (org-babel--get-vars params)))
128 (mapcar
129 (lambda (pair)
130 (org-babel-julia-assign-elisp
131 (car pair) (cdr pair)
132 (equal "yes" (cdr (assq :colnames params)))
133 (equal "yes" (cdr (assq :rownames params)))))
134 (mapcar
135 (lambda (i)
136 (cons (car (nth i vars))
137 (org-babel-reassemble-table
138 (cdr (nth i vars))
139 (cdr (nth i (cdr (assq :colname-names params))))
140 (cdr (nth i (cdr (assq :rowname-names params)))))))
141 (org-number-sequence 0 (1- (length vars)))))))
143 (defun org-babel-julia-quote-csv-field (s)
144 "Quote field S for export to julia."
145 (if (stringp s)
146 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
147 (format "%S" s)))
149 (defun org-babel-julia-assign-elisp (name value colnames-p rownames-p)
150 "Construct julia code assigning the elisp VALUE to a variable named NAME."
151 (if (listp value)
152 (let ((max (apply #'max (mapcar #'length (cl-remove-if-not
153 #'sequencep value))))
154 (min (apply #'min (mapcar #'length (cl-remove-if-not
155 #'sequencep value))))
156 (transition-file (org-babel-temp-file "julia-import-")))
157 ;; ensure VALUE has an orgtbl structure (depth of at least 2)
158 (unless (listp (car value)) (setq value (list value)))
159 (with-temp-file transition-file
160 (insert
161 (orgtbl-to-csv value '(:fmt org-babel-julia-quote-csv-field))
162 "\n"))
163 (let ((file (org-babel-process-file-name transition-file 'noquote))
164 (header (if (or (eq (nth 1 value) 'hline) colnames-p)
165 "TRUE" "FALSE"))
166 (row-names (if rownames-p "1" "NULL")))
167 (if (= max min)
168 (format "%s = readcsv(\"%s\")" name file)
169 (format "%s = readcsv(\"%s\")"
170 name file))))
171 (format "%s = %s" name (org-babel-julia-quote-csv-field value))))
173 (defvar ess-ask-for-ess-directory) ; dynamically scoped
175 (defun org-babel-julia-initiate-session (session params)
176 "If there is not a current julia process then create one."
177 (unless (string= session "none")
178 (let ((session (or session "*julia*"))
179 (ess-ask-for-ess-directory
180 (and (and (boundp 'ess-ask-for-ess-directory) ess-ask-for-ess-directory)
181 (not (cdr (assq :dir params))))))
182 (if (org-babel-comint-buffer-livep session)
183 session
184 (save-window-excursion
185 (require 'ess) (julia)
186 (rename-buffer
187 (if (bufferp session)
188 (buffer-name session)
189 (if (stringp session)
190 session
191 (buffer-name))))
192 (current-buffer))))))
194 (defun org-babel-julia-associate-session (session)
195 "Associate julia code buffer with a julia session.
196 Make SESSION be the inferior ESS process associated with the
197 current code buffer."
198 (setq ess-local-process-name
199 (process-name (get-buffer-process session)))
200 (ess-make-buffer-current))
202 (defun org-babel-julia-graphical-output-file (params)
203 "Name of file to which julia should send graphical output."
204 (and (member "graphics" (cdr (assq :result-params params)))
205 (cdr (assq :file params))))
207 (defvar org-babel-julia-eoe-indicator "print(\"org_babel_julia_eoe\")")
208 (defvar org-babel-julia-eoe-output "org_babel_julia_eoe")
210 (defvar org-babel-julia-write-object-command "writecsv(\"%s\",%s)")
212 ;; The following was a very complicated write object command
213 ;; The replacement needs to add error catching
214 ;(defvar org-babel-julia-write-object-command "{function(object,transfer.file){object;invisible(if(inherits(try({tfile<-tempfile();write.table(object,file=tfile,sep=\"\\t\",na=\"nil\",row.names=%s,col.names=%s,quote=FALSE);file.rename(tfile,transfer.file)},silent=TRUE),\"try-error\")){if(!file.exists(transfer.file))file.create(transfer.file)})}}(object=%s,transfer.file=\"%s\")")
216 (defun org-babel-julia-evaluate
217 (session body result-type result-params column-names-p row-names-p)
218 "Evaluate julia code in BODY."
219 (if session
220 (org-babel-julia-evaluate-session
221 session body result-type result-params column-names-p row-names-p)
222 (org-babel-julia-evaluate-external-process
223 body result-type result-params column-names-p row-names-p)))
225 (defun org-babel-julia-evaluate-external-process
226 (body result-type result-params column-names-p row-names-p)
227 "Evaluate BODY in external julia process.
228 If RESULT-TYPE equals 'output then return standard output as a
229 string. If RESULT-TYPE equals 'value then return the value of the
230 last statement in BODY, as elisp."
231 (cl-case result-type
232 (value
233 (let ((tmp-file (org-babel-temp-file "julia-")))
234 (org-babel-eval org-babel-julia-command
235 (format org-babel-julia-write-object-command
236 (org-babel-process-file-name tmp-file 'noquote)
237 (format "begin\n%s\nend" body)))
238 (org-babel-julia-process-value-result
239 (org-babel-result-cond result-params
240 (with-temp-buffer
241 (insert-file-contents tmp-file)
242 (buffer-string))
243 (org-babel-import-elisp-from-file tmp-file '(4)))
244 column-names-p)))
245 (output (org-babel-eval org-babel-julia-command body))))
247 (defun org-babel-julia-evaluate-session
248 (session body result-type result-params column-names-p row-names-p)
249 "Evaluate BODY in SESSION.
250 If RESULT-TYPE equals 'output then return standard output as a
251 string. If RESULT-TYPE equals 'value then return the value of the
252 last statement in BODY, as elisp."
253 (cl-case result-type
254 (value
255 (with-temp-buffer
256 (insert (org-babel-chomp body))
257 (let ((ess-local-process-name
258 (process-name (get-buffer-process session)))
259 (ess-eval-visibly-p nil))
260 (ess-eval-buffer nil)))
261 (let ((tmp-file (org-babel-temp-file "julia-")))
262 (org-babel-comint-eval-invisibly-and-wait-for-file
263 session tmp-file
264 (format org-babel-julia-write-object-command
265 (org-babel-process-file-name tmp-file 'noquote) "ans"))
266 (org-babel-julia-process-value-result
267 (org-babel-result-cond result-params
268 (with-temp-buffer
269 (insert-file-contents tmp-file)
270 (buffer-string))
271 (org-babel-import-elisp-from-file tmp-file '(4)))
272 column-names-p)))
273 (output
274 (mapconcat
275 #'org-babel-chomp
276 (butlast
277 (delq nil
278 (mapcar
279 (lambda (line) (when (> (length line) 0) line))
280 (mapcar
281 (lambda (line) ;; cleanup extra prompts left in output
282 (if (string-match
283 "^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
284 (substring line (match-end 1))
285 line))
286 (org-babel-comint-with-output (session org-babel-julia-eoe-output)
287 (insert (mapconcat #'org-babel-chomp
288 (list body org-babel-julia-eoe-indicator)
289 "\n"))
290 (inferior-ess-send-input)))))) "\n"))))
292 (defun org-babel-julia-process-value-result (result column-names-p)
293 "julia-specific processing of return value.
294 Insert hline if column names in output have been requested."
295 (if column-names-p
296 (cons (car result) (cons 'hline (cdr result)))
297 result))
299 (provide 'ob-julia)
301 ;;; ob-julia.el ends here