1 ;;; org-babel-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009 Eric Schulte, Dan Davison
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
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)
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.
29 ;; for more information see the comments in org-babel.el
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
))
36 (add-to-list 'org-export-interblocks
'(lob org-babel-exp-lob-one-liners
))
38 (defun org-babel-exp-src-blocks (body &rest headers
)
39 "Process src block for export. Depending on the 'export'
40 headers argument in replace the source code block with...
42 both ---- display the code and the results
44 code ---- the default, display the code inside the block but do
47 results - just like none only the block is run on export ensuring
48 that it's results are present in the org-mode buffer
50 none ----- do not display either code or results upon export"
52 (unless headers
(error "org-babel can't process a source block without knowing the source code"))
53 (message "org-babel-exp processing...")
54 (let* ((lang (car headers
))
55 (lang-headers (intern (concat "org-babel-default-header-args:" lang
)))
56 (params (org-babel-merge-params
57 org-babel-default-header-args
58 (if (boundp lang-headers
) (eval lang-headers
) nil
)
59 (org-babel-params-from-properties)
60 (org-babel-parse-header-arguments
61 (mapconcat #'identity
(cdr headers
) " ")))))
62 (org-babel-exp-do-export (list lang body params
) 'block
)))
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'."
71 (while (and (< (point) end
)
72 (re-search-forward org-babel-inline-src-block-regexp end t
))
73 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
74 (replacement (save-match-data
75 (org-babel-exp-do-export info
'inline
))))
76 (setq end
(+ end
(- (length replacement
) (length (match-string 1)))))
77 (replace-match replacement t t nil
1)))))
79 (defun org-babel-exp-lob-one-liners (start end
)
80 "Process #+lob (Library of Babel) calls between START and END for export.
81 See `org-babel-exp-src-blocks' for export options. Currently the
82 options are taken from `org-babel-default-header-args'."
87 (while (and (< (point) end
)
88 (re-search-forward org-babel-lob-one-liner-regexp nil t
))
91 (org-babel-exp-do-export
92 (list "emacs-lisp" "results"
93 (org-babel-merge-params
94 org-babel-default-header-args
95 (org-babel-parse-header-arguments
96 (org-babel-clean-text-properties
97 (concat ":var results="
98 (mapconcat #'identity
(org-babel-lob-get-info) " "))))))
100 (setq end
(+ end
(- (length replacement
) (length (match-string 0)))))
101 (replace-match replacement t t
)))))
103 (defun org-babel-exp-do-export (info type
)
104 (case (intern (or (cdr (assoc :exports
(third info
))) "code"))
106 ('code
(org-babel-exp-code info type
))
107 ('results
(org-babel-exp-results info type
))
108 ('both
(concat (org-babel-exp-code info type
)
110 (org-babel-exp-results info type
)))))
112 (defun org-babel-exp-code (info type
)
113 (let ((lang (first info
))
114 (body (second info
)))
116 ('inline
(format "=%s=" (second info
)))
117 ('block
(format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body
118 (if (string-match "\n$" body
) "" "\n")))
119 ('lob
(save-excursion
120 (re-search-backward org-babel-lob-one-liner-regexp
)
121 (format "#+BEGIN_SRC org-babel-lob\n%s\n#+END_SRC"
122 (first (org-babel-lob-get-info))))))))
124 (defun org-babel-exp-results (info type
)
125 (let ((lang (first info
))
128 ;; lets ensure that we lookup references in the original file
129 (mapcar (lambda (pair)
130 (if (and org-current-export-file
132 (string-match org-babel-ref-split-regexp
(cdr pair
)))
133 `(:var .
,(concat (match-string 1 (cdr pair
))
134 "=" org-current-export-file
135 ":" (match-string 2 (cdr pair
))))
140 (let ((raw (org-babel-execute-src-block
141 nil info
'((:results .
"silent"))))
142 (result-params (split-string (cdr (assoc :results params
)))))
143 (cond ;; respect the value of the :results header argument
144 ((member "file" result-params
)
145 (org-babel-result-to-file raw
))
146 ((or (member "raw" result-params
) (member "org" result-params
))
148 ((member "code" result-params
)
149 (format "src_%s{%s}" lang raw
))
152 (if (= 0 (length raw
)) "=(no results)="
154 (format "=%S=" raw
))))))
156 (save-excursion ;; org-exp-blocks places us at the end of the block
157 (re-search-backward org-babel-src-block-regexp nil t
)
158 (org-babel-execute-src-block
159 nil nil
(org-babel-merge-params params
'((:results .
"replace")))) ""))
162 (re-search-backward org-babel-lob-one-liner-regexp nil t
)
163 (org-babel-execute-src-block
164 nil
(list lang body
(org-babel-merge-params params
'((:results .
"replace"))))) "")))))
166 (provide 'org-babel-exp
)
167 ;;; org-babel-exp.el ends here