Make ox-koma-letter pass checkdoc.
[org-mode/org-kjn.git] / lisp / ob-exp.el
blobc8479e36d7ab349be3c89a6d3e36fbfbb275b312
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Authors: 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-core)
27 (eval-when-compile
28 (require 'cl))
30 (defvar org-current-export-file)
31 (defvar org-babel-lob-one-liner-regexp)
32 (defvar org-babel-ref-split-regexp)
33 (defvar org-list-forbidden-blocks)
35 (declare-function org-babel-lob-get-info "ob-lob" ())
36 (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
37 (declare-function org-between-regexps-p "org"
38 (start-re end-re &optional lim-up lim-down))
39 (declare-function org-get-indentation "org" (&optional line))
40 (declare-function org-heading-components "org" ())
41 (declare-function org-in-block-p "org" (names))
42 (declare-function org-in-verbatim-emphasis "org" ())
43 (declare-function org-link-search "org" (s &optional type avoid-pos stealth))
44 (declare-function org-fill-template "org" (template alist))
45 (declare-function org-split-string "org" (string &optional separators))
46 (declare-function org-element-at-point "org-element" (&optional keep-trail))
47 (declare-function org-element-context "org-element" ())
48 (declare-function org-element-property "org-element" (property element))
49 (declare-function org-element-type "org-element" (element))
50 (declare-function org-escape-code-in-string "org-src" (s))
52 (defcustom org-export-babel-evaluate t
53 "Switch controlling code evaluation during export.
54 When set to nil no code will be evaluated as part of the export
55 process. When set to 'inline-only, only inline code blocks will
56 be executed."
57 :group 'org-babel
58 :version "24.1"
59 :type '(choice (const :tag "Never" nil)
60 (const :tag "Only inline code" inline-only)
61 (const :tag "Always" t)))
62 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
64 (defun org-babel-exp-get-export-buffer ()
65 "Return the current export buffer if possible."
66 (cond
67 ((bufferp org-current-export-file) org-current-export-file)
68 (org-current-export-file (get-file-buffer org-current-export-file))
69 ('otherwise
70 (error "Requested export buffer when `org-current-export-file' is nil"))))
72 (defmacro org-babel-exp-in-export-file (lang &rest body)
73 (declare (indent 1))
74 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
75 (heading (nth 4 (ignore-errors (org-heading-components))))
76 (export-buffer (current-buffer))
77 (original-buffer (org-babel-exp-get-export-buffer)) results)
78 (when original-buffer
79 ;; resolve parameters in the original file so that
80 ;; headline and file-wide parameters are included, attempt
81 ;; to go to the same heading in the original file
82 (set-buffer original-buffer)
83 (save-restriction
84 (when heading
85 (condition-case nil
86 (let ((org-link-search-inhibit-query t))
87 (org-link-search heading))
88 (error (when heading
89 (goto-char (point-min))
90 (re-search-forward (regexp-quote heading) nil t)))))
91 (setq results ,@body))
92 (set-buffer export-buffer)
93 results)))
94 (def-edebug-spec org-babel-exp-in-export-file (form body))
96 (defun org-babel-exp-src-block (&rest headers)
97 "Process source block for export.
98 Depending on the 'export' headers argument, replace the source
99 code block like this:
101 both ---- display the code and the results
103 code ---- the default, display the code inside the block but do
104 not process
106 results - just like none only the block is run on export ensuring
107 that it's results are present in the org-mode buffer
109 none ---- do not display either code or results upon export
111 Assume point is at the beginning of block's starting line."
112 (interactive)
113 (unless noninteractive (message "org-babel-exp processing..."))
114 (save-excursion
115 (let* ((info (org-babel-get-src-block-info 'light))
116 (lang (nth 0 info))
117 (raw-params (nth 2 info)) hash)
118 ;; bail if we couldn't get any info from the block
119 (when info
120 ;; if we're actually going to need the parameters
121 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
122 (org-babel-exp-in-export-file lang
123 (setf (nth 2 info)
124 (org-babel-process-params
125 (apply #'org-babel-merge-params
126 org-babel-default-header-args
127 (if (boundp lang-headers) (eval lang-headers) nil)
128 (append (org-babel-params-from-properties lang)
129 (list raw-params))))))
130 (setf hash (org-babel-sha1-hash info)))
131 (org-babel-exp-do-export info 'block hash)))))
133 (defcustom org-babel-exp-call-line-template
135 "Template used to export call lines.
136 This template may be customized to include the call line name
137 with any export markup. The template is filled out using
138 `org-fill-template', and the following %keys may be used.
140 line --- call line
142 An example value would be \"\\n: call: %line\" to export the call line
143 wrapped in a verbatim environment.
145 Note: the results are inserted separately after the contents of
146 this template."
147 :group 'org-babel
148 :type 'string)
150 (defvar org-babel-default-lob-header-args)
151 (defun org-babel-exp-non-block-elements (start end)
152 "Process inline source and call lines between START and END for export."
153 (interactive)
154 (save-excursion
155 (goto-char start)
156 (unless (markerp end)
157 (let ((m (make-marker)))
158 (set-marker m end (current-buffer))
159 (setq end m)))
160 (let ((rx (concat "\\(?:" org-babel-inline-src-block-regexp
161 "\\|" org-babel-lob-one-liner-regexp "\\)")))
162 (while (re-search-forward rx end t)
163 (save-excursion
164 (let* ((element (save-excursion
165 ;; If match is inline, point is at its
166 ;; end. Move backward so
167 ;; `org-element-context' can get the
168 ;; object, not the following one.
169 (backward-char)
170 (save-match-data (org-element-context))))
171 (type (org-element-type element)))
172 (when (memq type '(babel-call inline-babel-call inline-src-block))
173 (let ((beg-el (org-element-property :begin element))
174 (end-el (org-element-property :end element)))
175 (case type
176 (inline-src-block
177 (let* ((info (org-babel-parse-inline-src-block-match))
178 (params (nth 2 info)))
179 (setf (nth 1 info)
180 (if (and (cdr (assoc :noweb params))
181 (string= "yes" (cdr (assoc :noweb params))))
182 (org-babel-expand-noweb-references
183 info (org-babel-exp-get-export-buffer))
184 (nth 1 info)))
185 (goto-char beg-el)
186 (let ((replacement (org-babel-exp-do-export info 'inline)))
187 (if (equal replacement "")
188 ;; Replacement code is empty: completely
189 ;; remove inline src block, including extra
190 ;; white space that might have been created
191 ;; when inserting results.
192 (delete-region beg-el
193 (progn (goto-char end-el)
194 (skip-chars-forward " \t")
195 (point)))
196 ;; Otherwise: remove inline src block but
197 ;; preserve following white spaces. Then
198 ;; insert value.
199 (delete-region beg-el
200 (progn (goto-char end-el)
201 (skip-chars-backward " \t")
202 (point)))
203 (insert replacement)))))
204 ((babel-call inline-babel-call)
205 (let* ((lob-info (org-babel-lob-get-info))
206 (results
207 (org-babel-exp-do-export
208 (list "emacs-lisp" "results"
209 (apply #'org-babel-merge-params
210 org-babel-default-header-args
211 org-babel-default-lob-header-args
212 (append
213 (org-babel-params-from-properties)
214 (list
215 (org-babel-parse-header-arguments
216 (org-no-properties
217 (concat
218 ":var results="
219 (mapconcat 'identity
220 (butlast lob-info 2)
221 " ")))))))
222 "" (nth 3 lob-info) (nth 2 lob-info))
223 'lob))
224 (rep (org-fill-template
225 org-babel-exp-call-line-template
226 `(("line" . ,(nth 0 lob-info))))))
227 ;; If replacement is empty, completely remove the
228 ;; object/element, including any extra white space
229 ;; that might have been created when including
230 ;; results.
231 (if (equal rep "")
232 (delete-region
233 beg-el
234 (progn (goto-char end-el)
235 (if (not (eq type 'babel-call))
236 (progn (skip-chars-forward " \t") (point))
237 (skip-chars-forward " \r\t\n")
238 (line-beginning-position))))
239 ;; Otherwise, preserve following white
240 ;; spaces/newlines and then, insert replacement
241 ;; string.
242 (goto-char beg-el)
243 (delete-region beg-el
244 (progn (goto-char end-el)
245 (skip-chars-backward " \r\t\n")
246 (point)))
247 (insert rep)))))))))))))
249 (defvar org-src-preserve-indentation) ; From org-src.el
250 (defun org-babel-exp-process-buffer ()
251 "Execute all blocks in visible part of buffer."
252 (interactive)
253 (save-window-excursion
254 (let ((case-fold-search t)
255 (pos (point-min)))
256 (goto-char pos)
257 (while (re-search-forward "^[ \t]*#\\+BEGIN_SRC" nil t)
258 (let ((element (save-match-data (org-element-at-point))))
259 (when (eq (org-element-type element) 'src-block)
260 (let* ((match-start (copy-marker (match-beginning 0)))
261 (begin (copy-marker (org-element-property :begin element)))
262 ;; Make sure we don't remove any blank lines after
263 ;; the block when replacing it.
264 (block-end (save-excursion
265 (goto-char (org-element-property :end element))
266 (skip-chars-backward " \r\t\n")
267 (copy-marker (line-end-position))))
268 (ind (org-get-indentation))
269 (headers
270 (cons
271 (org-element-property :language element)
272 (let ((params (org-element-property :parameters element)))
273 (and params (org-split-string params "[ \t]+"))))))
274 ;; Execute all non-block elements between POS and
275 ;; current block.
276 (org-babel-exp-non-block-elements pos begin)
277 ;; Take care of matched block: compute replacement
278 ;; string. In particular, a nil REPLACEMENT means the
279 ;; block should be left as-is while an empty string
280 ;; should remove the block.
281 (let ((replacement (progn (goto-char match-start)
282 (org-babel-exp-src-block headers))))
283 (cond ((not replacement) (goto-char block-end))
284 ((equal replacement "")
285 (delete-region begin
286 (progn (goto-char block-end)
287 (skip-chars-forward " \r\t\n")
288 (if (eobp) (point)
289 (line-beginning-position)))))
291 (goto-char match-start)
292 (delete-region (point) block-end)
293 (insert replacement)
294 (if (org-element-property :preserve-indent element)
295 ;; Indent only the code block markers.
296 (save-excursion (skip-chars-backward " \r\t\n")
297 (indent-line-to ind)
298 (goto-char match-start)
299 (indent-line-to ind))
300 ;; Indent everything.
301 (indent-rigidly match-start (point) ind)))))
302 (setq pos (line-beginning-position))
303 ;; Cleanup markers.
304 (set-marker match-start nil)
305 (set-marker begin nil)
306 (set-marker block-end nil)))))
307 ;; Eventually execute all non-block Babel elements between last
308 ;; src-block and end of buffer.
309 (org-babel-exp-non-block-elements pos (point-max)))))
311 (defun org-babel-in-example-or-verbatim ()
312 "Return true if point is in example or verbatim code.
313 Example and verbatim code include escaped portions of
314 an org-mode buffer code that should be treated as normal
315 org-mode text."
316 (or (save-match-data
317 (save-excursion
318 (goto-char (point-at-bol))
319 (looking-at "[ \t]*:[ \t]")))
320 (org-in-verbatim-emphasis)
321 (org-in-block-p org-list-forbidden-blocks)
322 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
324 (defun org-babel-exp-do-export (info type &optional hash)
325 "Return a string with the exported content of a code block.
326 The function respects the value of the :exports header argument."
327 (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
328 (when (not (and session (equal "none" session)))
329 (org-babel-exp-results info type 'silent)))))
330 (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
331 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
332 ('none (funcall silently) (funcall clean) "")
333 ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
334 ('results (org-babel-exp-results info type nil hash) "")
335 ('both (org-babel-exp-results info type nil hash)
336 (org-babel-exp-code info)))))
338 (defcustom org-babel-exp-code-template
339 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
340 "Template used to export the body of code blocks.
341 This template may be customized to include additional information
342 such as the code block name, or the values of particular header
343 arguments. The template is filled out using `org-fill-template',
344 and the following %keys may be used.
346 lang ------ the language of the code block
347 name ------ the name of the code block
348 body ------ the body of the code block
349 flags ----- the flags passed to the code block
351 In addition to the keys mentioned above, every header argument
352 defined for the code block may be used as a key and will be
353 replaced with its value."
354 :group 'org-babel
355 :type 'string)
357 (defun org-babel-exp-code (info)
358 "Return the original code block formatted for export."
359 (setf (nth 1 info)
360 (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
361 (replace-regexp-in-string
362 (org-babel-noweb-wrap) "" (nth 1 info))
363 (if (org-babel-noweb-p (nth 2 info) :export)
364 (org-babel-expand-noweb-references
365 info (org-babel-exp-get-export-buffer))
366 (nth 1 info))))
367 (org-fill-template
368 org-babel-exp-code-template
369 `(("lang" . ,(nth 0 info))
370 ("body" . ,(org-escape-code-in-string (nth 1 info)))
371 ,@(mapcar (lambda (pair)
372 (cons (substring (symbol-name (car pair)) 1)
373 (format "%S" (cdr pair))))
374 (nth 2 info))
375 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
376 ("name" . ,(or (nth 4 info) "")))))
378 (defun org-babel-exp-results (info type &optional silent hash)
379 "Evaluate and return the results of the current code block for export.
380 Results are prepared in a manner suitable for export by org-mode.
381 This function is called by `org-babel-exp-do-export'. The code
382 block will be evaluated. Optional argument SILENT can be used to
383 inhibit insertion of results into the buffer."
384 (when (and (or (eq org-export-babel-evaluate t)
385 (and (eq type 'inline)
386 (eq org-export-babel-evaluate 'inline-only)))
387 (not (and hash (equal hash (org-babel-current-result-hash)))))
388 (let ((lang (nth 0 info))
389 (body (if (org-babel-noweb-p (nth 2 info) :eval)
390 (org-babel-expand-noweb-references
391 info (org-babel-exp-get-export-buffer))
392 (nth 1 info)))
393 (info (copy-sequence info))
394 (org-babel-current-src-block-location (point-marker)))
395 ;; skip code blocks which we can't evaluate
396 (when (fboundp (intern (concat "org-babel-execute:" lang)))
397 (org-babel-eval-wipe-error-buffer)
398 (prog1 nil
399 (setf (nth 1 info) body)
400 (setf (nth 2 info)
401 (org-babel-exp-in-export-file lang
402 (org-babel-process-params
403 (org-babel-merge-params
404 (nth 2 info)
405 `((:results . ,(if silent "silent" "replace")))))))
406 (cond
407 ((equal type 'block)
408 (org-babel-execute-src-block nil info))
409 ((equal type 'inline)
410 ;; position the point on the inline source block allowing
411 ;; `org-babel-insert-result' to check that the block is
412 ;; inline
413 (re-search-backward "[ \f\t\n\r\v]" nil t)
414 (re-search-forward org-babel-inline-src-block-regexp nil t)
415 (re-search-backward "src_" nil t)
416 (org-babel-execute-src-block nil info))
417 ((equal type 'lob)
418 (save-excursion
419 (re-search-backward org-babel-lob-one-liner-regexp nil t)
420 (let (org-confirm-babel-evaluate)
421 (org-babel-execute-src-block nil info))))))))))
424 (provide 'ob-exp)
426 ;;; ob-exp.el ends here