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