From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.help Subject: Re: Elisp Reference Manual on inserting tables Date: Sun, 06 Nov 2022 21:48:40 +0100 Message-ID: <87r0yfbyhj.fsf@web.de> References: <87iljuishm.fsf@ucl.ac.uk> <87v8nth846.fsf@ucl.ac.uk> <6Z6_K1-F06LaCcWUNoIbXE2j8T1KXL2FCaQ-hNfVFK6TfqUFTZPVJIdJR2pn_MlmjA-eSlg1Hzcl-t2WhXmzKztKLhvVn2iY_PP9JWaCVe4=@protonmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19748"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:7j3i1A2qnrbFdDsd5yMwM4mhqcA= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun Nov 06 21:49:42 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ormaL-0004xI-NV for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 06 Nov 2022 21:49:41 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ormZZ-0005Rl-QJ; Sun, 06 Nov 2022 15:48:53 -0500 Original-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 1ormZY-0005RO-9P for help-gnu-emacs@gnu.org; Sun, 06 Nov 2022 15:48:52 -0500 Original-Received: from ciao.gmane.io ([116.202.254.214]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ormZW-0004sE-Ol for help-gnu-emacs@gnu.org; Sun, 06 Nov 2022 15:48:51 -0500 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1ormZV-0003ui-Fr for help-gnu-emacs@gnu.org; Sun, 06 Nov 2022 21:48:49 +0100 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=geh-help-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:140745 Archived-At: --=-=-= Content-Type: text/plain Heime writes: > As I was talking about making tables from elisp, I though there should > be something in the Elisp Manual as well. Or perhaps in the Elisp > Tutorial? Emacs doesn't have a simple function to format simple text tables built-in (so there is nothing to document). I wrote my own one some time ago, maybe it is of help: --=-=-= Content-Type: application/emacs-lisp Content-Disposition: inline; filename=tbl.el Content-Transfer-Encoding: quoted-printable ;; -*- lexical-binding: t -*- (defun my-print-table (table &optional col-formatters alignments col-separa= tor header-line) "Return formatted TABLE, a matrix of strings, as a string. TABLE is a list of lists of strings which is interpreted as a list of table lines. COL-FORMATTERS is a list of functions (or nil entries) to format the according column entries. These functions should all accept one string as argument, an table entry to print. ALIGNMENTS is a list of elements of the form [left|right|centered|nil]. COL-FORMATTERS and ALIGNMENTS are allowed to be shorter than the number of entries in TABLE. COL-SEPARATOR is a string to use as a column separator, like \"|\". Default is \" \". It can alternatively be a list of number of columns minus 1 separators to use. HEADER-LINE is an additional table line to use as header line. Should have as many elements as the table has columns. Example: (insert (my-print-table (let ((numbers (number-sequence 1 15))) (mapcar (lambda (n) (list n (expt n 2) (sqrt n))) numbers)) (list #\\=3D'number-to-string #\\=3D'number-to-string (apply-partially #\\=3D'format \"%.5f\")) (list \\=3D'right \\=3D'right \\=3D'center) \" | \" (list \"n\" \"n^2\" \"sqrt(n)\")))" (let* ((nb-cols (length (car table))) (fill-up-spec (lambda (spec-list) (let ((d (- nb-cols (length spec-list)))) (if (< 0 d) (append spec-list (make-list d nil)) (copy-sequence spec-list))))) (col-formatters (funcall fill-up-spec col-formatters)) (alignments (funcall fill-up-spec alignments)) (table (mapcar (lambda (line) (cl-mapcar (lambda (entry formatter) (if formatter (funcall formatter entry) entry)) line col-formatters)) table)) (max-col-lengths (mapcar (lambda (i) (apply #'max (mapcar #'length (mapcar (apply-partially #'nth i) (if header-line (cons header-line table) table))))) (number-sequence 0 (1- nb-cols))))) (cl-flet ((format-line (line) (let ((i 0)) (apply #'concat (cl-mapcar (lambda (entry alignment) (prog1 (concat (let ((padding (- (nth i max-col-length= s) (length entry)))) (pcase alignment ('right (concat (make-string paddin= g ?\ ) entry)) ((and (or 'center 'centered) (let left (/ padding 2))) (concat (make-string left ?\ ) entry (make-string (- padding le= ft) ?\ ))) (_ (concat entry (make-string paddi= ng ?\ ))))) (unless (=3D i (1- nb-cols)) (pcase col-separator ((and (pred consp) (pred (nth i)) s= ) s) ((pred identity) col-separator) (_ " ")))) (cl-incf i))) line alignments))))) (concat (and-let* ((header-line) (header-line (format-line header-line))) (concat header-line "\n" (make-string (length header-line) = ?-) "\n")) (mapconcat #'format-line table "\n"))))) --=-=-= Content-Type: text/plain Michael. --=-=-=--