2018-03-20 16:40:26 +00:00
|
|
|
;;; use-package-ensure.el --- Support for the :ensure and :pin keywords -*- lexical-binding: t; -*-
|
2017-12-03 19:54:19 +00:00
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
;; Copyright (C) 2012-2024 Free Software Foundation, Inc.
|
2017-12-03 19:54:19 +00:00
|
|
|
|
|
|
|
;; Author: John Wiegley <johnw@newartisans.com>
|
|
|
|
;; Maintainer: John Wiegley <johnw@newartisans.com>
|
|
|
|
|
2022-12-08 22:56:24 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2022-11-16 07:37:27 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2017-12-03 19:54:19 +00:00
|
|
|
|
2022-12-08 22:56:24 +00:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2022-11-16 07:37:27 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2017-12-03 19:54:19 +00:00
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2022-12-08 22:56:24 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2017-12-03 19:54:19 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2022-12-08 23:24:15 +00:00
|
|
|
;; Provides support for the :ensure and :pin keywords, which are made
|
|
|
|
;; available by default by requiring `use-package'.
|
|
|
|
;;
|
|
|
|
;; See the `use-package' info manual for more information.
|
2017-12-03 19:54:19 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2017-12-04 19:26:19 +00:00
|
|
|
(require 'cl-lib)
|
2017-12-04 21:21:54 +00:00
|
|
|
(require 'use-package-core)
|
2017-12-03 19:54:19 +00:00
|
|
|
|
2017-12-03 21:11:49 +00:00
|
|
|
(eval-when-compile
|
|
|
|
(declare-function package-installed-p "package")
|
|
|
|
(declare-function package-read-all-archive-contents "package" ()))
|
|
|
|
|
2017-12-03 19:54:19 +00:00
|
|
|
;;;; :pin
|
|
|
|
|
2018-03-20 16:40:26 +00:00
|
|
|
(defun use-package-normalize/:pin (_name keyword args)
|
2017-12-03 19:54:19 +00:00
|
|
|
(use-package-only-one (symbol-name keyword) args
|
2018-03-20 16:40:26 +00:00
|
|
|
#'(lambda (_label arg)
|
2018-01-27 21:17:26 +00:00
|
|
|
(cond
|
|
|
|
((stringp arg) arg)
|
|
|
|
((use-package-non-nil-symbolp arg) (symbol-name arg))
|
|
|
|
(t
|
|
|
|
(use-package-error
|
|
|
|
":pin wants an archive name (a string)"))))))
|
2017-12-03 19:54:19 +00:00
|
|
|
|
|
|
|
(eval-when-compile
|
|
|
|
(defvar package-pinned-packages)
|
|
|
|
(defvar package-archives))
|
|
|
|
|
|
|
|
(defun use-package-archive-exists-p (archive)
|
|
|
|
"Check if a given ARCHIVE is enabled.
|
|
|
|
|
2022-08-08 11:33:17 +00:00
|
|
|
ARCHIVE can be a string or a symbol or `manual' to indicate a
|
2017-12-03 19:54:19 +00:00
|
|
|
manually updated package."
|
|
|
|
(if (member archive '(manual "manual"))
|
|
|
|
't
|
|
|
|
(let ((valid nil))
|
|
|
|
(dolist (pa package-archives)
|
|
|
|
(when (member archive (list (car pa) (intern (car pa))))
|
|
|
|
(setq valid 't)))
|
|
|
|
valid)))
|
|
|
|
|
|
|
|
(defun use-package-pin-package (package archive)
|
|
|
|
"Pin PACKAGE to ARCHIVE."
|
|
|
|
(unless (boundp 'package-pinned-packages)
|
|
|
|
(setq package-pinned-packages ()))
|
|
|
|
(let ((archive-symbol (if (symbolp archive) archive (intern archive)))
|
|
|
|
(archive-name (if (stringp archive) archive (symbol-name archive))))
|
|
|
|
(if (use-package-archive-exists-p archive-symbol)
|
|
|
|
(add-to-list 'package-pinned-packages (cons package archive-name))
|
2022-11-14 01:15:19 +00:00
|
|
|
(error "Archive '%s' requested for package '%s' is not available"
|
2017-12-03 19:54:19 +00:00
|
|
|
archive-name package))
|
|
|
|
(unless (bound-and-true-p package--initialized)
|
|
|
|
(package-initialize t))))
|
|
|
|
|
2018-03-20 16:40:26 +00:00
|
|
|
(defun use-package-handler/:pin (name _keyword archive-name rest state)
|
2017-12-03 19:54:19 +00:00
|
|
|
(let ((body (use-package-process-keywords name rest state))
|
|
|
|
(pin-form (if archive-name
|
|
|
|
`(use-package-pin-package ',(use-package-as-symbol name)
|
|
|
|
,archive-name))))
|
|
|
|
;; Pinning should occur just before ensuring
|
|
|
|
;; See `use-package-handler/:ensure'.
|
|
|
|
(if (bound-and-true-p byte-compile-current-file)
|
|
|
|
(eval pin-form) ; Eval when byte-compiling,
|
|
|
|
(push pin-form body)) ; or else wait until runtime.
|
|
|
|
body))
|
|
|
|
|
|
|
|
;;;; :ensure
|
|
|
|
|
|
|
|
(defvar package-archive-contents)
|
|
|
|
|
2017-12-05 18:28:28 +00:00
|
|
|
;;;###autoload
|
2018-03-20 16:40:26 +00:00
|
|
|
(defun use-package-normalize/:ensure (_name keyword args)
|
2017-12-03 19:54:19 +00:00
|
|
|
(if (null args)
|
2017-12-05 00:46:46 +00:00
|
|
|
(list t)
|
2017-12-03 19:54:19 +00:00
|
|
|
(use-package-only-one (symbol-name keyword) args
|
2018-03-20 16:40:26 +00:00
|
|
|
#'(lambda (_label arg)
|
2018-01-27 21:17:26 +00:00
|
|
|
(cond
|
|
|
|
((symbolp arg)
|
|
|
|
(list arg))
|
|
|
|
((and (listp arg) (= 3 (length arg))
|
|
|
|
(symbolp (nth 0 arg))
|
|
|
|
(eq :pin (nth 1 arg))
|
|
|
|
(or (stringp (nth 2 arg))
|
|
|
|
(symbolp (nth 2 arg))))
|
|
|
|
(list (cons (nth 0 arg) (nth 2 arg))))
|
|
|
|
(t
|
|
|
|
(use-package-error
|
|
|
|
(concat ":ensure wants an optional package name "
|
|
|
|
"(an unquoted symbol name), or (<symbol> :pin <string>)"))))))))
|
2018-01-27 16:19:42 +00:00
|
|
|
|
2018-03-20 16:40:26 +00:00
|
|
|
(defun use-package-ensure-elpa (name args _state &optional _no-refresh)
|
2017-12-04 19:26:19 +00:00
|
|
|
(dolist (ensure args)
|
|
|
|
(let ((package
|
|
|
|
(or (and (eq ensure t) (use-package-as-symbol name))
|
|
|
|
ensure)))
|
|
|
|
(when package
|
|
|
|
(require 'package)
|
2017-12-04 23:39:31 +00:00
|
|
|
(when (consp package)
|
|
|
|
(use-package-pin-package (car package) (cdr package))
|
|
|
|
(setq package (car package)))
|
2017-12-04 19:26:19 +00:00
|
|
|
(unless (package-installed-p package)
|
|
|
|
(condition-case-unless-debug err
|
|
|
|
(progn
|
2017-12-04 18:29:27 +00:00
|
|
|
(when (assoc package (bound-and-true-p
|
|
|
|
package-pinned-packages))
|
|
|
|
(package-read-all-archive-contents))
|
2017-12-04 19:26:19 +00:00
|
|
|
(if (assoc package package-archive-contents)
|
|
|
|
(package-install package)
|
|
|
|
(package-refresh-contents)
|
|
|
|
(when (assoc package (bound-and-true-p
|
|
|
|
package-pinned-packages))
|
|
|
|
(package-read-all-archive-contents))
|
|
|
|
(package-install package))
|
|
|
|
t)
|
|
|
|
(error
|
2017-12-08 06:40:48 +00:00
|
|
|
(display-warning 'use-package
|
|
|
|
(format "Failed to install %s: %s"
|
|
|
|
name (error-message-string err))
|
|
|
|
:error))))))))
|
2017-12-03 19:54:19 +00:00
|
|
|
|
2017-12-05 18:28:28 +00:00
|
|
|
;;;###autoload
|
2018-03-20 16:40:26 +00:00
|
|
|
(defun use-package-handler/:ensure (name _keyword ensure rest state)
|
2022-12-29 10:05:04 +00:00
|
|
|
(let* ((body (use-package-process-keywords name rest state))
|
|
|
|
(ensure (and (not (plist-member rest :vc)) ensure)))
|
2017-12-03 19:54:19 +00:00
|
|
|
;; We want to avoid installing packages when the `use-package' macro is
|
|
|
|
;; being macro-expanded by elisp completion (see `lisp--local-variables'),
|
|
|
|
;; but still install packages when byte-compiling, to avoid requiring
|
|
|
|
;; `package' at runtime.
|
|
|
|
(if (bound-and-true-p byte-compile-current-file)
|
|
|
|
;; Eval when byte-compiling,
|
|
|
|
(funcall use-package-ensure-function name ensure state)
|
|
|
|
;; or else wait until runtime.
|
|
|
|
(push `(,use-package-ensure-function ',name ',ensure ',state)
|
|
|
|
body))
|
|
|
|
body))
|
|
|
|
|
2017-12-04 21:21:54 +00:00
|
|
|
(provide 'use-package-ensure)
|
2017-12-04 23:24:31 +00:00
|
|
|
|
|
|
|
;;; use-package-ensure.el ends here
|