4

I am following this tutorial, and the author is doing something like this:

~/.emacs.d/init.el

;; add your modules path
(add-to-list 'load-path "~/.emacs.d/custom/")

;; load your modules
(require 'setup-foo)
(require 'setup-bar)
......

~/.emacs.d/custome/setup-foo.el

(provide 'setup-foo)

;; various configuration settings for foo

~/.emacs.d/custome/setup-bar.el

(provide 'setup-bar)

;; various configuration settings for bar

I am wondering what's the advantage of doing this instead of simply loading everything.

From my understanding, the point of using provide/require is to enable "lazy loading", so that a feature is only loaded when needed, but init.el always load the setup-*.el scripts, which renders this practice pointless to me.

I wouldn't say this question is a duplicate of What does (require 'package) mean for emacs and how does it differ from load-file?, though they're indeed related. My question ask on how to use the two different mechanisms, whereas the other one is about what they are.

nalzok
  • 665
  • 6
  • 18
  • Another advantage of provide/require is that it stops you from accidentally loading a file twice, but that too seems irrelevant to your case. So personally, I'd go for just loading the files. By the way, it is customary to put the `provide` at the end of each file, so that if the file fails to load for some reason, the corresponding feature is not enabled – and a new `require` will retry loading the file. – Harald Hanche-Olsen Feb 12 '19 at 10:53
  • @Harald remove the word "Another" from your comment, and I would say that is the answer. `provide`/`require` isn't related to "lazy"/autoloading. – npostavs Feb 12 '19 at 14:02
  • @npostavs Good point. Stefan already provided an answer to that effect, so that is good enough. – Harald Hanche-Olsen Feb 12 '19 at 15:14
  • Also, `require` lets you do a "soft require", which does not raise an error if the library is not available: `(require 'foo nil t)`. And please see the doc, starting perhaps with the Emacs manual, node [How Programs Do Loading](https://www.gnu.org/software/emacs/manual/html_node/elisp/How-Programs-Do-Loading.html). – Drew Feb 12 '19 at 16:05
  • Your latest edit would have been more appropriate as a comment, I think. – Harald Hanche-Olsen Feb 13 '19 at 13:26
  • @HaraldHanche-Olsen My question was marked a “possible duplicate”, so I got a notification asking me to “edit to clarify” :P – nalzok Feb 13 '19 at 13:41
  • Yes, but the edit does not clarify the question; you are merely arguing that the question is not a duplicate. Nothing wrong with that, but a comment is more appropriate for such discussion. (In my opinion – others may disagree.) – Harald Hanche-Olsen Feb 13 '19 at 15:13
  • @Drew `load` also lets you do a "soft load": `(load "foo" t)`. – npostavs Feb 14 '19 at 19:38
  • @npostavs: True. – Drew Feb 14 '19 at 19:45

1 Answers1

3

The point of require is not lazy-loading (that would be autoload instead). Instead, the purpose is to avoid loading the same package multiple times.

In the case of config files, which of load or require is preferable is unclear and will depend on your particular use case (but I think in most cases the difference will be negligible).

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • I think it‘s no problem to describe `require` as lazy-loading, as the manual puts "provide and require are an alternative to autoload for loading files automatically." And the canonical use case is to put `require` in the body some other function definition to ensure loading. Both `require` and `autoload` provide an idempotent trigger to make lazy-loading possible. – nichijou Jan 22 '22 at 16:27