We can avoid adding duplicate elements to a list variable by using add-to-list
.
When setting a customizable variable (i.e. one that is or will be defined by defcustom
) from our init file, we're encouraged to use custom-set-variables
(or customize-set-variable
) rather than setq
, to ensure the variable's setter function is run. This also correctly handles cases where the variable hasn't been defined yet, by "remembering" it and setting the desired value once defcustom
has defined the variable.
But if the customizable variable will hold a list, how should we add to it? I've been using this sort of thing:
(with-eval-after-load 'savehist
(add-to-list 'savehist-additional-variables
'evil-ex-history))
But I just read in the Elisp manual:
Normally, well-designed Lisp programs should not use with-eval-after-load. If you need to examine and set the variables defined in another library (those meant for outside use), you can do it immediately—there is no need to wait until the library is loaded.
Is there another way to handle adding to a customizable list variable that may not have been defined yet? For that matter, even if it has been defined, is there a recommended way to use the custom setter function when dealing with a list variable?