Subject: [bug#28832] [PATCH 1/3] gnu: Add emacs-json-reformat.

From: Oleg Pykhalov <[email-masked]>
Date: Fri, 15 Dec 2017 12:36:28 +0300

From adda59022a61dfcf2add1d9f01d02df4be90a0f1 Mon Sep 17 00:00:00 2001
From: Oleg Pykhalov <[email-masked]>
Date: Wed, 13 Dec 2017 08:10:21 +0300
Subject: [PATCH] emacs-build-system: Add EMACSLOADPATH.

* guix/build/emacs-build-system.scm: Add EMACSLOADPATH.
---
 guix/build/emacs-build-system.scm | 47 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/guix/build/emacs-build-system.scm b/guix/build/emacs-build-system.scm
index bd0d2e026..06479bf6b 100644
--- a/guix/build/emacs-build-system.scm
+++ b/guix/build/emacs-build-system.scm
@@ -2,6 +2,7 @@
 ;;; Copyright © 2015 Federico Beffa <[email-masked]>
 ;;; Copyright © 2016 David Thompson <[email-masked]>
 ;;; Copyright © 2016 Alex Kost <[email-masked]>
+;;; Copyright © 2017 Oleg Pykhalov <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -183,6 +184,10 @@ store in '.el' files."
   "Check if NAME correspond to the name of an Emacs package."
   (string-prefix? "emacs-" name))
 
+(define (string-drop-emacs value)
+  "Drops `emacs-' from a string."
+  (string-drop value 6))
+
 (define (emacs-inputs inputs)
   "Retrieve the list of Emacs packages from INPUTS."
   (filter (match-lambda
@@ -222,6 +227,47 @@ second hyphen.  This corresponds to 'name-version' as used in ELPA packages."
             strip-store-file-name)
    store-dir))
 
+(define (store-directory->package-name store-dir)
+  "Extract package name from STORE-DIR."
+  (let-values (((name _) ((compose package-name->name+version
+                                   strip-store-file-name)
+                          store-dir)))
+    name))
+
+(define (store-directory->package-version store-dir)
+  "Extract package version from STORE-DIR."
+  (let-values (((_ version) ((compose package-name->name+version
+                                      strip-store-file-name)
+                             store-dir)))
+    version))
+
+(define* (setup-environment #:key inputs #:allow-other-keys)
+  "Export the variable EMACSLOADPATH, which are based on INPUTS respectively."
+  (let* ((filtered-inputs (emacs-inputs inputs))
+         (emacs-input-dir (cdr (assoc "emacs" filtered-inputs)))
+         (inputs-dirs (cdr filtered-inputs)))
+    ;; EMACSLOADPATH is where Emacs looks for the source code of the build's
+    ;; dependencies.
+    (setenv "EMACSLOADPATH"
+            (string-append emacs-input-dir "/share/emacs/"
+                           (store-directory->package-version emacs-input-dir)
+                           "/lisp"))
+    (for-each
+     (lambda (input)
+       (let ((store-item (cdr input)))
+         (setenv "EMACSLOADPATH"
+                 (string-append
+                  (or (getenv "EMACSLOADPATH") "")
+                  ":" store-item %install-suffix "/"
+                  ((compose string-drop-emacs store-directory->package-name)
+                   store-item)))))
+     ((compose (lambda (inputs) (alist-delete "emacs" inputs))
+               (lambda (inputs) (alist-delete "source" inputs)))
+      (delete-duplicates inputs-dirs)))
+    (format #t "environment variable EMACSLOADPATH set to `~a'~%"
+            (getenv "EMACSLOADPATH"))
+    #t))
+
 (define %standard-phases
   (modify-phases gnu:%standard-phases
     (replace 'unpack unpack)
@@ -229,6 +275,7 @@ second hyphen.  This corresponds to 'name-version' as used in ELPA packages."
     (delete 'check)
     (delete 'install)
     (replace 'build build)
+    (add-before 'build 'setup-environment setup-environment)
     (add-before 'build 'install install)
     (add-after 'install 'make-autoloads make-autoloads)
     (add-after 'make-autoloads 'patch-el-files patch-el-files)
-- 
2.15.1