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
removes the column names, processes the table, puts back the column
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
not contain any horizontal rule. When set to =no=, Org does not
pre-process column names at all.
Org does the same to the first non-hline row, even if the initial
table does not contain any horizontal rule. When set to =no=, Org
does not pre-process column names at all.
# 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~

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
colnames, and the `cdr' of which contains a list of the column
names."
;; Skip over leading hlines.
(while (eq 'hline (car table)) (pop table))
(if (eq 'hline (nth 1 table))
(cons (cddr table) (car table))
(cons (cdr table) (car table))))
@ -1895,9 +1897,16 @@ of the vars, cnames and rnames."
(when (and (not (equal colnames "no"))
;; Compatibility note: avoid `length>', which
;; isn't available until Emacs 28.
(or colnames (and (> (length (cdr var)) 1)
(eq (nth 1 (cdr var)) 'hline)
(not (member 'hline (cddr (cdr var)))))))
(or colnames
;; :colnames nil (default)
;; 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))))
(setq cnames (cons (cons (car var) (cdr both))
cnames))