From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nick Dokos Subject: Re: Bug: Revert orgtbl-create-or-convert-from-region [6.33x] Date: Mon, 05 Aug 2013 10:19:57 -0400 Message-ID: <871u68kynm.fsf@gmail.com> References: <201308050950.16717.daniel.hornung@ds.mpg.de> <87zjsw33aw.fsf@gmail.com> <201308051149.55650.daniel.hornung@ds.mpg.de> <8738qo8e1f.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:35953) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V6Le0-0000Mw-VZ for emacs-orgmode@gnu.org; Mon, 05 Aug 2013 10:20:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V6Ldt-00056S-Vv for emacs-orgmode@gnu.org; Mon, 05 Aug 2013 10:20:20 -0400 Received: from plane.gmane.org ([80.91.229.3]:41416) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V6Ldt-00055Z-Pk for emacs-orgmode@gnu.org; Mon, 05 Aug 2013 10:20:13 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1V6Lds-0003MA-30 for emacs-orgmode@gnu.org; Mon, 05 Aug 2013 16:20:12 +0200 Received: from pool-108-7-96-134.bstnma.fios.verizon.net ([108.7.96.134]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 05 Aug 2013 16:20:12 +0200 Received: from ndokos by pool-108-7-96-134.bstnma.fios.verizon.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 05 Aug 2013 16:20:12 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Thorsten Jolitz writes: > Daniel Hornung writes: > >> On Monday, August 05, 2013 11:16:55 Thorsten Jolitz wrote: >>> Daniel Hornung writes: >>> > I think one great way to increase the usefulness of org-mode would be a >>> > function which turns a table into a csv or tsv block of text again. >>> > >>> > I assume that the functionality exists already in org-table-export, I >>> > would just wish for this to be exposed as a function which converts the >>> > table in place instead of writing it into a new file (org-table-export >>> > does not allow overwriting the current file). >>> > >>> > This would finally give a comfortable way to edit tsv or csv tables >>> > without hassles in emacs. >>> >>> Would you need something more sophisticated than this? >>> >>> #+TBLNAME: tbl >>> >>> | header 1 | header 2 | header 3 | >>> | >>> |----------+----------+----------| >>> | >>> | label1 | 3 | 99 | >>> | label2 | 2 | 66 | >>> | label3 | 7 | 231 | >>> >>> #+TBLFM: $3=$2*33 >>> >>> #+HEADER: :var table=tbl :hlines no >>> #+HEADER: :results list verbatim >>> #+begin_src emacs-lisp >>> (defun tbl2csv (table-as-lisp) >>> (mapconcat >>> (lambda (row) >>> (mapconcat >>> (lambda (cell) >>> (format "%s" cell)) >>> row ",")) >>> table-as-lisp ",")) >>> >>> (tbl2csv table) >>> #+end_src >>> >>> #+results: >>> : "header 1,header 2,header 3,label1,3,99,label2,2,66,label3,7,231" >>> >>> -- >>> cheers, >>> Thorsten >> >> The idea looks OK, although I did not get it to run with C-x C-e (copied the >> content into a new buffer, entered org-mode and executed the elisp >> code). > > Mmh...it works here ... the table looks a bit distorted in your post ... > >> For more specific handling of e.g. strings, the code used in >> http://orgmode.org/w/?p=org-mode.git;a=blob;f=lisp/org-table.el;hb=HEAD#l601 >> looks more like it could be used already, though. Plus, it allows to >> specify the column and row separators (e.g. "\t" and "\n"). > > In tbl2csv, one could replace the two hardcoded "," with function args > 'col-separator' and 'row-separator'. > > Or use something like this: > > #+begin_src emacs-lisp > (defun tj/insert-exported-table (&optional insertion-point file) > (let ((tmp-file (or file (make-temp-file "foo"))) > (insert-point (or insertion-point (1+ (org-table-end))))) > (org-table-export tmp-file "orgtbl-to-csv") > (goto-char insert-point) > (insert-file-contents tmp-file))) > #+end_src > > #+results: > : tj/insert-exported-table > > then do 'M-: (tj/insert-exported-table)' on this table: > > | header 1 | header 2 | header 3 | > |----------+----------+----------| > | label1 | 3 | 99 | > | label2 | 2 | 66 | > | label3 | 7 | 231 | > > header 1,header 2,header 3 > label1,3,99 > label2,2,66 > label3,7,231 There are orgtbl-to-tsv and orgtbl-to-csv (and orgtbl-to-generic which the first two call) functions in org: it should be possible to use them and avoid reinventing wheels. -- Nick