Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

emacs

lisp

elisp

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

like image 577
mike Avatar asked Apr 16 '09 18:04

mike


People also ask

Does Lisp have variables?

Common Lisp supports two kinds of variables: lexical and dynamic. These two types correspond roughly to "local" and "global" variables in other languages.

What is Lisp variable?

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.

How do I set variables in Emacs?

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.

Is Emacs Lisp the same as Lisp?

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.


2 Answers

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 
like image 54
dfa Avatar answered Sep 27 '22 22:09

dfa


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 
like image 41
Jacob Gabrielson Avatar answered Sep 27 '22 22:09

Jacob Gabrielson