GNU Emacs Lisp Reference Manual Learning Notes
Notes recorded during learning Emacs Lisp Reference Manual, I did not finish the whole book, only 12 chapters.
Introduction
nil
and t
nil
- symbol with name "nil"
- logical truth value
false
- empty list
t
- truth value t
- symbol "t"
booleanp
obj return non-=nil= if obj is t or nil, otherwise not
version info
emacs-version
&optional here functionemacs-build-time
variableemacs-version
variableemacs-major-version
variableemacs-minor-version
variable
Data Types
one object can have only one primitive type, but may can belong to several other types primitive types are: integer, float, cons, symbol, string, vector, hash-table, subr, byte-code function
character type
the read syntax of basic char is ?<X>, <X> stands for the character to be represented, so ?A stands for 'A'
equality predicates
-
eq
obj1 obj2 - return
t
if obj1 and obj2 are the same object, otherwise returnnil
integers with same value are considered to be the same object, so(eq 1 1)
will returnt
exception:
(eq "abc" "abc")
will returnnil
, but(eq "" "")
will returnt
because the empty string is only stored one copy as expected,(eq '(1 2 3) '(1 2 3))
will returnnil
-
equal
obj1 obj2 - return
t
if obj1 and obj2 have equal components, otherwise returnnil
unlike
eq
,equal
will look into arguments to check if there contents are the same, if obj1 and obj2 areeq
, they must beequal
. so, as expected,(equal '(1 2 3) '(1 2 3))
and(equal "abc" "abc")
will both returnt
Numbers
two types
integer
float
type predicates functions
floatp
objectintegerp
objectnumberp
objectnatnump
object : returnst
if object is a natural number, such as 0, 1, 2…zerop
object
comparison functions
max
num1 &rest nums return the maximum valuemin
num1 &rest nums return the minimun value
conversion functions
float
number convert the number to float typetruncate
number &optional divisor convert the number to integer by rounding towards zerofloor
number &optional divisor convert the number to integer by rounding towards negative infinityceiling
number &optional divisor convert the number to integer by rounding towards positive infinityround
number &optional divisor convert the number to integer by rounding towards nearest integer
random function
random
&optional limit returns a pseudo-random integerif limit is a positive integer, the value is chosen in [0, limit), and of course only choose integer
if limit is
t
, it will choose a new seed, otherwise Emacs will always use the same seed, so the returned pseudo-random integer sequences are always the same
Strings and Characters
predicates functions
stringp
objectstring-or-null-p
objectchar-or-string-p
object
useful functions
substring
: to get substringconcat
: to concatenate strings togethersplit-string
: split string into several stringsstring=
/string-equal
: judge the equality of two stringsstring-prefix-p
: check if a string is a prefix of anotherdowncase
/upcase
: change a string or a character to opposite case
Lists
cons cells
a cons cell has two slots, the first is called CAR and the second is called CDR, and it may looks like the structure below:
struct cons_cell { void * CAR; void * CDR; };
so the two slots can hold any values, and lists are built up from cons cell, which stores object pointer in CAR slot, and stores the nexe node pointer in CDR slot
element accessing functions
car
/cdr
pop
nth
/nthcdr
last
building functions
cons
list
make-list
append
reverse
number-sequence
modification functions
push
add-to-list
setcar
/setcdr
nconc
&rest lists : return a list containing all the elements of lists, last CDR of each given list is set to point to next listmemq
object list : test if object is in list, if is in, return the sublist from the position object occurs the first timedelq
object list : delete all occurrences of object in list, note that it useseq
to check if two objects are equal, same asmemq
remq
object list : returns a list copy with all elementseq
to object removedmember
object list : likememq
, only difference is it usesequal
to check the equality of two objectsdelete
object sequence : likedelq
, but usesequal
for comparisonremove
object sequence : likedelete
, but it always returns a copy of processed sequence, no matter sequence is a list, vector, or a string
association lists
alist for short, it is a list consists of cons cells, that is to say, every element of alist is a cons cell, the CAR of the cons cell stores key while the CDR stores value
assoc
key alist : returns the first occurrence of key in alist, usesequal
for comparison, note that it returns the whole cons cell, not only the CDRrassoc
value alist : likeassoc
, but uses CDR for comparison,assoc
uses CARassq
key alist : likeassoc
, but useseq
for comparisonrassq
value alist : …assq-delete-all
/rassq-delete-all
: …
Sequences, Arrays and Vectors
sequence: list, array array: vector, string, char-table, bool-vector
sequence functions
sequencep
length
elt
sequence index : returns the element of sequence at index
array functions
arrayp
aref
array index : returns element of array at indexaset
array index object : set object to the index element of arrayfillarray
array object : fills array with object
vector functions
vectorp
vector
&rest objects : creates a vector with objectsmake-vector
length object : makes a vector has length and filled by objectvconcat
&test sequences : returns a new vector containing all elements in sequences
char-table, bool-vector (skipped)
not so useful, so skip them
Hash Tables (skipped)
not so useful, so skip it
Symbols
symbol components
each symbol has four components:
- print name: the name of the symbol
- value: symbol's current value as a variable
- function: symbol's function definition, can also hold a symbol, a keymap, or a keyboard macro
- property list: symbol's property list
create and intern symbols
symbols are stored in a vector called obarray, symbol name and symbol is with a one-to-one mapping relationship interning a symbol means hash it and put it into obarray, or find it from obarray, so there will never be two symbols with same name in one obarray, or two names pointing to one symbol.
*intern*
: find a symbol from obarray, or create a new symbol and put it into obarray. so the finding action is also a interning operation
of course there are uninterned symbols, it means they are not put into any obarray, they may exist in some other object or as variable values
functions
symbol-name
make-symbol
intern
name &optional obarray : returns the interned symbol with name name, creates a new one and puts it into obarray if no symbol foundintern-soft
name &optional obarray : returns the already interned symbol with name name, returnsnil
if not foundmapatoms
function &optional obarray : calls function once with each symbol in obarray, then returnsnil
property list
property list (so called plist) is like association list (alist), it is also key-value pair data structure, but it often uses symbol as the key, so a plist may always have unique keys, while alist may not.
plist functions
symbol-plist
symbolsetplist
symbol plistget
symbol propertyput
symbol property valueplist-get
plist propertyplist-put
plist property valueplist-member
plist property
Evaluation
a Lisp object that is intended for evaluation is called a form or expression, or S-expression any Lisp object can be evaluated, but in practice only numbers, string, symbols, lists are evaluated very often
kinds of forms
how Emacs evaluates a form depends on its data type: symbols, lists, and "all other data types"
- self-evaluating forms: "all other data types"
the result of self-evaluating is the object itself, so 25 will be evaluated to 25, "abc" will be evaluated to "abc"
- symbol forms: will be evaluated as a variable, so its value field will be returned
- list forms: the evaluating way of a list is determined by its first element, if first element is:
- a symbol: the symbol's function field will be obtained and replace the symbol with that function
- a function: evaluates the remaining elements of the list first, and then call the function with these evaluated remaining elements as parameters
- a macro: evaluates the macro with the remaining elements as parameters without evaluating the remaining elements
- special forms: such as if, and, evaluation of elements in the list depends on the special forms' syntax requirements
Control Structures (skipped)
things about special forms for flow control, such as if, progn, cond, while, so skip it
Variables
local variables
let
bindings… forms…let*
bindings… forms… : likelet
, difference is a local variable binding will take effect immediately before next local variable binding takes placemakeunbound
symbol : cancel the symbol's binding to a valueboundp
variable : to judge if the variable is bound, returnst
if its value is not void
global variables
global variables can be defined by defvar
and defconst
, they do not have actually difference, defconst
is intent to inform human readers that the variable may should not be changed, but actually its value can be changed
Functions
function types:
- lambda expression
- primitive : written in C
- special form : like
if
,and
,while
- macro
- command : (includes keyboard macros)
- closure
- byte-code function
- autoload object
mapping functions
mapcar
function sequence : applies function to each element in sequence in turn, return a list of resultsmapc
function sequence : likemapcar
, but it is used for side effects, return value will be ignoredmapconcat
function sequence separator : likemapcar
, but function's result must be string, and the strings will be concatenated as return value
Generated by Emacs 24.5.1 (Org mode 8.x)
Copyright © 2014 - Kelvin Hu - Powered by org-page
Themed with emacs_love