All notes


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.


; 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



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)

(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."




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.
  (boundp 'abracadabra))
; t
(boundp 'abracadabra)          ; Still globally void.
; nil

(setq abracadabra 5)           ; Make it globally nonvoid.
; 5
(boundp 'abracadabra)
; 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)


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.
; 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


;; (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:
;  (add-hook 'post-self-insert-hook
;            'python-indent-post-self-insert-function nil 'local)
;; So in order to disable the python-indent after every self-insert-command:
(add-hook 'python-mode-hook
  (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

;; 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")

Language Reference


while loop.

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


if, when, unless, cond.

;;;;; if condition then-form else-forms...
(if nil
    (print 'true)
  ⇒ 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))