From f08174a459b4aa005f640f88f3a8cf4492543f79 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Fri, 8 Mar 2024 12:42:33 +0300 Subject: [PATCH] org-export: Display a warning when the value of :exports header arg is invalid * lisp/ob-exp.el (org-babel-exp-do-export): Display warning when :exports value is not known. Document nil return value. (org-babel-exp-process-buffer): Do not remove code block when `org-babel-exp-do-export' returns nil. Reported-by: Max Nikulin Link: https://orgmode.org/list/usc9jn$g2r$1@ciao.gmane.io --- lisp/ob-exp.el | 70 +++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el index aa6091924..af726dc2c 100644 --- a/lisp/ob-exp.el +++ b/lisp/ob-exp.el @@ -218,22 +218,27 @@ this template." (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)))))) + (cond + ((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)))) + ((not replacement) + ;; Replacement code cannot be determined. + ;; Leave the code block as is. + (goto-char end)) + ;; Otherwise: remove inline source block + ;; but preserve following white spaces. + ;; Then insert value. + ((not (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) @@ -249,21 +254,27 @@ this template." ;; 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)))) + (cond + ((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))))) + ((not rep) + ;; Replacement code cannot be determined. + ;; Leave the code block as is. + (goto-char end)) + (t ;; Otherwise, preserve trailing ;; spaces/newlines and then, insert ;; replacement string. (goto-char begin) (delete-region begin end) - (insert rep)))) + (insert rep))))) (`src-block (let ((match-start (copy-marker (match-beginning 0))) (ind (org-current-text-indentation))) @@ -335,6 +346,8 @@ this template." TYPE is the code block type: `block', `inline', or `lob'. HASH is the result hash. +Return nil when exported content cannot be determined. + The function respects the value of the :exports header argument." (let ((silently (lambda () (let ((session (cdr (assq :session (nth 2 info))))) (unless (equal "none" session) @@ -348,7 +361,10 @@ The function respects the value of the :exports header argument." ("results" (org-babel-exp-results info type nil hash) "") ("both" (org-babel-exp-results info type nil hash) - (org-babel-exp-code info type))))) + (org-babel-exp-code info type)) + (unknown-value + (warn "Unknown value of src block parameter :exports %S" unknown-value) + nil)))) (defcustom org-babel-exp-code-template "#+begin_src %lang%switches%flags\n%body\n#+end_src"