[PATCH 0/2] Add a function to parse emacs elisp's package header

OpenSubmitted by Fredrik Salomonsson.
Details
One participant
  • Fredrik Salomonsson
Owner
unassigned
Severity
normal
F
F
Fredrik Salomonsson wrote on 15 May 01:00 +0200
(address . guix-patches@gnu.org)(name . Fredrik Salomonsson)(address . plattfot@posteo.net)
20220514230017.27372-1-plattfot@posteo.net
When writing a guix.scm file for an emacs packages, I wanted a way to fetch
the version I specify in the header of the elisp file. I found that I could do
that with the lisp-mnt library in emacs. Unfortunately none of the emacs
utility functions in guix/build/emacs-utils.scm return the output of an
elisp expression.

With these two patches I aim to add that functionality.

I spilt it in two functions:

emacs-batch-script: Run an arbitrary elisp expression and return the output as
a string.

emacs-header-parse: Given a header section and a file it will return the value
of that section.

For example (emacs-header-parse "version" "path/to/an/elisp/package.el")

Reason for the spilt is that people might want to run other elisp snippets.

Fredrik Salomonsson (2):
guix: emacs-utils: Add emacs-batch-script.
guix: emacs-utils: Add emacs-header-parse.

guix/build/emacs-utils.scm | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)


base-commit: 3935451f63c078cae9a928d87c6838ec3138abc0
--
2.36.0
F
F
Fredrik Salomonsson wrote on 15 May 01:05 +0200
[PATCH 1/2] guix: emacs-utils: Add emacs-batch-script.
(address . 55420@debbugs.gnu.org)(name . Fredrik Salomonsson)(address . plattfot@posteo.net)
20220514230508.27885-1-plattfot@posteo.net
* guix/build/emacs-utils.scm (emacs-batch-script): New procedure.
---
guix/build/emacs-utils.scm | 13 +++++++++++++
1 file changed, 13 insertions(+)

Toggle diff (44 lines)
diff --git a/guix/build/emacs-utils.scm b/guix/build/emacs-utils.scm
index 60a754b9e9..8d40b9e139 100644
--- a/guix/build/emacs-utils.scm
+++ b/guix/build/emacs-utils.scm
@@ -3,6 +3,7 @@
 ;;; Copyright © 2014 Alex Kost <alezost@gmail.com>
 ;;; Copyright © 2018, 2020, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;; Copyright © 2019 Liliana Marie Prikler <liliana.prikler@gmail.com>
+;;; Copyright © 2022 Fredrik Salomonsson <plattfot@posteo.net>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -22,10 +23,13 @@
 (define-module (guix build emacs-utils)
   #:use-module (guix build utils)
   #:use-module (ice-9 format)
+  #:use-module (ice-9 popen)
+  #:use-module (ice-9 rdelim)
   #:export (%emacs
             emacs-batch-eval
             emacs-batch-edit-file
             emacs-batch-disable-compilation
+            emacs-batch-script
             emacs-generate-autoloads
             emacs-byte-compile-directory
 
@@ -69,6 +73,15 @@ (define (emacs-batch-disable-compilation file)
       (add-file-local-variable 'no-byte-compile t)
       (basic-save-buffer))))
 
+(define (emacs-batch-script expr)
+  "Execute the Elisp code EXPR in Emacs batch mode and return output."
+  (call-with-port
+      (open-pipe*
+       OPEN_READ
+       (%emacs) "--quick" "--batch"
+       (string-append "--eval=" (expr->string expr)))
+    read-string))
+
 (define (emacs-generate-autoloads name directory)
   "Generate autoloads for Emacs package NAME placed in DIRECTORY."
   (let* ((file (string-append directory "/" name "-autoloads.el"))
-- 
2.36.0
F
F
Fredrik Salomonsson wrote on 15 May 01:05 +0200
[PATCH 2/2] guix: emacs-utils: Add emacs-header-parse.
(address . 55420@debbugs.gnu.org)(name . Fredrik Salomonsson)(address . plattfot@posteo.net)
20220514230508.27885-2-plattfot@posteo.net
* guix/build/emacs-utils.scm (emacs-header-parse): New procedure.
---
guix/build/emacs-utils.scm | 9 +++++++++
1 file changed, 9 insertions(+)

Toggle diff (29 lines)
diff --git a/guix/build/emacs-utils.scm b/guix/build/emacs-utils.scm
index 8d40b9e139..76d9464806 100644
--- a/guix/build/emacs-utils.scm
+++ b/guix/build/emacs-utils.scm
@@ -32,6 +32,7 @@ (define-module (guix build emacs-utils)
             emacs-batch-script
             emacs-generate-autoloads
             emacs-byte-compile-directory
+            emacs-header-parse
 
             as-display
             emacs-substitute-sexps
@@ -97,6 +98,14 @@ (define* (emacs-byte-compile-directory dir)
                 (byte-recompile-directory (file-name-as-directory ,dir) 0 1))))
     (emacs-batch-eval expr)))
 
+(define (emacs-header-parse section file)
+  "Parse the header SECTION in FILE and return it as a string."
+  (emacs-batch-script
+   `(progn
+     (require 'lisp-mnt)
+     (find-file ,file)
+     (princ (lm-header ,section)))))
+
 (define as-display         ;syntactic keyword for 'emacs-substitute-sexps'
   '(as display))
 
-- 
2.36.0
?