I would like to define a variable using defcustom so that it can be customized by users of an emacs package. The variable is an integer (let's call it spam-override), and its function is to override the value of another integer variable (spam). 0 is an allowed value of both spam and spam-override. If spam-override is set to 0, that means "override with the value 0" rather than "do not override". It seems natural to allow spam-override nil to indicate that the override is disabled.

I suspect that using nil in this way isn't how defcustom is best used for variables of this kind.

What's the best way to achieve this functionality? For example, is it usually best to use a composite defcustom type by using the choice constructor, or to define an extra boolean variable, maybe named override-spam to indicate whether integer variable spam-override should be used?

  • Re-reading the docs again I guess I want :type '(choice integer (const nil)) gnu.org/software/emacs/manual/html_node/elisp/… Commented Feb 3, 2019 at 11:55
  • You can provide that as an answer to your own question. You can also accept that answer, if it solves your problem and you don't see a better answer posted. Having an accepted answer for a question can be helpful to other readers.
    – Drew
    Commented Feb 3, 2019 at 22:07
  • Whether to use one user option with multiple kinds of value or two options, each with a different kind of value, is a stylistic or usability choice. It depends what you think is most useful/usable by your users. Other things being equal, and unless the sets of allowable values are complex or confusable, having fewer options is often a good idea. In general here, there is no "best" in general; there is what you think is best for your use.
    – Drew
    Commented Feb 3, 2019 at 22:10
  • Hi Drew, I left it for somebody else to answer to see if others agreed about good style here. My mutable opinion is that it's not just a matter of opinion: I think using nil in this particular case is better, if only because that's commonly used in emacs (and, less contingently, it reduces the problem of having a lot of variables to deal with). I had a moment where I thought "maybe that's only commonly used for non-customizable variables" because I didn't see a way to do it that worked nicely with customize... but it works fine. Commented Feb 9, 2019 at 10:15
  • ...but I take your point there might well be other cases where a different choice is better. Commented Feb 9, 2019 at 10:17

1 Answer 1


You've ascertained it correctly in your subsequent comment, but here's an example with labels for the options:

:type '(choice (integer :tag "Limit")
               (const :tag "Unlimited" nil))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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