From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id YPW/DX5D5V6KJwAA0tVLHw (envelope-from ) for ; Sat, 13 Jun 2020 21:22:06 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id uBHDCX5D5V7QTAAA1q6Kng (envelope-from ) for ; Sat, 13 Jun 2020 21:22:06 +0000 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 1843C940669 for ; Sat, 13 Jun 2020 21:22:05 +0000 (UTC) Received: from localhost ([::1]:44324 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jkDbH-0005AY-1K for larch@yhetil.org; Sat, 13 Jun 2020 17:22:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47722) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jkDat-0005AM-0Z for emacs-orgmode@gnu.org; Sat, 13 Jun 2020 17:21:39 -0400 Received: from devianza.investici.org ([198.167.222.108]:60673) by eggs.gnu.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jkDap-0007eT-8i for emacs-orgmode@gnu.org; Sat, 13 Jun 2020 17:21:38 -0400 Received: from mx2.investici.org (unknown [127.0.0.1]) by devianza.investici.org (Postfix) with ESMTP id E404EE05BB; Sat, 13 Jun 2020 21:21:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anche.no; s=stigmate; t=1592083282; bh=tzebQaAeJP5dKozzNmpgen5aCt+nOTGvirgEa4uiaFQ=; h=Subject:From:To:References:Date:In-Reply-To:From; b=I8vLqWRp8uSc0jBPMA6qtf/acT6KcSCJV+AU6rjvah7qD9CnbgjKxhBNeo94JWg8V 0RYMYvkZv9anUEOf/oBSGD7mv3H1yJBlCIeG7jEsMsVevPjEovh8sg3KsKGIK4/QGK Lq8vWcUGnXnm5Y46q6koZtzZARbXtkzxuXjdxR4A= Received: from [198.167.222.108] (mx2.investici.org [198.167.222.108]) (Authenticated sender: mariotomo@inventati.org) by localhost (Postfix) with ESMTPSA id CBCBFE0565; Sat, 13 Jun 2020 21:21:21 +0000 (UTC) Subject: Re: [PATCH] allow for multiline headers From: Mario Frasca To: emacs-orgmode@gnu.org References: <87wo4bhpkx.fsf@nicolasgoaziou.fr> Message-ID: <51f61edb-0cc9-4cb2-cb8f-8e73170633ca@anche.no> Date: Sat, 13 Jun 2020 16:20:58 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------B0BC3A8B07172C0584C7C9C7" Content-Language: en-US Received-SPF: pass client-ip=198.167.222.108; envelope-from=mario@anche.no; helo=devianza.investici.org X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/13 16:20:29 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=anche.no header.s=stigmate header.b=I8vLqWRp; dmarc=none; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: -1.21 X-TUID: WqE7Ez57kGd8 This is a multi-part message in MIME format. --------------B0BC3A8B07172C0584C7C9C7 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit what about these two groups of tests, and the header collapse function? --------------B0BC3A8B07172C0584C7C9C7 Content-Type: text/x-patch; charset=UTF-8; name="0001-lisp-org-table.el-Allow-collapsing-header-into-singl.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-lisp-org-table.el-Allow-collapsing-header-into-singl.pa"; filename*1="tch" >From ceb21024159a75dbdb9fef32eebe1fc8c7076d2f Mon Sep 17 00:00:00 2001 From: mfrasca Date: Fri, 12 Jun 2020 11:42:34 -0500 Subject: [PATCH] lisp/org-table.el: Allow collapsing header into single line * lisp/org-table.el (org-table-collapse-header): new function that collapses multiple header lines into one list. * lisp/org-plot.el (org-plot/gnuplot): use org-table-collapse-header and trust there will be no more leading `hline' symbols in lisp table. * testing/lisp/test-org-table.el (test-org-table/to-lisp): adding tests to already existing to-lisp function. (test-org-table/collapse-header): adding tests to new collapse-header function. --- lisp/org-plot.el | 6 ++-- lisp/org-table.el | 27 ++++++++++++++++ testing/lisp/test-org-table.el | 58 ++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/lisp/org-plot.el b/lisp/org-plot.el index a23195d2a..662d38e54 100644 --- a/lisp/org-plot.el +++ b/lisp/org-plot.el @@ -289,11 +289,9 @@ line directly before or after the table." (setf params (plist-put params (car pair) (cdr pair))))) ;; collect table and table information (let* ((data-file (make-temp-file "org-plot")) - (table (org-table-to-lisp)) - (num-cols (length (if (eq (nth 0 table) 'hline) (nth 1 table) - (nth 0 table))))) + (table (org-table-collapse-header (org-table-to-lisp))) + (num-cols (length (nth 0 table)))) (run-with-idle-timer 0.1 nil #'delete-file data-file) - (while (eq 'hline (car table)) (setf table (cdr table))) (when (eq (cadr table) 'hline) (setf params (plist-put params :labels (nth 0 table))) ; headers to labels diff --git a/lisp/org-table.el b/lisp/org-table.el index 6462b99c4..c40ad5bea 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -5458,6 +5458,33 @@ The table is taken from the parameter TXT, or from the buffer at point." (forward-line)) (nreverse table))))) +(defun org-table-collapse-header (table &optional glue max-header-lines) + "Collapse the lines before 'hline into a single header. + +The given TABLE is a list of lists as returned by `org-table-to-lisp'. +The leading lines before the first `hline' symbol are considered +forming the table header. This function collapses all leading header +lines into a single header line, followed by the `hline' symbol, and +the rest of the TABLE. Header cells are GLUEd together with a space, +or the given character." + (setq glue (or glue " ")) + (setq max-header-lines (or max-header-lines 4)) + (while (equal 'hline (car table)) + (setq table (cdr table))) + (let* ((trailer table) + (header-lines (cl-loop for line in table + until (equal line 'hline) + collect line + do (setq trailer (cdr trailer))))) + (if (and trailer (<= (length header-lines) max-header-lines)) + (cons (apply #'cl-mapcar + #'(lambda (&rest x) + (org-trim + (mapconcat #'identity x glue))) + header-lines) + trailer) + table))) + (defun orgtbl-send-table (&optional maybe) "Send a transformed version of table at point to the receiver position. With argument MAYBE, fail quietly if no transformation is defined diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index 64a1b4b16..5d54f4999 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -1304,6 +1304,64 @@ See also `test-org-table/copy-field'." (should (string= got expect))))) +;;; the initial to lisp converter + +(ert-deftest test-org-table/to-lisp () + "Test `orgtbl-to-lisp' specifications." + ;; 2x2 no header + (should + (equal '(("a" "b") ("c" "d")) + (org-table-to-lisp "|a|b|\n|c|d|"))) + ;; 2x2 with 1-line header + (should + (equal '(("a" "b") hline ("c" "d")) + (org-table-to-lisp "|a|b|\n|-\n|c|d|"))) + ;; 2x4 with 2-line header + (should + (equal '(("a" "b") ("A" "B") hline ("c" "d") ("aa" "bb")) + (org-table-to-lisp "|a|b|\n|A|B|\n|-\n|c|d|\n|aa|bb|"))) + ;; leading hlines do not get stripped + (should + (equal '(hline ("a" "b") hline ("c" "d")) + (org-table-to-lisp "|-\n|a|b|\n|-\n|c|d|"))) + (should + (equal '(hline ("a" "b") ("c" "d")) + (org-table-to-lisp "|-\n|a|b|\n|c|d|"))) + (should + (equal '(hline hline hline hline ("a" "b") ("c" "d")) + (org-table-to-lisp "|-\n|-\n|-\n|-\n|a|b|\n|c|d|")))) + +(ert-deftest test-org-table/collapse-header () + "Test `orgtbl-to-lisp' specifications." + ;; 2x2 no header - no collapsing + (should + (equal '(("a" "b") ("c" "d")) + (org-table-collapse-header (org-table-to-lisp "|a|b|\n|c|d|")))) + ;; 2x2 with 1-line header - no collapsing + (should + (equal '(("a" "b") hline ("c" "d")) + (org-table-collapse-header (org-table-to-lisp "|a|b|\n|-\n|c|d|")))) + ;; 2x4 with 2-line header - collapsed + (should + (equal '(("a A" "b B") hline ("c" "d") ("aa" "bb")) + (org-table-collapse-header (org-table-to-lisp "|a|b|\n|A|B|\n|-\n|c|d|\n|aa|bb|")))) + ;; 2x4 with 2-line header, custom glue - collapsed + (should + (equal '(("a.A" "b.B") hline ("c" "d") ("aa" "bb")) + (org-table-collapse-header (org-table-to-lisp "|a|b|\n|A|B|\n|-\n|c|d|\n|aa|bb|") "."))) + ;; 2x4 with 2-line header, threshold 1 - not collapsed + (should + (equal '(("a" "b") ("A" "B") hline ("c" "d") ("aa" "bb")) + (org-table-collapse-header (org-table-to-lisp "|a|b|\n|A|B|\n|-\n|c|d|\n|aa|bb|") nil 1))) + ;; 2x4 with 2-line header, threshold 2 - collapsed + (should + (equal '(("a A" "b B") hline ("c" "d") ("aa" "bb")) + (org-table-collapse-header (org-table-to-lisp "|a|b|\n|A|B|\n|-\n|c|d|\n|aa|bb|") nil 2))) + ;; 2x8 with 6-line header, default threshold 5 - not collapsed + (should + (equal '(("a" "b") ("A" "B") ("a" "b") ("A" "B") ("a" "b") ("A" "B") hline ("c" "d") ("aa" "bb")) + (org-table-collapse-header (org-table-to-lisp "|a|b|\n|A|B|\n|a|b|\n|A|B|\n|a|b|\n|A|B|\n|-\n|c|d|\n|aa|bb|"))))) + ;;; Radio Tables (ert-deftest test-org-table/to-generic () -- 2.20.1 --------------B0BC3A8B07172C0584C7C9C7--