From: Max Nikulin <manikulin@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: Importing "quoted" strings in `org-babel-import-elisp-from-file'
Date: Sat, 4 May 2024 14:53:39 +0700 [thread overview]
Message-ID: <v14pi5$jrb$1@ciao.gmane.io> (raw)
In-Reply-To: <87h6fft7z6.fsf@localhost>
[-- Attachment #1: Type: text/plain, Size: 1156 bytes --]
On 03/05/2024 19:06, Ihor Radchenko wrote:
> Max Nikulin writes:
>
>> What I do not like in `org-babel-read' is false positive for escaped
>> quote when actually backslash is escaped:
>>
>> (org-babel-read "\"1\\\\\" 2 \\\\\"3\"" t)
>> "1\\"
>
> Fixed, on main.
> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=2028bb15c
I have no idea if "other\n\"string\"\nlines" may be passed
`org-babel-read', but it is not discarded by the current regexp:
"^[[:space:]]*\"\\(.*\\)\"[[:space:]]*$"
Is there a reason why it is necessary to call `read' twice on the same
content? From my point of view, result of first call may be returned.
Does `read' have other role than unescaping backslash-protected
characters? Likely it can be done by `replace-regexp-in-string', see the
attachment. I have tried regexp on the following string:
(let ((cases '(("" . nil)
("\"" . nil)
("\"\"" . t)
("\"\\\"" . nil)
("\"\\\\\"" . t)
("a" . nil)
("\"a\"" . t)
("\\\"a\\\"" . nil)
("\\\"a\\\\\"" . nil)
("\"a\\\"\"" . t)
("\"aa\\\"bb\"" . t)
(" \"aa( bb\"" . t))))
[-- Attachment #2: patch-ob-read-quoted-string.diff --]
[-- Type: text/x-patch, Size: 2050 bytes --]
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 470db9fe6..a44d27cba 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -3346,6 +3346,26 @@ (defun org-babel-script-escape (str &optional force)
(t str))))
(condition-case nil (org-babel-read escaped) (error escaped))))
+(defconst org-babel--quoted-string-regexp
+ (rx string-start
+ (zero-or-more (or space ?\n))
+ ?\"
+ (group
+ (zero-or-more
+ ;; Anything besides double quotes ended not with backslash.
+ (zero-or-one (zero-or-more (not ?\"))
+ (not (or ?\" ?\\)))
+ ;; Skip backslashes escaping themselves.
+ (zero-or-more "\\\\")
+ ;; Escaped quotes are allowed.
+ (zero-or-one "\\\"")))
+ ?\"
+ (zero-or-more (or space ?\n))
+ string-end)
+ "Regexp matching single string in double quotes.
+Group 1 is text inside quotes. String may be optionally padded with
+spaces. Backslashes quote other characters.")
+
(defun org-babel-read (cell &optional inhibit-lisp-eval)
"Convert the string value of CELL to a number if appropriate.
Otherwise if CELL looks like Lisp (meaning it starts with a
@@ -3361,15 +3381,11 @@ (defun org-babel-read (cell &optional inhibit-lisp-eval)
;; FIXME: Arbitrary code evaluation.
(eval (read cell) t))
((save-match-data
- (and (string-match "^[[:space:]]*\"\\(.*\\)\"[[:space:]]*$" cell)
- ;; CELL is a single string
- (with-temp-buffer
- (insert cell)
- (goto-char 1)
- (read (current-buffer))
- (skip-chars-forward "[:space:]")
- (eobp))))
- (read cell))
+ (and (string-match org-babel--quoted-string-regexp cell)
+ ;; Unquote characters escaped by backslashes similar to `read'.
+ (replace-regexp-in-string
+ "\\\\\\(?:\\(.\\)\\|\n\\)" "\\1"
+ (match-string 1 cell) 'fixedcase nil))))
(t (org-no-properties cell))))
(defun org-babel--string-to-number (string)
next prev parent reply other threads:[~2024-05-04 7:54 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-28 13:15 [BUG] ob-shell: results missing leading quotes Matt
2024-04-29 11:58 ` Ihor Radchenko
2024-05-01 10:48 ` Max Nikulin
2024-05-01 12:19 ` Importing "quoted" strings in `org-babel-import-elisp-from-file' (was: [BUG] ob-shell: results missing leading quotes) Ihor Radchenko
2024-05-02 17:56 ` Importing "quoted" strings in `org-babel-import-elisp-from-file' Max Nikulin
2024-05-03 12:06 ` Ihor Radchenko
2024-05-04 7:53 ` Max Nikulin [this message]
2024-05-04 8:03 ` Ihor Radchenko
2024-05-04 11:17 ` Max Nikulin
2024-05-04 11:51 ` Ihor Radchenko
2024-05-04 14:55 ` Max Nikulin
2024-05-04 19:34 ` Ihor Radchenko
2024-05-04 15:58 ` Max Nikulin
2024-05-04 19:35 ` 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='v14pi5$jrb$1@ciao.gmane.io' \
--to=manikulin@gmail.com \
--cc=emacs-orgmode@gnu.org \
/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).