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?
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.
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.
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.
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.
defvar
does exactly that. It assigns a value to a variable only if it's unbound.
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