[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[O] Re: List-table feature (or a potential quick and easy mullti-lines t
[O] Re: List-table feature (or a potential quick and easy mullti-lines table in org?)
Thu, 17 Mar 2011 16:41:56 +0100
Ben <address@hidden> writes:
> I'm thinking about a potential alternative and I would like to know if
> anyone here would know if this can be done with org.
> ReStructured Text  has a nice feature called list-tables. As you can
> guess from the name, you write a list and an instruction to process it and
> it creates a table out of the list in the export target. See the ReST
> documentation for a quick explanation . What is does it to transform a
> nested list in a simple table. And potentially it would make long list items
> / table content easy to edit.
> Does anyone has heard of such a possibility in Org?
Out of boredom, I've written a draft for it.
First we need the following function that might be of some use (i.e. to
Babel, as you can write an Org list as a lisp list and write it to the
buffer). Well anyway, here it is:
(defun org-list-to-org (list)
"Convert LIST into an Org list.
LIST is as returned by `org-list-parse-list'."
(let ((sep (if (eq org-plain-list-ordered-item-terminator ?\)) ")" "."))
(ltype (make-vector 20 "-")))
:ostart (and (aset ltype depth (concat "1" sep)) "")
:dstart (and (aset ltype depth "-") "")
:ustart (and (aset ltype depth "-") "")
:icount (concat (make-string depth ?\ )
(org-list-bullet-string (aref ltype depth))
(format "address@hidden " counter))
:istart (concat (make-string depth ?\ )
(org-list-bullet-string (aref ltype depth)))
:cbon "[X]" :cboff "[ ]"
:dtstart (if (and org-list-two-spaces-after-bullet-regexp
"-")) " " " ")
:ddstart " :: "))))
Next, we need bi-directional internal representation converters from
a table to a list:
(defun org-lisp-list-to-table (list)
"Change LIST lisp representation into a table lisp representation."
(mapcar (lambda (sub)
(cons (nth 1 sub)
(mapcar (lambda (item) (nth 1 item)) (cdr (nth 2 sub)))))
(defun org-lisp-table-to-list (table)
"Change TABLE lisp representation into a list lisp representation."
(mapcar (lambda (row)
(list nil (car row)
(mapcar (lambda (cell)
(list nil cell))
Finally, we need the interactive functions for the user. Those will
also clean up output.
(defun org-convert-list-to-table ()
"Transform list at point into a table."
(let ((parsed (org-list-parse-list t)))
(insert (orgtbl-to-orgtbl (org-lisp-list-to-table parsed) nil) "\n")
(error "Not at a list")))
(defun org-convert-table-to-list ()
"Transform table at point into a list."
(let ((pos (point))
(delete-region (org-table-begin) (org-table-end))
(insert (org-list-to-org (org-lisp-table-to-list table)))
(error "Not at a table")))
All of this will convert
- Row 1
- Row 2
| Row 1 | 1.1 | 1.2 | 1.3 |
| Row 2 | 2.1 | 2.2 | 2.3 |
and the other way.
- I'm far from being an Org table expert. There probably are corner
cases. This also doesn't support hlines.
- This requires latest git head (b6fc03b)