Emacs Lisp
Encyclopedia
Emacs Lisp is a dialect of the Lisp programming language
Lisp programming language
Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older...

 used by the GNU Emacs and XEmacs
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...

 text editor
Text editor
A text editor is a type of program used for editing plain text files.Text editors are often provided with operating systems or software development packages, and can be used to change configuration files and programming language source code....

s (which this article will refer to collectively as "Emacs
Emacs
Emacs is a class of text editors, usually characterized by their extensibility. GNU Emacs has over 1,000 commands. It also allows the user to combine these commands into macros to automate work.Development began in the mid-1970s and continues actively...

"). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 (as is the Lisp interpreter
Interpreter (computing)
In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language...

 itself). Users of Emacs commonly write Emacs Lisp code to customize and extend Emacs.

Emacs Lisp can also function as a scripting language, much like the Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 Bourne shell
Bourne shell
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...

, Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

, Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

, scsh
Scsh
Scsh is a POSIX API layered on top of the Scheme programming language in a manner to make the most of Scheme's capability for scripting. It is limited to 32-bit platforms but there is a development version against the latest scheme48 that works in 64bit mode.....

, or GNU Guile
GNU Guile
GNU Guile is an interpreter/virtual machine for the Scheme programming language. It was first released in 1993. Guile includes modularized extensions for POSIX system calls, APL array functionality, and others packaged as an object library...

. Just as with those languages, it may be called from the command line or via an executable file. Its editing functions, such as buffers and movement commands, complement the features of Lisp and work in batch mode.

Some people refer to Emacs Lisp as Elisp, at the risk of confusion with an older unrelated Lisp dialect with the same name. In terms of features, it is closely related to the Maclisp
Maclisp
MACLISP is a dialect of the Lisp programming language. It originated at MIT's Project MAC in the late 1960s and was based on Lisp 1.5. Richard Greenblatt was the main developer of the original codebase for the PDP-6; Jonl White was responsible for its later maintenance and development...

 dialect, with some later influence from Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

. It supports imperative
Procedural programming
Procedural programming can sometimes be used as a synonym for imperative programming , but can also refer to a programming paradigm, derived from structured programming, based upon the concept of the procedure call...

 and functional programming
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

 methods. Richard Stallman
Richard Stallman
Richard Matthew Stallman , often shortened to rms,"'Richard Stallman' is just my mundane name; you can call me 'rms'"|last= Stallman|first= Richard|date= N.D.|work=Richard Stallman's homepage...

 chose Lisp as the extension language for his rewrite of Emacs (the original used TECO
Text Editor and Corrector
TECO is a text editor originally developed at the Massachusetts Institute of Technology in the 1960s, after which it was modified by 'just about everybody'...

 as its extension language) because of its powerful features, including the ability to treat functions as data. Unlike Common Lisp, Scheme existed at the time Stallman was rewriting Gosling Emacs
Gosling Emacs
Gosling Emacs was an Emacs implementation written in 1981 by James Gosling in C. Its extension language, Mocklisp, has a syntax that appears similar to Lisp, but Mocklisp does not have lists or any other structured datatypes...

 into GNU Emacs, but he chose not to use it because of its comparatively poor performance on workstations, 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
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

 and Scheme dialects commonly used for applications programming. For example: Emacs Lisp uses dynamic rather than lexical scope
Scope (programming)
In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes. The type of scope determines what kind of entities it can contain and how it affects them—or semantics...

 by default. That is, local variables in a calling function can be referenced from a called function without passing pointers or references, regardless of whether the called function was defined within the caller.

Other methods exist for customizing GNU Emacs apart from writing Emacs Lisp. Since version 20, GNU Emacs has included a "Customize" facility which allows users to set common customization variables through a graphical interface. "Customize" works by writing Emacs Lisp code for the user, and is limited to relatively simple customizations. Not every user needs the full degree of extensibility offered by Emacs; those who do can write their own Emacs Lisp code.

Example

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 issue the "C-x 2" command to open a new window. This runs the Emacs Lisp function split-window-vertically. 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-vertically)
(set-window-buffer (next-window) (other-buffer)))

(global-set-key "\C-x2" 'my-split-window-func)


The first statement, (defun ...), defines a new function, my-split-window-func, which calls split-window-vertically (the old window-splitting function), then tells the new window to display another buffer. The second statement, (global-set-key ...) re-binds the key sequence "C-x 2" to the new function.

However, an easier way exists to write this. Emacs Lisp has a powerful feature called advice, which allows the user to create wrappers around existing functions instead of defining their own. Using advice, the above code can be reimplemented as follows:


(defadvice split-window-vertically
(after my-window-splitting-advice first activate)
(set-window-buffer (next-window) (other-buffer)))


This instructs split-window-vertically to execute the user-supplied code whenever it is called, before executing the rest of the function.

These changes take effect at code evaluation time, using (for instance) the command "M-x eval-buffer". It is not necessary to recompile or even restart Emacs. If the code is saved into the Emacs "init file" (usually a file named ".emacs" in the user's home directory
Home directory
A Home directory is a file system directory on a multi-user operating system containing files for a given user of the system. The specifics of the home directory is defined by the operating system involved; for example, Windows systems between 2000 and 2003 keep home directories in a folder...

), then Emacs will load the extension the next time it starts. Otherwise, the changes will be lost when the user exits Emacs.

Source code

In file-systems, Emacs Lisp code exists as plain text
Plain text
In computing, plain text is the contents of an ordinary sequential file readable as textual material without much processing, usually opposed to formatted text....

 files with the filename suffix ".el" (a common exception, the user's init file, often appears or appeared as ".emacs".) 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 re-loaded.

In order to save time and memory space, much of the functionality of Emacs loads only when required. Each set of optional features is implemented by a collection of Emacs code called a "library". For example, there is a library for highlighting keywords in program source code, and a library for playing the game of Tetris
Tetris
Tetris is a puzzle video game originally designed and programmed by Alexey Pajitnov in the Soviet Union. It was released on June 6, 1984, while he was working for the Dorodnicyn Computing Centre of the Academy of Science of the USSR in Moscow, Russian Soviet Federative Socialist Republic...

. Each library is implemented using one or more Emacs Lisp source files.

Emacs developers write certain functions in C. These are "primitives", also known as "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
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...

, 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, crashing the editor, and writing C code that interacts correctly with the Emacs Lisp garbage collector
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...

 is error-prone, relatively few functions are implemented as primitives.

Byte code

"Byte-compilation" can further increase the performance of Emacs Lisp code. Emacs contains a compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

 which can translate Emacs Lisp source files into a special representation known as bytecode
Bytecode
Bytecode, also known as p-code , is a term which has been used to denote various forms of instruction sets designed for efficient execution by a software interpreter as well as being suitable for further compilation into machine code...

. Emacs Lisp bytecode files have the filename suffix ".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" package implements a fairly large subset of Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

.

Emacs Lisp uses dynamic, not static (or lexical), scope
Scope (programming)
In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes. The type of scope determines what kind of entities it can contain and how it affects them—or semantics...

. 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
Optimization (computer science)
In computer science, program optimization or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources...

; lexical scoping was still uncommon and of uncertain performance. 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. As a result, plans have been made to convert Emacs Lisp to lexical scoping, though this has not yet been done. The lexical-let macro in the "cl" package does provide effective lexical scope to Emacs Lisp programmers, but while `cl' is widely used, lexical-let is rarely used. Variables bound with lexical-let are never released, even if they are never used. However support for lexical bindings is provided in Emacs trunk (24) and can be activated by setting the file local variable lexical-binding.

Emacs Lisp (unlike some other Lisp implementations) does not do tail-call optimization. Without this, tail recursions can eventually lead to stack overflow
Stack overflow
In software, a stack overflow occurs when too much memory is used on the call stack. The call stack contains a limited amount of memory, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture,...

.

The apel
Apel (emacs)
In computer programming, apel provides support for writing portable code in Emacs Lisp.XEmacs features a version of apel called apel-xemacs.- External links :* *...

 library aids in writing portable Emacs Lisp code.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK