Emacs Lisp

Last updated
Emacs Lisp
EmacsIcon.svg
Emacs logo
Paradigm Functional, meta, reflective
Family Lisp
Designed by Richard Stallman,
Guy L. Steele, Jr.
Developer GNU Project
First appeared1985;39 years ago (1985)
Stable release
29.1 / 30 July 2023;6 months ago (2023-07-30)
Typing discipline Dynamic, strong
Scope Dynamic, optionally lexical
Platform Emacs
OS Cross-platform
License GPLv3
Filename extensions .el, .elc, .eln
Website www.gnu.org/software/emacs
Influenced by
Common Lisp, Maclisp

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. Emacs Lisp is also termed Elisp, although there are also older, unrelated Lisp dialects with that name. [1] [2]

Contents

Users of Emacs commonly write Emacs Lisp code to customize and extend Emacs. Itself written in Emacs Lisp, Customize 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.

Emacs Lisp can also function as a 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.

Compared to other Lisp dialects

Emacs Lisp is most closely related to Maclisp, with some later influence from Common Lisp. [3] 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 (as opposed to the minicomputers that were Emacs' traditional home), and he wanted to develop a dialect which he thought would be more easily optimized. [4]

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.

19581960196519701975198019851990199520002005201020152020
 LISP 1, 1.5, LISP 2(abandoned)
  Maclisp
  Interlisp
  MDL
  Lisp Machine Lisp
  Scheme  R5RS R6RS R7RS small
  NIL
  ZIL (Zork Implementation Language)
  Franz Lisp
  Common Lisp  ANSI standard
  Le Lisp
  MIT Scheme
  XLISP
  T
  Chez Scheme
  Emacs Lisp
  AutoLISP
  PicoLisp
  Gambit
  EuLisp
  ISLISP
  OpenLisp
  PLT Scheme   Racket
  newLISP
  GNU Guile
  Visual LISP
  Clojure
  Arc
  LFE
  Hy
  Chialisp

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:

(defunmy-split-window-func()(interactive)(split-window-below)(set-window-buffer(next-window)(other-buffer)))(global-set-key(kbd"C-x 2")#'my-split-window-func)

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 called advice , which allows the user to create wrappers around existing functions instead of defining their own. This has the advantage of not requiring keybindings to be changed and working wherever the original function is called, as well as being simpler to write but the disadvantage of making debugging more complicated. For this reason, advice is not allowed in the source code of GNU Emacs, [5] but if a user wishes, the advice feature can be used in their code to reimplement the above code as follows:

(defadvicesplit-window-below(aftermy-window-splitting-advicefirst()activate)(set-window-buffer(next-window)(other-buffer)))

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 [6] this defadvice mechanism with advice-add, which is claimed to be more flexible and simpler. [7] The advice above could be reimplemented using the new system as:

(defunswitch-to-next-window-in-split()(set-window-buffer(next-window)(other-buffer)))(advice-add'split-window-below:before#'switch-to-next-window-in-split)

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.

Source code

Emacs Lisp code is stored in filesystems as plain text files, by convention with the filename suffix ".el". The user's init file is an exception, often appearing as ".emacs" despite being evaluated as any Emacs Lisp code. Since the mid-1990s, Emacs also loads ~/.emacs.el and ~/.emacs.d/init.el. Additionally, users may specify any file to load as a config file on the command line, or explicitly state that no config file is to be loaded. When the files are loaded, an interpreter component of the Emacs program reads and parses the functions and variables, storing them in memory. They are then available to other editing functions, and to user commands. Functions and variables can be freely modified and redefined without restarting the editor or reloading the config file.

In order to save time and memory space, much of the functionality of Emacs loads only when required. Each set of optional features shipped with Emacs is implemented by a collection of Emacs code called a package or library. For example, there is a library for highlighting keywords in program source code, and a library for playing the game of Tetris. Each library is implemented using one or more Emacs Lisp source files. Libraries can define one or more major modes to activate and control their function.

Emacs developers write certain functions in C. These are primitives, also termed built-in functions or subrs. Although primitives can be called from Lisp code, they can only be modified by editing the C source files and recompiling. In GNU Emacs, primitives are not available as external libraries; they are part of the Emacs executable. In XEmacs, runtime loading of such primitives is possible, using the operating system's support for dynamic linking. Functions may be written as primitives because they need access to external data and libraries not otherwise available from Emacs Lisp, or because they are called often enough that the comparative speed of C versus Emacs Lisp makes a worthwhile difference.

However, because errors in C code can easily lead to segmentation violations or to more subtle bugs, which crash the editor, and because writing C code that interacts correctly with the Emacs Lisp garbage collector is error-prone, the number of functions implemented as primitives is kept to a necessary minimum.

Byte code

Byte-compiling can make Emacs Lisp code execute faster. Emacs contains a compiler which can translate Emacs Lisp source files into a special representation termed bytecode. Emacs Lisp bytecode files have the filename suffix ".elc". Compared to source files, bytecode files load and run faster, occupy less disk space, and use less memory when loaded.

Bytecode still runs more slowly than primitives, but functions loaded as bytecode can be easily modified and re-loaded. In addition, bytecode files are platform-independent. The standard Emacs Lisp code distributed with Emacs is loaded as bytecode, although the matching source files are usually provided for the user's reference as well. User-supplied extensions are typically not byte-compiled, as they are neither as large nor as computationally intensive.

Language features

Notably, the "cl-lib" package implements a fairly large subset of Common Lisp. This package replaces an earlier "cl" package, which would overwrite existing Emacs Lisp function definitions with ones more similar to those found in Common Lisp. The "cl-lib" package, on the other hand, follows Emacs Lisp style guidelines more closely and prefixes each function and macro it defines with "cl-" (e.g., cl-defun, which doesn't conflict with the name of the built-in defun), avoiding the unexpected changes in behavior that could occur whenever the "cl" package was loaded.

Emacs Lisp (unlike some other Lisp implementations) does not do tail-call optimization. [8] Without this, tail recursions can eventually lead to stack overflow.

The apel library aids in writing portable Emacs Lisp code, with the help of the polysylabi platform bridge.

Emacs Lisp is a Lisp-2 like Common Lisp, meaning that it has a function namespace which is separate from the namespace it uses for other variables. [9]

From dynamic to lexical scoping

Like MacLisp, Emacs Lisp uses dynamic scope, offering static (or lexical) as an option starting from version 24. [10] It can be activated by setting the file local variable lexical-binding. [11] [12] Before this option was added, one could use the lexical-let macro from the (now deprecated) "cl" package to provide effective lexical scope. [13]

In dynamic scoping, if a programmer declares a variable within the scope of a function, it is available to subroutines called from within that function. Originally, this was intended as an optimization; lexical scoping was still uncommon and of uncertain performance. In computer scientist Olin Shivers’s recollection, "I asked RMS when he was implementing emacs lisp why it was dynamically scoped and his exact reply was that lexical scope was too inefficient." [14] Dynamic scoping was also meant to provide greater flexibility for user customizations. However, dynamic scoping has several disadvantages. Firstly, it can easily lead to bugs in large programs, due to unintended interactions between variables in different functions. Secondly, accessing variables under dynamic scoping is generally slower than under lexical scoping. [15]

See also

Related Research Articles

<span class="mw-page-title-main">Common Lisp</span> Programming language standard

Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ANSI INCITS 226-1994 (S2018). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived from the ANSI Common Lisp standard.

An integrated development environment (IDE) is a software application that provides comprehensive facilities for software development. An IDE normally consists of at least a source-code editor, build automation tools, and a debugger. Some IDEs, such as IntelliJ IDEA, Eclipse and Lazarus contain the necessary compiler, interpreter or both; others, such as SharpDevelop and NetBeans, do not.

<span class="mw-page-title-main">Lisp (programming language)</span> Programming language family

Lisp is a family of programming languages with a long history and a distinctive, fully parenthesized prefix notation. Originally specified in 1960, Lisp is the third-oldest high-level programming language still in common use, after Fortran and COBOL. Lisp has changed since its early days, and many dialects have existed over its history. Today, the best-known general-purpose Lisp dialects are Common Lisp, Scheme, Racket, and Clojure.

<span class="mw-page-title-main">Scheme (programming language)</span> Dialect of Lisp

Scheme is a dialect of the Lisp family of programming languages. Scheme was created during the 1970s at the MIT Computer Science and Artificial Intelligence Laboratory and released by its developers, Guy L. Steele and Gerald Jay Sussman, via a series of memos now known as the Lambda Papers. It was the first dialect of Lisp to choose lexical scope and the first to require implementations to perform tail-call optimization, giving stronger support for functional programming and associated techniques such as recursive algorithms. It was also one of the first programming languages to support first-class continuations. It had a significant influence on the effort that led to the development of Common Lisp.

<span class="mw-page-title-main">Interpreter (computing)</span> Program that executes source code without a separate compilation step

In computer science, an interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. An interpreter generally uses one of the following strategies for program execution:

  1. Parse the source code and perform its behavior directly;
  2. Translate source code into some efficient intermediate representation or object code and immediately execute that;
  3. Explicitly execute stored precompiled bytecode made by a compiler and matched with the interpreter Virtual Machine.
<span class="mw-page-title-main">Genera (operating system)</span> Symbolics operating system based on Lisp

Genera is a commercial operating system and integrated development environment for Lisp machines created by Symbolics. It is essentially a fork of an earlier operating system originating on the Massachusetts Institute of Technology (MIT) AI Lab's Lisp machines which Symbolics had used in common with Lisp Machines, Inc. (LMI), and Texas Instruments (TI). Genera was also sold by Symbolics as Open Genera, which runs Genera on computers based on a Digital Equipment Corporation (DEC) Alpha processor using Tru64 UNIX. In 2021 a new version was released as Portable Genera which runs on Tru64 UNIX on Alpha, Linux on x86-64 and Arm64 Linux, and macOS on x86-64 and Arm64. It is released and licensed as proprietary software.

In computer programming, the scope of a name binding is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts of the program, the name may refer to a different entity, or to nothing at all. Scope helps prevent name collisions by allowing the same name to refer to different objects – as long as the names have separate scopes. The scope of a name binding is also known as the visibility of an entity, particularly in older or more technical literature—this is in relation to the referenced entity, not the referencing name.

Bytecode is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references that encode the result of compiler parsing and performing semantic analysis of things like type, scope, and nesting depths of program objects.

In aspect and functional programming, advice describes a class of functions which modify other functions when the latter are run; it is a certain function, method or procedure that is to be applied at a given join point of a program.

<span class="mw-page-title-main">GNU Guile</span> Extension Language

GNU Ubiquitous Intelligent Language for Extensions is the preferred extension language system for the GNU Project and features an implementation of the programming language Scheme. Its first version was released in 1993. In addition to large parts of Scheme standards, Guile Scheme includes modularized extensions for many different programming tasks.

newLISP

newLISP is a scripting language which is a dialect of the Lisp family of programming languages. It was designed and developed by Lutz Mueller. Because of its small resource requirements, newLISP is excellent for embedded systems applications. Most of the functions you will ever need are already built in. This includes networking functions, support for distributed and multicore processing, and Bayesian statistics. newLISP is free and open-source software released under the GNU General Public License, version 3 or later.

In computing, minimalism refers to the application of minimalist philosophies and principles in the design and use of hardware and software. Minimalism, in this sense, means designing systems that use the least hardware and software resources possible.

Hemlock is a free Emacs text editor for most POSIX-compliant Unix systems. It follows the tradition of the Lisp Machine editor ZWEI and the ITS/TOPS-20 implementation of Emacs, but differs from XEmacs or GNU Emacs, the most popular Emacs variants, in that it is written in Common Lisp rather than Emacs Lisp and C—although it borrows features from the later editors. Hemlock was originally written by the CMU Spice project in Spice Lisp for the PERQ computer.

<span class="mw-page-title-main">EuLisp</span>

EuLisp is a statically and dynamically scoped Lisp dialect developed by a loose formation of industrial and academic Lisp users and developers from around Europe. The standardizers intended to create a new Lisp "less encumbered by the past", and not so minimalist as Scheme. Another objective was to integrate the object-oriented programming paradigm well. It is a third-generation programming language.

<span class="mw-page-title-main">SLIME</span>

SLIME, the Superior Lisp Interaction Mode for Emacs, is an Emacs mode for developing Common Lisp applications. SLIME originates in an Emacs mode called SLIM written by Eric Marsden. It is developed as an open-source public domain software project by Luke Gorrie and Helmut Eller. Over 100 Lisp developers have contributed code to SLIME since the project was started in 2003. SLIME uses a backend called Swank that is loaded into Common Lisp.

PC-LISP is an implementation of the Franz Lisp dialect by Peter Ashwood-Smith.

<span class="mw-page-title-main">GNU Emacs</span> GNU version of the Emacs text editor

GNU Emacs is a free software text editor. It was created by GNU Project founder Richard Stallman, based on the Emacs editor developed for Unix operating systems. GNU Emacs has been a central component of the GNU project and a flagship project of the free software movement. Its tag line is "the extensible self-documenting text editor."

Emacs, originally named EMACS, is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, self-documenting, real-time display editor". Development of the first Emacs began in the mid-1970s, and work on GNU Emacs, directly descended from the original, is ongoing; its latest version is 29.2, released January 2024.

<span class="mw-page-title-main">Scripting language</span> Programming language for run-time events

A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled.

References

  1. "HEDRICK at RUTGERS (Mngr DEC-20's/Dir LCSR Comp Facility" (1981-12-18). ""information about Common Lisp implementation"". Letter to "rpg at SU-AI, jonl at MIT-AI". Archived from the original on 2016-09-20. Retrieved 2019-07-28. We have some experience in Lisp implementation now, since Elisp (the extended implementation of Rutgers/UCI Lisp) is essentially finished.{{cite press release}}: CS1 maint: numeric names: authors list (link)
  2. "Ad for CCA EMACS". Unix Review. December 1984. p. 16. CCA EMACS and Elisp are trademarks of CCA Uniworks, Inc.
  3. "GNU Emacs Lisp is largely inspired by Maclisp, and a little by Common Lisp. If you know Common Lisp, you will notice many similarities. However, many features of Common Lisp have been omitted or simplified in order to reduce the memory requirements of GNU Emacs. Sometimes the simplifications are so drastic that a Common Lisp user might be very confused. We will occasionally point out how GNU Emacs Lisp differs from Common Lisp." – from the "History" section of the "Introduction" to the Emacs Lisp Manual, as of Emacs 21
  4. "So the development of that operating system, the GNU operating system, is what led me to write the GNU Emacs. In doing this, I aimed to make the absolute minimal possible Lisp implementation. The size of the programs was a tremendous concern. There were people in those days, in 1985, who had one-megabyte machines without virtual memory. They wanted to be able to use GNU Emacs. This meant I had to keep the program as small as possible." – from "My Lisp Experiences and the Development of GNU Emacs"
  5. "Re: [Emacs-diffs] /srv/bzr/emacs/trunk r111086: gmm-utils.el (gmm-flet". Lists.gnu.org. 2012-12-05. Retrieved 2013-08-18.
  6. "NEWS.24.4".
  7. "Porting old advice".
  8. "Appendix C Porting Common Lisp". Gnu.org. Retrieved 2019-10-28. Lisp programmers will want to note that the current Emacs Lisp compiler does not optimize tail recursion
  9. "Google Groups". groups.google.com.
  10. "Emacs 24.1 released". Lists.gnu.org. Retrieved 2013-08-18.
  11. "Lexical binding". Lists.gnu.org. 2011-04-01. Retrieved 2013-08-18.
  12. "Dynamic Binding Vs Lexical Binding". EmacsWiki. 2013-05-17. Retrieved 2013-08-18.
  13. "Obsolete Lexical Binding". GNU Emacs Common Lisp Emulation. GNU Press. Retrieved 27 May 2021.
  14. "T". People.csail.mit.edu. Retrieved 2013-08-18.
  15. Featherston, Sam; Winkler, Susanne (2009-06-02). Process. Walter de Gruyter. ISBN   978-3-11-021614-1.