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