Emacs Lisp is a
dialect
The term dialect (from Latin , , from the Ancient Greek word , 'discourse', from , 'through' and , 'I speak') can refer to either of two distinctly different types of linguistic phenomena:
One usage refers to a variety of a language that is a ...
of the
Lisp programming language
Lisp (historically 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 second-oldest high-level programming language still in common u ...
used as a
scripting language
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.
A scripting ...
by
Emacs (a
text editor
A text editor is a type of computer program that edits plain text. Such programs are sometimes known as "notepad" software (e.g. Windows Notepad). Text editors are provided with operating systems and software development packages, and can be ...
family most commonly associated with
GNU Emacs
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 ...
and
XEmacs
XEmacs is a graphical- and console-based text editor which runs on almost any Unix-like operating system as well as Microsoft Windows. XEmacs is a fork, based on a version of GNU Emacs from the late 1980s. Any user can download, use, and modify X ...
). 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 is also an older, unrelated Lisp dialect with that name.
Users of Emacs commonly write Emacs Lisp code to customize and extend Emacs. Other options include the ''Customize'' feature that's been in GNU Emacs since version 20. 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
In computing, configuration files (commonly known simply as config files) are files used to configure the parameters and initial settings for some computer programs. They are used for user applications, server processes and operating system se ...
, 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
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.
A scripting ...
, much like the
Unix
Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, an ...
Bourne shell or
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
, 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
In the industrial design field of human–computer interaction, a user interface (UI) is the space where interactions between humans and machines occur. The goal of this interaction is to allow effective operation and control of the machine f ...
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. It supports
imperative and
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
methods.
Richard Stallman
Richard Matthew Stallman (; born March 16, 1953), also known by his initials, rms, is an American free software movement activist and programmer. He campaigns for software to be distributed in such a manner that its users have the freedom to ...
chose Lisp as the extension language for his rewrite of Emacs (the original used
Text Editor and Corrector
TECO (), short for ''Text Editor & Corrector'',"A powerful and sophisticated text editor, TECO (Text Editor and Corrector) ... is both a character-oriented text editor and a programming language, that was developed in 1962 for use on Digital Equip ...
(TECO) as its 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 Stallman was rewriting
Gosling Emacs
Gosling Emacs (often shortened to "Gosmacs" or "gmacs") is a discontinued Emacs implementation written in 1981 by James Gosling in C.
Gosling initially allowed Gosling Emacs to be redistributed with no formal restrictions, as required by the "E ...
into GNU Emacs. He 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.
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
Scope or scopes may refer to:
People with the surname
* Jamie Scope (born 1986), English footballer
* John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution
Arts, media, and entertainment
* Cinem ...
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
To understand the logic behind Emacs Lisp, it is important to remember that there is an emphasis on 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
computing, a keyboard shortcut also known as hotkey is a series of one or several keys to quickly invoke a software program or perform a preprogrammed action. This action may be part of the standard functionality of the operating system or ...
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:
(defun my-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
Advice (noun) or advise (verb) may refer to:
* Advice (opinion), an opinion or recommendation offered as a guide to action, conduct
* Advice (constitutional law) a frequently binding instruction issued to a constitutional office-holder
* Advice (p ...
'', 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,
but if a user wishes, the advice feature can be used in their code to reimplement the above code as follows:
(defadvice split-window-below
(after my-window-splitting-advice first () 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 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:
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 filesystem
In computing, file system or filesystem (often abbreviated to fs) is a method and data structure that the operating system uses to control how data is Computer data storage, stored and retrieved. Without a file system, data placed in a storage me ...
s as plain text
In computing, plain text is a loose term for data (e.g. file contents) that represent only characters of readable material but not its graphical representation nor other objects (floating-point numbers, images, etc.). It may also include a limit ...
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
A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
. For example, there is a library for highlighting keywords in program source code, and a library for playing the game of Tetris
''Tetris'' (russian: link=no, Тетрис) is a puzzle video game created by Soviet software engineer Alexey Pajitnov in 1984. It has been published by several companies for multiple platforms, most prominently during a dispute over the appro ...
. 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
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 ...
, primitives are not available as external libraries; they are part of the Emacs executable. In XEmacs
XEmacs is a graphical- and console-based text editor which runs on almost any Unix-like operating system as well as Microsoft Windows. XEmacs is a fork, based on a version of GNU Emacs from the late 1980s. Any user can download, use, and modify X ...
, runtime loading of such primitives is possible, using the operating system's support for dynamic linking
In computing, a dynamic linker is the part of an operating system that loads and links the shared libraries needed by an executable when it is executed (at " run time"), by copying the content of libraries from persistent storage to RAM, filling ...
. 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 violation
In computing, a segmentation fault (often shortened to segfault) or access violation is a fault, or failure condition, raised by hardware with memory protection, notifying an operating system (OS) the software has attempted to access a restricte ...
s or to more subtle bugs, which crash the editor, and because writing C code that interacts correctly with the Emacs Lisp garbage collector
A waste collector, also known as a garbageman, garbage collector, trashman (in the US), binman or (rarely) dustman (in the UK), is a person employed by a public or private enterprise to collect and dispose of municipal solid waste (refuse) and r ...
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
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
which can translate Emacs Lisp source files into a special representation termed bytecode
Bytecode (also called portable code or p-code) 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 (norma ...
. Emacs Lisp bytecode files have the filename suffix
A filename extension, file name extension or file extension is a suffix to the name of a computer file (e.g., .txt, .docx, .md). The extension indicates a characteristic of the file contents or its intended use. A filename extension is typically d ...
".elc
". Compared to source files, bytecode files load faster, occupy less space on the disk, use less memory when loaded, and run faster.
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
In computer science, a tail call is a subroutine call performed as the final action of a procedure. If the target of a tail is the same subroutine, the subroutine is said to be tail recursive, which is a special case of direct recursion. Tail recu ...
. Without this, tail recursion
In computer science, a tail call is a subroutine call performed as the final action of a procedure. If the target of a tail is the same subroutine, the subroutine is said to be tail recursive, which is a special case of direct recursion. Tail recur ...
s can eventually lead to stack overflow
In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many facto ...
.
The apel library aids in writing portable Emacs Lisp code, with the help of the polysylabi platform bridge.
Emacs Lisp is a Lisp-2 meaning that it has a function namespace which is separate from the namespace it uses for other variables.
From dynamic to lexical scoping
Like MacLisp, Emacs Lisp uses dynamic scope
Scope or scopes may refer to:
People with the surname
* Jamie Scope (born 1986), English footballer
* John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution
Arts, media, and entertainment
* Cinem ...
, offering static (or lexical) as an option starting from version 24. It can be activated by setting the file local variable lexical-binding
. Before this option was added, one could use the lexical-let
macro from the (now deprecated) "cl" package to provide effective lexical scope.
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
Mathematical optimization (alternatively spelled ''optimisation'') or mathematical programming is the selection of a best element, with regard to some criterion, from some set of available alternatives. It is generally divided into two subfi ...
; lexical scoping was still uncommon and of uncertain performance. "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." 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.
See also
* Vim script
Vim (;
"Vim is pronounced as one word, like Jim, not vi-ai-em. It's written with a capital, since it's a name, again like Jim." ...
References
External links
* , GNU Project
{{Portal bar, Free and open-source software
Articles with example Lisp (programming language) code
Lisp
Free compilers and interpreters
Lisp programming language family
Scripting languages
Text editors
Programming languages created in 1985