org-babel: Improve parsing of colnames in tables with horizontal lines

* lisp/ob-core.el (org-babel-get-colnames): When table starts with
horizontal lines, consider column names to be the first non-hline row.
(org-babel-disassemble-tables): When detecting automatic column names,
do not assign colnames when the first row is an hline.
* doc/org-manual.org (Passing arguments): Update the manual, detailing
that leading hline rows are skipped for :colnames yes.

Link: https://orgmode.org/list/87wmqexjoj.fsf@localhost
This commit is contained in:
Ihor Radchenko 2024-03-07 22:02:21 +03:00
parent 0a6c881174
commit cab81f2428
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 15 additions and 6 deletions

View File

@ -18121,9 +18121,9 @@ Here are examples of passing values by reference:
names---because the second row is a horizontal rule---then Org names---because the second row is a horizontal rule---then Org
removes the column names, processes the table, puts back the column removes the column names, processes the table, puts back the column
names, and then writes the table to the results block. Using =yes=, names, and then writes the table to the results block. Using =yes=,
Org does the same to the first row, even if the initial table does Org does the same to the first non-hline row, even if the initial
not contain any horizontal rule. When set to =no=, Org does not table does not contain any horizontal rule. When set to =no=, Org
pre-process column names at all. does not pre-process column names at all.
# We keep python blocks unindented on purpose - to keep the example # We keep python blocks unindented on purpose - to keep the example
# working even for users who changed the default value of ~org-src-preserve-indentation~ # working even for users who changed the default value of ~org-src-preserve-indentation~

View File

@ -1834,6 +1834,8 @@ HEADER-ARGUMENTS is alist of all the arguments."
Return a cons cell, the `car' of which contains the TABLE less Return a cons cell, the `car' of which contains the TABLE less
colnames, and the `cdr' of which contains a list of the column colnames, and the `cdr' of which contains a list of the column
names." names."
;; Skip over leading hlines.
(while (eq 'hline (car table)) (pop table))
(if (eq 'hline (nth 1 table)) (if (eq 'hline (nth 1 table))
(cons (cddr table) (car table)) (cons (cddr table) (car table))
(cons (cdr table) (car table)))) (cons (cdr table) (car table))))
@ -1895,9 +1897,16 @@ of the vars, cnames and rnames."
(when (and (not (equal colnames "no")) (when (and (not (equal colnames "no"))
;; Compatibility note: avoid `length>', which ;; Compatibility note: avoid `length>', which
;; isn't available until Emacs 28. ;; isn't available until Emacs 28.
(or colnames (and (> (length (cdr var)) 1) (or colnames
(eq (nth 1 (cdr var)) 'hline) ;; :colnames nil (default)
(not (member 'hline (cddr (cdr var))))))) ;; Auto-assign column names when the table
;; has hline as the second line after
;; non-hline row.
(and (> (length (cdr var)) 1)
(not (eq (car (cdr var)) 'hline)) ; first row
(eq (nth 1 (cdr var)) 'hline) ; second row
(not (member 'hline (cddr (cdr var)))) ; other rows
)))
(let ((both (org-babel-get-colnames (cdr var)))) (let ((both (org-babel-get-colnames (cdr var))))
(setq cnames (cons (cons (car var) (cdr both)) (setq cnames (cons (cons (car var) (cdr both))
cnames)) cnames))