[Top][All Lists]

[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

From: Nicolas
Subject: [O] Re: List-table feature (or a potential quick and easy mullti-lines table in org?)
Date: 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 [2] 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 [3]. 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:

#+begin_src emacs-lisp
(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 "-")))
     '(:isep "\n"
             :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
                                             "-")) "  " " ")
             :csep "\n"
             :ddstart " :: "))))

Next, we need bi-directional internal representation converters from
a table to a list:

#+begin_src emacs-lisp
(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)))))
          (cdr list)))

(defun org-lisp-table-to-list (table)
  "Change TABLE lisp representation into a list lisp representation."
  (cons 'unordered
        (mapcar (lambda (row)
                  (list nil (car row)
                        (cons 'unordered
                              (mapcar (lambda (cell)
                                        (list nil cell))
                                      (cdr row)))))

Finally, we need the interactive functions for the user. Those will
also clean up output.

#+begin_src emacs-lisp
(defun org-convert-list-to-table ()
  "Transform list at point into a table."
  (if (org-at-item-p)
      (let ((parsed (org-list-parse-list t)))
        (insert (orgtbl-to-orgtbl (org-lisp-list-to-table parsed) nil) "\n")
        (goto-char (org-table-begin))
    (error "Not at a list")))

(defun org-convert-table-to-list ()
  "Transform table at point into a list."
  (if (org-at-table-p)
      (let ((pos (point))
            (table (org-table-to-lisp)))
        (delete-region (org-table-begin) (org-table-end))
        (insert (org-list-to-org (org-lisp-table-to-list table)))
        (goto-char pos)
    (error "Not at a table")))

That's it.

All of this will convert

- Row 1
  - 1.1
  - 1.2
  - 1.3
- Row 2
  - 2.1
  - 2.2
  - 2.3


| Row 1 | 1.1 | 1.2 | 1.3 |
| Row 2 | 2.1 | 2.2 | 2.3 |

and the other way.

Notes :
- 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)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]