2016-06-18 17:31:15 -04:00
|
|
|
;;; ob-exp.el --- Exportation of Babel Source Blocks -*- lexical-binding: t; -*-
|
2009-04-23 18:48:22 -04:00
|
|
|
|
2023-01-01 05:31:12 -05:00
|
|
|
;; Copyright (C) 2009-2023 Free Software Foundation, Inc.
|
2009-04-23 18:48:22 -04:00
|
|
|
|
2012-04-01 18:53:28 -04:00
|
|
|
;; Authors: Eric Schulte
|
2011-08-24 14:55:11 -04:00
|
|
|
;; Dan Davison
|
2009-04-23 18:48:22 -04:00
|
|
|
;; Keywords: literate programming, reproducible research
|
2021-09-26 03:44:29 -04:00
|
|
|
;; URL: https://orgmode.org
|
2009-04-23 18:48:22 -04:00
|
|
|
|
2010-06-25 12:20:39 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-04-23 18:48:22 -04:00
|
|
|
|
2010-06-25 12:20:39 -04:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-04-23 18:48:22 -04:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 12:20:39 -04:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-04-23 18:48:22 -04:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-04-23 18:48:22 -04:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 18:52:52 -04:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2009-04-23 18:48:22 -04:00
|
|
|
|
|
|
|
;;; Code:
|
2022-08-04 09:53:05 -04:00
|
|
|
|
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2012-12-11 18:58:16 -05:00
|
|
|
(require 'ob-core)
|
2010-06-12 21:32:23 -04:00
|
|
|
|
2022-10-18 11:27:54 -04:00
|
|
|
(declare-function org-babel-lob-get-info "ob-lob" (&optional datum no-eval))
|
2022-01-06 12:39:05 -05:00
|
|
|
(declare-function org-element-at-point "org-element" (&optional pom cached-only))
|
2016-05-14 22:56:53 -04:00
|
|
|
(declare-function org-element-context "org-element" (&optional element))
|
2023-05-16 06:41:53 -04:00
|
|
|
(declare-function org-element-property "org-element-ast" (property node))
|
2023-05-03 10:26:41 -04:00
|
|
|
(declare-function org-element-begin "org-element" (node))
|
|
|
|
(declare-function org-element-end "org-element" (node))
|
2023-05-16 06:41:53 -04:00
|
|
|
(declare-function org-element-type "org-element-ast" (node &optional anonymous))
|
2016-07-17 06:04:28 -04:00
|
|
|
(declare-function org-escape-code-in-string "org-src" (s))
|
2022-10-07 00:46:48 -04:00
|
|
|
(declare-function org-export-copy-buffer "ox"
|
|
|
|
(&optional buffer drop-visibility
|
|
|
|
drop-narrowing drop-contents
|
|
|
|
drop-locals))
|
2022-06-16 00:15:03 -04:00
|
|
|
(declare-function org-in-commented-heading-p "org" (&optional no-inheritance element))
|
|
|
|
(declare-function org-in-archived-heading-p "org" (&optional no-inheritance element))
|
2016-06-18 16:11:14 -04:00
|
|
|
|
|
|
|
(defvar org-src-preserve-indentation)
|
2010-06-12 21:32:23 -04:00
|
|
|
|
2016-11-12 21:54:20 -05:00
|
|
|
(defcustom org-export-use-babel t
|
|
|
|
"Switch controlling code evaluation and header processing during export.
|
2010-09-26 06:39:43 -04:00
|
|
|
When set to nil no code will be evaluated as part of the export
|
2017-08-26 04:32:26 -04:00
|
|
|
process and no header arguments will be obeyed. Users who wish
|
|
|
|
to avoid evaluating code on export should use the header argument
|
|
|
|
`:eval never-export'."
|
2010-07-08 13:28:47 -04:00
|
|
|
:group 'org-babel
|
2012-03-19 16:38:12 -04:00
|
|
|
:version "24.1"
|
2013-03-31 18:47:44 -04:00
|
|
|
:type '(choice (const :tag "Never" nil)
|
2016-11-12 21:54:20 -05:00
|
|
|
(const :tag "Always" t))
|
|
|
|
:safe #'null)
|
|
|
|
|
2010-07-08 13:28:47 -04:00
|
|
|
|
2016-06-18 17:15:24 -04:00
|
|
|
(defmacro org-babel-exp--at-source (&rest body)
|
|
|
|
"Evaluate BODY at the source of the Babel block at point.
|
|
|
|
Source is located in `org-babel-exp-reference-buffer'. The value
|
|
|
|
returned is the value of the last form in BODY. Assume that
|
|
|
|
point is at the beginning of the Babel block."
|
|
|
|
(declare (indent 1) (debug body))
|
|
|
|
`(let ((source (get-text-property (point) 'org-reference)))
|
2017-12-24 18:10:39 -05:00
|
|
|
;; Source blocks created during export process (e.g., by other
|
|
|
|
;; source blocks) are not referenced. In this case, do not move
|
|
|
|
;; point at all.
|
|
|
|
(with-current-buffer (if source org-babel-exp-reference-buffer
|
|
|
|
(current-buffer))
|
2016-06-18 17:15:24 -04:00
|
|
|
(org-with-wide-buffer
|
2017-12-24 18:10:39 -05:00
|
|
|
(when source (goto-char source))
|
2016-06-18 17:15:24 -04:00
|
|
|
,@body))))
|
|
|
|
|
2022-06-15 23:54:31 -04:00
|
|
|
(defun org-babel-exp-src-block (&optional element)
|
2010-07-13 19:04:47 -04:00
|
|
|
"Process source block for export.
|
2016-06-18 17:15:24 -04:00
|
|
|
Depending on the \":export\" header argument, replace the source
|
2013-04-10 10:19:04 -04:00
|
|
|
code block like this:
|
2009-04-23 18:48:22 -04:00
|
|
|
|
2009-08-17 14:19:25 -04:00
|
|
|
both ---- display the code and the results
|
2009-04-23 18:48:22 -04:00
|
|
|
|
2009-08-17 14:19:25 -04:00
|
|
|
code ---- the default, display the code inside the block but do
|
|
|
|
not process
|
2009-04-23 18:48:22 -04:00
|
|
|
|
2009-09-01 17:42:14 -04:00
|
|
|
results - just like none only the block is run on export ensuring
|
2016-06-18 17:15:24 -04:00
|
|
|
that its results are present in the Org mode buffer
|
2009-04-23 18:48:22 -04:00
|
|
|
|
2013-04-10 10:19:04 -04:00
|
|
|
none ---- do not display either code or results upon export
|
2012-08-19 16:07:55 -04:00
|
|
|
|
2022-06-15 23:54:31 -04:00
|
|
|
Optional argument ELEMENT must contain source block element at point.
|
|
|
|
|
2016-06-18 17:15:24 -04:00
|
|
|
Assume point is at block opening line."
|
2009-04-23 18:48:22 -04:00
|
|
|
(interactive)
|
2010-06-17 12:29:38 -04:00
|
|
|
(save-excursion
|
2022-06-15 23:54:31 -04:00
|
|
|
(let* ((info (org-babel-get-src-block-info nil element))
|
2010-09-18 21:01:49 -04:00
|
|
|
(lang (nth 0 info))
|
2016-06-18 17:15:24 -04:00
|
|
|
(raw-params (nth 2 info))
|
|
|
|
hash)
|
2010-07-16 18:34:57 -04:00
|
|
|
;; bail if we couldn't get any info from the block
|
2013-11-07 10:24:53 -05:00
|
|
|
(unless noninteractive
|
2016-05-20 07:38:57 -04:00
|
|
|
(message "org-babel-exp process %s at position %d..."
|
2016-06-18 17:15:24 -04:00
|
|
|
lang
|
|
|
|
(line-beginning-position)))
|
2010-07-16 18:34:57 -04:00
|
|
|
(when info
|
2011-02-27 13:01:56 -05:00
|
|
|
;; if we're actually going to need the parameters
|
2016-06-18 17:15:24 -04:00
|
|
|
(when (member (cdr (assq :exports (nth 2 info))) '("both" "results"))
|
|
|
|
(let ((lang-headers (intern (concat "org-babel-default-header-args:"
|
|
|
|
lang))))
|
|
|
|
(org-babel-exp--at-source
|
2016-06-23 18:55:03 -04:00
|
|
|
(setf (nth 2 info)
|
|
|
|
(org-babel-process-params
|
|
|
|
(apply #'org-babel-merge-params
|
|
|
|
org-babel-default-header-args
|
|
|
|
(and (boundp lang-headers)
|
2021-09-29 03:18:48 -04:00
|
|
|
(symbol-value lang-headers))
|
2016-06-23 18:55:03 -04:00
|
|
|
(append (org-babel-params-from-properties lang)
|
|
|
|
(list raw-params)))))))
|
2018-06-23 17:04:45 -04:00
|
|
|
(setf hash (org-babel-sha1-hash info :export)))
|
2011-02-27 13:01:56 -05:00
|
|
|
(org-babel-exp-do-export info 'block hash)))))
|
2009-04-26 16:29:59 -04:00
|
|
|
|
2012-03-19 16:38:12 -04:00
|
|
|
(defcustom org-babel-exp-call-line-template
|
|
|
|
""
|
|
|
|
"Template used to export call lines.
|
|
|
|
This template may be customized to include the call line name
|
|
|
|
with any export markup. The template is filled out using
|
|
|
|
`org-fill-template', and the following %keys may be used.
|
|
|
|
|
|
|
|
line --- call line
|
|
|
|
|
|
|
|
An example value would be \"\\n: call: %line\" to export the call line
|
|
|
|
wrapped in a verbatim environment.
|
|
|
|
|
|
|
|
Note: the results are inserted separately after the contents of
|
|
|
|
this template."
|
|
|
|
:group 'org-babel
|
|
|
|
:type 'string)
|
|
|
|
|
2016-06-18 16:11:14 -04:00
|
|
|
(defun org-babel-exp-process-buffer ()
|
|
|
|
"Execute all Babel blocks in current buffer."
|
2009-04-26 16:29:59 -04:00
|
|
|
(interactive)
|
2016-11-12 21:54:20 -05:00
|
|
|
(when org-export-use-babel
|
2023-04-02 11:00:06 -04:00
|
|
|
(let ((case-fold-search t)
|
|
|
|
(regexp "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)")
|
|
|
|
;; Get a pristine copy of current buffer so Babel
|
|
|
|
;; references are properly resolved and source block
|
|
|
|
;; context is preserved.
|
|
|
|
(org-babel-exp-reference-buffer (org-export-copy-buffer))
|
|
|
|
element)
|
|
|
|
(unwind-protect
|
|
|
|
(save-excursion
|
|
|
|
;; First attach to every source block their original
|
|
|
|
;; position, so that they can be retrieved within
|
|
|
|
;; `org-babel-exp-reference-buffer', even after heavy
|
|
|
|
;; modifications on current buffer.
|
|
|
|
;;
|
|
|
|
;; False positives are harmless, so we don't check if
|
|
|
|
;; we're really at some Babel object. Moreover,
|
|
|
|
;; `line-end-position' ensures that we propertize
|
|
|
|
;; a noticeable part of the object, without affecting
|
|
|
|
;; multiple objects on the same line.
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward regexp nil t)
|
|
|
|
(let ((s (match-beginning 0)))
|
|
|
|
(put-text-property s (line-end-position) 'org-reference s)))
|
|
|
|
;; Evaluate from top to bottom every Babel block
|
|
|
|
;; encountered.
|
|
|
|
(goto-char (point-min))
|
|
|
|
;; We are about to do a large number of changes in
|
|
|
|
;; buffer, but we do not care about folding in this
|
|
|
|
;; buffer.
|
|
|
|
(org-fold-core-ignore-modifications
|
2016-06-18 17:15:24 -04:00
|
|
|
(while (re-search-forward regexp nil t)
|
2023-05-06 09:03:04 -04:00
|
|
|
(setq element (save-match-data (org-element-at-point)))
|
2023-04-02 11:00:06 -04:00
|
|
|
(unless (save-match-data
|
|
|
|
(or (org-in-commented-heading-p nil element)
|
|
|
|
(org-in-archived-heading-p nil element)))
|
|
|
|
(let* ((object? (match-end 1))
|
|
|
|
(element (save-match-data
|
|
|
|
(if object?
|
|
|
|
(org-element-context element)
|
|
|
|
;; No deep inspection if we're
|
|
|
|
;; just looking for an element.
|
|
|
|
element)))
|
|
|
|
(type
|
|
|
|
(pcase (org-element-type element)
|
|
|
|
;; Discard block elements if we're looking
|
|
|
|
;; for inline objects. False results
|
|
|
|
;; happen when, e.g., "call_" syntax is
|
|
|
|
;; located within affiliated keywords:
|
|
|
|
;;
|
|
|
|
;; #+name: call_src
|
|
|
|
;; #+begin_src ...
|
|
|
|
((and (or `babel-call `src-block) (guard object?))
|
|
|
|
nil)
|
|
|
|
(type type)))
|
|
|
|
(begin
|
2023-05-03 10:26:41 -04:00
|
|
|
(copy-marker (org-element-begin element)))
|
2023-04-02 11:00:06 -04:00
|
|
|
(end
|
|
|
|
(copy-marker
|
|
|
|
(save-excursion
|
2023-05-03 10:26:41 -04:00
|
|
|
(goto-char (org-element-end element))
|
2023-04-02 11:00:06 -04:00
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(point)))))
|
|
|
|
(pcase type
|
|
|
|
(`inline-src-block
|
|
|
|
(let* ((info
|
|
|
|
(org-babel-get-src-block-info nil element))
|
|
|
|
(params (nth 2 info)))
|
|
|
|
(setf (nth 1 info)
|
|
|
|
(if (and (cdr (assq :noweb params))
|
|
|
|
(string= "yes"
|
|
|
|
(cdr (assq :noweb params))))
|
|
|
|
(org-babel-expand-noweb-references
|
|
|
|
info org-babel-exp-reference-buffer)
|
|
|
|
(nth 1 info)))
|
|
|
|
(goto-char begin)
|
|
|
|
(let ((replacement
|
|
|
|
(org-babel-exp-do-export info 'inline)))
|
|
|
|
(if (equal replacement "")
|
|
|
|
;; Replacement code is empty: remove
|
|
|
|
;; inline source block, including extra
|
|
|
|
;; white space that might have been
|
|
|
|
;; created when inserting results.
|
|
|
|
(delete-region begin
|
|
|
|
(progn (goto-char end)
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
(point)))
|
|
|
|
;; Otherwise: remove inline source block
|
|
|
|
;; but preserve following white spaces.
|
|
|
|
;; Then insert value.
|
|
|
|
(unless (string= replacement
|
|
|
|
(buffer-substring begin end))
|
|
|
|
(delete-region begin end)
|
|
|
|
(insert replacement))))))
|
|
|
|
((or `babel-call `inline-babel-call)
|
|
|
|
(org-babel-exp-do-export
|
|
|
|
(or (org-babel-lob-get-info element)
|
|
|
|
(user-error "Unknown Babel reference: %s"
|
|
|
|
(org-element-property :call element)))
|
|
|
|
'lob)
|
|
|
|
(let ((rep
|
|
|
|
(org-fill-template
|
|
|
|
org-babel-exp-call-line-template
|
|
|
|
`(("line" .
|
|
|
|
,(org-element-property :value element))))))
|
|
|
|
;; If replacement is empty, completely remove
|
|
|
|
;; the object/element, including any extra
|
|
|
|
;; white space that might have been created
|
|
|
|
;; when including results.
|
|
|
|
(if (equal rep "")
|
|
|
|
(delete-region
|
|
|
|
begin
|
|
|
|
(progn (goto-char end)
|
|
|
|
(if (not (eq type 'babel-call))
|
|
|
|
(progn (skip-chars-forward " \t")
|
|
|
|
(point))
|
|
|
|
(skip-chars-forward " \r\t\n")
|
|
|
|
(line-beginning-position))))
|
|
|
|
;; Otherwise, preserve trailing
|
|
|
|
;; spaces/newlines and then, insert
|
|
|
|
;; replacement string.
|
2022-06-15 23:54:31 -04:00
|
|
|
(goto-char begin)
|
2023-04-02 11:00:06 -04:00
|
|
|
(delete-region begin end)
|
|
|
|
(insert rep))))
|
|
|
|
(`src-block
|
|
|
|
(let ((match-start (copy-marker (match-beginning 0)))
|
|
|
|
(ind (org-current-text-indentation)))
|
|
|
|
;; Take care of matched block: compute
|
|
|
|
;; replacement string. In particular, a nil
|
|
|
|
;; REPLACEMENT means the block is left as-is
|
|
|
|
;; while an empty string removes the block.
|
|
|
|
(let ((replacement
|
|
|
|
(progn (goto-char match-start)
|
|
|
|
(org-babel-exp-src-block element))))
|
|
|
|
(cond ((not replacement) (goto-char end))
|
|
|
|
((equal replacement "")
|
|
|
|
(goto-char end)
|
|
|
|
(skip-chars-forward " \r\t\n")
|
2023-05-10 09:27:13 -04:00
|
|
|
(forward-line 0)
|
2023-04-02 11:00:06 -04:00
|
|
|
(delete-region begin (point)))
|
|
|
|
(t
|
|
|
|
(if (or org-src-preserve-indentation
|
|
|
|
(org-element-property
|
|
|
|
:preserve-indent element))
|
|
|
|
;; Indent only code block
|
|
|
|
;; markers.
|
|
|
|
(with-temp-buffer
|
|
|
|
;; Do not use tabs for block
|
|
|
|
;; indentation.
|
|
|
|
(when (fboundp 'indent-tabs-mode)
|
|
|
|
(indent-tabs-mode -1)
|
|
|
|
;; FIXME: Emacs 26
|
|
|
|
;; compatibility.
|
|
|
|
(setq-local indent-tabs-mode nil))
|
|
|
|
(insert replacement)
|
|
|
|
(skip-chars-backward " \r\t\n")
|
|
|
|
(indent-line-to ind)
|
|
|
|
(goto-char 1)
|
|
|
|
(indent-line-to ind)
|
|
|
|
(setq replacement (buffer-string)))
|
|
|
|
;; Indent everything.
|
|
|
|
(with-temp-buffer
|
|
|
|
;; Do not use tabs for block
|
|
|
|
;; indentation.
|
|
|
|
(when (fboundp 'indent-tabs-mode)
|
|
|
|
(indent-tabs-mode -1)
|
|
|
|
;; FIXME: Emacs 26
|
|
|
|
;; compatibility.
|
|
|
|
(setq-local indent-tabs-mode nil))
|
|
|
|
(insert replacement)
|
|
|
|
(indent-rigidly
|
|
|
|
1 (point) ind)
|
|
|
|
(setq replacement (buffer-string))))
|
|
|
|
(goto-char match-start)
|
|
|
|
(let ((rend (save-excursion
|
|
|
|
(goto-char end)
|
|
|
|
(line-end-position))))
|
|
|
|
(if (string-equal replacement
|
|
|
|
(buffer-substring match-start rend))
|
|
|
|
(goto-char rend)
|
|
|
|
(delete-region match-start
|
|
|
|
(save-excursion
|
|
|
|
(goto-char end)
|
|
|
|
(line-end-position)))
|
|
|
|
(insert replacement))))))
|
|
|
|
(set-marker match-start nil))))
|
|
|
|
(set-marker begin nil)
|
|
|
|
(set-marker end nil))))))
|
|
|
|
(kill-buffer org-babel-exp-reference-buffer)
|
|
|
|
(remove-text-properties (point-min) (point-max)
|
|
|
|
'(org-reference nil))))))
|
2012-08-19 16:19:59 -04:00
|
|
|
|
2011-02-27 13:01:56 -05:00
|
|
|
(defun org-babel-exp-do-export (info type &optional hash)
|
2010-07-13 19:04:47 -04:00
|
|
|
"Return a string with the exported content of a code block.
|
|
|
|
The function respects the value of the :exports header argument."
|
2016-09-22 13:45:15 -04:00
|
|
|
(let ((silently (lambda () (let ((session (cdr (assq :session (nth 2 info)))))
|
2023-04-02 11:00:06 -04:00
|
|
|
(unless (equal "none" session)
|
|
|
|
(org-babel-exp-results info type 'silent)))))
|
2015-01-29 15:05:31 -05:00
|
|
|
(clean (lambda () (if (eq type 'inline)
|
2023-04-02 11:00:06 -04:00
|
|
|
(org-babel-remove-inline-result)
|
|
|
|
(org-babel-remove-result info)))))
|
2016-02-10 08:48:06 -05:00
|
|
|
(pcase (or (cdr (assq :exports (nth 2 info))) "code")
|
|
|
|
("none" (funcall silently) (funcall clean) "")
|
|
|
|
("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
|
|
|
|
("results" (org-babel-exp-results info type nil hash) "")
|
|
|
|
("both"
|
|
|
|
(org-babel-exp-results info type nil hash)
|
|
|
|
(org-babel-exp-code info type)))))
|
2011-05-19 09:37:33 -04:00
|
|
|
|
2012-03-19 16:38:12 -04:00
|
|
|
(defcustom org-babel-exp-code-template
|
2022-06-15 23:54:31 -04:00
|
|
|
"#+begin_src %lang%switches%flags\n%body\n#+end_src"
|
2012-03-19 16:38:12 -04:00
|
|
|
"Template used to export the body of code blocks.
|
|
|
|
This template may be customized to include additional information
|
|
|
|
such as the code block name, or the values of particular header
|
|
|
|
arguments. The template is filled out using `org-fill-template',
|
|
|
|
and the following %keys may be used.
|
|
|
|
|
|
|
|
lang ------ the language of the code block
|
|
|
|
name ------ the name of the code block
|
|
|
|
body ------ the body of the code block
|
2014-01-29 03:01:43 -05:00
|
|
|
switches -- the switches associated to the code block
|
2012-03-19 16:38:12 -04:00
|
|
|
flags ----- the flags passed to the code block
|
|
|
|
|
|
|
|
In addition to the keys mentioned above, every header argument
|
|
|
|
defined for the code block may be used as a key and will be
|
|
|
|
replaced with its value."
|
|
|
|
:group 'org-babel
|
2022-11-13 02:29:15 -05:00
|
|
|
:type 'string
|
|
|
|
:package-version '(Org . "9.6"))
|
2012-03-19 16:38:12 -04:00
|
|
|
|
2014-06-13 09:32:54 -04:00
|
|
|
(defcustom org-babel-exp-inline-code-template
|
|
|
|
"src_%lang[%switches%flags]{%body}"
|
|
|
|
"Template used to export the body of inline code blocks.
|
|
|
|
This template may be customized to include additional information
|
|
|
|
such as the code block name, or the values of particular header
|
|
|
|
arguments. The template is filled out using `org-fill-template',
|
|
|
|
and the following %keys may be used.
|
|
|
|
|
|
|
|
lang ------ the language of the code block
|
|
|
|
name ------ the name of the code block
|
|
|
|
body ------ the body of the code block
|
|
|
|
switches -- the switches associated to the code block
|
|
|
|
flags ----- the flags passed to the code block
|
|
|
|
|
|
|
|
In addition to the keys mentioned above, every header argument
|
|
|
|
defined for the code block may be used as a key and will be
|
|
|
|
replaced with its value."
|
|
|
|
:group 'org-babel
|
|
|
|
:type 'string
|
2017-01-25 23:39:18 -05:00
|
|
|
:version "26.1"
|
2014-06-13 09:32:54 -04:00
|
|
|
:package-version '(Org . "8.3"))
|
|
|
|
|
|
|
|
(defun org-babel-exp-code (info type)
|
2011-05-19 09:37:33 -04:00
|
|
|
"Return the original code block formatted for export."
|
2012-03-31 11:28:37 -04:00
|
|
|
(setf (nth 1 info)
|
2016-09-22 13:45:15 -04:00
|
|
|
(if (string= "strip-export" (cdr (assq :noweb (nth 2 info))))
|
2012-03-31 11:28:37 -04:00
|
|
|
(replace-regexp-in-string
|
|
|
|
(org-babel-noweb-wrap) "" (nth 1 info))
|
|
|
|
(if (org-babel-noweb-p (nth 2 info) :export)
|
|
|
|
(org-babel-expand-noweb-references
|
2014-03-14 10:27:32 -04:00
|
|
|
info org-babel-exp-reference-buffer)
|
2012-03-31 11:28:37 -04:00
|
|
|
(nth 1 info))))
|
2011-05-19 13:05:41 -04:00
|
|
|
(org-fill-template
|
2014-06-13 09:32:54 -04:00
|
|
|
(if (eq type 'inline)
|
2015-08-10 01:18:52 -04:00
|
|
|
org-babel-exp-inline-code-template
|
2021-04-13 03:55:29 -04:00
|
|
|
org-babel-exp-code-template)
|
2011-05-19 13:05:41 -04:00
|
|
|
`(("lang" . ,(nth 0 info))
|
2021-04-13 03:55:29 -04:00
|
|
|
;; Inline source code should not be escaped.
|
|
|
|
("body" . ,(let ((body (nth 1 info)))
|
|
|
|
(if (eq type 'inline) body
|
|
|
|
(org-escape-code-in-string body))))
|
2014-01-29 03:01:43 -05:00
|
|
|
("switches" . ,(let ((f (nth 3 info)))
|
|
|
|
(and (org-string-nw-p f) (concat " " f))))
|
2014-01-20 08:07:57 -05:00
|
|
|
("flags" . ,(let ((f (assq :flags (nth 2 info))))
|
2014-01-29 03:01:43 -05:00
|
|
|
(and f (concat " " (cdr f)))))
|
2012-03-19 16:38:12 -04:00
|
|
|
,@(mapcar (lambda (pair)
|
|
|
|
(cons (substring (symbol-name (car pair)) 1)
|
|
|
|
(format "%S" (cdr pair))))
|
|
|
|
(nth 2 info))
|
|
|
|
("name" . ,(or (nth 4 info) "")))))
|
2009-04-23 18:48:22 -04:00
|
|
|
|
2011-02-27 13:01:56 -05:00
|
|
|
(defun org-babel-exp-results (info type &optional silent hash)
|
2010-07-13 19:04:47 -04:00
|
|
|
"Evaluate and return the results of the current code block for export.
|
2016-02-10 16:47:09 -05:00
|
|
|
Results are prepared in a manner suitable for export by Org mode.
|
2010-07-13 19:04:47 -04:00
|
|
|
This function is called by `org-babel-exp-do-export'. The code
|
|
|
|
block will be evaluated. Optional argument SILENT can be used to
|
|
|
|
inhibit insertion of results into the buffer."
|
2016-04-28 11:25:31 -04:00
|
|
|
(unless (and hash (equal hash (org-babel-current-result-hash)))
|
2011-01-27 14:48:45 -05:00
|
|
|
(let ((lang (nth 0 info))
|
2019-01-19 11:27:20 -05:00
|
|
|
(body (if (org-babel-noweb-p (nth 2 info) :eval)
|
2012-03-31 11:28:37 -04:00
|
|
|
(org-babel-expand-noweb-references
|
2014-03-14 10:27:32 -04:00
|
|
|
info org-babel-exp-reference-buffer)
|
2012-03-31 11:28:37 -04:00
|
|
|
(nth 1 info)))
|
2013-06-08 14:00:53 -04:00
|
|
|
(info (copy-sequence info))
|
2013-06-08 15:19:38 -04:00
|
|
|
(org-babel-current-src-block-location (point-marker)))
|
2016-02-09 18:22:09 -05:00
|
|
|
;; Skip code blocks which we can't evaluate.
|
2023-04-02 11:02:39 -04:00
|
|
|
(if (not (fboundp (intern (concat "org-babel-execute:" lang))))
|
|
|
|
(warn "org-export: No org-babel-execute function for %s. Not updating exported results." lang)
|
2011-01-27 14:48:45 -05:00
|
|
|
(org-babel-eval-wipe-error-buffer)
|
2016-06-18 17:15:24 -04:00
|
|
|
(setf (nth 1 info) body)
|
|
|
|
(setf (nth 2 info)
|
|
|
|
(org-babel-exp--at-source
|
2021-09-29 03:22:47 -04:00
|
|
|
(org-babel-process-params
|
|
|
|
(org-babel-merge-params
|
|
|
|
(nth 2 info)
|
|
|
|
`((:results . ,(if silent "silent" "replace")))))))
|
2016-06-18 17:15:24 -04:00
|
|
|
(pcase type
|
|
|
|
(`block (org-babel-execute-src-block nil info))
|
|
|
|
(`inline
|
|
|
|
;; Position the point on the inline source block
|
|
|
|
;; allowing `org-babel-insert-result' to check that the
|
|
|
|
;; block is inline.
|
|
|
|
(goto-char (nth 5 info))
|
|
|
|
(org-babel-execute-src-block nil info))
|
|
|
|
(`lob
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (nth 5 info))
|
2020-11-01 18:33:39 -05:00
|
|
|
(org-babel-execute-src-block nil info))))))))
|
2009-09-12 14:14:09 -04:00
|
|
|
|
2012-08-19 16:19:59 -04:00
|
|
|
(provide 'ob-exp)
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-exp.el ends here
|