From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas 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 Message-ID: <87hbb192kb.fsf@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from [140.186.70.92] (port=55868 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q0FLQ-0001dx-Gs for emacs-orgmode@gnu.org; Thu, 17 Mar 2011 11:42:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q0FKp-0001ag-0O for emacs-orgmode@gnu.org; Thu, 17 Mar 2011 11:42:02 -0400 Received: from mail-bw0-f41.google.com ([209.85.214.41]:36681) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q0FKo-0001aQ-Ob for emacs-orgmode@gnu.org; Thu, 17 Mar 2011 11:41:58 -0400 Received: by bwz17 with SMTP id 17so2882792bwz.0 for ; Thu, 17 Mar 2011 08:41:57 -0700 (PDT) In-Reply-To: (Ben's message of "Thu, 17 Mar 2011 12:04:50 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Ben Cc: emacs org-mode mailing list Hello, Ben 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 "-"))) (org-list-to-generic list '(: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 " :: ")))) #+end_src 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))))) table))) #+end_src 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." (interactive) (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)) (org-table-align)) (error "Not at a list"))) (defun org-convert-table-to-list () "Transform table at point into a list." (interactive) (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) (org-list-repair)) (error "Not at a table"))) #+end_src That's it. All of this will convert - Row 1 - 1.1 - 1.2 - 1.3 - Row 2 - 2.1 - 2.2 - 2.3 to | 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) Regards, -- Nicolas