All notes
Lisp

# Functions

## Find help in Emacs

Type M-x info, search for the elisp reference manual, then search for topics (such as backquote) within that.

## String related


(stringp OBJECT) ;; Return t if OBJECT is a string.


## defun


; As an example, here is the code for a function that multiplies its argument by 7. This example is not interactive.

(defun multiply-by-seven (number)
"Multiply NUMBER by seven."
(* 7 number))

(defun multiply-by-seven (number)       ; Interactive version.
"Multiply NUMBER by seven."
(interactive "p") ;  The "p" tells Emacs to pass the prefix argument to the function and use its value for the argument of the function.
(message "The result is %d" (* 7 number)))

(defun insertTab (&optional unindented-ok)
"Insert Tab."
(interactive "p")
(insert-char #x09)
)
; See nthelp for whole list for ASCII.


#### Interactive function

• You make a function interactive by placing a list that begins with the special form interactive immediately after the documentation.
• A user can invoke an interactive function by typing M-x and then the name of the function; or by typing the keys to which it is bound, for example, by typing C-n for next-line or C-x h for mark-whole-buffer.
• Interestingly, when you call an interactive function interactively, the value returned is not automatically displayed in the echo area. This is because you often call an interactive function for its side effects, such as moving forward by a word or line, and not for the value returned. If the returned value were displayed in the echo area each time you typed a key, it would be very distracting.

## Variables

### let

GNU: let. let creates a name for a local variable that overshadows any use of the same name outside the let expression. This is like understanding that whenever your host refers to ‘the house’, he means his house, not yours. (Symbols used in argument lists work the same way.)


(let varlist body)

(let ((variable value)
(variable value)
)
body)

(let ((zebra 'stripes)
(tiger 'fierce))
(message "One kind of animal has %s and another is %s."
zebra tiger))
; "One kind of animal has stripes and another is fierce."


### quote

#### Explanation

• ' and (quote varname) both perform the same purpose in emacs-lisp: pass the unevaluated form to the surrounding environment rather than evaluate it.
• Lisp evaluates forms as they are reached, by quoting the form you prevent evaluation so that the actual variable (or list, or function name) is passed.
• If you are trying to use the variable itself, then use 'some-variable. If you are trying to use the value stored in the variable, use some-variable.

### boundp, bound-and-true-p, fboundp


(boundp SYMBOL)
; Return t if SYMBOL's value is not void.

(bound-and-true-p VAR)
; Return the value of symbol VAR if it is bound, else nil.

;; Here is the definition for bound-and-true-p:
(defmacro bound-and-true-p (var)
"Return the value of symbol VAR if it is bound, else nil."
(and (boundp (quote ,var)) ,var))

(fboundp SYMBOL)
; Return t if SYMBOL’s function definition is not void.


(setq some-variable "less")

(when (boundp 'some-variable)
(message "some-variable is %s" some-variable))
; "some-variable is less"

; Also work.
(when (bound-and-true-p some-variable)
(message "some-variable is %s" some-variable))
;; Note that in this example, using single-quote causes error - Wrong type argument: symbolp, (quote some-variable)

;;;;;;;;;; Another example

(boundp 'abracadabra)          ; Starts out void.
; nil

(let ((abracadabra 5))         ; Locally bind it.
; t
(boundp 'abracadabra)          ; Still globally void.
; nil

(setq abracadabra 5)           ; Make it globally nonvoid.
; 5
; t


### Backquote,


;; Generally, quote and backquote are similar.
(a list of (+ 2 3) elements)
;     ⇒ (a list of (+ 2 3) elements)
'(a list of (+ 2 3) elements)
;     ⇒ (a list of (+ 2 3) elements)

;; With backquote, comma denotes evaluation.
(a list of ,(+ 2 3) elements)
;     ⇒ (a list of 5 elements)
(1 2 (3 ,(+ 4 5)))
;     ⇒ (1 2 (3 9))

;; With ,@, evaluated values can be spliced.
(setq some-list '(2 3))
;     ⇒ (2 3)
(1 ,@some-list 4 ,@some-list)
;     ⇒ (1 2 3 4 2 3)
;; The equivalent code without using ‘’ is often unreadable.
(cons 1 (append some-list '(4) some-list))
;     ⇒ (1 2 3 4 2 3)


### makunbound

This function empties out the value cell of symbol, making the variable void. It returns symbol.


(setq x 1)               ; Put a value in the global binding.
; 1

(let ((x 2))             ; Locally bind it.
(makunbound 'x)        ; Void the local binding.
x)
; error: Symbol's value as variable is void: x
x                        ; The global binding is unchanged.
; 1

(let ((x 2))             ; Locally bind it.
(let ((x 3))           ; And again.
(makunbound 'x)      ; Void the innermost-local binding.
x))                  ; And refer: it’s void.
; error: Symbol's value as variable is void: x

(let ((x 2))
(let ((x 3))
(makunbound 'x))     ; Void inner binding, then remove it.
x)                     ; Now outer let binding is visible.
; 2


## hooks


;; (add-hook HOOK FUNCTION &optional APPEND LOCAL)
; FUNCTION is added (if necessary) at the beginning of the hook list unless the optional argument APPEND is non-nil, in which case FUNCTION is added at the end.
; The optional fourth argument, LOCAL, if non-nil, says to modify the hook's buffer-local value rather than its global value.

;; (remove-hook HOOK FUNCTION &optional LOCAL)

;; In python.el.gz we could see:
;            'python-indent-post-self-insert-function nil 'local)
;; So in order to disable the python-indent after every self-insert-command:
(lambda ()
(setq indent-tabs-mode t)
(setq python-indent 4)
(setq tab-width 4)
(remove-hook 'post-self-insert-hook 'python-indent-post-self-insert-function 'local)
(set (make-local-variable 'indent-line-function) #'indent-relative)))


# Data structure

## Alists, Plists

• Alists: Association lists. Records a mapping from keys to values.
• Plists: Property lists.

;; Alist
((pine . cones)
(oak . acorns)
(maple . seeds))

;; Both the values and the keys in an alist may be any Lisp objects.
;; For example, in the following alist, the symbol ‘a’ is associated with
;; the number ‘1’, and the string ‘"b"’ is associated with the _list_ ‘(2
;; 3)’, which is the CDR of the alist element:
((a . 1) ("b" 2 3))

;; Plist
;; This property list associates ‘pine’ with ‘cones’, ‘numbers’ with ‘(1 2
;; 3)’, and ‘color’ with ‘"blue"’.  The property names and values can be
;; any Lisp objects, but the names are usually symbols (as they are in this
;; example).
(pine cones numbers (1 2 3) color "blue")

• In contrast to association lists, the order of the pairs in the property list is not significant, since the property names must be distinct.
• Property lists are better than association lists for attaching information to various Lisp function names or variables.
• Depending on your application, it may be faster to add an association to the front of an association list than to update a property. An association list may be used like a stack where associations are pushed on the front of the list and later discarded; this is not possible with a property list.

# Language Reference

## Loops

### while


(setq elFiles (directory-files "~/wcfShells/emacsEls/wcfModules" t ".+\.el\$"))
(while elFiles
(setq elFiles (cdr elFiles))
)


## Conditionals

if, when, unless, cond.



;;;;; if condition then-form else-forms...
(if nil
(print 'true)
'very-false)
⇒ very-false

;;;;; when condition then-forms...
; A variant of if where there are no else-forms, and possibly several then-forms.

(when condition a b c)
; is entirely equivalent to
(if condition (progn a b c) nil)

;;;;; unless condition forms...
; A variant of if where there is no then-form:

(unless condition a b c)
; is entirely equivalent to
(if condition nil
a b c)

;;;;; cond clause...
; cond chooses among an arbitrary number of alternatives. Each clause in the cond must be a list. The car of this list is the condition; the remaining elements, if any, the body-forms.
; cond tries the clauses in textual order, by evaluating the condition of each clause. If the value of condition is non-nil, the clause succeeds; then cond evaluates its body-forms, and returns the value of the last of body-forms. Any remaining clauses are ignored.

; The following example has four clauses, which test for the cases where the value of x is a number, string, buffer and symbol, respectively:

(cond ((numberp x) x)
((stringp x) x)
((bufferp x)
(setq temporary-hack x) ; multiple body-forms
(buffer-name x))        ; in one clause
((symbolp x) (symbol-value x)))

; Often we want to execute the last clause whenever none of the previous clauses was successful. To do this, we use t as the condition of the last clause, like this: (t body-forms). The form t evaluates to t, which is never nil, so this clause never fails, provided the cond gets to it at all. For example:
(setq a 5)
(cond ((eq a 'hack) 'foo)
(t "default"))
⇒ "default"

(if a b c)
;; Same as
(cond (a b) (t c))
`