From mboxrd@z Thu Jan 1 00:00:00 1970 From: Heikki Lehvaslaiho Subject: Re: manipulate org tables using emacs-lisp Date: Sun, 2 Oct 2016 10:19:28 +0300 Message-ID: References: <87k2dtm7or.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a114c1ee61a519a053ddca7a5 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:36198) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bqb3w-0008B8-JV for emacs-orgmode@gnu.org; Sun, 02 Oct 2016 03:19:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bqb3u-00022k-EH for emacs-orgmode@gnu.org; Sun, 02 Oct 2016 03:19:51 -0400 Received: from mail-wm0-x236.google.com ([2a00:1450:400c:c09::236]:37208) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bqb3u-00022b-3q for emacs-orgmode@gnu.org; Sun, 02 Oct 2016 03:19:50 -0400 Received: by mail-wm0-x236.google.com with SMTP id b201so24643809wmb.0 for ; Sun, 02 Oct 2016 00:19:49 -0700 (PDT) In-Reply-To: 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" To: Alan Schmitt Cc: emacs-orgmode@gnu.org, Thorsten Jolitz --001a114c1ee61a519a053ddca7a5 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Alan, Try this function. You can test if the table was found before manipulating it. Table functions typically work only inside tables, but the #+NAME line is part of it although it is before the table itself (as defined by function org-table-begin). (defun my/org-table-find-by-name (str) "Move point to the first table of the buffer with STR in the name. The point does not move if the table is not found. Returns the new position if successful, nil otherwise. " (interactive "sTable name substring: ") (let ((re (concat "#\\+NAME:.+" str)) (loc-table nil)) (setq loc-table (save-excursion (goto-char (point-min)) (re-search-forward re nil t))) (if (number-or-marker-p loc-table) (goto-char loc-table)))) -Heikki Hi Thorsten, > > On 2016-09-30 22:52, Thorsten Jolitz writes: > > >> Are there functions for manipulating org-tables using emacs-lisp? More > >> precisely, I would like to refer to a table by its name, read some cel= ls > >> (either by position or by matching some given text with some text in t= he > >> first row/column), and write in some cells. > > > > ,----[ C-h f org-table-to-lisp RET ] > > | org-table-to-lisp is an autoloaded compiled Lisp function in > > | =E2=80=98../org-mode/lisp/org-table.el=E2=80=99. > > | > > | (org-table-to-lisp &optional TXT) > > | > > | Convert the table at point to a Lisp structure. > > | The structure will be a list. Each item is either the symbol =E2=80= =98hline=E2=80=99 > > | for a horizontal separator line, or a list of field values as strings= . > > | The table is taken from the parameter TXT, or from the buffer at poin= t. > > | > > | [back] > > `---- > > > > returns the table as a nested list you can map with lots of Elisp > > functions (like mapcar). > > > > Ex.: > > > > | my | tab | > > | 1 | 2 | > > > > =3D> > > > > (("my" "tab") ("1" "2")) > > This is very useful, thank you. But how do I go to a named table? I > found org-babel-goto-named-block, but nothing for tables. > > Thanks again, > > Alan > > -- > OpenPGP Key ID : 040D0A3B4ED2E5C7 > Monthly Athmospheric CO=E2=82=82, Mauna Loa Obs. 2016-08: 402.25, 2015-08= : 398.93 > --001a114c1ee61a519a053ddca7a5 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi Alan,

Try this function. = You can test if the table was found before manipulating it. Table functions= typically work only inside tables, but the #+NAME line is part of it altho= ugh it is before the table itself (as defined by function org-table-begin).=


=C2=A0 (defun my/org-table-find-by= -name (str)
=C2=A0 =C2=A0 "Move point to the first table of = the buffer with STR in the name.

The point does no= t move if the table is not found.
Returns the new position if suc= cessful, nil otherwise.
"
=C2=A0 =C2=A0 (interacti= ve "sTable name substring: ")
=C2=A0 =C2=A0 (let ((re (= concat "#\\+NAME:.+" str))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 (loc-table nil))
=C2=A0 =C2=A0 =C2=A0 (setq loc-table (sav= e-excursion
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (goto-char (point-min))
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 (re-search-forward re nil t)))
=C2=A0 =C2=A0 =C2=A0 (if (numb= er-or-marker-p loc-table)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (got= o-char loc-table))))

=C2=A0 -Heikki

=
Hi Thor= sten,

On 2016-09-30 22:52, Thorsten Jolitz <tjolitz@gmail.com> writes:

>> Are there functions for manipulating org-tables using emacs-lisp? = More
>> precisely, I would like to refer to a table by its name, read some= cells
>> (either by position or by matching some given text with some text = in the
>> first row/column), and write in some cells.
>
> ,----[ C-h f org-table-to-lisp RET ]
> | org-table-to-lisp is an autoloaded compiled Lisp function in
> | =E2=80=98../org-mode/lisp/org-table.el=E2=80=99.
> |
> | (org-table-to-lisp &optional TXT)
> |
> | Convert the table at point to a Lisp structure.
> | The structure will be a list.=C2=A0 Each item is either the symbol = =E2=80=98hline=E2=80=99
> | for a horizontal separator line, or a list of field values as string= s.
> | The table is taken from the parameter TXT, or from the buffer at poi= nt.
> |
> | [back]
> `----
>
> returns the table as a nested list you can map with lots of Elisp
> functions (like mapcar).
>
> Ex.:
>
> | my | tab |
> |=C2=A0 1 |=C2=A0 =C2=A02 |
>
> =3D>
>
> (("my" "tab") ("1" "2"))

This is very useful, thank you. But how do I go to a named table? I<= br> found org-babel-goto-named-block, but nothing for tables.

Thanks again,

Alan

--
OpenPGP Key ID : 040D0A3B4ED2E5C7
Monthly Athmospheric CO=E2=82=82, Mauna Loa Obs. 2016-08: 402.25, 2015-08: = 398.93

--001a114c1ee61a519a053ddca7a5--