Rearrange R evaluation code so that .Last.value evaluated in :results value case...
[rgr-org-mode.git] / lisp / org-babel-exp.el
bloba7445585a4093e654479425b585b967ae7780788
1 ;;; org-babel-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; for more information see the comments in org-babel.el
31 ;;; Code:
32 (require 'org-babel)
33 (require 'org-exp-blocks)
34 (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
35 (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
37 (defun org-babel-exp-src-blocks (body &rest headers)
38 "Process src block for export. Depending on the 'export'
39 headers argument in replace the source code block with...
41 both ---- display the code and the results
43 code ---- the default, display the code inside the block but do
44 not process
46 results - just like none only the block is run on export ensuring
47 that it's results are present in the org-mode buffer
49 none ----- do not display either code or results upon export"
50 (interactive)
51 (unless headers (error "org-babel can't process a source block without knowing the source code"))
52 (message "org-babel-exp processing...")
53 (let* ((lang (car headers))
54 (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
55 (params (org-babel-merge-params
56 org-babel-default-header-args
57 (org-babel-params-from-properties)
58 (if (boundp lang-headers) (eval lang-headers) nil)
59 (org-babel-parse-header-arguments
60 (mapconcat #'identity (cdr headers) " ")))))
61 (message "exporting params are %S" params)
62 (org-babel-exp-do-export lang body params)))
64 (defun org-babel-exp-inline-src-blocks (start end)
65 "Process inline src blocks between START and END for export.
66 See `org-babel-exp-src-blocks' for export options, currently the
67 options and are taken from `org-babel-defualt-inline-header-args'."
68 (interactive)
69 (save-excursion
70 (goto-char start)
71 (while (and (< (point) end) (re-search-forward org-babel-inline-src-block-regexp end t))
72 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
73 (replacement (save-match-data
74 (org-babel-exp-do-export (first info) (second info) (third info) t))))
75 (setf end (+ end (- (length replacement)
76 (+ 6 (length (first info)) (length (second info))))))
77 (replace-match replacement t t)))))
79 (defun org-babel-exp-do-export (lang body params &optional inline)
80 (case (intern (or (cdr (assoc :exports params)) "code"))
81 ('none "")
82 ('code (org-babel-exp-code body lang params inline))
83 ('results (save-excursion
84 ;; org-exp-blocks places us at the end of the block
85 (re-search-backward org-babel-src-block-regexp nil t)
86 (org-babel-execute-src-block) ""))
87 ('both (concat (org-babel-exp-code body lang params inline)
88 "\n\n"
89 (org-babel-exp-results body lang params inline)))))
91 (defun org-babel-exp-code (body lang params &optional inline)
92 (if inline
93 (format "=%s=" body)
94 (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body
95 (if (string-match "\n$" body) "" "\n"))))
97 (provide 'org-babel-exp)
98 ;;; org-babel-exp.el ends here