[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!
However:
- (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:
"^#\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$"
- 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)))
nil)))))
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).
- Re: custom type `color' is not enforced, (continued)
- Re: custom type `color' is not enforced, Richard Stallman, 2007/12/19
- Re: custom type `color' is not enforced, Lennart Borgman (gmail), 2007/12/19
- RE: custom type `color' is not enforced, Drew Adams, 2007/12/20
- RE: custom type `color' is not enforced, Drew Adams, 2007/12/20
- Re: custom type `color' is not enforced, Richard Stallman, 2007/12/20
- RE: custom type `color' is not enforced, Drew Adams, 2007/12/21
- Re: custom type `color' is not enforced, Richard Stallman, 2007/12/20
- RE: custom type `color' is not enforced, Drew Adams, 2007/12/21
- Re: custom type `color' is not enforced, Lennart Borgman (gmail), 2007/12/21
- RE: custom type `color' is not enforced,
Drew Adams <=
- RE: custom type `color' is not enforced, Drew Adams, 2007/12/21
- Re: custom type `color' is not enforced, Richard Stallman, 2007/12/22
- Re: custom type `color' is not enforced, Lennart Borgman (gmail), 2007/12/22
- Re: custom type `color' is not enforced, Per Abrahamsen, 2007/12/23
- Re: custom type `color' is not enforced, Lennart Borgman (gmail), 2007/12/23
- Re: custom type `color' is not enforced, Per Abrahamsen, 2007/12/25