Making some lines shorter and other trivial cosmetics
[rgr-org-mode.git] / lisp / org-babel-exp.el
blob4ff6db90093fa425c72b8582849a9d87eaf38a66
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 (let* ((cmd (intern (concat "org-babel-execute:" lang)))
88 (result (funcall cmd body params))
89 (result-as-org (org-babel-result-to-org-string result)))
90 (if inline
91 (format "=%s=" result)
92 (if (stringp result)
93 (format "#+BEGIN_EXAMPLE\n%s%s\n#+END_EXAMPLE" result
94 (if (string-match "\n$" body) "" "\n"))
95 result-as-org))))
97 (provide 'org-babel-exp)
98 ;;; org-babel-exp.el ends here