From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id KOo5LK98xmUa7AAAqHPOHw:P1 (envelope-from ) for ; Fri, 09 Feb 2024 20:27:43 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id KOo5LK98xmUa7AAAqHPOHw (envelope-from ) for ; Fri, 09 Feb 2024 20:27:43 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=rimm.ee header.s=herman header.b=MRNR7yIG; 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"; dmarc=pass (policy=none) header.from=gnu.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1707506863; h=from:from:sender:sender:reply-to: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=iglSg6lJSvpt1mvW6yDKA1v6LBrFqLDh2JD8IjI932s=; b=EkQh4MAPglU4WAEJSYdZWVyz9pvamqz1gjdQrUKecn56pP0bd1gefXKnLxFP5HcwiZrhC4 zqivHuBtpr6FtnMzzBtipdVwlIH5zDOeKSdgNQ221svD8Y/kGLqojmxLOSEQKj5TPnyUFO BemWJj45pOh8umB29qzhdBTvAK5jNhxI6Mzq34EU2LBa0frR9IN8wHj1hy/p6OXWe+Z5Wx sA4/ESyKf9gISmb74dqM3s6BuHZvVReAhxzh1kxjvnk6FFQBObIRo0Yvn4w4064Uz0R0D0 yl2jqOoCCMysNpt8BVXYZgBRSMzovUt9Cx+zSCE/UtqK4nCN2Mpz36Eb2N43nA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=rimm.ee header.s=herman header.b=MRNR7yIG; 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"; dmarc=pass (policy=none) header.from=gnu.org ARC-Seal: i=1; s=key1; d=yhetil.org; t=1707506863; a=rsa-sha256; cv=none; b=pT0prkLaiIQpIWiW5WGgq2dn2Fhyj3uLRi/cebvDnXaU1oMfuOTQ8N1ghnkyG2K0D+TCZ/ bZSycNZPh/qcpSeCS0t6XIVgsmjQe6DDG2yCgwg4fu/gSuMyHnj6VHTFTlYcnhFChP1DjD uY4eRZdGqfM7KItRo2k6o+uJJEQDm7SDDDIc6+5YSphK30rnktfZm8Cgef1VejY7UsvZrf 0ilRy3pLqQ+i5ilPAUL+J+3rtWYL8K/nWjLJ3/1WvOCiGUWVuLBBpZLa45t5kM2/gQcVtN KBMiw5NRBMl4z8/1jU+W3RSDvDU84ZPFxxR/UJqhMYUqUj1lvZoVIsNUHzR4MQ== 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 9F23F12708 for ; Fri, 9 Feb 2024 20:27:43 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rYWWS-0002VX-Nx; Fri, 09 Feb 2024 14:26:52 -0500 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 1rYWWR-0002Ul-JP for guix-patches@gnu.org; Fri, 09 Feb 2024 14:26:51 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rYWWR-0000hz-As; Fri, 09 Feb 2024 14:26:51 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rYWWd-0001bS-GV; Fri, 09 Feb 2024 14:27:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#68935] [PATCH v2 4/6] utils: Add find-expression procedure. Resent-From: Herman Rimm Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, rekado@elephly.net, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Fri, 09 Feb 2024 19:27:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68935 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 68935@debbugs.gnu.org Cc: ludo@gnu.org, Herman Rimm , Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 68935-submit@debbugs.gnu.org id=B68935.17075068076074 (code B ref 68935); Fri, 09 Feb 2024 19:27:03 +0000 Received: (at 68935) by debbugs.gnu.org; 9 Feb 2024 19:26:47 +0000 Received: from localhost ([127.0.0.1]:60234 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rYWWN-0001Zp-Ee for submit@debbugs.gnu.org; Fri, 09 Feb 2024 14:26:47 -0500 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117]:52227 helo=email.rimm.ee) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rYWWK-0001Ya-8O for 68935@debbugs.gnu.org; Fri, 09 Feb 2024 14:26:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1707506774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iglSg6lJSvpt1mvW6yDKA1v6LBrFqLDh2JD8IjI932s=; b=MRNR7yIGmS53WTOZ9vhbX4SwkOYaON7jlqqfRJz8gbeGb3QdQkvIhb4/viH3aoyL9uwUXb qd/WgKIRGo5h4L5687gQZSbAb9GlvsZ3FXbvrLxSubuYGbVb20vR2Q3WFO+gHwVydvsvHp wxfF6TsWycGiu/otX9xJi+V2icq+C8aQflH6eFkFXmw2R/xuhRBVu9bsoux+xcTdj8Mpko vpBrcV2EyOVoA2Th+lav1Bpu6XEzd6qlLwEgJOTpDs217sALrFokXHBPxHsNH5G64IEtAH xBCvxdJ8hs66KvBqZ92N2DOsUX4ugKPmiKpvO+weP4oX/ZF6gvIPLq901YFpTA== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id a31a6ace (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO); Fri, 9 Feb 2024 19:26:14 +0000 (UTC) Date: Fri, 9 Feb 2024 20:25:16 +0100 Message-ID: <028a3d700965947fa0547eb17d4894b8302eb67c.1707505804.git.herman@rimm.ee> X-Mailer: git-send-email 2.41.0 In-Reply-To: <3c0da641938e96a9f0c491abb74b30c75634df9a.1707505804.git.herman@rimm.ee> References: <3c0da641938e96a9f0c491abb74b30c75634df9a.1707505804.git.herman@rimm.ee> 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: , Reply-to: Herman Rimm X-ACL-Warn: , Herman Rimm via Guix-patches From: Herman Rimm via Guix-patches via Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -5.17 X-Migadu-Scanner: mx13.migadu.com X-Spam-Score: -5.17 X-Migadu-Queue-Id: 9F23F12708 X-TUID: kN0CoAul9puI * guix/utils.scm (find-expression): Add and export procedure. * tests/utils.scm ("find-expression"): Add test. Change-Id: Ie209df39c1f006b20aa6436fb1aef4c84b1694ee --- guix/utils.scm | 24 ++++++++++++++++++++++++ tests/utils.scm | 16 ++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/guix/utils.scm b/guix/utils.scm index 8f0bc2399e..315feeb232 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -148,6 +148,7 @@ (define-module (guix utils) edit-expression delete-expression insert-expression + find-expression filtered-port decompressed-port @@ -513,6 +514,29 @@ (define (insert-expression source-properties expr) (string-append expr "\n\n" str)))) (edit-expression source-properties insert))) +(define (find-expression file expr proc) + "Search in FILE for a top-level expression which alphabetically +succeeds EXPR. Call PROC with the location if found, or with #f +otherwise." + (let* ((name (match expr + (('define-public symbol _ ...) + (symbol->string symbol)))) + (source-properties + (call-with-input-file + file + (lambda (port) + (do ((syntax (read-syntax port) + (read-syntax port))) + ((match (syntax->datum syntax) + (('define-public symbol _ ...) + (string> (symbol->string symbol) + name)) + ((? eof-object?) #t) + (_ #f)) + (if (eof-object? syntax) + #f (syntax-source syntax)))))))) + (proc source-properties))) + ;;; ;;; Keyword arguments. diff --git a/tests/utils.scm b/tests/utils.scm index cd54112846..feaed4b561 100644 --- a/tests/utils.scm +++ b/tests/utils.scm @@ -288,6 +288,22 @@ (define-public package-2\n 'package)\n" `(define-public package-1 'package)) (call-with-input-file temp-file get-string-all))) +(test-equal "find-expression" + (list `((filename . ,temp-file) (line . 0) (column . 0)) + `((filename . ,temp-file) (line . 5) (column . 0)) + #f) + (begin + (call-with-output-file temp-file + (lambda (port) + (display "(define-public package-1\n 'foo)\n\n" port) + (display "(define foo 'bar)\n\n" port) + (display "(define-public package-2\n 'baz)\n" port))) + (map (lambda (expr) + (find-expression temp-file expr identity)) + (list `(define-public package 'foo) + `(define-public package-1 'bar) + `(define-public package-2 'baz))))) + (test-equal "string-distance" '(0 1 1 5 5) (list -- 2.41.0