emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: Max Nikulin <manikulin@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: [BUG] obscure error for invalid :exports
Date: Fri, 08 Mar 2024 09:46:30 +0000	[thread overview]
Message-ID: <87wmqdhx8p.fsf@localhost> (raw)
In-Reply-To: <usc9jn$g2r$1@ciao.gmane.io>

[-- 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>

  reply	other threads:[~2024-03-08  9:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-07 11:49 [BUG] obscure error for invalid :exports Max Nikulin
2024-03-08  9:46 ` Ihor Radchenko [this message]
2024-03-10 10:30   ` Max Nikulin
2024-03-12 13:10     ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wmqdhx8p.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    --cc=manikulin@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).