Some historical reference is due.
Emacs Lisp borrows many things from Common Lisp, but the similarity is only surface deep. Colon symbol in Common Lisp is a namespace qualifier. Canonical symbol names have two parts: the name of the package and the name of the symbol in that package, so, for example, cl-user:apropos
is a symbol defined in package cl-user
. There is also a special keyword package automatically imported by Common Lisp when it is started. This package is used to communicate between other packages (so that if packages need to send symbolical information to each other, they wouldn't fight over who has to declare it). As you might have guessed, symbols in that package are written with the leading colon.
Emacs Lisp doesn't have a concept of packages, but the colon is still special in that it instructs the reader to interpret the expression as self-evaluating symbol (i.e. it is a variable whose value is that variable itself). There are several such built-in self-evaluating symbols: t
and nil
are examples I'll write about later.
The apostrophe is also borrowed from Common Lisp, where it is a standard reader macro (i.e. it is a code that the Lisp interpreter executes while reading in the source of the program). It expands as follows: 'x -> (quote x)
, where (quote ...)
is a special form which prevents the Lisp interpreter from evaluating the contents of the expression inside. Emacs Lisp has three built-in reader macros: '
, `
and #'
, but you cannot add your own. The first two expand to (quote ...)
, however the second allows special syntax for building the unevaluated expression. #'
expands to the (function ...)
form, which instructs the interpreter that the value must be looked up in the function namespace.
Symbols in Emacs Lisp must evaluate to some value (similar to variables in other languages) otherwise it is an error. However, you may prevent evaluation by using the quote form.
Finally, the difference between:
(search-forward "something" nil 'noerror)
and
(search-forward "something" nil :noerror)
Is that in the first case you called search-forward
with a symbol with no value, for which only the name is known, and in the second case you've called this function with a symbol whose value is this symbol itself. It so happens that this function doesn't care about which one you use.
Types are an important programming tool for automatic verification of programs. Emacs Lisp type system is similar to Common Lisp in that its universal type (written as t
) is the type from which all other types are derived. On the other hand, nil
is the type from which no type is derived (the complement of t
). This is unlike many popular programming languages, which may not have a concept of universal type, or have a distinct Boolean type.
Yet again, unfortunately, Emacs Lisp didn't copy this part precisely from Common Lisp, and the type-of
function gives confusing results for t
and nil
.
Emacs Lisp:
(type-of t) ; symbol
Common Lisp
(type-of t) ; BOOLEAN
Common Lisp extension of Emacs Lisp has a function cl-typep
which might, however, help understand the relationship. So, for example
(cl-typep 'noerror t) ; t
i.e. whatever 'noerror
is it is of type t
.