Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "with-eval-after-load" in Emacs Lisp

I came across the macro with-eval-after-load when trying to install persp-mode from here. But I am unable to find the macro inside Emacs and/or on Google. Where is it defined? Is it part of standard Emacs Lisp?

like image 235
Talespin_Kit Avatar asked Feb 19 '14 12:02

Talespin_Kit


People also ask

How do I load a lisp in emacs?

To load an Emacs Lisp file, type M-x load-file . This command reads a file name using the minibuffer, and executes the contents of that file as Emacs Lisp code. It is not necessary to visit the file first; this command reads the file directly from disk, not from an existing Emacs buffer.

What does #' mean in Emacs Lisp?

#'... is short-hand for (function ...) which is simply a variant of '... / (quote ...) that also hints to the byte-compiler that it can compile the quoted form as a function.


1 Answers

From etc/NEWS:

* Lisp Changes in Emacs 24.4 ... ** New macro `with-eval-after-load'. This is like the old `eval-after-load', but better behaved. 

Emacs 24.4 was released on 20th October 2014.

eval-after-load is considered ill-behaved because it is a function, not a macro, and thus requires the code inside it to be quoted, which means that it cannot be byte-compiled. It also accepts only one form, so if you have more than one, you need to use progn. For example:

(eval-after-load "foo"   '(progn      (setq foo 42)      (setq bar 17))) 

The equivalent version with with-eval-after-load would be:

(with-eval-after-load "foo"   (setq foo 42)   (setq bar 17)) 

As noted by Clément in a comment, one disadvantage of with-eval-after-load is that you cannot rely on macros defined in the module in question, while with eval-after-load you can be sure that such macros are defined and available to use. This was discussed on emacs-devel.

like image 50
legoscia Avatar answered Sep 20 '22 16:09

legoscia