download: Use 'with-imported-modules'.
[jackhill/guix/guix.git] / emacs / guix-emacs.el
1 ;;; guix-emacs.el --- Emacs packages installed with Guix
2
3 ;; Copyright © 2014, 2015, 2016 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 'cl-lib)
28 (unless (require 'guix-profiles nil t)
29 (defvar guix-user-profile (expand-file-name "~/.guix-profile")))
30
31 (defcustom guix-package-enable-at-startup t
32 "If non-nil, activate Emacs packages installed in a user profile.
33 Set this variable to nil before requiring `guix-emacs' file to
34 avoid loading autoloads of Emacs packages installed in
35 `guix-user-profile'."
36 :type 'boolean
37 :group 'guix)
38
39 (defvar guix-emacs-autoloads nil
40 "List of the last loaded Emacs autoloads.")
41
42 (defvar guix-emacs-autoloads-regexp
43 (rx (group (* any) "-autoloads")
44 ".el" (zero-or-one "c") string-end)
45 "Regexp to match Emacs 'autoloads' file.")
46
47 (defun guix-emacs-directory (&optional profile)
48 "Return directory with Emacs packages installed in PROFILE.
49 If PROFILE is nil, use `guix-user-profile'."
50 (expand-file-name "share/emacs/site-lisp"
51 (or profile guix-user-profile)))
52
53 (defun guix-emacs-find-autoloads (directory)
54 "Return a list of Emacs 'autoloads' files in DIRECTORY.
55 The files in the list do not have extensions (.el, .elc)."
56 (cl-remove-duplicates
57 (delq nil
58 (mapcar (lambda (file)
59 (when (string-match guix-emacs-autoloads-regexp file)
60 (match-string 1 file)))
61 (directory-files directory 'full-name nil 'no-sort)))
62 :test #'string=))
63
64 (defun guix-emacs-subdirs (directory)
65 "Return list of DIRECTORY subdirectories."
66 (cl-remove-if (lambda (file)
67 (or (string-match-p (rx "/." string-end) file)
68 (string-match-p (rx "/.." string-end) file)
69 (not (file-directory-p file))))
70 (directory-files directory 'full-name nil 'no-sort)))
71
72 (defun guix-emacs-directories (&optional profile)
73 "Return the list of directories under PROFILE that contain Emacs packages.
74 This includes both `share/emacs/site-lisp/guix.d/PACKAGE'
75 sub-directories and `share/emacs/site-lisp' itself.
76
77 If PROFILE is nil, use `guix-user-profile'.
78 Return nil, if Emacs packages are not installed in PROFILE."
79 (let ((root-dir (guix-emacs-directory (or profile guix-user-profile))))
80 (when (file-directory-p root-dir)
81 (let* ((pkgs-dir (expand-file-name "guix.d" root-dir))
82 (pkgs-dirs (when (file-directory-p pkgs-dir)
83 (guix-emacs-subdirs pkgs-dir))))
84 (cons root-dir pkgs-dirs)))))
85
86 ;;;###autoload
87 (defun guix-emacs-autoload-packages (&rest profiles)
88 "Autoload Emacs packages installed in PROFILES.
89 If PROFILES are not specified, use a default user and system
90 profiles.
91
92 'Autoload' means add directories with Emacs packages to
93 `load-path' and load 'autoloads' files matching
94 `guix-emacs-autoloads-regexp'."
95 (interactive (list (if (fboundp 'guix-profile-prompt)
96 (funcall 'guix-profile-prompt)
97 guix-user-profile)))
98 (let ((profiles (or profiles
99 (list "/run/current-system/profile"
100 guix-user-profile))))
101 (dolist (profile profiles)
102 (let ((dirs (guix-emacs-directories profile)))
103 (when dirs
104 (let* ((autoloads (cl-mapcan #'guix-emacs-find-autoloads
105 dirs))
106 (new-autoloads (cl-nset-difference autoloads
107 guix-emacs-autoloads
108 :test #'string=)))
109 (dolist (dir dirs)
110 (cl-pushnew (directory-file-name dir)
111 load-path
112 :test #'string=))
113 (dolist (file new-autoloads)
114 (load file 'noerror))
115 (setq guix-emacs-autoloads
116 (append new-autoloads guix-emacs-autoloads))))))))
117
118 (when guix-package-enable-at-startup
119 (guix-emacs-autoload-packages))
120
121 (provide 'guix-emacs)
122
123 ;;; guix-emacs.el ends here