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 (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 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 functionsplit-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 ...)
, 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, but if a user wishes, the advice feature can be used in their code to reimplement the above code as follows:
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:
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 Byte code
''Byte-compiling'' can make Emacs Lisp code execute faster. Emacs contains a.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. 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.
From dynamic to lexical scoping
Like MacLisp, Emacs Lisp uses dynamic scope, offering static (or lexical) as an option starting from version 24. It can be activated by setting the file local variablelexical-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; 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." 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 scriptReferences
External links
* , GNU Project {{Portal bar, Free and open-source software Articles with example Lisp (programming language) code Dynamically scoped programming languages Lisp Free and open source compilers Free and open source interpreters Lisp programming language family Scripting languages Text editors Programming languages created in 1985 GNU Project Lisp programming language implementations