Klimi's new dotfiles with stow.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

214 řádky
8.0 KiB

před 4 roky
  1. ;;; use-package-ensure.el --- Support for the :ensure and :pin keywords -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2017 John Wiegley
  3. ;; Author: John Wiegley <johnw@newartisans.com>
  4. ;; Maintainer: John Wiegley <johnw@newartisans.com>
  5. ;; Created: 17 Jun 2012
  6. ;; Modified: 3 Dec 2017
  7. ;; Version: 1.0
  8. ;; Package-Requires: ((emacs "24.3") (use-package "2.4"))
  9. ;; Keywords: dotemacs startup speed config package
  10. ;; URL: https://github.com/jwiegley/use-package
  11. ;; This program is free software; you can redistribute it and/or
  12. ;; modify it under the terms of the GNU General Public License as
  13. ;; published by the Free Software Foundation; either version 3, or (at
  14. ;; your option) any later version.
  15. ;; This program is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. ;; General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23. ;;; Commentary:
  24. ;; Provides support for the :ensure and :pin keywords, which is made available
  25. ;; by default by requiring `use-package'.
  26. ;;; Code:
  27. (require 'cl-lib)
  28. (require 'use-package-core)
  29. (defgroup use-package-ensure nil
  30. "Support for :ensure and :pin keywords in use-package declarations."
  31. :group 'use-package)
  32. (eval-when-compile
  33. (declare-function package-installed-p "package")
  34. (declare-function package-read-all-archive-contents "package" ()))
  35. (defcustom use-package-always-ensure nil
  36. "Treat every package as though it had specified using `:ensure SEXP'.
  37. See also `use-package-defaults', which uses this value."
  38. :type 'sexp
  39. :group 'use-package-ensure)
  40. (defcustom use-package-always-pin nil
  41. "Treat every package as though it had specified using `:pin SYM'.
  42. See also `use-package-defaults', which uses this value."
  43. :type 'symbol
  44. :group 'use-package-ensure)
  45. (defcustom use-package-ensure-function 'use-package-ensure-elpa
  46. "Function that ensures a package is installed.
  47. This function is called with three arguments: the name of the
  48. package declared in the `use-package' form; the arguments passed
  49. to all `:ensure' keywords (always a list, even if only one); and
  50. the current `state' plist created by previous handlers.
  51. Note that this function is called whenever `:ensure' is provided,
  52. even if it is nil. It is up to the function to decide on the
  53. semantics of the various values for `:ensure'.
  54. This function should return non-nil if the package is installed.
  55. The default value uses package.el to install the package."
  56. :type '(choice (const :tag "package.el" use-package-ensure-elpa)
  57. (function :tag "Custom"))
  58. :group 'use-package-ensure)
  59. ;;;; :pin
  60. (defun use-package-normalize/:pin (_name keyword args)
  61. (use-package-only-one (symbol-name keyword) args
  62. #'(lambda (_label arg)
  63. (cond
  64. ((stringp arg) arg)
  65. ((use-package-non-nil-symbolp arg) (symbol-name arg))
  66. (t
  67. (use-package-error
  68. ":pin wants an archive name (a string)"))))))
  69. (eval-when-compile
  70. (defvar package-pinned-packages)
  71. (defvar package-archives))
  72. (defun use-package-archive-exists-p (archive)
  73. "Check if a given ARCHIVE is enabled.
  74. ARCHIVE can be a string or a symbol or 'manual to indicate a
  75. manually updated package."
  76. (if (member archive '(manual "manual"))
  77. 't
  78. (let ((valid nil))
  79. (dolist (pa package-archives)
  80. (when (member archive (list (car pa) (intern (car pa))))
  81. (setq valid 't)))
  82. valid)))
  83. (defun use-package-pin-package (package archive)
  84. "Pin PACKAGE to ARCHIVE."
  85. (unless (boundp 'package-pinned-packages)
  86. (setq package-pinned-packages ()))
  87. (let ((archive-symbol (if (symbolp archive) archive (intern archive)))
  88. (archive-name (if (stringp archive) archive (symbol-name archive))))
  89. (if (use-package-archive-exists-p archive-symbol)
  90. (add-to-list 'package-pinned-packages (cons package archive-name))
  91. (error "Archive '%s' requested for package '%s' is not available."
  92. archive-name package))
  93. (unless (bound-and-true-p package--initialized)
  94. (package-initialize t))))
  95. (defun use-package-handler/:pin (name _keyword archive-name rest state)
  96. (let ((body (use-package-process-keywords name rest state))
  97. (pin-form (if archive-name
  98. `(use-package-pin-package ',(use-package-as-symbol name)
  99. ,archive-name))))
  100. ;; Pinning should occur just before ensuring
  101. ;; See `use-package-handler/:ensure'.
  102. (if (bound-and-true-p byte-compile-current-file)
  103. (eval pin-form) ; Eval when byte-compiling,
  104. (push pin-form body)) ; or else wait until runtime.
  105. body))
  106. ;;;; :ensure
  107. (defvar package-archive-contents)
  108. ;;;###autoload
  109. (defun use-package-normalize/:ensure (_name keyword args)
  110. (if (null args)
  111. (list t)
  112. (use-package-only-one (symbol-name keyword) args
  113. #'(lambda (_label arg)
  114. (cond
  115. ((symbolp arg)
  116. (list arg))
  117. ((and (listp arg) (= 3 (length arg))
  118. (symbolp (nth 0 arg))
  119. (eq :pin (nth 1 arg))
  120. (or (stringp (nth 2 arg))
  121. (symbolp (nth 2 arg))))
  122. (list (cons (nth 0 arg) (nth 2 arg))))
  123. (t
  124. (use-package-error
  125. (concat ":ensure wants an optional package name "
  126. "(an unquoted symbol name), or (<symbol> :pin <string>)"))))))))
  127. (defun use-package-ensure-elpa (name args _state &optional _no-refresh)
  128. (dolist (ensure args)
  129. (let ((package
  130. (or (and (eq ensure t) (use-package-as-symbol name))
  131. ensure)))
  132. (when package
  133. (require 'package)
  134. (when (consp package)
  135. (use-package-pin-package (car package) (cdr package))
  136. (setq package (car package)))
  137. (unless (package-installed-p package)
  138. (condition-case-unless-debug err
  139. (progn
  140. (when (assoc package (bound-and-true-p
  141. package-pinned-packages))
  142. (package-read-all-archive-contents))
  143. (if (assoc package package-archive-contents)
  144. (package-install package)
  145. (package-refresh-contents)
  146. (when (assoc package (bound-and-true-p
  147. package-pinned-packages))
  148. (package-read-all-archive-contents))
  149. (package-install package))
  150. t)
  151. (error
  152. (display-warning 'use-package
  153. (format "Failed to install %s: %s"
  154. name (error-message-string err))
  155. :error))))))))
  156. ;;;###autoload
  157. (defun use-package-handler/:ensure (name _keyword ensure rest state)
  158. (let* ((body (use-package-process-keywords name rest state)))
  159. ;; We want to avoid installing packages when the `use-package' macro is
  160. ;; being macro-expanded by elisp completion (see `lisp--local-variables'),
  161. ;; but still install packages when byte-compiling, to avoid requiring
  162. ;; `package' at runtime.
  163. (if (bound-and-true-p byte-compile-current-file)
  164. ;; Eval when byte-compiling,
  165. (funcall use-package-ensure-function name ensure state)
  166. ;; or else wait until runtime.
  167. (push `(,use-package-ensure-function ',name ',ensure ',state)
  168. body))
  169. body))
  170. (add-to-list 'use-package-defaults
  171. '(:ensure (list use-package-always-ensure)
  172. (lambda (name args)
  173. (and use-package-always-ensure
  174. (not (plist-member args :load-path))))) t)
  175. (add-to-list 'use-package-defaults
  176. '(:pin use-package-always-pin use-package-always-pin) t)
  177. (add-to-list 'use-package-keywords :ensure)
  178. (add-to-list 'use-package-keywords :pin)
  179. (provide 'use-package-ensure)
  180. ;;; use-package-ensure.el ends here