* Heime <heimebor...@protonmail.com> [2022-11-05 10:45]: > Have been introspecting the possibility of conveniently inserting table > programatically > in elisp and encountered "table.el". > > Have constructed this function, but the difficulty centers around > the challenge of inserting text in specific tests. > > (defun make-table () > (interactive) > (table-insert 4 5) > (table-forward-cell) > (table-insert-sequence "icomplt-horz" 1 1 1 'center) > (table-forward-cell 4) > (table-insert-sequence "icomplt-vert" 1 1 1 'center) > (table-release)) > > It might be that "table.el" was designed under the assumption that the table > would > be edited interactively rather than from ELisp. The lack of info may just > reflect > that nobody has thought about it making tables programatically that also goes > beyond > its utilisation in "org-mode". > > I understand that "Org" cooperates with its author Takaaki Ota, perhaps > things could > be extended in a way that makes inserting tables programatically much easier > to work > with.
I would go programmatically not by hard coding the table walk, but by using some structure, for example: (setq my-table '(("ID" "Description" "Amount") ;; this must be header (1 "Payment for domain" 10.50) (2 "Transfer from Doe" 250))) Then I would use some function, something like: (my-org-table-generate my-table calculat-total 3rd-column) Then the function would only interpolate the table with basic small details, which allow later automatic table alignment: (defun rcd-org-table-cell (object) (cond ((numberp object) (format "| %.2f " object)) (t (format "| %s " object)))) (rcd-org-table-cell "Hello") ⇒ "| Hello " (defun rcd-org-table-horizontal-line () "Return `|-' as horizontal-line." "|--\n") then for rows: (defun rcd-org-table-row (list) (with-temp-buffer (while list (insert (rcd-org-table-cell (pop list)))) (insert "\n") (buffer-string))) This means you can do something like: (rcd-org-table-row '("ID" "Description" "Amount")) ⇒ "| ID | Description | Amount " then you make the main function: (defun rcd-org-table (structure) (let ((header (pop structure))) (with-temp-buffer (insert "\n") (insert (rcd-org-table-row header)) (insert (rcd-org-table-horizontal-line)) (while structure (insert (rcd-org-table-row (pop structure)))) (insert "\n") (org-mode) (goto-char 2) (org-table-align) (buffer-substring-no-properties (point-min) (point-max))))) And now the final result: (rcd-org-table my-table) ⇒ " | ID | Description | Amount | |------+--------------------+--------| | 1.00 | Payment for domain | 10.50 | | 2.00 | Transfer from Doe | 250.00 | " -- Jean Take action in Free Software Foundation campaigns: https://www.fsf.org/campaigns In support of Richard M. Stallman https://stallmansupport.org/