gnu: chez-scheme: Remove use of 'ghostscript-gs' wrapper.
[jackhill/guix/guix.git] / emacs / guix-emacs.el
CommitLineData
12f2490a
AK
1;;; guix-emacs.el --- Emacs packages installed with Guix
2
e4e21546 3;; Copyright © 2014, 2015, 2016 Alex Kost <alezost@gmail.com>
12f2490a
AK
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
6078594b 27(require 'cl-lib)
e4e21546
AK
28(unless (require 'guix-profiles nil t)
29 (defvar guix-user-profile (expand-file-name "~/.guix-profile")))
12f2490a 30
6d268e52
AK
31(defcustom guix-package-enable-at-startup t
32 "If non-nil, activate Emacs packages installed in a user profile.
33Set this variable to nil before requiring `guix-emacs' file to
34avoid loading autoloads of Emacs packages installed in
35`guix-user-profile'."
36 :type 'boolean
37 :group 'guix)
38
12f2490a
AK
39(defvar guix-emacs-autoloads nil
40 "List of the last loaded Emacs autoloads.")
41
98f4a900
AK
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
12f2490a
AK
47(defun guix-emacs-directory (&optional profile)
48 "Return directory with Emacs packages installed in PROFILE.
49If PROFILE is nil, use `guix-user-profile'."
50 (expand-file-name "share/emacs/site-lisp"
51 (or profile guix-user-profile)))
52
abea77e3 53(defun guix-emacs-find-autoloads (directory)
98f4a900
AK
54 "Return a list of Emacs 'autoloads' files in DIRECTORY.
55The 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=))
77411390
AK
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
abea77e3
AK
72(defun guix-emacs-directories (&optional profile)
73 "Return the list of directories under PROFILE that contain Emacs packages.
74This includes both `share/emacs/site-lisp/guix.d/PACKAGE'
75sub-directories and `share/emacs/site-lisp' itself.
76
12f2490a 77If PROFILE is nil, use `guix-user-profile'.
abea77e3
AK
78Return 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)))))
12f2490a
AK
85
86;;;###autoload
abea77e3
AK
87(defun guix-emacs-autoload-packages (&rest profiles)
88 "Autoload Emacs packages installed in PROFILES.
004ea629
AK
89If PROFILES are not specified, use a default user and system
90profiles.
abea77e3
AK
91
92'Autoload' means add directories with Emacs packages to
93`load-path' and load 'autoloads' files matching
94`guix-emacs-autoloads-regexp'."
e4e21546
AK
95 (interactive (list (if (fboundp 'guix-profile-prompt)
96 (funcall 'guix-profile-prompt)
97 guix-user-profile)))
abea77e3 98 (let ((profiles (or profiles
004ea629
AK
99 (list "/run/current-system/profile"
100 guix-user-profile))))
abea77e3
AK
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))))))))
12f2490a 117
6d268e52 118(when guix-package-enable-at-startup
abea77e3 119 (guix-emacs-autoload-packages))
6d268e52 120
12f2490a
AK
121(provide 'guix-emacs)
122
123;;; guix-emacs.el ends here