7

I use use-package to organize my init.el. I noticed that all my declarations use :ensure t. An example declaration is:

(use-package auto-complete
  :ensure t
  :diminish auto-complete-mode
  :init (global-auto-complete-mode t))

Since all my use-package declarations use :ensure t, is there a way to modify use-package to implicitly include :ensure t without modifying use-package.el directly? Ideally I'd like not to use a differently-named function.

I'm a little bit familiar with advice-add, but my impression is that this is used to add a hook or environment to a function, not to modify the function or macro itself.

  • 2
    Advice won't help here, as you noted: use-package is actually a Lisp macro. You can probably create your own my-use-package macro which expands to use-package with the default settings your prefer. – glucas Mar 4 '15 at 17:31
  • Macros can be advised also. Case closed. – politza Mar 4 '15 at 18:42
  • @politza True -- not sure what I was thinking. :-) Could you provide an example how that might work in this case? I suppose you could use :filter-args advice to add keywords like :ensure t? – glucas Mar 4 '15 at 19:10
  • I didn't know that either, before I read the info site about nadvice.el . – politza Mar 5 '15 at 2:03
  • I understand the solution here works but I think the same thing to do would have been to write a wrapper function for use-package and just use that wrapper instead. It's much more clear, and it sounds like all of the code involved is under your control. – Joseph Garvin Oct 16 '17 at 21:10
6

Based on @politza's comment that (of course!) advice works with macros, I found something that seems to work.

(defun use-package-always-ensure (form)
  (append form '(:ensure t)))

(advice-add #'use-package :filter-args #'use-package-always-ensure)

Note this example is using the Emacs 24.4 advice functions. I haven't figured out what the equivalent would be for older versions of Emacs.

  • I'm not really sure how this use of advice affects the behavior of byte-compiling files that use the advised macro... Can anyone comment on that? – glucas Mar 4 '15 at 19:39
  • 1
    Macros are evaluated during compilation, hence the advice must be available at compilation time, either by requireing a feature providing the advice, or with eval-when-compile. – lunaryorn Mar 18 '16 at 9:59
5

The newer version of use-package has the following to enable this behavior for all packages:

(setq use-package-always-ensure t) 

Just in case anyone else had the same question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.