add gitigonre file
[dotfiles.git] / emacs.d_2 / elpa / use-package-20160403.1129 / use-package.el
1 ;;; use-package.el --- A use-package declaration for simplifying your .emacs
2
3 ;; Copyright (C) 2012 John Wiegley
4
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Maintainer: John Wiegley <jwiegley@gmail.com>
7 ;; Created: 17 Jun 2012
8 ;; Modified: 26 Sep 2015
9 ;; Version: 2.1
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
14
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.
19
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.
24
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.
29
30 ;;; Commentary:
31
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
37 ;; functionality!
38 ;;
39 ;; Please see README.md from the same repository for documentation.
40
41 ;;; Code:
42
43 (require 'bind-key)
44 (require 'bytecomp)
45 (require 'diminish nil t)
46 (require 'bytecomp)
47 (eval-when-compile (require 'cl))
48
49 (declare-function package-installed-p 'package)
50
51 (defgroup use-package nil
52   "A use-package declaration for simplifying your `.emacs'."
53   :group 'startup)
54
55 (defcustom use-package-verbose nil
56   "Whether to report about loading and configuration details.
57
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))
64   :group 'use-package)
65
66 (defcustom use-package-debug nil
67   "Whether to display use-package expansions in a *use-package* buffer."
68   :type 'boolean
69   :group 'use-package)
70
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'."
74   :type 'boolean
75   :group 'use-package)
76
77 (defcustom use-package-always-defer nil
78   "If non-nil, assume `:defer t` unless `:demand t` is given."
79   :type 'boolean
80   :group 'use-package)
81
82 (defcustom use-package-always-ensure nil
83   "Treat every package as though it had specified `:ensure SEXP`."
84   :type 'sexp
85   :group 'use-package)
86
87 (defcustom use-package-always-pin nil
88   "Treat every package as though it had specified `:pin SYM."
89   :type 'symbol
90   :group 'use-package)
91
92 (defcustom use-package-minimum-reported-time 0.1
93   "Minimal load time that will be reported.
94
95 Note that `use-package-verbose' has to be set to t, for anything
96 to be reported at all.
97
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."
102   :type 'number
103   :group 'use-package)
104
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
108 become available:
109
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'
114
115 This way, you can add to these hooks before evalaution of a
116 `use-package` declaration, and exercise some control over what
117 happens.
118
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
122 the user specified."
123   :type 'boolean
124   :group 'use-package)
125
126 (defcustom use-package-keywords
127   '(:disabled
128     :pin
129     :ensure
130     :if
131     :when
132     :unless
133     :requires
134     :load-path
135     :preface
136     :no-require
137     :bind
138     :bind*
139     :bind-keymap
140     :bind-keymap*
141     :interpreter
142     :mode
143     :commands
144     :defines
145     :functions
146     :defer
147     :after
148     :demand
149     :init
150     :config
151     :diminish
152     :delight)
153   "Establish which keywords are valid, and the order they are processed in.
154
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)
158   :group 'use-package)
159
160 (defcustom use-package-expand-minimally nil
161   "If non-nil, make the expanded code as minimal as possible.
162 This disables:
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."
168   :type 'boolean
169   :group 'use-package)
170
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 ;;
173 ;; Utility functions
174 ;;
175
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)))
181
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)))
187
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."
191   (if (stringp name)
192       `(load ,name 'noerror)
193     `(require ',name nil 'noerror)))
194
195 (defun use-package-expand (name label form)
196   "FORM is a list of forms, so `((foo))' if only `foo' is being called."
197   (declare (indent 1))
198   (when form
199     (if use-package-expand-minimally
200         form
201       (let ((err (make-symbol "err")))
202         (list
203          `(condition-case-unless-debug ,err
204               ,(macroexp-progn form)
205             (error
206              (ignore
207               (display-warning 'use-package
208                                (format "%s %s: %s"
209                                        ,name ,label (error-message-string ,err))
210                                :error)))))))))
211
212 (put 'use-package-expand 'lisp-indent-function 'defun)
213
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)))
220       (when body
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"))))))
226             ,(macroexp-progn
227               (use-package-expand name-string (format "%s" keyword) body))
228             ,(macroexp-progn
229               (use-package-expand name-string (format "post-%s hook" keyword)
230                 `((run-hooks
231                    ',(intern (concat "use-package--" name-string
232                                      "--post-" keyword-name "-hook"))))))))))))
233
234 (defun use-package--with-elapsed-timer (text body)
235   "BODY is a list of forms, so `((foo))' if only `foo' is being called."
236   (declare (indent 1))
237   (if use-package-expand-minimally
238       body
239     (let ((nowvar (make-symbol "now")))
240       (if (bound-and-true-p use-package-verbose)
241           `((let ((,nowvar (current-time)))
242               (message "%s..." ,text)
243               (prog1
244                   ,(macroexp-progn body)
245                 (let ((elapsed
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))))))
250         body))))
251
252 (put 'use-package--with-elapsed-timer 'lisp-indent-function 1)
253
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))
257
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)
261       plist
262     (plist-put plist property value)))
263
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))))
267
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))))
271
272 (defun use-package-plist-delete (plist property)
273   "Delete PROPERTY from PLIST.
274 This is in contrast to merely setting it to 0."
275   (let (p)
276     (while plist
277       (if (not (eq property (car plist)))
278           (setq p (plist-put p (car plist) (nth 1 plist))))
279       (setq plist (cddr plist)))
280     p))
281
282 (defun use-package-split-list (pred xs)
283   (let ((ys (list nil)) (zs (list nil)) flip)
284     (dolist (x xs)
285       (if flip
286           (nconc zs (list x))
287         (if (funcall pred x)
288             (progn
289               (setq flip t)
290               (nconc zs (list x)))
291           (nconc ys (list x)))))
292     (cons (cdr ys) (cdr zs))))
293
294 (defun use-package-keyword-index (keyword)
295   (loop named outer
296         with index = 0
297         for k in use-package-keywords do
298         (if (eq k keyword)
299             (return-from outer index))
300         (incf index)))
301
302 (defun use-package-sort-keywords (plist)
303   (let (plist-grouped)
304     (while plist
305       (push (cons (car plist) (cadr plist))
306             plist-grouped)
307       (setq plist (cddr plist)))
308     (let (result)
309       (dolist (x
310                (nreverse
311                 (sort plist-grouped
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))))
315       result)))
316
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))))
320
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))))
325
326 (font-lock-add-keywords 'emacs-lisp-mode use-package-font-lock-keywords)
327
328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 ;;
330 ;; Keyword processing
331 ;;
332
333 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
334 ;;
335 ;; Normalization functions
336 ;;
337
338 (defun use-package-normalize-plist (name input)
339   "Given a pseudo-plist, normalize it to a regular plist."
340   (unless (null input)
341     (let* ((keyword (car input))
342            (xs (use-package-split-list #'keywordp (cdr input)))
343            (args (car xs))
344            (tail (cdr xs))
345            (normalizer (intern (concat "use-package-normalize/"
346                                        (symbol-name keyword))))
347            (arg
348             (cond
349              ((eq keyword :disabled)
350               (use-package-normalize-plist name tail))
351              ((functionp normalizer)
352               (funcall normalizer name keyword args))
353              ((= (length args) 1)
354               (car args))
355              (t
356               args))))
357       (if (memq keyword use-package-keywords)
358           (cons keyword
359                 (cons arg (use-package-normalize-plist name tail)))
360         (use-package-error (format "Unrecognized keyword: %s" keyword))))))
361
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).
366
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.
370
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."
375   (declare (indent 1))
376   (unless (null plist)
377     (let* ((keyword (car plist))
378            (arg (cadr plist))
379            (rest (cddr 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)
386           (use-package-error
387            (format "Keyword handler not defined: %s" handler)))))))
388
389 (put 'use-package-process-keywords 'lisp-indent-function 'defun)
390
391 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
392 ;;
393 ;; :pin
394 ;;
395
396 (defun use-package-only-one (label args f)
397   "Call F on the first member of ARGS if it has exactly one element."
398   (declare (indent 1))
399   (cond
400    ((and (listp args) (listp (cdr args))
401          (= (length args) 1))
402     (funcall f label (car args)))
403    (t
404     (use-package-error
405      (concat label " wants exactly one argument")))))
406
407 (put 'use-package-only-one 'lisp-indent-function 'defun)
408
409 (defun use-package-normalize/:pin (name keyword args)
410   (use-package-only-one (symbol-name keyword) args
411     (lambda (label arg)
412       (cond
413        ((stringp arg) arg)
414        ((symbolp arg) (symbol-name arg))
415        (t
416         (use-package-error
417          ":pin wants an archive name (a string)"))))))
418
419 (eval-when-compile
420   (defvar package-pinned-packages)
421   (defvar package-archives))
422
423 (defun use-package--archive-exists-p (archive)
424   "Check if a given ARCHIVE is enabled.
425
426 ARCHIVE can be a string or a symbol or 'manual to indicate a
427 manually updated package."
428   (if (member archive '(manual "manual"))
429       't
430     (let ((valid nil))
431       (dolist (pa package-archives)
432         (when (member archive (list (car pa) (intern (car pa))))
433           (setq valid 't)))
434       valid)))
435
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))))
448
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)
453                                                 ,archive-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.
459     body))
460
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462 ;;
463 ;; :ensure
464 ;;
465 (defvar package-archive-contents)
466 (defun use-package-normalize/:ensure (name keyword args)
467   (if (null args)
468       t
469     (use-package-only-one (symbol-name keyword) args
470       (lambda (label arg)
471         (if (symbolp arg)
472             arg
473           (use-package-error
474            (concat ":ensure wants an optional package name "
475                    "(an unquoted symbol name)")))))))
476
477 (defun use-package-ensure-elpa (package &optional no-refresh)
478   (if (package-installed-p package)
479       t
480     (if (or (assoc package package-archive-contents) no-refresh)
481         (package-install package)
482       (progn
483         (package-refresh-contents)
484         (use-package-ensure-elpa package t)))))
485
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.
499     body))
500
501 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
502 ;;
503 ;; :if, :when and :unless
504 ;;
505
506 (defsubst use-package-normalize-value (label arg)
507   "Normalize a value."
508   (cond ((symbolp arg)
509          `(symbol-value ',arg))
510         ((functionp arg)
511          `(funcall #',arg))
512         (t arg)))
513
514 (defun use-package-normalize-test (name keyword args)
515   (use-package-only-one (symbol-name keyword) args
516     #'use-package-normalize-value))
517
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)
521
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))))
525
526 (defalias 'use-package-handler/:when 'use-package-handler/:if)
527
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))))
531
532 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
533 ;;
534 ;; :requires
535 ;;
536
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."
539   (declare (indent 1))
540   (if (and (listp args) (listp (cdr args)))
541       (if (= (length args) 1)
542           (funcall f label (car args))
543         (funcall f label args))
544     (use-package-error
545      (concat label " wants a list"))))
546
547 (put 'use-package-as-one 'lisp-indent-function 'defun)
548
549 (defun use-package-normalize-symbols (label arg &optional recursed)
550   "Normalize a list of symbols."
551   (cond
552    ((symbolp arg)
553     (list arg))
554    ((and (not recursed) (listp arg) (listp (cdr arg)))
555     (mapcar #'(lambda (x) (car (use-package-normalize-symbols label x t))) arg))
556    (t
557     (use-package-error
558      (concat label " wants a symbol, or list of symbols")))))
559
560 (defun use-package-normalize-symlist (name keyword args)
561   (use-package-as-one (symbol-name keyword) args
562     #'use-package-normalize-symbols))
563
564 (defalias 'use-package-normalize/:requires 'use-package-normalize-symlist)
565
566 (defun use-package-handler/:requires (name keyword requires rest state)
567   (let ((body (use-package-process-keywords name rest state)))
568     (if (null requires)
569         body
570       `((when ,(if (listp requires)
571                    `(not (member nil (mapcar #'featurep ',requires)))
572                  `(featurep ',requires))
573           ,@body)))))
574
575 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
576 ;;
577 ;; :load-path
578 ;;
579
580 (defun use-package-normalize-paths (label arg &optional recursed)
581   "Normalize a list of filesystem paths."
582   (cond
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))))
586    ((stringp arg)
587     (let ((path (if (file-name-absolute-p arg)
588                     arg
589                   (expand-file-name arg user-emacs-directory))))
590       (list path)))
591    ((and (not recursed) (listp arg) (listp (cdr arg)))
592     (mapcar #'(lambda (x)
593                 (car (use-package-normalize-paths label x t))) arg))
594    (t
595     (use-package-error
596      (concat label " wants a directory path, or list of paths")))))
597
598 (defun use-package-normalize/:load-path (name keyword args)
599   (use-package-as-one (symbol-name keyword) args
600     #'use-package-normalize-paths))
601
602 (defun use-package-handler/:load-path (name keyword arg rest state)
603   (let ((body (use-package-process-keywords name rest state)))
604     (use-package-concat
605      (mapcar #'(lambda (path)
606                  `(eval-and-compile (add-to-list 'load-path ,path))) arg)
607      body)))
608
609 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
610 ;;
611 ;; :no-require
612 ;;
613
614 (defun use-package-normalize-predicate (name keyword args)
615   (if (null args)
616       t
617     (use-package-only-one (symbol-name keyword) args
618       #'use-package-normalize-value)))
619
620 (defalias 'use-package-normalize/:no-require 'use-package-normalize-predicate)
621
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))
625
626 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
627 ;;
628 ;; :preface
629 ;;
630
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))
638                   (macroexpand form)
639                 form)) args))
640
641 (defun use-package-normalize-forms (name keyword args)
642   (use-package-normalize-form (symbol-name keyword) args))
643
644 (defalias 'use-package-normalize/:preface 'use-package-normalize-forms)
645
646 (defun use-package-handler/:preface (name keyword arg rest state)
647   (let ((body (use-package-process-keywords name rest state)))
648     (use-package-concat
649      (unless (null arg)
650        `((eval-and-compile ,@arg)))
651      body)))
652
653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
654 ;;
655 ;; :bind, :bind*
656 ;;
657
658 (defsubst use-package-is-sympair (x &optional allow-vector)
659   "Return t if X has the type (STRING . SYMBOL)."
660   (and (consp x)
661        (or (stringp (car x))
662            (and allow-vector (vectorp (car x))))
663        (symbolp (cdr x))))
664
665 (defsubst use-package-is-string-pair (x)
666   "Return t if X has the type (STRING . STRING)."
667   (and (consp x)
668        (stringp (car x))
669        (stringp (cdr x))))
670
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'."
679   (cond
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)
683     (list arg))
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)))
688                   (if (listp ret)
689                       (car ret)
690                     ret))) arg))
691    ((and allow-string-cdrs (use-package-is-string-pair arg))
692     (list arg))
693    (t arg)))
694
695 (defun use-package-normalize-binder (name keyword args)
696   (use-package-as-one (symbol-name keyword) args
697     (lambda (label arg)
698       (use-package-normalize-pairs name label arg nil t t))))
699
700 (defalias 'use-package-normalize/:bind 'use-package-normalize-binder)
701 (defalias 'use-package-normalize/:bind* 'use-package-normalize-binder)
702
703 (defun use-package-handler/:bind
704     (name keyword arg rest state &optional bind-macro)
705   (let ((commands (remq nil (mapcar #'(lambda (arg)
706                                         (if (listp arg)
707                                             (cdr arg)
708                                           nil)) arg))))
709     (use-package-concat
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))
714      `((ignore
715         ,(macroexpand
716           `(,(if bind-macro bind-macro 'bind-keys)
717             :package ,name ,@arg)))))))
718
719 (defun use-package-handler/:bind* (name keyword arg rest state)
720   (use-package-handler/:bind name keyword arg rest state 'bind-keys*))
721
722 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
723 ;;
724 ;; :bind-keymap, :bind-keymap*
725 ;;
726
727 (defalias 'use-package-normalize/:bind-keymap 'use-package-normalize-binder)
728 (defalias 'use-package-normalize/:bind-keymap* 'use-package-normalize-binder)
729
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.
735
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)))
748           (if override
749               (bind-key* key keymap)
750             (bind-key key keymap))
751           (setq unread-command-events
752                 (listify-key-sequence kv)))
753       (use-package-error
754        (format "use-package: package.el %s failed to define keymap %s"
755                package keymap-symbol)))))
756
757 (defun use-package-handler/:bind-keymap
758     (name keyword arg rest state &optional override)
759   (let ((form (mapcar
760                #'(lambda (binding)
761                    `(,(if override
762                           'bind-key*
763                         'bind-key)
764                      ,(car binding)
765                      #'(lambda ()
766                          (interactive)
767                          (use-package-autoload-keymap
768                           ',(cdr binding) ',(use-package-as-symbol name) ,override)))) arg)))
769     (use-package-concat
770      (use-package-process-keywords name
771        (use-package-sort-keywords
772         (use-package-plist-maybe-put rest :defer t))
773        state)
774      `((ignore ,@form)))))
775
776 (defun use-package-handler/:bind-keymap* (name keyword arg rest state)
777   (use-package-handler/:bind-keymap name keyword arg rest state t))
778
779 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
780 ;;
781 ;; :interpreter
782 ;;
783
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)))
787
788 (defalias 'use-package-normalize/:interpreter 'use-package-normalize-mode)
789
790 (defun use-package-handler/:interpreter (name keyword arg rest state)
791   (let* (commands
792          (form (mapcar #'(lambda (interpreter)
793                            (push (cdr interpreter) commands)
794                            `(add-to-list 'interpreter-mode-alist ',interpreter)) arg)))
795     (use-package-concat
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)))))
801
802 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
803 ;;
804 ;; :mode
805 ;;
806
807 (defalias 'use-package-normalize/:mode 'use-package-normalize-mode)
808
809 (defun use-package-handler/:mode (name keyword arg rest state)
810   (let* (commands
811          (form (mapcar #'(lambda (mode)
812                            (push (cdr mode) commands)
813                            `(add-to-list 'auto-mode-alist ',mode)) arg)))
814     (use-package-concat
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)))))
820
821 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
822 ;;
823 ;; :commands
824 ;;
825
826 (defalias 'use-package-normalize/:commands 'use-package-normalize-symlist)
827
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)))
834
835 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
836 ;;
837 ;; :defines
838 ;;
839
840 (defalias 'use-package-normalize/:defines 'use-package-normalize-symlist)
841
842 (defun use-package-handler/:defines (name keyword arg rest state)
843   (let ((body (use-package-process-keywords name rest state)))
844     body))
845
846 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
847 ;;
848 ;; :functions
849 ;;
850
851 (defalias 'use-package-normalize/:functions 'use-package-normalize-symlist)
852
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))
856         body
857       (use-package-concat
858        (unless (null arg)
859          `((eval-when-compile
860              ,@(mapcar
861                 #'(lambda (fn)
862                     `(declare-function ,fn ,(use-package-as-string name))) arg))))
863        body))))
864
865 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
866 ;;
867 ;; :defer
868 ;;
869
870 (defalias 'use-package-normalize/:defer 'use-package-normalize-predicate)
871
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)))
876     (use-package-concat
877      ;; Load the package after a set amount of idle time, if the argument to
878      ;; `:defer' was a number.
879      (when (numberp arg)
880        `((run-with-idle-timer ,arg nil #'require ',(use-package-as-symbol name) nil t)))
881
882      ;; Since we deferring load, establish any necessary autoloads, and also
883      ;; keep the byte-compiler happy.
884      (apply
885       #'nconc
886       (mapcar #'(lambda (command)
887                   (when (not (stringp command))
888                     (append
889                      `((unless (fboundp ',command)
890                          (autoload #',command ,name-string nil t)))
891                      (when (bound-and-true-p byte-compile-current-file)
892                        `((eval-when-compile
893                            (declare-function ,command ,name-string)))))))
894               (delete-dups (plist-get state :commands))))
895
896      body)))
897
898
899 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
900 ;;
901 ;; :after
902 ;;
903
904 (defalias 'use-package-normalize/:after 'use-package-normalize-symlist)
905
906 (defun use-package-require-after-load (features name)
907   "Return form for after any of FEATURES require NAME."
908   `(progn
909      ,@(mapcar
910         (lambda (feat)
911           `(eval-after-load
912                (quote ,feat)
913              (quote (require (quote ,name)))))
914         features)))
915
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)))
920     (use-package-concat
921      (when arg
922        (list (use-package-require-after-load arg name)))
923      body)))
924
925 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
926 ;;
927 ;; :demand
928 ;;
929
930 (defalias 'use-package-normalize/:demand 'use-package-normalize-predicate)
931
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)))
935
936 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
937 ;;
938 ;; :init
939 ;;
940
941 (defalias 'use-package-normalize/:init 'use-package-normalize-forms)
942
943 (defun use-package-handler/:init (name keyword arg rest state)
944   (let ((body (use-package-process-keywords name rest state)))
945     (use-package-concat
946      ;; The user's initializations
947      (let ((init-body
948             (use-package-hook-injector (use-package-as-string name)
949                                        :init arg)))
950        (if use-package-check-before-init
951            `((if (locate-library ,(use-package-as-string name))
952                  ,(macroexp-progn init-body)))
953          init-body))
954      body)))
955
956 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
957 ;;
958 ;; :config
959 ;;
960
961 (defalias 'use-package-normalize/:config 'use-package-normalize-forms)
962
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))
966          (config-body
967           (if (equal arg '(t))
968               body
969             (use-package--with-elapsed-timer
970                 (format "Configuring package %s" name-symbol)
971               (use-package-concat
972                (use-package-hook-injector (symbol-name name-symbol)
973                                           :config arg)
974                body
975                (list t))))))
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
983             (use-package-concat
984              (list (use-package-load-name name))
985              config-body)
986           `((if (not ,(use-package-load-name name t))
987                 (ignore
988                  (message (format "Cannot load %s" ',name)))
989               ,@config-body)))))))
990
991 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
992 ;;
993 ;; :diminish
994
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:
997      SYMBOL
998      (SYMBOL . STRING)"
999   (cond
1000    ((symbolp arg)
1001     (list arg))
1002    ((stringp arg)
1003     (list (cons (intern (concat (use-package-as-string name) "-mode")) arg)))
1004    ((and (consp arg) (stringp (cdr arg)))
1005     (list 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))
1009    (t
1010     (use-package-error
1011      (concat label " wants a string, symbol, "
1012              "(symbol . string) or list of these")))))
1013
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)))
1017
1018 (defun use-package-handler/:diminish (name keyword arg rest state)
1019   (let ((body (use-package-process-keywords name rest state)))
1020     (use-package-concat
1021      (mapcar #'(lambda (var)
1022                  `(if (fboundp 'diminish)
1023                       ,(if (consp var)
1024                            `(diminish ',(car var) ,(cdr var))
1025                          `(diminish ',var))))
1026              arg)
1027      body)))
1028
1029 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1030 ;;
1031 ;; :delight
1032 ;;
1033
1034 (defun use-package-normalize/:delight (name keyword args)
1035   "Normalize arguments to delight."
1036   (cond
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)))
1045     args)
1046    (t
1047     (use-package-error ":delight expects same args as delight function"))))
1048
1049 (defun use-package-handler/:delight (name keyword args rest state)
1050   (let ((body (use-package-process-keywords name rest state)))
1051     (use-package-concat
1052      body
1053      `((delight (quote ,(nth 0 args)) ,(nth 1 args) (quote ,(nth 2 args))) t))))
1054
1055 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1056 ;;
1057 ;; The main macro
1058 ;;
1059
1060 ;;;###autoload
1061 (defmacro use-package (name &rest args)
1062   "Declare an Emacs package by specifying a group of configuration options.
1063
1064 For full documentation, please see the README file that came with
1065 this file.  Usage:
1066
1067   (use-package package-name
1068      [:keyword [option]]...)
1069
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.
1077
1078 :mode          Form to be added to `auto-mode-alist'.
1079 :interpreter   Form to be added to `interpreter-mode-alist'.
1080
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.
1085
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
1092
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.
1097
1098 :after         Defer loading of a package until after any of the named
1099                features are loaded.
1100
1101 :demand        Prevent deferred loading in all cases.
1102
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)
1116                    :config '(t)))
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)
1121                      args0)))
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)
1126                      args*))))
1127
1128       ;; When byte-compiling, pre-load the package so all its symbols are in
1129       ;; scope.
1130       (if (bound-and-true-p byte-compile-current-file)
1131           (setq args*
1132                 (use-package-plist-cons
1133                  args* :preface
1134                  `(eval-when-compile
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)))))))
1143
1144       (let ((body
1145              (macroexp-progn
1146               (use-package-process-keywords name args*
1147                 (and use-package-always-defer '(:deferred t))))))
1148         (if use-package-debug
1149             (display-buffer
1150              (save-current-buffer
1151                (let ((buf (get-buffer-create "*use-package*")))
1152                  (with-current-buffer buf
1153                    (delete-region (point-min) (point-max))
1154                    (emacs-lisp-mode)
1155                    (insert (pp-to-string body)))
1156                  buf))))
1157         body))))
1158
1159
1160 (put 'use-package 'lisp-indent-function 'defun)
1161
1162 (provide 'use-package)
1163
1164 ;; Local Variables:
1165 ;; indent-tabs-mode: nil
1166 ;; End:
1167
1168 ;;; use-package.el ends here