emacs: Make "guix-emacs.el" independent.
[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-emacs-activate-after-operation t
32 "Activate Emacs packages after installing.
33 If nil, do not load autoloads of the Emacs packages after
34 they are successfully installed."
35 :type 'boolean
36 :group 'guix)
37
38 (defvar guix-emacs-autoloads nil
39 "List of the last loaded Emacs autoloads.")
40
41 (defvar guix-emacs-autoloads-regexp
42 (rx (group (* any) "-autoloads")
43 ".el" (zero-or-one "c") string-end)
44 "Regexp to match Emacs 'autoloads' file.")
45
46 (defun guix-emacs-directory (&optional profile)
47 "Return directory with Emacs packages installed in PROFILE.
48 If PROFILE is nil, use `guix-user-profile'."
49 (expand-file-name "share/emacs/site-lisp"
50 (or profile guix-user-profile)))
51
52 (defun guix-emacs-find-autoloads-in-directory (directory)
53 "Return a list of Emacs 'autoloads' files in DIRECTORY.
54 The files in the list do not have extensions (.el, .elc)."
55 (cl-remove-duplicates
56 (delq nil
57 (mapcar (lambda (file)
58 (when (string-match guix-emacs-autoloads-regexp file)
59 (match-string 1 file)))
60 (directory-files directory 'full-name nil 'no-sort)))
61 :test #'string=))
62
63 (defun guix-emacs-subdirs (directory)
64 "Return list of DIRECTORY subdirectories."
65 (cl-remove-if (lambda (file)
66 (or (string-match-p (rx "/." string-end) file)
67 (string-match-p (rx "/.." string-end) file)
68 (not (file-directory-p file))))
69 (directory-files directory 'full-name nil 'no-sort)))
70
71 (defun guix-emacs-find-autoloads (&optional profile)
72 "Return list of autoloads of Emacs packages installed in PROFILE.
73 If PROFILE is nil, use `guix-user-profile'.
74 Return nil if there are no emacs packages installed in PROFILE."
75 (let ((elisp-root-dir (guix-emacs-directory profile)))
76 (if (file-directory-p elisp-root-dir)
77 (let ((elisp-pkgs-dir (expand-file-name "guix.d" elisp-root-dir))
78 (root-autoloads (guix-emacs-find-autoloads-in-directory
79 elisp-root-dir)))
80 (if (file-directory-p elisp-pkgs-dir)
81 (let ((pkgs-autoloads
82 (cl-mapcan #'guix-emacs-find-autoloads-in-directory
83 (guix-emacs-subdirs elisp-pkgs-dir))))
84 (append root-autoloads pkgs-autoloads))
85 root-autoloads))
86 (message "Directory '%s' does not exist." elisp-root-dir)
87 nil)))
88
89 ;;;###autoload
90 (defun guix-emacs-load-autoloads (&optional profile)
91 "Load autoloads for Emacs packages installed in PROFILE.
92 If PROFILE is nil, use `guix-user-profile'.
93 Add autoloads directories to `load-path'."
94 (interactive (list (if (fboundp 'guix-profile-prompt)
95 (funcall 'guix-profile-prompt)
96 guix-user-profile)))
97 (let* ((autoloads (guix-emacs-find-autoloads profile))
98 (new-autoloads (cl-nset-difference autoloads
99 guix-emacs-autoloads
100 :test #'string=)))
101 (dolist (file new-autoloads)
102 (cl-pushnew (directory-file-name (file-name-directory file))
103 load-path
104 :test #'string=)
105 (load file 'noerror))
106 (setq guix-emacs-autoloads
107 (append new-autoloads guix-emacs-autoloads))))
108
109 (defun guix-emacs-load-autoloads-maybe ()
110 "Load autoloads for Emacs packages if needed.
111 See `guix-emacs-activate-after-operation' for details."
112 (and guix-emacs-activate-after-operation
113 ;; FIXME Since a user can work with a non-current profile (using
114 ;; C-u before `guix-search-by-name' and other commands), emacs
115 ;; packages can be installed to another profile, and the
116 ;; following code will not work (i.e., the autoloads for this
117 ;; profile will not be loaded).
118 (guix-emacs-load-autoloads guix-current-profile)))
119
120 (provide 'guix-emacs)
121
122 ;;; guix-emacs.el ends here