Emacs Lisp: What's Function

By Xah Lee. Date: . Last updated: .

Emacs Lisp manual uses the following terms to distinguish different behavior of functions. For advanced lisp programing, you need to understand them.

  1. lambda expression → Anonymous function. Usually temp functions. Usually created by lambda.
  2. Primitive → (aka subr, builtin function.) elisp function implemented in C. For example: {list, car, append, point, , …}. Typically the most fundamental ones.
  3. special form → a function that doesn't eval in the normal way and implemented in C. For example: {if, cond, and, or, while, progn}. (normally, arguments to a function are all evaluated first.)
  4. macro → Similar to “special form”, but defined by lisp expression, by defmacro. i.e. the arguments are not eval'd, and you can manipulate them and eval them.
  5. command → functions that can be called by command-execute. Typically, defun with interactive clause. All functions you can call by “M-x” are commands.
  6. Function → The catch all. Lambda expression, primitive, special form, macro, command, are all functions. And a function not any of the above is also a function. Usually created by defun.

Note: these are not classification of functions. Most of these are not mutually exclusive. For example, forward-word is a command, but is also a primitive (that is, implemented in C.), any special form by definition is also a primitive.

These distinction of functions are useful because they help you understand if arguments are evaluated in standard way or not (For example, special form and macro do not.), and whether a function is user-oriented (For example, command), and whether a function is a primitive, it usually means it's fast and fundamental to elisp.

Note: there are more, but the above are the basics. For detail, see (info "(elisp) What Is a Function")

Classification of Symbols in Emacs Lisp Manual

In emacs lisp manual, each symbol has one of these indicator prefix:

KindPredicateExample
Special Formspecial-form-pprogn
Macromacropwhen
Commandcommandpcount-words
Prefix Commandnonehelp-command
Functionfunctionpfile-name-directory
Constantnonedir-locals-file
User Optionnoneafter-save-hook
Variableboundpbuffer-file-name

Note: there are only 1 Prefix Command in elisp manual: help-command

Note: there are only 2 constants in elisp manual: • dir-locals-filedisplay-buffer-fallback-action

See also: Emacs Lisp: What's Symbol

How to Check a Symbol's “type”

Here's how to check if a symbol is which:

;; check if a symbol is special form
(special-form-p 'progn)       ; t

;; check if a symbol is macro
(macrop 'when )               ; t

;; check if a symbol is command
(commandp 'count-words )      ; t

;; check if a symbol is function (this includes special form and macro). In other words, the symbol is callable
(functionp 'buffer-file-name) ; t

;; check if a symbol is variable
(boundp 'buffer-file-name)    ; t

The following doesn't have predicate to check:

  1. Prefix Command
  2. Constant
  3. User Option

Moust useful are fboundp and boundp.

fboundp → return true if a function is defined first. (that is, symbol's function cell is not empty. 〔►see Emacs Lisp: What's Symbol〕 )

boundp → return true if a variable is defined. (That is, symbol's value cell is not empty) 〔►see Emacs Lisp: Check If a {function, variable, feature} is Defined/Loaded

How to Check for Primitive

Use subrp on a symbol's function cell to check if a function is a primitive.

;; check if a function is a elisp primitive; i.e. a elisp function written in C
;; arg to subrp must be a lisp object, not symbol

;; these are core lisp functions, implemented in C
(subrp (symbol-function 'list))         ;t
(subrp (symbol-function '+))            ;t

;; these are special forms, implemented in C
(subrp (symbol-function 'while))        ;t
(subrp (symbol-function 'save-excursion)) ;t

;; these are commands, implemented in C
(subrp (symbol-function 'goto-char))    ;t
(subrp (symbol-function 'beginning-of-line)) ;t
(subrp (symbol-function 'forward-word)) ;t

;; lambda is a macro.
(subrp (symbol-function 'lambda))       ;nil

;; Return a symbol's function cell value
(symbol-function 'setq)                 ;#<subr setq>

Command vs Non-Command

For practical emacs lisp programing, the most important concept here is “command”. Commands are basically (defun …) with (interactive …) clause. Function defined without the “interactive” can only be called by other elisp functions. The “interactive” function also provide many ways to automatically feed arguments to your function when emacs user calls your function. 〔►see Emacs Lisp: Get User Input〕 ((info "(elisp) Defining Commands"))

When looking for a function, call apropos-command, with universal-argumentCtrl+u】 first. It'll then also list functions. 〔►see Emacs Lisp: Search Documentation

To search all symbols, call apropos.

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs