emacs: Factorize searching for Emacs packages.
* emacs/guix-emacs.el: (guix-emacs-directories): New procedure. (guix-emacs-find-autoloads-in-directory): Rename to... (guix-emacs-find-autoloads): ... this. (guix-emacs-load-autoloads): Remove. Replace with... (guix-emacs-autoload-packages): ... this. New procedure. At first, find a list of directories with Emacs packages, then add them to 'load-path' and finally, load autoloads. * doc/emacs.texi (Emacs Initial Setup): Adjust accordingly.
This commit is contained in:
		
							parent
							
								
									6588c2834a
								
							
						
					
					
						commit
						abea77e3f0
					
				
					 2 changed files with 41 additions and 36 deletions
				
			
		| 
						 | 
					@ -106,7 +106,7 @@ for the built-in Emacs package system (@pxref{Package Installation,,,
 | 
				
			||||||
emacs, The GNU Emacs Manual}).
 | 
					emacs, The GNU Emacs Manual}).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
You can activate Emacs packages installed in your profile whenever you
 | 
					You can activate Emacs packages installed in your profile whenever you
 | 
				
			||||||
want using @kbd{M-x@tie{}guix-emacs-load-autoloads}.
 | 
					want using @kbd{M-x@tie{}guix-emacs-autoload-packages}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@node Emacs Package Management
 | 
					@node Emacs Package Management
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -57,7 +57,7 @@ If PROFILE is nil, use `guix-user-profile'."
 | 
				
			||||||
  (expand-file-name "share/emacs/site-lisp"
 | 
					  (expand-file-name "share/emacs/site-lisp"
 | 
				
			||||||
                    (or profile guix-user-profile)))
 | 
					                    (or profile guix-user-profile)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun guix-emacs-find-autoloads-in-directory (directory)
 | 
					(defun guix-emacs-find-autoloads (directory)
 | 
				
			||||||
  "Return a list of Emacs 'autoloads' files in DIRECTORY.
 | 
					  "Return a list of Emacs 'autoloads' files in DIRECTORY.
 | 
				
			||||||
The files in the list do not have extensions (.el, .elc)."
 | 
					The files in the list do not have extensions (.el, .elc)."
 | 
				
			||||||
  (cl-remove-duplicates
 | 
					  (cl-remove-duplicates
 | 
				
			||||||
| 
						 | 
					@ -76,43 +76,49 @@ The files in the list do not have extensions (.el, .elc)."
 | 
				
			||||||
                      (not (file-directory-p file))))
 | 
					                      (not (file-directory-p file))))
 | 
				
			||||||
                (directory-files directory 'full-name nil 'no-sort)))
 | 
					                (directory-files directory 'full-name nil 'no-sort)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun guix-emacs-find-autoloads (&optional profile)
 | 
					(defun guix-emacs-directories (&optional profile)
 | 
				
			||||||
  "Return list of autoloads of Emacs packages installed in PROFILE.
 | 
					  "Return the list of directories under PROFILE that contain Emacs packages.
 | 
				
			||||||
 | 
					This includes both `share/emacs/site-lisp/guix.d/PACKAGE'
 | 
				
			||||||
 | 
					sub-directories and `share/emacs/site-lisp' itself.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
If PROFILE is nil, use `guix-user-profile'.
 | 
					If PROFILE is nil, use `guix-user-profile'.
 | 
				
			||||||
Return nil if there are no emacs packages installed in PROFILE."
 | 
					Return nil, if Emacs packages are not installed in PROFILE."
 | 
				
			||||||
  (let ((elisp-root-dir (guix-emacs-directory profile)))
 | 
					  (let ((root-dir (guix-emacs-directory (or profile guix-user-profile))))
 | 
				
			||||||
    (if (file-directory-p elisp-root-dir)
 | 
					    (when (file-directory-p root-dir)
 | 
				
			||||||
        (let ((elisp-pkgs-dir (expand-file-name "guix.d" elisp-root-dir))
 | 
					      (let* ((pkgs-dir  (expand-file-name "guix.d" root-dir))
 | 
				
			||||||
              (root-autoloads (guix-emacs-find-autoloads-in-directory
 | 
					             (pkgs-dirs (when (file-directory-p pkgs-dir)
 | 
				
			||||||
                               elisp-root-dir)))
 | 
					                          (guix-emacs-subdirs pkgs-dir))))
 | 
				
			||||||
          (if (file-directory-p elisp-pkgs-dir)
 | 
					        (cons root-dir pkgs-dirs)))))
 | 
				
			||||||
              (let ((pkgs-autoloads
 | 
					 | 
				
			||||||
                     (cl-mapcan #'guix-emacs-find-autoloads-in-directory
 | 
					 | 
				
			||||||
                                (guix-emacs-subdirs elisp-pkgs-dir))))
 | 
					 | 
				
			||||||
                (append root-autoloads pkgs-autoloads))
 | 
					 | 
				
			||||||
            root-autoloads))
 | 
					 | 
				
			||||||
      (message "Directory '%s' does not exist." elisp-root-dir)
 | 
					 | 
				
			||||||
      nil)))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
;;;###autoload
 | 
					;;;###autoload
 | 
				
			||||||
(defun guix-emacs-load-autoloads (&optional profile)
 | 
					(defun guix-emacs-autoload-packages (&rest profiles)
 | 
				
			||||||
  "Load autoloads for Emacs packages installed in PROFILE.
 | 
					  "Autoload Emacs packages installed in PROFILES.
 | 
				
			||||||
If PROFILE is nil, use `guix-user-profile'.
 | 
					If PROFILES are not specified, use `guix-user-profile'.
 | 
				
			||||||
Add autoloads directories to `load-path'."
 | 
					
 | 
				
			||||||
 | 
					'Autoload' means add directories with Emacs packages to
 | 
				
			||||||
 | 
					`load-path' and load 'autoloads' files matching
 | 
				
			||||||
 | 
					`guix-emacs-autoloads-regexp'."
 | 
				
			||||||
  (interactive (list (if (fboundp 'guix-profile-prompt)
 | 
					  (interactive (list (if (fboundp 'guix-profile-prompt)
 | 
				
			||||||
                         (funcall 'guix-profile-prompt)
 | 
					                         (funcall 'guix-profile-prompt)
 | 
				
			||||||
                       guix-user-profile)))
 | 
					                       guix-user-profile)))
 | 
				
			||||||
  (let* ((autoloads     (guix-emacs-find-autoloads profile))
 | 
					  (let ((profiles (or profiles
 | 
				
			||||||
         (new-autoloads (cl-nset-difference autoloads
 | 
					                      (list guix-user-profile))))
 | 
				
			||||||
                                            guix-emacs-autoloads
 | 
					    (dolist (profile profiles)
 | 
				
			||||||
                                            :test #'string=)))
 | 
					      (let ((dirs (guix-emacs-directories profile)))
 | 
				
			||||||
    (dolist (file new-autoloads)
 | 
					        (when dirs
 | 
				
			||||||
      (cl-pushnew (directory-file-name (file-name-directory file))
 | 
					          (let* ((autoloads     (cl-mapcan #'guix-emacs-find-autoloads
 | 
				
			||||||
                  load-path
 | 
					                                           dirs))
 | 
				
			||||||
                  :test #'string=)
 | 
					                 (new-autoloads (cl-nset-difference autoloads
 | 
				
			||||||
      (load file 'noerror))
 | 
					                                                    guix-emacs-autoloads
 | 
				
			||||||
    (setq guix-emacs-autoloads
 | 
					                                                    :test #'string=)))
 | 
				
			||||||
          (append new-autoloads guix-emacs-autoloads))))
 | 
					            (dolist (dir dirs)
 | 
				
			||||||
 | 
					              (cl-pushnew (directory-file-name dir)
 | 
				
			||||||
 | 
					                          load-path
 | 
				
			||||||
 | 
					                          :test #'string=))
 | 
				
			||||||
 | 
					            (dolist (file new-autoloads)
 | 
				
			||||||
 | 
					              (load file 'noerror))
 | 
				
			||||||
 | 
					            (setq guix-emacs-autoloads
 | 
				
			||||||
 | 
					                  (append new-autoloads guix-emacs-autoloads))))))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun guix-emacs-load-autoloads-maybe ()
 | 
					(defun guix-emacs-load-autoloads-maybe ()
 | 
				
			||||||
  "Load autoloads for Emacs packages if needed.
 | 
					  "Load autoloads for Emacs packages if needed.
 | 
				
			||||||
| 
						 | 
					@ -123,11 +129,10 @@ See `guix-emacs-activate-after-operation' for details."
 | 
				
			||||||
       ;; packages can be installed to another profile, and the
 | 
					       ;; packages can be installed to another profile, and the
 | 
				
			||||||
       ;; following code will not work (i.e., the autoloads for this
 | 
					       ;; following code will not work (i.e., the autoloads for this
 | 
				
			||||||
       ;; profile will not be loaded).
 | 
					       ;; profile will not be loaded).
 | 
				
			||||||
       (guix-emacs-load-autoloads guix-current-profile)))
 | 
					       (guix-emacs-autoload-packages guix-current-profile)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(when guix-package-enable-at-startup
 | 
					(when guix-package-enable-at-startup
 | 
				
			||||||
  (add-to-list 'load-path (guix-emacs-directory))
 | 
					  (guix-emacs-autoload-packages))
 | 
				
			||||||
  (guix-emacs-load-autoloads))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
(provide 'guix-emacs)
 | 
					(provide 'guix-emacs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Reference in a new issue