In Emacs Lisp, how to check if a variable is defined?

In Emacs Lisp, how to check if a variable is defined?

+83
emacs lisp elisp
Apr 16 '09 at 18:58
source share
4 answers

you may need boundp: returns t if the variable (character) is not void; more precisely, if its current binding is not empty. It returns zero otherwise.

(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 
+127
Apr 16 '09 at 19:06
source share

In addition to dfa's answer, you can also see if it is related as a function using fboundp :

 (defun baz () ) => baz (boundp 'baz) => nil (fboundp 'baz) => t 
+41
Apr 17 '09 at 15:43
source share

If you want to check the value of a variable from emacs (I don’t know if this is applicable, since you wrote "in Emacs Lisp"?):

M-: launches Eval in the mini-buffer. Write the name of the variable and press return. The mini-buffer shows the value of the variable.

If the variable is not defined, you get a debugger error.

+4
Jun 29 '10 at 12:01
source share

Remember that variables that are nil are considered defined.

 (progn (setq filename3 nil) (boundp 'filename3)) ;; returns t (progn (setq filename3 nil) (boundp 'filename5)) ;; returns nil 
0
Jul 07 '18 at 5:28
source share



All Articles