diff options
Diffstat (limited to 'lisp/use-package/use-package.el')
-rw-r--r-- | lisp/use-package/use-package.el | 168 |
1 files changed, 99 insertions, 69 deletions
diff --git a/lisp/use-package/use-package.el b/lisp/use-package/use-package.el index a5195146df8..f933ce2d936 100644 --- a/lisp/use-package/use-package.el +++ b/lisp/use-package/use-package.el @@ -5,8 +5,8 @@ ;; Author: John Wiegley <jwiegley@gmail.com> ;; Maintainer: John Wiegley <jwiegley@gmail.com> ;; Created: 17 Jun 2012 -;; Modified: 26 Sep 2015 -;; Version: 2.1 +;; Modified: 17 Oct 2016 +;; Version: 2.3 ;; Package-Requires: ((bind-key "1.0") (diminish "0.44")) ;; Keywords: dotemacs startup speed config package ;; URL: https://github.com/jwiegley/use-package @@ -46,7 +46,8 @@ (eval-when-compile (require 'cl)) (eval-when-compile (require 'regexp-opt)) -(declare-function package-installed-p 'package) +(declare-function package-installed-p "package") +(declare-function package-read-all-archive-contents "package" ()) (defgroup use-package nil "A use-package declaration for simplifying your `.emacs'." @@ -79,6 +80,11 @@ The check is performed by looking for the module using `locate-library'." :type 'boolean :group 'use-package) +(defcustom use-package-always-demand nil + "If non-nil, assume `:demand t` unless `:defer t` is given." + :type 'boolean + :group 'use-package) + (defcustom use-package-always-ensure nil "Treat every package as though it had specified `:ensure SEXP`." :type 'sexp @@ -125,6 +131,7 @@ the user specified." (defcustom use-package-keywords '(:disabled + :preface :pin :ensure :if @@ -132,7 +139,6 @@ the user specified." :unless :requires :load-path - :preface :no-require :bind :bind* @@ -144,9 +150,9 @@ the user specified." :defines :functions :defer + :init :after :demand - :init :config :diminish :delight) @@ -176,20 +182,42 @@ Must be set before loading use-package." :type 'boolean :group 'use-package) +(defcustom use-package-ensure-function 'use-package-ensure-elpa + "Function that ensures a package is installed. +This function is called with one argument, the package name as a +symbol, by the `:ensure' keyword. + +The default value uses package.el to install the package." + :type '(choice (const :tag "package.el" use-package-ensure-elpa) + (function :tag "Custom")) + :group 'use-package) + +(defcustom use-package-defaults + '((:config '(t) t) + (:ensure use-package-always-ensure use-package-always-ensure) + (:pin use-package-always-pin use-package-always-pin)) + "Alist of default values for `use-package' keywords. +Each entry in the alist is a list of three elements. The first +element is the `use-package' keyword and the second is a form +that can be evaluated to get the default value. The third element +is a form that can be evaluated to determine whether or not to +assign a default value; if it evaluates to nil, then the default +value is not assigned even if the keyword is not present in the +`use-package' form." + :type '(repeat (list symbol sexp sexp))) + (when use-package-enable-imenu-support - ;; Not defined in Emacs 24 - (defvar lisp-mode-symbol-regexp - "\\(?:\\sw\\|\\s_\\|\\\\.\\)+") - (add-to-list - 'lisp-imenu-generic-expression - (list "Package" - (purecopy (concat "^\\s-*(" - (eval-when-compile - (regexp-opt - '("use-package" "require") - t)) - "\\s-+\\(" lisp-mode-symbol-regexp "\\)")) - 2))) + (eval-after-load 'lisp-mode + `(let ((sym-regexp (or (bound-and-true-p lisp-mode-symbol-regexp) + "\\(?:\\sw\\|\\s_\\|\\\\.\\)+"))) + (add-to-list + 'lisp-imenu-generic-expression + (list "Packages" + (concat "^\\s-*(" + ,(eval-when-compile + (regexp-opt '("use-package" "require") t)) + "\\s-+\\(" sym-regexp "\\)") + 2))))) (defvar use-package-form-regexp "^\\s-*(\\s-*use-package\\s-+\\_<%s\\_>" "Regexp used in `use-package-jump-to-package-form' to find use @@ -234,7 +262,7 @@ instead." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; Utility functions +;;; Utility functions ;; (defun use-package-as-symbol (string-or-symbol) @@ -253,8 +281,8 @@ convert it to a string and return that." "Return a form which will load or require NAME depending on whether it's a string or symbol." (if (stringp name) - `(load ,name 'noerror) - `(require ',name nil 'noerror))) + `(load ,name ',noerror) + `(require ',name nil ',noerror))) (defun use-package-expand (name label form) "FORM is a list of forms, so `((foo))' if only `foo' is being called." @@ -339,7 +367,7 @@ This is in contrast to merely setting it to 0." (let (p) (while plist (if (not (eq property (car plist))) - (setq p (plist-put p (car plist) (nth 1 plist)))) + (setq p (plist-put p (car plist) (nth 1 plist)))) (setq plist (cddr plist))) p)) @@ -391,12 +419,12 @@ This is in contrast to merely setting it to 0." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; Keyword processing +;;; Keyword processing ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; Normalization functions +;;; Normalization functions ;; (defun use-package-normalize-plist (name input) @@ -454,7 +482,7 @@ next value for the STATE." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :pin +;;; :pin ;; (defun use-package-only-one (label args f) @@ -524,7 +552,7 @@ manually updated package." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :ensure +;;; :ensure ;; (defvar package-archive-contents) (defun use-package-normalize/:ensure (name keyword args) @@ -539,8 +567,12 @@ manually updated package." "(an unquoted symbol name)"))))))) (defun use-package-ensure-elpa (package &optional no-refresh) + (require 'package) (if (package-installed-p package) t + (if (and (not no-refresh) + (assoc package (bound-and-true-p package-pinned-packages))) + (package-read-all-archive-contents)) (if (or (assoc package package-archive-contents) no-refresh) (package-install package) (progn @@ -550,9 +582,8 @@ manually updated package." (defun use-package-handler/:ensure (name keyword ensure rest state) (let* ((body (use-package-process-keywords name rest state)) (package-name (or (and (eq ensure t) (use-package-as-symbol name)) ensure)) - (ensure-form (if package-name - `(progn (require 'package) - (use-package-ensure-elpa ',package-name))))) + (ensure-form (when package-name + `(,use-package-ensure-function ',package-name)))) ;; We want to avoid installing packages when the `use-package' ;; macro is being macro-expanded by elisp completion (see ;; `lisp--local-variables'), but still do install packages when @@ -564,7 +595,7 @@ manually updated package." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :if, :when and :unless +;;; :if, :when and :unless ;; (defsubst use-package-normalize-value (label arg) @@ -595,7 +626,7 @@ manually updated package." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :requires +;;; :requires ;; (defun use-package-as-one (label args f) @@ -638,7 +669,7 @@ manually updated package." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :load-path +;;; :load-path ;; (defun use-package-normalize-paths (label arg &optional recursed) @@ -672,7 +703,7 @@ manually updated package." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :no-require +;;; :no-require ;; (defun use-package-normalize-predicate (name keyword args) @@ -689,7 +720,7 @@ manually updated package." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :preface +;;; :preface ;; (defun use-package-normalize-form (label args) @@ -716,7 +747,7 @@ manually updated package." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :bind, :bind* +;;; :bind, :bind* ;; (defsubst use-package-is-sympair (x &optional allow-vector) @@ -785,7 +816,7 @@ may also be a string, as accepted by `define-key'." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :bind-keymap, :bind-keymap* +;;; :bind-keymap, :bind-keymap* ;; (defalias 'use-package-normalize/:bind-keymap 'use-package-normalize-binder) @@ -842,7 +873,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :interpreter +;;; :interpreter ;; (defun use-package-normalize-mode (name keyword args) @@ -865,7 +896,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :mode +;;; :mode ;; (defalias 'use-package-normalize/:mode 'use-package-normalize-mode) @@ -884,7 +915,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :commands +;;; :commands ;; (defalias 'use-package-normalize/:commands 'use-package-normalize-symlist) @@ -898,7 +929,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :defines +;;; :defines ;; (defalias 'use-package-normalize/:defines 'use-package-normalize-symlist) @@ -909,7 +940,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :functions +;;; :functions ;; (defalias 'use-package-normalize/:functions 'use-package-normalize-symlist) @@ -928,7 +959,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :defer +;;; :defer ;; (defalias 'use-package-normalize/:defer 'use-package-normalize-predicate) @@ -962,7 +993,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :after +;;; :after ;; (defalias 'use-package-normalize/:after 'use-package-normalize-symlist) @@ -988,7 +1019,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :demand +;;; :demand ;; (defalias 'use-package-normalize/:demand 'use-package-normalize-predicate) @@ -999,7 +1030,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :init +;;; :init ;; (defalias 'use-package-normalize/:init 'use-package-normalize-forms) @@ -1019,7 +1050,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :config +;;; :config ;; (defalias 'use-package-normalize/:config 'use-package-normalize-forms) @@ -1054,7 +1085,8 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :diminish +;;; :diminish +;; (defun use-package-normalize-diminish (name label arg &optional recursed) "Normalize the arguments to diminish down to a list of one of two forms: @@ -1092,7 +1124,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; :delight +;;; :delight ;; (defun use-package-normalize/:delight (name keyword args) @@ -1118,7 +1150,7 @@ deferred until the prefix key sequence is pressed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; The main macro +;;; The main macro ;; ;;;###autoload @@ -1174,41 +1206,38 @@ this file. Usage: :pin Pin the package to an archive." (declare (indent 1)) (unless (member :disabled args) - (let* ((name-symbol (if (stringp name) (intern name) name)) - (args0 (use-package-plist-maybe-put - (use-package-normalize-plist name args) - :config '(t))) - (args* (use-package-sort-keywords - (if use-package-always-ensure - (use-package-plist-maybe-put - args0 :ensure use-package-always-ensure) - args0))) - (args* (use-package-sort-keywords - (if use-package-always-pin - (use-package-plist-maybe-put - args* :pin use-package-always-pin) - args*)))) + (let ((name-symbol (if (stringp name) (intern name) name)) + (args (use-package-normalize-plist name args))) + (dolist (spec use-package-defaults) + (setq args (use-package-sort-keywords + (if (eval (nth 2 spec)) + (use-package-plist-maybe-put + args (nth 0 spec) (eval (nth 1 spec))) + args)))) ;; When byte-compiling, pre-load the package so all its symbols are in ;; scope. (if (bound-and-true-p byte-compile-current-file) - (setq args* + (setq args (use-package-plist-cons - args* :preface + args :preface `(eval-when-compile ,@(mapcar #'(lambda (var) `(defvar ,var)) - (plist-get args* :defines)) + (plist-get args :defines)) (with-demoted-errors ,(format "Cannot load %s: %%S" name) ,(if (eq use-package-verbose 'debug) `(message "Compiling package %s" ',name-symbol)) - ,(unless (plist-get args* :no-require) + ,(unless (plist-get args :no-require) (use-package-load-name name))))))) (let ((body (macroexp-progn - (use-package-process-keywords name args* - (and use-package-always-defer '(:deferred t)))))) + (use-package-process-keywords name + (if use-package-always-demand + (append args '(:demand t)) + args) + (and use-package-always-defer (list :deferred t)))))) (if use-package-debug (display-buffer (save-current-buffer @@ -1226,6 +1255,7 @@ this file. Usage: (provide 'use-package) ;; Local Variables: +;; outline-regexp: ";;;\\(;* [^\s\t\n]\\|###autoload\\)\\|(" ;; indent-tabs-mode: nil ;; End: |