Find help in Emacs
Type M-x info, search for the elisp reference manual, then search for topics (such as backquote) within that.
(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.
- 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.
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."
- ' 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. (boundp 'abracadabra)) ; t (boundp 'abracadabra) ; Still globally void. ; nil (setq abracadabra 5) ; Make it globally nonvoid. ; 5 (boundp 'abracadabra) ; t
;; 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. 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
;; (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)))