User Tools

Site Tools


programming:elisp

Emacs Lisp

Style

Assignment

  • define a symbol as variable and assign an (possibly calculated) initial value: (defvar sym &optional init “doc”)
  • do the same as defvar, but define sym as a constant variable:1) (defconst sym init “doc”)
  • declare a symbol as a customizable variable, with a default value init: (defcustom sym init “doc” &rest args)
  • assign a value to a symbol, also evaluating the symbol (not only the value): (set 'sym val)
  • assign values to variables (dependencies allowed, does not evaluate var): (setq var val …)
  • assign default values to variables (dependencies allowed): (setq-default var val …)
  • call a function with args, one after another: (apply fun &rest args)
  • apply a function to each element of a sequence2) and make a list from the results: (mapcar fun seq)

(Not) Eval

  • temp. bind some variables (overwriting outer variables of same name), then evaluate body:3) let
  • do the same as letf, but dependencies between assigned (init) values in variable bindings are allowed: let*
  • evaluate body (multiple statements) as a single statement: (progn …)
  • return a (self-quoting, not evaluated) lambda expression: (lambda …)

Control

  • if..else control structure: (if condition …)
  • and without an else path: (when condition …)
  • an equivalent to (when (not condition) …) is: (unless condition …)
  • try each clause until success: (cond …)

Simple Lists

  • make a list:
    • '(a b c d)
    • (quote a b c d)
    • (list a b c d)
    • (append a b '(c d))
  • add element elt to list lst: (add-to-list 'lst elt)
  • concatenate any number of lists: (nconc &rest lists)
  • is element elt a member of list lst: (member elt lst)
  • or, to get a true boolean value: (not (null (member elt lst)))
  • get the first element from a list: (car lst)
  • get a list starting from the second element of lst: (cdr lst)
  • get the second element (see prev. two lines): (car (cdr lst)), or (cadr lst)
  • get the n-th element: (nth n lst)
  • set the first element of a list to a new value: (setcar lst val)
  • number of elements in a list: (length lst)
  • iterate over all list elements: (dolist …)
  • apply a function to all list elements: (apply …)
  • stringify and concatenate all list elements: (mapconcat …)
  • find a key in car of list members: (assoc key lst)

Association Lists (Alist)

Alists can be seen as simple lists with special elements (so called cons cells), representing an association between key and val.

  • make a cons cell for an association between key and val: (cons key val)
  • or, the same as above: '(key . val)
  • get the association for key in alst: (assq key alst)
  • get the (reverse) association for val in alst: (rassoc val alst)
  • make a copy of an alist (using cl-lib): (copy-alist alst)

Strings

  • test if object is a string: (stringp obj)
  • compare two strings: (string= str1 str2)
  • test if string matches a regular expression: (string-match-p regexp str)

Symbols

  • test if symbols value is not void (symbol exist): (boundp sym)
  • test if symbols function definition is not void (function exist): (fboundp sym)
  • test if object is a function: (functionp obj)
  • quote a function name (for byte-compile):4) (function fun), or #'fun
  • call fun (this might be a variable, else quote it) with args: (funcall fun args..)
  • get the symbol name as string (symbol-name sym)
  • make/reference a symbol from a name (string): (intern name)
  • set symbol property:5) (put sym prop val)
  • get symbol property: (get sym prop)
  • remove property from symbol (using cl-lib): (remprop sym prop)
1)
Citation from Emacs help: This declares that neither programs nor users should ever change the value. This constancy is not actually enforced by Emacs Lisp, but the symbol is marked as a special variable so that it is never lexically bound.
2)
A sequence may be a list, a vector, a bool-vector, or a string.
3)
For details see the article by Artur Malabarba: Understanding letf and how it replaces flet.
4)
Strictly avoid using quote for that. For details see the article by Artur Malabarba: Get in the habit of using sharp quote.
5)
For standard symbol properties (plist) see Emacs Manual.
programming/elisp.txt · Last modified: 2019/07/19 07:09 by Ralf Hoppe