emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nicolas <n.goaziou@gmail.com>
To: Ben <bip@maleloria.org>
Cc: emacs org-mode mailing list <emacs-orgmode@gnu.org>
Subject: Re: List-table feature (or a potential quick and easy mullti-lines table in org?)
Date: Thu, 17 Mar 2011 16:41:56 +0100	[thread overview]
Message-ID: <87hbb192kb.fsf@gmail.com> (raw)
In-Reply-To: <AANLkTi=Ev-v5YZ+C5NSTwx2JgMciunHnLB74LgA8-maM@mail.gmail.com> (Ben's message of "Thu, 17 Mar 2011 12:04:50 +0100")


Ben <bip@maleloria.org> 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 "[@%d] " 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
                               (string-match 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)



  parent reply	other threads:[~2011-03-17 15:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-17 11:04 Ben
2011-03-17 11:16 ` Jambunathan K
2011-03-17 15:41 ` Nicolas [this message]
2011-03-17 22:18   ` Ben
2011-03-18  0:31     ` Nick Dokos
2011-03-18  0:33     ` feng shu
2011-03-18  7:02     ` Nicolas
2011-03-18  9:55       ` Ben

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87hbb192kb.fsf@gmail.com \
    --to=n.goaziou@gmail.com \
    --cc=bip@maleloria.org \
    --cc=emacs-orgmode@gnu.org \
    --subject='Re: List-table feature (or a potential quick and easy mullti-lines table in org?)' \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).