Summary: undid failed test
< (require 'benchmark)
< (defun my-require (feat)
< (if (featurep feat)
< (progn (message "erraneous usage: '%s'" feat) nil)
< (message "'%s' loaded in %.2fs" feat
< (benchmark-elapse (load-library (symbol-name feat))))))
> (require 'benchmark)
> (defun my-require (feat)
> (if (featurep feat)
> (progn (message "erraneous usage: '%s'" feat) nil)
> (message "'%s' loaded in %.2fs" feat
> (benchmark-elapse (load-library (symbol-name feat))))))
In Emacs Lisp code, you can require the loading of a Lisp library that provides a feature, as follows:
(require 'the-feature "the-library-file" <soft-flag>)
A library declares that it provides a certain feature as follows:
The first argument to
‘require’ is a symbol naming the required feature. The second argument is an optional string that names the file (library) that provides the feature (with or without extension). If the second argument is missing or nil, then the print-name of the first argument is used as the file name.
The optional third argument, if non-nil, specifies that no error is to be raised if loading the library fails for some reason (
‘nil’ is returned instead). The most common cause for such failure is that the library cannot be found in your
If the optional third argument is non-nil, then the
‘require’ expression is a soft
‘require’; otherwise, it is a hard
(require 'frame-cmds nil t)– no error if not found
(require 'ring+)– error if not found
This page describes a function ‘safe-load’, and this .emacs contains a ‘try-require’, which inspired this ‘my-require’ function that gives a message if the library is already loaded (to help remove duplicate or redundant ‘requires’ from your init), and provide a benchmark if the library is loaded.
(require 'benchmark) (defun my-require (feat) (if (featurep feat) (progn (message "erraneous usage: '%s'" feat) nil) (message "'%s' loaded in %.2fs" feat (benchmark-elapse (load-library (symbol-name feat))))))
Also see avoiding-the-tyranny-of-other-peoples-decisions for another require that doesn’t default to loading the .elc (as opposed to .el).
See also LibraryDependencies.