Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1593 lines
59 KiB

5 years ago
  1. ;;; use-package-core.el --- A configuration macro for simplifying your .emacs -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2017 John Wiegley
  3. ;; Author: John Wiegley <johnw@newartisans.com>
  4. ;; Maintainer: John Wiegley <johnw@newartisans.com>
  5. ;; Created: 17 Jun 2012
  6. ;; Modified: 29 Nov 2017
  7. ;; Version: 2.4
  8. ;; Package-Requires: ((emacs "24.3"))
  9. ;; Keywords: dotemacs startup speed config package
  10. ;; URL: https://github.com/jwiegley/use-package
  11. ;; This program is free software; you can redistribute it and/or
  12. ;; modify it under the terms of the GNU General Public License as
  13. ;; published by the Free Software Foundation; either version 3, or (at
  14. ;; your option) any later version.
  15. ;; This program is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. ;; General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23. ;;; Commentary:
  24. ;; The `use-package' declaration macro allows you to isolate package
  25. ;; configuration in your ".emacs" in a way that is performance-oriented and,
  26. ;; well, just tidy. I created it because I have over 80 packages that I use
  27. ;; in Emacs, and things were getting difficult to manage. Yet with this
  28. ;; utility my total load time is just under 1 second, with no loss of
  29. ;; functionality!
  30. ;;
  31. ;; Please see README.md from the same repository for documentation.
  32. ;;; Code:
  33. (require 'bytecomp)
  34. (require 'cl-lib)
  35. (require 'tabulated-list)
  36. (if (and (eq emacs-major-version 24) (eq emacs-minor-version 3))
  37. (defsubst hash-table-keys (hash-table)
  38. "Return a list of keys in HASH-TABLE."
  39. (cl-loop for k being the hash-keys of hash-table collect k))
  40. (eval-when-compile (require 'subr-x)))
  41. (eval-when-compile
  42. (require 'regexp-opt))
  43. (defgroup use-package nil
  44. "A use-package declaration for simplifying your `.emacs'."
  45. :group 'startup)
  46. (defconst use-package-version "2.4"
  47. "This version of use-package.")
  48. (defcustom use-package-keywords
  49. '(:disabled
  50. :load-path
  51. :requires
  52. :defines
  53. :functions
  54. :preface
  55. :if :when :unless
  56. :no-require
  57. :catch
  58. :after
  59. :custom
  60. :custom-face
  61. :bind
  62. :bind*
  63. :bind-keymap
  64. :bind-keymap*
  65. :interpreter
  66. :mode
  67. :magic
  68. :magic-fallback
  69. :hook
  70. ;; Any other keyword that also declares commands to be autoloaded (such as
  71. ;; :bind) must appear before this keyword.
  72. :commands
  73. :init
  74. :defer
  75. :demand
  76. :load
  77. ;; This must occur almost last; the only forms which should appear after
  78. ;; are those that must happen directly after the config forms.
  79. :config)
  80. "The set of valid keywords, in the order they are processed in.
  81. The order of this list is *very important*, so it is only
  82. advisable to insert new keywords, never to delete or reorder
  83. them. Further, attention should be paid to the NEWS.md if the
  84. default order ever changes, as they may have subtle effects on
  85. the semantics of use-package declarations and may necessitate
  86. changing where you had inserted a new keyword earlier.
  87. Note that `:disabled' is special in this list, as it causes
  88. nothing at all to happen, even if the rest of the use-package
  89. declaration is incorrect."
  90. :type '(repeat symbol)
  91. :group 'use-package)
  92. (defcustom use-package-deferring-keywords
  93. '(:bind-keymap
  94. :bind-keymap*
  95. :commands)
  96. "Unless `:demand' is used, keywords in this list imply deferred loading.
  97. The reason keywords like `:hook' are not in this list is that
  98. they only imply deferred loading if they reference actual
  99. function symbols that can be autoloaded from the module; whereas
  100. the default keywords provided here always defer loading unless
  101. otherwise requested."
  102. :type '(repeat symbol)
  103. :group 'use-package)
  104. (defcustom use-package-ignore-unknown-keywords nil
  105. "If non-nil, issue warning instead of error when unknown
  106. keyword is encountered. The unknown keyword and its associated
  107. arguments will be ignored in the `use-package' expansion."
  108. :type 'boolean
  109. :group 'use-package)
  110. (defcustom use-package-verbose nil
  111. "Whether to report about loading and configuration details.
  112. If you customize this, then you should require the `use-package'
  113. feature in files that use `use-package', even if these files only
  114. contain compiled expansions of the macros. If you don't do so,
  115. then the expanded macros do their job silently."
  116. :type '(choice (const :tag "Quiet, without catching errors" errors)
  117. (const :tag "Quiet" nil)
  118. (const :tag "Verbose" t)
  119. (const :tag "Debug" debug))
  120. :group 'use-package)
  121. (defcustom use-package-check-before-init nil
  122. "If non-nil, check that package exists before executing its `:init' block.
  123. This check is performed by calling `locate-library'."
  124. :type 'boolean
  125. :group 'use-package)
  126. (defcustom use-package-always-defer nil
  127. "If non-nil, assume `:defer t' unless `:demand' is used.
  128. See also `use-package-defaults', which uses this value."
  129. :type 'boolean
  130. :group 'use-package)
  131. (defcustom use-package-always-demand nil
  132. "If non-nil, assume `:demand t' unless `:defer' is used.
  133. See also `use-package-defaults', which uses this value."
  134. :type 'boolean
  135. :group 'use-package)
  136. (defcustom use-package-defaults
  137. '(;; this '(t) has special meaning; see `use-package-handler/:config'
  138. (:config '(t) t)
  139. (:init nil t)
  140. (:catch t (lambda (name args)
  141. (not use-package-expand-minimally)))
  142. (:defer use-package-always-defer
  143. (lambda (name args)
  144. (and use-package-always-defer
  145. (not (plist-member args :defer))
  146. (not (plist-member args :demand)))))
  147. (:demand use-package-always-demand
  148. (lambda (name args)
  149. (and use-package-always-demand
  150. (not (plist-member args :defer))
  151. (not (plist-member args :demand))))))
  152. "Default values for specified `use-package' keywords.
  153. Each entry in the alist is a list of three elements:
  154. The first element is the `use-package' keyword.
  155. The second is a form that can be evaluated to get the default
  156. value. It can also be a function that will receive the name of
  157. the use-package declaration and the keyword plist given to
  158. `use-package', in normalized form. The value it returns should
  159. also be in normalized form (which is sometimes *not* what one
  160. would normally write in a `use-package' declaration, so use
  161. caution).
  162. The third element is a form that can be evaluated to determine
  163. whether or not to assign a default value; if it evaluates to nil,
  164. then the default value is not assigned even if the keyword is not
  165. present in the `use-package' form. This third element may also be
  166. a function, in which case it receives the name of the package (as
  167. a symbol) and a list of keywords (in normalized form). It should
  168. return nil or non-nil depending on whether defaulting should be
  169. attempted."
  170. :type `(repeat
  171. (list (choice :tag "Keyword"
  172. ,@(mapcar #'(lambda (k) (list 'const k))
  173. use-package-keywords))
  174. (choice :tag "Default value" sexp function)
  175. (choice :tag "Enable if non-nil" sexp function)))
  176. :group 'use-package)
  177. (defcustom use-package-merge-key-alist
  178. '((:if . (lambda (new old) `(and ,new ,old)))
  179. (:after . (lambda (new old) `(:all ,new ,old)))
  180. (:defer . (lambda (new old) old))
  181. (:bind . (lambda (new old) (append new (list :break) old))))
  182. "Alist of keys and the functions used to merge multiple values.
  183. For example, if the following form is provided:
  184. (use-package foo :if pred1 :if pred2)
  185. Then based on the above defaults, the merged result will be:
  186. (use-package foo :if (and pred1 pred2))
  187. This is done so that, at the stage of invoking handlers, each
  188. handler is called only once."
  189. :type `(repeat
  190. (cons (choice :tag "Keyword"
  191. ,@(mapcar #'(lambda (k) (list 'const k))
  192. use-package-keywords)
  193. (const :tag "Any" t))
  194. function))
  195. :group 'use-package)
  196. (defcustom use-package-hook-name-suffix "-hook"
  197. "Text append to the name of hooks mentioned by :hook.
  198. Set to nil if you don't want this to happen; it's only a
  199. convenience."
  200. :type '(choice string (const :tag "No suffix" nil))
  201. :group 'use-package)
  202. (defcustom use-package-minimum-reported-time 0.1
  203. "Minimal load time that will be reported.
  204. Note that `use-package-verbose' has to be set to a non-nil value
  205. for anything to be reported at all."
  206. :type 'number
  207. :group 'use-package)
  208. (defcustom use-package-inject-hooks nil
  209. "If non-nil, add hooks to the `:init' and `:config' sections.
  210. In particular, for a given package `foo', the following hooks
  211. become available:
  212. `use-package--foo--pre-init-hook'
  213. `use-package--foo--post-init-hook'
  214. `use-package--foo--pre-config-hook'
  215. `use-package--foo--post-config-hook'
  216. This way, you can add to these hooks before evaluation of a
  217. `use-package` declaration, and exercise some control over what
  218. happens.
  219. NOTE: These hooks are run even if the user does not specify an
  220. `:init' or `:config' block, and they will happen at the regular
  221. time when initialization and configuration would have been
  222. performed.
  223. NOTE: If the `pre-init' hook return a nil value, that block's
  224. user-supplied configuration is not evaluated, so be certain to
  225. return t if you only wish to add behavior to what the user had
  226. specified."
  227. :type 'boolean
  228. :group 'use-package)
  229. (defcustom use-package-expand-minimally nil
  230. "If non-nil, make the expanded code as minimal as possible.
  231. This disables:
  232. - Printing to the *Messages* buffer of slowly-evaluating forms
  233. - Capturing of load errors (normally redisplayed as warnings)
  234. - Conditional loading of packages (load failures become errors)
  235. The main advantage to this variable is that, if you know your
  236. configuration works, it will make the byte-compiled file as
  237. minimal as possible. It can also help with reading macro-expanded
  238. definitions, to understand the main intent of what's happening."
  239. :type 'boolean
  240. :group 'use-package)
  241. (defcustom use-package-form-regexp-eval
  242. `(concat ,(eval-when-compile
  243. (concat "^\\s-*("
  244. (regexp-opt '("use-package" "require") t)
  245. "\\s-+\\("))
  246. (or (bound-and-true-p lisp-mode-symbol-regexp)
  247. "\\(?:\\sw\\|\\s_\\|\\\\.\\)+") "\\)")
  248. "Sexp providing regexp for finding use-package forms in user files.
  249. This is used by `use-package-jump-to-package-form' and
  250. `use-package-enable-imenu-support'."
  251. :type 'sexp
  252. :group 'use-package)
  253. (defcustom use-package-enable-imenu-support nil
  254. "If non-nil, cause imenu to see `use-package' declarations.
  255. This is done by adjusting `lisp-imenu-generic-expression' to
  256. include support for finding `use-package' and `require' forms.
  257. Must be set before loading use-package."
  258. :type 'boolean
  259. :set
  260. #'(lambda (_sym value)
  261. (eval-after-load 'lisp-mode
  262. (if value
  263. `(add-to-list 'lisp-imenu-generic-expression
  264. (list "Packages" ,use-package-form-regexp-eval 2))
  265. `(setq lisp-imenu-generic-expression
  266. (remove (list "Packages" ,use-package-form-regexp-eval 2)
  267. lisp-imenu-generic-expression)))))
  268. :group 'use-package)
  269. (defconst use-package-font-lock-keywords
  270. '(("(\\(use-package\\)\\_>[ \t']*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
  271. (1 font-lock-keyword-face)
  272. (2 font-lock-constant-face nil t))))
  273. (font-lock-add-keywords 'emacs-lisp-mode use-package-font-lock-keywords)
  274. (defcustom use-package-compute-statistics nil
  275. "If non-nil, compute statistics concerned use-package declarations.
  276. View the statistical report using `use-package-report'. Note that
  277. if this option is enabled, you must require `use-package' in your
  278. user init file at loadup time, or you will see errors concerning
  279. undefined variables."
  280. :type 'boolean
  281. :group 'use-package)
  282. (defvar use-package-statistics (make-hash-table))
  283. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  284. ;;
  285. ;;; Utility functions
  286. ;;
  287. (defsubst use-package-error (msg)
  288. "Report MSG as an error, so the user knows it came from this package."
  289. (error "use-package: %s" msg))
  290. (defsubst use-package-concat (&rest elems)
  291. "Delete all empty lists from ELEMS (nil or (list nil)), and append them."
  292. (apply #'append (delete nil (delete (list nil) elems))))
  293. (defsubst use-package-non-nil-symbolp (sym)
  294. (and sym (symbolp sym)))
  295. (defsubst use-package-as-symbol (string-or-symbol)
  296. "If STRING-OR-SYMBOL is already a symbol, return it. Otherwise
  297. convert it to a symbol and return that."
  298. (if (symbolp string-or-symbol) string-or-symbol
  299. (intern string-or-symbol)))
  300. (defsubst use-package-as-string (string-or-symbol)
  301. "If STRING-OR-SYMBOL is already a string, return it. Otherwise
  302. convert it to a string and return that."
  303. (if (stringp string-or-symbol) string-or-symbol
  304. (symbol-name string-or-symbol)))
  305. (defsubst use-package-regex-p (re)
  306. "Return t if RE is some regexp-like thing."
  307. (or (and (listp re) (eq (car re) 'rx))
  308. (stringp re)))
  309. (defun use-package-normalize-regex (re)
  310. "Given some regexp-like thing in RE, resolve to a regular expression."
  311. (cond
  312. ((and (listp re) (eq (car re) 'rx)) (eval re))
  313. ((stringp re) re)
  314. (t (error "Not recognized as regular expression: %s" re))))
  315. (defsubst use-package-is-pair (x car-pred cdr-pred)
  316. "Return non-nil if X is a cons satisfying the given predicates.
  317. CAR-PRED and CDR-PRED are applied to X's `car' and `cdr',
  318. respectively."
  319. (and (consp x)
  320. (funcall car-pred (car x))
  321. (funcall cdr-pred (cdr x))))
  322. (defun use-package-as-mode (string-or-symbol)
  323. "If STRING-OR-SYMBOL ends in `-mode' (or its name does), return
  324. it as a symbol. Otherwise, return it as a symbol with `-mode'
  325. appended."
  326. (let ((string (use-package-as-string string-or-symbol)))
  327. (intern (if (string-match "-mode\\'" string)
  328. string
  329. (concat string "-mode")))))
  330. (defsubst use-package-load-name (name &optional noerror)
  331. "Return a form which will load or require NAME.
  332. It does the right thing no matter if NAME is a string or symbol.
  333. Argument NOERROR means to indicate load failures as a warning."
  334. (if (stringp name)
  335. `(load ,name ,noerror)
  336. `(require ',name nil ,noerror)))
  337. (defun use-package-hook-injector (name-string keyword body)
  338. "Wrap pre/post hook injections around the given BODY for KEYWORD.
  339. The BODY is a list of forms, so `((foo))' if only `foo' is being called."
  340. (if (not use-package-inject-hooks)
  341. body
  342. (let ((keyword-name (substring (format "%s" keyword) 1)))
  343. `((when (run-hook-with-args-until-failure
  344. ',(intern (concat "use-package--" name-string
  345. "--pre-" keyword-name "-hook")))
  346. ,@body
  347. (run-hooks
  348. ',(intern (concat "use-package--" name-string
  349. "--post-" keyword-name "-hook"))))))))
  350. (defun use-package-with-elapsed-timer (text body)
  351. "BODY is a list of forms, so `((foo))' if only `foo' is being called."
  352. (declare (indent 1))
  353. (if use-package-expand-minimally
  354. body
  355. (let ((nowvar (make-symbol "now")))
  356. (if (bound-and-true-p use-package-verbose)
  357. `((let ((,nowvar (current-time)))
  358. (message "%s..." ,text)
  359. (prog1
  360. ,(macroexp-progn body)
  361. (let ((elapsed
  362. (float-time (time-subtract (current-time) ,nowvar))))
  363. (if (> elapsed ,use-package-minimum-reported-time)
  364. (message "%s...done (%.3fs)" ,text elapsed)
  365. (message "%s...done" ,text))))))
  366. body))))
  367. (put 'use-package-with-elapsed-timer 'lisp-indent-function 1)
  368. (defun use-package-require (name &optional no-require body)
  369. (if use-package-expand-minimally
  370. (use-package-concat
  371. (unless no-require
  372. (list (use-package-load-name name)))
  373. body)
  374. (if no-require
  375. body
  376. (use-package-with-elapsed-timer
  377. (format "Loading package %s" name)
  378. `((if (not ,(use-package-load-name name t))
  379. (display-warning 'use-package
  380. (format "Cannot load %s" ',name)
  381. :error)
  382. ,@body))))))
  383. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  384. ;;
  385. ;;; Property lists
  386. ;;
  387. (defun use-package-plist-delete (plist property)
  388. "Delete PROPERTY from PLIST.
  389. This is in contrast to merely setting it to 0."
  390. (let (p)
  391. (while plist
  392. (if (not (eq property (car plist)))
  393. (setq p (plist-put p (car plist) (nth 1 plist))))
  394. (setq plist (cddr plist)))
  395. p))
  396. (defun use-package-plist-delete-first (plist property)
  397. "Delete PROPERTY from PLIST.
  398. This is in contrast to merely setting it to 0."
  399. (let (p)
  400. (while plist
  401. (if (eq property (car plist))
  402. (setq p (nconc p (cddr plist))
  403. plist nil)
  404. (setq p (nconc p (list (car plist) (cadr plist)))
  405. plist (cddr plist))))
  406. p))
  407. (defsubst use-package-plist-maybe-put (plist property value)
  408. "Add a VALUE for PROPERTY to PLIST, if it does not already exist."
  409. (if (plist-member plist property)
  410. plist
  411. (plist-put plist property value)))
  412. (defsubst use-package-plist-cons (plist property value)
  413. "Cons VALUE onto the head of the list at PROPERTY in PLIST."
  414. (plist-put plist property (cons value (plist-get plist property))))
  415. (defsubst use-package-plist-append (plist property value)
  416. "Append VALUE onto the front of the list at PROPERTY in PLIST."
  417. (plist-put plist property (append value (plist-get plist property))))
  418. (defun use-package-split-list (pred xs)
  419. (let ((ys (list nil)) (zs (list nil)) flip)
  420. (cl-dolist (x xs)
  421. (if flip
  422. (nconc zs (list x))
  423. (if (funcall pred x)
  424. (progn
  425. (setq flip t)
  426. (nconc zs (list x)))
  427. (nconc ys (list x)))))
  428. (cons (cdr ys) (cdr zs))))
  429. (defun use-package-split-list-at-keys (key lst)
  430. (and lst
  431. (let ((xs (use-package-split-list (apply-partially #'eq key) lst)))
  432. (cons (car xs) (use-package-split-list-at-keys key (cddr xs))))))
  433. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  434. ;;
  435. ;;; Keywords
  436. ;;
  437. (defun use-package-keyword-index (keyword)
  438. (cl-loop named outer
  439. with index = 0
  440. for k in use-package-keywords do
  441. (if (eq k keyword)
  442. (cl-return-from outer index))
  443. (cl-incf index)))
  444. (defun use-package-normalize-plist (name input &optional plist merge-function)
  445. "Given a pseudo-plist, normalize it to a regular plist.
  446. The normalized key/value pairs from input are added to PLIST,
  447. extending any keys already present."
  448. (if (null input)
  449. plist
  450. (let* ((keyword (car input))
  451. (xs (use-package-split-list #'keywordp (cdr input)))
  452. (args (car xs))
  453. (tail (cdr xs))
  454. (normalizer
  455. (intern-soft (concat "use-package-normalize/"
  456. (symbol-name keyword))))
  457. (arg (and (functionp normalizer)
  458. (funcall normalizer name keyword args)))
  459. (error-string (format "Unrecognized keyword: %s" keyword)))
  460. (if (memq keyword use-package-keywords)
  461. (progn
  462. (setq plist (use-package-normalize-plist
  463. name tail plist merge-function))
  464. (plist-put plist keyword
  465. (if (plist-member plist keyword)
  466. (funcall merge-function keyword arg
  467. (plist-get plist keyword))
  468. arg)))
  469. (if use-package-ignore-unknown-keywords
  470. (progn
  471. (display-warning 'use-package error-string)
  472. (use-package-normalize-plist
  473. name tail plist merge-function))
  474. (use-package-error error-string))))))
  475. (defun use-package-unalias-keywords (_name args)
  476. (setq args (cl-nsubstitute :if :when args))
  477. (let (temp)
  478. (while (setq temp (plist-get args :unless))
  479. (setq args (use-package-plist-delete-first args :unless)
  480. args (append args `(:if (not ,temp))))))
  481. args)
  482. (defun use-package-merge-keys (key new old)
  483. (let ((merger (assq key use-package-merge-key-alist)))
  484. (if merger
  485. (funcall (cdr merger) new old)
  486. (append new old))))
  487. (defun use-package-sort-keywords (plist)
  488. (let (plist-grouped)
  489. (while plist
  490. (push (cons (car plist) (cadr plist))
  491. plist-grouped)
  492. (setq plist (cddr plist)))
  493. (let (result)
  494. (cl-dolist
  495. (x
  496. (nreverse
  497. (sort plist-grouped
  498. #'(lambda (l r) (< (use-package-keyword-index (car l))
  499. (use-package-keyword-index (car r)))))))
  500. (setq result (cons (car x) (cons (cdr x) result))))
  501. result)))
  502. (defun use-package-normalize-keywords (name args)
  503. (let* ((name-symbol (if (stringp name) (intern name) name))
  504. (name-string (symbol-name name-symbol)))
  505. ;; The function `elisp--local-variables' inserts this unbound variable into
  506. ;; macro forms to determine the locally bound variables for
  507. ;; `elisp-completion-at-point'. It ends up throwing a lot of errors since it
  508. ;; can occupy the position of a keyword (or look like a second argument to a
  509. ;; keyword that takes one). Deleting it when it's at the top level should be
  510. ;; harmless since there should be no locally bound variables to discover
  511. ;; here anyway.
  512. (setq args (delq 'elisp--witness--lisp args))
  513. ;; Reduce the set of keywords down to its most fundamental expression.
  514. (setq args (use-package-unalias-keywords name-symbol args))
  515. ;; Normalize keyword values, coalescing multiple occurrences.
  516. (setq args (use-package-normalize-plist name-symbol args nil
  517. #'use-package-merge-keys))
  518. ;; Add default values for keywords not specified, when applicable.
  519. (cl-dolist (spec use-package-defaults)
  520. (when (let ((func (nth 2 spec)))
  521. (if (and func (functionp func))
  522. (funcall func name args)
  523. (eval func)))
  524. (setq args (use-package-plist-maybe-put
  525. args (nth 0 spec)
  526. (let ((func (nth 1 spec)))
  527. (if (and func (functionp func))
  528. (funcall func name args)
  529. (eval func)))))))
  530. ;; Determine any autoloads implied by the keywords used.
  531. (let ((iargs args)
  532. commands)
  533. (while iargs
  534. (when (keywordp (car iargs))
  535. (let ((autoloads
  536. (intern-soft (concat "use-package-autoloads/"
  537. (symbol-name (car iargs))))))
  538. (when (functionp autoloads)
  539. (setq commands
  540. ;; jww (2017-12-07): Right now we just ignored the type of
  541. ;; the autoload being requested, and assume they are all
  542. ;; `command'.
  543. (append (mapcar
  544. #'car
  545. (funcall autoloads name-symbol (car iargs)
  546. (cadr iargs)))
  547. commands)))))
  548. (setq iargs (cddr iargs)))
  549. (when commands
  550. (setq args
  551. ;; Like `use-package-plist-append', but removing duplicates.
  552. (plist-put args :commands
  553. (delete-dups
  554. (append commands (plist-get args :commands)))))))
  555. ;; If byte-compiling, pre-load the package so all its symbols are in
  556. ;; scope. This is done by prepending statements to the :preface.
  557. (when (bound-and-true-p byte-compile-current-file)
  558. (setq args
  559. (use-package-plist-append
  560. args :preface
  561. (use-package-concat
  562. (mapcar #'(lambda (var) `(defvar ,var))
  563. (plist-get args :defines))
  564. (mapcar #'(lambda (fn) `(declare-function ,fn ,name-string))
  565. (plist-get args :functions))
  566. `((eval-when-compile
  567. (with-demoted-errors
  568. ,(format "Cannot load %s: %%S" name-string)
  569. ,(when (eq use-package-verbose 'debug)
  570. `(message ,(format "Compiling package %s" name-string)))
  571. ,(unless (plist-get args :no-require)
  572. `(unless (featurep ',name-symbol)
  573. (load ,name-string nil t))))))))))
  574. ;; Certain keywords imply :defer, if :demand was not specified.
  575. (when (and (not (plist-member args :demand))
  576. (not (plist-member args :defer))
  577. (not (or (equal '(t) (plist-get args :load))
  578. (equal (list (use-package-as-string name))
  579. (mapcar #'use-package-as-string
  580. (plist-get args :load)))))
  581. (cl-some #'identity
  582. (mapcar (apply-partially #'plist-member args)
  583. use-package-deferring-keywords)))
  584. (setq args (append args '(:defer t))))
  585. ;; The :load keyword overrides :no-require
  586. (when (and (plist-member args :load)
  587. (plist-member args :no-require))
  588. (setq args (use-package-plist-delete args :no-require)))
  589. ;; If at this point no :load, :defer or :no-require has been seen, then
  590. ;; :load the package itself.
  591. (when (and (not (plist-member args :load))
  592. (not (plist-member args :defer))
  593. (not (plist-member args :no-require)))
  594. (setq args (append args `(:load (,name)))))
  595. ;; Sort the list of keywords based on the order of `use-package-keywords'.
  596. (use-package-sort-keywords args)))
  597. (defun use-package-process-keywords (name plist &optional state)
  598. "Process the next keyword in the free-form property list PLIST.
  599. The values in the PLIST have each been normalized by the function
  600. use-package-normalize/KEYWORD (minus the colon).
  601. STATE is a property list that the function may modify and/or
  602. query. This is useful if a package defines multiple keywords and
  603. wishes them to have some kind of stateful interaction.
  604. Unless the KEYWORD being processed intends to ignore remaining
  605. keywords, it must call this function recursively, passing in the
  606. plist with its keyword and argument removed, and passing in the
  607. next value for the STATE."
  608. (declare (indent 1))
  609. (unless (null plist)
  610. (let* ((keyword (car plist))
  611. (arg (cadr plist))
  612. (rest (cddr plist)))
  613. (unless (keywordp keyword)
  614. (use-package-error (format "%s is not a keyword" keyword)))
  615. (let* ((handler (concat "use-package-handler/" (symbol-name keyword)))
  616. (handler-sym (intern handler)))
  617. (if (functionp handler-sym)
  618. (funcall handler-sym name keyword arg rest state)
  619. (use-package-error
  620. (format "Keyword handler not defined: %s" handler)))))))
  621. (put 'use-package-process-keywords 'lisp-indent-function 'defun)
  622. (defun use-package-list-insert (elem xs &optional anchor after test)
  623. "Insert ELEM into the list XS.
  624. If ANCHOR is also a keyword, place the new KEYWORD before that
  625. one.
  626. If AFTER is non-nil, insert KEYWORD either at the end of the
  627. keywords list, or after the ANCHOR if one has been provided.
  628. If TEST is non-nil, it is the test used to compare ELEM to list
  629. elements. The default is `eq'.
  630. The modified list is returned. The original list is not modified."
  631. (let (result)
  632. (dolist (k xs)
  633. (if (funcall (or test #'eq) k anchor)
  634. (if after
  635. (setq result (cons k result)
  636. result (cons elem result))
  637. (setq result (cons elem result)
  638. result (cons k result)))
  639. (setq result (cons k result))))
  640. (if anchor
  641. (nreverse result)
  642. (if after
  643. (nreverse (cons elem result))
  644. (cons elem (nreverse result))))))
  645. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  646. ;;
  647. ;;; Argument Processing
  648. ;;
  649. (defun use-package-only-one (label args f)
  650. "Call F on the first member of ARGS if it has exactly one element."
  651. (declare (indent 1))
  652. (cond
  653. ((and (listp args) (listp (cdr args))
  654. (= (length args) 1))
  655. (funcall f label (car args)))
  656. (t
  657. (use-package-error
  658. (concat label " wants exactly one argument")))))
  659. (put 'use-package-only-one 'lisp-indent-function 'defun)
  660. (defun use-package-as-one (label args f &optional allow-empty)
  661. "Call F on the first element of ARGS if it has one element, or all of ARGS.
  662. If ALLOW-EMPTY is non-nil, it's OK for ARGS to be an empty list."
  663. (declare (indent 1))
  664. (if (if args
  665. (and (listp args) (listp (cdr args)))
  666. allow-empty)
  667. (if (= (length args) 1)
  668. (funcall f label (car args))
  669. (funcall f label args))
  670. (use-package-error
  671. (concat label " wants a non-empty list"))))
  672. (put 'use-package-as-one 'lisp-indent-function 'defun)
  673. (defun use-package-memoize (f arg)
  674. "Ensure the macro-expansion of F applied to ARG evaluates ARG
  675. no more than once."
  676. (let ((loaded (cl-gentemp "use-package--loaded"))
  677. (result (cl-gentemp "use-package--result"))
  678. (next (cl-gentemp "use-package--next")))
  679. `((defvar ,loaded nil)
  680. (defvar ,result nil)
  681. (defvar ,next #'(lambda () (if ,loaded ,result
  682. (setq ,loaded t ,result ,arg))))
  683. ,@(funcall f `((funcall ,next))))))
  684. (defsubst use-package-normalize-value (_label arg)
  685. "Normalize the Lisp value given by ARG.
  686. The argument LABEL is ignored."
  687. (cond ((null arg) nil)
  688. ((eq t arg) t)
  689. ((use-package-non-nil-symbolp arg)
  690. `(symbol-value ',arg))
  691. ((functionp arg)
  692. `(funcall #',arg))
  693. (t arg)))
  694. (defun use-package-normalize-symbols (label arg &optional recursed)
  695. "Normalize a list of symbols."
  696. (cond
  697. ((use-package-non-nil-symbolp arg)
  698. (list arg))
  699. ((and (not recursed) (listp arg) (listp (cdr arg)))
  700. (mapcar #'(lambda (x) (car (use-package-normalize-symbols label x t))) arg))
  701. (t
  702. (use-package-error
  703. (concat label " wants a symbol, or list of symbols")))))
  704. (defun use-package-normalize-symlist (_name keyword args)
  705. (use-package-as-one (symbol-name keyword) args
  706. #'use-package-normalize-symbols))
  707. (defun use-package-normalize-recursive-symbols (label arg)
  708. "Normalize a list of symbols."
  709. (cond
  710. ((use-package-non-nil-symbolp arg)
  711. arg)
  712. ((and (listp arg) (listp (cdr arg)))
  713. (mapcar #'(lambda (x) (use-package-normalize-recursive-symbols label x))
  714. arg))
  715. (t
  716. (use-package-error
  717. (concat label " wants a symbol, or nested list of symbols")))))
  718. (defun use-package-normalize-recursive-symlist (_name keyword args)
  719. (use-package-as-one (symbol-name keyword) args
  720. #'use-package-normalize-recursive-symbols))
  721. (defun use-package-normalize-paths (label arg &optional recursed)
  722. "Normalize a list of filesystem paths."
  723. (cond
  724. ((and arg (or (use-package-non-nil-symbolp arg) (functionp arg)))
  725. (let ((value (use-package-normalize-value label arg)))
  726. (use-package-normalize-paths label (eval value))))
  727. ((stringp arg)
  728. (let ((path (if (file-name-absolute-p arg)
  729. arg
  730. (expand-file-name arg user-emacs-directory))))
  731. (list path)))
  732. ((and (not recursed) (listp arg) (listp (cdr arg)))
  733. (mapcar #'(lambda (x)
  734. (car (use-package-normalize-paths label x t))) arg))
  735. (t
  736. (use-package-error
  737. (concat label " wants a directory path, or list of paths")))))
  738. (defun use-package-normalize-predicate (_name keyword args)
  739. (if (null args)
  740. t
  741. (use-package-only-one (symbol-name keyword) args
  742. #'use-package-normalize-value)))
  743. (defun use-package-normalize-form (label args)
  744. "Given a list of forms, return it wrapped in `progn'."
  745. (unless (listp (car args))
  746. (use-package-error (concat label " wants a sexp or list of sexps")))
  747. (mapcar #'(lambda (form)
  748. (if (and (consp form)
  749. (memq (car form)
  750. '(use-package bind-key bind-key*
  751. unbind-key bind-keys bind-keys*)))
  752. (macroexpand form)
  753. form)) args))
  754. (defun use-package-normalize-forms (_name keyword args)
  755. (use-package-normalize-form (symbol-name keyword) args))
  756. (defun use-package-normalize-pairs
  757. (key-pred val-pred name label arg &optional recursed)
  758. "Normalize a list of pairs.
  759. KEY-PRED and VAL-PRED are predicates recognizing valid keys and
  760. values, respectively.
  761. If RECURSED is non-nil, recurse into sublists."
  762. (cond
  763. ((funcall key-pred arg)
  764. (list (cons arg (use-package-as-symbol name))))
  765. ((use-package-is-pair arg key-pred val-pred)
  766. (list arg))
  767. ((and (not recursed) (listp arg) (listp (cdr arg)))
  768. (let (last-item)
  769. (mapcar
  770. #'(lambda (x)
  771. (prog1
  772. (let ((ret (use-package-normalize-pairs
  773. key-pred val-pred name label x t)))
  774. (if (and (listp ret)
  775. (not (keywordp last-item)))
  776. (car ret)
  777. ret))
  778. (setq last-item x))) arg)))
  779. (t arg)))
  780. (defun use-package-recognize-function (v &optional binding additional-pred)
  781. "A predicate that recognizes functional constructions:
  782. nil
  783. sym
  784. 'sym
  785. (quote sym)
  786. #'sym
  787. (function sym)
  788. (lambda () ...)
  789. '(lambda () ...)
  790. (quote (lambda () ...))
  791. #'(lambda () ...)
  792. (function (lambda () ...))"
  793. (or (if binding
  794. (symbolp v)
  795. (use-package-non-nil-symbolp v))
  796. (and (listp v)
  797. (memq (car v) '(quote function))
  798. (use-package-non-nil-symbolp (cadr v)))
  799. (if binding (commandp v) (functionp v))
  800. (and additional-pred
  801. (funcall additional-pred v))))
  802. (defun use-package-normalize-function (v)
  803. "Reduce functional constructions to one of two normal forms:
  804. sym
  805. #'(lambda () ...)"
  806. (cond ((symbolp v) v)
  807. ((and (listp v)
  808. (memq (car v) '(quote function))
  809. (use-package-non-nil-symbolp (cadr v)))
  810. (cadr v))
  811. ((and (consp v)
  812. (eq 'lambda (car v)))
  813. v)
  814. ((and (listp v)
  815. (memq (car v) '(quote function))
  816. (eq 'lambda (car (cadr v))))
  817. (cadr v))
  818. (t v)))
  819. (defun use-package-normalize-commands (args)
  820. "Map over ARGS of the form ((_ . F) ...), normalizing functional F's."
  821. (mapcar #'(lambda (x)
  822. (if (consp x)
  823. (cons (car x) (use-package-normalize-function (cdr x)))
  824. x))
  825. args))
  826. (defun use-package-normalize-mode (name keyword args)
  827. "Normalize arguments for keywords which add regexp/mode pairs to an alist."
  828. (use-package-as-one (symbol-name keyword) args
  829. (apply-partially #'use-package-normalize-pairs
  830. #'use-package-regex-p
  831. #'use-package-recognize-function
  832. name)))
  833. (defun use-package-autoloads-mode (_name _keyword args)
  834. (mapcar
  835. #'(lambda (x) (cons (cdr x) 'command))
  836. (cl-remove-if-not #'(lambda (x)
  837. (and (consp x)
  838. (use-package-non-nil-symbolp (cdr x))))
  839. args)))
  840. (defun use-package-handle-mode (name alist args rest state)
  841. "Handle keywords which add regexp/mode pairs to an alist."
  842. (use-package-concat
  843. (use-package-process-keywords name rest state)
  844. (mapcar
  845. #'(lambda (thing)
  846. `(add-to-list
  847. ',alist
  848. ',(cons (use-package-normalize-regex (car thing))
  849. (cdr thing))))
  850. (use-package-normalize-commands args))))
  851. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  852. ;;
  853. ;;; Statistics
  854. ;;
  855. (defun use-package-reset-statistics ()
  856. (interactive)
  857. (setq use-package-statistics (make-hash-table)))
  858. (defun use-package-statistics-status (package)
  859. "Return loading configuration status of PACKAGE statistics."
  860. (cond ((gethash :config package) "Configured")
  861. ((gethash :init package) "Initialized")
  862. ((gethash :preface package) "Prefaced")
  863. ((gethash :use-package package) "Declared")))
  864. (defun use-package-statistics-last-event (package)
  865. "Return the date when PACKAGE's status last changed.
  866. The date is returned as a string."
  867. (format-time-string "%Y-%m-%d %a %H:%M"
  868. (or (gethash :config package)
  869. (gethash :init package)
  870. (gethash :preface package)
  871. (gethash :use-package package))))
  872. (defun use-package-statistics-time (package)
  873. "Return the time is took for PACKAGE to load."
  874. (+ (float-time (gethash :config-secs package '(0 0 0 0)))
  875. (float-time (gethash :init-secs package '(0 0 0 0)))
  876. (float-time (gethash :preface-secs package '(0 0 0 0)))
  877. (float-time (gethash :use-package-secs package '(0 0 0 0)))))
  878. (defun use-package-statistics-convert (package)
  879. "Return information about PACKAGE.
  880. The information is formatted in a way suitable for
  881. `use-package-statistics-mode'."
  882. (let ((statistics (gethash package use-package-statistics)))
  883. (list
  884. package
  885. (vector
  886. (symbol-name package)
  887. (use-package-statistics-status statistics)
  888. (use-package-statistics-last-event statistics)
  889. (format "%.2f" (use-package-statistics-time statistics))))))
  890. (defun use-package-report ()
  891. "Show current statistics gathered about use-package declarations.
  892. In the table that's generated, the status field has the following
  893. meaning:
  894. Configured :config has been processed (the package is loaded!)
  895. Initialized :init has been processed (load status unknown)
  896. Prefaced :preface has been processed
  897. Declared the use-package declaration was seen"
  898. (interactive)
  899. (with-current-buffer (get-buffer-create "*use-package statistics*")
  900. (setq tabulated-list-entries
  901. (mapcar #'use-package-statistics-convert
  902. (hash-table-keys use-package-statistics)))
  903. (use-package-statistics-mode)
  904. (tabulated-list-print)
  905. (display-buffer (current-buffer))))
  906. (define-derived-mode use-package-statistics-mode tabulated-list-mode
  907. "use-package statistics"
  908. "Show current statistics gathered about use-package declarations."
  909. (setq tabulated-list-format
  910. ;; The sum of column width is 80 caracters:
  911. #[("Package" 25 t)
  912. ("Status" 13 t)
  913. ("Last Event" 23 t)
  914. ("Time" 10 t)])
  915. (tabulated-list-init-header))
  916. (defun use-package-statistics-gather (keyword name after)
  917. (let* ((hash (gethash name use-package-statistics
  918. (make-hash-table)))
  919. (before (and after (gethash keyword hash (current-time)))))
  920. (puthash keyword (current-time) hash)
  921. (when after
  922. (puthash (intern (concat (symbol-name keyword) "-secs"))
  923. (time-subtract (current-time) before) hash))
  924. (puthash name hash use-package-statistics)))
  925. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  926. ;;
  927. ;;; Handlers
  928. ;;
  929. ;;;; :disabled
  930. ;; Don't alias this to `ignore', as that will cause the resulting
  931. ;; function to be interactive.
  932. (defun use-package-normalize/:disabled (_name _keyword _arg)
  933. "Do nothing, return nil.")
  934. (defun use-package-handler/:disabled (name _keyword _arg rest state)
  935. (use-package-process-keywords name rest state))
  936. ;;;; :if, :when and :unless
  937. (defun use-package-normalize-test (_name keyword args)
  938. (use-package-only-one (symbol-name keyword) args
  939. #'use-package-normalize-value))
  940. (defalias 'use-package-normalize/:if 'use-package-normalize-test)
  941. (defun use-package-handler/:if (name _keyword pred rest state)
  942. (let ((body (use-package-process-keywords name rest state)))
  943. `((when ,pred ,@body))))
  944. (defalias 'use-package-normalize/:when 'use-package-normalize-test)
  945. (defalias 'use-package-handler/:when 'use-package-handler/:if)
  946. (defalias 'use-package-normalize/:unless 'use-package-normalize-test)
  947. (defun use-package-handler/:unless (name _keyword pred rest state)
  948. (let ((body (use-package-process-keywords name rest state)))
  949. `((unless ,pred ,@body))))
  950. ;;;; :requires
  951. (defalias 'use-package-normalize/:requires 'use-package-normalize-symlist)
  952. (defun use-package-handler/:requires (name _keyword requires rest state)
  953. (let ((body (use-package-process-keywords name rest state)))
  954. (if (null requires)
  955. body
  956. `((when ,(if (> (length requires) 1)
  957. `(not (member nil (mapcar #'featurep ',requires)))
  958. `(featurep ',(car requires)))
  959. ,@body)))))
  960. ;;;; :load-path
  961. (defun use-package-normalize/:load-path (_name keyword args)
  962. (use-package-as-one (symbol-name keyword) args
  963. #'use-package-normalize-paths))
  964. (defun use-package-handler/:load-path (name _keyword arg rest state)
  965. (let ((body (use-package-process-keywords name rest state)))
  966. (use-package-concat
  967. (mapcar #'(lambda (path)
  968. `(eval-and-compile (add-to-list 'load-path ,path)))
  969. arg)
  970. body)))
  971. ;;;; :no-require
  972. (defalias 'use-package-normalize/:no-require 'use-package-normalize-predicate)
  973. (defun use-package-handler/:no-require (name _keyword _arg rest state)
  974. (use-package-process-keywords name rest state))
  975. ;;;; :defines
  976. (defalias 'use-package-normalize/:defines 'use-package-normalize-symlist)
  977. (defun use-package-handler/:defines (name _keyword _arg rest state)
  978. (use-package-process-keywords name rest state))
  979. ;;;; :functions
  980. (defalias 'use-package-normalize/:functions 'use-package-normalize-symlist)
  981. (defun use-package-handler/:functions (name _keyword _arg rest state)
  982. (use-package-process-keywords name rest state))
  983. ;;;; :preface
  984. (defalias 'use-package-normalize/:preface 'use-package-normalize-forms)
  985. (defun use-package-handler/:preface (name _keyword arg rest state)
  986. (let ((body (use-package-process-keywords name rest state)))
  987. (use-package-concat
  988. (when use-package-compute-statistics
  989. `((use-package-statistics-gather :preface ',name nil)))
  990. (when arg
  991. `((eval-and-compile ,@arg)))
  992. body
  993. (when use-package-compute-statistics
  994. `((use-package-statistics-gather :preface ',name t))))))
  995. ;;;; :catch
  996. (defvar use-package--form)
  997. (defvar use-package--hush-function #'(lambda (_keyword body) body))
  998. (defsubst use-package-hush (context keyword body)
  999. `((condition-case-unless-debug err
  1000. ,(macroexp-progn body)
  1001. (error (funcall ,context ,keyword err)))))
  1002. (defun use-package-normalize/:catch (_name keyword args)
  1003. (if (null args)
  1004. t
  1005. (use-package-only-one (symbol-name keyword) args
  1006. use-package--hush-function)))
  1007. (defun use-package-handler/:catch (name keyword arg rest state)
  1008. (let* ((context (cl-gentemp "use-package--warning")))
  1009. (cond
  1010. ((not arg)
  1011. (use-package-process-keywords name rest state))
  1012. ((eq arg t)
  1013. `((defvar ,context
  1014. #'(lambda (keyword err)
  1015. (let ((msg (format "%s/%s: %s" ',name keyword
  1016. (error-message-string err))))
  1017. ,@(when (eq use-package-verbose 'debug)
  1018. `((with-current-buffer
  1019. (get-buffer-create "*use-package*")
  1020. (goto-char (point-max))
  1021. (insert "-----\n" msg ,use-package--form)
  1022. (emacs-lisp-mode))
  1023. (setq msg
  1024. (concat msg
  1025. " (see the *use-package* buffer)"))))
  1026. (display-warning 'use-package msg :error))))
  1027. ,@(let ((use-package--hush-function
  1028. (apply-partially #'use-package-hush context)))
  1029. (funcall use-package--hush-function keyword
  1030. (use-package-process-keywords name rest state)))))
  1031. ((functionp arg)
  1032. `((defvar ,context ,arg)
  1033. ,@(let ((use-package--hush-function
  1034. (apply-partially #'use-package-hush context)))
  1035. (funcall use-package--hush-function keyword
  1036. (use-package-process-keywords name rest state)))))
  1037. (t
  1038. (use-package-error "The :catch keyword expects 't' or a function")))))
  1039. ;;;; :interpreter
  1040. (defalias 'use-package-normalize/:interpreter 'use-package-normalize-mode)
  1041. (defalias 'use-package-autoloads/:interpreter 'use-package-autoloads-mode)
  1042. (defun use-package-handler/:interpreter (name _keyword arg rest state)
  1043. (use-package-handle-mode name 'interpreter-mode-alist arg rest state))
  1044. ;;;; :mode
  1045. (defalias 'use-package-normalize/:mode 'use-package-normalize-mode)
  1046. (defalias 'use-package-autoloads/:mode 'use-package-autoloads-mode)
  1047. (defun use-package-handler/:mode (name _keyword arg rest state)
  1048. (use-package-handle-mode name 'auto-mode-alist arg rest state))
  1049. ;;;; :magic
  1050. (defalias 'use-package-normalize/:magic 'use-package-normalize-mode)
  1051. (defalias 'use-package-autoloads/:magic 'use-package-autoloads-mode)
  1052. (defun use-package-handler/:magic (name _keyword arg rest state)
  1053. (use-package-handle-mode name 'magic-mode-alist arg rest state))
  1054. ;;;; :magic-fallback
  1055. (defalias 'use-package-normalize/:magic-fallback 'use-package-normalize-mode)
  1056. (defalias 'use-package-autoloads/:magic-fallback 'use-package-autoloads-mode)
  1057. (defun use-package-handler/:magic-fallback (name _keyword arg rest state)
  1058. (use-package-handle-mode name 'magic-fallback-mode-alist arg rest state))
  1059. ;;;; :hook
  1060. (defun use-package-normalize/:hook (name keyword args)
  1061. (use-package-as-one (symbol-name keyword) args
  1062. #'(lambda (label arg)
  1063. (unless (or (use-package-non-nil-symbolp arg) (consp arg))
  1064. (use-package-error
  1065. (concat label " a <symbol> or (<symbol or list of symbols> . <symbol or function>)"
  1066. " or list of these")))
  1067. (use-package-normalize-pairs
  1068. #'(lambda (k)
  1069. (or (use-package-non-nil-symbolp k)
  1070. (and k (let ((every t))
  1071. (while (and every k)
  1072. (if (and (consp k)
  1073. (use-package-non-nil-symbolp (car k)))
  1074. (setq k (cdr k))
  1075. (setq every nil)))
  1076. every))))
  1077. #'use-package-recognize-function
  1078. name label arg))))
  1079. (defalias 'use-package-autoloads/:hook 'use-package-autoloads-mode)
  1080. (defun use-package-handler/:hook (name _keyword args rest state)
  1081. "Generate use-package custom keyword code."
  1082. (use-package-concat
  1083. (use-package-process-keywords name rest state)
  1084. (cl-mapcan
  1085. #'(lambda (def)
  1086. (let ((syms (car def))
  1087. (fun (cdr def)))
  1088. (when fun
  1089. (mapcar
  1090. #'(lambda (sym)
  1091. `(add-hook
  1092. (quote ,(intern
  1093. (concat (symbol-name sym)
  1094. use-package-hook-name-suffix)))
  1095. (function ,fun)))
  1096. (if (use-package-non-nil-symbolp syms) (list syms) syms)))))
  1097. (use-package-normalize-commands args))))
  1098. ;;;; :commands
  1099. (defalias 'use-package-normalize/:commands 'use-package-normalize-symlist)
  1100. (defun use-package-handler/:commands (name _keyword arg rest state)
  1101. (use-package-concat
  1102. ;; Since we deferring load, establish any necessary autoloads, and also
  1103. ;; keep the byte-compiler happy.
  1104. (let ((name-string (use-package-as-string name)))
  1105. (cl-mapcan
  1106. #'(lambda (command)
  1107. (when (symbolp command)
  1108. (append
  1109. (unless (plist-get state :demand)
  1110. `((unless (fboundp ',command)
  1111. (autoload #',command ,name-string nil t))))
  1112. (when (bound-and-true-p byte-compile-current-file)
  1113. `((eval-when-compile
  1114. (declare-function ,command ,name-string)))))))
  1115. (delete-dups arg)))
  1116. (use-package-process-keywords name rest state)))
  1117. ;;;; :defer
  1118. (defalias 'use-package-normalize/:defer 'use-package-normalize-predicate)
  1119. (defun use-package-handler/:defer (name _keyword arg rest state)
  1120. (let ((body (use-package-process-keywords name rest state)))
  1121. (use-package-concat
  1122. ;; Load the package after a set amount of idle time, if the argument to
  1123. ;; `:defer' was a number.
  1124. (when (numberp arg)
  1125. `((run-with-idle-timer ,arg nil #'require
  1126. ',(use-package-as-symbol name) nil t)))
  1127. (if (or (not arg) (null body))
  1128. body
  1129. `((eval-after-load ',name ',(macroexp-progn body)))))))
  1130. ;;;; :after
  1131. (defun use-package-normalize/:after (name keyword args)
  1132. (setq args (use-package-normalize-recursive-symlist name keyword args))
  1133. (if (consp args)
  1134. args
  1135. (list args)))
  1136. (defun use-package-after-count-uses (features*)
  1137. "Count the number of time the body would appear in the result."
  1138. (cond ((use-package-non-nil-symbolp features*)
  1139. 1)
  1140. ((and (consp features*)
  1141. (memq (car features*) '(:or :any)))
  1142. (let ((num 0))
  1143. (cl-dolist (next (cdr features*))
  1144. (setq num (+ num (use-package-after-count-uses next))))
  1145. num))
  1146. ((and (consp features*)
  1147. (memq (car features*) '(:and :all)))
  1148. (apply #'max (mapcar #'use-package-after-count-uses
  1149. (cdr features*))))
  1150. ((listp features*)
  1151. (use-package-after-count-uses (cons :all features*)))))
  1152. (defun use-package-require-after-load (features* body)
  1153. "Generate `eval-after-load' statements to represents FEATURES*.
  1154. FEATURES* is a list containing keywords `:and' and `:all', where
  1155. no keyword implies `:all'."
  1156. (cond
  1157. ((use-package-non-nil-symbolp features*)
  1158. `((eval-after-load ',features* ',(macroexp-progn body))))
  1159. ((and (consp features*)
  1160. (memq (car features*) '(:or :any)))
  1161. (cl-mapcan #'(lambda (x) (use-package-require-after-load x body))
  1162. (cdr features*)))
  1163. ((and (consp features*)
  1164. (memq (car features*) '(:and :all)))
  1165. (cl-dolist (next (cdr features*))
  1166. (setq body (use-package-require-after-load next body)))
  1167. body)
  1168. ((listp features*)
  1169. (use-package-require-after-load (cons :all features*) body))))
  1170. (defun use-package-handler/:after (name _keyword arg rest state)
  1171. (let ((body (use-package-process-keywords name rest state))
  1172. (uses (use-package-after-count-uses arg)))
  1173. (if (or (null uses) (null body))
  1174. body
  1175. (if (<= uses 1)
  1176. (use-package-require-after-load arg body)
  1177. (use-package-memoize
  1178. (apply-partially #'use-package-require-after-load arg)
  1179. (macroexp-progn body))))))
  1180. ;;;; :demand
  1181. (defalias 'use-package-normalize/:demand 'use-package-normalize-predicate)
  1182. (defun use-package-handler/:demand (name _keyword _arg rest state)
  1183. (use-package-process-keywords name rest state))
  1184. ;;;; :custom
  1185. (defun use-package-normalize/:custom (_name keyword args)
  1186. "Normalize use-package custom keyword."
  1187. (use-package-as-one (symbol-name keyword) args
  1188. #'(lambda (label arg)
  1189. (unless (listp arg)
  1190. (use-package-error
  1191. (concat label " a (<symbol> <value> [comment])"
  1192. " or list of these")))
  1193. (if (use-package-non-nil-symbolp (car arg))
  1194. (list arg)
  1195. arg))))
  1196. (defun use-package-handler/:custom (name _keyword args rest state)
  1197. "Generate use-package custom keyword code."
  1198. (use-package-concat
  1199. (mapcar
  1200. #'(lambda (def)
  1201. (let ((variable (nth 0 def))
  1202. (value (nth 1 def))
  1203. (comment (nth 2 def)))
  1204. (unless (and comment (stringp comment))
  1205. (setq comment (format "Customized with use-package %s" name)))
  1206. `(customize-set-variable (quote ,variable) ,value ,comment)))
  1207. args)
  1208. (use-package-process-keywords name rest state)))
  1209. ;;;; :custom-face
  1210. (defun use-package-normalize/:custom-face (name-symbol _keyword arg)
  1211. "Normalize use-package custom-face keyword."
  1212. (let ((error-msg
  1213. (format "%s wants a (<symbol> <face-spec>) or list of these"
  1214. name-symbol)))
  1215. (unless (listp arg)
  1216. (use-package-error error-msg))
  1217. (cl-dolist (def arg arg)
  1218. (unless (listp def)
  1219. (use-package-error error-msg))
  1220. (let ((face (nth 0 def))
  1221. (spec (nth 1 def)))
  1222. (when (or (not face)
  1223. (not spec)
  1224. (> (length def) 2))
  1225. (use-package-error error-msg))))))
  1226. (defun use-package-handler/:custom-face (name _keyword args rest state)
  1227. "Generate use-package custom-face keyword code."
  1228. (use-package-concat
  1229. (mapcar #'(lambda (def) `(custom-set-faces (backquote ,def))) args)
  1230. (use-package-process-keywords name rest state)))
  1231. ;;;; :init
  1232. (defalias 'use-package-normalize/:init 'use-package-normalize-forms)
  1233. (defun use-package-handler/:init (name _keyword arg rest state)
  1234. (use-package-concat
  1235. (when use-package-compute-statistics
  1236. `((use-package-statistics-gather :init ',name nil)))
  1237. (let ((init-body
  1238. (use-package-hook-injector (use-package-as-string name)
  1239. :init arg)))
  1240. (when init-body
  1241. (funcall use-package--hush-function :init
  1242. (if use-package-check-before-init
  1243. `((when (locate-library ,(use-package-as-string name))
  1244. ,@init-body))
  1245. init-body))))
  1246. (use-package-process-keywords name rest state)
  1247. (when use-package-compute-statistics
  1248. `((use-package-statistics-gather :init ',name t)))))
  1249. ;;;; :load
  1250. (defun use-package-normalize/:load (name keyword args)
  1251. (setq args (use-package-normalize-recursive-symlist name keyword args))
  1252. (if (consp args)
  1253. args
  1254. (list args)))
  1255. (defun use-package-handler/:load (name _keyword arg rest state)
  1256. (let ((body (use-package-process-keywords name rest state)))
  1257. (cl-dolist (pkg arg)
  1258. (setq body (use-package-require (if (eq t pkg) name pkg) nil body)))
  1259. body))
  1260. ;;;; :config
  1261. (defalias 'use-package-normalize/:config 'use-package-normalize-forms)
  1262. (defun use-package-handler/:config (name _keyword arg rest state)
  1263. (let* ((body (use-package-process-keywords name rest state))
  1264. (name-symbol (use-package-as-symbol name)))
  1265. (use-package-concat
  1266. (when use-package-compute-statistics
  1267. `((use-package-statistics-gather :config ',name nil)))
  1268. (if (or (null arg) (equal arg '(t)))
  1269. body
  1270. (use-package-with-elapsed-timer
  1271. (format "Configuring package %s" name-symbol)
  1272. (funcall use-package--hush-function :config
  1273. (use-package-concat
  1274. (use-package-hook-injector
  1275. (symbol-name name-symbol) :config arg)
  1276. body
  1277. (list t)))))
  1278. (when use-package-compute-statistics
  1279. `((use-package-statistics-gather :config ',name t))))))
  1280. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1281. ;;
  1282. ;;; The main macro
  1283. ;;
  1284. (defmacro use-package-core (name args)
  1285. `(let* ((args* (use-package-normalize-keywords ,name ,args))
  1286. (use-package--form
  1287. (if (eq use-package-verbose 'debug)
  1288. (concat "\n\n"
  1289. (pp-to-string `(use-package ,name ,@,args))
  1290. "\n -->\n\n"
  1291. (pp-to-string `(use-package ,name ,@args*))
  1292. "\n ==>\n\n"
  1293. (pp-to-string
  1294. (macroexp-progn
  1295. (let ((use-package-verbose 'errors)
  1296. (use-package-expand-minimally t))
  1297. (use-package-process-keywords name args*
  1298. (and (plist-get args* :demand)
  1299. (list :demand t)))))))
  1300. "")))
  1301. (use-package-process-keywords name args*
  1302. (and (plist-get args* :demand)
  1303. (list :demand t)))))
  1304. ;;;###autoload
  1305. (defmacro use-package (name &rest args)
  1306. "Declare an Emacs package by specifying a group of configuration options.
  1307. For full documentation, please see the README file that came with
  1308. this file. Usage:
  1309. (use-package package-name
  1310. [:keyword [option]]...)
  1311. :init Code to run before PACKAGE-NAME has been loaded.
  1312. :config Code to run after PACKAGE-NAME has been loaded. Note that
  1313. if loading is deferred for any reason, this code does not
  1314. execute until the lazy load has occurred.
  1315. :preface Code to be run before everything except `:disabled'; this
  1316. can be used to define functions for use in `:if', or that
  1317. should be seen by the byte-compiler.
  1318. :mode Form to be added to `auto-mode-alist'.
  1319. :magic Form to be added to `magic-mode-alist'.
  1320. :magic-fallback Form to be added to `magic-fallback-mode-alist'.
  1321. :interpreter Form to be added to `interpreter-mode-alist'.
  1322. :commands Define autoloads for commands that will be defined by the
  1323. package. This is useful if the package is being lazily
  1324. loaded, and you wish to conditionally call functions in your
  1325. `:init' block that are defined in the package.
  1326. :hook Specify hook(s) to attach this package to.
  1327. :bind Bind keys, and define autoloads for the bound commands.
  1328. :bind* Bind keys, and define autoloads for the bound commands,
  1329. *overriding all minor mode bindings*.
  1330. :bind-keymap Bind a key prefix to an auto-loaded keymap defined in the
  1331. package. This is like `:bind', but for keymaps.
  1332. :bind-keymap* Like `:bind-keymap', but overrides all minor mode bindings
  1333. :defer Defer loading of a package -- this is implied when using
  1334. `:commands', `:bind', `:bind*', `:mode', `:magic', `:hook',
  1335. `:magic-fallback', or `:interpreter'. This can be an integer,
  1336. to force loading after N seconds of idle time, if the package
  1337. has not already been loaded.
  1338. :after Defer loading of a package until after any of the named
  1339. features are loaded.
  1340. :demand Prevent deferred loading in all cases.
  1341. :if EXPR Initialize and load only if EXPR evaluates to a non-nil value.
  1342. :disabled The package is ignored completely if this keyword is present.
  1343. :defines Declare certain variables to silence the byte-compiler.
  1344. :functions Declare certain functions to silence the byte-compiler.
  1345. :load-path Add to the `load-path' before attempting to load the package.
  1346. :diminish Support for diminish.el (if installed).
  1347. :delight Support for delight.el (if installed).
  1348. :custom Call `customize-set-variable' with each variable definition.
  1349. :custom-face Call `customize-set-faces' with each face definition.
  1350. :ensure Loads the package using package.el if necessary.
  1351. :pin Pin the package to an archive."
  1352. (declare (indent 1))
  1353. (unless (memq :disabled args)
  1354. (macroexp-progn
  1355. (use-package-concat
  1356. (when use-package-compute-statistics
  1357. `((use-package-statistics-gather :use-package ',name nil)))
  1358. (if (eq use-package-verbose 'errors)
  1359. (use-package-core name args)
  1360. (condition-case-unless-debug err
  1361. (use-package-core name args)
  1362. (error
  1363. (ignore
  1364. (display-warning
  1365. 'use-package
  1366. (format "Failed to parse package %s: %s"
  1367. name (error-message-string err)) :error)))))
  1368. (when use-package-compute-statistics
  1369. `((use-package-statistics-gather :use-package ',name t)))))))
  1370. (put 'use-package 'lisp-indent-function 'defun)
  1371. (provide 'use-package-core)
  1372. ;; Local Variables:
  1373. ;; indent-tabs-mode: nil
  1374. ;; End:
  1375. ;;; use-package-core.el ends here