Use-package: :init vs :config

Created on 20 Oct 2016  Â·  11Comments  Â·  Source: jwiegley/use-package

Thanks so much for the great package. I am not sure exactly when should I use :init and :config. For, exapmle, the next code in my .emacs how I write it using use-packge:

(add-to-list 'load-path "/home/wyousef/Downloads/sage-7.0/local/share/emacs/site-lisp/sage-mode")
(require 'sage "sage")
(setq sage-command "/home/wyousef/Downloads/sage-7.0/sage")
(require 'sage-view "sage-view")
(add-hook 'sage-startup-after-prompt-hook 'sage-view)
(add-hook 'sage-startup-after-prompt-hook 'sage-view-enable-inline-output)
(add-hook 'sage-startup-after-prompt-hook 'sage-view-enable-inline-plots)
(setq sage-view-inline-plots-method 'emacsclient)
(setq sage-view-scale 1.4)
(setq sage-view-scale-factor 0.2)

Most helpful comment

Use :init for code that still has meaning if the package is not loaded. Primarily, this is code that you'd like in place even if loading is deferred until a later time. Use :config for code that has meaning after the package is loaded.

Note that this does not mean that :init _will_ happen before loading; it's always possible that a package might be loaded before your use-package form is encountered. But any code in the :init section should not assume that the package is available.

All 11 comments

Use :init for code that still has meaning if the package is not loaded. Primarily, this is code that you'd like in place even if loading is deferred until a later time. Use :config for code that has meaning after the package is loaded.

Note that this does not mean that :init _will_ happen before loading; it's always possible that a package might be loaded before your use-package form is encountered. But any code in the :init section should not assume that the package is available.

1- how the package is loaded before use-package is encountered.

2- could you please write the code, that I just included, in a use-package
format so that I better understand. When I did it, I added :load-path
statement; however use-package contacted melpa.

On Oct 20, 2016 7:02 PM, "John Wiegley" [email protected] wrote:

Use :init for code that still has meaning if the package is not loaded.
Primarily, this is code that you'd like in place even if loading is
deferred until a later time. Use :config for code that has meaning after
the package is loaded.

Note that this does not mean that :init _will_ happen before loading;
it's always possible that a package might be loaded before your
use-package form is encountered. But any code in the :init section should
not assume that the package is available.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/jwiegley/use-package/issues/394#issuecomment-255166069,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALpbzZ9AsPCRN6GNNfaX9nUpunavwcVSks5q1561gaJpZM4KcNeT
.

@DrWaleedAYousef 1. (require 'foo) (foo-something) (use-package foo)

  1. Can you show me what you wrote?

Thanks so much for your reply; this is how I wrote it:

.
.
.
(setq use-package-always-ensure t)
.
.
.

(use-package sage "sage"
:load-path "/home/wyousef/Downloads/sage-7.0/local/share/emacs/site-lisp/sage-mode/"
:init
(setq sage-command "/home/wyousef/Downloads/sage-7.0/sage")
(require 'sage-view "sage-view")
(add-hook 'sage-startup-after-prompt-hook 'sage-view)
(add-hook 'sage-startup-after-prompt-hook 'sage-view-enable-inline-output)
(add-hook 'sage-startup-after-prompt-hook 'sage-view-enable-inline-plots)
:config
(setq sage-view-inline-plots-method 'emacsclient)
(setq sage-view-scale 1.4)
(setq sage-view-scale-factor 0.2)
)

Also, how can I use-package for packages called as:

(pdf-tools-install)

Thanks so much for your support.

John Wiegley [email protected] writes:

@DrWaleedAYousef 1. (require 'foo) (foo-something) (use-package foo)

  1. Can you show me what you wrote?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.*

I would likely have written something like:

(use-package sage
  :load-path
  "/home/wyousef/Downloads/sage-7.0/local/share/emacs/site-lisp/sage-mode/"
  :init
  (setq sage-command "/home/wyousef/Downloads/sage-7.0/sage")
  :config
  (use-package sage-view
    :config
    (add-hook 'sage-startup-after-prompt-hook 'sage-view)
    (add-hook 'sage-startup-after-prompt-hook 'sage-view-enable-inline-output)
    (add-hook 'sage-startup-after-prompt-hook 'sage-view-enable-inline-plots))
  (setq sage-view-inline-plots-method 'emacsclient)
  (setq sage-view-scale 1.4)
  (setq sage-view-scale-factor 0.2))

;; And probably something like...

(use-package pdf-tools
  :init (pdf-tools-install))

When I did it, I added :load-path statement; however use-package contacted melpa.

Note you need :ensure nil to counteract the (setq use-package-always-ensure t), see also #190.

Thanks so much for both of your replies:

Regarding pdf-tools, I did as you said; however emacs takes few seconds
loading it at the startup! I thought use-package would defer that delay.

(use-package pdf-tools
:init (pdf-tools-install))

Regarding sage, I did as you said; in addition I added :ensure nil. It
also keeps contacting Melpa!

(use-package sage
:ensure nil
:load-path "/home/wyousef/Downloads/sage-7.0/local/share/emacs/site-lisp/sage-mode/"
:init (setq sage-command "/home/wyousef/Downloads/sage-7.0/sage")
:config
(use-package sage-view
:config
(add-hook 'sage-startup-after-prompt-hook 'sage-view)
(add-hook 'sage-startup-after-prompt-hook 'sage-view-enable-inline-output)
(add-hook 'sage-startup-after-prompt-hook 'sage-view-enable-inline-plots))
(setq sage-view-inline-plots-method 'emacsclient)
(setq sage-view-scale 1.4)
(setq sage-view-scale-factor 0.2))

Noam Postavsky [email protected] writes:

When I did it, I added :load-path statement; however use-package contacted melpa.

Note you need :ensure nil to counteract the (setq use-package-always-ensure t), see also #190.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.*

Regarding sage, I did as you said; in addition I added :ensure nil. It also keeps contacting Melpa!

You need :ensure nil on the inner (use-package sage-view ...) as well.

Regarding pdf-tools, I did as you said; however emacs takes few seconds
loading it at the startup! I thought use-package would defer that delay.

You'd need to say:

(use-package pdf-tools
  :defer 10
  :init (pdf-tools-install))

So that it only installs it after 10 seconds of idle time.

I tried it and it works; but I do not understand it:

1- why not use: :defer t
2- why I have to use any :defer; why it does not defer automatically as
the default action of use-package
3- what is '''elisp?

Thanks so much.

John Wiegley [email protected] writes:

Regarding pdf-tools, I did as you said; however emacs takes few seconds
loading it at the startup! I thought use-package would defer that delay.

You'd need to say:

(use-package pdf-tools
:defer 10
:init (pdf-tools-install))

So that it only installs it after 10 seconds of idle time.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.*

1- why not use: :defer t

Because without some other trigger, the package will never be loaded until you
explicit use M-x load.

2- why I have to use any :defer; why it does not defer automatically as
the default action of use-package

This is only the defer if you've specified some form of trigger,
using :mode, :bind or :commands.

3- what is '''elisp?

Oops, GitHub is supposed to render that block for me using Emacs Lisp syntax.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kpbochenek picture kpbochenek  Â·  8Comments

jtecca picture jtecca  Â·  9Comments

kirk86 picture kirk86  Â·  6Comments

DarkWingMcQuack picture DarkWingMcQuack  Â·  4Comments

sondr3 picture sondr3  Â·  14Comments