Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programatically set a Custom variable in Emacs Lisp?

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.

like image 231
Ryan C. Thompson Avatar asked Aug 30 '13 23:08

Ryan C. Thompson


1 Answers

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.

like image 160
Drew Avatar answered Nov 19 '22 20:11

Drew