What should I use:
(when (package-installed-p 'ivy)
(global-set-key (kbd "C-x b") #'ivy-switch-buffer))
or:
(when (fboundp 'ivy-switch-buffer)
(global-set-key (kbd "C-x b") #'ivy-switch-buffer))
or:
(eval-after-load ivy ...) ; haven't tested it yet
I try to rebind key based on a package presence. We are in 2021 so it is safe to assume of presence of package.el
but what if I installed a package from git/hg?
UPDATE With fboundp
& compilation of .emacs
it doesn't redefine key.
Works with package-installed-p
though... I haven't (ivy-mode +1)
so function definition ivy-switch-buffer
can be in form of autoload
...
require
. You can do that without raising an error if not found by doing(require 'the-library nil t)
. SeeC-h f require
.require
adds delay to boot time. Packages register autoloads to avoid eager loading... I was searching for the way to discover such "registration".package-installed-p
&eval-after-load
are nice alternatives. update it is for.emacs
. For libraries only therequire
is viable option...require
(e.g.(require 'foo nil t)
) doesn't add delay, if the library has already been loaded. It just checksload-history
. It takes time only if the library has not yet been loaded, by loading the library. It all depends on what behavior you want. As I said, if you want to actually make sure the library is loaded when the sexp is evaluated then use a soft require. (If you want to load the library, and bind the key, at some later time, that's a different story.)