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 (defvar org-babel-function-def-export-keyword
"function"
39 "When exporting a source block function, this keyword will
40 appear in the exported version in the place of source name
41 line. A source block is considered to be a source block function
42 if the source name is present and is followed by a parenthesized
43 argument list. The parentheses may be empty or contain
44 whitespace. An example is the following which generates n random
53 (defvar org-babel-function-def-export-indent
4
54 "When exporting a source block function, the block contents
55 will be indented by this many characters. See
56 `org-babel-function-def-export-name' for the definition of a
57 source block function.")
59 (defun org-babel-exp-src-blocks (body &rest headers
)
60 "Process src block for export. Depending on the 'export'
61 headers argument in replace the source code block with...
63 both ---- display the code and the results
65 code ---- the default, display the code inside the block but do
68 results - just like none only the block is run on export ensuring
69 that it's results are present in the org-mode buffer
71 none ----- do not display either code or results upon export"
73 (message "org-babel-exp processing...")
74 (or (and (re-search-backward org-babel-src-block-regexp nil t
)
75 (org-babel-exp-do-export (org-babel-get-src-block-info) 'block
))
76 (and (re-search-backward org-block-regexp nil t
)
78 (error "Unmatched block [bug in `org-babel-exp-src-blocks'].")))
80 (defun org-babel-exp-inline-src-blocks (start end
)
81 "Process inline src blocks between START and END for export.
82 See `org-babel-exp-src-blocks' for export options, currently the
83 options and are taken from `org-babel-defualt-inline-header-args'."
87 (while (and (< (point) end
)
88 (re-search-forward org-babel-inline-src-block-regexp end t
))
89 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
90 (replacement (save-match-data
91 (org-babel-exp-do-export info
'inline
))))
92 (setq end
(+ end
(- (length replacement
) (length (match-string 1)))))
93 (replace-match replacement t t nil
1)))))
95 (defun org-babel-exp-lob-one-liners (start end
)
96 "Process #+lob (Library of Babel) calls between START and END for export.
97 See `org-babel-exp-src-blocks' for export options. Currently the
98 options are taken from `org-babel-default-header-args'."
103 (while (and (< (point) end
)
104 (re-search-forward org-babel-lob-one-liner-regexp nil t
))
107 (org-babel-exp-do-export
108 (list "emacs-lisp" "results"
109 (org-babel-merge-params
110 org-babel-default-header-args
111 (org-babel-parse-header-arguments
112 (org-babel-clean-text-properties
113 (concat ":var results="
114 (mapconcat #'identity
(org-babel-lob-get-info) " "))))))
116 (setq end
(+ end
(- (length replacement
) (length (match-string 0)))))
117 (replace-match replacement t t
)))))
119 (defun org-babel-exp-do-export (info type
)
120 (case (intern (or (cdr (assoc :exports
(third info
))) "code"))
122 ('code
(org-babel-exp-code info type
))
123 ('results
(org-babel-exp-results info type
))
124 ('both
(concat (org-babel-exp-code info type
)
126 (org-babel-exp-results info type
)))))
128 (defun org-babel-exp-code (info type
)
129 (let ((lang (first info
))
131 (switches (fourth info
))
134 (function-def-line ""))
136 ('inline
(format "=%s=" (second info
)))
139 (unless (string-match "-i\\>" switches
)
140 (setq switches
(concat switches
" -i")))
141 (setq body
(with-temp-buffer
143 (indent-code-rigidly (point-min) (point-max) org-babel-function-def-export-indent
)
145 (setq args
(mapconcat #'identity
146 (delq nil
(mapcar (lambda (el) (and (length (cdr el
)) (cdr el
))) args
))
148 (setq function-def-line
149 (format "#+BEGIN_SRC org-babel-lob\n%s %s(%s):\n#+END_SRC\n"
150 org-babel-function-def-export-keyword name args
)))
151 (concat function-def-line
152 (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC" lang switches body
153 (if (string-match "\n$" body
) "" "\n"))))
154 ('lob
(save-excursion
155 (re-search-backward org-babel-lob-one-liner-regexp
)
156 (format "#+BEGIN_SRC org-babel-lob\n%s\n#+END_SRC"
157 (first (org-babel-lob-get-info))))))))
159 (defun org-babel-exp-results (info type
)
160 (let ((lang (first info
))
163 ;; lets ensure that we lookup references in the original file
164 (mapcar (lambda (pair)
165 (if (and org-current-export-file
167 (string-match org-babel-ref-split-regexp
(cdr pair
)))
168 `(:var .
,(concat (match-string 1 (cdr pair
))
169 "=" org-current-export-file
170 ":" (match-string 2 (cdr pair
))))
175 (let ((raw (org-babel-execute-src-block
176 nil info
'((:results .
"silent"))))
177 (result-params (split-string (cdr (assoc :results params
)))))
178 (cond ;; respect the value of the :results header argument
179 ((member "file" result-params
)
180 (org-babel-result-to-file raw
))
181 ((or (member "raw" result-params
) (member "org" result-params
))
183 ((member "code" result-params
)
184 (format "src_%s{%s}" lang raw
))
187 (if (= 0 (length raw
)) "=(no results)="
189 (format "=%S=" raw
))))))
191 (org-babel-execute-src-block
192 nil nil
(org-babel-merge-params params
'((:results .
"replace"))))
196 (re-search-backward org-babel-lob-one-liner-regexp nil t
)
197 (org-babel-execute-src-block
198 nil
(list lang body
(org-babel-merge-params params
'((:results .
"replace"))))) "")))))
200 (provide 'org-babel-exp
)
201 ;;; org-babel-exp.el ends here