emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Babel C-mode corrupts double-quoted strings in output


From: Martin Jerabek
Subject: Babel C-mode corrupts double-quoted strings in output
Date: Wed, 31 Aug 2022 18:35:14 +0200
User-agent: Cyrus-JMAP/3.7.0-alpha0-841-g7899e99a45-fm-20220811.002-g7899e99a

Hi!

I recently started to use Org Babel for C++ programs. One of the programs 
outputs several lines with double-quoted strings, similar to this:

#+NAME: doublequotes_cpp
#+begin_src cpp :includes <iostream> :results output verbatim raw
std::cout << "\"line 1\"\n";
std::cout << "\"line 2\"\n";
std::cout << "\"line 3\"\n";
#+end_src

#+RESULTS: doublequotes_cpp
line 1

As you can see, only the first line is copied to the RESULTS block, and it is 
stripped of the double quotes.

I tracked down the problem to org-babel-read (in ob-core.el). 
org-babel-C-execute (in ob-C.el) calls this function with the output of the C++ 
program. The problem is the following line:

((eq (string-to-char cell) ?\") (read cell))

i.e. if the output of the program starts with a double quote, it is passed to 
read which reads only the first string and also removes the double quotes, 
resulting in the observed output.

The original version of this piece of code was added with the following commit:

commit 60a8ba556d682849eafb0f84e689967cd2965549
Author: Eric Schulte <schulte.eric@gmail.com>
Date:   Wed Mar 2 07:55:39 2011 -0700

    ob: read string variable values wrapped in double quotes, removing the 
quotes
    
    * lisp/ob.el (org-babel-read): Read string variable values wrapped in
      double quotes, removing the quotes.

AFAICT this modification was done in response to the email thread "[Orgmode] 
org-babel-read should have option NOT to interpret as elisp" started on 
2011-02-27, more specifically the email on "Wed, 02 Mar 2011 07:56:45 -0700" 
from Eric Schulte. This was obviously done for parsing variables in the header 
line, not for the program output, but the Babel C mode uses org-babel-read also 
for the output.

I assumed that ":results output verbatim raw" would prevent any postprocessing 
of the output but this is not the case for C mode.

I am not sure how to fix this without breaking backward compatibility. I assume 
it should be fixed directly in org-babel-C-execute, not in a central function 
like org-babel-read to minimize the impact. Surprisingly (for me) the 
equivalent shell script works as expected:

#+NAME: doublequotes
#+begin_src shell :results output verbatim raw
echo '"line 1"'
echo '"line 2"'
echo '"line 3"'
#+end_src

#+RESULTS: doublequotes
"line 1"
"line 2"
"line 3"

because org-babel-execute:shell does not process the output with 
org-babel-read. I do not know if languages other than the C family (C, C++, D) 
are affected.

At the very least, the documentation of org-babel-read should be expanded to 
document the fact that if the CELL parameter starts with a double quote, it is 
processed by the read function.

Best regards
Martin Jerabek



reply via email to

[Prev in Thread] Current Thread [Next in Thread]