org-e-ascii: Enforce a final new line in output
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-exp.el
blob442a9f1c40bed7877607387b1a969588af83b6db
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Dan Davison
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Code:
26 (require 'ob)
27 (require 'org-exp-blocks)
28 (eval-when-compile
29 (require 'cl))
31 (defvar obe-marker nil)
32 (defvar org-current-export-file)
33 (defvar org-babel-lob-one-liner-regexp)
34 (defvar org-babel-ref-split-regexp)
35 (declare-function org-babel-lob-get-info "ob-lob" ())
36 (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
37 (add-to-list 'org-export-interblocks '(src org-babel-exp-non-block-elements))
39 (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
41 (defcustom org-export-babel-evaluate t
42 "Switch controlling code evaluation during export.
43 When set to nil no code will be evaluated as part of the export
44 process."
45 :group 'org-babel
46 :type 'boolean)
47 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
49 (defmacro org-babel-exp-in-export-file (lang &rest body)
50 (declare (indent 1))
51 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
52 (heading (nth 4 (ignore-errors (org-heading-components))))
53 (link (when org-current-export-file
54 (org-make-link-string
55 (if heading
56 (concat org-current-export-file "::" heading)
57 org-current-export-file))))
58 (export-buffer (current-buffer)) results)
59 (when link
60 ;; resolve parameters in the original file so that
61 ;; headline and file-wide parameters are included, attempt
62 ;; to go to the same heading in the original file
63 (set-buffer (get-file-buffer org-current-export-file))
64 (save-restriction
65 (condition-case nil
66 (let ((org-link-search-inhibit-query t))
67 (org-open-link-from-string link))
68 (error (when heading
69 (goto-char (point-min))
70 (re-search-forward (regexp-quote heading) nil t))))
71 (setq results ,@body))
72 (set-buffer export-buffer)
73 results)))
74 (def-edebug-spec org-babel-exp-in-export-file (form body))
76 (defun org-babel-exp-src-block (body &rest headers)
77 "Process source block for export.
78 Depending on the 'export' headers argument in replace the source
79 code block with...
81 both ---- display the code and the results
83 code ---- the default, display the code inside the block but do
84 not process
86 results - just like none only the block is run on export ensuring
87 that it's results are present in the org-mode buffer
89 none ----- do not display either code or results upon export"
90 (interactive)
91 (unless noninteractive (message "org-babel-exp processing..."))
92 (save-excursion
93 (goto-char (match-beginning 0))
94 (let* ((info (org-babel-get-src-block-info 'light))
95 (lang (nth 0 info))
96 (raw-params (nth 2 info)) hash)
97 ;; bail if we couldn't get any info from the block
98 (when info
99 ;; if we're actually going to need the parameters
100 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
101 (org-babel-exp-in-export-file lang
102 (setf (nth 2 info)
103 (org-babel-process-params
104 (org-babel-merge-params
105 org-babel-default-header-args
106 (org-babel-params-from-properties lang)
107 (if (boundp lang-headers) (eval lang-headers) nil)
108 raw-params))))
109 (setf hash (org-babel-sha1-hash info)))
110 ;; expand noweb references in the original file
111 (setf (nth 1 info)
112 (if (org-babel-noweb-p (nth 2 info) :export)
113 (org-babel-expand-noweb-references
114 info (get-file-buffer org-current-export-file))
115 (nth 1 info)))
116 (org-babel-exp-do-export info 'block hash)))))
118 (defcustom org-babel-exp-call-line-template
120 "Template used to export call lines.
121 This template may be customized to include the call line name
122 with any export markup. The template is filled out using
123 `org-fill-template', and the following %keys may be used.
125 line --- call line
127 An example value would be \"\\n: call: %line\" to export the call line
128 wrapped in a verbatim environment.
130 Note: the results are inserted separately after the contents of
131 this template."
132 :group 'org-babel
133 :type 'string)
135 (defvar org-babel-default-lob-header-args)
136 (defun org-babel-exp-non-block-elements (start end)
137 "Process inline source and call lines between START and END for export."
138 (interactive)
139 (save-excursion
140 (goto-char start)
141 (unless (markerp end)
142 (let ((m (make-marker)))
143 (set-marker m end (current-buffer))
144 (setq end m)))
145 (let ((rx (concat "\\(" org-babel-inline-src-block-regexp
146 "\\|" org-babel-lob-one-liner-regexp "\\)")))
147 (while (and (< (point) (marker-position end))
148 (re-search-forward rx end t))
149 (if (save-excursion
150 (goto-char (match-beginning 0))
151 (looking-at org-babel-inline-src-block-regexp))
152 (progn
153 (forward-char 1)
154 (let* ((info (save-match-data
155 (org-babel-parse-inline-src-block-match)))
156 (params (nth 2 info)))
157 (save-match-data
158 (goto-char (match-beginning 2))
159 (unless (org-babel-in-example-or-verbatim)
160 ;; expand noweb references in the original file
161 (setf (nth 1 info)
162 (if (and (cdr (assoc :noweb params))
163 (string= "yes" (cdr (assoc :noweb params))))
164 (org-babel-expand-noweb-references
165 info (get-file-buffer org-current-export-file))
166 (nth 1 info)))
167 (let ((code-replacement (save-match-data
168 (org-babel-exp-do-export
169 info 'inline))))
170 (if code-replacement
171 (progn (replace-match code-replacement nil nil nil 1)
172 (delete-char 1))
173 (org-babel-examplize-region (match-beginning 1)
174 (match-end 1))
175 (forward-char 2)))))))
176 (unless (org-babel-in-example-or-verbatim)
177 (let* ((lob-info (org-babel-lob-get-info))
178 (inlinep (match-string 11))
179 (inline-start (match-end 11))
180 (inline-end (match-end 0))
181 (results (save-match-data
182 (org-babel-exp-do-export
183 (list "emacs-lisp" "results"
184 (org-babel-merge-params
185 org-babel-default-header-args
186 org-babel-default-lob-header-args
187 (org-babel-params-from-properties)
188 (org-babel-parse-header-arguments
189 (org-babel-clean-text-properties
190 (concat ":var results="
191 (mapconcat #'identity
192 (butlast lob-info)
193 " ")))))
194 "" nil (car (last lob-info)))
195 'lob)))
196 (rep (org-fill-template
197 org-babel-exp-call-line-template
198 `(("line" . ,(nth 0 lob-info))))))
199 (if inlinep
200 (save-excursion
201 (goto-char inline-start)
202 (delete-region inline-start inline-end)
203 (insert rep))
204 (replace-match rep t t)))))))))
206 (defun org-babel-in-example-or-verbatim ()
207 "Return true if point is in example or verbatim code.
208 Example and verbatim code include escaped portions of
209 an org-mode buffer code that should be treated as normal
210 org-mode text."
211 (or (save-match-data
212 (save-excursion
213 (goto-char (point-at-bol))
214 (looking-at "[ \t]*:[ \t]")))
215 (org-in-verbatim-emphasis)
216 (org-in-block-p org-list-forbidden-blocks)
217 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
219 (defun org-babel-exp-do-export (info type &optional hash)
220 "Return a string with the exported content of a code block.
221 The function respects the value of the :exports header argument."
222 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
223 (when (not (and session (equal "none" session)))
224 (org-babel-exp-results info type 'silent))))
225 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
226 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
227 ('none (silently) (clean) "")
228 ('code (silently) (clean) (org-babel-exp-code info))
229 ('results (org-babel-exp-results info type nil hash) "")
230 ('both (org-babel-exp-results info type nil hash)
231 (org-babel-exp-code info)))))
233 (defcustom org-babel-exp-code-template
234 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
235 "Template used to export the body of code blocks.
236 This template may be customized to include additional information
237 such as the code block name, or the values of particular header
238 arguments. The template is filled out using `org-fill-template',
239 and the following %keys may be used.
241 lang ------ the language of the code block
242 name ------ the name of the code block
243 body ------ the body of the code block
244 flags ----- the flags passed to the code block
246 In addition to the keys mentioned above, every header argument
247 defined for the code block may be used as a key and will be
248 replaced with its value."
249 :group 'org-babel
250 :type 'string)
252 (defun org-babel-exp-code (info)
253 "Return the original code block formatted for export."
254 (org-fill-template
255 org-babel-exp-code-template
256 `(("lang" . ,(nth 0 info))
257 ("body" . ,(nth 1 info))
258 ,@(mapcar (lambda (pair)
259 (cons (substring (symbol-name (car pair)) 1)
260 (format "%S" (cdr pair))))
261 (nth 2 info))
262 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
263 ("name" . ,(or (nth 4 info) "")))))
265 (defun org-babel-exp-results (info type &optional silent hash)
266 "Evaluate and return the results of the current code block for export.
267 Results are prepared in a manner suitable for export by org-mode.
268 This function is called by `org-babel-exp-do-export'. The code
269 block will be evaluated. Optional argument SILENT can be used to
270 inhibit insertion of results into the buffer."
271 (when (and org-export-babel-evaluate
272 (not (and hash (equal hash (org-babel-current-result-hash)))))
273 (let ((lang (nth 0 info))
274 (body (nth 1 info))
275 (info (copy-sequence info)))
276 ;; skip code blocks which we can't evaluate
277 (when (fboundp (intern (concat "org-babel-execute:" lang)))
278 (org-babel-eval-wipe-error-buffer)
279 (prog1 nil
280 (setf (nth 2 info)
281 (org-babel-exp-in-export-file lang
282 (org-babel-process-params
283 (org-babel-merge-params
284 (nth 2 info)
285 `((:results . ,(if silent "silent" "replace")))))))
286 (cond
287 ((equal type 'block)
288 (org-babel-execute-src-block nil info))
289 ((equal type 'inline)
290 ;; position the point on the inline source block allowing
291 ;; `org-babel-insert-result' to check that the block is
292 ;; inline
293 (re-search-backward "[ \f\t\n\r\v]" nil t)
294 (re-search-forward org-babel-inline-src-block-regexp nil t)
295 (re-search-backward "src_" nil t)
296 (org-babel-execute-src-block nil info))
297 ((equal type 'lob)
298 (save-excursion
299 (re-search-backward org-babel-lob-one-liner-regexp nil t)
300 (org-babel-execute-src-block nil info)))))))))
302 (provide 'ob-exp)
306 ;;; ob-exp.el ends here