* Re: [BUG] obscure error for invalid :exports
2024-03-07 11:49 [BUG] obscure error for invalid :exports Max Nikulin
@ 2024-03-08 9:46 ` Ihor Radchenko
2024-03-10 10:30 ` Max Nikulin
0 siblings, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2024-03-08 9:46 UTC (permalink / raw)
To: Max Nikulin; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 615 bytes --]
Max Nikulin <manikulin@gmail.com> writes:
> Trying to export (to HTML buffer)
>
> src_elisp[:exports source]{a}
>
> I have got an obscure error
>
> org-export-as: Wrong type argument: char-or-string-p, nil
>
> I believe, some meaningful error should be signaled.
>
> I was quite surprised since the following is exported with no error
>
> #+begin_src elisp :exports source
> a
> #+end_src
Confirmed.
The behavior on unknown value of :exports header arg is undefined.
We may, however, show a warning in such scenario and avoid throwing
cryptic error.
See the attached tentative patch.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-export-Display-a-warning-when-the-value-of-expor.patch --]
[-- Type: text/x-patch, Size: 5022 bytes --]
From 8fcf787ca739008118f9834720ee56d6e190401d Mon Sep 17 00:00:00 2001
Message-ID: <8fcf787ca739008118f9834720ee56d6e190401d.1709891167.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
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 <manikulin@gmail.com>
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 @@ (defun org-babel-exp-process-buffer ()
(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 @@ (defun org-babel-exp-process-buffer ()
;; 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 @@ (defun org-babel-exp-do-export (info type &optional hash)
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 @@ (defun org-babel-exp-do-export (info type &optional hash)
("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"
--
2.43.0
[-- Attachment #3: Type: text/plain, Size: 224 bytes --]
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
^ permalink raw reply related [flat|nested] 4+ messages in thread