ob-julia.el 12 KB

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