[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Elisp macro for working with org tables
From: |
Mark Clements |
Subject: |
Elisp macro for working with org tables |
Date: |
Wed, 3 Aug 2022 08:33:59 +0000 |
In response to a question on Stackoverflow
(https://stackoverflow.com/questions/50921305/org-table-and-named-columns-for-babel-processing),
I have written an elisp macro to work with org tables:
#+NAME: test-table
| a | b |
|---+---|
| 1 | 2 |
| 3 | 4 |
#+NAME: test-table-script
#+BEGIN_SRC emacs-lisp :var table=test-table :colnames no
(require 'cl)
(defmacro with-table (table expression)
"Given an org table with colnames, evaluate an expression using the
colnames.
Note: use ':colnames no' in the header -- this imports the names but does not
export them."
`(let* ((table-with-colnames (org-babel-get-colnames ,table))
(names (loop for name in (cdr table-with-colnames)
collect (if (stringp name) (intern name) name))))
(funcall
`(lambda (data)
(mapcar (lambda (row)
(cl-destructuring-bind ,names row ,',expression))
data))
(car table-with-colnames))))
;; example to add a third column
(cons '(a b c) (cons 'hline (with-table table (list a b (+ a b)))))
#+END_SRC
#+RESULTS: test-table-script
| a | b | c |
|---+---+---|
| 1 | 2 | 3 |
| 3 | 4 | 7 |
Comments are welcome -- and I hope that this is useful.
Sincerely, Mark.
När du skickar e-post till Karolinska Institutet (KI) innebär detta att KI
kommer att behandla dina personuppgifter. Här finns information om hur KI
behandlar personuppgifter<https://ki.se/medarbetare/integritetsskyddspolicy>.
Sending email to Karolinska Institutet (KI) will result in KI processing your
personal data. You can read more about KI’s processing of personal data
here<https://ki.se/en/staff/data-protection-policy>.
- Elisp macro for working with org tables,
Mark Clements <=