Reputation: 202535
I am trying to program GNU Emacs 23 to issue require
commands lazily, on demand, instead of up front in my .emacs
file. If I wanted to delay the execution of a load
command, I could use autoload
. But require
and load
take different sorts of arguments.
Is there a predefined function that does for require
the same job that autoload
does for load
? And if not, what tools would people recommend that I use to roll my own?
Upvotes: 8
Views: 3192
Reputation: 73274
One more answer to help clarify (this was a little verbose for a comment):
autoload
says "if this function isn't already defined, then load
this file (if and when the function is called)."require
says "if this library isn't already loaded, then load
this file (immediately)."Note in particular that you don't need to use require
to load a library; that's simply the way you ensure that you don't load it again (assuming you don't want to do that). The (provide 'FEATURE)
expression in the library will be evaluated no matter how the library was loaded, which lets any future require
know that it doesn't need to do anything.
It's a similar situation for autoload
-- if the file has already been loaded (and therefore the function in question properly defined), then the autoload no longer has any effect.
Upvotes: 4
Reputation:
There is not difference between require
and load
with regards to autoload
. require
is just a frontend to load
, which more or less comes down to:
(defun require (feature &optional filename noerror)
(unless (featurep feature)
(let ((filename (or filename (symbol-name feature))))
(load filename noerror))))
As you can see, the symbol name given to require
is equal to the filename given to load
. As a matter of fact, the first (require 'foo)
evaluated in an Emacs session is equivalent to (load "foo")
.
Thus, you can just use (auto-load 'foo-function "foo")
for foo-function
from the library foo
, that you can load with (require 'foo)
.
Upvotes: 7
Reputation: 30699
What kind of "demand" do you have in mind for your "on demand"?
If a given command or other function needs (or soft-needs) a given library, then that function itself can use (require 'foo)
or (require 'foo nil t)
. The library will be loaded on demand from that function.
Consider also whether you might need to load the file more than once, i.e., reload it in some situations, whether or not it has already been loaded.
For #2, for instance, my code that uses a library of Lisp macros, icicles-mac.el
does not just use require
, because I want to make sure that if a user gets a new version of that library (e.g., downloads a new source version and byte-compiles it) then that new version is used whenever s?he byte-compiles another library that needs it. (This is important -- when a library of macros changes, other libraries that use those macros generally need to be recompiled after loading the new macros file.) For that, instead of just (require 'icicles-mac)
I use this:
(eval-when-compile
(or (condition-case nil
(load-library "icicles-mac") ; Use load-library to ensure latest .elc.
(error nil))
(require 'icicles-mac))) ; Require, so can load separately if not on `load-path'.
Upvotes: 2