In Emacs Lisp, how do I check if a variable is defined?
Common Lisp supports two kinds of variables: lexical and dynamic. These two types correspond roughly to "local" and "global" variables in other languages.
A variable is a name used in a program to stand for a value. In Lisp, each variable is represented by a Lisp symbol (see Symbols). The variable name is simply the symbol's name, and the variable's value is stored in the symbol's value cell8.
set-variable is an interactive command, meaning that you can type M-x set-variable RET to be interactively prompted for a variable name and value. setq is not an interactive command, meaning it's only suitable for writing in Emacs Lisp code.
By default, Common Lisp is lexically scoped, that is, every variable is lexically scoped except for special variables. By default, Emacs Lisp files are dynamically scoped, that is, every variable is dynamically scoped. The my-test. el is a lexically scoped file because of the first line.
you may want boundp: returns t if variable (a symbol) is not void; more precisely, if its current binding is not void. It returns nil 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
In addition to dfa's answer you may also want to see if it's bound as a function using fboundp:
(defun baz () ) => baz (boundp 'baz) => nil (fboundp 'baz) => t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With