[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: custom type `color' is not enforced

From: Drew Adams
Subject: RE: custom type `color' is not enforced
Date: Fri, 21 Dec 2007 10:20:44 -0800

> > I agree it would be good to make `color' check for a valid color.
> > I hope someone will do that.
> Something like this?
> (defun color-digits-p (color)
>    (save-match-data
>      (string-match (rx bos "#"
>                        (or (repeat 3 3 hex-digit)
>                            (repeat 6 6 hex-digit))
>                        eos)
>                    color)))
> (defun widget-color-validate (widget)
>    (let ((val (widget-value widget)))
>      (unless (or (member val x-colors)
>                  (and (stringp val)
>                       (color-digits-p val)))
>        (widget-put widget :error (format "Invalid color: %S"
>                                          (widget-value widget)))
>        widget)))
> (define-widget 'color 'editable-field
>    "Choose a color name (with sample)."
>    :format "%{%t%}: %v (%{sample%})\n"
>    :size 10
>    :tag "Color"
>    :validate 'widget-color-validate
>    :value "black"
>    :complete 'widget-color-complete
>    :sample-face-get 'widget-color-sample-face-get
>    :notify 'widget-color-notify
>    :action 'widget-color-action)
> (defcustom test-color "black" "color test" :type 'color)

Thanks, Lennart!


- (defined-colors) should perhaps be used in place of `x-colors'.

- `color-digits-p' doesn't work for some legitimate colors, such as
"#FFFF0000FFFF". Any number of hex digits is legitimate, as long as it is a
multiple of 3 (RGB). FWIW, I use this regexp to check for an RGB string:


- Perhaps :size should be a bit larger? Some color names are longer than 10
chars. But I see that you took that and the rest of the `color' widget
definition from wid-edit.el: 10 is used in the Emacs definition of the
`color' widget.

Trying your fix made me realize that my type test was insufficient. I don't
understand widgets very well.

Instead of testing (widget-apply type :match val), it looks like I need to
test (and var-widget (not (widget-apply var-widget :validate))), where
var-widget is (get var 'custom-variable).

Here is what I use now; it seems to work (but suggestions are welcome). When
combined with your fix, it correctly distinguishes options whose values are
colors from those with arbitrary string values.

(defun help-var-is-of-type-p (variable types)
  "Return non-nil if VARIABLE is of one of the custom types in TYPES.
Non-nil means either VARIABLE's custom type is a member of list TYPES
or VARIABLE is bound and its value satisfies a type in list TYPES."
  (or (memq (get variable 'custom-type) types)
      (and (boundp variable)
           (let ((val (symbol-value variable))
                 (var-widget (get variable 'custom-variable)))
             (catch 'help-type-matches
               (dolist (type types)
                 (setq type (widget-convert type))
                 (when (and var-widget
                       (not (widget-apply var-widget :validate)))
                   (throw 'help-type-matches t)))

So `C-u C-h C-o' (my `describe-option-of-type') now shows, as completion
candidates, all options whose values are legitimate colors (and no others).

reply via email to

[Prev in Thread] Current Thread [Next in Thread]