4

I have definition:

(defvar my/python-checker-alist
  '((pylint . (my/python-pylint-command my/python-pylint-args))
    (pep8 . (my/python-pep8-command my/python-pep8-args))
    (pyflakes . (my/python-pyflakes-command my/python-pyflakes-args)))
  "Known Python source code checkers.")

Default checker defined in:

(defcustom my/python-default-checker 'pyflakes
  "Default Python source code checker. See `my/python-checker-alist' for full alist."
  :group 'my/python
  :type 'symbol)

I would like to restrict user choice in M-x customize-group to only symbols from list:

'(pyflakes pylint pep8)

(1) How can I do this?

(2) What if I want to access alway up to date values? Can I specify function reference of lambda, like this:

(lambda () (mapcar 'car my/python-checker-alist))

2 Answers 2

8

Standard Info workflow

Here's how to get the info that you need:

  1. <f1> i to start Info.
  2. g (elisp) RET to select the Elisp node.
  3. i defcustom RET to search the index for defcustom.

Immediately you'll see:

‘:type TYPE’ Use TYPE as the data type for this option. It specifies which values are legitimate, and how to display the value (*note Customization Types::).

and:

‘:set SETFUNCTION’ Specify SETFUNCTION as the way to change the value of this option when using the Customize interface. The function SETFUNCTION should take two arguments, a symbol (the option name) and the new value, and should do whatever is necessary to update the value properly for this option (which may not mean simply setting the option as a Lisp variable); preferably, though, it should not modify its value argument destructively. The default for SETFUNCTION is ‘set-default’.

 If you specify this keyword, the variable’s documentation string
 should describe how to do the same job in hand-written Lisp code.

My shortcut for the above.

M-x counsel-info-lookup-symbol defcustom RET will bring you to the same info page.

Sample code for :type

(defcustom avy-keys-alist nil
  "Alist of avy-jump commands to `avy-keys' overriding the default `avy-keys'."
  :type '(alist
          :key-type (choice :tag "Command"
                     (const avy-goto-char)
                     (const avy-goto-char-2)
                     (const avy-isearch)
                     (const avy-goto-line)
                     (const avy-goto-subword-0)
                     (const avy-goto-subword-1)
                     (const avy-goto-word-0)
                     (const avy-goto-word-1)
                     (const avy-copy-line)
                     (const avy-copy-region)
                     (const avy-move-line))
          :value-type (repeat :tag "Keys" character)))

Sample code for :set

(defcustom ediff-diff-options
  (if (memq system-type '(ms-dos windows-nt)) "--binary" "")
  "Options to pass to `ediff-diff-program'.
If Unix diff is used as `ediff-diff-program',
then a useful option is `-w', to ignore space.
Options `-c', `-u', and `-i' are not allowed. Case sensitivity can be
toggled interactively using \\[ediff-toggle-ignore-case].
Do not remove the default options. If you need to change this variable, add new
options after the default ones.
This variable is not for customizing the look of the differences produced by
the command \\[ediff-show-diff-output]. Use the variable
`ediff-custom-diff-options' for that."
  :set 'ediff-set-diff-options
  :type 'string
  :group 'ediff-diff)
3
  • 1
    Thanks for help. I already read info topics but never have experience and so didn't able to combine that knowledge. +1
    – gavenkoa
    Commented Feb 16, 2016 at 16:36
  • (Or if you are in a buffer with Emacs-Lisp mode, just C-h S defcustom, to get to its doc in the Elisp manual.)
    – Drew
    Commented Feb 16, 2016 at 16:52
  • I can't see how this addresses the Q which is how to select from a list of values. we have to hard code multiple options?
    – RichieHH
    Commented Apr 29, 2021 at 17:17
0

I added:

PROPOSAL: Is there a way to update available choices for defcustom variable?

http://debbugs.gnu.org/cgi/bugreport.cgi?archive=yes&bug=22703

Seems that defcustom API doesn't reevaluate :type.

As proposal I ask about accepting function (lambda) as value of :type. In that case :type may be evaluated when widget build on M-x customize-XXX

3
  • 1
    I think the restricted-sexp type can satisfy this requirement
    – npostavs
    Commented Feb 16, 2016 at 18:41
  • I didn't completely understand purpose of restricted-sexp. It has only one active parts: predicates applied over user input. It doesn't make actual choice for user. but rather verify user input.
    – gavenkoa
    Commented Feb 16, 2016 at 19:12
  • oh, I misunderstood what you meant by "function as value of :type".
    – npostavs
    Commented Feb 16, 2016 at 21:39

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.