12
votes

In Emacs, some variables have special behaviors when set via M-x customize that do not get triggered when you set the same variable via setq. Is there a programmatic way to set such variables such that the special behavior will be triggered as if the user had set the variable through customize?

Also, is there a function to programmatically save the value to the user's custom file? I'm trying to implement a functionality to re-add the default elements to a list, without also removing the user's added elements.

2
Would you be looking for custom-set-variables? This is exactly what you normally have in your .emacs file. - nickie

2 Answers

11
votes

customize-set-variable is what you want, not custom-set-variables (no need for that here).

And in general it is a good idea to also take a look at the defcustom for the variable (user option), to see what its :set slot does etc. Not necessary, but good to know before you try to set the value programmatically.

This reply to a [email protected] question might also help: http://lists.gnu.org/archive/html/help-gnu-emacs/2013-08/msg00544.html


Update:

You later edited your question to also ask about saving the new value. For that you can use customize-save-variable. It sets and saves the new value.

0
votes

Using function default-value followed by mentioned custom-set-variable might do it.