ob-core: Resolve named list references to simple lists

* lisp/ob-core.el (org-babel-read-list): Return a simple list instead
of list of lists.  Document this in the docstring.
* testing/lisp/test-ob-java.el (ob-java/read-return-list):
(ob-java/read-list-return-array):
(ob-java/read-return-list-with-package): Fix tests assuming previous
behavior.
* testing/lisp/test-ob.el (test-ob/simple-variable-resolution): Add
new tests.
* etc/ORG-NEWS (List references in source block variable assignments
are now proper lists): Document the change.

This commit fixes the broken promise in the manual section 16.4
Environment of a Code Block where the named references to lists should
be converted to simple lists consisting of the top-level items.

The inconsistency existed for a while and possibly lurked into some
third-party packages.  So, announcement in NEWS is required.

Reported-by: Alain.Cochard@unistra.fr
Link: https://orgmode.org/list/87pmdqfao4.fsf@localhost
This commit is contained in:
Ihor Radchenko 2022-11-15 13:52:04 +08:00
parent c72d5ee840
commit b4e437f968
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
4 changed files with 71 additions and 10 deletions

View File

@ -778,6 +778,40 @@ If you prefer to keep the keybinding, you can add it back to
(define-key org-mode-map (kbd "C-c SPC") #'org-table-blank-field)
#+end_src
*** List references in source block variable assignments are now proper lists
List representation of named lists is now converted to a simple list
as promised by the manual section [[info:org#Environment of a Code Block][org#Environment of a Code Block]].
Previously, it was converted to a list of lists.
Before:
#+begin_src org
,#+NAME: example-list
- simple
- not
- nested
- list
,#+BEGIN_SRC emacs-lisp :var x=example-list :results value
(format "%S" x)
,#+END_SRC
,#+RESULTS:
: (("simple" (unordered ("not") ("nested"))) ("list"))
#+end_src
After:
#+begin_src org
,#+BEGIN_SRC emacs-lisp :var x=example-list :results value
(format "%S" x)
,#+END_SRC
,#+RESULTS:
: ("simple" "list")
#+end_src
** New features
*** New citation engine

View File

@ -2239,8 +2239,15 @@ Return nil if ELEMENT cannot be read."
(org-table-to-lisp)))
(defun org-babel-read-list ()
"Read the list at point into emacs-lisp."
(mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval))
"Read the list at point into emacs-lisp.
Return the list of strings representing top level items:
(item1 item2 ...)
Only consider top level items. See Info node `(org)Environment of \
a Code Block'."
(mapcar (lambda (el) (org-babel-read (car el) 'inhibit-lisp-eval))
(cdr (org-list-to-lisp))))
(defvar org-link-types-re)

View File

@ -379,8 +379,8 @@ return a;
"#+begin_src java :dir 'nil :var a=java_list :results value silent
import java.util.List;
import java.util.Arrays;
List<String> b = Arrays.asList(a.get(0).get(0),
a.get(1).get(0));
List<String> b = Arrays.asList(a.get(0),
a.get(1));
return b;
#+end_src
@ -394,7 +394,7 @@ return b;
"Read a list and return an array."
(org-test-with-temp-text
"#+begin_src java :dir 'nil :var a=java_list :results value silent
String[] b = {a.get(0).get(0), a.get(1).get(0)};
String[] b = {a.get(0), a.get(1)};
return b;
#+end_src
@ -411,8 +411,8 @@ return b;
package pkg;
import java.util.List;
import java.util.Arrays;
List<String> b = Arrays.asList(a.get(0).get(0),
a.get(1).get(0));
List<String> b = Arrays.asList(a.get(0),
a.get(1));
return b;
#+end_src

View File

@ -204,7 +204,27 @@ list, then it should be treated as such; not as the symbol nil."
(forward-line 5)
(should (string= ": 4" (buffer-substring
(point-at-bol)
(point-at-eol))))))
(point-at-eol)))))
;; Test reading lists.
(org-test-with-temp-text-in-file "
#+NAME: example-list
- simple
- not
- nested
- list
<point>#+BEGIN_SRC emacs-lisp :var x=example-list
(print x)
#+END_SRC"
(should (equal '("simple" "list") (org-babel-execute-src-block)))
(forward-line 5)
(should (string=
"| simple | list |"
(buffer-substring
(point-at-bol)
(point-at-eol))))))
(ert-deftest test-ob/block-content-resolution ()
"Test block content resolution."
@ -218,8 +238,8 @@ list, then it should be treated as such; not as the symbol nil."
#+begin_src emacs-lisp :var four=four[]
(length (eval (car (read-from-string four))))
#+end_src"
(org-babel-next-src-block 2)
(should (= 4 (org-babel-execute-src-block)))))
(org-babel-next-src-block 2)
(should (= 4 (org-babel-execute-src-block)))))
(ert-deftest test-ob/cons-cell-as-variable ()
"Test that cons cell can be assigned as variable."