1

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/… – Croad Langshan Feb 3 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 Feb 3 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 Feb 3 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. – Croad Langshan Feb 9 at 10:15
  • ...but I take your point there might well be other cases where a different choice is better. – Croad Langshan Feb 9 at 10:17
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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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