org-babel.el 26 KB

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