ArticleslgStudy

science

Emacs Lisp

Emacs Lisp is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Emacs Lisp rather than just read about it. In short: Emacs Lisp is a Lisp dialect made for GNU Emacs. It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.

Emacs Lisp — main illustration
Emacs Lisp — illustration

Key takeaways

  • Emacs Lisp belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Emacs Lisp to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Emacs Lisp from memory before moving on to harder problems.

Reference excerpt

Emacs Lisp is a Lisp dialect made for GNU Emacs. It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter. Emacs Lisp code is used to modify, extend and customize Emacs. Those not wanting to write the code themselves can use the Customize function instead. It provides a set of preferences pages allowing the user to set options and preview their effect in the running Emacs session. When the user saves their changes, Customize simply writes the necessary Emacs Lisp code to the user's config file, which can be set to a special file that only Customize uses, to avoid the possibility of altering the user's own file. Besides being a programming language that can be compiled to bytecode and to machine code, Emacs Lisp can also function as an interpreted scripting language, much like the Unix Bourne shell or Perl, by calling Emacs in batch mode. In this way it may be called from the command line or via an executable file, and its editing functions, such as buffers and movement commands are available to the program just as in the normal mode. No user interface is presented when Emacs is started in batch mode; it simply executes the passed-in script and exits, displaying any output from the script. Emacs Lisp is also termed Elisp, although there are also older, unrelated Lisp dialects with that name.

Compared to other Lisp dialects Emacs Lisp is most closely related to Maclisp, with some later influence from Common Lisp. It supports imperative and functional programming methods. Lisp was the default extension language for Emacs derivatives such as EINE and ZWEI. When Richard Stallman forked Gosling Emacs into GNU Emacs, he also chose Lisp as the extension language, because of its powerful features, including the ability to treat functions as data. Although the Common Lisp standard had yet to be formulated, Scheme existed at the time, but Stallman chose not to use it because of its comparatively poor performance on workstations (relative to the minicomputers that were Emacs' traditional home), and he wanted to develop a dialect which he thought would be more easily optimized. The Lisp dialect used in Emacs differs substantially from the more modern Common Lisp and Scheme dialects used for applications programming. A prominent characteristic of Emacs Lisp is in its use of dynamic rather than lexical scope by default. That is, a function may reference local variables in the scope it is called from, but not in the scope where it was defined. Recently, there has been an ongoing effort to update code to use lexical scoping, for reasons outlined below.

Example The development of Emacs Lisp was guided by the goal of providing data structures and features specific to making a versatile text editor over implementing a general-purpose programming language. For example, Emacs Lisp cannot easily read a file a line at a time—the entire file must be read into an Emacs buffer. However, Emacs Lisp provides many features for navigating and modifying buffer text at a sentence, paragraph, or higher syntactic level as defined by modes. Here follows a simple example of an Emacs extension written in Emacs Lisp. In Emacs, the editing area can be split into separate areas called windows, each displaying a different buffer. A buffer is a region of text loaded into Emacs' memory (possibly from a file) which can be saved into a text document. Users can press the default C-x 2 key binding to open a new window. This runs the Emacs Lisp function split-window-below. Normally, when the new window appears, it displays the same buffer as the previous one. Suppose we wish to make it display the next available buffer. In order to do this, the user writes the following Emacs Lisp code, in either an existing Emacs Lisp source file or an empty Emacs buffer:

The first statement, (defun ...), defines a new function, my-split-window-func, which calls split-window-below (the old window-splitting function), then tells the new window to display another (new) buffer. The second statement, (global-set-key ...) re-binds the key sequence "C-x 2" to the new function. This can also be written using the feature named advice, which allows a user to create wrappers around extant functions instead of defining their own. This has the advantages of not needing keybindings to be changed, of working wherever the original function is called, and of being simpler to write, but the disadvantage of making debugging more complex. For this reason, advice is not allowed in the source code of GNU Emacs, but if a user wishes, the advice feature can be used in their code to reimplement the above code as follows:

This instructs split-window-below to execute the user-supplied code whenever it is called, after executing the rest of the function. Advice can also be specified to execute before the original function, around it (literally wrapping the original), or to conditionally execute the original function based on the results of the advice. Emacs 24.4 replaces this defadvice mechanism with advice-add, which is claimed to be more flexible and simpler. The advice above could be reimplemented using the new system as:

These changes take effect as soon as the code is evaluated. It is not necessary to recompile, restart Emacs, or even rehash a configuration file. If the code is saved into an Emacs init file, then Emacs will load the extension the next time it starts. Otherwise, the changes must be reevaluated manually when Emacs is restarted.

… excerpt ends here. Continue reading the full article.

Illustrations

Emacs Lisp illustration

Worked examples

Example 1 — a first encounter with Emacs Lisp

Start with the simplest possible case. Write down what Emacs Lisp claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Emacs Lisp before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Emacs Lisp ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Emacs Lisp

In research
Emacs Lisp appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Emacs Lisp in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Emacs Lisp is common in secondary-school and first-year university syllabi. It links to neighbouring topics Dynamically scoped programming languages, Emacs, Free and open source compilers, so understanding it makes those chapters shorter.
In everyday life
Look for Emacs Lisp outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Emacs Lisp” →

How to study Emacs Lisp in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Emacs Lisp means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Emacs Lisp out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Emacs Lisp in simple terms?

Emacs Lisp is a Lisp dialect made for GNU Emacs. It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.

Why does Emacs Lisp matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Emacs Lisp?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Emacs Lisp.

Tags

  • Dynamically scoped programming languages
  • Emacs
  • Free and open source compilers
  • Free and open source interpreters
  • GNU Project Lisp programming language implementations
  • Lisp (programming language)
  • Lisp programming language family
  • Programming languages created in 1985
  • Scripting languages
  • Text editors

Keep exploring