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