org-babel.el 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. ;;; org-babel.el --- facilitating communication between programming languages and people
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  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.
  12. ;;
  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.
  17. ;;
  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.
  22. ;;; Commentary:
  23. ;; See org-babel.org in the parent directory for more information
  24. ;;; Code:
  25. (require 'org)
  26. (defun org-babel-execute-src-block-maybe ()
  27. "Detect if this is context for a org-babel src-block and if so
  28. then run `org-babel-execute-src-block'."
  29. (interactive)
  30. (let ((info (org-babel-get-src-block-info)))
  31. (if info (progn (org-babel-execute-src-block current-prefix-arg info) t) nil)))
  32. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
  33. (defadvice org-edit-special (around org-babel-prep-session-for-edit activate)
  34. "Prepare the current source block's session according to it's
  35. header arguments before editing in an org-src buffer. This
  36. function is called when `org-edit-special' is called with a
  37. prefix argument from inside of a source-code block."
  38. (when current-prefix-arg
  39. (let* ((info (org-babel-get-src-block-info))
  40. (lang (first info))
  41. (params (third info))
  42. (session (cdr (assoc :session params))))
  43. (when (and info session) ;; if we are in a source-code block which has a session
  44. (funcall (intern (concat "org-babel-prep-session:" lang)) session params))))
  45. ad-do-it)
  46. (defadvice org-open-at-point (around org-babel-open-at-point activate)
  47. "If `point' is on a source code block, then open that block's
  48. results with `org-babel-open-src-block-results', otherwise defer
  49. to `org-open-at-point'."
  50. (interactive "P")
  51. (or (call-interactively #'org-babel-open-src-block-result) ad-do-it))
  52. (defun org-babel-load-in-session-maybe ()
  53. "Detect if this is context for a org-babel src-block and if so
  54. then run `org-babel-load-in-session'."
  55. (interactive)
  56. (let ((info (org-babel-get-src-block-info)))
  57. (if info (progn (org-babel-load-in-session current-prefix-arg info) t) nil)))
  58. (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
  59. (defun org-babel-pop-to-session-maybe ()
  60. "Detect if this is context for a org-babel src-block and if so
  61. then run `org-babel-pop-to-session'."
  62. (interactive)
  63. (let ((info (org-babel-get-src-block-info)))
  64. (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
  65. (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
  66. (defvar org-babel-default-header-args
  67. '((:session . "none") (:results . "replace") (:exports . "code") (:nocache) (:noweb . "no"))
  68. "Default arguments to use when evaluating a source block.")
  69. (defvar org-babel-default-inline-header-args
  70. '((:session . "none") (:results . "silent") (:exports . "results"))
  71. "Default arguments to use when evaluating an inline source block.")
  72. (defvar org-babel-src-block-regexp nil
  73. "Regexp used to test when inside of a org-babel src-block")
  74. (defvar org-babel-inline-src-block-regexp nil
  75. "Regexp used to test when on an inline org-babel src-block")
  76. (defvar org-babel-result-regexp
  77. "#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:"
  78. "Regular expression used to match result lines. If the
  79. results are associated with a hash key then the hash will be
  80. saved in the second match data.")
  81. (defvar org-babel-source-name-regexp
  82. "#\\+\\(srcname\\|source\\|function\\):[ \t]*"
  83. "Regular expression used to match a source name line.")
  84. (defvar org-babel-min-lines-for-block-output 10
  85. "If number of lines of output is equal to or exceeds this
  86. value, the output is placed in a #+begin_example...#+end_example
  87. block. Otherwise the output is marked as literal by inserting
  88. colons at the starts of the lines. This variable only takes
  89. effect if the :results output option is in effect.")
  90. (defvar org-babel-noweb-error-langs nil
  91. "List of language for which errors should be raised when the
  92. source code block satisfying a noweb reference in this language
  93. can not be resolved.")
  94. (defvar org-babel-hash-show 4
  95. "Number of initial characters to show of a hidden results hash.")
  96. (defun org-babel-named-src-block-regexp-for-name (name)
  97. "Regexp used to match named src block."
  98. (concat org-babel-source-name-regexp (regexp-quote name) "[ \t\n]*"
  99. (substring org-babel-src-block-regexp 1)))
  100. (defun org-babel-set-interpreters (var value)
  101. (set-default var value)
  102. (setq org-babel-src-block-regexp
  103. (concat "^[ \t]*#\\+begin_src[ \t]+\\(" ;; (1) lang
  104. (mapconcat 'regexp-quote value "\\|")
  105. "\\)[ \t]*"
  106. "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)" ;; (2) switches
  107. "\\([^\n]*\\)\n" ;; (3) header arguments
  108. "\\([^\000]+?\\)#\\+end_src")) ;; (4) body
  109. (setq org-babel-inline-src-block-regexp
  110. (concat "[ \f\t\n\r\v]\\(src_" ;; (1) replacement target
  111. "\\(" ;; (2) lang
  112. (mapconcat 'regexp-quote value "\\|")
  113. "\\)"
  114. "\\(\\|\\[\\(.*\\)\\]\\)" ;; (3,4) (unused, headers)
  115. "{\\([^\f\n\r\v]+\\)}" ;; (5) body
  116. "\\)")))
  117. (defun org-babel-add-interpreter (interpreter)
  118. "Add INTERPRETER to `org-babel-interpreters' and update
  119. `org-babel-src-block-regexp' appropriately."
  120. (unless (member interpreter org-babel-interpreters)
  121. (setq org-babel-interpreters (cons interpreter org-babel-interpreters))
  122. (org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
  123. (defcustom org-babel-interpreters '()
  124. "Interpreters allows for evaluation tags.
  125. This is a list of program names (as strings) that can evaluate code and
  126. insert the output into an Org-mode buffer. Valid choices are
  127. R Evaluate R code
  128. emacs-lisp Evaluate Emacs Lisp code and display the result
  129. sh Pass command to the shell and display the result
  130. perl The perl interpreter
  131. python The python interpreter
  132. ruby The ruby interpreter
  133. The source block regexp `org-babel-src-block-regexp' is updated
  134. when a new interpreter is added to this list through the
  135. customize interface. To add interpreters to this variable from
  136. lisp code use the `org-babel-add-interpreter' function."
  137. :group 'org-babel
  138. :set 'org-babel-set-interpreters
  139. :type '(set :greedy t
  140. (const "R")
  141. (const "emacs-lisp")
  142. (const "sh")
  143. (const "perl")
  144. (const "python")
  145. (const "ruby")))
  146. ;;; functions
  147. (defun org-babel-execute-src-block (&optional arg info params)
  148. "Execute the current source code block, and insert the results
  149. into the buffer. Source code execution and the collection and
  150. formatting of results can be controlled through a variety of
  151. header arguments.
  152. Optionally supply a value for INFO in the form returned by
  153. `org-babel-get-src-block-info'.
  154. Optionally supply a value for PARAMS which will be merged with
  155. the header arguments specified at the front of the source code
  156. block."
  157. (interactive)
  158. ;; (message "supplied params=%S" params) ;; debugging
  159. (let* ((info (or info (org-babel-get-src-block-info)))
  160. (lang (first info))
  161. (params (setf (third info)
  162. (sort (org-babel-merge-params (third info) params)
  163. (lambda (el1 el2) (string< (symbol-name (car el1))
  164. (symbol-name (car el2)))))))
  165. (new-hash (unless (assoc :nocache params) (org-babel-sha1-hash info)))
  166. (old-hash (org-babel-result-hash info))
  167. (body (setf (second info)
  168. (if (and (cdr (assoc :noweb params))
  169. (string= "yes" (cdr (assoc :noweb params))))
  170. (org-babel-expand-noweb-references info) (second info))))
  171. (result-params (split-string (or (cdr (assoc :results params)) "")))
  172. (result-type (cond ((member "output" result-params) 'output)
  173. ((member "value" result-params) 'value)
  174. (t 'value)))
  175. (cmd (intern (concat "org-babel-execute:" lang)))
  176. result)
  177. ;; (message "params=%S" params) ;; debugging
  178. (unless (member lang org-babel-interpreters)
  179. (error "Language is not in `org-babel-interpreters': %s" lang))
  180. (if (and (not arg) new-hash (equal new-hash old-hash))
  181. (save-excursion ;; return cached result
  182. (goto-char (org-babel-where-is-src-block-result nil info))
  183. (move-end-of-line 1) (forward-char 1)
  184. (setq result (org-babel-read-result))
  185. (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result)
  186. (setq result (funcall cmd body params))
  187. (if (eq result-type 'value)
  188. (setq result (if (and (or (member "vector" result-params)
  189. (member "table" result-params))
  190. (not (listp result)))
  191. (list (list result))
  192. result)))
  193. (org-babel-insert-result result result-params info new-hash)
  194. result)))
  195. (defun org-babel-load-in-session (&optional arg info)
  196. "Load the body of the current source-code block. Evaluate the
  197. header arguments for the source block before entering the
  198. session. After loading the body this pops open the session."
  199. (interactive)
  200. (let* ((info (or info (org-babel-get-src-block-info)))
  201. (lang (first info))
  202. (body (second info))
  203. (params (third info))
  204. (session (cdr (assoc :session params))))
  205. (unless (member lang org-babel-interpreters)
  206. (error "Language is not in `org-babel-interpreters': %s" lang))
  207. ;; if called with a prefix argument, then process header arguments
  208. (pop-to-buffer (funcall (intern (concat "org-babel-load-session:" lang)) session body params))
  209. (move-end-of-line 1)))
  210. (defun org-babel-pop-to-session (&optional arg info)
  211. "Pop to the session of the current source-code block. If
  212. called with a prefix argument then evaluate the header arguments
  213. for the source block before entering the session. Copy the body
  214. of the source block to the kill ring."
  215. (interactive)
  216. (let* ((info (or info (org-babel-get-src-block-info)))
  217. (lang (first info))
  218. (body (second info))
  219. (params (third info))
  220. (session (cdr (assoc :session params))))
  221. (unless (member lang org-babel-interpreters)
  222. (error "Language is not in `org-babel-interpreters': %s" lang))
  223. ;; copy body to the kill ring
  224. (with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
  225. ;; if called with a prefix argument, then process header arguments
  226. (if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
  227. ;; just to the session using pop-to-buffer
  228. (pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session))
  229. (move-end-of-line 1)))
  230. (defun org-babel-open-src-block-result (&optional re-run)
  231. "If `point' is on a src block then open the results of the
  232. source code block, otherwise return nil. With optional prefix
  233. argument RE-RUN the source-code block is evaluated even if
  234. results already exist."
  235. (interactive "P")
  236. (when (org-babel-get-src-block-info)
  237. (save-excursion
  238. ;; go to the results, if there aren't any then run the block
  239. (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
  240. (progn (org-babel-execute-src-block)
  241. (org-babel-where-is-src-block-result))))
  242. (move-end-of-line 1) (forward-char 1)
  243. ;; open the results
  244. (if (looking-at org-bracket-link-regexp)
  245. ;; file results
  246. (org-open-at-point)
  247. (let ((results (org-babel-read-result)))
  248. (flet ((echo-res (result)
  249. (if (stringp result) result (format "%S" result))))
  250. (pop-to-buffer (get-buffer-create "org-babel-results"))
  251. (delete-region (point-min) (point-max))
  252. (if (listp results)
  253. ;; table result
  254. (insert (orgtbl-to-generic results '(:sep "\t" :fmt echo-res)))
  255. ;; scalar result
  256. (insert (echo-res results))))))
  257. t)))
  258. (defun org-babel-execute-buffer (&optional arg)
  259. "Replace EVAL snippets in the entire buffer."
  260. (interactive "P")
  261. (save-excursion
  262. (goto-char (point-min))
  263. (while (re-search-forward org-babel-src-block-regexp nil t)
  264. (goto-char (match-beginning 0))
  265. (org-babel-execute-src-block arg)
  266. (goto-char (match-end 0)))))
  267. (defun org-babel-execute-subtree (&optional arg)
  268. "Replace EVAL snippets in the entire subtree."
  269. (interactive "P")
  270. (save-excursion
  271. (org-narrow-to-subtree)
  272. (org-babel-execute-buffer)
  273. (widen)))
  274. (defun org-babel-get-src-block-info (&optional header-vars-only)
  275. "Get information of the current source block.
  276. Returns a list
  277. (language body header-arguments-alist switches name function-args).
  278. Unless HEADER-VARS-ONLY is non-nil, any variable
  279. references provided in 'function call style' (i.e. in a
  280. parenthesised argument list following the src block name) are
  281. added to the header-arguments-alist."
  282. (let ((case-fold-search t) head info args)
  283. (if (setq head (org-babel-where-is-src-block-head))
  284. (save-excursion
  285. (goto-char head)
  286. (setq info (org-babel-parse-src-block-match))
  287. (forward-line -1)
  288. (when (looking-at (concat org-babel-source-name-regexp
  289. "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
  290. (setq info (append info (list (org-babel-clean-text-properties (match-string 2)))))
  291. ;; Note that e.g. "name()" and "name( )" result in ((:var . "")).
  292. ;; We maintain that behaviour, and the resulting non-nil sixth
  293. ;; element is relied upon in org-babel-exp-code to detect a functional-style
  294. ;; block in those cases. However, "name" without any
  295. ;; parentheses would result in the same thing, so we
  296. ;; explicitly avoid that.
  297. (if (setq args (match-string 4))
  298. (setq info (append info (list (mapcar (lambda (ref) (cons :var ref))
  299. (org-babel-ref-split-args args))))))
  300. (unless header-vars-only
  301. (setf (third info)
  302. (org-babel-merge-params (sixth info) (third info)))))
  303. info)
  304. (if (save-excursion ;; inline source block
  305. (re-search-backward "[ \f\t\n\r\v]" nil t)
  306. (looking-at org-babel-inline-src-block-regexp))
  307. (org-babel-parse-inline-src-block-match)
  308. nil)))) ;; indicate that no source block was found
  309. (defun org-babel-sha1-hash (&optional info)
  310. (interactive)
  311. (let* ((info (or info (org-babel-get-src-block-info)))
  312. (hash (sha1 (format "%s-%s" (mapconcat (lambda (arg) (format "%S" arg))
  313. (third info) ":")
  314. (second info)))))
  315. (when (interactive-p) (message hash))
  316. hash))
  317. (defun org-babel-result-hash (&optional info)
  318. (org-babel-where-is-src-block-result nil info)
  319. (org-babel-clean-text-properties (match-string 3)))
  320. (defun org-babel-hide-hash ()
  321. "Hide the hash in the current results line. Only the initial
  322. `org-babel-hash-show' characters of the hash will remain
  323. visible."
  324. (org-add-to-invisibility-spec '(org-babel-hide-hash . t))
  325. (save-excursion
  326. (when (and (re-search-forward org-babel-result-regexp nil t)
  327. (match-string 3))
  328. (let* ((start (match-beginning 3))
  329. (hide-start (+ org-babel-hash-show start))
  330. (end (match-end 3))
  331. (hash (match-string 3))
  332. ov1 ov2)
  333. (setq ov1 (org-make-overlay start hide-start))
  334. (setq ov2 (org-make-overlay hide-start end))
  335. (org-overlay-put ov2 'invisible 'org-babel-hide-hash)
  336. (org-overlay-put ov1 'babel-hash hash)))))
  337. (defun org-babel-hide-all-hashes ()
  338. "Hide the hash in the current buffer. Only the initial
  339. `org-babel-hash-show' characters of each hash will remain
  340. visible. This function should be called as part of the
  341. `org-mode-hook'."
  342. (save-excursion
  343. (while (re-search-forward org-babel-result-regexp nil t)
  344. (goto-char (match-beginning 0))
  345. (org-babel-hide-hash)
  346. (goto-char (match-end 0)))))
  347. (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
  348. (defun org-babel-hash-at-point (&optional point)
  349. "Return the value of the hash at `point'. The hash is also
  350. added as the last element of the kill ring. This can be called
  351. with C-c C-c."
  352. (interactive)
  353. (let ((hash (car (delq nil (mapcar
  354. (lambda (ol) (org-overlay-get ol 'babel-hash))
  355. (org-overlays-at (or point (point))))))))
  356. (when hash (kill-new hash) (message hash))))
  357. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
  358. (defun org-babel-result-hide-spec ()
  359. (org-add-to-invisibility-spec '(org-babel-hide-result . t)))
  360. (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
  361. (defvar org-babel-hide-result-overlays nil
  362. "Overlays hiding results.")
  363. (defun org-babel-result-hide-all ()
  364. "Fold all results in the current buffer."
  365. (interactive)
  366. (org-babel-show-result-all)
  367. (save-excursion
  368. (while (re-search-forward org-babel-result-regexp nil t)
  369. (save-excursion (goto-char (match-beginning 0))
  370. (org-babel-hide-result-toggle-maybe)))))
  371. (defun org-babel-show-result-all ()
  372. "Unfold all results in the current buffer."
  373. (mapc 'org-delete-overlay org-babel-hide-result-overlays)
  374. (setq org-babel-hide-result-overlays nil))
  375. (defun org-babel-hide-result-toggle-maybe ()
  376. "Toggle visibility of result at point."
  377. (interactive)
  378. (let ((case-fold-search t))
  379. (if (save-excursion
  380. (beginning-of-line 1)
  381. (looking-at org-babel-result-regexp))
  382. (progn (org-babel-hide-result-toggle)
  383. t) ;; to signal that we took action
  384. nil))) ;; to signal that we did not
  385. (defun org-babel-hide-result-toggle (&optional force)
  386. "Toggle the visibility of the current result."
  387. (interactive)
  388. (save-excursion
  389. (beginning-of-line)
  390. (if (re-search-forward org-babel-result-regexp nil t)
  391. (let ((start (progn (beginning-of-line 2) (- (point) 1)))
  392. (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
  393. ov)
  394. (if (memq t (mapcar (lambda (overlay)
  395. (eq (org-overlay-get overlay 'invisible)
  396. 'org-babel-hide-result))
  397. (org-overlays-at start)))
  398. (if (or (not force) (eq force 'off))
  399. (mapc (lambda (ov)
  400. (when (member ov org-babel-hide-result-overlays)
  401. (setq org-babel-hide-result-overlays
  402. (delq ov org-babel-hide-result-overlays)))
  403. (when (eq (org-overlay-get ov 'invisible)
  404. 'org-babel-hide-result)
  405. (org-delete-overlay ov)))
  406. (org-overlays-at start)))
  407. (setq ov (org-make-overlay start end))
  408. (org-overlay-put ov 'invisible 'org-babel-hide-result)
  409. ;; make the block accessible to isearch
  410. (org-overlay-put
  411. ov 'isearch-open-invisible
  412. (lambda (ov)
  413. (when (member ov org-babel-hide-result-overlays)
  414. (setq org-babel-hide-result-overlays
  415. (delq ov org-babel-hide-result-overlays)))
  416. (when (eq (org-overlay-get ov 'invisible)
  417. 'org-babel-hide-result)
  418. (org-delete-overlay ov))))
  419. (push ov org-babel-hide-result-overlays)))
  420. (error "Not looking at a result line"))))
  421. ;; org-tab-after-check-for-cycling-hook
  422. (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
  423. ;; Remove overlays when changing major mode
  424. (add-hook 'org-mode-hook
  425. (lambda () (org-add-hook 'change-major-mode-hook
  426. 'org-babel-show-result-all 'append 'local)))
  427. (defmacro org-babel-map-source-blocks (file &rest body)
  428. "Evaluate BODY forms on each source-block in FILE."
  429. (declare (indent 1))
  430. `(let ((visited-p (get-buffer (file-name-nondirectory ,file))))
  431. (save-window-excursion
  432. (find-file ,file) (goto-char (point-min))
  433. (while (re-search-forward org-babel-src-block-regexp nil t)
  434. (goto-char (match-beginning 0))
  435. (save-match-data ,@body)
  436. (goto-char (match-end 0))))
  437. (unless visited-p (kill-buffer (file-name-nondirectory file)))))
  438. (defun org-babel-params-from-properties ()
  439. "Return an association list of any source block params which
  440. may be specified in the properties of the current outline entry."
  441. (save-match-data
  442. (delq nil
  443. (mapcar
  444. (lambda (header-arg)
  445. (let ((val (or (condition-case nil
  446. (org-entry-get (point) header-arg 'selective)
  447. (error nil))
  448. (cdr (assoc header-arg org-file-properties)))))
  449. (when val
  450. ;; (message "param-from-property %s=%s" header-arg val) ;; debugging statement
  451. (cons (intern (concat ":" header-arg)) val))))
  452. '("exports" "results" "session" "tangle" "var")))))
  453. (defun org-babel-parse-src-block-match ()
  454. (let* ((lang (org-babel-clean-text-properties (match-string 1)))
  455. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  456. (switches (match-string 2))
  457. (body (org-babel-clean-text-properties (match-string 4)))
  458. (preserve-indentation (or org-src-preserve-indentation
  459. (string-match "-i\\>" switches))))
  460. (list lang
  461. ;; get src block body removing properties, protective commas, and indentation
  462. (with-temp-buffer
  463. (save-match-data
  464. (insert (org-babel-strip-protective-commas body))
  465. (unless preserve-indentation (org-do-remove-indentation))
  466. (buffer-string)))
  467. (org-babel-merge-params
  468. org-babel-default-header-args
  469. (org-babel-params-from-properties)
  470. (if (boundp lang-headers) (eval lang-headers) nil)
  471. (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) ""))))
  472. switches)))
  473. (defun org-babel-parse-inline-src-block-match ()
  474. (let* ((lang (org-babel-clean-text-properties (match-string 2)))
  475. (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
  476. (list lang
  477. (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 5)))
  478. (org-babel-merge-params
  479. org-babel-default-inline-header-args
  480. (org-babel-params-from-properties)
  481. (if (boundp lang-headers) (eval lang-headers) nil)
  482. (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 4) "")))))))
  483. (defun org-babel-parse-header-arguments (arg-string)
  484. "Parse a string of header arguments returning an alist."
  485. (if (> (length arg-string) 0)
  486. (delq nil
  487. (mapcar
  488. (lambda (arg)
  489. (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" arg)
  490. (cons (intern (concat ":" (match-string 1 arg)))
  491. (let ((raw (org-babel-chomp (match-string 2 arg))))
  492. (if (org-babel-number-p raw) raw (eval (org-babel-read raw)))))
  493. (cons (intern (concat ":" arg)) nil)))
  494. (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
  495. (defun org-babel-process-params (params)
  496. "Parse params and resolve references.
  497. Return a list (session vars result-params result-type)."
  498. (let* ((session (cdr (assoc :session params)))
  499. (vars (org-babel-ref-variables params))
  500. (result-params (split-string (or (cdr (assoc :results params)) "")))
  501. (result-type (cond ((member "output" result-params) 'output)
  502. ((member "value" result-params) 'value)
  503. (t 'value))))
  504. (list session vars result-params result-type)))
  505. (defun org-babel-where-is-src-block-head ()
  506. "Return the point at the beginning of the current source
  507. block. Specifically at the beginning of the #+BEGIN_SRC line.
  508. If the point is not on a source block then return nil."
  509. (let ((initial (point)) top bottom)
  510. (or
  511. (save-excursion ;; on a source name line
  512. (beginning-of-line 1)
  513. (and (looking-at org-babel-source-name-regexp) (forward-line 1)
  514. (looking-at org-babel-src-block-regexp)
  515. (point)))
  516. (save-excursion ;; on a #+begin_src line
  517. (beginning-of-line 1)
  518. (and (looking-at org-babel-src-block-regexp)
  519. (point)))
  520. (save-excursion ;; inside a src block
  521. (and
  522. (re-search-backward "#\\+begin_src" nil t) (setq top (point))
  523. (re-search-forward "#\\+end_src" nil t) (setq bottom (point))
  524. (< top initial) (< initial bottom)
  525. (goto-char top) (move-beginning-of-line 1)
  526. (looking-at org-babel-src-block-regexp)
  527. (point))))))
  528. (defun org-babel-goto-named-source-block (&optional name)
  529. "Go to a named source-code block."
  530. (interactive "ssource-block name: ")
  531. (let ((point (org-babel-find-named-block name)))
  532. (if point
  533. ;; taken from `org-open-at-point'
  534. (progn (goto-char point) (org-show-context))
  535. (message "source-code block '%s' not found in this buffer" name))))
  536. (defun org-babel-find-named-block (name)
  537. "Find a named source-code block.
  538. Return the location of the source block identified by source
  539. NAME, or nil if no such block exists. Set match data according to
  540. org-babel-named-src-block-regexp."
  541. (save-excursion
  542. (let ((case-fold-search t)
  543. (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
  544. (goto-char (point-min))
  545. (when (or (re-search-forward regexp nil t)
  546. (re-search-backward regexp nil t))
  547. (match-beginning 0)))))
  548. (defun org-babel-find-named-result (name)
  549. "Return the location of the result named NAME in the current
  550. buffer or nil if no such result exists."
  551. (save-excursion
  552. (goto-char (point-min))
  553. (when (re-search-forward
  554. (concat org-babel-result-regexp "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
  555. (move-beginning-of-line 0) (point))))
  556. (defun org-babel-where-is-src-block-result (&optional insert info hash)
  557. "Return the point at the beginning of the result of the current
  558. source block. Specifically at the beginning of the results line.
  559. If no result exists for this block then create a results line
  560. following the source block."
  561. (save-excursion
  562. (let* ((on-lob-line (progn (beginning-of-line 1)
  563. (looking-at org-babel-lob-one-liner-regexp)))
  564. (name (if on-lob-line (first (org-babel-lob-get-info))
  565. (fifth (or info (org-babel-get-src-block-info)))))
  566. (head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
  567. (when head (goto-char head))
  568. (or (and name (org-babel-find-named-result name))
  569. (and (or on-lob-line (re-search-forward "#\\+end_src" nil t))
  570. (progn (move-end-of-line 1)
  571. (if (eobp) (insert "\n") (forward-char 1))
  572. (setq end (point))
  573. (or (and (not name)
  574. (progn ;; unnamed results line already exists
  575. (re-search-forward "[^ \f\t\n\r\v]" nil t)
  576. (move-beginning-of-line 1)
  577. (looking-at (concat org-babel-result-regexp "\n"))))
  578. ;; or (with optional insert) back up and make one ourselves
  579. (when insert
  580. (goto-char end)
  581. (if (looking-at "[\n\r]") (forward-char 1) (insert "\n"))
  582. (insert (concat "#+results" (if hash (concat "["hash"]"))
  583. ":"(if name (concat " " name)) "\n"))
  584. (move-beginning-of-line 0)
  585. (if hash (org-babel-hide-hash)) t)))
  586. (point))))))
  587. (defun org-babel-read-result ()
  588. "Read the result at `point' into emacs-lisp."
  589. (let ((case-fold-search t) result-string)
  590. (cond
  591. ((org-at-table-p) (org-babel-read-table))
  592. ((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
  593. ((looking-at ": ")
  594. (setq result-string
  595. (org-babel-trim
  596. (mapconcat (lambda (line) (if (and (> (length line) 1)
  597. (string= ": " (substring line 0 2)))
  598. (substring line 2)
  599. line))
  600. (split-string
  601. (buffer-substring (point) (org-babel-result-end)) "[\r\n]+")
  602. "\n")))
  603. (or (org-babel-number-p result-string) result-string))
  604. ((looking-at org-babel-result-regexp)
  605. (save-excursion (forward-line 1) (org-babel-read-result))))))
  606. (defun org-babel-read-table ()
  607. "Read the table at `point' into emacs-lisp."
  608. (mapcar (lambda (row)
  609. (if (and (symbolp row) (equal row 'hline)) row
  610. (mapcar #'org-babel-read row)))
  611. (org-table-to-lisp)))
  612. (defun org-babel-insert-result (result &optional result-params info hash)
  613. "Insert RESULT into the current buffer after the end of the
  614. current source block. With optional argument RESULT-PARAMS
  615. controls insertion of results in the org-mode file.
  616. RESULT-PARAMS can take the following values...
  617. replace - (default option) insert results after the source block
  618. replacing any previously inserted results
  619. silent -- no results are inserted
  620. file ---- the results are interpreted as a file path, and are
  621. inserted into the buffer using the Org-mode file syntax
  622. raw ----- results are added directly to the org-mode file. This
  623. is a good option if you code block will output org-mode
  624. formatted text.
  625. org ----- this is the same as the 'raw' option
  626. html ---- results are added inside of a #+BEGIN_HTML block. This
  627. is a good option if you code block will output html
  628. formatted text.
  629. latex --- results are added inside of a #+BEGIN_LATEX block.
  630. This is a good option if you code block will output
  631. latex formatted text.
  632. code ---- the results are extracted in the syntax of the source
  633. code of the language being evaluated and are added
  634. inside of a #+BEGIN_SRC block with the source-code
  635. language set appropriately."
  636. (if (stringp result)
  637. (progn
  638. (setq result (org-babel-clean-text-properties result))
  639. (when (member "file" result-params)
  640. (setq result (org-babel-result-to-file result))))
  641. (unless (listp result) (setq result (format "%S" result))))
  642. (if (and result-params (member "replace" result-params)
  643. (not (member "silent" result-params)))
  644. (org-babel-remove-result info))
  645. (if (= (length result) 0)
  646. (if (member "value" result-params)
  647. (message "No result returned by source block")
  648. (message "Source block produced no output"))
  649. (if (and result-params (member "silent" result-params))
  650. (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
  651. result)
  652. (when (and (stringp result) ;; ensure results end in a newline
  653. (not (or (string-equal (substring result -1) "\n")
  654. (string-equal (substring result -1) "\r"))))
  655. (setq result (concat result "\n")))
  656. (save-excursion
  657. (let ((existing-result (org-babel-where-is-src-block-result t info hash))
  658. (results-switches (cdr (assoc :results_switches (third info)))))
  659. (when existing-result (goto-char existing-result) (forward-line 1))
  660. (setq results-switches
  661. (if results-switches (concat " " results-switches) ""))
  662. (cond
  663. ;; assume the result is a table if it's not a string
  664. ((not (stringp result))
  665. (insert (concat (orgtbl-to-orgtbl
  666. (if (and (listp (car result))
  667. (listp (cdr (car result))))
  668. result (list result))
  669. '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
  670. (forward-line -1) (org-cycle))
  671. ((member "file" result-params)
  672. (insert result))
  673. ((member "html" result-params)
  674. (insert (format "#+BEGIN_HTML%s\n%s#+END_HTML\n" results-switches result)))
  675. ((member "latex" result-params)
  676. (insert (format "#+BEGIN_LaTeX%s\n%s#+END_LaTeX\n" results-switches result)))
  677. ((member "code" result-params)
  678. (insert (format "#+BEGIN_SRC %s%s\n%s#+END_SRC\n" lang results-switches result)))
  679. ((or (member "raw" result-params) (member "org" result-params))
  680. (save-excursion (insert result)) (if (org-at-table-p) (org-cycle)))
  681. (t
  682. (org-babel-examplize-region
  683. (point) (progn (insert result) (point)) results-switches)))))
  684. (message "finished"))))
  685. (defun org-babel-result-to-org-string (result)
  686. "Return RESULT as a string in org-mode format. This function
  687. relies on `org-babel-insert-result'."
  688. (with-temp-buffer (org-babel-insert-result result) (buffer-string)))
  689. (defun org-babel-remove-result (&optional info)
  690. "Remove the result of the current source block."
  691. (interactive)
  692. (let ((location (org-babel-where-is-src-block-result nil info)) start)
  693. (when location
  694. (save-excursion
  695. (goto-char location) (setq start (point)) (forward-line 1)
  696. (delete-region start (org-babel-result-end))))))
  697. (defun org-babel-result-end ()
  698. "Return the point at the end of the current set of results"
  699. (save-excursion
  700. (if (org-at-table-p)
  701. (progn (goto-char (org-table-end)) (point))
  702. (let ((case-fold-search t))
  703. (cond
  704. ((looking-at "#\\+begin_latex")
  705. (search-forward "#+end_latex" nil t)
  706. (forward-line 1))
  707. ((looking-at "#\\+begin_html")
  708. (search-forward "#+end_html" nil t)
  709. (forward-line 1))
  710. ((looking-at "#\\+begin_example")
  711. (search-forward "#+end_example" nil t)
  712. (forward-line 1))
  713. ((looking-at "#\\+begin_src")
  714. (search-forward "#+end_src" nil t)
  715. (forward-line 1))
  716. (t (progn (while (looking-at "\\(: \\|\\[\\[\\)")
  717. (forward-line 1))))))
  718. (point))))
  719. (defun org-babel-result-to-file (result)
  720. "Return an `org-mode' link with the path being the value or
  721. RESULT, and the display being the `file-name-nondirectory' if
  722. non-nil."
  723. (concat "[[file:" result "]]"))
  724. (defun org-babel-examplize-region (beg end &optional results-switches)
  725. "Comment out region using the ': ' org example quote."
  726. (interactive "*r")
  727. (let ((size (abs (- (line-number-at-pos end)
  728. (line-number-at-pos beg)))))
  729. (save-excursion
  730. (cond ((= size 0)
  731. (error "This should be impossible: a newline was appended to result if missing"))
  732. ((< size org-babel-min-lines-for-block-output)
  733. (goto-char beg)
  734. (dotimes (n size)
  735. (move-beginning-of-line 1) (insert ": ") (forward-line 1)))
  736. (t
  737. (goto-char beg)
  738. (insert (if results-switches
  739. (format "#+begin_example%s\n" results-switches)
  740. "#+begin_example\n"))
  741. (forward-char (- end beg))
  742. (insert "#+end_example\n"))))))
  743. (defun org-babel-merge-params (&rest plists)
  744. "Combine all parameter association lists in PLISTS. Later
  745. elements of PLISTS override the values of previous element. This
  746. takes into account some special considerations for certain
  747. parameters when merging lists."
  748. (let ((results-exclusive-groups
  749. '(("file" "vector" "table" "scalar" "raw" "org" "html" "latex" "code" "pp")
  750. ("replace" "silent")
  751. ("output" "value")))
  752. (exports-exclusive-groups
  753. '(("code" "results" "both" "none")))
  754. params results exports tangle noweb cache vars var ref)
  755. (flet ((e-merge (exclusive-groups &rest result-params)
  756. ;; maintain exclusivity of mutually exclusive parameters
  757. (let (output)
  758. (mapc (lambda (new-params)
  759. (mapc (lambda (new-param)
  760. (mapc (lambda (exclusive-group)
  761. (when (member new-param exclusive-group)
  762. (mapcar (lambda (excluded-param)
  763. (setq output (delete excluded-param output)))
  764. exclusive-group)))
  765. exclusive-groups)
  766. (setq output (org-uniquify (cons new-param output))))
  767. new-params))
  768. result-params)
  769. output)))
  770. (mapc (lambda (plist)
  771. (mapc (lambda (pair)
  772. (case (car pair)
  773. (:var
  774. ;; we want only one specification per variable
  775. (when (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*=[ \t]*\\([^\f\n\r\v]+\\)$" (cdr pair))
  776. ;; TODO: When is this not true?
  777. (setq var (intern (match-string 1 (cdr pair)))
  778. ref (match-string 2 (cdr pair))
  779. vars (cons (cons var ref) (assq-delete-all var vars)))))
  780. (:results
  781. (setq results
  782. (e-merge results-exclusive-groups results (split-string (cdr pair)))))
  783. (:file
  784. (when (cdr pair)
  785. (setq results (e-merge results-exclusive-groups results '("file")))
  786. (unless (or (member "both" exports) (member "none" exports))
  787. (setq exports (e-merge exports-exclusive-groups exports '("results"))))
  788. (setq params (cons pair (assq-delete-all (car pair) params)))))
  789. (:exports
  790. (setq exports (e-merge exports-exclusive-groups
  791. exports (split-string (cdr pair)))))
  792. (:tangle
  793. (setq tangle (e-merge '(("yes" "no"))
  794. tangle (split-string (or (cdr pair) "")))))
  795. (:noweb
  796. (setq noweb (e-merge '(("yes" "no"))
  797. noweb (split-string (or (cdr pair) "")))))
  798. (:cache (setq cache t)) (:nocache (setq cache nil))
  799. (t ;; replace: this covers e.g. :session
  800. (setq params (cons pair (assq-delete-all (car pair) params))))))
  801. plist))
  802. plists))
  803. (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
  804. (while vars (setq params (cons (cons :var (pop vars)) params)))
  805. (cons (if cache (list :cache) (list :nocache))
  806. (cons (cons :noweb (mapconcat 'identity noweb " "))
  807. (cons (cons :tangle (mapconcat 'identity tangle " "))
  808. (cons (cons :exports (mapconcat 'identity exports " "))
  809. (cons (cons :results (mapconcat 'identity results " "))
  810. params)))))))
  811. (defun org-babel-expand-noweb-references (&optional info parent-buffer)
  812. "This function expands Noweb style references in the body of
  813. the current source-code block. For example the following
  814. reference would be replaced with the body of the source-code
  815. block named 'example-block'.
  816. <<example-block>>
  817. Note that any text preceding the <<foo>> construct on a line will
  818. be interposed between the lines of the replacement text. So for
  819. example if <<foo>> is placed behind a comment, then the entire
  820. replacement text will also be commented.
  821. This function must be called from inside of the buffer containing
  822. the source-code block which holds BODY.
  823. In addition the following syntax can be used to insert the
  824. results of evaluating the source-code block named 'example-block'.
  825. <<example-block()>>
  826. Any optional arguments can be passed to example-block by placing
  827. the arguments inside the parenthesis following the convention
  828. defined by `org-babel-lob'. For example
  829. <<example-block(a=9)>>
  830. would set the value of argument \"a\" equal to \"9\". Note that
  831. these arguments are not evaluated in the current source-code
  832. block but are passed literally to the \"example-block\"."
  833. (let* ((parent-buffer (or parent-buffer (current-buffer)))
  834. (info (or info (org-babel-get-src-block-info)))
  835. (lang (first info))
  836. (body (second info))
  837. (new-body "") index source-name evaluate prefix)
  838. (flet ((nb-add (text)
  839. (setq new-body (concat new-body text))))
  840. (with-temp-buffer
  841. (insert body) (goto-char (point-min))
  842. (funcall (intern (concat (or (and (cdr (assoc lang org-src-lang-modes))
  843. (symbol-name
  844. (cdr (assoc lang org-src-lang-modes))))
  845. lang) "-mode")))
  846. (setq index (point))
  847. (while (and (re-search-forward "<<\\(.+?\\)>>" nil t))
  848. (save-match-data (setf source-name (match-string 1)))
  849. (save-match-data (setq evaluate (string-match "\(.*\)" source-name)))
  850. (save-match-data
  851. (setq prefix (buffer-substring (match-beginning 0)
  852. (save-excursion
  853. (move-beginning-of-line 1) (point)))))
  854. ;; add interval to new-body (removing noweb reference)
  855. (goto-char (match-beginning 0))
  856. (nb-add (buffer-substring index (point)))
  857. (goto-char (match-end 0))
  858. (setq index (point))
  859. (nb-add (save-excursion
  860. (set-buffer parent-buffer)
  861. (mapconcat ;; interpose `prefix' between every line
  862. #'identity
  863. (split-string
  864. (if evaluate
  865. (let ((raw (org-babel-ref-resolve-reference
  866. source-name nil)))
  867. (if (stringp raw) raw (format "%S" raw)))
  868. (let ((point (org-babel-find-named-block source-name)))
  869. (if point
  870. (save-excursion
  871. (goto-char point)
  872. (org-babel-trim (org-babel-expand-noweb-references
  873. (org-babel-get-src-block-info))))
  874. ;; optionally raise an error if named
  875. ;; source-block doesn't exist
  876. (if (member lang org-babel-noweb-error-langs)
  877. (error
  878. "<<%s>> could not be resolved (see `org-babel-noweb-error-langs')"
  879. source-name)
  880. "")))) "[\n\r]") (concat "\n" prefix)))))
  881. (nb-add (buffer-substring index (point-max)))))
  882. new-body))
  883. (defun org-babel-clean-text-properties (text)
  884. "Strip all properties from text return."
  885. (set-text-properties 0 (length text) nil text) text)
  886. (defun org-babel-strip-protective-commas (body)
  887. "Strip protective commas from bodies of source blocks."
  888. (replace-regexp-in-string "^,#" "#" body))
  889. (defun org-babel-read (cell)
  890. "Convert the string value of CELL to a number if appropriate.
  891. Otherwise if cell looks like lisp (meaning it starts with a
  892. \"(\" or a \"'\") then read it as lisp, otherwise return it
  893. unmodified as a string.
  894. This is taken almost directly from `org-read-prop'."
  895. (if (and (stringp cell) (not (equal cell "")))
  896. (or (org-babel-number-p cell)
  897. (if (or (equal "(" (substring cell 0 1))
  898. (equal "'" (substring cell 0 1)))
  899. (read cell)
  900. (progn (set-text-properties 0 (length cell) nil cell) cell)))
  901. cell))
  902. (defun org-babel-number-p (string)
  903. "Return t if STRING represents a number"
  904. (if (and (string-match "^-?[[:digit:]]*\\.?[[:digit:]]*$" string)
  905. (= (match-end 0) (length string)))
  906. (string-to-number string)))
  907. (defun org-babel-import-elisp-from-file (file-name)
  908. "Read the results located at FILE-NAME into an elisp table. If
  909. the table is trivial, then return it as a scalar."
  910. (let (result)
  911. (with-temp-buffer
  912. (condition-case nil
  913. (progn
  914. (org-table-import file-name nil)
  915. (delete-file file-name)
  916. (setq result (mapcar (lambda (row)
  917. (mapcar #'org-babel-string-read row))
  918. (org-table-to-lisp))))
  919. (error nil)))
  920. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  921. (if (consp (car result))
  922. (if (null (cdr (car result)))
  923. (caar result)
  924. result)
  925. (car result))
  926. result)))
  927. (defun org-babel-string-read (cell)
  928. "Strip nested \"s from around strings in exported R values."
  929. (org-babel-read (or (and (stringp cell)
  930. (string-match "\\\"\\(.+\\)\\\"" cell)
  931. (match-string 1 cell))
  932. cell)))
  933. (defun org-babel-reverse-string (string)
  934. (apply 'string (reverse (string-to-list string))))
  935. (defun org-babel-chomp (string &optional regexp)
  936. "Remove any trailing space or carriage returns characters from
  937. STRING. Default regexp used is \"[ \f\t\n\r\v]\" but can be
  938. overwritten by specifying a regexp as a second argument."
  939. (let ((regexp (or regexp "[ \f\t\n\r\v]")))
  940. (while (and (> (length string) 0) (string-match regexp (substring string -1)))
  941. (setq string (substring string 0 -1)))
  942. string))
  943. (defun org-babel-trim (string &optional regexp)
  944. "Like `org-babel-chomp' only it runs on both the front and back of the string"
  945. (org-babel-chomp (org-babel-reverse-string
  946. (org-babel-chomp (org-babel-reverse-string string) regexp)) regexp))
  947. (provide 'org-babel)
  948. ;;; org-babel.el ends here