Introduction
An .el file holds Emacs Lisp, the dialect of Lisp built into the
GNU Emacs text editor. Most of Emacs
is written in it, and so are its extensions and packages. A user’s own settings, in
init.el, are Emacs Lisp too.
Emacs can byte-compile an .el file to a .elc file, and
versions built with native compilation produce .eln files as well. The
.el file stays the source that people read and edit.
This section describes the language as GNU Emacs reads it, following the GNU Emacs Lisp Reference Manual for Emacs 27 to 31, and describes how the viewer on this site colours it. The pages are written for someone who wants to read a file or write a reader for it, so they give the syntax first and the library by area.
Structure
An Emacs Lisp file is plain text, usually UTF-8, made of parenthesised forms that are
evaluated in turn. Comments start with ; and run to the end of the line.
By convention, the number of semicolons shows the level of a comment. Emacs also reads
the first line for file-local settings between -*- marks. The setting
lexical-binding: t turns on lexical scope, which new code is expected to
use.
A library follows a set layout: a first line naming the file and describing it, a
header of comment lines such as Author:, Version: and
Package-Requires:, a ;;; Commentary: part and a
;;; Code: part. It ends with (provide 'name), which lets
(require 'name) load it, and a closing comment. Package tools read these
headers. Functions are defined with defun and user settings with
defcustom, and a function that can be run with a keystroke or
M-x contains (interactive).
;;; greet.el --- Say hello -*- lexical-binding: t; -*-
;; Author: A. Example
;; Version: 1.0
;;; Commentary:
;; A small example library.
;;; Code:
(defun greet (name)
"Show a greeting for NAME."
(interactive "sName: ")
(message "Hello, %s" name))
(provide 'greet)
;;; greet.el ends here
The files
| File | What it is | Viewer |
|---|---|---|
*.el | Emacs Lisp source: a library, a package, or a file of settings (init.el, early-init.el, custom.el, *-pkg.el, *-autoloads.el, loaddefs.el) | Coloured text, “Emacs Lisp source file” |
.dir-locals.el, .dir-locals-2.el | Settings for every file in a directory tree: an alist of modes and variable bindings (File structure) | Coloured text, “Emacs directory-local variables file” |
.emacs, _emacs, .gnus, .spacemacs, .abbrev_defs | Files that Emacs or a distribution reads by name; all hold Emacs Lisp forms | Coloured text, “Emacs configuration file” |
Cask | The package dependency file of the Cask tool: Emacs Lisp forms such as (depends-on "f") (Packages and autoloads) | Coloured text, “Cask file” |
*.elc | Byte-compiled Emacs Lisp: a ;ELC header, then forms holding byte-code objects (Byte-compilation) | Bytes only |
*.eln | Native-compiled Emacs Lisp: a shared library for the machine it was built on (Native compilation) | Bytes only |
Reference pages
- File structure – the first line, library headers and sections, footers, autoload cookies, file-local variables,
.dir-locals.el, encodings and line endings - Lexical structure – every token: comments, symbols, numbers, strings, character syntax, quote and backquote,
#forms - Data types – integers, floats, symbols, conses, strings, vectors, hash tables, records, functions, buffers, markers and the rest
- Evaluation – forms, the evaluator, special forms, quoting,
eval,funcall - Control structures –
if,cond, loops, non-local exits, errors andcondition-case - Functions – lambda expressions, argument lists, closures, advice,
declare - Macros –
defmacro, backquote, expansion, hygiene,pcase - Variables and scope – dynamic and lexical binding, buffer-local variables,
defvar,defcustom - Lists and hash tables – conses, alists, plists, sequences,
seq, hash tables - Strings and characters – strings, characters,
format, text properties - Buffers and text – buffers, point, markers, insertion, narrowing, overlays
- Regexps and syntax tables – the regexp language, searching,
rx, syntax classes - Commands and modes –
interactive, keymaps, major and minor modes, hooks - Minibuffer – reading input, completion
- Files – visiting, saving, file names, directories, file attributes
- Processes – subprocesses, network connections, threads
- Packages and autoloads –
provide,require,load,package.el, autoloads - Byte-compilation – the
.elcfile, byte-code objects, dynamic doc strings - Native compilation – the
.elnfile,libgccjit, the cache - Emacs versions – what Emacs 22 to 31 added to the language
How the viewer colours Emacs Lisp
The viewer has its own scanner for Emacs Lisp. It follows the Emacs reader (the
terminating characters of a symbol, \ as the only escape inside a symbol,
the # forms, the character syntax) and keeps a small context for each open
list, so it knows whether a name is a call, a definition, a parameter, a binding, quoted
data, a pcase pattern or a customization type. It does not run the code,
so a macro that a program defines for itself is coloured as a function call unless its
name looks like a definer (def…, define-…, with-…).
| Colour class | What it marks |
|---|---|
| Comment | ; comments, documentation strings (of defun, defvar, defcustom, defface, define-minor-mode and the other definers), #! lines, and the bytes skipped by #@N |
| String | strings with their text; character escapes inside are Directive |
| Number | integers, floats (1e3, 1.0e+INF, -0.0e+NaN), radix integers (#xFF #b101 #o17 #24r1k) and character syntax (?a ?\C-x ?\M-x ?\x41 ?\N{…}), because a character is an integer |
| Keyword | special forms (if, let, setq, condition-case …), macros (when, dolist, pcase, cl-loop, use-package …) and names that look like definers (defun, define-minor-mode, with-temp-buffer) when they head a list |
| Function | any other name at the head of a list, the name that defun, defmacro, define-minor-mode and the like define, #'name, and well-known function names written after a quote ('car, 'emacs-lisp-mode) |
| Variable | the name that defvar, defconst, defcustom and defgroup define, the variables of Emacs that code reads and sets (load-path, case-fold-search, major-mode …), and names that end in -hook, -functions, -mode-map, -syntax-table or are written *like-this* |
| Type | customization type names after :type (choice, string, repeat …), faces (bold, font-lock-comment-face, the name given to defface), error symbols (error, wrong-type-argument, file-missing), the name given to cl-defstruct, and the type name of a #s(…) record |
| Literal | nil, t, keyword symbols (:key), ## and uninterned symbols (#:g1) |
| Regex | strings that are passed as a regular expression: the pattern argument of string-match, re-search-forward, looking-at, replace-regexp-in-string, split-string and the like, and the value of a defconst whose name ends in regexp |
| Directive | quote ', backquote `, comma , and ,@, #', #s, #[, #(, #&N, #N= and #N#; lambda-list words (&optional, &rest); the heads inside declare; pcase pattern words (pred, app, guard); customization, face, minor-mode, advice and use-package keywords (:group, :type, :foreground, :lighter, :around, :ensure); the cookie -*- lexical-binding: t -*-; library header names (Author:); ;;; Commentary: and ;;; Code:; ;;;###autoload; the Local Variables: block; format directives (%s %-5d); escapes in strings (\n \x41 é \N{…} \C-a); and the \\[command] and \\{map} markup in documentation |
How the viewer is chosen: every .el file is Emacs Lisp. A .lisp or
.lsp file whose text has Emacs Lisp marks ((provide 'name),
defcustom, ;;;###autoload, (interactive …) and others
that Common Lisp lacks) is named “Emacs Lisp source file” too; the rules are on the
Other Lisps page. .dir-locals.el,
.emacs and Cask are chosen by their whole name. .elc and
.eln are named and shown as bytes.
Not described
Known, not yet covered
- Reference: the individual functions. The manual documents about 1,400 functions, macros and special forms; the pages name the ones each area needs and group the rest, but do not give each one’s argument list and return value. The manual’s function index is the place for that.
- Reference: the chapters on display (faces in full, fonts, images, fringes, the mode line format, display specifications), frames and windows, abbrevs, the Lisp debugger and Edebug, tree-sitter, the C source and dynamic modules, and tips for writing code are only named, in Commands and modes and Buffers and text.
- Reference: the byte-code instruction set (the numbers and meaning of the opcodes) and the layout of the
.elnshared library are not listed (Byte-compilation, Native compilation); the byte-code disassembler in Emacs lists the instructions of any function. - Reference: the Emacs 22 to 26 changes are given from memory of the release notes and are less complete than the Emacs 27 to 31 ones, which were read from
NEWS(Emacs versions). - Viewer: the colouring follows the text and not the program. A macro that the file defines for itself, a data list written without a quote inside a macro that does not evaluate it, and a name used both as a function and a variable are coloured by their place and shape. Where a macro takes its arguments unusually (
cl-loopclauses,use-packagebodies,rxforms) only the head is coloured as a macro. - Viewer: the lists of built-in variables, functions, faces and error symbols are partial: the common names are known, the 480 variables and 1,400 functions of the manual are not all in them. A name that is not known is shown without colour (a variable) or as a function (at the head of a list).
- Viewer:
.elcand.elnfiles are named and shown as bytes; the byte-code is not disassembled.
Unknown
- Viewer: the colouring was tested on hand-written cases and on the few
.elfiles that exist on this Mac (a gyp mode, several.dir-locals.elfiles, mode files from LLVM, OCaml, GDB and protobuf). No large Emacs Lisp code base was available, so unusual styles may colour wrongly.
Also used for
- Byte-compiled Emacs Lisp – Emacs Lisp compiled to byte-code, with a ;ELC header.
- Natively compiled Emacs Lisp – Emacs Lisp compiled to machine code, a shared library for one machine.
Links
Software
Developers
- tree-sitter-elisp – Emacs Lisp grammar for the Tree-sitter parser used by editors
- Emacs source mirror – the reader (
src/lread.c), the byte compiler (lisp/emacs-lisp/bytecomp.el) and the native compiler (lisp/emacs-lisp/comp.el) (C, Emacs Lisp)
Reference
- GNU Emacs Lisp Reference Manual – the official description of the language
- Conventional Headers for Emacs Libraries – the header lines a library should have
- Byte Compilation – the manual’s chapter on
.elcfiles - Native Compilation – the manual’s chapter on
.elnfiles