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.

glucas
  • 20,175
  • 1
  • 51
  • 83
bsamek
  • 213
  • 1
  • 3
  • 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 04 '15 at 17:31
  • Macros can be advised also. Case closed. – politza Mar 04 '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 04 '15 at 19:10
  • I didn't know that either, before I read the info site about nadvice.el . – politza Mar 05 '15 at 02: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

2 Answers2

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.

glucas
  • 20,175
  • 1
  • 51
  • 83
  • 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 04 '15 at 19:39
  • 1
    Macros are evaluated during compilation, hence the advice must be available at compilation time, either by `require`ing a feature providing the advice, or with `eval-when-compile`. –  Mar 18 '16 at 09: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.

Tianxiang Xiong
  • 3,848
  • 16
  • 27