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 load
ing 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.
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 newrequire
will retry loading the file.provide
/require
isn't related to "lazy"/autoloading.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.