Subject: [PATCH v2 4/4] gnu: emacs: Reload subdirs.el files in 'guix-emacs-autoload-packages'.

From: Maxim Cournoyer <[email-masked]>
Date: Mon, 28 Aug 2023 11:16:36 -0400

This fixes a regression introduced with 79cfe30f3 ("build-system: emacs: Use
subdirectories again.") which caused the 'guix-emacs-autoload-packages' to no
longer be able to autoload all packages.

* gnu/packages/aux-files/emacs/guix-emacs.el
(guix-emacs-autoload-packages-called): New variable.
(guix-emacs-autoload-packages): Reload subdirs.el files on all but the first
call of this procedure, or when a prefix argument is provided.  Update doc.
* doc/guix.texi (Application Setup): Document that
'guix-emacs-autoload-packages' can be invoked interactively to auto-reload
newly installed Emacs packages.

---

Changes in v2:
- Safely load subdirs.el files
- Add 'reload' prefix argument as override for guix-emacs-autoload-packages

 doc/guix.texi                              | 11 +++++++----
 gnu/packages/aux-files/emacs/guix-emacs.el | 21 ++++++++++++++++++---
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index f82bb99069..66da4f9cd4 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -2167,12 +2167,15 @@ Application Setup
 Emacs through the @env{EMACSLOADPATH} environment variable, which is
 set when installing Emacs itself.
 
+@cindex guix-emacs-autoload-packages, refreshing Emacs packages
 Additionally, autoload definitions are automatically evaluated at the
 initialization of Emacs, by the Guix-specific
-@code{guix-emacs-autoload-packages} procedure.  If, for some reason, you
-want to avoid auto-loading the Emacs packages installed with Guix, you
-can do so by running Emacs with the @option{--no-site-file} option
-(@pxref{Init File,,, emacs, The GNU Emacs Manual}).
+@code{guix-emacs-autoload-packages} procedure.  This procedure can be
+interactively invoked to have newly installed Emacs packages discovered,
+without having to restart Emacs.  If, for some reason, you want to avoid
+auto-loading the Emacs packages installed with Guix, you can do so by
+running Emacs with the @option{--no-site-file} option (@pxref{Init
+File,,, emacs, The GNU Emacs Manual}).
 
 @quotation Note
 Emacs can now compile packages natively.  Under the default
diff --git a/gnu/packages/aux-files/emacs/guix-emacs.el b/gnu/packages/aux-files/emacs/guix-emacs.el
index 84284dde39..aeb85f8487 100644
--- a/gnu/packages/aux-files/emacs/guix-emacs.el
+++ b/gnu/packages/aux-files/emacs/guix-emacs.el
@@ -64,13 +64,28 @@ The files in the list do not have extensions (.el, .elc)."
                         (expand-file-name "subdirs.el" dir))
                       (guix-emacs--non-core-load-path))))
 
+(defvar guix-emacs-autoload-packages-called nil
+  "True if `guix-emacs-autoload-packages' was already run.")
+
 ;;;###autoload
-(defun guix-emacs-autoload-packages ()
+(defun guix-emacs-autoload-packages (&optional reload)
   "Autoload Emacs packages found in EMACSLOADPATH.
 
 'Autoload' means to load the 'autoloads' files matching
-`guix-emacs-autoloads-regexp'."
-  (interactive)
+`guix-emacs-autoloads-regexp'.  When the optional RELOAD prefix
+argument is true, reload the subdirs.el files found on the load
+path; this behavior is active by default unless you've started
+Emacs with the '--no-site-file' option."
+  (interactive "P")
+  ;; Reload the subdirs.el files such as the one generated by the Guix profile
+  ;; hook, so that newly installed Emacs packages located under
+  ;; sub-directories are put on the load-path without having to restart Emacs.
+  (if (or reload guix-emacs-autoload-packages-called)
+      (progn
+        (mapc #'guix-emacs--load-file-no-error (guix-emacs--subdirs-files))
+        (setq load-path (delete-dups load-path)))
+    (setq guix-emacs-autoload-packages-called t))
+
   (let ((autoloads (mapcan #'guix-emacs-find-autoloads
                            (guix-emacs--non-core-load-path))))
     (mapc #'guix-emacs--load-file-no-error autoloads)))
-- 
2.41.0