emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Inserting tables programatically in elisp
@ 2022-11-05  7:41 Heime
  2022-11-05  8:49 ` Ihor Radchenko
  2022-11-05  9:02 ` Jean Louis
  0 siblings, 2 replies; 7+ messages in thread
From: Heime @ 2022-11-05  7:41 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

Have been introspecting the possibility of conveniently inserting table programatically
in elisp and encountered "table.el".

Have constructed this function, but the difficulty centers around
the challenge of inserting text in specific tests.

(defun make-table ()
  (interactive)
  (table-insert 4 5)
  (table-forward-cell)
  (table-insert-sequence "icomplt-horz" 1 1 1 'center)
  (table-forward-cell 4)
  (table-insert-sequence "icomplt-vert" 1 1 1 'center)
  (table-release))

It might be that "table.el" was designed under the assumption that the table would
be edited interactively rather than from ELisp.  The lack of info may just reflect
that nobody has thought about it making tables programatically that also goes beyond
its utilisation in "org-mode".

I understand that "Org" cooperates with its author Takaaki Ota, perhaps things could
be extended in a way that makes inserting tables programatically much easier to work
with.
 




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inserting tables programatically in elisp
  2022-11-05  7:41 Inserting tables programatically in elisp Heime
@ 2022-11-05  8:49 ` Ihor Radchenko
  2022-11-05  9:20   ` Heime
  2022-11-05  9:02 ` Jean Louis
  1 sibling, 1 reply; 7+ messages in thread
From: Ihor Radchenko @ 2022-11-05  8:49 UTC (permalink / raw)
  To: Heime; +Cc: emacs-orgmode@gnu.org

Heime <heimeborgia@protonmail.com> writes:

> Have constructed this function, but the difficulty centers around
> the challenge of inserting text in specific tests.
>
> (defun make-table ()
>   (interactive)
>   (table-insert 4 5)
>   (table-forward-cell)
>   (table-insert-sequence "icomplt-horz" 1 1 1 'center)
>   (table-forward-cell 4)
>   (table-insert-sequence "icomplt-vert" 1 1 1 'center)
>   (table-release))
>
> It might be that "table.el" was designed under the assumption that the table would
> be edited interactively rather than from ELisp.  The lack of info may just reflect
> that nobody has thought about it making tables programatically that also goes beyond
> its utilisation in "org-mode".

You can call functions interactively from lisp, if needed.

> I understand that "Org" cooperates with its author Takaaki Ota, perhaps things could
> be extended in a way that makes inserting tables programatically much easier to work
> with.

Do you refer to things on table.el side? Then, you may have more luck
asking on emacs-devel list.

As for programmatically inserting native Org tables, you can always
construct table AST via org-element-create API and then insert it via
org-element-interpret-data.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inserting tables programatically in elisp
  2022-11-05  7:41 Inserting tables programatically in elisp Heime
  2022-11-05  8:49 ` Ihor Radchenko
@ 2022-11-05  9:02 ` Jean Louis
  2022-11-05  9:26   ` Heime
  1 sibling, 1 reply; 7+ messages in thread
From: Jean Louis @ 2022-11-05  9:02 UTC (permalink / raw)
  To: Heime; +Cc: emacs-orgmode@gnu.org

* Heime <heimeborgia@protonmail.com> [2022-11-05 10:45]:
> Have been introspecting the possibility of conveniently inserting table programatically
> in elisp and encountered "table.el".
> 
> Have constructed this function, but the difficulty centers around
> the challenge of inserting text in specific tests.
> 
> (defun make-table ()
>   (interactive)
>   (table-insert 4 5)
>   (table-forward-cell)
>   (table-insert-sequence "icomplt-horz" 1 1 1 'center)
>   (table-forward-cell 4)
>   (table-insert-sequence "icomplt-vert" 1 1 1 'center)
>   (table-release))
> 
> It might be that "table.el" was designed under the assumption that the table would
> be edited interactively rather than from ELisp.  The lack of info may just reflect
> that nobody has thought about it making tables programatically that also goes beyond
> its utilisation in "org-mode".
> 
> I understand that "Org" cooperates with its author Takaaki Ota, perhaps things could
> be extended in a way that makes inserting tables programatically much easier to work
> with.

I would go programmatically not by hard coding the table walk, but by
using some structure, for example:

(setq my-table '(("ID" "Description" "Amount") ;; this must be header
		 (1 "Payment for domain" 10.50)
		 (2 "Transfer from Doe" 250)))

Then I would use some function, something like:

(my-org-table-generate my-table calculat-total 3rd-column)

Then the function would only interpolate the table with basic
 small details, which allow later automatic table alignment:

(defun rcd-org-table-cell (object)
  (cond ((numberp object) (format "| %.2f " object))
	(t (format "| %s " object))))

(rcd-org-table-cell "Hello") ⇒ "| Hello "

(defun rcd-org-table-horizontal-line ()
  "Return `|-' as horizontal-line."
  "|--\n")

then for rows:

(defun rcd-org-table-row (list)
  (with-temp-buffer
    (while list
      (insert (rcd-org-table-cell (pop list))))
    (insert "\n")
    (buffer-string)))

This means you can do something like:

(rcd-org-table-row '("ID" "Description" "Amount")) ⇒ "| ID | Description | Amount "

then you make the main function:

(defun rcd-org-table (structure)
  (let ((header (pop structure)))
    (with-temp-buffer
      (insert "\n")
      (insert (rcd-org-table-row header))
      (insert (rcd-org-table-horizontal-line))
      (while structure
	(insert (rcd-org-table-row (pop structure))))
      (insert "\n")
      (org-mode)
      (goto-char 2)
      (org-table-align)
      (buffer-substring-no-properties (point-min) (point-max)))))

And now the final result:

(rcd-org-table my-table) ⇒ "
|   ID | Description        | Amount |
|------+--------------------+--------|
| 1.00 | Payment for domain |  10.50 |
| 2.00 | Transfer from Doe  | 250.00 |

"


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inserting tables programatically in elisp
  2022-11-05  8:49 ` Ihor Radchenko
@ 2022-11-05  9:20   ` Heime
  0 siblings, 0 replies; 7+ messages in thread
From: Heime @ 2022-11-05  9:20 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode@gnu.org

------- Original Message -------
On Saturday, November 5th, 2022 at 8:49 AM, Ihor Radchenko <yantar92@posteo.net> wrote:


> Heime heimeborgia@protonmail.com writes:
> 
> > Have constructed this function, but the difficulty centers around
> > the challenge of inserting text in specific tests.
> > 
> > (defun make-table ()
> > (interactive)
> > (table-insert 4 5)
> > (table-forward-cell)
> > (table-insert-sequence "icomplt-horz" 1 1 1 'center)
> > (table-forward-cell 4)
> > (table-insert-sequence "icomplt-vert" 1 1 1 'center)
> > (table-release))
> > 
> > It might be that "table.el" was designed under the assumption that the table would
> > be edited interactively rather than from ELisp. The lack of info may just reflect
> > that nobody has thought about it making tables programatically that also goes beyond
> > its utilisation in "org-mode".
> 
> You can call functions interactively from lisp, if needed.

One could work as you say, but only if the calls do not rely on finding table regions
and so on.
 
> > I understand that "Org" cooperates with its author Takaaki Ota, perhaps things could
> > be extended in a way that makes inserting tables programatically much easier to work
> > with.
> 
> 
> Do you refer to things on table.el side? Then, you may have more luck
> asking on emacs-devel list.

Correct.  Thanks.  
 
> As for programmatically inserting native Org tables, you can always
> construct table AST via org-element-create API and then insert it via
> org-element-interpret-data.

I am discussing the use of tables beyond "org".
 



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inserting tables programatically in elisp
  2022-11-05  9:02 ` Jean Louis
@ 2022-11-05  9:26   ` Heime
  2022-11-05 10:05     ` Jean Louis
  2022-11-05 10:23     ` Jean Louis
  0 siblings, 2 replies; 7+ messages in thread
From: Heime @ 2022-11-05  9:26 UTC (permalink / raw)
  To: Jean Louis; +Cc: emacs-orgmode@gnu.org

------- Original Message -------
On Saturday, November 5th, 2022 at 9:02 AM, Jean Louis <bugs@gnu.support> wrote:

> * Heime heimeborgia@protonmail.com [2022-11-05 10:45]:
> 
> > Have been introspecting the possibility of conveniently inserting table programatically
> > in elisp and encountered "table.el".

> I would go programmatically not by hard coding the table walk, but by
> using some structure, for example:
> 
> (setq my-table '(("ID" "Description" "Amount") ;; this must be header
> (1 "Payment for domain" 10.50)
> (2 "Transfer from Doe" 250)))
> 
> Then I would use some function, something like:
> 
> (my-org-table-generate my-table calculat-total 3rd-column)
> 
> Then the function would only interpolate the table with basic
> small details, which allow later automatic table alignment:
> 
> (defun rcd-org-table-cell (object)
> (cond ((numberp object) (format "| %.2f " object))
> (t (format "| %s " object))))
> 
> (rcd-org-table-cell "Hello") ⇒ "| Hello "
> 
> (defun rcd-org-table-horizontal-line ()
> "Return `|-' as horizontal-line."
> "|--\n")
> 
> then for rows:
> 
> (defun rcd-org-table-row (list)
> (with-temp-buffer
> (while list
> (insert (rcd-org-table-cell (pop list))))
> (insert "\n")
> (buffer-string)))
> 
> This means you can do something like:
> 
> (rcd-org-table-row '("ID" "Description" "Amount")) ⇒ "| ID | Description | Amount "
> 
> then you make the main function:
> 
> (defun rcd-org-table (structure)
> (let ((header (pop structure)))
> (with-temp-buffer
> (insert "\n")
> (insert (rcd-org-table-row header))
> (insert (rcd-org-table-horizontal-line))
> (while structure
> (insert (rcd-org-table-row (pop structure))))
> (insert "\n")
> (org-mode)
> (goto-char 2)
> (org-table-align)
> (buffer-substring-no-properties (point-min) (point-max)))))
> 
> And now the final result:
> 
> (rcd-org-table my-table) ⇒ "
> | ID | Description | Amount |
> |------+--------------------+--------|
> | 1.00 | Payment for domain | 10.50 |
> | 2.00 | Transfer from Doe | 250.00 |
> 
> "

The problem I see is that the code assumes use of org-mode, whereas I am interested in
more general tables in any buffer.  At first perhaps just making a table for display rather
than for user interaction. 




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inserting tables programatically in elisp
  2022-11-05  9:26   ` Heime
@ 2022-11-05 10:05     ` Jean Louis
  2022-11-05 10:23     ` Jean Louis
  1 sibling, 0 replies; 7+ messages in thread
From: Jean Louis @ 2022-11-05 10:05 UTC (permalink / raw)
  To: Heime; +Cc: emacs-orgmode@gnu.org

* Heime <heimeborgia@protonmail.com> [2022-11-05 12:27]:
> > And now the final result:
> > 
> > (rcd-org-table my-table) ⇒ "
> > | ID | Description | Amount |
> > |------+--------------------+--------|
> > | 1.00 | Payment for domain | 10.50 |
> > | 2.00 | Transfer from Doe | 250.00 |
> > 
> > "
> 
> The problem I see is that the code assumes use of org-mode, whereas
> I am interested in more general tables in any buffer.  At first
> perhaps just making a table for display rather than for user
> interaction.

Me too, that is what I do.

The output of that function is string, not "Org". The table format is
in Org format. 

Once you have got table as string, you may use Org export functions to
get for example nicely formatted Unicode table.

Mix anything.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inserting tables programatically in elisp
  2022-11-05  9:26   ` Heime
  2022-11-05 10:05     ` Jean Louis
@ 2022-11-05 10:23     ` Jean Louis
  1 sibling, 0 replies; 7+ messages in thread
From: Jean Louis @ 2022-11-05 10:23 UTC (permalink / raw)
  To: Heime; +Cc: emacs-orgmode@gnu.org

* Heime <heimeborgia@protonmail.com> [2022-11-05 12:27]:
> The problem I see is that the code assumes use of org-mode, whereas
> I am interested in more general tables in any buffer.  At first
> perhaps just making a table for display rather than for user
> interaction.

You said you want them generated programmatically first. That is what
I also do and like.

Though I may often use PostgreSQL for data storage, and it's
Asciidoc table output function. It is built-in.

For presentation purposes I use HTML or PDF.

For presentation and purposes in Emacs, I use tabulated-list-mode and
often Org mode.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-11-05 10:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-05  7:41 Inserting tables programatically in elisp Heime
2022-11-05  8:49 ` Ihor Radchenko
2022-11-05  9:20   ` Heime
2022-11-05  9:02 ` Jean Louis
2022-11-05  9:26   ` Heime
2022-11-05 10:05     ` Jean Louis
2022-11-05 10:23     ` Jean Louis

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

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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).