From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id yLyIJEyb6WLr/wAAbAwnHQ (envelope-from ) for ; Tue, 02 Aug 2022 23:46:52 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id XCxiJEyb6WJIYQEAauVa8A (envelope-from ) for ; Tue, 02 Aug 2022 23:46:52 +0200 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 105593D044 for ; Tue, 2 Aug 2022 23:46:51 +0200 (CEST) Received: from localhost ([::1]:59374 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIzj1-0002lS-1b for larch@yhetil.org; Tue, 02 Aug 2022 17:46:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44446) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzhI-0001vs-Kt for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:55559) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIzhI-0006NS-BQ for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:04 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oIzhI-00039v-7g for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:04 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56898] [PATCH 07/13] read-print: Recognize page breaks. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 02 Aug 2022 21:45:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56898 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 56898@debbugs.gnu.org Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= Received: via spool by 56898-submit@debbugs.gnu.org id=B56898.165947668412009 (code B ref 56898); Tue, 02 Aug 2022 21:45:04 +0000 Received: (at 56898) by debbugs.gnu.org; 2 Aug 2022 21:44:44 +0000 Received: from localhost ([127.0.0.1]:45289 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgy-00037V-1l for submit@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:44 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59556) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgq-00035a-Q9 for 56898@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:37 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:55162) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzgl-00069s-In; Tue, 02 Aug 2022 17:44:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=TZsiGBUECifF5vB9KehYhuZHWx6CeIIJlKp8LFvyaOk=; b=Kakq489civk38XzGEuNX RtvH040qHkFs8gNv+kRWNnKrlN1pCKRgZARqLC7K3Jy4ORXMen4vOzUz6UIn+zHXloOb6Vuu995DZ XALjDDiE69R9JArs52E1URIVbeDQTFNHIuYfjH3HJCgW+jviRTundYgCRkuqSmhSUxcdooSTFB/iD 2wHsL4u0GERdj+GNdendn5tVEWgKyPGgwR6Bq0r0t9V4djOIkmHsbiDB+WKqscHJbuDmUVscx4YTO pAH5r9zCqlSV/UzYfjMrZKPfb9LtxDosuQhLgPxdmDyNZmQbx2iHQNrlmGjrPi9rGYJUaeHJgNIVA gWEAclC6zXujww==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:52235 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIzgl-0006W4-6q; Tue, 02 Aug 2022 17:44:31 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Tue, 2 Aug 2022 23:44:13 +0200 Message-Id: <20220802214419.19013-7-ludo@gnu.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220802214419.19013-1-ludo@gnu.org> References: <20220802214419.19013-1-ludo@gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1659476812; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=vY1pRSsrh3926SOSRRwgw7Tau4hnksi7tXTgRMMh36Q=; b=LC8co6QRrRhFaLhHj4mdpmUudqAACaLaYyKXTVcQAenzFwzCxlN2jV+8pgnSfg9QB+R3/d 9t/oNEd27YzlWGbdHVZfAjaAfzmI/gcxCOD3mnbdLGZhaYRykg6rUSs2GrcMpECuYKHCkM gTXm/em/QRA/fK5aFDFSPnOCutoKPBQ1YnZEA8t09BxaTe/pomN9tCTOrZEHHUD54eE4pr OgEvuQCp7UTYHCeIJTYnGg4z/JZ/6lTyu3mEvJxw67mzDLRnmb4RGcVJ71sZ8iXHWC1qUK Nw0g753mjoWZFOlS1oQqOhA1XlBbBzCNvTVewH8m2lBNR39Eq94x4otAfpVhWQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1659476812; a=rsa-sha256; cv=none; b=SPUx9p5+zn61r6ufVYrHYpqHKGBoCZYAe3tsjQ0OlNRouHOmZleLEgZL9QB9rM6SI4RzCx GJKycREkRbPi7CaxNYQXuu9bCRs8l8Lxo6ysHXNYfsuT2meEH1fBfvMO9OPT8iCyVPjlgq CZ5EzP/2uiZvlzXjRwJzu7fY6n39YWcbiHqO2UzX0vT1YmPzrlOw14RoWd8IWQ1kXNO6KV Qao728TdkZ9CYUH+YDMZB5r0uo16vRuOw80qIiyMsVTTcemQxGKJUnbn45V/SC+6olGLzM Np6Mf00tyt7+ISfHr2RtlcTZulQb7MKZ/ALYoplj6v192BPhZz2RFdVCtTPIKw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gnu.org header.s=fencepost-gnu-org header.b=Kakq489c; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -2.82 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gnu.org header.s=fencepost-gnu-org header.b=Kakq489c; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 105593D044 X-Spam-Score: -2.82 X-Migadu-Scanner: scn0.migadu.com X-TUID: 72lxUdFesv8q * guix/read-print.scm (, page-break?, page-break) (char-set:whitespace-sans-page-break): New variables. (space?): New procedure. (read-vertical-space): Use it. (read-until-end-of-line): New procedure. (read-with-comments): Add #\page case. (pretty-print-with-comments): Add 'page-break?' case. * tests/read-print.scm ("read-with-comments: top-level page break"): New test. Add round-trip test with page break within an sexp. --- guix/read-print.scm | 46 +++++++++++++++++++++++++++++++++++++++++--- tests/read-print.scm | 22 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/guix/read-print.scm b/guix/read-print.scm index 2b626ba281..33ed6e3dbe 100644 --- a/guix/read-print.scm +++ b/guix/read-print.scm @@ -35,6 +35,9 @@ (define-module (guix read-print) vertical-space-height canonicalize-vertical-space + page-break + page-break? + comment comment? comment->string @@ -83,6 +86,18 @@ (define canonicalize-vertical-space "Return a vertical space corresponding to a single blank line." unit))) +(define + (make-record-type ' '() + #:parent + #:extensible? #f)) + +(define page-break? (record-predicate )) +(define page-break + (let ((break ((record-type-constructor )))) + (lambda () + break))) + + (define ;; Comments. (make-record-type ' '(str margin?) @@ -105,12 +120,17 @@ (define* (comment str #:optional margin?) (&message (message "invalid comment string"))))) (string->comment str margin?)) +(define char-set:whitespace-sans-page-break + ;; White space, excluding #\page. + (char-set-difference char-set:whitespace (char-set #\page))) + +(define (space? chr) + "Return true if CHR is white space, except for page breaks." + (char-set-contains? char-set:whitespace-sans-page-break chr)) + (define (read-vertical-space port) "Read from PORT until a non-vertical-space character is met, and return a single record." - (define (space? chr) - (char-set-contains? char-set:whitespace chr)) - (let loop ((height 1)) (match (read-char port) (#\newline (loop (+ 1 height))) @@ -118,6 +138,15 @@ (define (space? chr) ((? space?) (loop height)) (chr (unread-char chr port) (vertical-space height))))) +(define (read-until-end-of-line port) + "Read white space from PORT until the end of line, included." + (let loop () + (match (read-char port) + (#\newline #t) + ((? eof-object?) #t) + ((? space?) (loop)) + (chr (unread-char chr port))))) + (define (read-with-comments port) "Like 'read', but include objects when they're encountered." ;; Note: Instead of implementing this functionality in 'read' proper, which @@ -148,6 +177,11 @@ (define (reverse/dot lst) (if blank-line? (read-vertical-space port) (loop #t return))) + ((eqv? chr #\page) + ;; Assume that a page break is on a line of its own and read + ;; subsequent white space and newline. + (read-until-end-of-line port) + (page-break)) ((char-set-contains? char-set:whitespace chr) (loop blank-line? return)) ((memv chr '(#\( #\[)) @@ -444,6 +478,12 @@ (define (special-form? head) (loop (- i 1)))) (display (make-string indent #\space) port) indent) + ((? page-break?) + (unless delimited? (newline port)) + (display #\page port) + (newline port) + (display (make-string indent #\space) port) + indent) (('quote lst) (unless delimited? (display " " port)) (display "'" port) diff --git a/tests/read-print.scm b/tests/read-print.scm index f915b7e2d2..70be7754f8 100644 --- a/tests/read-print.scm +++ b/tests/read-print.scm @@ -70,6 +70,21 @@ (define-syntax-rule (test-pretty-print str args ...) (read-with-comments port) (read-with-comments port))))) +(test-equal "read-with-comments: top-level page break" + (list (comment ";; Begin.\n") (vertical-space 1) + (page-break) + (comment ";; End.\n")) + (call-with-input-string "\ +;; Begin. + + +;; End.\n" + (lambda (port) + (list (read-with-comments port) + (read-with-comments port) + (read-with-comments port) + (read-with-comments port))))) + (test-pretty-print "(list 1 2 3 4)") (test-pretty-print "((a . 1) (b . 2))") (test-pretty-print "(a b c . boom)") @@ -229,6 +244,13 @@ (define-syntax-rule (test-pretty-print str args ...) ;; Comment after blank line. two)") +(test-pretty-print "\ +(begin + break + + ;; page break above + end)") + (test-equal "pretty-print-with-comments, canonicalize-comment" "\ (list abc -- 2.37.1