Merge commit 'origin/master'
[rgr-org-mode.git] / lisp / org-babel-exp.el
blobabf7d487a9a8a895e0e5c04ff313fc5db6c14b86
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 (add-to-list 'org-export-blocks '(src org-babel-exp-src-blocks))
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 ---- the default, display the code and the results
43 code ---- display the code inside the block but do not process
45 results - process the block and replace it with the results of
46 execution
48 none ----- do not display either code or results upon export"
49 (interactive)
50 (unless headers (error "org-babel can't process a source block without knowing the source code"))
51 (message "org-babel processing...")
52 (let ((lang (car headers))
53 (params (org-babel-parse-header-arguments (mapconcat #'identity (cdr headers) " "))))
54 (org-babel-exp-do-export lang body params)))
56 (defun org-babel-exp-inline-src-blocks (start end)
57 "Process inline src blocks between START and END for export.
58 See `org-babel-exp-src-blocks' for export options, currently the
59 options and are taken from `org-babel-defualt-inline-header-args'."
60 (interactive)
61 (save-excursion
62 (goto-char start)
63 (while (and (< (point) end) (re-search-forward org-babel-inline-src-block-regexp end t))
64 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
65 (replacement (save-match-data
66 (org-babel-exp-do-export (first info) (second info) (third info) t))))
67 (setf end (+ end (- (length replacement)
68 (+ 6 (length (first info)) (length (second info))))))
69 (replace-match replacement t t)))))
71 (defun org-babel-exp-do-export (lang body params &optional inline)
72 (case (intern (or (cdr (assoc :exports params)) "code"))
73 ('none "")
74 ('code (org-babel-exp-code body lang params inline))
75 ('results (org-babel-exp-results body lang params inline))
76 ('both (concat (org-babel-exp-code body lang params inline)
77 "\n\n"
78 (org-babel-exp-results body lang params inline)))))
80 (defun org-babel-exp-code (body lang params &optional inline)
81 (if inline
82 (format "=%s=" body)
83 (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body
84 (if (string-match "\n$" body) "" "\n"))))
86 (defun org-babel-exp-results (body lang params &optional inline)
87 ;; I expect there's a good reason why not, but would it be possible
88 ;; to use org-babel-execute-src-block here? [ded]
89 (let* ((cmd (intern (concat "org-babel-execute:" lang)))
90 (result
91 (multiple-value-bind (session vars result-params result-type)
92 (org-babel-process-params params) (funcall cmd body params)))
93 (result-as-org (org-babel-result-to-org-string result)))
94 (if inline
95 (format "=%s=" result)
96 (if (stringp result)
97 (format "#+BEGIN_EXAMPLE\n%s%s\n#+END_EXAMPLE" result
98 (if (string-match "\n$" body) "" "\n"))
99 result-as-org))))
101 (provide 'org-babel-exp)
102 ;;; org-babel-exp.el ends here