1 ;;; use-package.el --- A use-package declaration for simplifying your .emacs
3 ;; Copyright (C) 2012 John Wiegley
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Maintainer: John Wiegley <jwiegley@gmail.com>
7 ;; Created: 17 Jun 2012
8 ;; Modified: 26 Sep 2015
10 ;; Package-Version: 20160403.1129
11 ;; Package-Requires: ((bind-key "1.0") (diminish "0.44"))
12 ;; Keywords: dotemacs startup speed config package
13 ;; URL: https://github.com/jwiegley/use-package
15 ;; This program is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License as
17 ;; published by the Free Software Foundation; either version 2, or (at
18 ;; your option) any later version.
20 ;; This program is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
32 ;; The `use-package' declaration macro allows you to isolate package
33 ;; configuration in your ".emacs" in a way that is performance-oriented and,
34 ;; well, just tidy. I created it because I have over 80 packages that I use
35 ;; in Emacs, and things were getting difficult to manage. Yet with this
36 ;; utility my total load time is just under 1 second, with no loss of
39 ;; Please see README.md from the same repository for documentation.
45 (require 'diminish nil t)
47 (eval-when-compile (require 'cl))
49 (declare-function package-installed-p 'package)
51 (defgroup use-package nil
52 "A use-package declaration for simplifying your `.emacs'."
55 (defcustom use-package-verbose nil
56 "Whether to report about loading and configuration details.
58 If you customize this, then you should require the `use-package'
59 feature in files that use `use-package', even if these files only
60 contain compiled expansions of the macros. If you don't do so,
61 then the expanded macros do their job silently."
62 :type '(choice (const :tag "Quiet" nil) (const :tag "Verbose" t)
63 (const :tag "Debug" debug))
66 (defcustom use-package-debug nil
67 "Whether to display use-package expansions in a *use-package* buffer."
71 (defcustom use-package-check-before-init nil
72 "If non-nil, check that package exists before executing its `:init' block.
73 The check is performed by looking for the module using `locate-library'."
77 (defcustom use-package-always-defer nil
78 "If non-nil, assume `:defer t` unless `:demand t` is given."
82 (defcustom use-package-always-ensure nil
83 "Treat every package as though it had specified `:ensure SEXP`."
87 (defcustom use-package-always-pin nil
88 "Treat every package as though it had specified `:pin SYM."
92 (defcustom use-package-minimum-reported-time 0.1
93 "Minimal load time that will be reported.
95 Note that `use-package-verbose' has to be set to t, for anything
96 to be reported at all.
98 If you customize this, then you should require the `use-package'
99 feature in files that use `use-package', even if these files only
100 contain compiled expansions of the macros. If you don't do so,
101 then the expanded macros do their job silently."
105 (defcustom use-package-inject-hooks nil
106 "If non-nil, add hooks to the `:init' and `:config' sections.
107 In particular, for a given package `foo', the following hooks
110 `use-package--foo--pre-init-hook'
111 `use-package--foo--post-init-hook'
112 `use-package--foo--pre-config-hook'
113 `use-package--foo--post-config-hook'
115 This way, you can add to these hooks before evalaution of a
116 `use-package` declaration, and exercise some control over what
119 Note that if either `pre-init' hooks returns a nil value, that
120 block's user-supplied configuration is not evaluated, so be
121 certain to return `t' if you only wish to add behavior to what
126 (defcustom use-package-keywords
153 "Establish which keywords are valid, and the order they are processed in.
155 Note that `:disabled' is special, in that it causes nothing at all to happen,
156 even if the rest of the use-package declaration is incorrect."
157 :type '(repeat symbol)
160 (defcustom use-package-expand-minimally nil
161 "If non-nil, make the expanded code as minimal as possible.
163 - Printing to the *Messages* buffer of slowly-evaluating forms
164 - Capture of load errors (normally redisplayed as warnings)
165 - Conditional loading of packages (load failures become errors)
166 The only advantage is that, if you know your configuration works,
167 then your byte-compiled init file is as minimal as possible."
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 (defun use-package-as-symbol (string-or-symbol)
177 "If STRING-OR-SYMBOL is already a symbol, return it. Otherwise
178 convert it to a symbol and return that."
179 (if (symbolp string-or-symbol) string-or-symbol
180 (intern string-or-symbol)))
182 (defun use-package-as-string (string-or-symbol)
183 "If STRING-OR-SYMBOL is already a string, return it. Otherwise
184 convert it to a string and return that."
185 (if (stringp string-or-symbol) string-or-symbol
186 (symbol-name string-or-symbol)))
188 (defun use-package-load-name (name &optional noerror)
189 "Return a form which will load or require NAME depending on
190 whether it's a string or symbol."
192 `(load ,name 'noerror)
193 `(require ',name nil 'noerror)))
195 (defun use-package-expand (name label form)
196 "FORM is a list of forms, so `((foo))' if only `foo' is being called."
199 (if use-package-expand-minimally
201 (let ((err (make-symbol "err")))
203 `(condition-case-unless-debug ,err
204 ,(macroexp-progn form)
207 (display-warning 'use-package
209 ,name ,label (error-message-string ,err))
212 (put 'use-package-expand 'lisp-indent-function 'defun)
214 (defun use-package-hook-injector (name-string keyword body)
215 "Wrap pre/post hook injections around a given keyword form.
216 ARGS is a list of forms, so `((foo))' if only `foo' is being called."
217 (if (not use-package-inject-hooks)
218 (use-package-expand name-string (format "%s" keyword) body)
219 (let ((keyword-name (substring (format "%s" keyword) 1)))
221 `((when ,(macroexp-progn
222 (use-package-expand name-string (format "pre-%s hook" keyword)
223 `((run-hook-with-args-until-failure
224 ',(intern (concat "use-package--" name-string
225 "--pre-" keyword-name "-hook"))))))
227 (use-package-expand name-string (format "%s" keyword) body))
229 (use-package-expand name-string (format "post-%s hook" keyword)
231 ',(intern (concat "use-package--" name-string
232 "--post-" keyword-name "-hook"))))))))))))
234 (defun use-package--with-elapsed-timer (text body)
235 "BODY is a list of forms, so `((foo))' if only `foo' is being called."
237 (if use-package-expand-minimally
239 (let ((nowvar (make-symbol "now")))
240 (if (bound-and-true-p use-package-verbose)
241 `((let ((,nowvar (current-time)))
242 (message "%s..." ,text)
244 ,(macroexp-progn body)
246 (float-time (time-subtract (current-time) ,nowvar))))
247 (if (> elapsed ,use-package-minimum-reported-time)
248 (message "%s...done (%.3fs)" ,text elapsed)
249 (message "%s...done" ,text))))))
252 (put 'use-package--with-elapsed-timer 'lisp-indent-function 1)
254 (defsubst use-package-error (msg)
255 "Report MSG as an error, so the user knows it came from this package."
256 (error "use-package: %s" msg))
258 (defsubst use-package-plist-maybe-put (plist property value)
259 "Add a VALUE for PROPERTY to PLIST, if it does not already exist."
260 (if (plist-member plist property)
262 (plist-put plist property value)))
264 (defsubst use-package-plist-cons (plist property value)
265 "Cons VALUE onto the head of the list at PROPERTY in PLIST."
266 (plist-put plist property (cons value (plist-get plist property))))
268 (defsubst use-package-plist-append (plist property value)
269 "Append VALUE onto the front of the list at PROPERTY in PLIST."
270 (plist-put plist property (append value (plist-get plist property))))
272 (defun use-package-plist-delete (plist property)
273 "Delete PROPERTY from PLIST.
274 This is in contrast to merely setting it to 0."
277 (if (not (eq property (car plist)))
278 (setq p (plist-put p (car plist) (nth 1 plist))))
279 (setq plist (cddr plist)))
282 (defun use-package-split-list (pred xs)
283 (let ((ys (list nil)) (zs (list nil)) flip)
291 (nconc ys (list x)))))
292 (cons (cdr ys) (cdr zs))))
294 (defun use-package-keyword-index (keyword)
297 for k in use-package-keywords do
299 (return-from outer index))
302 (defun use-package-sort-keywords (plist)
305 (push (cons (car plist) (cadr plist))
307 (setq plist (cddr plist)))
312 #'(lambda (l r) (< (use-package-keyword-index (car l))
313 (use-package-keyword-index (car r)))))))
314 (setq result (cons (car x) (cons (cdr x) result))))
317 (defsubst use-package-concat (&rest elems)
318 "Delete all empty lists from ELEMS (nil or (list nil)), and append them."
319 (apply #'nconc (delete nil (delete (list nil) elems))))
321 (defconst use-package-font-lock-keywords
322 '(("(\\(use-package\\)\\_>[ \t']*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
323 (1 font-lock-keyword-face)
324 (2 font-lock-constant-face nil t))))
326 (font-lock-add-keywords 'emacs-lisp-mode use-package-font-lock-keywords)
328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 ;; Keyword processing
333 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
335 ;; Normalization functions
338 (defun use-package-normalize-plist (name input)
339 "Given a pseudo-plist, normalize it to a regular plist."
341 (let* ((keyword (car input))
342 (xs (use-package-split-list #'keywordp (cdr input)))
345 (normalizer (intern (concat "use-package-normalize/"
346 (symbol-name keyword))))
349 ((eq keyword :disabled)
350 (use-package-normalize-plist name tail))
351 ((functionp normalizer)
352 (funcall normalizer name keyword args))
357 (if (memq keyword use-package-keywords)
359 (cons arg (use-package-normalize-plist name tail)))
360 (use-package-error (format "Unrecognized keyword: %s" keyword))))))
362 (defun use-package-process-keywords (name plist &optional state)
363 "Process the next keyword in the free-form property list PLIST.
364 The values in the PLIST have each been normalized by the function
365 use-package-normalize/KEYWORD (minus the colon).
367 STATE is a property list that the function may modify and/or
368 query. This is useful if a package defines multiple keywords and
369 wishes them to have some kind of stateful interaction.
371 Unless the KEYWORD being processed intends to ignore remaining
372 keywords, it must call this function recursively, passing in the
373 plist with its keyword and argument removed, and passing in the
374 next value for the STATE."
377 (let* ((keyword (car plist))
380 (unless (keywordp keyword)
381 (use-package-error (format "%s is not a keyword" keyword)))
382 (let* ((handler (concat "use-package-handler/" (symbol-name keyword)))
383 (handler-sym (intern handler)))
384 (if (functionp handler-sym)
385 (funcall handler-sym name keyword arg rest state)
387 (format "Keyword handler not defined: %s" handler)))))))
389 (put 'use-package-process-keywords 'lisp-indent-function 'defun)
391 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
396 (defun use-package-only-one (label args f)
397 "Call F on the first member of ARGS if it has exactly one element."
400 ((and (listp args) (listp (cdr args))
402 (funcall f label (car args)))
405 (concat label " wants exactly one argument")))))
407 (put 'use-package-only-one 'lisp-indent-function 'defun)
409 (defun use-package-normalize/:pin (name keyword args)
410 (use-package-only-one (symbol-name keyword) args
414 ((symbolp arg) (symbol-name arg))
417 ":pin wants an archive name (a string)"))))))
420 (defvar package-pinned-packages)
421 (defvar package-archives))
423 (defun use-package--archive-exists-p (archive)
424 "Check if a given ARCHIVE is enabled.
426 ARCHIVE can be a string or a symbol or 'manual to indicate a
427 manually updated package."
428 (if (member archive '(manual "manual"))
431 (dolist (pa package-archives)
432 (when (member archive (list (car pa) (intern (car pa))))
436 (defun use-package-pin-package (package archive)
437 "Pin PACKAGE to ARCHIVE."
438 (unless (boundp 'package-pinned-packages)
439 (setq package-pinned-packages ()))
440 (let ((archive-symbol (if (symbolp archive) archive (intern archive)))
441 (archive-name (if (stringp archive) archive (symbol-name archive))))
442 (if (use-package--archive-exists-p archive-symbol)
443 (add-to-list 'package-pinned-packages (cons package archive-name))
444 (error "Archive '%s' requested for package '%s' is not available."
445 archive-name package))
446 (unless (bound-and-true-p package--initialized)
447 (package-initialize t))))
449 (defun use-package-handler/:pin (name keyword archive-name rest state)
450 (let ((body (use-package-process-keywords name rest state))
451 (pin-form (if archive-name
452 `(use-package-pin-package ',(use-package-as-symbol name)
454 ;; Pinning should occur just before ensuring
455 ;; See `use-package-handler/:ensure'.
456 (if (bound-and-true-p byte-compile-current-file)
457 (eval pin-form) ; Eval when byte-compiling,
458 (push pin-form body)) ; or else wait until runtime.
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 (defvar package-archive-contents)
466 (defun use-package-normalize/:ensure (name keyword args)
469 (use-package-only-one (symbol-name keyword) args
474 (concat ":ensure wants an optional package name "
475 "(an unquoted symbol name)")))))))
477 (defun use-package-ensure-elpa (package &optional no-refresh)
478 (if (package-installed-p package)
480 (if (or (assoc package package-archive-contents) no-refresh)
481 (package-install package)
483 (package-refresh-contents)
484 (use-package-ensure-elpa package t)))))
486 (defun use-package-handler/:ensure (name keyword ensure rest state)
487 (let* ((body (use-package-process-keywords name rest state))
488 (package-name (or (and (eq ensure t) (use-package-as-symbol name)) ensure))
489 (ensure-form (if package-name
490 `(progn (require 'package)
491 (use-package-ensure-elpa ',package-name)))))
492 ;; We want to avoid installing packages when the `use-package'
493 ;; macro is being macro-expanded by elisp completion (see
494 ;; `lisp--local-variables'), but still do install packages when
495 ;; byte-compiling to avoid requiring `package' at runtime.
496 (if (bound-and-true-p byte-compile-current-file)
497 (eval ensure-form) ; Eval when byte-compiling,
498 (push ensure-form body)) ; or else wait until runtime.
501 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
503 ;; :if, :when and :unless
506 (defsubst use-package-normalize-value (label arg)
509 `(symbol-value ',arg))
514 (defun use-package-normalize-test (name keyword args)
515 (use-package-only-one (symbol-name keyword) args
516 #'use-package-normalize-value))
518 (defalias 'use-package-normalize/:if 'use-package-normalize-test)
519 (defalias 'use-package-normalize/:when 'use-package-normalize-test)
520 (defalias 'use-package-normalize/:unless 'use-package-normalize-test)
522 (defun use-package-handler/:if (name keyword pred rest state)
523 (let ((body (use-package-process-keywords name rest state)))
524 `((when ,pred ,@body))))
526 (defalias 'use-package-handler/:when 'use-package-handler/:if)
528 (defun use-package-handler/:unless (name keyword pred rest state)
529 (let ((body (use-package-process-keywords name rest state)))
530 `((unless ,pred ,@body))))
532 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
537 (defun use-package-as-one (label args f)
538 "Call F on the first element of ARGS if it has one element, or all of ARGS."
540 (if (and (listp args) (listp (cdr args)))
541 (if (= (length args) 1)
542 (funcall f label (car args))
543 (funcall f label args))
545 (concat label " wants a list"))))
547 (put 'use-package-as-one 'lisp-indent-function 'defun)
549 (defun use-package-normalize-symbols (label arg &optional recursed)
550 "Normalize a list of symbols."
554 ((and (not recursed) (listp arg) (listp (cdr arg)))
555 (mapcar #'(lambda (x) (car (use-package-normalize-symbols label x t))) arg))
558 (concat label " wants a symbol, or list of symbols")))))
560 (defun use-package-normalize-symlist (name keyword args)
561 (use-package-as-one (symbol-name keyword) args
562 #'use-package-normalize-symbols))
564 (defalias 'use-package-normalize/:requires 'use-package-normalize-symlist)
566 (defun use-package-handler/:requires (name keyword requires rest state)
567 (let ((body (use-package-process-keywords name rest state)))
570 `((when ,(if (listp requires)
571 `(not (member nil (mapcar #'featurep ',requires)))
572 `(featurep ',requires))
575 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
580 (defun use-package-normalize-paths (label arg &optional recursed)
581 "Normalize a list of filesystem paths."
583 ((and arg (or (symbolp arg) (functionp arg)))
584 (let ((value (use-package-normalize-value label arg)))
585 (use-package-normalize-paths label (eval value))))
587 (let ((path (if (file-name-absolute-p arg)
589 (expand-file-name arg user-emacs-directory))))
591 ((and (not recursed) (listp arg) (listp (cdr arg)))
592 (mapcar #'(lambda (x)
593 (car (use-package-normalize-paths label x t))) arg))
596 (concat label " wants a directory path, or list of paths")))))
598 (defun use-package-normalize/:load-path (name keyword args)
599 (use-package-as-one (symbol-name keyword) args
600 #'use-package-normalize-paths))
602 (defun use-package-handler/:load-path (name keyword arg rest state)
603 (let ((body (use-package-process-keywords name rest state)))
605 (mapcar #'(lambda (path)
606 `(eval-and-compile (add-to-list 'load-path ,path))) arg)
609 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
614 (defun use-package-normalize-predicate (name keyword args)
617 (use-package-only-one (symbol-name keyword) args
618 #'use-package-normalize-value)))
620 (defalias 'use-package-normalize/:no-require 'use-package-normalize-predicate)
622 (defun use-package-handler/:no-require (name keyword arg rest state)
623 ;; This keyword has no functional meaning.
624 (use-package-process-keywords name rest state))
626 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
631 (defun use-package-normalize-form (label args)
632 "Given a list of forms, return it wrapped in `progn'."
633 (unless (listp (car args))
634 (use-package-error (concat label " wants a sexp or list of sexps")))
635 (mapcar #'(lambda (form)
636 (if (and (consp form)
637 (eq (car form) 'use-package))
641 (defun use-package-normalize-forms (name keyword args)
642 (use-package-normalize-form (symbol-name keyword) args))
644 (defalias 'use-package-normalize/:preface 'use-package-normalize-forms)
646 (defun use-package-handler/:preface (name keyword arg rest state)
647 (let ((body (use-package-process-keywords name rest state)))
650 `((eval-and-compile ,@arg)))
653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
658 (defsubst use-package-is-sympair (x &optional allow-vector)
659 "Return t if X has the type (STRING . SYMBOL)."
661 (or (stringp (car x))
662 (and allow-vector (vectorp (car x))))
665 (defsubst use-package-is-string-pair (x)
666 "Return t if X has the type (STRING . STRING)."
671 (defun use-package-normalize-pairs
672 (name label arg &optional recursed allow-vector allow-string-cdrs)
673 "Normalize a list of string/symbol pairs.
674 If RECURSED is non-nil, recurse into sublists.
675 If ALLOW-VECTOR is non-nil, then the key to bind may specify a
676 vector of keys, as accepted by `define-key'.
677 If ALLOW-STRING-CDRS is non-nil, then the command name to bind to
678 may also be a string, as accepted by `define-key'."
680 ((or (stringp arg) (and allow-vector (vectorp arg)))
681 (list (cons arg (use-package-as-symbol name))))
682 ((use-package-is-sympair arg allow-vector)
684 ((and (not recursed) (listp arg) (listp (cdr arg)))
685 (mapcar #'(lambda (x)
686 (let ((ret (use-package-normalize-pairs
687 name label x t allow-vector allow-string-cdrs)))
691 ((and allow-string-cdrs (use-package-is-string-pair arg))
695 (defun use-package-normalize-binder (name keyword args)
696 (use-package-as-one (symbol-name keyword) args
698 (use-package-normalize-pairs name label arg nil t t))))
700 (defalias 'use-package-normalize/:bind 'use-package-normalize-binder)
701 (defalias 'use-package-normalize/:bind* 'use-package-normalize-binder)
703 (defun use-package-handler/:bind
704 (name keyword arg rest state &optional bind-macro)
705 (let ((commands (remq nil (mapcar #'(lambda (arg)
710 (use-package-process-keywords name
711 (use-package-sort-keywords
712 (use-package-plist-maybe-put rest :defer t))
713 (use-package-plist-append state :commands commands))
716 `(,(if bind-macro bind-macro 'bind-keys)
717 :package ,name ,@arg)))))))
719 (defun use-package-handler/:bind* (name keyword arg rest state)
720 (use-package-handler/:bind name keyword arg rest state 'bind-keys*))
722 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
724 ;; :bind-keymap, :bind-keymap*
727 (defalias 'use-package-normalize/:bind-keymap 'use-package-normalize-binder)
728 (defalias 'use-package-normalize/:bind-keymap* 'use-package-normalize-binder)
730 (defun use-package-autoload-keymap (keymap-symbol package override)
731 "Loads PACKAGE and then binds the key sequence used to invoke
732 this function to KEYMAP-SYMBOL. It then simulates pressing the
733 same key sequence a again, so that the next key pressed is routed
734 to the newly loaded keymap.
736 This function supports use-package's :bind-keymap keyword. It
737 works by binding the given key sequence to an invocation of this
738 function for a particular keymap. The keymap is expected to be
739 defined by the package. In this way, loading the package is
740 deferred until the prefix key sequence is pressed."
741 (if (not (require package nil t))
742 (use-package-error (format "Cannot load package.el: %s" package))
743 (if (and (boundp keymap-symbol)
744 (keymapp (symbol-value keymap-symbol)))
745 (let* ((kv (this-command-keys-vector))
746 (key (key-description kv))
747 (keymap (symbol-value keymap-symbol)))
749 (bind-key* key keymap)
750 (bind-key key keymap))
751 (setq unread-command-events
752 (listify-key-sequence kv)))
754 (format "use-package: package.el %s failed to define keymap %s"
755 package keymap-symbol)))))
757 (defun use-package-handler/:bind-keymap
758 (name keyword arg rest state &optional override)
767 (use-package-autoload-keymap
768 ',(cdr binding) ',(use-package-as-symbol name) ,override)))) arg)))
770 (use-package-process-keywords name
771 (use-package-sort-keywords
772 (use-package-plist-maybe-put rest :defer t))
774 `((ignore ,@form)))))
776 (defun use-package-handler/:bind-keymap* (name keyword arg rest state)
777 (use-package-handler/:bind-keymap name keyword arg rest state t))
779 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
784 (defun use-package-normalize-mode (name keyword args)
785 (use-package-as-one (symbol-name keyword) args
786 (apply-partially #'use-package-normalize-pairs name)))
788 (defalias 'use-package-normalize/:interpreter 'use-package-normalize-mode)
790 (defun use-package-handler/:interpreter (name keyword arg rest state)
792 (form (mapcar #'(lambda (interpreter)
793 (push (cdr interpreter) commands)
794 `(add-to-list 'interpreter-mode-alist ',interpreter)) arg)))
796 (use-package-process-keywords name
797 (use-package-sort-keywords
798 (use-package-plist-maybe-put rest :defer t))
799 (use-package-plist-append state :commands commands))
800 `((ignore ,@form)))))
802 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
807 (defalias 'use-package-normalize/:mode 'use-package-normalize-mode)
809 (defun use-package-handler/:mode (name keyword arg rest state)
811 (form (mapcar #'(lambda (mode)
812 (push (cdr mode) commands)
813 `(add-to-list 'auto-mode-alist ',mode)) arg)))
815 (use-package-process-keywords name
816 (use-package-sort-keywords
817 (use-package-plist-maybe-put rest :defer t))
818 (use-package-plist-append state :commands commands))
819 `((ignore ,@form)))))
821 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
826 (defalias 'use-package-normalize/:commands 'use-package-normalize-symlist)
828 (defun use-package-handler/:commands (name keyword arg rest state)
829 ;; The actual processing for commands is done in :defer
830 (use-package-process-keywords name
831 (use-package-sort-keywords
832 (use-package-plist-maybe-put rest :defer t))
833 (use-package-plist-append state :commands arg)))
835 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
840 (defalias 'use-package-normalize/:defines 'use-package-normalize-symlist)
842 (defun use-package-handler/:defines (name keyword arg rest state)
843 (let ((body (use-package-process-keywords name rest state)))
846 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
851 (defalias 'use-package-normalize/:functions 'use-package-normalize-symlist)
853 (defun use-package-handler/:functions (name keyword arg rest state)
854 (let ((body (use-package-process-keywords name rest state)))
855 (if (not (bound-and-true-p byte-compile-current-file))
862 `(declare-function ,fn ,(use-package-as-string name))) arg))))
865 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
870 (defalias 'use-package-normalize/:defer 'use-package-normalize-predicate)
872 (defun use-package-handler/:defer (name keyword arg rest state)
873 (let ((body (use-package-process-keywords name rest
874 (plist-put state :deferred t)))
875 (name-string (use-package-as-string name)))
877 ;; Load the package after a set amount of idle time, if the argument to
878 ;; `:defer' was a number.
880 `((run-with-idle-timer ,arg nil #'require ',(use-package-as-symbol name) nil t)))
882 ;; Since we deferring load, establish any necessary autoloads, and also
883 ;; keep the byte-compiler happy.
886 (mapcar #'(lambda (command)
887 (when (not (stringp command))
889 `((unless (fboundp ',command)
890 (autoload #',command ,name-string nil t)))
891 (when (bound-and-true-p byte-compile-current-file)
893 (declare-function ,command ,name-string)))))))
894 (delete-dups (plist-get state :commands))))
899 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
904 (defalias 'use-package-normalize/:after 'use-package-normalize-symlist)
906 (defun use-package-require-after-load (features name)
907 "Return form for after any of FEATURES require NAME."
913 (quote (require (quote ,name)))))
916 (defun use-package-handler/:after (name keyword arg rest state)
917 (let ((body (use-package-process-keywords name rest
918 (plist-put state :deferred t)))
919 (name-string (use-package-as-string name)))
922 (list (use-package-require-after-load arg name)))
925 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
930 (defalias 'use-package-normalize/:demand 'use-package-normalize-predicate)
932 (defun use-package-handler/:demand (name keyword arg rest state)
933 (use-package-process-keywords name rest
934 (use-package-plist-delete state :deferred)))
936 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
941 (defalias 'use-package-normalize/:init 'use-package-normalize-forms)
943 (defun use-package-handler/:init (name keyword arg rest state)
944 (let ((body (use-package-process-keywords name rest state)))
946 ;; The user's initializations
948 (use-package-hook-injector (use-package-as-string name)
950 (if use-package-check-before-init
951 `((if (locate-library ,(use-package-as-string name))
952 ,(macroexp-progn init-body)))
956 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
961 (defalias 'use-package-normalize/:config 'use-package-normalize-forms)
963 (defun use-package-handler/:config (name keyword arg rest state)
964 (let* ((body (use-package-process-keywords name rest state))
965 (name-symbol (use-package-as-symbol name))
969 (use-package--with-elapsed-timer
970 (format "Configuring package %s" name-symbol)
972 (use-package-hook-injector (symbol-name name-symbol)
976 (if (plist-get state :deferred)
977 (unless (or (null config-body) (equal config-body '(t)))
978 `((eval-after-load ,(if (symbolp name) `',name name)
979 ',(macroexp-progn config-body))))
980 (use-package--with-elapsed-timer
981 (format "Loading package %s" name)
982 (if use-package-expand-minimally
984 (list (use-package-load-name name))
986 `((if (not ,(use-package-load-name name t))
988 (message (format "Cannot load %s" ',name)))
991 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
995 (defun use-package-normalize-diminish (name label arg &optional recursed)
996 "Normalize the arguments to diminish down to a list of one of two forms:
1003 (list (cons (intern (concat (use-package-as-string name) "-mode")) arg)))
1004 ((and (consp arg) (stringp (cdr arg)))
1006 ((and (not recursed) (listp arg) (listp (cdr arg)))
1007 (mapcar #'(lambda (x) (car (use-package-normalize-diminish
1008 name label x t))) arg))
1011 (concat label " wants a string, symbol, "
1012 "(symbol . string) or list of these")))))
1014 (defun use-package-normalize/:diminish (name keyword args)
1015 (use-package-as-one (symbol-name keyword) args
1016 (apply-partially #'use-package-normalize-diminish name)))
1018 (defun use-package-handler/:diminish (name keyword arg rest state)
1019 (let ((body (use-package-process-keywords name rest state)))
1021 (mapcar #'(lambda (var)
1022 `(if (fboundp 'diminish)
1024 `(diminish ',(car var) ,(cdr var))
1025 `(diminish ',var))))
1029 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1034 (defun use-package-normalize/:delight (name keyword args)
1035 "Normalize arguments to delight."
1037 ((and (= (length args) 1)
1038 (symbolp (car args)))
1039 (list (car args) nil name))
1040 ((and (= (length args) 2)
1041 (symbolp (car args)))
1042 (list (car args) (cadr args) (use-package-as-symbol name)))
1043 ((and (= (length args) 3)
1044 (symbolp (car args)))
1047 (use-package-error ":delight expects same args as delight function"))))
1049 (defun use-package-handler/:delight (name keyword args rest state)
1050 (let ((body (use-package-process-keywords name rest state)))
1053 `((delight (quote ,(nth 0 args)) ,(nth 1 args) (quote ,(nth 2 args))) t))))
1055 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1061 (defmacro use-package (name &rest args)
1062 "Declare an Emacs package by specifying a group of configuration options.
1064 For full documentation, please see the README file that came with
1067 (use-package package-name
1068 [:keyword [option]]...)
1070 :init Code to run before PACKAGE-NAME has been loaded.
1071 :config Code to run after PACKAGE-NAME has been loaded. Note that if
1072 loading is deferred for any reason, this code does not execute
1073 until the lazy load has occurred.
1074 :preface Code to be run before everything except `:disabled'; this can
1075 be used to define functions for use in `:if', or that should be
1076 seen by the byte-compiler.
1078 :mode Form to be added to `auto-mode-alist'.
1079 :interpreter Form to be added to `interpreter-mode-alist'.
1081 :commands Define autoloads for commands that will be defined by the
1082 package. This is useful if the package is being lazily loaded,
1083 and you wish to conditionally call functions in your `:init'
1084 block that are defined in the package.
1086 :bind Bind keys, and define autoloads for the bound commands.
1087 :bind* Bind keys, and define autoloads for the bound commands,
1088 *overriding all minor mode bindings*.
1089 :bind-keymap Bind a key prefix to an auto-loaded keymap defined in the
1090 package. This is like `:bind', but for keymaps.
1091 :bind-keymap* Like `:bind-keymap', but overrides all minor mode bindings
1093 :defer Defer loading of a package -- this is implied when using
1094 `:commands', `:bind', `:bind*', `:mode' or `:interpreter'.
1095 This can be an integer, to force loading after N seconds of
1096 idle time, if the package has not already been loaded.
1098 :after Defer loading of a package until after any of the named
1099 features are loaded.
1101 :demand Prevent deferred loading in all cases.
1103 :if EXPR Initialize and load only if EXPR evaluates to a non-nil value.
1104 :disabled The package is ignored completely if this keyword is present.
1105 :defines Declare certain variables to silence the byte-compiler.
1106 :functions Declare certain functions to silence the byte-compiler.
1107 :load-path Add to the `load-path' before attempting to load the package.
1108 :diminish Support for diminish.el (if installed).
1109 :ensure Loads the package using package.el if necessary.
1110 :pin Pin the package to an archive."
1111 (declare (indent 1))
1112 (unless (member :disabled args)
1113 (let* ((name-symbol (if (stringp name) (intern name) name))
1114 (args0 (use-package-plist-maybe-put
1115 (use-package-normalize-plist name args)
1117 (args* (use-package-sort-keywords
1118 (if use-package-always-ensure
1119 (use-package-plist-maybe-put
1120 args0 :ensure use-package-always-ensure)
1122 (args* (use-package-sort-keywords
1123 (if use-package-always-pin
1124 (use-package-plist-maybe-put
1125 args* :pin use-package-always-pin)
1128 ;; When byte-compiling, pre-load the package so all its symbols are in
1130 (if (bound-and-true-p byte-compile-current-file)
1132 (use-package-plist-cons
1135 ,@(mapcar #'(lambda (var) `(defvar ,var))
1136 (plist-get args* :defines))
1137 (with-demoted-errors
1138 ,(format "Cannot load %s: %%S" name)
1139 ,(if (eq use-package-verbose 'debug)
1140 `(message "Compiling package %s" ',name-symbol))
1141 ,(unless (plist-get args* :no-require)
1142 (use-package-load-name name)))))))
1146 (use-package-process-keywords name args*
1147 (and use-package-always-defer '(:deferred t))))))
1148 (if use-package-debug
1150 (save-current-buffer
1151 (let ((buf (get-buffer-create "*use-package*")))
1152 (with-current-buffer buf
1153 (delete-region (point-min) (point-max))
1155 (insert (pp-to-string body)))
1160 (put 'use-package 'lisp-indent-function 'defun)
1162 (provide 'use-package)
1165 ;; indent-tabs-mode: nil
1168 ;;; use-package.el ends here