gnu: Add ruby-power-assert.
[jackhill/guix/guix.git] / emacs / guix-emacs.el
1 ;;; guix-emacs.el --- Emacs packages installed with Guix
2
3 ;; Copyright © 2014 Alex Kost <alezost@gmail.com>
4
5 ;; This file is part of GNU Guix.
6
7 ;; GNU Guix is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11
12 ;; GNU Guix is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 ;;; Commentary:
21
22 ;; This file provides auxiliary code for working with Emacs packages
23 ;; installed with Guix.
24
25 ;;; Code:
26
27 (require 'guix-profiles)
28 (require 'cl-lib)
29
30 (defcustom guix-emacs-activate-after-operation t
31 "Activate Emacs packages after installing.
32 If nil, do not load autoloads of the Emacs packages after
33 they are successfully installed."
34 :type 'boolean
35 :group 'guix)
36
37 (defvar guix-emacs-autoloads nil
38 "List of the last loaded Emacs autoloads.")
39
40 (defun guix-emacs-directory (&optional profile)
41 "Return directory with Emacs packages installed in PROFILE.
42 If PROFILE is nil, use `guix-user-profile'."
43 (expand-file-name "share/emacs/site-lisp"
44 (or profile guix-user-profile)))
45
46 (defun guix-emacs-find-autoloads-in-directory (directory)
47 "Return list of Emacs 'autoloads' files in DIRECTORY."
48 (directory-files directory 'full-name "-autoloads\\.el\\'" 'no-sort))
49
50 (defun guix-emacs-subdirs (directory)
51 "Return list of DIRECTORY subdirectories."
52 (cl-remove-if (lambda (file)
53 (or (string-match-p (rx "/." string-end) file)
54 (string-match-p (rx "/.." string-end) file)
55 (not (file-directory-p file))))
56 (directory-files directory 'full-name nil 'no-sort)))
57
58 (defun guix-emacs-find-autoloads (&optional profile)
59 "Return list of autoloads of Emacs packages installed in PROFILE.
60 If PROFILE is nil, use `guix-user-profile'.
61 Return nil if there are no emacs packages installed in PROFILE."
62 (let ((elisp-root-dir (guix-emacs-directory profile)))
63 (if (file-directory-p elisp-root-dir)
64 (let ((elisp-pkgs-dir (expand-file-name "guix.d" elisp-root-dir))
65 (root-autoloads (guix-emacs-find-autoloads-in-directory
66 elisp-root-dir)))
67 (if (file-directory-p elisp-pkgs-dir)
68 (let ((pkgs-autoloads
69 (cl-mapcan #'guix-emacs-find-autoloads-in-directory
70 (guix-emacs-subdirs elisp-pkgs-dir))))
71 (append root-autoloads pkgs-autoloads))
72 root-autoloads))
73 (message "Directory '%s' does not exist." elisp-root-dir)
74 nil)))
75
76 ;;;###autoload
77 (defun guix-emacs-load-autoloads (&optional all)
78 "Load autoloads for Emacs packages installed in a user profile.
79 Add autoloads directories to `load-path'.
80 If ALL is nil, activate only those packages that were installed
81 after the last activation, otherwise activate all Emacs packages
82 installed in `guix-user-profile'."
83 (interactive "P")
84 (let* ((autoloads (guix-emacs-find-autoloads))
85 (files (if all
86 autoloads
87 (cl-nset-difference autoloads guix-emacs-autoloads
88 :test #'string=))))
89 (dolist (file files)
90 (cl-pushnew (file-name-directory file) load-path
91 :test #'string=)
92 (load file 'noerror))
93 (setq guix-emacs-autoloads autoloads)))
94
95 (defun guix-emacs-load-autoloads-maybe ()
96 "Load autoloads for Emacs packages if needed.
97 See `guix-emacs-activate-after-operation' for details."
98 (and guix-emacs-activate-after-operation
99 (guix-emacs-load-autoloads)))
100
101 (provide 'guix-emacs)
102
103 ;;; guix-emacs.el ends here