Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use `require` a package which is `autoload`?

For example, evil is autoload,

Some blogs/threads will use

(require 'evil)
(evil-mode 1)

to enable evil mode.

AFAIK, Elisp is lisp2 and evil-mode is also a function, so doesn't need setq.

(defadvice evil-mode (after start-evil activate)
  "Enable Evil in Fundamental mode."
  (if evil-mode
      (progn
        (when (eq (default-value 'major-mode) 'fundamental-mode)
          ;; changed back by `evil-local-mode'
          (setq-default major-mode 'turn-on-evil-mode))
        (ad-enable-regexp "^evil")
        (ad-activate-regexp "^evil")
        (with-no-warnings (evil-esc-mode 1)))
    (when (eq (default-value 'major-mode) 'turn-on-evil-mode)
      (setq-default major-mode 'fundamental-mode))
    (ad-disable-regexp "^evil")
    (ad-update-regexp "^evil")
    (with-no-warnings (evil-esc-mode -1))))

But, because of autoload,

(evil-mode 1)

Without require, it also works fine(at least for me)

(If anywhere below is wrong, please correct me, thx)

My question is when must I use require if the required package is autoloaded

like image 902
Chen Li Avatar asked Jul 14 '18 01:07

Chen Li


1 Answers

You're correct -- if an autoload declaration for evil-mode has been evaluated, then there is no need to (require 'evil) prior to (evil-mode 1)

Individual users may or may not need to (require 'evil) though. It's a question of how the library has been installed. On its own an ;;;###autoload cookie does nothing; but if you installed evil using package.el, then they will have been parsed and turned into a file of autoload declarations, which Emacs will load when packages are initialised.

If you install a library without using a package manager, then you may need to require it, or else write your own autoload declaration for the functions you might be calling.

like image 166
phils Avatar answered Nov 20 '22 10:11

phils