org-babel.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. (defun org-babel-pop-to-session-maybe ()
  34. "Detect if this is context for a org-babel src-block and if so
  35. then run `org-babel-pop-to-session'."
  36. (interactive)
  37. (let ((info (org-babel-get-src-block-info)))
  38. (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
  39. (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
  40. (defvar org-babel-default-header-args '((:session . "none") (:results . "replace"))
  41. "Default arguments to use when evaluating a source block.")
  42. (defvar org-babel-default-inline-header-args '((:results . "silent") (:exports . "results"))
  43. "Default arguments to use when evaluating an inline source block.")
  44. (defvar org-babel-src-block-regexp nil
  45. "Regexp used to test when inside of a org-babel src-block")
  46. (defvar org-babel-inline-src-block-regexp nil
  47. "Regexp used to test when on an inline org-babel src-block")
  48. (defun org-babel-named-src-block-regexp-for-name (name)
  49. "Regexp used to match named src block."
  50. (concat "#\\+srcname:[ \t]*" (regexp-quote name) "[ \t\n]*"
  51. org-babel-src-block-regexp))
  52. (defun org-babel-set-interpreters (var value)
  53. (set-default var value)
  54. (setq org-babel-src-block-regexp
  55. (concat "#\\+begin_src \\("
  56. (mapconcat 'regexp-quote value "\\|")
  57. "\\)[ \t]*"
  58. "\\([ \t]+\\([^\n]+\\)\\)?\n" ;; match header arguments
  59. "\\([^\000]+?\\)#\\+end_src"))
  60. (setq org-babel-inline-src-block-regexp
  61. (concat "src_\\("
  62. (mapconcat 'regexp-quote value "\\|")
  63. "\\)"
  64. "\\(\\|\\[\\(.*\\)\\]\\)"
  65. "{\\([^\n]+\\)}")))
  66. (defun org-babel-add-interpreter (interpreter)
  67. "Add INTERPRETER to `org-babel-interpreters' and update
  68. `org-babel-src-block-regexp' appropriately."
  69. (unless (member interpreter org-babel-interpreters)
  70. (setq org-babel-interpreters (cons interpreter org-babel-interpreters))
  71. ;; (add-to-list 'org-babel-session-defaults (cons interpreter (format "org-babel-%s" interpreter)))
  72. (org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
  73. (defcustom org-babel-interpreters '()
  74. "Interpreters allows for evaluation tags.
  75. This is a list of program names (as strings) that can evaluate code and
  76. insert the output into an Org-mode buffer. Valid choices are
  77. R Evaluate R code
  78. emacs-lisp Evaluate Emacs Lisp code and display the result
  79. sh Pass command to the shell and display the result
  80. perl The perl interpreter
  81. python The python interpreter
  82. ruby The ruby interpreter
  83. The source block regexp `org-babel-src-block-regexp' is updated
  84. when a new interpreter is added to this list through the
  85. customize interface. To add interpreters to this variable from
  86. lisp code use the `org-babel-add-interpreter' function."
  87. :group 'org-babel
  88. :set 'org-babel-set-interpreters
  89. :type '(set :greedy t
  90. (const "R")
  91. (const "emacs-lisp")
  92. (const "sh")
  93. (const "perl")
  94. (const "python")
  95. (const "ruby")))
  96. ;;; functions
  97. (defun org-babel-pop-to-session (&optional arg info)
  98. "Pop to the session of the current source-code block. If
  99. called with a prefix argument then evaluate the header arguments
  100. for the source block before entering the session. Copy the body
  101. of the source block to the kill ring."
  102. (interactive)
  103. (let* ((info (or info (org-babel-get-src-block-info)))
  104. (lang (first info))
  105. (body (second info))
  106. (params (third info))
  107. (session (cdr (assoc :session params))))
  108. (unless (member lang org-babel-interpreters)
  109. (error "Language is not in `org-babel-interpreters': %s" lang))
  110. ;; copy body to the kill ring
  111. (with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
  112. ;; if called with a prefix argument, then process header arguments
  113. (if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
  114. ;; just to the session using pop-to-buffer
  115. (pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session))
  116. (move-end-of-line 1)))
  117. (defun org-babel-execute-src-block (&optional arg info params)
  118. "Execute the current source code block, and dump the results
  119. into the buffer immediately following the block. Results are
  120. commented by `org-toggle-fixed-width-section'. With optional
  121. prefix don't dump results into buffer but rather return the
  122. results in raw elisp (this is useful for automated execution of a
  123. source block).
  124. Optionally supply a value for INFO in the form returned by
  125. `org-babel-get-src-block-info'.
  126. Optionally supply a value for PARAMS which will be merged with
  127. the header arguments specified at the source code block."
  128. (interactive)
  129. (let* ((info (or info (org-babel-get-src-block-info)))
  130. (lang (first info))
  131. (body (second info))
  132. (params (org-babel-merge-params
  133. (third info) (org-babel-get-src-block-function-args) params))
  134. (processed-params (org-babel-process-params params))
  135. (result-params (third processed-params))
  136. (result-type (fourth processed-params))
  137. (cmd (intern (concat "org-babel-execute:" lang)))
  138. result)
  139. ;; (message (format "params=%S" params)) ;; debugging
  140. (unless (member lang org-babel-interpreters)
  141. (error "Language is not in `org-babel-interpreters': %s" lang))
  142. (when arg (setq result-params (cons "silent" result-params)))
  143. (setq result
  144. (org-babel-process-result
  145. (multiple-value-bind (session vars result-params result-type) processed-params
  146. (funcall cmd body params)) result-type))
  147. (org-babel-insert-result result result-params)
  148. (case result-type (output nil) (value result))))
  149. (defun org-babel-process-result (result result-type)
  150. "This doesn't do anything currently.
  151. You can see below the various fragments of results-processing
  152. code that were present in the language-specific files. Out of
  153. those fragments, I've moved the
  154. org-babel-python-table-or-results and
  155. org-babel-import-elisp-from-file functionality into the
  156. org-babel-*-evaluate functions. I think those should only be
  157. used in the :results value case, as in the 'output case we are
  158. not concerned with creating elisp versions of results.
  159. The rest of the functionality below, concerned with vectorising
  160. or scalarising results is commented out, has not yet been
  161. replaced, and may need to be reinstated in this function. "
  162. result)
  163. ;; ;; ruby
  164. ;; (if (member "scalar" result-params)
  165. ;; results
  166. ;; (case result-type ;; process results based on the result-type
  167. ;; ('output (let ((tmp-file (make-temp-file "org-babel-ruby")))
  168. ;; (with-temp-file tmp-file (insert results))
  169. ;; (org-babel-import-elisp-from-file tmp-file)))
  170. ;; ('value (org-babel-ruby-table-or-results results))))))
  171. ;; python
  172. ;; (if (member "scalar" result-params)
  173. ;; results
  174. ;; (setq result (case result-type ;; process results based on the result-type
  175. ;; ('output (let ((tmp-file (make-temp-file "org-babel-python")))
  176. ;; (with-temp-file tmp-file (insert results))
  177. ;; (org-babel-import-elisp-from-file tmp-file)))
  178. ;; ('value (org-babel-python-table-or-results results))))
  179. ;; (if (and (member "vector" results) (not (listp results)))
  180. ;; (list (list results))
  181. ;; results))))
  182. ;; ;; sh
  183. ;; (if (member "scalar" result-params)
  184. ;; results
  185. ;; (setq results (let ((tmp-file (make-temp-file "org-babel-shell")))
  186. ;; (with-temp-file tmp-file (insert results))
  187. ;; (org-babel-import-elisp-from-file tmp-file)))
  188. ;; (if (and (member "vector" results) (not (listp results)))
  189. ;; (list (list results))
  190. ;; results))))
  191. ;; ;; R
  192. ;; (setq results (if (member "scalar" result-params)
  193. ;; results
  194. ;; (let ((tmp-file (make-temp-file "org-babel-R")))
  195. ;; (with-temp-file tmp-file (insert results))
  196. ;; (org-babel-import-elisp-from-file tmp-file))))
  197. ;; (if (and (member "vector" result-params) (not (listp results)))
  198. ;; (list (list results))
  199. ;; results))))
  200. ;; ;; rest of org-babel-execute-src-block
  201. ;; ;; possibly force result into a vector
  202. ;; (if (and (not (listp result)) (cdr (assoc :results params))
  203. ;; (member "vector" (split-string (cdr (assoc :results params)))))
  204. ;; (setq result (list result)))
  205. ;; result))
  206. (defun org-babel-execute-buffer (&optional arg)
  207. "Replace EVAL snippets in the entire buffer."
  208. (interactive "P")
  209. (save-excursion
  210. (goto-char (point-min))
  211. (while (re-search-forward org-babel-src-block-regexp nil t)
  212. (goto-char (match-beginning 0))
  213. (org-babel-execute-src-block arg)
  214. (goto-char (match-end 0)))))
  215. (defun org-babel-execute-subtree (&optional arg)
  216. "Replace EVAL snippets in the entire subtree."
  217. (interactive "P")
  218. (save-excursion
  219. (org-narrow-to-subtree)
  220. (org-babel-execute-buffer)
  221. (widen)))
  222. (defun org-babel-get-src-block-name ()
  223. "Return the name of the current source block if one exists.
  224. This function is analogous to org-babel-lob-get-info. For both
  225. functions, after they are called, (match-string 1) matches the
  226. function name, and (match-string 2) matches the function
  227. arguments inside the parentheses. I think perhaps these functions
  228. should be renamed to bring out this similarity, perhaps involving
  229. the word 'call'."
  230. (let ((case-fold-search t)
  231. (head (org-babel-where-is-src-block-head)))
  232. (if head
  233. (save-excursion
  234. (goto-char head)
  235. (if (save-excursion
  236. (forward-line -1)
  237. (looking-at "#\\+srcname:[ \f\t\n\r\v]*\\([^ \f\t\n\r\v]+\\)\(\\(.*\\)\)"))
  238. (org-babel-clean-text-properties (match-string 1)))))))
  239. (defun org-babel-get-src-block-info ()
  240. "Return the information of the current source block as a list
  241. of the following form. (language body header-arguments-alist)"
  242. (let ((case-fold-search t) head)
  243. (if (setq head (org-babel-where-is-src-block-head))
  244. (save-excursion (goto-char head) (org-babel-parse-src-block-match))
  245. (if (save-excursion ;; inline source block
  246. (re-search-backward "[ \f\t\n\r\v]" nil t)
  247. (forward-char 1)
  248. (looking-at org-babel-inline-src-block-regexp))
  249. (org-babel-parse-inline-src-block-match)
  250. nil)))) ;; indicate that no source block was found
  251. (defun org-babel-get-src-block-function-args ()
  252. (when (org-babel-get-src-block-name)
  253. (mapcar (lambda (ref) (cons :var ref))
  254. (split-string (org-babel-clean-text-properties (match-string 2))
  255. ",[ \f\t\n\r\v]*"))))
  256. (defmacro org-babel-map-source-blocks (file &rest body)
  257. "Evaluate BODY forms on each source-block in FILE."
  258. (declare (indent 1))
  259. `(save-window-excursion
  260. (find-file ,file) (goto-char (point-min))
  261. (while (re-search-forward org-babel-src-block-regexp nil t)
  262. (goto-char (match-beginning 0))
  263. (save-match-data ,@body)
  264. (goto-char (match-end 0)))))
  265. (defun org-babel-parse-src-block-match ()
  266. (list (org-babel-clean-text-properties (match-string 1))
  267. (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 4)))
  268. (org-babel-merge-params org-babel-default-header-args
  269. (org-babel-parse-header-arguments
  270. (org-babel-clean-text-properties (or (match-string 3) ""))))))
  271. (defun org-babel-parse-inline-src-block-match ()
  272. (list (org-babel-clean-text-properties (match-string 1))
  273. (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 4)))
  274. (org-combine-plists org-babel-default-inline-header-args
  275. (org-babel-parse-header-arguments
  276. (org-babel-clean-text-properties (or (match-string 3) ""))))))
  277. (defun org-babel-parse-header-arguments (arg-string)
  278. "Parse a string of header arguments returning an alist."
  279. (if (> (length arg-string) 0)
  280. (delq nil
  281. (mapcar
  282. (lambda (arg)
  283. (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" arg)
  284. (cons (intern (concat ":" (match-string 1 arg)))
  285. (org-babel-chomp (match-string 2 arg)))
  286. (cons (intern (concat ":" arg)) nil)))
  287. (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
  288. (defun org-babel-process-params (params)
  289. "Parse params and resolve references.
  290. Return a list (session vars result-params result-type). These are
  291. made available to the org-babel-execute:LANG functions via
  292. multiple-value-bind."
  293. (let* ((session (cdr (assoc :session params)))
  294. (vars (org-babel-ref-variables params))
  295. (result-params (split-string (or (cdr (assoc :results params)) "")))
  296. (result-type (cond ((member "output" result-params) 'output)
  297. ((member "value" result-params) 'value)
  298. (t 'value))))
  299. (list session vars result-params result-type)))
  300. (defun org-babel-where-is-src-block-head ()
  301. "Return the point at the beginning of the current source
  302. block. Specifically at the beginning of the #+BEGIN_SRC line.
  303. If the point is not on a source block then return nil."
  304. (let ((initial (point)) top bottom)
  305. (or
  306. (save-excursion ;; on a #+srcname: line
  307. (beginning-of-line 1)
  308. (and (looking-at "#\\+srcname") (forward-line 1)
  309. (looking-at org-babel-src-block-regexp)
  310. (point)))
  311. (save-excursion ;; on a #+begin_src line
  312. (beginning-of-line 1)
  313. (and (looking-at org-babel-src-block-regexp)
  314. (point)))
  315. (save-excursion ;; inside a src block
  316. (and
  317. (re-search-backward "#\\+begin_src" nil t) (setq top (point))
  318. (re-search-forward "#\\+end_src" nil t) (setq bottom (point))
  319. (< top initial) (< initial bottom)
  320. (goto-char top) (looking-at org-babel-src-block-regexp)
  321. (point))))))
  322. (defun org-babel-goto-named-source-block (&optional name)
  323. "Go to a named source-code block."
  324. (interactive "ssource-block name: ")
  325. (let ((point (org-babel-find-named-block name)))
  326. (if point
  327. ;; taken from `org-open-at-point'
  328. (progn (goto-char point) (org-show-context))
  329. (message "source-code block '%s' not found in this buffer" name))))
  330. (defun org-babel-find-named-block (name)
  331. "Find a named source-code block.
  332. Return the location of the source block identified by
  333. #+srcname NAME, or nil if no such block exists. Set match data
  334. according to org-babel-named-src-block-regexp."
  335. (save-excursion
  336. (let ((case-fold-search t)
  337. (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
  338. (goto-char (point-min))
  339. (when (or (re-search-forward regexp nil t)
  340. (re-search-backward regexp nil t))
  341. (match-beginning 0)))))
  342. (defun org-babel-find-named-result (name)
  343. "Return the location of the result named NAME in the current
  344. buffer or nil if no such result exists."
  345. (save-excursion
  346. (goto-char (point-min))
  347. (when (re-search-forward (concat "#\\+resname:[ \t]*" (regexp-quote name)) nil t)
  348. (move-beginning-of-line 1) (point))))
  349. (defun org-babel-where-is-src-block-result ()
  350. "Return the point at the beginning of the result of the current
  351. source block. Specifically at the beginning of the #+RESNAME:
  352. line. If no result exists for this block then create a
  353. #+RESNAME: line following the source block."
  354. (save-excursion
  355. (let* ((on-lob-line (progn (beginning-of-line 1)
  356. (looking-at org-babel-lob-one-liner-regexp)))
  357. (name (if on-lob-line (org-babel-lob-get-info) (org-babel-get-src-block-name)))
  358. end head)
  359. (unless on-lob-line (goto-char (org-babel-where-is-src-block-head)))
  360. (or (and name (message name) (org-babel-find-named-result name))
  361. (and (or on-lob-line (re-search-forward "#\\+end_src" nil t))
  362. (progn (move-end-of-line 1)
  363. (if (eobp) (insert "\n") (forward-char 1))
  364. (setq end (point))
  365. (or (progn ;; either an unnamed #+resname: line already exists
  366. (re-search-forward "[^ \f\t\n\r\v]" nil t)
  367. (move-beginning-of-line 1) (looking-at "#\\+resname:"))
  368. (progn ;; or we need to back up and make one ourselves
  369. (goto-char end) (open-line 2) (forward-char 1)
  370. (insert (concat "#+resname:" (if name (concat " " name))))
  371. (move-beginning-of-line 1) t)))
  372. (point))))))
  373. (defun org-babel-insert-result (result &optional insert)
  374. "Insert RESULT into the current buffer after the end of the
  375. current source block. With optional argument INSERT controls
  376. insertion of results in the org-mode file. INSERT can take the
  377. following values...
  378. t ------ the default options, simply insert the results after the
  379. source block
  380. replace - insert results after the source block replacing any
  381. previously inserted results
  382. silent -- no results are inserted"
  383. (if (stringp result)
  384. (progn
  385. (setq result (org-babel-clean-text-properties result))
  386. (if (member "file" insert) (setq result (org-babel-result-to-file result))))
  387. (unless (listp result) (setq result (format "%S" result))))
  388. (if (and insert (member "replace" insert)) (org-babel-remove-result))
  389. (if (= (length result) 0)
  390. (if (member "value" result-params)
  391. (message "No result returned by source block")
  392. (message "Source block produced no output"))
  393. (if (and insert (member "silent" insert))
  394. (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result)
  395. (when (and (stringp result) ;; ensure results end in a newline
  396. (not (or (string-equal (substring result -1) "\n")
  397. (string-equal (substring result -1) "\r"))))
  398. (setq result (concat result "\n")))
  399. (save-excursion
  400. (goto-char (org-babel-where-is-src-block-result)) (forward-line 1)
  401. (if (stringp result) ;; assume the result is a table if it's not a string
  402. (if (member "file" insert)
  403. (insert result)
  404. (org-babel-examplize-region (point) (progn (insert result) (point))))
  405. (progn
  406. (insert
  407. (concat (orgtbl-to-orgtbl
  408. (if (consp (car result)) result (list result))
  409. '(:fmt (lambda (cell) (format "%S" cell)))) "\n"))
  410. (forward-line -1)
  411. (org-cycle))))
  412. (message "finished"))))
  413. (defun org-babel-result-to-org-string (result)
  414. "Return RESULT as a string in org-mode format. This function
  415. relies on `org-babel-insert-result'."
  416. (with-temp-buffer (org-babel-insert-result result) (buffer-string)))
  417. (defun org-babel-remove-result ()
  418. "Remove the result of the current source block."
  419. (interactive)
  420. (save-excursion
  421. (goto-char (org-babel-where-is-src-block-result)) (forward-line 1)
  422. (delete-region (point) (org-babel-result-end))))
  423. (defun org-babel-result-end ()
  424. "Return the point at the end of the current set of results"
  425. (save-excursion
  426. (if (org-at-table-p)
  427. (org-table-end)
  428. (while (if (looking-at "\\(: \\|\\[\\[\\)")
  429. (progn (while (looking-at "\\(: \\|\\[\\[\\)")
  430. (forward-line 1)) t))
  431. (forward-line 1))
  432. (forward-line -1)
  433. (point))))
  434. (defun org-babel-result-to-file (result)
  435. "Return an `org-mode' link with the path being the value or
  436. RESULT, and the display being the `file-name-nondirectory' if
  437. non-nil."
  438. (let ((name (file-name-nondirectory result)))
  439. (concat "[[" result (if name (concat "][" name "]]") "]]"))))
  440. (defun org-babel-examplize-region (beg end)
  441. "Comment out region using the ': ' org example quote."
  442. (interactive "*r")
  443. (let ((size (abs (- (line-number-at-pos end)
  444. (line-number-at-pos beg)))))
  445. (if (= size 0)
  446. (let ((result (buffer-substring beg end)))
  447. (delete-region beg end)
  448. (insert (concat ": " result)))
  449. (save-excursion
  450. (goto-char beg)
  451. (dotimes (n size)
  452. (move-beginning-of-line 1) (insert ": ") (forward-line 1))))))
  453. (defun org-babel-merge-params (&rest plists)
  454. "Combine all parameter association lists in PLISTS. Later
  455. elements of PLISTS override the values of previous element. This
  456. takes into account some special considerations for certain
  457. parameters when merging lists."
  458. (let (params results vars var ref)
  459. (mapc (lambda (plist)
  460. (mapc (lambda (pair)
  461. (case (car pair)
  462. (:var
  463. ;; we want only one specification per variable
  464. (when (string-match "^\\([^= \f\t\n\r\v]+\\)=\\([^\f\n\r\v]+\\)$" (cdr pair))
  465. ;; TODO: When is this not true? Can there be whitespace around the '='?
  466. (setq var (intern (match-string 1 (cdr pair)))
  467. ref (match-string 2 (cdr pair))
  468. vars (cons (cons var ref) (assq-delete-all var vars)))))
  469. (:results
  470. ;; maintain list of unique :results specifications
  471. (setq results (org-uniquify (append (split-string (cdr pair)) results))))
  472. (t
  473. ;; replace: this covers e.g. :session
  474. (setq params (cons pair (assq-delete-all (car pair) params))))))
  475. plist))
  476. plists)
  477. (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
  478. (while vars (setq params (cons (cons :var (pop vars)) params)))
  479. (cons (cons :results (mapconcat 'identity results " ")) params)))
  480. (defun org-babel-clean-text-properties (text)
  481. "Strip all properties from text return."
  482. (set-text-properties 0 (length text) nil text) text)
  483. (defun org-babel-strip-protective-commas (body)
  484. "Strip protective commas from bodies of source blocks."
  485. (replace-regexp-in-string "^,#" "#" body))
  486. (defun org-babel-read (cell)
  487. "Convert the string value of CELL to a number if appropriate.
  488. Otherwise if cell looks like lisp (meaning it starts with a
  489. \"(\" or a \"'\") then read it as lisp, otherwise return it
  490. unmodified as a string.
  491. This is taken almost directly from `org-read-prop'."
  492. (if (and (stringp cell) (not (equal cell "")))
  493. (or (org-babel-number-p cell)
  494. (if (or (equal "(" (substring cell 0 1))
  495. (equal "'" (substring cell 0 1)))
  496. (read cell)
  497. (progn (set-text-properties 0 (length cell) nil cell) cell)))
  498. cell))
  499. (defun org-babel-number-p (string)
  500. "Return t if STRING represents a number"
  501. (if (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string)
  502. (string-to-number string)))
  503. (defun org-babel-import-elisp-from-file (file-name)
  504. "Read the results located at FILE-NAME into an elisp table. If
  505. the table is trivial, then return it as a scalar."
  506. (let (result)
  507. (with-temp-buffer
  508. (condition-case nil
  509. (progn
  510. (org-table-import file-name nil)
  511. (delete-file file-name)
  512. (setq result (mapcar (lambda (row)
  513. (mapcar #'org-babel-string-read row))
  514. (org-table-to-lisp))))
  515. (error nil))
  516. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  517. (if (consp (car result))
  518. (if (null (cdr (car result)))
  519. (caar result)
  520. result)
  521. (car result))
  522. result))))
  523. (defun org-babel-string-read (cell)
  524. "Strip nested \"s from around strings in exported R values."
  525. (org-babel-read (or (and (stringp cell)
  526. (string-match "\\\"\\(.+\\)\\\"" cell)
  527. (match-string 1 cell))
  528. cell)))
  529. (defun org-babel-reverse-string (string)
  530. (apply 'string (reverse (string-to-list string))))
  531. (defun org-babel-chomp (string &optional regexp)
  532. "Remove any trailing space or carriage returns characters from
  533. STRING. Default regexp used is \"[ \f\t\n\r\v]\" but can be
  534. overwritten by specifying a regexp as a second argument."
  535. (while (and (> (length string) 0) (string-match "[ \f\t\n\r\v]" (substring string -1)))
  536. (setq string (substring string 0 -1)))
  537. string)
  538. (defun org-babel-trim (string &optional regexp)
  539. "Like `org-babel-chomp' only it runs on both the front and back of the string"
  540. (org-babel-chomp (org-babel-reverse-string
  541. (org-babel-chomp (org-babel-reverse-string string) regexp)) regexp))
  542. (provide 'org-babel)
  543. ;;; org-babel.el ends here