- Do I need
(package-initialize)
before these lines?
If your version of Emacs (M-xemacs-version
RET) is older than 27, and you are using the built-in package manager, then you indeed need to call (package-initialize)
before you can call on any installed packages, such as use-package
.
From Emacs 27 onwards, package-initialize
will effectively be called before user-init-file
is loaded. This can be disabled by setting package-enable-at-startup
to nil
in the new early-init-file
.
It seems to me that without doing that, the use-package
macro wouldn't be available.
It depends. The example is written from the perspective of someone who doesn't use the built-in package manager (e.g. the author of use-package
), and who manually installs packages so that they can be found by Emacs on their load-path
. This is what package-install
and package-initialize
effectively automate for the user.
- I do not need the 4th line in the above snippet, right? As I have installed
use-package
from MELPA Stable, and the package is installed to the default location ~/.emacs.d/elpa/use-package-2.4
.
Correct. If you install packages using the package manager and call package-initialize
, then you do not need to manually edit your load-path
.
- In this example, is
(use-package foo)
equivalent to (require 'foo)
?
Let's find out: with point after the closing parenthesis of (use-package foo)
, call M-xpp-macroexpand-last-sexp
RET. This will print what use-package
is actually doing under the bonnet to the buffer *Pp Macroexpand Output*
:
(progn
(defvar use-package--warning0
(lambda (keyword err)
(let ((msg (format "%s/%s: %s" 'foo keyword
(error-message-string err))))
(display-warning 'use-package msg :error))))
(condition-case-unless-debug err
(unless (require 'foo nil t)
(display-warning 'use-package
(format "Cannot load %s" 'foo)
:error))
(error
(funcall use-package--warning0 :catch err))))
Stripping away all of the error reporting, we're left with:
(require 'foo nil t)
Which is like (require 'foo)
, but doesn't signal an error if the named feature foo
can't be found.