with-eval-after-load-feature.el --- Eval after loading feature with fine compilation
Compiling a form passed to
with-eval-after-load have a problem that
it causes "free variable" warnings during the compilation to access a
variable defined in the feature you are waiting for loading. To avoid
loads the feature
before the compilation and behaves the same what
with-eval-after-load( package body... )
This macro is defined as a core functionality in Emacs 24.4 and later. It does the same as
eval-after-loadexcept that the body is not be quoted.
with-eval-after-load-feature( package body... )
Arrange that if package is loaded, body will be run immediately afterwards. This is equivalent to
with-eval-after-loadexcept two additional features: the package is automatically loaded when body is compiled, and, package can be a list form.
Note that body is compiled as a function body by the following code. Don't put anything which should not be in a function body.
(byte-compile `(lambda () ,@body))
Consider to compile the following code. You will get "Warning: reference to free variable `anything-map'" warning.
(with-eval-after-load 'anything (define-key anything-map (kbd "M-n") #'anything-next-source) (define-key anything-map (kbd "M-p") #'anything-previous-source))
with-eval-after-load-feature instead of
suppresses the warning since the feature is automatically loaded at
(with-eval-after-load-feature 'anything (define-key anything-map (kbd "M-n") #'anything-next-source) (define-key anything-map (kbd "M-p") #'anything-previous-source))
If you need to wait loading multiple features, you can pass a list form.
(with-eval-after-load-feature (evil anything-config) (define-key evil-motion-state-map (kbd ":") #'anything-for-files))
This is equivalent to the following code.
(with-eval-after-load-feature 'evil (with-eval-after-load-feature 'anything-config (define-key evil-motion-state-map (kbd ":") #'anything-for-files)))
The original implementation
An idea to suppress warnings when