[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
03/07: utils: Add insert-expression procedure.
From: |
guix-commits |
Subject: |
03/07: utils: Add insert-expression procedure. |
Date: |
Fri, 23 Feb 2024 14:51:40 -0500 (EST) |
civodul pushed a commit to branch master
in repository guix.
commit babd39e84389c544e8dab44be8ddec57e52709c9
Author: Herman Rimm <herman@rimm.ee>
AuthorDate: Tue Feb 20 21:45:11 2024 +0100
utils: Add insert-expression procedure.
* guix/utils.scm (define-module): Use (guix read-print) and export
(insert-expression).
(insert-expression): Add procedure.
* tests/utils.scm ("insert-expression"): Add test.
Change-Id: I971a43a78aa6ecaaef33c1a7a0db4b287eb85036
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
---
guix/utils.scm | 11 +++++++++++
tests/utils.scm | 14 ++++++++++++++
2 files changed, 25 insertions(+)
diff --git a/guix/utils.scm b/guix/utils.scm
index e4e9d922e7..94b4d753d0 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -20,6 +20,7 @@
;;; Copyright © 2023 Janneke Nieuwenhuizen <janneke@gnu.org>
;;; Copyright © 2023 Zheng Junjie <873216071@qq.com>
;;; Copyright © 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
+;;; Copyright © 2024 Herman Rimm <herman@rimm.ee>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -38,6 +39,7 @@
(define-module (guix utils)
#:use-module (guix config)
+ #:autoload (guix read-print) (object->string*)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-11)
#:use-module (srfi srfi-26)
@@ -145,6 +147,7 @@
go-to-location
edit-expression
delete-expression
+ insert-expression
filtered-port
decompressed-port
@@ -502,6 +505,14 @@ the trailing line is included in the edited expression."
"Delete the expression specified by SOURCE-PROPERTIES."
(edit-expression source-properties (const "") #:include-trailing-newline?
#t))
+(define (insert-expression source-properties expr)
+ "Insert EXPR before the top-level expression specified by
+SOURCE-PROPERTIES."
+ (let* ((expr (object->string* expr 0))
+ (insert (lambda (str)
+ (string-append expr "\n\n" str))))
+ (edit-expression source-properties insert)))
+
;;;
;;; Keyword arguments.
diff --git a/tests/utils.scm b/tests/utils.scm
index 5664165c85..cd54112846 100644
--- a/tests/utils.scm
+++ b/tests/utils.scm
@@ -5,6 +5,7 @@
;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
;;; Copyright © 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
+;;; Copyright © 2024 Herman Rimm <herman@rimm.ee>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -274,6 +275,19 @@ skip these tests."
string-reverse)
(call-with-input-file temp-file get-string-all)))
+(test-equal "insert-expression"
+ "(define-public package-1\n 'package)\n
+(define-public package-2\n 'package)\n"
+ (begin
+ (call-with-output-file temp-file
+ (lambda (port)
+ (display "(define-public package-2\n 'package)\n" port)))
+ (insert-expression `((filename . ,temp-file)
+ (line . 0)
+ (column . 0))
+ `(define-public package-1 'package))
+ (call-with-input-file temp-file get-string-all)))
+
(test-equal "string-distance"
'(0 1 1 5 5)
(list
- branch master updated (e7990b1217 -> b386c11e78), guix-commits, 2024/02/23
- 03/07: utils: Add insert-expression procedure.,
guix-commits <=
- 02/07: import: Wrap package expressions with define-public., guix-commits, 2024/02/23
- 01/07: doc: Note SVN dependency of texlive importer., guix-commits, 2024/02/23
- 05/07: import: Insert packages into modules alphabetically., guix-commits, 2024/02/23
- 06/07: import: Discard args after --version and --help., guix-commits, 2024/02/23
- 07/07: import: Do not return package name with json importer., guix-commits, 2024/02/23
- 04/07: utils: Add find-definition-insertion-location procedure., guix-commits, 2024/02/23