From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.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 KJqgJF6b6WLhZQEAbAwnHQ (envelope-from ) for ; Tue, 02 Aug 2022 23:47:10 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id UEW6I16b6WJEYwEAG6o9tA (envelope-from ) for ; Tue, 02 Aug 2022 23:47:10 +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 349643D439 for ; Tue, 2 Aug 2022 23:47:10 +0200 (CEST) Received: from localhost ([::1]:60876 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIzjJ-0003s0-E3 for larch@yhetil.org; Tue, 02 Aug 2022 17:47:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44444) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzhI-0001v9-72 for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:55558) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIzhH-0006KT-Tm for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oIzhH-00039n-Py for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56898] [PATCH 06/13] read-print: Read and render vertical space. 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:03 +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.165947668412001 (code B ref 56898); Tue, 02 Aug 2022 21:45:03 +0000 Received: (at 56898) by debbugs.gnu.org; 2 Aug 2022 21:44:44 +0000 Received: from localhost ([127.0.0.1]:45287 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgx-00037O-Dj for submit@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:43 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59552) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgq-00035V-8c for 56898@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:36 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:55160) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzgl-00069m-0U; 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=kR5dSnJ5x/ASJtlbQSE9/nsQz3ihJBLIwzHylHZweAE=; b=WlhD+agcaUV+Is9h80n0 Jwpac5mTcR0W2pLOJXJ0XrwQE+AXgLh9ATJRG1spPIyDjuzvdjpfv1lqYVVBQspytNgwH1tW83vpX 8kAOyg233r8cHVtpeQiMXZI6u+RlyQNclap1L+WuOwpBVATRFFuK8wKsXv1ydu0TLLLsjOqYSwyUX 9ak0Q8jxYFZlu4gmeFuBTwR7Jntf9iQyablXVJGfpeowTvS+r35CfIX1nbD8cPMfvkxtzPWb5wuac OGYtreKaJtudi7qvnaqvUdfNNXnoRqVPdJ0lTAv4mc4GkTHFQlnc116bvj6btX7Eabqv2rs1z9hJd 5/PzQBXy3LAdNw==; 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 1oIzgk-0006W4-Kp; Tue, 02 Aug 2022 17:44:30 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Tue, 2 Aug 2022 23:44:12 +0200 Message-Id: <20220802214419.19013-6-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=1659476830; 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=kR5dSnJ5x/ASJtlbQSE9/nsQz3ihJBLIwzHylHZweAE=; b=f9w4reCcI/odXVuBXs3BquCDSufZUKbT1OS2AMaqj6VKGI2HlNPkQHbVi/SqTeoN13Zr4W uu/v5CYaTE2mR+yTWH6PgvKD020snOCi5Uh0f3ra2+OYZg+7JMh4uPOb75HMJ9b2yYzl+7 Lo5h21TisV+6/ofg9IdkU6ERueGFticdVEAB79hRtdSLZ92hC+bii//rnWHRQyV/M/iIQt YT+6e8QzClUW9fktp/2RfHgomhYWHrGlbMYBev8Ti4qKea/spe7xpQE5cqmoRYuVTEEagG /JWvqLLGPCPD0OvMwcyA2YmuHcaSjbyuBFnt8IRBSuqdQ8h30n8xf/jVLpVkhg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1659476830; a=rsa-sha256; cv=none; b=dc2md/WTqo/nBcgKAxw3+U0nORLsq0DDTGqYWC0dNWFnAJaie1uGrpmJTb65yIF4UPV5hG DqKmzd8fsTN3JxRrzQQrSoJ3s0c5mI27sTUucw4ksbRr4UluFe85biYRraph8d5xLHKnAX /06et2w2TIX5SHAhkspPpcc+FEzLybiRvY1bDYmDvxdcqvhtVoNXB05EN3R6p15c3gNctg Xlude2pV1hINhNKTd4im8itEgj+BOdqMrIe6hg/WpK7Xusm9mDv3/wXNoCllaKGrL25lzi 6oR58PNmfZpUZGg+zm4w7P1yY1lN4IjIgBLdwXr+KZ9Xl6b43RwOuTCpo/f4IA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=WlhD+agc; 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.32 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=WlhD+agc; 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: 349643D439 X-Spam-Score: -2.32 X-Migadu-Scanner: scn1.migadu.com X-TUID: gD/XCvQYOfWP * guix/read-print.scm (, vertical-space?) (vertical-space, vertical-space-height): New variables. (combine-vertical-space, canonicalize-vertical-space) (read-vertical-space): New procedures. (read-with-comments): Use it in the #\newline case. (pretty-print-with-comments): Add #:format-vertical-space and honor it. Add case for 'vertical-space?'. * guix/scripts/style.scm (format-package-definition): Pass #:format-vertical-space to 'object->string*'. * tests/read-print.scm ("read-with-comments: list with blank line") ("read-with-comments: list with multiple blank lines") ("read-with-comments: top-level blank lines") ("pretty-print-with-comments, canonicalize-vertical-space"): New tests. Add a couple of additional round-trip tests. --- guix/read-print.scm | 54 ++++++++++++++++++++++++++++-- guix/scripts/style.scm | 3 +- tests/read-print.scm | 76 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 4 deletions(-) diff --git a/guix/read-print.scm b/guix/read-print.scm index 732d0dc1f8..2b626ba281 100644 --- a/guix/read-print.scm +++ b/guix/read-print.scm @@ -30,6 +30,11 @@ (define-module (guix read-print) blank? + vertical-space + vertical-space? + vertical-space-height + canonicalize-vertical-space + comment comment? comment->string @@ -58,6 +63,26 @@ (define (define blank? (record-predicate )) +(define + (make-record-type ' '(height) + #:parent + #:extensible? #f)) + +(define vertical-space? (record-predicate )) +(define vertical-space (record-type-constructor )) +(define vertical-space-height (record-accessor 'height)) + +(define (combine-vertical-space x y) + "Return vertical space as high as the combination of X and Y." + (vertical-space (+ (vertical-space-height x) + (vertical-space-height y)))) + +(define canonicalize-vertical-space + (let ((unit (vertical-space 1))) + (lambda (space) + "Return a vertical space corresponding to a single blank line." + unit))) + (define ;; Comments. (make-record-type ' '(str margin?) @@ -80,6 +105,19 @@ (define* (comment str #:optional margin?) (&message (message "invalid comment string"))))) (string->comment str margin?)) +(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))) + ((? eof-object?) (vertical-space height)) + ((? space?) (loop height)) + (chr (unread-char chr port) (vertical-space height))))) + (define (read-with-comments port) "Like 'read', but include objects when they're encountered." ;; Note: Instead of implementing this functionality in 'read' proper, which @@ -107,7 +145,9 @@ (define (reverse/dot lst) eof) ;oops! (chr (cond ((eqv? chr #\newline) - (loop #t return)) + (if blank-line? + (read-vertical-space port) + (loop #t return))) ((char-set-contains? char-set:whitespace chr) (loop blank-line? return)) ((memv chr '(#\( #\[)) @@ -297,6 +337,7 @@ (define (canonicalize-comment c) (define* (pretty-print-with-comments port obj #:key (format-comment identity) + (format-vertical-space identity) (indent 0) (max-width 78) (long-list 5)) @@ -306,7 +347,8 @@ (define* (pretty-print-with-comments port obj Lists longer than LONG-LIST are written as one element per line. Comments are passed through FORMAT-COMMENT before being emitted; a useful value for -FORMAT-COMMENT is 'canonicalize-comment'." +FORMAT-COMMENT is 'canonicalize-comment'. Vertical space is passed through +FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'." (define (list-of-lists? head tail) ;; Return true if HEAD and TAIL denote a list of lists--e.g., a list of ;; 'let' bindings. @@ -394,6 +436,14 @@ (define (special-form? head) port))) (display (make-string indent #\space) port) indent) + ((? vertical-space? space) + (unless delimited? (newline port)) + (let loop ((i (vertical-space-height (format-vertical-space space)))) + (unless (zero? i) + (newline port) + (loop (- i 1)))) + (display (make-string indent #\space) port) + indent) (('quote lst) (unless delimited? (display " " port)) (display "'" port) diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index 5c0ecc0896..2e14bc68fd 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -316,7 +316,8 @@ (define* (format-package-definition package (object->string* exp (location-column (package-definition-location package)) - #:format-comment canonicalize-comment))))) + #:format-comment canonicalize-comment + #:format-vertical-space canonicalize-vertical-space))))) (define (package-location