From patchwork Sun May 15 04:36:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 39270 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id F183C27BBEB; Sun, 15 May 2022 05:47:40 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 7599C27BBE9 for ; Sun, 15 May 2022 05:47:40 +0100 (BST) Received: from localhost ([::1]:47092 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nq6AN-00029d-Kr for patchwork@mira.cbaines.net; Sun, 15 May 2022 00:47:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41920) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nq69n-00028K-Kr for guix-patches@gnu.org; Sun, 15 May 2022 00:47:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:53967) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nq69n-0003b3-Bu for guix-patches@gnu.org; Sun, 15 May 2022 00:47:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nq69n-0002tP-B6 for guix-patches@gnu.org; Sun, 15 May 2022 00:47:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#55424] [PATCH 004/602] utils: Add a 'delete-expression' procedure. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 15 May 2022 04:47:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 55424 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 55424@debbugs.gnu.org Cc: Maxim Cournoyer Received: via spool by 55424-submit@debbugs.gnu.org id=B55424.165259001711061 (code B ref 55424); Sun, 15 May 2022 04:47:03 +0000 Received: (at 55424) by debbugs.gnu.org; 15 May 2022 04:46:57 +0000 Received: from localhost ([127.0.0.1]:47857 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nq69g-0002sK-Vz for submit@debbugs.gnu.org; Sun, 15 May 2022 00:46:57 -0400 Received: from mail-qt1-f173.google.com ([209.85.160.173]:38524) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nq69f-0002re-0D for 55424@debbugs.gnu.org; Sun, 15 May 2022 00:46:55 -0400 Received: by mail-qt1-f173.google.com with SMTP id fu47so10075973qtb.5 for <55424@debbugs.gnu.org>; Sat, 14 May 2022 21:46:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0hSHUoXvsA3T1Phj1BQ2SDD7VKjgWbirW4feNufPc28=; b=XHL/Ynkeks9iEc1DB+vip/NjU3tYOX5naBUHwHr0bR977/9VyIEOsn7daFevjEri2b nQ3JU6dEDCH9R0P3FY0ptNwFe3Kfgz8dd/qpyUfRIxqre/y3ukgwUGUB6LEotWJkh+0Y bYxoT8wjzqyW3W9qHvJkeQRCJ3x0HFxHHBGwOWq72lRbS2IeSuo25pxv+QhG2MwfWwUG fvpgBABjPd1PNgZ+7yc3EfW6Apz4pj/c0xIlVoYa/UyEuLzUlpclfd7clN8sUnbTlOQE +CAftXeeT7NzFHJdiLjSMrjibe1MOTWi3mwR7+ffCmg4WYuSkYzMuBEAPldasKmkRg3T SOiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0hSHUoXvsA3T1Phj1BQ2SDD7VKjgWbirW4feNufPc28=; b=oclg5St/tOHXYJzqihYiMWgNzfjBcKuk3Nk3pHFuPI+6W0b9Q1AGSvnvXvuaOCTc6a Y/ogHjgL9KFVSzaT1kaYGEa95eAbyc2/5I7fE5s5FDFgozgw7KaBw4ggNOYgMBdAPe+I l5XfzRBMk6DvGMWQxoz0MY4DC+LeY+z2VZ1YxZv+b+k1tMzhmC2uARqA0w/xtsgSgdSj vmwgY7Tj9o64Kf6nXC9x6eCWNheXl44gZ6S3B/CQblqH5lw2A+FdH+dfHBgV0r1j4TY1 YSHqzm32vDp/V6TyM2b3qh41AUfBuGoW5v3BNeL1Pu6+nJ3AS34/s2GIGqgXGrQAnXlS Vv3w== X-Gm-Message-State: AOAM530Ra8XtPQOikPWQ8vAYqmyNsFCivQvfxQbURMNFsAnnQi8zqvuN XWYFUONMbnZEiAspOQASzsmNGF6XULI8LQ== X-Google-Smtp-Source: ABdhPJww/Q2Ac4xWtzoH57XWgfTfl1mTZx8jeLZHCGwWw6IqiqN4HkIXp/zi+XY/QuhlYnumXicQpg== X-Received: by 2002:a05:622a:42:b0:2f3:dbbf:4508 with SMTP id y2-20020a05622a004200b002f3dbbf4508mr10966479qtw.206.1652590009303; Sat, 14 May 2022 21:46:49 -0700 (PDT) Received: from localhost.localdomain (dsl-149-239.b2b2c.ca. [66.158.149.239]) by smtp.gmail.com with ESMTPSA id 2-20020ac82082000000b002f39b99f672sm4287690qtd.12.2022.05.14.21.46.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 May 2022 21:46:49 -0700 (PDT) From: Maxim Cournoyer Date: Sun, 15 May 2022 00:36:31 -0400 Message-Id: <20220515044629.6843-4-maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220515044629.6843-1-maxim.cournoyer@gmail.com> References: <20220515044629.6843-1-maxim.cournoyer@gmail.com> MIME-Version: 1.0 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+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/utils.scm: Fix copyright lines and order imports. (edit-expression): Fix typo in doc. Add a new 'include-trailing-newline?' keyword argument. Update doc. (delete-expression): New procedure. --- guix/utils.scm | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/guix/utils.scm b/guix/utils.scm index 44c46cb4a9..e169624ee6 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -8,12 +8,11 @@ ;;; Copyright © 2017 Mathieu Othacehe ;;; Copyright © 2018, 2020 Marius Bakke ;;; Copyright © 2020, 2021 Efraim Flashner -;;; Copyright © 2020 Maxim Cournoyer +;;; Copyright © 2020, 2021, 2022 Maxim Cournoyer ;;; Copyright © 2021 Simon Tournier ;;; Copyright © 2021 Chris Marusich ;;; Copyright © 2021 Maxime Devos ;;; Copyright © 2018 Steve Sprang -;;; Copyright © 2021 Maxim Cournoyer ;;; ;;; This file is part of GNU Guix. ;;; @@ -38,7 +37,6 @@ (define-module (guix utils) #:use-module (srfi srfi-26) #:use-module (srfi srfi-39) #:use-module (srfi srfi-71) - #:use-module (ice-9 ftw) #:use-module (rnrs io ports) ;need 'port-position' etc. #:use-module ((rnrs bytevectors) #:select (bytevector-u8-set!)) #:use-module (guix memoization) @@ -49,10 +47,11 @@ (define-module (guix utils) #:use-module ((guix combinators) #:select (fold2)) #:use-module (guix diagnostics) ;, &error-location, etc. #:use-module (ice-9 format) - #:use-module (ice-9 regex) - #:use-module (ice-9 match) - #:use-module (ice-9 format) + #:use-module (ice-9 ftw) #:use-module ((ice-9 iconv) #:prefix iconv:) + #:use-module (ice-9 match) + #:use-module (ice-9 regex) + #:use-module (ice-9 rdelim) #:use-module (ice-9 vlist) #:autoload (zlib) (make-zlib-input-port make-zlib-output-port) #:use-module (system foreign) @@ -133,6 +132,7 @@ (define-module (guix utils) readlink* go-to-location edit-expression + delete-expression filtered-port decompressed-port @@ -433,11 +433,13 @@ (define (move-source-location-map! source target line) (hash-set! %source-location-map target-key `(,@target-stamp ,source-map))))))) -(define* (edit-expression source-properties proc #:key (encoding "UTF-8")) +(define* (edit-expression source-properties proc #:key (encoding "UTF-8") + include-trailing-newline?) "Edit the expression specified by SOURCE-PROPERTIES using PROC, which should be a procedure that takes the original expression in string and returns a new -one. ENCODING will be used to interpret all port I/O, it default to UTF-8. -This procedure returns #t on success." +one. ENCODING will be used to interpret all port I/O, it defaults to UTF-8. +This procedure returns #t on success. When INCLUDE-TRAILING-NEWLINE? is true, +the trailing line is included in the edited expression." (define file (assq-ref source-properties 'filename)) (define line (assq-ref source-properties 'line)) (define column (assq-ref source-properties 'column)) @@ -446,10 +448,14 @@ (define column (assq-ref source-properties 'column)) (call-with-input-file file (lambda (in) (let* ( ;; The start byte position of the expression. - (start (begin (go-to-location in (+ 1 line) (+ 1 column)) + (start (begin (go-to-location + in (+ 1 line) (+ 1 column)) (ftell in))) ;; The end byte position of the expression. - (end (begin (read in) (ftell in)))) + (end (begin (read in) + (when include-trailing-newline? + (read-line in)) + (ftell in)))) (seek in 0 SEEK_SET) ; read from the beginning of the file. (let* ((pre-bv (get-bytevector-n in start)) ;; The expression in string form. @@ -478,6 +484,10 @@ (define column (assq-ref source-properties 'column)) (move-source-location-map! (stat in) (stat file) (+ 1 line)))))))))) +(define (delete-expression source-properties) + "Delete the expression specified by SOURCE-PROPERTIES." + (edit-expression source-properties (const "") #:include-trailing-newline? #t)) + ;;; ;;; Keyword arguments.