14

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.

Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
  • 1
    Would you be looking for `custom-set-variables`? This is exactly what you normally have in your `.emacs` file. – nickie Aug 31 '13 at 00:25

2 Answers2

15

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 help-gnu-emacs@gnu.org 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.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • Yep, that's the one I wanted, precisely because it uses the `:set` property to set the variable. – Ryan C. Thompson Aug 31 '13 at 21:30
  • 2
    Noting for posterity: If you want to programmatically set a Custom variable and also save the new value for future sessions, `custom-save-variable` is the function you want; its signature is identical to that of `custom-set-variable`, and the only difference in its behavior is in also writing the new value as a `custom-set-variables` form in the user's customization file. – Aaron Miller Oct 24 '13 at 21:50
  • Why is `customize-set-variable` preferred to `custom-set-variables` in this case? – HelloGoodbye Feb 23 '19 at 20:27
  • @HelloGoodbye: 1. `custom-set-variables` does not act *"if the user had set the variable through customize"*. It both sets and saves the value. You could use it to answer the second question posed (later, as an update). 2. The generality of `custom-set-variables` and `custom-save-variables` is not needed here. You can of course use them, but you need not. There are in fact several `custom-*` and `customize-*` functions, depending on what you want to do. And the `customize-*` ones are also commands, so you can use them interactively. – Drew Feb 23 '19 at 21:27
  • @Drew What is the difference between setting and saving a value? In Emacs, the way to save a value is to add it to your `.emacs` file, right (this is at least what happens when you choose "Save Options" from the _Options_ menu)? So, if you use `customize-set-variable` in your `.emacs` file, isn't the value also essentially saved? – HelloGoodbye Feb 23 '19 at 21:34
  • @HelloGoodbye: Anything in a file is already "saved", yes. 1. But the (first) question was how to set a variable's value, getting the same behavior you get by setting it in the Customize UI. That's what `customize-set-variable` does. Setting a variable with Customize or with that command does *not* save the new value. The OP is asking a programming question; you are presuming a particular use context. 2. Customize does necessarily save to your init file. If you have defined `custom-file` with a value different from your init file then that's where it saves. – Drew Feb 23 '19 at 21:42
  • @Drew I'm slightly confused by your answer. First, you say that Customize doesn't save the new value, then you say that it saves it to your init file. Which one is it? Both cannot be true at the same time. – HelloGoodbye Feb 23 '19 at 21:56
  • @HelloGoodbye: Please reread what I wrote. **Setting** an option value in Customize does *not* save it. **Saving** a changed option value in Customize saves it (to your `custom-file` or, if none, to your init file). Do yourself a favor by consulting the relevant doc in the manuals, and even the relevant doc strings. – Drew Feb 24 '19 at 01:25
0

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

Andreas Röhler
  • 4,804
  • 14
  • 18