From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id oMzVNW0nZmMVGwEAbAwnHQ (envelope-from ) for ; Sat, 05 Nov 2022 10:05:49 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id qKHVNW0nZmNQPwEA9RJhRA (envelope-from ) for ; Sat, 05 Nov 2022 10:05:49 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 6A5E4C895 for ; Sat, 5 Nov 2022 10:05:49 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1orF6c-0005iu-NL; Sat, 05 Nov 2022 05:04:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1orF6Z-0005iS-IS for emacs-orgmode@gnu.org; Sat, 05 Nov 2022 05:04:45 -0400 Received: from stw1.rcdrun.com ([217.170.207.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1orF6X-0007Eb-Gv for emacs-orgmode@gnu.org; Sat, 05 Nov 2022 05:04:43 -0400 Received: from localhost ([::ffff:197.239.4.26]) (AUTH: PLAIN admin, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 0000000000081FA6.0000000063662706.00003BC0; Sat, 05 Nov 2022 02:04:05 -0700 Date: Sat, 5 Nov 2022 12:02:13 +0300 From: Jean Louis To: Heime Cc: "emacs-orgmode@gnu.org" Subject: Re: Inserting tables programatically in elisp Message-ID: Mail-Followup-To: Heime , "emacs-orgmode@gnu.org" References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/2.2.7+37 (a90f69b) (2022-09-02) Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_SBL=0.141, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Emacs-orgmode" Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1667639149; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=KLqWMRcqFgOzWLU9N8fPvNjzqmljGA+uBMDdv0l0DGQ=; b=nagQ70iEzGxNMV3Jumu1Biy3ApYxM5o2rKrDBny9kljvokOu47loU2EnJFi5wNubE7Whbq uZewS968ti8zDHoA2Lf5K3Ah/j1cJSHYMQoTrsAA2njnz3wfBCytd4lf+XYH5Kiz1dcJvc rKvCn/tmp5LoxOWpuMyRHGbZTrRgor/J5ankWzI71ecT+7qWarjKEYQPaysXoUr72dRKpy oZa1juFnVLliENVcFU4nbbX5yJzoJPPtQR6stbTfSV9bV9okW3bJwFSfRNVI6n6qfpAduQ 1ZYGdxH67mMTr5PwT3t6erykz7SGpLzt0W61xG80ka6mIXlyaG5UXgzvqb/Q0w== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1667639149; a=rsa-sha256; cv=none; b=KP9nh7vbMJGF0c+/HQnb6XtAFky8AzToF0FW9+32g4kvLPF2c2AtjQQK6GxVyYvRXs7X5L jwEhLh1dvbC8wKKTAMxQvzN2Bb+Fq0a9Nihx0BdSF/G+yl4HKv2NMq+d6w/i9V9ojf0rKB 5dSs/uTnUOpV+n21bfhHABkk1ZV6WQlMxthcO7zPGWEL/dLY2OLC2/jJvFEOT93AyjUhUp r5BGBjxrLaHUTy3aNWwFsayPA6MRHqlxslwiyOQ8Td96+maFZBHYB/ld2UhllTMnuRAX0P qYMgzycb1huxw+EMdoRnocS+7iaYN//Wmvgrb/tipNPFHL6jm3Lv8Rh8gAq3Kg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -0.00 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 6A5E4C895 X-Spam-Score: -0.00 X-Migadu-Scanner: scn1.migadu.com X-TUID: PAEWe8HAN06C * Heime [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/