1

Should I use custom-set-variables (as opposed to setq) when setting a variable that returns non-nil for custom-variable-p but was not defined by defcustom?

For example, echo-keystrokes is defined in keyboard.c via defvar_lisp, but the following returns non-nil:

(custom-variable-p 'echo-keystrokes)

The documentation for custom-variable-p states:

A customizable variable is either (i) a variable whose property list contains a non-nil ‘standard-value’ or ‘custom-autoload’ property, or (ii) an alias for another customizable variable.

This leaves me wondering if the conventional advice to prefer custom-set-variables over setq when dealing with custom variables (since they may have special setters and/or initialization code) applies to such variables, or whether it's exclusive to variables defined via defcustom.

If I should only bother with custom-set-variables when dealing with those defined by defcustom, how can I quickly make that determination? I was hoping to use custom-variable-p to programmatically check which variables I should set with custom-set-variables instead of setq, as I'd rather not have to dig through library source code for every variable I set.

  • 1
    You need not use custom-set-variables. Do you also consider it more of a bother to use customize-set-variable' or customize-set-value` than to use setq? (If so, why?) – Drew Apr 15 '17 at 0:19
  • @Drew In addition to its conciseness, I find setq to be more straightforward, especially when eval-when-compile starts getting involved. If I want to set multiple variables in one go, I need to construct quoted lists around my variables and values (which get eval'd). I haven't tried customize-set-variable, partly because it can't set multiple variables in one call. I didn't know about customize-set-value. I don't think I understand the difference between that and customize-set-variable, even after reading the docs. – ivan Apr 15 '17 at 0:42
  • I guess another question on my mind as I navigate the ins and outs of configuring Emacs is how reasonable is it to selectively ignore the best-practice and just use setq when dealing with custom variables that don't make use of special setters or initialization code (which seems to be most of them)? – ivan Apr 15 '17 at 0:47
  • 2
    It's generally fine to use setq if there are no setters etc. Most abuses of setq for options, which can get people into trouble, come from complete ignorance of the possibility of setters etc., not from knowing about them and not worrying about them. IOW, people just get in the habit of using setq, and most of the time they are not surprised. – Drew Apr 15 '17 at 3:52
  • @Drew It is fine that you discussed the depths of initialization of variables. But, now it is time for an accepted answer. I think your discussion of getters and setters is quite suitable for that. – Tobias Mar 20 '18 at 21:42
3

Use customize-set-variable or custom-set-variables, depending on just what you want/need.

Both of those make use of any initialization or setter functions (and so on) with which the user option might have been defined. See (elisp) Variable Definitions for information about these things.

If a given user option is defined without specifying any particular initialization, setter function then it is typically OK to simply use setq (or setf) to set its value.

But if the option has a setter function then do not use setq, as it will not do all that is needed.

Most abuses of setq for options, which can get people into trouble, come from complete ignorance of the possibility of setters etc., not from knowing about them and not worrying about them. IOW, people just get in the habit of using setq, and most of the time they are not surprised.

A setter function does something in addition to setting the option value. It may set the value in a special way, or it may also change other things in Emacs (e.g., the values of other variables) - things that need to go hand in hand with changing the option value.

  • I don't believe customize-set-value makes use of setter functions. Called as (customize-set-value VARIABLE VALUE) it's no different to (set VARIABLE VALUE). – phils Mar 21 '18 at 1:52
  • ...which is unfortunate, to my mind. IIRC using customize-set-variable will result in updates to your custom-file, the same as using the customize UI would do. I feel there ought to be a setq analog for user options which doesn't affect the custom-file, but otherwise plays nicely with customize. (I recall thinking that customize-set-value was exactly that, until I looked at the code.) – phils Mar 21 '18 at 2:06
  • @phils: Yeah, I kinda remembered it like that too, but was too lazy to confirm (almost hesitated to do so, but was in a hurry). I've made that correction, so as not to lead people astray. Thx. – Drew Mar 21 '18 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.