Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the Emacs function (eval-and-compile...)?

Tags:

emacs

elisp

I can read the documentation, so I'm not asking for a cut-and-paste of that.

I'm trying to understand the motivation for this function.

When would I want to use it?

like image 724
Cheeso Avatar asked May 25 '10 12:05

Cheeso


People also ask

What is Lisp evaluation?

10.1 Introduction to Evaluation. The Lisp interpreter, or evaluator, is the part of Emacs that computes the value of an expression that is given to it. When a function written in Lisp is called, the evaluator computes the value of the function by evaluating the expressions in the function body.

What is Emacs Lisp used for?

Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.

How do I run a function in Emacs?

This function can be run simply. The command [Alt][x] is used to a run a function interactively. Typing [Alt][x] switches the focus in Emacs to the minibuffer - if you then type in a function name it will be executed. To run doodlebug simply type [Alt][x] and then doodlebug.


1 Answers

The documentation in the Emacs lisp manual does have some example situations that seem to answer your question (as opposed to the doc string).

From looking at the Emacs source code, eval-and-compile is used to quiet the compiler, to make macros/functions available during compilation (and evaluation), or to make feature/version specific variants of macros/functions available during compilation.

One usage I found helpful to see was in ezimage.el. In there, an if statement was put inside the eval-and-compile to conditionally define macros depending on whether the package was compiled/eval'ed in Emacs or XEmacs, and additionally whether a particular feature was present. By wrapping that conditional inside the eval-and-compile you enable the appropriate macro usage during compilation. A similar situation can be found in mwheel.el.

Similarly, if you want to define a function via fset and have it available during compilation, you need to have the call to fset wrapped with eval-and-compile because otherwise the symbol -> function association isn't available until the file is evaluated (because compilation of a call to fset just optimizes the assignment, it doesn't actually do the assignment). Why would you want this assignment during compilation? To quiet the compiler. Note: this is just my re-wording of what is in the elisp documentation.

I did notice a lot of uses in Emacs code which just wrapped calls to require, which sounds redundant when you read the documentation. I'm at a loss as to how to explain those.

like image 97
Trey Jackson Avatar answered Oct 20 '22 16:10

Trey Jackson