`with-eval-after-load` with requering the feature during the compilation
Emacs Lisp
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.md
with-eval-after-load-feature.el

README.md

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 this, with-eval-after-load-feature requires or loads the feature before the compilation and behaves the same what with-eval-after-load does.

Macros

  • 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-load except 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-load except 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))

Example

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))

Using with-eval-after-load-feature instead of with-eval-after-load suppresses the warning since the feature is automatically loaded at compile time.

(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)))

Acknowledgment