Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in Emacs Lisp form to only set a variable if it is unbound?

Tags:

emacs

elisp

I've created the following macro in elisp. It will set the value "val" to a variable "var" only if the variable is unbound. This exists so variables set in your .emacs file do not get trampled over somewhere else.

(defmacro set-ifunbound (var val)
  `(if (not (boundp ',var))
       (setq ,var ,val)
     (identity ,var)))

Surely, this has to be a common pattern. Is there a built in way of doing the same thing?

like image 749
Chad Braun-Duin Avatar asked Sep 04 '11 17:09

Chad Braun-Duin


People also ask

How do you declare variables in Emacs?

To define a customizable variable, you should use defcustom (which calls defvar as a subroutine). See Variable Definitions. This special form defines symbol as a variable. Note that symbol is not evaluated; the symbol to be defined should appear explicitly in the defvar form.

What does setq mean in Lisp?

Sets the value of a symbol or symbols to associated expressions. (setq sym expr [sym expr]...) This is the basic assignment function in AutoLISP. The setq function can assign multiple symbols in one call to the function.

Is Emacs Lisp the same as Lisp?

Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.

What is SETQ in Emacs?

Special Form: (setq [symbol form]...) This special form is the most common method of changing a variable's value. Each SYMBOL is given a new value, which is the result of evaluating the corresponding FORM. The current binding of the symbol is changed. 'setq' does not evaluate SYMBOL; it sets the symbol that you write.


1 Answers

defvar does exactly that. It assigns a value to a variable only if it's unbound.

like image 184
Tom Avatar answered Sep 22 '22 17:09

Tom